Check if Array Pairs are Divisible by K | Hashmap Interview Questions Explained

  Рет қаралды 40,911

Pepcoding

Pepcoding

Күн бұрын

Please consume this content on nados.pepcoding.com for a richer experience. It is necessary to solve the questions while watching videos, nados.pepcoding.com enables that.
NADOS also enables doubt support, career opportunities and contests besides free of charge content for learning. In this video, we discuss the problem where we are required to check if the array pairs are divisible by any integer k. In this problem,
1. You are given an array(arr) of integers and a number K.
2. You have to find if the given array can be divided into pairs such that the sum of every pair is divisible by k.
To attempt and submit this question, click here: www.pepcoding....
For a better experience and more exercises, VISIT: www.pepcoding....
#hashmaps #datastructures #algorithms
Have a look at our result:
Follow us on our FB page: / pepcoding
Follow us on Instagram: / pepcoding
Follow us on LinkedIn: / pepcoding-education

Пікірлер: 115
@vardhmanjain1630
@vardhmanjain1630 3 жыл бұрын
In my life, sir , I have never learnt something explained so beautifully. Thanks for all your videos, it really helps .
@Pepcoding
@Pepcoding 3 жыл бұрын
Thank you so much and If you like our efforts, please upvote the comments written by the students about Pepcoding here (www.quora.com/What-are-the-good-websites-to-learn-data-structures-and-algorithms )
@Siddharthnawani
@Siddharthnawani 4 жыл бұрын
to handle negative numbers as well, the solution needs to be modified a little. Put the below where-ever you are calculating remainder. int rem = val % k; if(rem
@maverickreal09
@maverickreal09 3 жыл бұрын
why not:- int rem=((x%k)+k)%k;
@AnkitSingh-zj2uc
@AnkitSingh-zj2uc 3 жыл бұрын
didn't work on leetcode
@LegitGamer2345
@LegitGamer2345 3 жыл бұрын
@@maverickreal09 yeah we have to use this instead of rem+=k possibly for overflow reasons since rem+=k gives WA on leetcode
@abhishekraj7713
@abhishekraj7713 3 жыл бұрын
@@maverickreal09 that right...
@abhishekraj7713
@abhishekraj7713 3 жыл бұрын
@@maverickreal09 (a-b) %c ... We can get by modular arithematic property
@garimakumari4346
@garimakumari4346 2 жыл бұрын
now rewising this playlist again and getting more logic cleaer thanku team pepcoding and summet sir
@ayushbhardwaj5925
@ayushbhardwaj5925 4 жыл бұрын
What a explanation .............sir aapne to Next level samjhaya hai . Fantastic...problem looks so easy after watching your video.
@Pepcoding
@Pepcoding 4 жыл бұрын
Thanks and welcome
@AtulSharma-hy9yo
@AtulSharma-hy9yo 2 жыл бұрын
very well explained, I like your all explanations, will recommend it to everyone who wants to learn the hashmap questions technique
@stupidbutcurious6206
@stupidbutcurious6206 2 жыл бұрын
very well explained... i cam across this question while searching for another similar but a littile more complex... can you please help solve.. The Question is:- Given an array of integers of size N, count all possible distinct triplets whose sum is exactly divisible by given integer K. for triplets i, j, k --> i
@nitunsingh6986
@nitunsingh6986 3 жыл бұрын
Sir u must handle negative cases as well. Like what if array elements are negative
@naikajsevak6090
@naikajsevak6090 2 жыл бұрын
I haven't so much deep understanding of math u are great bro
@jitendrakumawat1000
@jitendrakumawat1000 3 жыл бұрын
Thanks for your great contents. This code will not work for negative integer so I did change to get the remainder let rmd = ((arr[i] % k) + k) % k. Also you can remove the if 2 * rem == k condition because this will be always even if not then either array count will be odd or any other pair will be mismatched. Like arr = 9,39,36 and k = 8 then array count is odd and arr = 9,39,36,54 and k = 8 so 9 and 39 will create one pair and in case of 36 it will match frequency of remainder 4 to itselef 4 but it will not match for remainder 54 % 8 = 6, there is no number exist with 2 remainder.
@aj9706
@aj9706 3 жыл бұрын
Can u explain this It's suppose to be r1 + r2 = ky Where y>=1 My question is how is r1 + r2 = k ?
@aj9706
@aj9706 3 жыл бұрын
How are the remainder sum exactly equal to k
@syeraa9374
@syeraa9374 2 жыл бұрын
Hey! can you please explain why this formula ((arr[i] % k) + k) % k working for negative numbers.
@awalkingnosebleed
@awalkingnosebleed Ай бұрын
I don't think what you are saying correct, I think the check for k/2 is necessary and cannot be omitted.
@shinobiwolf8133
@shinobiwolf8133 3 жыл бұрын
Great solution! But sir can provide explanation fro -ve cases.
@noobhike5609
@noobhike5609 4 жыл бұрын
Sir is back ❤️❤️
@yuvrajgarg3847
@yuvrajgarg3847 3 жыл бұрын
SIR PLEASE MAKE A PLAYLIST FOR HASHMAP INTERVIEW QUESTIONS
@codehustler8582
@codehustler8582 Ай бұрын
Great explaination sir
@anuragv400
@anuragv400 4 жыл бұрын
Great explanation!! :)
@Pepcoding
@Pepcoding 4 жыл бұрын
Beta, recursion main itne question ho gye h, ki bacho ki thinking capability bn jaye ki vo naye question ki recursion khud se soch paye, issliye dynamic programming ki series main directly dp solution krvaya gya h. But if you are facing some problem or for clearing your doubts, you can join our community on telegram - t.me/pepcoding
@sagargupta1615
@sagargupta1615 3 жыл бұрын
Superb explanation.. thanks 😊
@anssha2643
@anssha2643 4 жыл бұрын
Thanks for uploading this. Sir, can you plz upload all the questions related to subarray. I find them very confusing.
@Pepcoding
@Pepcoding 4 жыл бұрын
As soon as possible
@rohansharma4065
@rohansharma4065 3 жыл бұрын
sir please add a test case for k/2, i got correct without implementing that
@utkarshagarwal587
@utkarshagarwal587 2 жыл бұрын
This same question came in my google's OA round
@neerajsaini1748
@neerajsaini1748 3 жыл бұрын
Leetcode : 1497 with negative values(we change -ve remainder in positive ) class Solution { public: bool canArrange(vector& arr, int k) { unordered_map m; for(auto x:arr) { int rem=(x)%k; rem=(rem+k)%k; m[rem]++; } for(auto x: arr) { x=(x)%k; x=(x+k)%k; if(x==0) { if(m[0]%2!=0) return false; } else if(k==x*2) { if(m[x]%2!=0) return false; } else { if(m[x] !=m[k-x]) return false; } } return true; } };
@sushilvarande6223
@sushilvarande6223 4 жыл бұрын
No. 1 explaination 👌
@Pepcoding
@Pepcoding 4 жыл бұрын
If you like my efforts, I request a review g.page/Pepcoding/review?rc
@aj9706
@aj9706 3 жыл бұрын
❤️ Beautiful explanation
@divyanshuchaudhari3257
@divyanshuchaudhari3257 3 жыл бұрын
Great explanation sir!!
@abhinavshukla6418
@abhinavshukla6418 3 жыл бұрын
damn!!u xplained beautifully, i wish i could think like dt
@milanpatel6240
@milanpatel6240 3 жыл бұрын
Excellent Explanation.
@AbhiKumar-vt5ns
@AbhiKumar-vt5ns 3 жыл бұрын
Sir Jii you are the best!!!!!!!!!!!!!!!
@Pepcoding
@Pepcoding 3 жыл бұрын
Glad to know that you liked the content and thank you for appreciating. The love and respect which I get from you people keep me highly motivated and the same I am able to forward It to you people through my videos. So, keep motivating, keep learning and keep loving Pepcoding😊
@aj9706
@aj9706 3 жыл бұрын
It's suppose to be r1 + r2 = ky Where y>=1 My question is how is r1 + r2 = k ?
@ayushbhardwaj5925
@ayushbhardwaj5925 4 жыл бұрын
Thanks sir for your time
@Pepcoding
@Pepcoding 4 жыл бұрын
Always welcome
@118_ruchirjain7
@118_ruchirjain7 3 жыл бұрын
sir leetcode pr yeh code pass nahi ho raha null pointer exception aa raha hai
@adarshsasidharan254
@adarshsasidharan254 Жыл бұрын
Nice explanation but this won't work for negative numbers I think
@AbhishekMishra-me4mu
@AbhishekMishra-me4mu 2 жыл бұрын
What will be the approach for negative numbers in the array? Nicely explained this one though!!👍👍
@Pepcoding
@Pepcoding 2 жыл бұрын
For better insight, visit nados.io, post your doubts, community will help you out there.
@PritamKumar-mr9wc
@PritamKumar-mr9wc 3 жыл бұрын
Thankfully got an explanation.
@Pepcoding
@Pepcoding 3 жыл бұрын
Glad it was helpful! If you like our efforts, will you like to write a few words about us here (www.quora.com/How-do-I-start-learning-or-strengthen-my-knowledge-of-data-structures-and-algorithms )
@rohitshinde8478
@rohitshinde8478 3 жыл бұрын
Thank you sir very much
@Pepcoding
@Pepcoding 3 жыл бұрын
Thankyou beta! I am glad you liked it. I also hope that you are watching till the end and trying to understand the what, how, and especially why of the problem. If you like our efforts, will you like to review us here - g.page/Pepcoding/review?rc
@ankitgarg2760
@ankitgarg2760 3 жыл бұрын
hey @pepcoding team Can you pls provide the optimized solution Explanation of count pairs in array whose product is divisible by k?
@raipallisiva107
@raipallisiva107 3 жыл бұрын
bro have u got any solution for product divisible by k
@ankitgarg2760
@ankitgarg2760 3 жыл бұрын
@@raipallisiva107 Nope bhai I am looking out for the solution or explanation but no one is able to do so
@ashwansrivastava6305
@ashwansrivastava6305 4 жыл бұрын
Sir please sort by value and sort by key krke ek video bana dijiye for hashmap..
@Pepcoding
@Pepcoding 4 жыл бұрын
ok ji
@nikeshsingh2081
@nikeshsingh2081 Жыл бұрын
Sir, this solution is not valid if remainder is negative or element are in negative.
@dhruvarora1660
@dhruvarora1660 2 жыл бұрын
Also.....If there are Negative numbers in the array....Just put rem + k else of rem in the remainder frequency map.
@yashtyagi8040
@yashtyagi8040 2 жыл бұрын
@Dhruv Arora but why?
@DEV_Chaudhary_69
@DEV_Chaudhary_69 2 жыл бұрын
@@yashtyagi8040 itna matt padho beti
@prashantjain6623
@prashantjain6623 3 жыл бұрын
Nice explanation !!
@Pepcoding
@Pepcoding 3 жыл бұрын
Glad it was helpful! and If you like our efforts, please upvote the comments written by the students about Pepcoding here (www.quora.com/What-are-the-good-websites-to-learn-data-structures-and-algorithms )
@gauravmandal01
@gauravmandal01 Жыл бұрын
((elements%k)+k)%k anybody who is strugling for negative testcase just change this
@ajeetworking
@ajeetworking 3 жыл бұрын
Thank you for the explaination
@Pepcoding
@Pepcoding 3 жыл бұрын
You're welcome😊🙏
@amansamal8233
@amansamal8233 2 жыл бұрын
op explanation sir
@Pepcoding
@Pepcoding 2 жыл бұрын
Thanks. For better experience visit on nados.io, even you can post your doubts on community tab on NADOS
@nathann4291
@nathann4291 4 жыл бұрын
U R AMAZING 👍
@Pepcoding
@Pepcoding 4 жыл бұрын
Thank you so much 😀
@Sunny-cx8ki
@Sunny-cx8ki 4 жыл бұрын
Sir ek baar Manacher's Algorithm padha do pls.....
@Pepcoding
@Pepcoding 4 жыл бұрын
hanji jaroor karenge
@Sunny-cx8ki
@Sunny-cx8ki 4 жыл бұрын
Thank You very much Sir
@codestreak6638
@codestreak6638 2 жыл бұрын
The explanation is too good it will be best if agar c++ code bhi hota 😅😅
@Pepcoding
@Pepcoding 2 жыл бұрын
Nados.io pe cpp Mei bhi hai
@codestreak6638
@codestreak6638 2 жыл бұрын
@@Pepcoding Got it now I can say best it is😎😎
@rohankumar-of5qe
@rohankumar-of5qe 3 жыл бұрын
Sir could you help me solve the below problem 🙏🙏🙏 You are given an array of 2N positive integers. There are N rounds in total. In each round you have to choose any two positive integers from the array and delete them. Your score in each round will be gcd(num1 , num2) * round_num where num1 & num2 are the numbers you have chosen. And round_number is the current round. Your total score will be the summation of the scores that you have obtained in every round. Determine the maximum total score The round number starts from 1 Ex-- N = 3 A = [8 5 6 25 6 16] O/p : 41
@harshtekriwal131
@harshtekriwal131 4 жыл бұрын
Sir explanation mast hai, par jo leetcode pr question hai usme negative number bi hai, to uske lie ye satisfy ni hoga. Ek bar wo wala case kese cover krenge wo bi batado.
@ankoor
@ankoor 4 жыл бұрын
Use ((x % k) + k) % k to calculate remainder, everything else is same
@Pepcoding
@Pepcoding 4 жыл бұрын
Ji, mujhse case choota hai. Jo neeche comment mei trick btaya hai wo correct hai
@harshtekriwal131
@harshtekriwal131 4 жыл бұрын
@@Pepcoding Sir ap is trick ka kisi video mei explanation dedijiega
@maverickreal09
@maverickreal09 3 жыл бұрын
@@ankoor why not :- int rem=x%k; if(rem
@ankoor
@ankoor 3 жыл бұрын
@@maverickreal09 this also works too...only difference is I am taking mod again...it doesn't matter...
@bartosz3667
@bartosz3667 2 жыл бұрын
If we don't include that 2*rem=k case in our code then also our code will run successfully and efficiently.
@awalkingnosebleed
@awalkingnosebleed Ай бұрын
I dont understand how??
@jyotidhatarwal9753
@jyotidhatarwal9753 3 жыл бұрын
Sir if the constraints were -10^9
@Pepcoding
@Pepcoding 3 жыл бұрын
Beta, I regret to inform you that, I won't be able to answer/solve the personal doubts of each and every student over here. For clearing your doubts, you can join our community on telegram - t.me/pepcoding.
@sachinpandey4095
@sachinpandey4095 3 жыл бұрын
In this case you can add k to the remainder if the remainder is negative (for negative numbers).
@targetsubconscious3003
@targetsubconscious3003 3 жыл бұрын
if (r
@utsavgupta746
@utsavgupta746 3 жыл бұрын
@@sachinpandey4095 bhai iska logic bata do kyu add kiya?
@tanishkumar6682
@tanishkumar6682 Жыл бұрын
understood
@umangkumar2005
@umangkumar2005 Жыл бұрын
when k=odd then in this k/2 case will fail
@rahulranjan7567
@rahulranjan7567 11 ай бұрын
There's a hack for this question. If the length of the array is odd, return False else search for the solution
@rmeena840
@rmeena840 3 жыл бұрын
What about negeative numbers?
@Pepcoding
@Pepcoding 3 жыл бұрын
Hey there! For better experience and well organised content visit - nados.pepcoding.com You can also post your queries on community tab and follow us on Instagram we will keep you updated. instagram.com/pepcoding/
@shivanigupta_19
@shivanigupta_19 4 жыл бұрын
😍
@anchalpandey2787
@anchalpandey2787 3 жыл бұрын
Greate explanation !!!
@Pepcoding
@Pepcoding 3 жыл бұрын
Thankyou beta! I am glad you liked it. I also hope that you are watching till end and trying to understand the what, how and especially why of the problem. Will you like to write a few words about us here ( www.quora.com/What-are-the-good-websites-to-learn-data-structures-and-algorithms )
@anchalpandey2787
@anchalpandey2787 3 жыл бұрын
@@Pepcoding yeah sure
@jitenderyadav721
@jitenderyadav721 4 жыл бұрын
Sir pehle graphs ka dalna tha na
@Pepcoding
@Pepcoding 4 жыл бұрын
wo bhi aaega beta jaldi he
@RITIKYADAVBIT
@RITIKYADAVBIT Жыл бұрын
leetcode 1497 solution class Solution { public boolean canArrange(int[] arr, int k) { Map rf = new HashMap(); for(int val:arr){ int rem = val%k; if(rem
@maverickreal09
@maverickreal09 3 жыл бұрын
how to handle negatives?
@Pepcoding
@Pepcoding 3 жыл бұрын
Beta, I regret to inform you that, I won't be able to answer/solve the personal doubts of each and every student over here. For clearing your doubts, you can join our community on telegram - t.me/pepcoding.
@maverickreal09
@maverickreal09 3 жыл бұрын
@@Pepcoding Ok sir, n.p. Thanks for the reply.
@W0807THORE
@W0807THORE 4 жыл бұрын
Can we add all the elements and module division by k and if module result is O and array length is even then we can print true else print false?? Will it work here
@Pepcoding
@Pepcoding 4 жыл бұрын
1, 2, 3, 1, 2, 3 and k is 6 this strategy won't work
@user-fz1cj8rh6k
@user-fz1cj8rh6k 2 жыл бұрын
Make videos in English.
ТЫ В ДЕТСТВЕ КОГДА ВЫПАЛ ЗУБ😂#shorts
00:59
BATEK_OFFICIAL
Рет қаралды 4,1 МЛН
бабл ти гель для душа // Eva mash
01:00
EVA mash
Рет қаралды 9 МЛН
How To Choose Mac N Cheese Date Night.. 🧀
00:58
Jojo Sim
Рет қаралды 84 МЛН
Count pairs in array divisible by K | GeeksforGeeks Problem of The Day
11:08
Check If Array Pairs Are Divisible by k | Leetcode 1497
12:53
Count of Equivalent Subarrays | Hashmap Interview Questions
16:27
Leetcode 1497. Check If Array Pairs Are Divisible by k
13:46
Array Pair Sum Divisibility Problem | Brute Force | Optimal | GfG POTD
27:41
ТЫ В ДЕТСТВЕ КОГДА ВЫПАЛ ЗУБ😂#shorts
00:59
BATEK_OFFICIAL
Рет қаралды 4,1 МЛН