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.
or you can click on the image too to follow youtube tutorial.
Here is the 100% working solution:-
source code :
// approach1- using hashmapstaticint[]icecreamParlorA1(intm,int[]arr){intresult[]=newint[2];Map<Integer,Integer>map=newHashMap<>();for(inti=0;i<arr.length;i++){intx=arr[i];inty=m-x;Integerj=map.get(y);if(j!=null){result[0]=j+1;result[1]=i+1;break;}map.put(x,i);}returnresult;}// approach2 - using arraystaticint[]icecreamParlorA2(intm,int[]arr){intresult[]=newint[2];intn=arr.length;intfrequency[]=newint[10001];Arrays.fill(frequency,-1);for(inti=0;i<n;i++){intx=arr[i];inty=m-x;if(y>=0){intj=frequency[y];if(j!=-1){result[0]=j+1;result[1]=i+1;break;}}frequency[x]=i;}returnresult;}// approach3- editorial approach with explanationstaticint[]icecreamParlorA3(intm,int[]arr){intresult[]=newint[2];intn=arr.length;intfrequency[]=newint[10001];intfirstFlavourIndex=0;intsecondFlavourIndexStartsFrom=0;intsecondFlavourCost=0;for(inti=0;i<n;i++){intpriceIndex=arr[i];frequency[priceIndex]++;}for(inti=0;i<n;i++){intfirstFlavourCost=arr[i];frequency[firstFlavourCost]--;secondFlavourCost=m-firstFlavourCost;if(secondFlavourCost>0&&frequency[secondFlavourCost]>0){firstFlavourIndex=i;secondFlavourIndexStartsFrom=firstFlavourIndex+1;result[0]=firstFlavourIndex+1;break;}}for(inti=secondFlavourIndexStartsFrom;i<n;i++){if(arr[i]==secondFlavourCost){result[1]=i+1;break;}}returnresult;}
Would really appreciate your feedback like, dislike , comment etc. on my video.
Ice Cream Parlor
You are viewing a single comment's thread. Return to all comments →
Hello friends,
In this video, I have solved hackerrank Ice Cream Parlor problem in an easy way which is based on Two Sum LeetCode problem in** O(n) time**.
Here I have explained three optimal approaches to solve this problem. Two sum with hashmap or without hashmap.
click here for the video explanation of generic algorithm with complexity analysis.
or you can click on the image too to follow youtube tutorial.
Here is the 100% working solution:-
source code :
Would really appreciate your feedback like, dislike , comment etc. on my video.
Do not forget to upvote, if you find it useful.