---
title: "Java program to find all pairs of elements in an array whose sum is equal to a given number."  
description: "Java program to find all pairs of elements in an array whose sum is equal to a given number."  
author: "Ravi Vishwakarma"  
published: 2024-07-17  
updated: 2024-07-17  
canonical: https://www.mindstick.com/interview/33947/java-program-to-find-all-pairs-of-elements-in-an-array-whose-sum-is-equal-to-a-given-number  
category: "java"  
tags: ["java", "javac"]  
reading_time: 3 minutes  

---

# Java program to find all pairs of elements in an array whose sum is equal to a given number.

Here's a step-by-step implementation in Java:

1. **Initialize a HashSet to store the elements we encounter.**
2. **Iterate through the array and for each element, calculate the complement that would sum to the target value.**
3. **Check if the complement exists in the HashSet. If it does, print the pair.**
4. **Add the current element to the HashSet.**

## Code Example:

```java
import java.util.*;

public class Main {
    public static void findPairs(int[] array, int sum) {
        // Create a HashSet to store elements
        HashSet<Integer> seen = new HashSet<>();

        System.out.println("Pairs with sum " + sum + ":");
        // Iterate through each element in the array
        for (int num : array) {
            // Calculate the complement
            int complement = sum - num;

            // Check if the complement is in the HashSet
            if (seen.contains(complement)) {
                System.out.println("(" + complement + ", " + num + ")");
            }

            // Add the current element to the HashSet
            seen.add(num);
        }
    }

    public static void main(String[] args) {
        int[] array = {1, 4, 5, 7, 3, 9, 2, 10, 0};
        int sum = 10;

        findPairs(array, sum);
    }
}
```

## Output,

![Java program to find all pairs of elements in an array whose sum is equal to a given number.](https://www.mindstick.com/interviewquestion/8f0649c5-90b5-451a-bdbc-83929787be07/images/3ad5a0a9-f944-4706-845b-c033253f7955.png)

## Answers

### Answer by Ravi Vishwakarma

Here's a step-by-step implementation in Java:

1. **Initialize a HashSet to store the elements we encounter.**
2. **Iterate through the array and for each element, calculate the complement that would sum to the target value.**
3. **Check if the complement exists in the HashSet. If it does, print the pair.**
4. **Add the current element to the HashSet.**

## Code Example:

```java
import java.util.*;

public class Main {
    public static void findPairs(int[] array, int sum) {
        // Create a HashSet to store elements
        HashSet<Integer> seen = new HashSet<>();

        System.out.println("Pairs with sum " + sum + ":");
        // Iterate through each element in the array
        for (int num : array) {
            // Calculate the complement
            int complement = sum - num;

            // Check if the complement is in the HashSet
            if (seen.contains(complement)) {
                System.out.println("(" + complement + ", " + num + ")");
            }

            // Add the current element to the HashSet
            seen.add(num);
        }
    }

    public static void main(String[] args) {
        int[] array = {1, 4, 5, 7, 3, 9, 2, 10, 0};
        int sum = 10;

        findPairs(array, sum);
    }
}
```

## Output,

![Java program to find all pairs of elements in an array whose sum is equal to a given number.](https://www.mindstick.com/interviewquestion/8f0649c5-90b5-451a-bdbc-83929787be07/images/3ad5a0a9-f944-4706-845b-c033253f7955.png)


---

Original Source: https://www.mindstick.com/interview/33947/java-program-to-find-all-pairs-of-elements-in-an-array-whose-sum-is-equal-to-a-given-number

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
