Hey Every one, If you find this tutorial helpful, please do not forget to like, comment, share and It would be great if you can leave your feedback about the tutorial, as I have put a lot of hard work to make things easy for you. Thanks ..!!
I watched this video for first 10 mins and you have got yourself a subscriber. Thanks for making such well explained tutorials. This is a quality stuff.
I solved this solution in O(n^2) in 10mins and after spending good 5-6 hours I came here, just watching your explanation helps so much. Without your videos, a lot of concepts would have never cleared for me. Thanks a ton. I did not check the midValue % r == 0 check and that wasted my so much time :P
Great explanation with optimized code 👍. Beginners also can understand easily and they can code without seeing code logic, because of your clear visual logic explanation. Great work.
I had to sign in to subscribe this....it was worth... This stuff is amazing....thanks Kanhaiya...thanks JAVAAID...please make more such videos...cheers
Great explanation - how do you come up with a solution like this? I would never think to use 2 hashmaps for storing i and k. Is it intuitive after a lot of practice with problems like these?
sir, please try to solve any kind of problem don't read the question because people are here after reading the question so directly switch on concept thanks for explaining
most of the people will not be able to solve the problem because problem statement it self its not clear. so to make the problem statement clear, we have to go through the problem statement. if you are clear with problem statement, you can always skip this and move ahead. but i would not recommend this because while going over problem statement also we will keep on telling some important stuff which you should not miss.
Hi Brother I have followed the same approch but still Testcase 3 and 6 are failing. static long countTriplets(List arr, long r) { HashMap rightMap=new HashMap(); HashMap leftMap=new HashMap(); long count=0; for(Long l:arr){ rightMap.put(l,rightMap.get(l)!=null?rightMap.get(l)+1:1); } //System.out.println(rightMap); for(Long l: arr){ int lCount=0; int rCount=0; rightMap.put(l,rightMap.get(l)>0?rightMap.get(l)-1:0); if(leftMap.get(l/r)!=null){ lCount+=leftMap.get(l/r); } if(rightMap.get(l*r)!=null){ rCount+=rightMap.get(l*r); } count+=(lCount*rCount); //rightMap.put(l,rightMap.get(l)-1); leftMap.put(l,leftMap.get(l)!=null?leftMap.get(l)+1:1); } return count; }
I have corrected your code but will suggest whenever you face such issues please watch the tutorial again and cross verify your code against our code, it will help you to improve your debugging skills. Here is the working code- (and now you need to find out what correction i made, its an exercise for you.. 😜 static long countTriplets(List arr, long r) { HashMap rightMap=new HashMap(); HashMap leftMap=new HashMap(); long count=0; for(Long l:arr){ rightMap.put(l,rightMap.get(l)!=null?rightMap.get(l)+1:1); } //System.out.println(rightMap); for(Long l: arr){ long lCount=0; long rCount=0; rightMap.put(l,rightMap.get(l)>0?rightMap.get(l)-1:0); if(leftMap.get(l/r)!=null && l % r == 0){ lCount=leftMap.get(l/r); } if(rightMap.get(l*r)!=null){ rCount=rightMap.get(l*r); } count+=(lCount*rCount); //rightMap.put(l,rightMap.get(l)-1); leftMap.put(l,leftMap.get(l)!=null?leftMap.get(l)+1:1); } return count; }
Those looking for the python version: from collections import Counter ``` # Complete the countTriplets function below. def countTriplets(arr, ratio): cnt = 0 future = Counter(arr) past = Counter() for present in arr: # remove self from the future future[present] -= 1 # check for mentions of ratios before = present / ratio after = present * ratio # now get the amount cnt += past[before] * future[after] # add self to the past past[present] += 1 return cnt ```
@@JavaAidTutorials I am also finding it difficult to come up with such time efficient solutions. Trying to be perseverant in learning your logical reasoning and approach, but not sure to what extent I'll be able to reproduce it at the right time.
Implementation not working if I sort the array . Any hint why ? function countTriplets(arr, r) { arr = arr.sort((a,b) => a-b); // WITHOUT THIS IT WORKS let count = 0; let lMap = {}; let rMap = {}; // Build rMap; for(let i = 0; i < arr.length; i++){ const key = arr[i]; if(!rMap[key]) rMap[key] = 0; if(!lMap[key]) lMap[key] = 0; rMap[key]++; } for(let i = 0; i < arr.length; i++){ let b = arr[i]; let a = 0, c = 0; rMap[b]--; if(lMap[b/r]){ a = lMap[b/r] } if(rMap[b*r]){ c = rMap[b*r]; } count += a*c; lMap[b]++; } return count; }
If you sort the array then how you will come to know the exact i,j and k index position. which element was at which place..? that's why sorting will spoil the game. whenever the index has its own importance in the problem statement, you can use sorting generally.