w3resource

Java: Find all Pairs of elements in an array whose sum is equal to a specified number


22. Find pairs with a given sum

Write a Java program to find all pairs of elements in an array whose sum is equal to a specified number.

Pictorial Presentation:

Java Array Exercises: Pairs of elements in an array whose sum is equal to a specified number

Sample Solution:

Java Code :

// Define a class named Exercise22.
public class Exercise22 {
    // Create a static method named pairs_value that takes an integer array and an input number.
    static void pairs_value(int inputArray[], int inputNumber) {
        System.out.println("Pairs of elements and their sum : ");

        // Iterate through the elements of the inputArray.
        for (int i = 0; i < inputArray.length; i++) {
            // Iterate through the elements following the current i.
            for (int j = i + 1; j < inputArray.length; j++) {
                // Check if the sum of inputArray[i] and inputArray[j] equals the inputNumber.
                if (inputArray[i] + inputArray[j] == inputNumber) {
                    // Print the pair of elements and their sum.
                    System.out.println(inputArray[i] + " + " + inputArray[j] + " = " + inputNumber);
                }
            }
        }
    }

    // The main method for executing the program.
    public static void main(String[] args) {
        // Call the pairs_value method with a sample array and input number.
        pairs_value(new int[]{2, 7, 4, -5, 11, 5, 20}, 15);

        // Call the pairs_value method with another sample array and input number.
        pairs_value(new int[]{14, -15, 9, 16, 25, 45, 12, 8}, 30);
    }
}

Sample Output:

Pairs of elements and their sum :                                                                             
4 + 11 = 15                                                                                                
-5 + 20 = 15                                                                                                
Pairs of elements and their sum :                                                                             
14 + 16 = 30                                                                                                
-15 + 45 = 30 

Flowchart:

Flowchart: Java exercises: Find all pairs of elements in an array whose sum is equal to a specified number

For more Practice: Solve these Related Problems:

  • Write a Java program to find all unique pairs in an array whose sum is a prime number.
  • Write a Java program to find all pairs of numbers in an array that add up to a given number without using nested loops.
  • Write a Java program to find all pairs of numbers in a sorted array that sum to a given value.
  • Write a Java program to find all triplets in an array whose sum is equal to a specified number.

Go to:


PREV : Convert ArrayList to array.
NEXT : Check if two arrays are equal.

Java Code Editor:

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.