Searching 6

QUESTION

Given an array of distinct elements. The task is to find triplets in array whose sum is zero. Take the array as input.

ANSWER

#include <stdio.h>
void findTriplets(int[], int);
int main()
{
  	
    int n=5,i,arr[6];
    for(i=0;i<n;i++)
      scanf("%d",&arr[i]);
    //n = sizeof(arr)/sizeof(arr[0]);
    findTriplets(arr, n);
 //   return 0;
//}/
	return 0;
}
  void findTriplets(int arr[], int n)
{
    int found = 1,i,j,k;
    for (i=0; i<n-2; i++)
    {
        for (j=i+1; j<n-1; j++)
      {
            for (k=j+1; k<n; k++)
            {
                if (arr[i]+arr[j]+arr[k] == 0)
                {
                    printf("%d %d %d\n",arr[i],arr[j],arr[k]);
                    found = 1;
                }
            }
        }
    }
 
    // If no triplet with 0 sum found in array
    if (found == 0)
        printf("not exist");
 
}
 
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.

Powered By
CHP Adblock Detector Plugin | Codehelppro