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.
Simplest way I did it was to search the 2 elements using binary search in a sorted array, and then finding these 2 elements using linear search.
Managing duplicates is a little tricky, I just incremented the second index by 1 if they both indexes are equal.
There may be a more efficient solution.
intbinS(vector<int>arr,intl,intr,intx){while(l<=r){intm=(l+r)/2;if(arr[m]==x)return1;// return true if foundif(arr[m]<x)l=m+1;elser=m-1;}return-1;}vector<int>icecreamParlor(intm,vector<int>arr){vector<int>orig=arr;vector<int>res;intleft,right,find,equal=0,found1=0,found2=0;sort(arr.begin(),arr.end());for(inti=0;i<arr.size()-1;i++){left=arr[i];find=abs(m-arr[i]);right=binS(arr,i,arr.size(),find);if(right!=-1){if(left==find)equal=1;elseequal=0;for(inti=0;i<orig.size();i++){if(orig[i]==left){res.push_back(i+1);found1=1;}if(orig[i]==find){res.push_back(i+1+equal);// increment second index by 1 if they are of the same elementfound2=1;}if(found1&&found2)// break loop if both are foundbreak;}break;}elsecontinue;}returnres;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Ice Cream Parlor
You are viewing a single comment's thread. Return to all comments →
Spoiler Answer (Please try solving it yourself)
Simplest way I did it was to search the 2 elements using binary search in a sorted array, and then finding these 2 elements using linear search.
Managing duplicates is a little tricky, I just incremented the second index by 1 if they both indexes are equal.
There may be a more efficient solution.