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.
The explanation is very helpful. For those implementing it in Python ... there is no Java like treeset in Python. A naive workaround is to use the bisect module which seems fast enough to solve this challenge:
frombisectimportinsort,bisect_rightdefmaximumSum(a,m):# Create prefix treeprefix=[0]*len(a)curr=0;foriinrange(len(a)):curr=(a[i]%m+curr)%mprefix[i]=curr# Compute max modsumpq=[prefix[0]]maxmodsum=max(prefix)foriinrange(1,len(a)):# Find cheapest prefix larger than prefix[i]left=bisect_right(pq,prefix[i])ifleft!=len(pq):# Update maxmodsum if possiblemodsum=(prefix[i]-pq[left]+m)%mmaxmodsum=max(maxmodsum,modsum)# add current prefix to heapinsort(pq,prefix[i])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 →
The explanation is very helpful. For those implementing it in Python ... there is no Java like treeset in Python. A naive workaround is to use the bisect module which seems fast enough to solve this challenge: