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
@charankumarreddy94954 жыл бұрын
brooo thank u!! :')
@tusharprajapati2574Ай бұрын
pairs repeat hoga
@rohitkushwaha6125 Жыл бұрын
Nice explanation. Sir sometimes it happens they ask to return array of such pairs how it could be done.
@chandanagoshika24548 күн бұрын
Super explanation..
@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 Жыл бұрын
this one is for sorted array please upload for unsorted array also
@Hemavani-lx6zb6 ай бұрын
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
@pritamagarwala94152 жыл бұрын
time complexity will be O(log n)...right ?
@ashwinishete5112 жыл бұрын
Very nice ...plz make seris of interview prog...in this way
@ProgrammingTutorials1M2 жыл бұрын
Thank you
@ProgrammingTutorials1M6 жыл бұрын
Code link is present in the description box.
@rohitshirodkar11292 жыл бұрын
what if array has duplicate elements? then low++, high-- (on sum =9) wont work?
@merxxibeaucoup90932 жыл бұрын
I solved it using a hashset
@simon-yi6jv2 жыл бұрын
@@merxxibeaucoup9093 only if array in sorted it will work
@pradyumna6684 жыл бұрын
what if duplicate are present or array with same elements like {2,2,2,2} and the required sum is 4
@ProgrammingTutorials1M4 жыл бұрын
For this problem, The array elements are distinct and in a sorted order
@abduchadili56918 ай бұрын
What if you have repeating pairs how would you code it ?
@sdetbyraviteja5687 ай бұрын
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_s3 жыл бұрын
This program asked on technical round.
@togashiyuuta-s1p Жыл бұрын
What if I want to find 6 variables not just 2 for the sum?
@sankarpatra27882 жыл бұрын
Sir please do some questions related to list stack and que
@lokeshborole8997 Жыл бұрын
for Unsorted ?
@vipulraj_11932 жыл бұрын
what if there are multiple numbers like example array 122344 and we have sum equal to 5 then how to do in this way
@ProgrammingTutorials1M2 жыл бұрын
Are you talking about duplicates in an array and in that case we have to find the pair?
@susongbrit4 жыл бұрын
Sir i subscribed ur channel and i will share it to my other frnds also ..awesome explantaion thank you sir
@ProgrammingTutorials1M4 жыл бұрын
Thank you so much.
@yadnyeshrane74484 жыл бұрын
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] + ")"); } } }
@ProgrammingTutorials1M4 жыл бұрын
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] + ")"); } } }
@merxxibeaucoup90932 жыл бұрын
yes but what if there are duplicates numbers in the array??
@xristosmestviridis37084 жыл бұрын
great job man. Congratulations
@ProgrammingTutorials1M4 жыл бұрын
Thank you very much.
@siddhigolatkar855810 ай бұрын
Thank you
@ProgrammingTutorials1M10 ай бұрын
Thank you
@ndmotivations4 жыл бұрын
thanks a lot for geat explanation Sir.
@ProgrammingTutorials1M4 жыл бұрын
You are most welcome
@AlokBehera-n3k9 ай бұрын
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-fu6hw3 жыл бұрын
thank you soo much your videos are soo helpful
@ProgrammingTutorials1M3 жыл бұрын
You're so welcome!
@WebMaster60010 ай бұрын
//for unsorted Arrays // k mean sum and n length of arr HashMapmap=new HashMap(); for(int i=0;i
@ketanmorey19534 жыл бұрын
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...
@ProgrammingTutorials1M4 жыл бұрын
Hi Ketan, here is the code
@ProgrammingTutorials1M4 жыл бұрын
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; }
@ketanmorey19534 жыл бұрын
Thank u sir
@ProgrammingTutorials1M4 жыл бұрын
@@ketanmorey1953 Please share this channel with your friends and colleagues.
@abduchadili56918 ай бұрын
you don't need if equal test, just use else which is the only case left
@amrish21814 жыл бұрын
does it only work with sorted array?
@ProgrammingTutorials1M4 жыл бұрын
Yes
@start1learn-n1714 ай бұрын
Tq
@adhvikworld17434 жыл бұрын
In case of unsorted array?
@ProgrammingTutorials1M3 жыл бұрын
I'll create separate video for unsorted array.
@arijitmohapatra1473 жыл бұрын
What if the numbers in random order?
@iamnew33533 жыл бұрын
I think you have to sort the array before finding the pairs.
@mujtabahussain70153 жыл бұрын
@@iamnew3353 but in that case using hashset is more efficient i think.
@jackelynmonteclaro26792 жыл бұрын
How about unsorted array sir?
@ProgrammingTutorials1M2 жыл бұрын
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.
@ProgrammingTutorials1M2 жыл бұрын
You can also check this tutorial for unsorted array - kzbin.info/www/bejne/g3LNiKaqeq9rqK8
@thorunitha77554 жыл бұрын
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
@ProgrammingTutorials1M4 жыл бұрын
It's O(n^2). You can but it's not an efficient way.
@thorunitha77554 жыл бұрын
@@ProgrammingTutorials1M Thank you sir! This tutorial is really helpful for me.
@prateekagrawal81663 жыл бұрын
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
@ProgrammingTutorials1M3 жыл бұрын
This work only for sorted array.
@technosys90602 жыл бұрын
work only with sorted array
@ProgrammingTutorials1M2 жыл бұрын
Yes, I have mentioned in title
@jayugjklb94665 жыл бұрын
Sprb
@ProgrammingTutorials1M3 жыл бұрын
Thank you
@sriracha97294 жыл бұрын
Didnt consider duplicates
@ProgrammingTutorials1M4 жыл бұрын
I have already mentioned in problem statement that array elements are distinct and in sorted order.