Find pairs with the given sum in an array. Give the algorithm.
Пікірлер: 99
@VinayK225 жыл бұрын
This algorithms is only for distinct elements fails for duplicates in array
@mdnayab58394 жыл бұрын
correct
@tintiniitk3 жыл бұрын
@@mdnayab5839 The fix for duplicates needs a small improvement to this. This is especially harder when there are duplicates both on the high and the low side at the same time. To handle duplicates, instead of just storing l, r indices, you also need to store something like the indices where l last changed (let's say lAnchor) and the index where r last changed (rAnchor), and keep incrementing l and r if they are same on moving forward. Now num += (l - lAnchor) * (rAnchor - r). Now reset lAnchor = ++l and rAnchor = --r . And repeat.
@RahulYadav-nl1ek3 жыл бұрын
@@tintiniitk but it will increase the complexity we are already in nlogn after sorting if we use extra space we can do this in O(n) time and O(n) space with duplicates
@bikramjitdas26213 жыл бұрын
@@tintiniitk could you just trace an example with this approach
@tintiniitk3 жыл бұрын
As has been pointed out by some, this doesn't handle duplicates at all. The fix for duplicates needs a small improvement to this. This is especially harder when there are duplicates both on the high and the low side at the same time. To handle duplicates, instead of just storing l, r indices, you also need to store something like the indices where l last changed (let's say lAnchor) and the index where r last changed (rAnchor), and keep incrementing l and r if they are same on moving forward. Now if array[l] + array[r] == sum, num += (l - lAnchor) * (rAnchor - r). After this, reset lAnchor = ++l ; rAnchor = --r . And repeat.
@tintiniitk3 жыл бұрын
This approach is O(n.log n). A much better O(n) approach is using hashmaps. array -> hashmap . hashmap in c++ is unordered_map. int count = 0; for (auto kv: hashmap) { if (kv.first * 2 == sum) count += kv.second * (kv.second - 1) / 2; else if (kv.first < sum - kv.first) { count += hashmap[sum - kv.first]; } return count; }
@ankan19947 жыл бұрын
i am promoting ur channel among my friends and they all find ur videos very helpful
@partapupreetham95183 жыл бұрын
This algorithm fails for duplicate elements
@prateektripathi1005 жыл бұрын
Awesome.....can't describe in words how amazing explanation it is
@ankan19947 жыл бұрын
very important and frequently asked array question...thanks for uploading sirji..a similar type of question which is faced by me in interview-let we have an given array of only (1) and (-1) as elements like arr[1,-1,-1,1,1,-1.....1] so we have to count the number of sub-arrays forming the sum 0.For eg- arr[1,-1,1,-1] here no of sub arrays is 4. i know how to do it, but still i want to get it from u bcoz u always bring the best and optimal solution.
@Rajveer04006 жыл бұрын
Best video ever for the array ...simple and easy concept cleared thank you so much ...
@naturebc2 жыл бұрын
for i = 0; i< array.length; for j = i+1; j
@dobbyunleashed5 жыл бұрын
This method does not work in case you have some numbers which are repeating multiple times in array.
@mdnayab58394 жыл бұрын
@Fatima faz i did not got your point. can you plz share a code snippet?
@TheMohit9872 жыл бұрын
Right
@TheMohit9872 жыл бұрын
@@mdnayab5839 This is not a full-proof solution. ❌❌❌ Like if input is: [2 -6 2 5 2 ] then right output should be [2 2], [2 2], [2 2] where as your code will return only two pairs.
@milindjain5442 жыл бұрын
Awesome lecture too clear and crisp
@dineshkathania22284 жыл бұрын
No need of sorting we can write in python in simple logic and it will cover all pair like duplicate numbers also:- def printPairs(arr, n, sum): for i in range(0, n ): for j in range(i + 1, n ): if (arr[i] + arr[j] == sum): print(arr[i],arr[j])
@preetsingh21335 жыл бұрын
It is failing in 2 2 2 2 2 (sum is 4) case
@tapanjeetroy82665 жыл бұрын
Thanks for uploading it.. You are doing a great job.. Please upload more videos of competitive programming.. We really need it
@Ankit135557 жыл бұрын
also plzz discuss the O(n) as per to me...worst case will be O(nlogn)-->sorting +n for checking all nos ...is it correct?
@kabooby03 жыл бұрын
You can do this in O(n) time with a hashmap
@devanshprakash83543 жыл бұрын
Is this two sum problem?
@sarahdaniel68624 жыл бұрын
This logic exceeds time limit on geeks for geeks.
@em_ashutosh3 жыл бұрын
sort krtai hi game over
@vikassrivastava64597 жыл бұрын
Hi Vivekanand - could you please upload a complete lecture on bit manipulation with some of the examples which will help in understanding the concept Thanks in advance
@rajaindirajith15346 жыл бұрын
In the else case you can do l++; and r--; not or. because if the sum produce by the two number will not produce the result with any other combination
@abhishekat80323 жыл бұрын
what if that array has duplicate items
@netturikowshikbasha48114 жыл бұрын
1 2 3 4 5 6 the output of code is 2 but actullay 3
@rajeshd73897 жыл бұрын
Hi.... nice explanation !!!! But this algorithm has time complexity O(nlogn) or O(n2) depending upon sorting technique used .... Can you extend this approach to hashing which has time complexity O(n).
@Nognamogo6 жыл бұрын
I'm pretty sure the sorting algorithm is ignored here. Sorting is only worth it if you'll be running multiple algorithms on the same data. Without the sorting it's O(n) which is what the video meant.
@mcaddit68023 жыл бұрын
awesome approach sir
@snehasisbarat24573 жыл бұрын
How to solve this problem if the array is unsorted and required time complexity is O(N)?
@bhaskarnaik58423 жыл бұрын
I need explanation of this topic given a sorted and rotated array, find if there is a pair with a given sum
@akhileshswaroopxisrnnadbt36694 жыл бұрын
How to solve when there are duplicate elements in array or all elements in array are same?
@45_ritiksharma324 жыл бұрын
Could you please explain the logic behind this code
@swethag11462 жыл бұрын
Thanks for this approach. Satisfies duplicate as well. Java code below... int[] num = {1,2,3,4,5,6,7,8}; Arrays.sort(num); int sum = 6; int i = 0; int j = num.length - 1; boolean found = false; while(i sum) j--; else if(num[i] + num[j] < sum) i++; else if(num[i] + num[j] == sum){ System.out.println(num[i] +","+ num[j]); i++; j--; found = true; } } if(!found) System.out.println("pair not found");
@sayantaniguha85192 жыл бұрын
what kind of a code is this / not working
@akhileshswaroopxisrnnadbt36694 жыл бұрын
@Vivekanand Khyade - Algorithm Every Day :How to solve when there are duplicate elements in array or all elements in array are same?
@ihowaonaro45115 жыл бұрын
This is great but it doesnt consider if the array has duplicate elements. e.g if there were two 10's you would miss the sum
@Prophetsarkar5 жыл бұрын
sir in the if(arr[l]+arr[r]>GS) why you put (l++) can we put (r--) there?
@sagar1u5 жыл бұрын
sir this video satisfies the condition of pairs ,but if we want combination of 3 or 4(suppose a[4,1,5,6,9,0,-1,20] and sum require is 31 then it has to be return [5,6,20]) elemnets from the array then what is the code or logic
@Vishal-nc8ju5 жыл бұрын
best channel
@RajaSekhar-te5nz5 жыл бұрын
Do the program on substracting adjacent numbers in array and finally display sum of all
@shilpika99425 жыл бұрын
sir when you find a pair and you print it then why we have to either do l++ or r-- why not both ? can u please explain?
@vikramsingla32175 жыл бұрын
"l++ or r--" Why to do only one of these. If we do both l++ and r--, does it make any difference? Doing both seems more correct approach to me.
@fuadhasan03626 жыл бұрын
thank you sir!! love and respect from bangladesh
@amolshelke96516 жыл бұрын
can you please send here link of the code ..github ??
@mohammadwahiduzzamankhan43975 жыл бұрын
Your explanation is amazing.
@sayankundu4854 жыл бұрын
Suppose it says given sum = 3 ... Then how will this algorithm work
@devanshprakash83543 жыл бұрын
Is this the two sum problem?
@seraj_valley4 жыл бұрын
no need of sorting use the concpt of hashing
@gamingroom64216 жыл бұрын
This method will work for only two element pairs
@AllTypeShorts..4 жыл бұрын
please explain the logic behind this code sir................
@tanveer.shaikh3 жыл бұрын
Your solution is O(nlog n)
@surajrohankar10285 жыл бұрын
In this example 1+2+8=11, 1+2+3+5=11 and so on, then how can we make it dynamic?
@vamsisaikrishnapaturu22615 жыл бұрын
only pairs (2 numbers only)
@cricbolly32524 жыл бұрын
Great sir
@rahulkolte15816 жыл бұрын
but it is giving only two elements pairs (1,2,8) this also sum is 11
@avinashch48115 жыл бұрын
Gs = 11 I want to get [1,3,7] as output how to write code for that ?
@alokcom4 жыл бұрын
Thats triplet in array in array question.
@ankitchawla84874 жыл бұрын
This algorithm dosen't work for duplicate elements.
@amolshelke96516 жыл бұрын
what if instead of finding pair of elements .. i want any number of elements whose sum is equal to given number ??
@varshapatil52635 жыл бұрын
I also want solution for same problem If you have plz share with me
@shiva_gaming81222 жыл бұрын
wha if all the elements are equal
@suneelsai99812 жыл бұрын
6+4+1 =11 is also a pair right?
@lesnar2292 жыл бұрын
Why need to sort arr
@TheMohit9872 жыл бұрын
This is not a full-proof solution. ❌❌❌ Like if input is: [2 -6 2 5 2 ] then right output should be [2 2], [2 2], [2 2] where as your code will return only two pairs.
@meenakshirana76645 жыл бұрын
Video explanation is good. But this approach of finding the count doesn't work, in case if array consists of duplicates.
@harderharder67135 жыл бұрын
the algorithm that he has mentioned will work in case of duplicates because he is not incrementing R and decrementing L simultaneously when a pair is found ...You are talking about the case when you increase and decrease them in a single step when a pair is found..
@sukhangadsingh7426 жыл бұрын
fails for {1,1,1,1}
@saikatdutta19915 жыл бұрын
how ?
@jermainegoss7053 жыл бұрын
Can you please do quadruple sum to target? pretty please lol
@pranaywelekar40603 жыл бұрын
can u send the correct code once
@himanshubhatt62403 жыл бұрын
Thanks 😊
@mowglishihtzutoy51974 жыл бұрын
Lot of love
@yashchandraverma31314 жыл бұрын
Thanks
@prashantverma33474 жыл бұрын
thank you
@ta3113ta5 жыл бұрын
Thank you sir.
@kirankumarmv27714 жыл бұрын
This algo works for duplicates aswell.. only thing is we have to have sorted array in first place.
@45_ritiksharma324 жыл бұрын
Please post more vidios
@kirankumarmv27714 жыл бұрын
we have to do both l++ & r--, as and when we found sum.
@aravindg61293 жыл бұрын
Bro whats the complexity
@jamesqiu67157 жыл бұрын
What??? why need to sort the array first ? Is this the "2 sum" problem ?
@dileepalla67696 жыл бұрын
thanks i learn
@orz55167 жыл бұрын
thank you from israel. u are great.
@insaniyat_15124 жыл бұрын
respect
@vincenr88227 жыл бұрын
HELLO FRIENDS :)
@vivekanandkhyade7 жыл бұрын
Thanks Vinster..!
@arjunnatukula71285 жыл бұрын
@@vivekanandkhyade codes plz
@backtobasics68657 жыл бұрын
not a good solution
@Fhhchchcddd4 жыл бұрын
हिंदी में बोल ले भाई
@neerajmahapatra52393 жыл бұрын
At least explain the logic behind your code just explaining code...😞