You are viewing a single comment's thread. Return to all comments →
C++ Solution. Using set.upperbound() function for O(nlogn) performance:
long long maximumSum(vector<long long> &a, long long m) { long long max=0, sum=0; set<long long> s; for (auto i=a.begin() ; i!=a.end() ; i++) { sum=(sum+*i)%m; if (sum>max) max=sum; auto b=s.upper_bound(sum); if (b!=s.end()) { long long possiblemax=(sum-*b+m)%m; if (possiblemax>max) max=possiblemax; } s.insert(sum); } return max; }
Seems like cookies are disabled on this browser, please enable them to open this website
Maximum Subarray Sum
You are viewing a single comment's thread. Return to all comments →
C++ Solution. Using set.upperbound() function for O(nlogn) performance: