Find pair of given sum in java






Example 1 in java>
arr = { 1, 3, 2, 4, 3, 5};
Pair with sum = 6 are 3,3 ,, 1,5 ,, 2,4

Program/ example to 
Find pair of given sum in java>

//Find pair of given sum
public class FindPairOfGivenNumer {
    public static void main(String args[]) {
          int[] arr = { 1, 3, 2, 4, 3, 5};
          int sum = 6;  //3,3    ,, 1,5  ,, 2,4
          findPairOfGivenSum(arrsum);
    }

    public static void findPairOfGivenSum(int[] arrint sum) {

          int pairCount = 0;

          System.out.println("Pairs= ");
          // Consider all possible pairs and check their sums
          for (int i = 0; iarr.lengthi++)
                 for (int ji + 1; jarr.lengthj++)
                       if ((arr[i] + arr[j]) == sum){
                              System.out.println(arr[i] +" "arr[j]);
                              pairCount++;
                       }

          System.out.printf("count = "pairCount);
    }
}

//output
/*
Pairs=
1 5
3 3
2 4
count = 3

*/

Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.

Related >





eEdit
Must read for you :