#include<bits/stdc++.h>
using namespace std;
void findTriplets(int arr[], int n)
{
bool found = 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 (arr[i]+arr[j]+arr[k] == 0)
{
cout << arr[i] << " "
<< arr[j] << " "
<< arr[k] <<endl;
found = true;
}
}
}
}
if (found == false)
cout << " not exist "<<endl;
}
int main()
{
int n = 5;
int arr[n],i;
for(i=0;i<n;i++) {
cin>>arr[i];
}
findTriplets(arr, n);
return 0; //sujan
}
Problem Description
Given an array of distinct elements. The task is to find triplets in array whose sum is zero. Take the array as input.
Test Case 1
Input (stdin)
0 -1 2 -3 1
Expected Output
0 -1 1
2 -3 1
Test Case 2
Input (stdin)
1 -2 1 0 5
Expected Output
1 -2 1
No comments:
Post a Comment