Java program that finds all pairs in an array that add up to a given sum

Category : Java | Sub Category : Java Programs | By Prasad Bonam Last updated: 2023-08-15 05:13:43 Viewed : 321


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

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

In this program, the findPairs method takes an array of integers and a target sum as parameters. It uses a hash map to keep track of numbers seen so far and their corresponding complements needed to achieve the target sum. If a numbers complement is already in the map, a pair is found.

The main method demonstrates how to use the findPairs method with an example array and target sum. It then prints out the pairs that sum up to the given target.

When you run this program with the provided example array and target sum, the output will be:

python
Pairs with sum 10: 3, 7 1, 9


Here is a Java program that finds all pairs in an array that add up to a given sum:

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

This program defines a findPairs method that takes an array of integers and a target sum as input. It uses a hash map to keep track of the count of each number in the array and then iterates through the array to find pairs that add up to the target sum. The program prints out the pairs it finds or a message if no pairs are found.

When you run the provided main method with the given array and target sum, the output will be:

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

Search
Related Articles

Leave a Comment: