Maximun Subarray Sum Challenge

Didn’t understand the input format of the problem though I’ve implemented the algorithm required to solve this problem correctly.
Code:-

            #include<iostream>
        using namespace std;
        int main() {
        	int arr[100];
            int n;
            cin>>n;
            int currSum=0;
            int maxSum=0;
            
            for(int i=0; i<n; i++){
                cin>>arr[i];
            }
            for(int i=0;i<n;i++){
                currSum = currSum + arr[i];
                if(currSum < 0){
                    currSum=0;
                }
                maxSum=max(currSum,maxSum);
            }
            cout<<maxSum<<endl;
            return 0;
        }

@prashantverma.vn the logic you have used is absolutely fine, but you have written the code according to one test case only. The user can input multiple test cases as well, ergo, your code would have failed in that situation. Also, the size of the arr you have taken is only 100, what if the user entered the value of n much larger than n. So I have modified your code a little bit. You can see the modified code here :

Hope this helps.

1 Like

Okayy…thanxx for the help…

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.