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.
Basically fetches the complement in the corresponding bucket adding to the result whatever is stored there. Then adds 1 to the bucket that corresponds to the remainder of the input.
Taking the provided example:
63132612Input:1INITIALSTATE:Bucket[0]:0;Bucket[1]:0;Bucket[2]:0Count:0Remainderr:1Complement:3-r=2count+=Bucket[2]:0bucket[1]++Input:3INITIALSTATE:Bucket[0]:0;Bucket[1]:1;Bucket[2]:0Count:0Remainderr:0Complement:3-r=3->0//(3 gets switched for 0 =) ).count+=bucket[0]:0bucket[0]++Input:2INITIALSTATE:Bucket[0]:1;Bucket[1]:1;Bucket[2]:0Count:0Remainderr:2Complement:3-r=1count+=bucket[1]:1bucket[2]++Input:6INITIALSTATE:Bucket[0]:1;Bucket[1]:1;Bucket[2]:1Count:1Remainderr:0Complement:3-r=3->0count+=bucket[0]:1bucket[0]++Input:1INITIALSTATE:Bucket[0]:2;Bucket[1]:1;Bucket[2]:1Count:2Remainderr:1Complement:3-r=2count+=bucket[2]:1bucket[1]++Input:2INITIALSTATE:Bucket[0]:2;Bucket[1]:2;Bucket[2]:1Count:3Remainderr:2Complement:3-r=1count+=bucket[1]:2bucket[2]++FINALSTATE:Bucket[0]:2;Bucket[1]:2;Bucket[2]:2Count:5
Hopefully I made no mistakes in the example run ;)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Divisible Sum Pairs
You are viewing a single comment's thread. Return to all comments →
For those wondering about a Java solution. The workings are pretty much the same as has been posted before in regards to an O(n) solution:
Basically fetches the complement in the corresponding bucket adding to the result whatever is stored there. Then adds 1 to the bucket that corresponds to the remainder of the input.
Taking the provided example:
Hopefully I made no mistakes in the example run ;)