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 Жыл бұрын
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.
@vaibhavshelke52883 ай бұрын
what about time complexity broo in your case it is O(n^2)
@reubenjosjoe26702 ай бұрын
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
@jatintilwani46042 ай бұрын
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 Жыл бұрын
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)
@adithyacharyyelloju33783 ай бұрын
High time complexity O(n 2 square )
@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 Жыл бұрын
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 Жыл бұрын
Yeah but here time complexity is O(n^2) which is greater than the method she has done
@TanishkAgrawal-ps6ew7 ай бұрын
Time limit exceeded
@venkataravikumarkammula26102 жыл бұрын
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.
@meriemETT8 ай бұрын
it's 19 21 not 19 11
@chagataiful4 жыл бұрын
u didnt sort array properly.U placed 19 before 11
@TanishkAgrawal-ps6ew7 ай бұрын
It's 21
@SurajRaj-vh5ei4 жыл бұрын
You've explained it brilliantly. Please upload more questions and solutions. And if possible also some logical tips.
@funkom487711 ай бұрын
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}")
@jjayeshpawar9 ай бұрын
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
@bharatsoni3433 жыл бұрын
Explaining way was outstanding
@VikasKumar-bz8zh3 жыл бұрын
thank you sister ,your concept is amezing
@vikinist6 ай бұрын
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)
@prithvidas82274 жыл бұрын
very interesting questions...
@gauravpatil79773 жыл бұрын
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))
@jahwill32762 жыл бұрын
can you explain this...
@gauravpatil79772 жыл бұрын
@@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.
@prachisaxena76353 жыл бұрын
is it just me or does no one else notice that it a not a sorted array? 19 is greater than 11.
@dineshbabu41798 ай бұрын
i did too
@garysam83394 жыл бұрын
Thank you ma'am for posting these questions..
@nETSETOStECH4 жыл бұрын
It's my pleasure. Share with your friends 😇
@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
@jeevapriya48534 жыл бұрын
Mam ur explanation was nice .....bt i do know how to claculate time nd space complexity ....so plz make videos on tht mam
@nETSETOStECH4 жыл бұрын
Thank you!! Sure
@jeevapriya48534 жыл бұрын
Kkie mam
@guineapeg35423 жыл бұрын
this is really good! but sadly it doesnt work for duplicate values :(
@sukantdwivedi42472 жыл бұрын
Change it into set() 🙂 simple
@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 Жыл бұрын
Wow
@kiranpatil4968 Жыл бұрын
Plese tell me ans :aabcd=1 aabcdd=2 aacbbdaa=3 How to write code
@pratiknaikwade95 Жыл бұрын
very well explained :) :) but program is too much big
@jeevapriya48534 жыл бұрын
Nd i was eagerly waiting for ur upcoming videos mam
@nETSETOStECH4 жыл бұрын
Uploaded. Please Check😊
@jeevapriya48534 жыл бұрын
😊😊
@kaizendom15663 жыл бұрын
thank you .... this is exactly what i was looking for
@Basavaraj.Y.S2 ай бұрын
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)
@jyotiranjanjena19463 жыл бұрын
superb
@nETSETOStECH3 жыл бұрын
Thank you! Cheers!
@harishcreations67493 күн бұрын
This quetion there is no values is equal to given int The output is. -1 -1
@niveditakumari7012 жыл бұрын
hi, thanks for the video. But, the array was not sorted properly, you had 11 after 19
@amanahmed60572 жыл бұрын
Point to be noted mylord
@mahiramodi34153 жыл бұрын
Can you please tell how will this give correct answer for following case: Arr- [2, 3,3,7,8,9] Sum = 10
@sahil5694 Жыл бұрын
It will return 2 and 8 as these are pairs firstly got whose sum is equal to Target sum using this logic
@siddhigolatkar855811 ай бұрын
Thank you
@MuthuAnushya Жыл бұрын
why u use right=right-1 and left=left+1 if we get a target value
@musicdiaries02959 Жыл бұрын
because we want the right and left pointers to converge at middle, thereby they consider all cases
@gauravpatil79773 жыл бұрын
o(n) is possible in this example by taking dictionary
@princesharma16343 жыл бұрын
How?... please mention code
@vidishasingh367 Жыл бұрын
please share the code
@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 Жыл бұрын
@@gauravpatil7977 it's not giving right answer
@gauravpatil7977 Жыл бұрын
@@vidishasingh367 really? I tested it before posting although 1 year ago. I'll check it out again.
@atharvakulkarni20243 жыл бұрын
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
@tanushreenagar31169 ай бұрын
perfect
@ravisolanke44952 жыл бұрын
# 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))
@hiteshsharma43953 жыл бұрын
What if the array is not sorted?
@abhishekprakash98033 жыл бұрын
Actually I wana know the index of pairs not value to detect number...plz help me
@_IT_Jason2 жыл бұрын
Then just display left,right rather than arr[left] and arr[right].
@kiranpatil4968 Жыл бұрын
Ip: my name is 12345 Op: mYY namEE iSS 123455 How to write code
@tobelorne Жыл бұрын
Got?
@peramvenkatasivapavani39903 жыл бұрын
please help us with this question
@sireeshajs88434 жыл бұрын
Code is not clearly visible... It was blur
@nETSETOStECH4 жыл бұрын
Is it?? For code, GitHub link is also mentioned in the description box
@handle_gc3 жыл бұрын
Array is not sorted 1911
@vamsh7i4 жыл бұрын
It is not sorted at all
@KallReport-yj2im4 ай бұрын
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)