The problem is that their are given one array and one number we have to find that unique pair of numbers,when these number are added then output is same as the given number.as example ,let there is an array int [] A = { 8, 7, 2, 5, 3, 1 }; and number is int sum = 10; now we ahve to find out the pair of number that make sum equal to number 10.then that numbers will be (8,2) and (7,3).
so to solve this problem i have given some solution of this problem.these are following.
Method #1
so to solve this problem i have given some solution of this problem.these are following.
Method #1
public class PairsOfElementsInArray
{
static void findThePairs(int inputArray[], int inputNumber)
{
System.out.println("Pairs of elements whose sum is "+inputNumber+" are : ");
for (int i = 0; i < inputArray.length; i++)
{
for (int j = i+1; j < inputArray.length; j++)
{
if(inputArray[i]+inputArray[j] == inputNumber)
{
System.out.println(inputArray[i]+" + "+inputArray[j]+" = "+inputNumber);
}
}
}
}
public static void main(String[] args)
{
findThePairs(new int[] {4, 6, 5,
-10, 8, 5, 20}, 10);
findThePairs(new int[] {12, 23,
125, 41, -75, 38, 27, 11}, 50);
}
}
Method #2
import
java.util.Arrays;
public class PairsOfElementsInArray
{
static void findThePairs(int inputArray[], int inputNumber)
{
Arrays.sort(inputArray);
System.out.println("Pairs of elements whose sum is "+inputNumber+" are : ");
int i = 0;
int j = inputArray.length-1;
while (i < j)
{
if(inputArray[i]+inputArray[j] == inputNumber)
{
System.out.println(inputArray[i]+" + "+inputArray[j]+" = "+inputNumber);
i++;
j--;
}
else if (inputArray[i]+inputArray[j] < inputNumber)
i++;
else if (inputArray[i]+inputArray[j] > inputNumber)
j--;
}
}
public static void main(String[] args)
{
findThePairs(new int[] {4, 6, 5,
-10, 8, 5, 20}, 10);
findThePairs(new int[] {12, 23,
10, 41, 15, 38, 27}, 50);
}
}
Output:
Note :complexity of method#1 is O(n2) and method#2 is O(n).
Method #3
OutPut:
Method #4
Output:
8 and 2
7 and 3
Method #3
import java.util.*;
class PairSum
{
public static void
printPairsUsingSet(int[] numbers, int n){
if(numbers.length
< 2){
return;
}
HashSet<Integer> set = new HashSet<>(numbers.length);
for(int value :
numbers)
{
int target = n - value;
// if target number is not in set then add value that
comes from array
if(!set.contains(target)) set.add(value);
else System.out.printf("(%d, %d) %n", value, target);
}
}
public static void
main(String[] args)
{
int intArr[] =
{4, 6, 5, -10, 8, 5, 20};
int sum=10;
printPairsUsingSet(intArr,
sum);
}
}
OutPut:
Method #4
import java.util.HashMap;
import java.util.Map;
class PairsOfElementsInArray {
//
Naive method to find a pair in an array with given sum
public static void findPair(int[] A,
int sum) {
// create an empty Hash Map
Map<Integer,
Integer> map = new HashMap<>();
// do for each element
for (int i = 0; i < A.length; i++)
{
// check if pair (arr[i], sum-arr[i]) exists
// if difference is seen before, print the pair
if (map.containsKey(sum - A[i])) {
System.out.println(map.get(sum - A[i]) + " and " + A[i]);
}
map.put(A[i], A[i]);
}
}
public static void main(String[] args) {
int[] A = { 8, 7, 2, 5, 3, 1 };
int sum = 10;
findPair(A, sum);
}
}
//
complexity ===O(n )
// space complexity ===O(n)
8 and 2
7 and 3