We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
longmaxModSum=0;longcurrentSum=0;// SortedSet to store prefix sums modulo kSortedSet<long>modSet=newSortedSet<long>();modSet.Add(0);foreach(longnumina){currentSum=(currentSum+num)%k;maxModSum=Math.Max(maxModSum,currentSum);// Find the smallest prefix greater than currentSumvarhigher=modSet.GetViewBetween(currentSum+1,k).Min;if(higher!=default(long)){maxModSum=Math.Max(maxModSum,(currentSum-higher+k)%k);}modSet.Add(currentSum);}returnmaxModSum;
Cookie support is required to access HackerRank
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 →
This is my idea