Тёмный

Count Triplets Hackerrank Solution | Interview Preparation Kit 

JAVAAID - Coding Interview Preparation
Подписаться 37 тыс.
Просмотров 37 тыс.
50% 1

Опубликовано:

 

11 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 91   
@JavaAidTutorials
@JavaAidTutorials 4 года назад
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 ..!!
@TheCrimemas
@TheCrimemas 4 года назад
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.
@JavaAidTutorials
@JavaAidTutorials 4 года назад
Awesome, thank you! :)
@darkmozila8661
@darkmozila8661 4 года назад
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
@JavaAidTutorials
@JavaAidTutorials 4 года назад
Glad it helped!
@alisultan6459
@alisultan6459 3 года назад
bruh this some genius shit right here
@williamphillips7665
@williamphillips7665 4 года назад
That was a really good explanation!! Please continue making these videos!
@JavaAidTutorials
@JavaAidTutorials 4 года назад
Thanks a lot for the feedback 😊. Sure, we are trying our best
@sayantanray9595
@sayantanray9595 4 года назад
Nice explanation and solution, mine 4 cases was failing for timeout and your approach helped!!!!
@JavaAidTutorials
@JavaAidTutorials 4 года назад
Thanks for your feedback 😊 I have seen your comments on multiple videos.I really appreciate it.
@krishna-nakum
@krishna-nakum 4 года назад
hello sir , i have watched many of your videos and learned , you are great in problem solving.
@JavaAidTutorials
@JavaAidTutorials 4 года назад
thanks but i don't think so..still need to learn a lot from you all 😀
@AbhishekSharma-wy1sx
@AbhishekSharma-wy1sx 4 года назад
you are life saver man..God bless you.
@JavaAidTutorials
@JavaAidTutorials 4 года назад
Thank you ..:)
@lokeshraj6648
@lokeshraj6648 4 года назад
Hey JAVAAID, thank you so much for making such explanatory video. I have been looking for such a video. Thank you so much.
@JavaAidTutorials
@JavaAidTutorials 4 года назад
Glad it was helpful!
@lokeshraj6648
@lokeshraj6648 4 года назад
Can you please tell me which notepad/editor you used during explanation. I have been looking for such software from some time. Can you help me in it?
@vijayalakshmi8337
@vijayalakshmi8337 4 года назад
Amazing. I read a lot of articles about this solution but yours is spot on! Thanks for this.
@JavaAidTutorials
@JavaAidTutorials 4 года назад
thanks for your feedback 🙂 If you find this channel helpful , please do share with others.
@Ardolynk
@Ardolynk Год назад
Thanks a lot! My problem was the count of triplets could be looooooooooong 😲
@sanketkale730
@sanketkale730 4 года назад
Really helpful....Thanx for making it look very simple!!
@JavaAidTutorials
@JavaAidTutorials 4 года назад
Thanks for your feedback 😊
@yosuasimanjuntak2822
@yosuasimanjuntak2822 4 года назад
I was using the logic without HashMap, but I got failed in several cases. Thanks for the explanation.
@JavaAidTutorials
@JavaAidTutorials 4 года назад
Glad it helped!
@vinothkannans1910
@vinothkannans1910 3 года назад
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.
@JavaAidTutorials
@JavaAidTutorials 3 года назад
Glad it was helpful!
@AnilBM99
@AnilBM99 4 года назад
Thanks brother.. I was really looking for hackerrank solutions.. thanks a lot
@JavaAidTutorials
@JavaAidTutorials 4 года назад
Most welcome..😊
@rohitmanohar2733
@rohitmanohar2733 3 года назад
Nice explanation , thank you
@JavaAidTutorials
@JavaAidTutorials 3 года назад
Glad it was helpful!
@arsalalam5998
@arsalalam5998 3 года назад
thnak you for the solution. was struggling with one map.
@JavaAidTutorials
@JavaAidTutorials 3 года назад
Keep learning , keep coding :)
@tuang4399
@tuang4399 4 года назад
You need to improve the sound quality. It will be easier for us to understand the problem. btw thanks a lot!
@JavaAidTutorials
@JavaAidTutorials 4 года назад
I will try my best
@ankurgupta4696
@ankurgupta4696 4 года назад
awsm explantion i understood the concept very clearly
@JavaAidTutorials
@JavaAidTutorials 4 года назад
thank you.🙂
@dangerzone8491
@dangerzone8491 4 года назад
Thanks brother you have given me hope to understand all the. Problems. Thanks alot..
@JavaAidTutorials
@JavaAidTutorials 4 года назад
Thanks for your feedback. I am glad my learnings are helping people. If you find my channel helpful, please share among your friends.
@chandankumarmishra336
@chandankumarmishra336 3 года назад
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
@JavaAidTutorials
@JavaAidTutorials 3 года назад
thanks for subscribing. We are working hard to provide good quality content to all of you.
@chandankumarmishra336
@chandankumarmishra336 3 года назад
@@JavaAidTutorials you totally deserve this...please keep up the momentum :) people will remember you for sure...
@JavaAidTutorials
@JavaAidTutorials 3 года назад
Thanks a lot for your kind words. Will try out best!
@Vaasu97
@Vaasu97 3 года назад
I found this more helpful than others, but i feel bad that i probably wouldn't have come to this solution on my own. gotta keep practicing i guess
@linlong
@linlong 4 года назад
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?
@JavaAidTutorials
@JavaAidTutorials 4 года назад
Yes, practice makes a man perfect..😊
@RishiRajput
@RishiRajput 4 года назад
Very Well Explained. Thank you!
@JavaAidTutorials
@JavaAidTutorials 4 года назад
most welcome. I am glad that you liked it...:)
@MrShozayen
@MrShozayen 4 года назад
Thanks for the clear explanation
@JavaAidTutorials
@JavaAidTutorials 4 года назад
welcome..🙂
@SLDOQ36
@SLDOQ36 3 года назад
Hey :) so what is the time complexity of this? I would assume it's O(n)? Pretty intelligent stuff!
@crjacinro
@crjacinro 3 года назад
Looks like O(n) with 2 passes
@osharaki
@osharaki 3 года назад
Much appreciated! Looking forward to more content.
@JavaAidTutorials
@JavaAidTutorials 3 года назад
Awesome, thank you!
@psharikkumar2434
@psharikkumar2434 4 года назад
God bless you, please keep doing the good work.
@JavaAidTutorials
@JavaAidTutorials 4 года назад
Thank you ..!! But to continue this good work , we need your support. if you find this channel helpful, please share among your friends.
@tech_non_tech9803
@tech_non_tech9803 3 года назад
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
@JavaAidTutorials
@JavaAidTutorials 3 года назад
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.
@johnnyshrewd2276
@johnnyshrewd2276 4 года назад
Very clear!
@JavaAidTutorials
@JavaAidTutorials 4 года назад
Thanks for your feedback.
@surajtopal9940
@surajtopal9940 4 года назад
Thanku sir jiii
@averychen4633
@averychen4633 3 месяца назад
you logic is only correct for a sorted array. if the array is 1, 3, 9,9,9, 3,27, 81. The second 3 can not form a (1,3,9) triplets.
@coolkannan0
@coolkannan0 3 года назад
I implemented this in javascript, r=1 fails in my case, so total 3 test cases fails, can you help me out
@IndianHistoryInDetails
@IndianHistoryInDetails 4 года назад
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; }
@JavaAidTutorials
@JavaAidTutorials 4 года назад
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; }
@heinrichkreuser2659
@heinrichkreuser2659 4 года назад
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 ```
@deepaligarg7643
@deepaligarg7643 2 года назад
This video does not load. It keeps spinning.
@forreal_bhenchod
@forreal_bhenchod 4 года назад
how to get logical thinking like you
@JavaAidTutorials
@JavaAidTutorials 4 года назад
just keep practising, after solving good amount of problems, you will be able to see the changes
@tanson86
@tanson86 4 года назад
@@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.
@raaghaviravisankar1853
@raaghaviravisankar1853 4 года назад
cannot find symbol rightmap.put(item,rightmap.getOrDefault(item,0L)+1); Why is it showing me this error?
@JavaAidTutorials
@JavaAidTutorials 4 года назад
this method got introduced in java8 , If you are using java7 you will get the error.
@nicolegarcia4689
@nicolegarcia4689 4 года назад
is there a reason to use map and not hashtable?
@JavaAidTutorials
@JavaAidTutorials 4 года назад
Hashtable is synchronised while map is not.and due to synchronisation,it will be a performance hit.
@Rahul-uk4su
@Rahul-uk4su 4 года назад
Please solve the angry children 2 problem in dynamic programming section
@JavaAidTutorials
@JavaAidTutorials 4 года назад
will try to upload but need some time. please stay tuned.
@Rahul-uk4su
@Rahul-uk4su 4 года назад
@@JavaAidTutorials Thank you for the reply.. Your work is inspiring..
@filipesa1038
@filipesa1038 4 года назад
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; }
@JavaAidTutorials
@JavaAidTutorials 4 года назад
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.
@vaishnavichaurasia5693
@vaishnavichaurasia5693 4 года назад
Why i am getting this error : Solution.java:18: error: cannot find symbol rightMap.put(item, rightMap.getOrDefault(item, 0L) + 1); ^ symbol: method getOrDefault(long,long) location: variable rightMap of type Map Solution.java:27: error: cannot find symbol rightMap.put(midTerm, rightMap.getOrDefault(midTerm, 0L) - 1); ^ symbol: method getOrDefault(long,long) location: variable rightMap of type Map Solution.java:38: error: cannot find symbol leftMap.put(midTerm, leftMap.getOrDefault(midTerm, 0L) + 1); ^ symbol: method getOrDefault(long,long) location: variable leftMap of type Map
@JavaAidTutorials
@JavaAidTutorials 4 года назад
It will be helpful if you can share your code as well, so that people can try to run and see what is the exact error.
@viru605
@viru605 4 года назад
What is the space complexity of this solution?
@JavaAidTutorials
@JavaAidTutorials 4 года назад
the complexity of this solution is O(n) . because we are doing everything thing in single pass.
@vijayalakshmi8337
@vijayalakshmi8337 4 года назад
@@JavaAidTutorials So, the time as well as space complexity is O(N)?
@venkateshviswanath6593
@venkateshviswanath6593 4 года назад
bro can u give testcase for 5 1 arr={1,1,1,1,1}
@JavaAidTutorials
@JavaAidTutorials 4 года назад
I hope this will help you to understand- *1* 1 1 1 1 = 0x4=0 1 *1* 1 1 1 = 1x3=3 1 1 *1* 1 1 = 2x2=4 1 1 1 *1* 1 = 3x1=3 1 1 1 1 *1* = 4x0=0 add all pairs (0+3+4+3+0)=10 which is your answer.
@sahukaraakash8516
@sahukaraakash8516 4 года назад
what about 1,9 81
@JavaAidTutorials
@JavaAidTutorials 4 года назад
Can you please post your complete question?
@hostroom826
@hostroom826 4 года назад
Thanks for this in depth explanation
@JavaAidTutorials
@JavaAidTutorials 4 года назад
most welcome..:)