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

preview_player
Показать описание
Find Out Pairs with given sum in an array in python of time complexity
O(n log n)- FACEBOOK,AMAZON

#python #DataStructures

-~-~~-~~~-~~-~-
Please watch: "LRU Cache (With Python Code) "
-~-~~-~~~-~~-~-
Рекомендации по теме
Комментарии
Автор

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)

jeevangg
Автор

u didnt sort array properly.U placed 19 before 11

chagataiful
Автор

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.

venkataravikumarkammula
Автор

You've explained it brilliantly. Please upload more questions and solutions. And if possible also some logical tips.

SurajRaj-vhei
Автор

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)

techiewithcamera
Автор

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)

jatintilwani
Автор

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])

someshwargarud
Автор

Thanks

Code 1 :

lst = sorted([5, 7, 4, 3, 9, 8, 19, 21])
sum = 17

temp = [ i for i in lst if i <=sum ]

d = {}

for i in temp:
c = sum - i
if c in temp:
print(i, c)
break

Code 2 :

lst = sorted([5, 7, 4, 3, 9, 8, 19, 21])
sum = 17

temp = [ i for i in lst if i <=sum ]

seen = []

for idx, val in enumerate(temp):
if sum - val in seen:
print([sum-val, val])
else:
seen.append(val)

enjoyfriends
Автор

is it just me or does no one else notice that it a not a sorted array? 19 is greater than 11.

prachisaxena
Автор

thank you sister , your concept is amezing

VikasKumar-bzzh
Автор

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])

anonymouskapisresth
Автор

Thank you ma'am for posting these questions..

garysam
Автор

thank you .... this is exactly what i was looking for

kaizendom
Автор

Nd i was eagerly waiting for ur upcoming videos mam

jeevapriya
Автор

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))

gauravpatil
Автор

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}")

funkom
Автор

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

jjayeshpawar
Автор

Mam ur explanation was nice i do know how to claculate time nd space complexity ....so plz make videos on tht mam

jeevapriya
Автор

very well explained :) :) but program is too much big

pratiknaikwade
Автор

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)

vikinist