Write a program that passes in an array and will then output:
1) all pairs that sum up to 10. 2) all unique pairs that sum to 10, no duplicates. 3) all unique pairs that sum to 10, no duplicates or reverse pairs.
I have the bellow so far but it only prints the following:
(1, 9)
(1, 9)
(4, 6)
(4, 6)
(5, 5)
(5, 5)
public class SumOfPairs {
public void pairedElements(int arr[], int sum)
{
int low = 0;
int high = arr.length -1;
while (low < high) {
if (arr[low] + arr[high] == sum) {
System.out.println(" ("+ arr[low] + ", " + arr[high] + ")");
}
if (arr[low] + arr[high] > sum) {
high--;
}
else {
low++;
}
}
}
public static void main(String[] args)
{
int arr[] = {1, 1, 2, 4, 4, 5, 5, 5, 6, 7, 9};
Arrays.sort(arr);
SumOfPairs sp = new SumOfPairs();
sp.pairedElements(arr, 10);
}