Find Pairs in Array with Given Sum | Programming Tutorials

  Рет қаралды 69,194

Programming Tutorials

Programming Tutorials

Күн бұрын

Пікірлер: 77
@kingnani1557
@kingnani1557 5 жыл бұрын
Super explanation brother... thanks
@ProgrammingTutorials1M
@ProgrammingTutorials1M 5 жыл бұрын
Thank you very much
@kingnani1557
@kingnani1557 5 жыл бұрын
@@ProgrammingTutorials1M 😁😁😁😁😁😁
@hackernight5110
@hackernight5110 4 жыл бұрын
for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr.length; j++) { if (arr[i]+arr[j]==sum) System.out.println("Pair "+arr[i]+" , "+arr[j]); } } With O(n^2) time complexity
@charankumarreddy9495
@charankumarreddy9495 4 жыл бұрын
brooo thank u!! :')
@tusharprajapati2574
@tusharprajapati2574 Ай бұрын
pairs repeat hoga
@rohitkushwaha6125
@rohitkushwaha6125 Жыл бұрын
Nice explanation. Sir sometimes it happens they ask to return array of such pairs how it could be done.
@chandanagoshika2454
@chandanagoshika2454 8 күн бұрын
Super explanation..
@WeREmotional
@WeREmotional Жыл бұрын
I had an interview earlier this week they asked me solve it without using loops but only using streams api.can you pleasehelp me.
@garimanagarwal1571
@garimanagarwal1571 Жыл бұрын
this one is for sorted array please upload for unsorted array also
@Hemavani-lx6zb
@Hemavani-lx6zb 6 ай бұрын
awesome explanation, need program for : Write a java program for getting the sum of 30 by adding min 3 numbers from count[]={1, 15, 10, 5}. pleas help
@pritamagarwala9415
@pritamagarwala9415 2 жыл бұрын
time complexity will be O(log n)...right ?
@ashwinishete511
@ashwinishete511 2 жыл бұрын
Very nice ...plz make seris of interview prog...in this way
@ProgrammingTutorials1M
@ProgrammingTutorials1M 2 жыл бұрын
Thank you
@ProgrammingTutorials1M
@ProgrammingTutorials1M 6 жыл бұрын
Code link is present in the description box.
@rohitshirodkar1129
@rohitshirodkar1129 2 жыл бұрын
what if array has duplicate elements? then low++, high-- (on sum =9) wont work?
@merxxibeaucoup9093
@merxxibeaucoup9093 2 жыл бұрын
I solved it using a hashset
@simon-yi6jv
@simon-yi6jv 2 жыл бұрын
@@merxxibeaucoup9093 only if array in sorted it will work
@pradyumna668
@pradyumna668 4 жыл бұрын
what if duplicate are present or array with same elements like {2,2,2,2} and the required sum is 4
@ProgrammingTutorials1M
@ProgrammingTutorials1M 4 жыл бұрын
For this problem, The array elements are distinct and in a sorted order
@abduchadili5691
@abduchadili5691 8 ай бұрын
What if you have repeating pairs how would you code it ?
@sdetbyraviteja568
@sdetbyraviteja568 7 ай бұрын
For that again we have to create one hash set and by using if condition by putting distinct pairs we can person the sum operation.
@pratuu_s
@pratuu_s 3 жыл бұрын
This program asked on technical round.
@togashiyuuta-s1p
@togashiyuuta-s1p Жыл бұрын
What if I want to find 6 variables not just 2 for the sum?
@sankarpatra2788
@sankarpatra2788 2 жыл бұрын
Sir please do some questions related to list stack and que
@lokeshborole8997
@lokeshborole8997 Жыл бұрын
for Unsorted ?
@vipulraj_1193
@vipulraj_1193 2 жыл бұрын
what if there are multiple numbers like example array 122344 and we have sum equal to 5 then how to do in this way
@ProgrammingTutorials1M
@ProgrammingTutorials1M 2 жыл бұрын
Are you talking about duplicates in an array and in that case we have to find the pair?
@susongbrit
@susongbrit 4 жыл бұрын
Sir i subscribed ur channel and i will share it to my other frnds also ..awesome explantaion thank you sir
@ProgrammingTutorials1M
@ProgrammingTutorials1M 4 жыл бұрын
Thank you so much.
@yadnyeshrane7448
@yadnyeshrane7448 4 жыл бұрын
Sir can i make use of 2 for loops..plz check whether the code is right int arr[] = { 1, 2, 3, 4, 5, 6, 7 }; int k = 9; for (int i = 0; i < arr.length; i++) { for (int j = i + 1; j < arr.length; j++) { if (arr[i] + arr[j] == k) { System.out.println("pair is (" + arr[i] +" " +arr[j] + ")"); } } }
@ProgrammingTutorials1M
@ProgrammingTutorials1M 4 жыл бұрын
But the time complexity in this case will be O(n^2). Your code is correct only small modification is needed int arr[] = { 1, 2, 3, 4, 5, 6, 7 }; int k = 9; for (int i = 0; i < arr.length-1; i++) { for (int j = i + 1; j < arr.length; j++) { if (arr[i] + arr[j] == k) { System.out.println("pair is (" + arr[i] +" " +arr[j] + ")"); } } }
@merxxibeaucoup9093
@merxxibeaucoup9093 2 жыл бұрын
yes but what if there are duplicates numbers in the array??
@xristosmestviridis3708
@xristosmestviridis3708 4 жыл бұрын
great job man. Congratulations
@ProgrammingTutorials1M
@ProgrammingTutorials1M 4 жыл бұрын
Thank you very much.
@siddhigolatkar8558
@siddhigolatkar8558 10 ай бұрын
Thank you
@ProgrammingTutorials1M
@ProgrammingTutorials1M 10 ай бұрын
Thank you
@ndmotivations
@ndmotivations 4 жыл бұрын
thanks a lot for geat explanation Sir.
@ProgrammingTutorials1M
@ProgrammingTutorials1M 4 жыл бұрын
You are most welcome
@AlokBehera-n3k
@AlokBehera-n3k 9 ай бұрын
HI I need this probem fixed in java 8 .Could you please help me for this probem Given array : 1,3,5,7,9,11,16,20 Input value : 16 Output [2,5][3,4] Write the logic to identify the pairs of index in given array which value sum equal to input value.I have pasted here the problem statement I need solution by using java 8 concepts.
@sarah-fu6hw
@sarah-fu6hw 3 жыл бұрын
thank you soo much your videos are soo helpful
@ProgrammingTutorials1M
@ProgrammingTutorials1M 3 жыл бұрын
You're so welcome!
@WebMaster600
@WebMaster600 10 ай бұрын
//for unsorted Arrays // k mean sum and n length of arr HashMapmap=new HashMap(); for(int i=0;i
@ketanmorey1953
@ketanmorey1953 4 жыл бұрын
Having array element like{123,51,33,678,67} want such elements in output having digits sum is 6 ... Find such elements and put it into output array....so code please sir...
@ProgrammingTutorials1M
@ProgrammingTutorials1M 4 жыл бұрын
Hi Ketan, here is the code
@ProgrammingTutorials1M
@ProgrammingTutorials1M 4 жыл бұрын
public static void main(String[] args) { int[] arr = {123,51,33,678,67}; int digitSum = 6; for(int elem : arr) { int sum = getSum(elem); if(sum == digitSum) { System.out.println(elem); } } } public static int getSum(int num) { int sum = 0; while(num > 0) { int digit = num%10; sum = sum + digit; num = num/10; } return sum; }
@ketanmorey1953
@ketanmorey1953 4 жыл бұрын
Thank u sir
@ProgrammingTutorials1M
@ProgrammingTutorials1M 4 жыл бұрын
@@ketanmorey1953 Please share this channel with your friends and colleagues.
@abduchadili5691
@abduchadili5691 8 ай бұрын
you don't need if equal test, just use else which is the only case left
@amrish2181
@amrish2181 4 жыл бұрын
does it only work with sorted array?
@ProgrammingTutorials1M
@ProgrammingTutorials1M 4 жыл бұрын
Yes
@start1learn-n171
@start1learn-n171 4 ай бұрын
Tq
@adhvikworld1743
@adhvikworld1743 4 жыл бұрын
In case of unsorted array?
@ProgrammingTutorials1M
@ProgrammingTutorials1M 3 жыл бұрын
I'll create separate video for unsorted array.
@arijitmohapatra147
@arijitmohapatra147 3 жыл бұрын
What if the numbers in random order?
@iamnew3353
@iamnew3353 3 жыл бұрын
I think you have to sort the array before finding the pairs.
@mujtabahussain7015
@mujtabahussain7015 3 жыл бұрын
@@iamnew3353 but in that case using hashset is more efficient i think.
@jackelynmonteclaro2679
@jackelynmonteclaro2679 2 жыл бұрын
How about unsorted array sir?
@ProgrammingTutorials1M
@ProgrammingTutorials1M 2 жыл бұрын
For unsorted array either you can use HashMap to find the pairs or you can first sort the array and then use two pointers approach to solve.
@ProgrammingTutorials1M
@ProgrammingTutorials1M 2 жыл бұрын
You can also check this tutorial for unsorted array - kzbin.info/www/bejne/g3LNiKaqeq9rqK8
@thorunitha7755
@thorunitha7755 4 жыл бұрын
import java.io.*; import java.util.*; class Pair { public static void main(String args[]) { int arr[]={30,5,10,20,35,40,45,25,15}; int sum=50; for(int i=0;i
@ProgrammingTutorials1M
@ProgrammingTutorials1M 4 жыл бұрын
It's O(n^2). You can but it's not an efficient way.
@thorunitha7755
@thorunitha7755 4 жыл бұрын
@@ProgrammingTutorials1M Thank you sir! This tutorial is really helpful for me.
@prateekagrawal8166
@prateekagrawal8166 3 жыл бұрын
This won't work in test acse- a[]={ 1, 5, 7, -1, 5}, sum=6. its o/p should be 3 but this won't come with ur logic
@ProgrammingTutorials1M
@ProgrammingTutorials1M 3 жыл бұрын
This work only for sorted array.
@technosys9060
@technosys9060 2 жыл бұрын
work only with sorted array
@ProgrammingTutorials1M
@ProgrammingTutorials1M 2 жыл бұрын
Yes, I have mentioned in title
@jayugjklb9466
@jayugjklb9466 5 жыл бұрын
Sprb
@ProgrammingTutorials1M
@ProgrammingTutorials1M 3 жыл бұрын
Thank you
@sriracha9729
@sriracha9729 4 жыл бұрын
Didnt consider duplicates
@ProgrammingTutorials1M
@ProgrammingTutorials1M 4 жыл бұрын
I have already mentioned in problem statement that array elements are distinct and in sorted order.
@prashantsingh456
@prashantsingh456 2 жыл бұрын
your voice is low
@ProgrammingTutorials1M
@ProgrammingTutorials1M 2 жыл бұрын
In my new videos, you won't face this issue
@Biharibabu135
@Biharibabu135 5 жыл бұрын
but 1+2+5=8 so it should print 1 2 5
@zarifhossain515
@zarifhossain515 3 жыл бұрын
it's not a pair then
Find Common Elements in Three Sorted Arrays - Java Code
10:44
Programming Tutorials
Рет қаралды 32 М.
pumpkins #shorts
00:39
Mr DegrEE
Рет қаралды 125 МЛН
إخفاء الطعام سرًا تحت الطاولة للتناول لاحقًا 😏🍽️
00:28
حرف إبداعية للمنزل في 5 دقائق
Рет қаралды 84 МЛН
Кәсіпқой бокс | Жәнібек Әлімханұлы - Андрей Михайлович
48:57
Count pairs with given sum | Array | Love Babbar DSA Sheet | Amazon🔥
12:07
Yogesh & Shailesh (CodeLibrary)
Рет қаралды 93 М.
Algorithms Explained for Beginners - How I Wish I Was Taught
17:38
Internet Made Coder
Рет қаралды 357 М.
Array vs. ArrayList in Java Tutorial - What's The Difference?
17:36
Coding with John
Рет қаралды 539 М.
Print Duplicate Elements in Array - Java Code
4:51
Programming Tutorials
Рет қаралды 108 М.
Top 6 Coding Interview Concepts (Data Structures & Algorithms)
10:51
pumpkins #shorts
00:39
Mr DegrEE
Рет қаралды 125 МЛН