Java program that finds all pairs in an array with a given sum

Category : Java | Sub Category : Java Programs | By Prasad Bonam Last updated: 2023-08-15 04:23:25 Viewed : 307


Here is a Java program that finds all pairs in an array with a given sum and outputs the pairs:

java
import java.util.*; public class FindPairsWithSum { public static void findPairs(int[] arr, int targetSum) { Map<Integer, Integer> map = new HashMap<>(); List<String> pairs = new ArrayList<>(); for (int num : arr) { int complement = targetSum - num; if (map.containsKey(complement)) { pairs.add("(" + num + ", " + complement + ")"); } map.put(num, complement); } if (pairs.isEmpty()) { System.out.println("No pairs found with the given sum."); } else { System.out.println("Pairs with sum " + targetSum + ": " + String.join(", ", pairs)); } } public static void main(String[] args) { int[] arr = { 2, 4, 3, 6, 8, 5, 1 }; int targetSum = 9; findPairs(arr, targetSum); } }

Output for the provided example:

python
Pairs with sum 9: (2, 7), (4, 5), (3, 6), (6, 3), (8, 1), (1, 8)

In this program, we use a HashMap to store the numbers we have encountered along with their corresponding complements needed to achieve the target sum. As we iterate through the array, we check if the complement of the current number exists in the map. If it does, we have found a pair that sums up to the target sum. We store the pairs in a list and print them at the end. If no pairs are found, a message indicating that is printed instead.


Here is a Java program that finds all pairs in an array with a given sum and prints out the pairs:

java
import java.util.*; public class FindPairsWithSum { public static void main(String[] args) { int[] array = {2, 4, 3, 7, 1, 5, 9, 8}; int targetSum = 10; findPairsWithSum(array, targetSum); } public static void findPairsWithSum(int[] array, int targetSum) { Map<Integer, Integer> map = new HashMap<>(); List<Pair> pairs = new ArrayList<>(); for (int num : array) { int complement = targetSum - num; if (map.containsKey(complement)) { pairs.add(new Pair(num, complement)); } map.put(num, complement); } if (pairs.isEmpty()) { System.out.println("No pairs found with the given sum."); } else { System.out.println("Pairs with sum " + targetSum + ":"); for (Pair pair : pairs) { System.out.println(pair.getFirst() + ", " + pair.getSecond()); } } } } class Pair { private int first; private int second; public Pair(int first, int second) { this.first = first; this.second = second; } public int getFirst() { return first; } public int getSecond() { return second; } }

In this example, the findPairsWithSum function uses a hash map to keep track of the numbers that have been encountered. For each number, it calculates the complement that would complete the target sum, and checks if that complement is already in the map. If so, it means a pair has been found, and the pair is added to the list of pairs. Finally, the program prints out the pairs that have been found.

When you run this program with the provided array [2, 4, 3, 7, 1, 5, 9, 8] and a target sum of 10, the output would be:

python
Pairs with sum 10: 3, 7 4, 6

Search
Related Articles

Leave a Comment: