Find Out Pairs with given sum in an array in python of time complexity O(n log n)- FACEBOOK,AMAZON

  Рет қаралды 50,826

nETSETOS

nETSETOS

Күн бұрын

Пікірлер
@jeevangg8975
@jeevangg8975 Жыл бұрын
This code is incomplete for few of the scenarios For example, if the sum = 12 For arr=[5,7,4,3,9,8,19,21] and it will display only [5,7] and it will not display [3,9],[4,8]. Below code might helps findout all possible scenarios. def find_num(num,sum): for i in range(len(num)): for j in range(i,len(num)): if num[i]+num[j]==sum: print(num[i],num[j]) return "code_ends_here" arr=[5,7,4,3,9,8,19,21] sum=12 s=find_num(arr,sum) print(s)
@santhoshks8808
@santhoshks8808 Жыл бұрын
hi....good one 🤟 but the second for loop should start from the second element which is for j in range(i+1, len(num)): becoz if the first in list is 6 it would give u a fake pair of elements.
@vaibhavshelke5288
@vaibhavshelke5288 3 ай бұрын
what about time complexity broo in your case it is O(n^2)
@reubenjosjoe2670
@reubenjosjoe2670 2 ай бұрын
no i think it works for all scenarios.the issue is you might have left out the part where you sort the array in the first step.if you sort it it works perfectly fine
@jatintilwani4604
@jatintilwani4604 2 ай бұрын
Another solution could be -- A=[5,7,4,3,9,8,19,21] target=12 dict1={} A.sort() for x in range(len(A)): for y in range(x+1,len(A)): if A[x]+A[y]==target: dict1[A[y]]=A[x] print(dict1)
@techiewithcamera
@techiewithcamera Жыл бұрын
My Solution: def pair_sum(list1, pair_sum): for i in range(len(list1)): for j in range(i+1, len(list1)): if pair_sum==list1[i]+list1[j]: print(f"num1: {list1[i]}, num2: {list1[j]}") list1=[5,7,4,3,9,8,19,21] pair_sum(list1, 17)
@adithyacharyyelloju3378
@adithyacharyyelloju3378 3 ай бұрын
High time complexity O(n 2 square )
@enjoyfriends873
@enjoyfriends873 Жыл бұрын
Thanks Code 1 : lst = sorted([5,7,4,3,9,8,19,21]) sum = 17 temp = [ i for i in lst if i
@someshwargarud5814
@someshwargarud5814 Жыл бұрын
list = [5,7,4,3,9,8,19,21] n = len(list) k=17 for i in range(n): for j in range(i+1,n): if list[i]+list[j] == k: print(list[i],list[j])
@psychogamer1368
@psychogamer1368 Жыл бұрын
Yeah but here time complexity is O(n^2) which is greater than the method she has done
@TanishkAgrawal-ps6ew
@TanishkAgrawal-ps6ew 7 ай бұрын
Time limit exceeded
@venkataravikumarkammula2610
@venkataravikumarkammula2610 2 жыл бұрын
In your example, how come a sorted array can have the last elements as 19, 11? Isn't it should be 11, 19 ? And as the asked target sum is 17, you can simply filter the elements which are greater than the target sum in your list. That way your list will appear much cleaner.
@meriemETT
@meriemETT 8 ай бұрын
it's 19 21 not 19 11
@chagataiful
@chagataiful 4 жыл бұрын
u didnt sort array properly.U placed 19 before 11
@TanishkAgrawal-ps6ew
@TanishkAgrawal-ps6ew 7 ай бұрын
It's 21
@SurajRaj-vh5ei
@SurajRaj-vh5ei 4 жыл бұрын
You've explained it brilliantly. Please upload more questions and solutions. And if possible also some logical tips.
@funkom4877
@funkom4877 11 ай бұрын
arr = [5, 7, 4, 3, 9, 8, 19, 21] total = 12 for i in arr: j = total - i if j in arr: print(f"The pair for {total} is {i} and {j}")
@jjayeshpawar
@jjayeshpawar 9 ай бұрын
l=[4,3,1,2,6,3,0] t=6 d={} for ind,ele in enumerate(l): #ind=5,ele=3 if t-ele in d: print(d[t-ele],ind) d[ele]=ind
@bharatsoni343
@bharatsoni343 3 жыл бұрын
Explaining way was outstanding
@VikasKumar-bz8zh
@VikasKumar-bz8zh 3 жыл бұрын
thank you sister ,your concept is amezing
@vikinist
@vikinist 6 ай бұрын
def pair_sum(l, s): a = [] b = set() for i in l: if s - i in b: a.append((i, s - i)) b.add(i) return a a = pair_sum([1, 2, 3, 4, 5, 6, 7], 7) print(a)
@prithvidas8227
@prithvidas8227 4 жыл бұрын
very interesting questions...
@gauravpatil7977
@gauravpatil7977 3 жыл бұрын
It can be done in O(n) with dictionary like this def two_sum(arr, target): dt = {} for i in range(len(arr)): if arr[i] in dt: print(dt[arr[i]],arr[i]) return True else: dt[target - arr[i]] = arr[i] return False arr = list(map(int,input().strip().split())) target = int(input()) print(two_sum(arr, target))
@jahwill3276
@jahwill3276 2 жыл бұрын
can you explain this...
@gauravpatil7977
@gauravpatil7977 2 жыл бұрын
@@jahwill3276 sure, let's take arr=[5, 4, 6, 9, 3] and target=10 Now, declare empty dictionary dt={} Now for loop will run till the range of Len of array and condition is if arr[i] exists in dt, but it obviously does not exist, so compiler will go to else block and will add the current value of i i.e. arr[i] to the key of difference of target and arr[i] so after first iteration dictionary will look like this - {5:5} and then compiler will again go for second iteration and again will follow the same process and now dictionary will look like dt={5:5, 6:4} and now compiler will go for third iteration and will find arr[i] that is, arr[2] that is 6 in dt as we added it to the dictionary before and hence as difference is found it will return as true.
@prachisaxena7635
@prachisaxena7635 3 жыл бұрын
is it just me or does no one else notice that it a not a sorted array? 19 is greater than 11.
@dineshbabu4179
@dineshbabu4179 8 ай бұрын
i did too
@garysam8339
@garysam8339 4 жыл бұрын
Thank you ma'am for posting these questions..
@nETSETOStECH
@nETSETOStECH 4 жыл бұрын
It's my pleasure. Share with your friends 😇
@tanmaydash803
@tanmaydash803 Жыл бұрын
def find_num(num,target): for i in range(len(num)): for j in range(i,len(num)) and i!=j: if num[i]+num[j]==target: return num[i],num[j] return -1
@jeevapriya4853
@jeevapriya4853 4 жыл бұрын
Mam ur explanation was nice .....bt i do know how to claculate time nd space complexity ....so plz make videos on tht mam
@nETSETOStECH
@nETSETOStECH 4 жыл бұрын
Thank you!! Sure
@jeevapriya4853
@jeevapriya4853 4 жыл бұрын
Kkie mam
@guineapeg3542
@guineapeg3542 3 жыл бұрын
this is really good! but sadly it doesnt work for duplicate values :(
@sukantdwivedi4247
@sukantdwivedi4247 2 жыл бұрын
Change it into set() 🙂 simple
@anonymouskapisresth
@anonymouskapisresth Жыл бұрын
arr=[3,4,5,7,8,9,19,11] sum=17 for i in range(len(arr)): if sum-arr[i] in arr: print(arr[i])
@Musiclover-jc8ze
@Musiclover-jc8ze Жыл бұрын
Wow
@kiranpatil4968
@kiranpatil4968 Жыл бұрын
Plese tell me ans :aabcd=1 aabcdd=2 aacbbdaa=3 How to write code
@pratiknaikwade95
@pratiknaikwade95 Жыл бұрын
very well explained :) :) but program is too much big
@jeevapriya4853
@jeevapriya4853 4 жыл бұрын
Nd i was eagerly waiting for ur upcoming videos mam
@nETSETOStECH
@nETSETOStECH 4 жыл бұрын
Uploaded. Please Check😊
@jeevapriya4853
@jeevapriya4853 4 жыл бұрын
😊😊
@kaizendom1566
@kaizendom1566 3 жыл бұрын
thank you .... this is exactly what i was looking for
@Basavaraj.Y.S
@Basavaraj.Y.S 2 ай бұрын
Please check this out, its more simple solution ip_list = [5,7,4,3,9,8,19,21] sum_value = 17 for i in ip_list: print(i) extra = sum_value - i if extra in ip_list: first_val = ip_list.index(i) second_val = ip_list.index(extra) print(first_val,second_val)
@jyotiranjanjena1946
@jyotiranjanjena1946 3 жыл бұрын
superb
@nETSETOStECH
@nETSETOStECH 3 жыл бұрын
Thank you! Cheers!
@harishcreations6749
@harishcreations6749 3 күн бұрын
This quetion there is no values is equal to given int The output is. -1 -1
@niveditakumari701
@niveditakumari701 2 жыл бұрын
hi, thanks for the video. But, the array was not sorted properly, you had 11 after 19
@amanahmed6057
@amanahmed6057 2 жыл бұрын
Point to be noted mylord
@mahiramodi3415
@mahiramodi3415 3 жыл бұрын
Can you please tell how will this give correct answer for following case: Arr- [2, 3,3,7,8,9] Sum = 10
@sahil5694
@sahil5694 Жыл бұрын
It will return 2 and 8 as these are pairs firstly got whose sum is equal to Target sum using this logic
@siddhigolatkar8558
@siddhigolatkar8558 11 ай бұрын
Thank you
@MuthuAnushya
@MuthuAnushya Жыл бұрын
why u use right=right-1 and left=left+1 if we get a target value
@musicdiaries02959
@musicdiaries02959 Жыл бұрын
because we want the right and left pointers to converge at middle, thereby they consider all cases
@gauravpatil7977
@gauravpatil7977 3 жыл бұрын
o(n) is possible in this example by taking dictionary
@princesharma1634
@princesharma1634 3 жыл бұрын
How?... please mention code
@vidishasingh367
@vidishasingh367 Жыл бұрын
please share the code
@gauravpatil7977
@gauravpatil7977 Жыл бұрын
@@vidishasingh367 def two_sum(arr, target): dt = {} for i in range(len(arr)): if arr[i] in dt: print(dt[arr[i]],arr[i]) return True else: dt[target - arr[i]] = arr[i] return False arr = list(map(int,input().strip().split())) target = int(input()) print(two_sum(arr, target))
@vidishasingh367
@vidishasingh367 Жыл бұрын
@@gauravpatil7977 it's not giving right answer
@gauravpatil7977
@gauravpatil7977 Жыл бұрын
@@vidishasingh367 really? I tested it before posting although 1 year ago. I'll check it out again.
@atharvakulkarni2024
@atharvakulkarni2024 3 жыл бұрын
Please may i know i have written the same code i dont know why it is failing a test case int s=0,e=n-1; int cnt=0; while(s
@tanushreenagar3116
@tanushreenagar3116 9 ай бұрын
perfect
@ravisolanke4495
@ravisolanke4495 2 жыл бұрын
# FIND OUT THE PAIRS OF GIVEN SUM O(n) :- def get_pairs(ns,tgt): # print(ns,tgt) obj={} for idx,el in enumerate(ns): if target - el in obj: print("PAIR:",(target - el,el)) obj[el]=idx return obj nums = [8, 7, 2, 5, 3, 1] target = 10 print(get_pairs(nums,target))
@hiteshsharma4395
@hiteshsharma4395 3 жыл бұрын
What if the array is not sorted?
@abhishekprakash9803
@abhishekprakash9803 3 жыл бұрын
Actually I wana know the index of pairs not value to detect number...plz help me
@_IT_Jason
@_IT_Jason 2 жыл бұрын
Then just display left,right rather than arr[left] and arr[right].
@kiranpatil4968
@kiranpatil4968 Жыл бұрын
Ip: my name is 12345 Op: mYY namEE iSS 123455 How to write code
@tobelorne
@tobelorne Жыл бұрын
Got?
@peramvenkatasivapavani3990
@peramvenkatasivapavani3990 3 жыл бұрын
please help us with this question
@sireeshajs8843
@sireeshajs8843 4 жыл бұрын
Code is not clearly visible... It was blur
@nETSETOStECH
@nETSETOStECH 4 жыл бұрын
Is it?? For code, GitHub link is also mentioned in the description box
@handle_gc
@handle_gc 3 жыл бұрын
Array is not sorted 1911
@vamsh7i
@vamsh7i 4 жыл бұрын
It is not sorted at all
@KallReport-yj2im
@KallReport-yj2im 4 ай бұрын
Short Summary for [Find Out Pairs with given sum in an array in python of time complexity O(n log n)- FACEBOOK,AMAZON](kzbin.info/www/bejne/gF6baaapn6qpb9E) Title: Pair Sum Detection Algorithm - Efficient Python Implementation in O(n log n) - FACEBOOK, AMAZON [01:18](kzbin.info/www/bejne/gF6baaapn6qpb9E&t=78) Detect pairs with the given sum in an array using O(n log n) time complexity [02:36](kzbin.info/www/bejne/gF6baaapn6qpb9E&t=156) Finding pairs in array with sum 17 [03:54](kzbin.info/www/bejne/gF6baaapn6qpb9E&t=234) Comparing pairs with given sum in an array using left and right pointers. [05:12](kzbin.info/www/bejne/gF6baaapn6qpb9E&t=312) Demonstrating iterating through array and comparing sums with target value [06:30](kzbin.info/www/bejne/gF6baaapn6qpb9E&t=390) Finding pairs with a given sum in an array with O(n log n) complexity [07:48](kzbin.info/www/bejne/gF6baaapn6qpb9E&t=468) Implement a function to find pairs in array with given sum efficiently in Python [09:06](kzbin.info/www/bejne/gF6baaapn6qpb9E&t=546) Algorithm to find pairs with given sum in O(n log n) time complexity [10:20](kzbin.info/www/bejne/gF6baaapn6qpb9E&t=620) Using a single while loop and sort yields O(n log n) time complexity --------------------------------- Detailed Summary for [Find Out Pairs with given sum in an array in python of time complexity O(n log n)- FACEBOOK,AMAZON](kzbin.info/www/bejne/gF6baaapn6qpb9E) by [Merlin](merlin.foyer.work/) Title: Pair Sum Detection Algorithm - Efficient Python Implementation in O(n log n) - FACEBOOK, AMAZON [01:18](kzbin.info/www/bejne/gF6baaapn6qpb9E&t=78) Detect pairs with the given sum in an array using O(n log n) time complexity - This interview question is frequently asked in Amazon, Facebook, and Google interviews. - The approach involves sorting the array and finding pairs that sum up to the given value. [02:36](kzbin.info/www/bejne/gF6baaapn6qpb9E&t=156) Finding pairs in array with sum 17 - Setting up pointers from left to right and right to left - Comparing sum of pair with target sum value [03:54](kzbin.info/www/bejne/gF6baaapn6qpb9E&t=234) Comparing pairs with given sum in an array using left and right pointers. - Using left and right pointers to compare array values with target sum. - Moving left pointer towards the right until reaching a solution. [05:12](kzbin.info/www/bejne/gF6baaapn6qpb9E&t=312) Demonstrating iterating through array and comparing sums with target value - Explaining comparison of sum with target value in order to update pointer positions - Detailing process of iterating through array and updating pointers based on comparison results [06:30](kzbin.info/www/bejne/gF6baaapn6qpb9E&t=390) Finding pairs with a given sum in an array with O(n log n) complexity - The algorithm involves sorting the array and then comparing the sum of two values with the target sum - The left and right values are adjusted based on the comparison to find the requested sum pair [07:48](kzbin.info/www/bejne/gF6baaapn6qpb9E&t=468) Implement a function to find pairs in array with given sum efficiently in Python - Sort the input array first for efficient searching - Use two pointers approach to iterate through the array and find pairs with the given sum [09:06](kzbin.info/www/bejne/gF6baaapn6qpb9E&t=546) Algorithm to find pairs with given sum in O(n log n) time complexity - Using a technique where left and right pointers move towards each other when sum is smaller - Printing the values of pairs found in the array and updating pointers accordingly [10:20](kzbin.info/www/bejne/gF6baaapn6qpb9E&t=620) Using a single while loop and sort yields O(n log n) time complexity - Values of pairs found are 8 and 9 - Overall time complexity: O(n log n) - Space complexity: O(1)
@hritikchand9930
@hritikchand9930 3 жыл бұрын
this code will not work if the element is same
@_IT_Jason
@_IT_Jason 2 жыл бұрын
Make it into set…
@freaky11100
@freaky11100 2 ай бұрын
samaj nhi aaya
FIND MISSING NUMBER IN AN ARRAY IN PYTHON
11:23
nETSETOS
Рет қаралды 49 М.
Симбу закрыли дома?! 🔒 #симба #симбочка #арти
00:41
Симбочка Пимпочка
Рет қаралды 4,5 МЛН
They Chose Kindness Over Abuse in Their Team #shorts
00:20
I migliori trucchetti di Fabiosa
Рет қаралды 12 МЛН
Subarray with given sum
5:04
Techdose
Рет қаралды 203 М.
Evaluate Postfix Expression using Stack
10:04
nETSETOS
Рет қаралды 16 М.
How I Would Learn Python FAST in 2024 (if I could start over)
12:19
Thu Vu data analytics
Рет қаралды 552 М.
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 674 М.
Find Pairs in Array with Given Sum | Programming Tutorials
5:41
Programming Tutorials
Рет қаралды 70 М.
Python 101: Learn the 5 Must-Know Concepts
20:00
Tech With Tim
Рет қаралды 1,2 МЛН
Симбу закрыли дома?! 🔒 #симба #симбочка #арти
00:41
Симбочка Пимпочка
Рет қаралды 4,5 МЛН