Find Pairs in Array with Given Sum | Programming Tutorials
HTML-код
- Опубликовано: 7 фев 2025
- In this tutorial, I have explained how to find pairs in array with given sum. Write a Java Program to find pairs with given sum in a sorted array.
Find pairs with given sum in an array Java Code - webrewrite.com...
Interview Questions on Binary Search - • Binary Search | Interv...
LeetCode Solutions Java Code - • LeetCode Solutions | Java
Linked List Interview Questions - • Linked List | Intervie...
Binary Tree Interview Questions - • Binary Tree | Intervie...
Stack Interview Questions - • Stack | Interview Ques...
Coding Interview Books -
Cracking the coding interview - amzn.to/3h70jXy
Data structure and algorithms made easy - amzn.to/3jgMl7U
Contact Me through this Form - webrewrite.com...
Website - webrewrite.com/
Join Membership - / @programmingtutorials1m
Super explanation brother... thanks
Thank you very much
@@ProgrammingTutorials1M 😁😁😁😁😁😁
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
brooo thank u!! :')
pairs repeat hoga
This will give duplicate pairs like for given sum 4 pairs will be -> 1+3 and 3+1 , I got rejected in the interview because I wrote the code in this approach , interviewer just said thanks and left the meeting
I had an interview earlier this week they asked me solve it without using loops but only using streams api.can you pleasehelp me.
this one is for sorted array
please upload for unsorted array also
Code link is present in the description box.
great job man. Congratulations
Thank you very much.
Sir i subscribed ur channel and i will share it to my other frnds also ..awesome explantaion thank you sir
Thank you so much.
Super explanation..
Nice explanation. Sir sometimes it happens they ask to return array of such pairs how it could be done.
time complexity will be O(log n)...right ?
//for unsorted Arrays
// k mean sum and n length of arr
HashMapmap=new HashMap();
for(int i=0;i
What if you have repeating pairs how would you code it ?
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.
thank you soo much your videos are soo helpful
You're so welcome!
What if I want to find 6 variables not just 2 for the sum?
thanks a lot for geat explanation Sir.
You are most welcome
what if array has duplicate elements? then low++, high-- (on sum =9) wont work?
I solved it using a hashset
@@merxxibeaucoup9093 only if array in sorted it will work
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
you don't need if equal test, just use else which is the only case left
for Unsorted ?
what if duplicate are present or array with same elements like {2,2,2,2} and the required sum is 4
For this problem, The array elements are distinct and in a sorted order
what if there are multiple numbers like example array 122344 and we have sum equal to 5 then how to do in this way
Are you talking about duplicates in an array and in that case we have to find the pair?
Sir please do some questions related to list stack and que
This program asked on technical round.
Very nice ...plz make seris of interview prog...in this way
Thank you
Thank you
Thank you
does it only work with sorted array?
Yes
In case of unsorted array?
I'll create separate video for unsorted array.
What if the numbers in random order?
I think you have to sort the array before finding the pairs.
@@iamnew3353 but in that case using hashset is more efficient i think.
How about unsorted array sir?
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.
You can also check this tutorial for unsorted array - ruclips.net/video/MAjWstEJ8rM/видео.html
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...
Hi Ketan, here is the code
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;
}
Thank u sir
@@ketanmorey1953 Please share this channel with your friends and colleagues.
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] + ")");
}
}
}
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] + ")");
}
}
}
yes but what if there are duplicates numbers in the array??
work only with sorted array
Yes, I have mentioned in title
Tq
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.
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
This work only for sorted array.
Sprb
Thank you
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
It's O(n^2). You can but it's not an efficient way.
@@ProgrammingTutorials1M Thank you sir! This tutorial is really helpful for me.
Didnt consider duplicates
I have already mentioned in problem statement that array elements are distinct and in sorted order.
your voice is low
In my new videos, you won't face this issue
but 1+2+5=8 so it should print 1 2 5
it's not a pair then