maximum subarray sum modulo m[Please urgent help]

Revision en2, by acash, 2019-07-12 13:15:04

I was trying to solve a problem problem link

This problem is also on hackerrank

I am failing in some test cases I used the approach given in above link

MY solution

Method (efficient approach): The idea is to compute prefix sum of array. We find maximum sum ending with every index and finally return overall maximum. To find maximum sum ending at index at index, we need to find the starting point of maximum sum ending with i. Below steps explain how to find the starting point.

Let prefix sum for index i be prefixi, i.e., prefixi = (arr[0] + arr[1] + .... arr[i] ) % m

Let maximum sum ending with i be, maxSumi. Let this sum begins with index j.

maxSumi = (prefixi — prefixj + m) % m

From above expression it is clear that the value of maxSumi becomes maximum when prefixj is greater than prefixi and closest to prefix[i We mainly have two operations in above algorithm.

1)Store all prefixes. 2)For current prefix, prefix[i], find the smallest value greater than or equal to prefix[i] + 1. For above operations, a self-balancing-binary-search-trees like AVL Tree, Red-Black Tree, etc are best suited. In below implementation we use set in STL which implements a self-balancing-binary-search-tree.

#include <bits/stdc++.h>

using namespace std;

// Complete the maximumSum function below.
long maximumSum(vector<long> &a, long m) {
set<long> seen;
//prefix[0]=a[0]%m;
long sum = a[0]%m;
long ans=0;
long maxx = 0;
for(long i=1;i<a.size();i++){
sum = (sum%m + a[i]%m)%m;
maxx = max(sum,maxx);
auto it  =  seen.lower_bound(sum+1);
if(it!=seen.end()){
ans = max(ans,(sum-(*it)+m));
}
seen.insert(sum);
}
return max(ans,maxx);

}

int main(){
long n,k;
long q;
cin>>q;
while(q--){
cin>>n>>k;
vector<long> a;
for(long i=0;i<n;i++){
long u;
cin>>u;
a.push_back(u);
}
cout<<maximumSum(a,k)<<endl;
}
}


#### History

Revisions

Rev. Lang. By When Δ Comment
en2 acash 2019-07-12 13:15:04 1093
en1 acash 2019-07-12 12:31:34 1178 Initial revision (published)
{"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "https://codeforces.com/topic/68775/en2", "fetch_time": 1576438966000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2019-51/segments/1575541309137.92/warc/CC-MAIN-20191215173718-20191215201718-00070.warc.gz", "warc_record_offset": 317043543, "warc_record_length": 14134, "token_count": 584, "char_count": 2056, "score": 3.703125, "int_score": 4, "crawl": "CC-MAIN-2019-51", "snapshot_type": "latest", "language": "en", "language_score": 0.6524291634559631, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00004-of-00064.parquet"}