Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6359565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:32:07+00:00 2026-05-24T23:32:07+00:00

Below is the program I wrote to find sum of a subarray from given

  • 0

Below is the program I wrote to find sum of a subarray from given array, however somehow I am not getting how can I get rid of the sentinel value (-32767 in this case)? and how can I optimise it?
and how can I keep track of range of max subarray?

#define EVALUE -32767

using namespace std;

int findMaxSubArray(vector<int>,int low,int high);
int findMaxSubArray_Mid(vector<int>,int low,int high);

int main()
{
    vector<int> v;
    int j=0;

    cout << "Enter array values(-32767 to end): ";
    while(1)
    {
        cin >> j;
        if (EVALUE==j)
            break;
        v.push_back(j);
    }

if(v.size()!=0)
    cout << "Max sum is: " << findMaxSubArray(v,0,v.size()-1) << "\n";
else
    cout << "No array elements entered, exiting...\n";

system("pause");
return 0;
}

int findMaxSubArray(vector<int> v, int low, int high)
{
    if(low==high) return v[low];

    int max_mid_sum=findMaxSubArray_Mid(v,low,high);
    int max_left_sum=findMaxSubArray(v,low,(low+high)/2);
    int max_right_sum=findMaxSubArray(v,(low+high)/2+1,high);

    if (max_mid_sum>max_left_sum) return (max_mid_sum>max_right_sum?max_mid_sum:max_right_sum);
    else return(max_left_sum>max_right_sum?max_left_sum:max_right_sum);
}

int findMaxSubArray_Mid(vector<int> v,int low,int high)
{
    int mid=high/2;
    int max_left_sum=0;
    int max_right_sum=0;
    int sum=0;

    for(int i=mid;i>=low;--i)
    {
        sum+=v[i];
        if(sum>max_left_sum)    
        {
            max_left_sum=sum;
        }
    }

    sum=0;
    for(int i=mid+1;i<=high;++i)
    {
        sum+=v[i];
        if(sum>max_right_sum)   
        {
            max_right_sum=sum;
        }
    }

    return (max_right_sum+max_left_sum);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T23:32:07+00:00Added an answer on May 24, 2026 at 11:32 pm

    When reading from a textfile, the last character that cin will get is the “EOF” character, or end of file character. You can send this character to your program in the command line with control+d. You’re going to want to check for this rather than -32767.

    This is a basic program that should identify a simple fix for your problem:

    #include <vector>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        vector<int> v;
        int j;
    
        cout << "Enter array values (Control+D (EOF) to end): ";
        cin >> j;
        while(cin.good())
        {
            v.push_back(j);
            cin >> j;
        }
    
        return 0;
    }
    

    If you want to get really smart you can use the below and it will directly insert the contents of the memory at cin (from the beginning until EOF) into your vector. As far as running time goes, this will probably be faster than your solution and the above solution.

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <iterator>
    
    using namespace std;
    
    int main()
    {
        vector<int> v;
        cout << "Enter array values (Control+D (EOF) to end): ";
    
        istream_iterator<int> in(cin);
        istream_iterator<int> eof;
        copy(in, eof, back_inserter(v));
    
        ostream_iterator<int> out(cout, "\n");
        copy(v.begin(), v.end(), out);
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to read and print data from a file. I wrote the program
I have to write a program that read from a file that contains the
I installed Java 1.7.0 in the following folder C:\Program Files\Java . My operating system
I have wrote an application that syncs two folders together. The problem with the
I'm trying to use mtrace to detect memory leaks in a fortran program. I'm
It's the weekend, so I relax from spending all week programming by writing a
My objective is to take directions from a user and eventually a text file
I have a large collection of documents scanned into PDF format, and I wish
I am using oracle 11g. I write code to connect oracle database with java
I have been attempting to enhance my GUI system written in Java to use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.