Question Name:Searching 6

#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

Leave a Reply

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.