Skip to content

Solution submitted for test-03 Maximum Subarray Sum is Wrong #28

@pptale

Description

@pptale

Chaitanya Kolhe thinks that the solution to the problem "test-03 Maximum Subarray Sum" uploaded on GitHub is incorrect. He also provided the following code.

#include<bits/stdc++.h>
#include
#define ll long long
#define pb push_back

using namespace std;

ll solve(int t){
vector arr;
for (int i=0; i<t; i++){
ll temp;
cin >> temp;
arr.pb(temp);
}

if(*max_element(arr.begin(), arr.end()) <= 0){
    return *max_element(arr.begin(), arr.end());
}
else{
    ll sum=0, max_sum=0;
    for (int i=0; i<t; i++){
        sum+=arr[i];
        max_sum = max(sum, max_sum);

        if (sum<0){
            sum=0;
        }
    }
    return max_sum;
}

}

int main(){
int t;
cin >> t;
ll ans;
ans = solve(t);
cout << ans << endl;

return 0;

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions