# Java Program to Find Groups of 3 Numbers from an Array which upon Adding Gives a Sum of Zero

In the previous article, we have discussed about Java Program to Print all SubArrays of a Given Array

In this article we are going to see how we can find groups of 3 numbers from an array which upon adding gives a sum of zero by using Java programming language.. This question is asked frequently in interviews.

## Java Program to Find Groups of 3 Numbers from an Array which upon Adding Gives a Sum of Zero

Here you need to find combination of three array elements which gives sum as 0.

For Example:

There is an array say arr= {9, 6, 8, -1,  2, -3, 4, 3, 2, 1}
Three array elements combinations whose sum are zero=
-1, -3, 4
2, -3, 1
-3, 2, 1

Let’s see different ways to find groups of 3 numbers from an array which upon adding gives a sum of zero.

### Method-1: Java Program to Find Groups of 3 Numbers from an Array which upon Adding Gives a Sum of Zero By Using Brute Force

Approach:

• Ask the user to enter the array size.
• Take the array size create an array and insert the elements.
• Run 3 for loops, first loop with iterator i from 0 to n, second loop iterator j from i+1 to n and third loop iterator k from j+1 to n.
• Add the elements at the loop index at i,j, and k, if they add upto zero then print else iterate.

Program:

import java.util.*;
public class Main
{
public static void main(String[] args)
{
// Asks the user to enter array size
System.out.println("Enter array size");
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int arr[] = new int[n];
// Takes the array as input
for(int i = 0; i < n; i++)
{
arr[i] = scan.nextInt();
}
// Prints the number groups
System.out.println("The numbers are-");
for (int i = 0; i < arr.length; i++)
{
for (int j = i + 1; j < arr.length; j++)
{
for (int k = j + 1; k < arr.length; k++)
{
if (arr[i] + arr[j] + arr[k] == 0)
{
System.out.println(arr[i]+", "+arr[j]+", "+arr[k]);
}
}
}
}
}
}

Output:

Enter array size
10
9 6 8 -1 2 -3 4 3 2 1
The numbers are-
-1, -3, 4
2, -3, 1
-3, 2, 1

### Method-2: Java Program to Find Groups of 3 Numbers from an Array which upon Adding Gives a Sum of Zero By Using Hashing

Approach:

• Ask the user to enter the array size.
• Take the array size create an array and insert the elements.
• Create an empty hashSet.
• Run two loops, one from 0 to n and the second one from i to n
• Add the two elements and check the hash for the additive inverse of the sum. If exists print the three elements.
• Add each scanned element to the hash

Program:

import java.util.*;
public class Main
{
public static void main(String[] args)
{
// Asks the user to enter array size
System.out.println("Enter array size");
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
System.out.print("Enter array elements-");
int arr[] = new int[n];
// Takes the array as input
for(int i = 0; i < n; i++)
{
arr[i] = scan.nextInt();
}
System.out.println("The numbers are-");
// Creates a new hashSet
HashSet hash = new HashSet<>();
// Prints the number groups
for (int i = 0; i < arr.length; i++)
{
hash.clear();
for (int j = i + 1; j < arr.length; j++)
{
// Adds the ith and jtj element and checks if there is an additive inverse in the array
int sum = -(arr[i] + arr[j]);
// if the sum equals to any number in the hashSet then prints
if(hash.contains(sum))
{
System.out.println(arr[i]+", "+arr[j]+", "+sum);
}
// Adds the scanned number to the hashSet
}
}
}
}

Output:

Enter array size
10
Enter array elements--3 5 -4 -2 2 1 -1 -3 0 4
The numbers are-
-3, -2, 5
-3, 1, 2
-3, 4, -1
5, -1, -4
5, -3, -2
-4, 4, 0
-2, 0, 2
2, -3, 1
1, 0, -1
-1, 4, -3

Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.

Related Java Programs: