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.
// k = 4, s = [1 1 1 2 2 3 3 3 3 4 4]// only 3 cases:// 1) we have 4 in s, we can take only one// 2) we can take only one 2, if it exists// 3) we can take 1 or 3, but not together, // 4 of "3" is bigger than 3 of "1", we take 4 of "3"intres=0;int[]modK=newint[k];for(intel:s){modK[el%k]++;}// we can take only one 4 if it existsif(modK[0]>0){res=1;}// start with 1 because we counted modK[0] beforefor(inti=1;i<k;i++){// if no numbers, just missif(modK[i]==0){continue;}// we can take only one 2 if it existsif(2*i==k){res++;}else{// we take 4 of "3", because it is bigger than 3 of "1"res+=Math.max(modK[i],modK[k-i]);// make them zero, not to count twicemodK[i]=0;modK[k-i]=0;}}returnres;
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Non-Divisible Subset
You are viewing a single comment's thread. Return to all comments →