Ramu willing to play a game with Manimaran. So he
decided to ask three numbers from the given array to
make whose sum is zero. For this purpose he created an
array with distinct elements. The task is to find three
numbers in array whose sum is zero. Take the array as
input.
.
TEST CASE 1
INPUT
8 -1 2 -3 1
OUTPUT
0 -1 1
2 -3 1
Answers
Answer:
The idea is to store all the numbers in an array and then check individually which of them are making zero, and store them in a different array.
Explanation:
class find{
static void SumZero(int a[], int n)
{
boolean ismatch = true;
for (int i=0; i<n-2; i++)
{
for (int j=i+1; j<n-1; j++)
{
for (int k=j+1; k<n; k++)
{
if (a[i]+a[j]+a[k] == 0)
{
System.out.print(a[i]);
System.out.print(" ");
System.out.print(a[j]);
System.out.print(" ");
System.out.print(a[k]);
System.out.print("\n");
ismatch = true;
}
}
}
}
if (ismatch == false)
System.out.println(" There is no number making zero ");
}
public static void main(String[] args)
{
int a[] = {8, -1, 2, -3, 1};
int n =a.length;
SumZero(a, n);
}
}