w3resource

Java: Find all triplets equal to a given sum in a unsorted array of integers


74. Find triplets summing to a target value in an array

Write a Java program to find all triplets equal to a given sum in an unsorted array of integers.

Example:
Input :
nums = { 1, 6, 3, 0, 8, 4, 1, 7 }
Output:
Triplets of sum 7
(0 1 6)
(0 3 4)

Sample Solution:

Java Code:

// Import the necessary Java classes.
import java.util.Arrays;
// Define the 'solution' class.
class solution {
    // Define a method to find and print all triplets with a given sum.
    public static void find_and_print_all_Triplets(int[] nums, int sum, int alen)
    {
        System.out.println("\nTriplets of sum " + sum);
        // Iterate through the array, leaving space for at least three elements.
        for (int i = 0; i <= alen - 3; i++)
        {
            int k = sum - nums[i];
            int l_index = i + 1, h_index = nums.length - 1;
            // Use a two-pointer approach to find triplets that sum up to 'k'.
            while (l_index < h_index)
            {
                if (nums[l_index] + nums[h_index] < k) {
                    l_index++;
                }
                else if (nums[l_index] + nums[h_index] > k) {
                    h_index--;
                }
                else {
                    // Print the triplet and adjust the pointers.
                    System.out.println("(" + nums[i] + " " + nums[l_index] + " " + nums[h_index] + ")");
                    l_index++;
                    h_index--;
                }
            }
        }
    }
    // Define the main method for running the program.
    public static void main(String[] args)
    {
        // Initialize the array and its length.
        int[] nums = { 1, 6, 3, 0, 8, 4, 1, 7 };
        int alen = nums.length;
        System.out.println("\nOriginal array: " + Arrays.toString(nums));
        // Sort the array in ascending order.
        Arrays.sort(nums);
        int sum = 7;
        // Call the 'find_and_print_all_Triplets' method to find and print triplets with the given sum.
        find_and_print_all_Triplets(nums, sum, alen);
    }
}

Sample Output:

Original array: [1, 6, 3, 0, 8, 4, 1, 7]

Triplets of sum 7
(0 1 6)
(0 3 4)

Flowchart:

Flowchart: Find all triplets equal to a given sum in a unsorted array of integers.


For more Practice: Solve these Related Problems:

  • Write a Java program to find all quadruplets in an array that sum to a given value.
  • Write a Java program to find the number of unique triplets that sum to zero.
  • Write a Java program to find all triplets where the sum of two elements equals the third element.
  • Write a Java program to find all triplets in an array whose product is equal to a given value.

Go to:


PREV : Sort array where only two elements are out of place.
NEXT : Calculate the largest gap between sorted elements in array.

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.