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.
I feel like this solution should work, and it appears to be doing what I think it's doing, but it only passes some of the tests. I'd be interested in knowing if anybody sees what's theoretically wrong with it.
publicstaticlongsolve(List<Integer>arr){arr.sort(null);// assuming ok to mutate - easy to copy if notintmaximum=arr.get(arr.size()-1);longcount=0;for(inti=0;i<arr.size();i++){intnextVal=arr.get(i);intcutoff=maximum/nextVal+1;//exclusiveif(cutoff<=nextVal){break;}intcutoffIndex=Collections.binarySearch(arr,cutoff);//exclusiveif(cutoffIndex<0){cutoffIndex=-(cutoffIndex+1);}else{// seek to beginning of range of same valuewhile(cutoffIndex>=0&&arr.get(cutoffIndex)==cutoff){--cutoffIndex;}++cutoffIndex;}count+=cutoffIndex-i-1;}returncount;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Array Pairs
You are viewing a single comment's thread. Return to all comments →
I feel like this solution should work, and it appears to be doing what I think it's doing, but it only passes some of the tests. I'd be interested in knowing if anybody sees what's theoretically wrong with it.