Triplet Sum in Array

QUESTION

Given an array A[] of n numbers and another number x, determine whether or not there exist three elements in A[] whose sum is exactly x.\n\nExpected time complexity is O(n^2).\n\nInput:\n\nThe first line of input contains an integer T denoting the number of test cases.\nThe first line of each test case is n and x, n is the size of array.\nThe second line of each test case contains n integers representing array elements C[i].\n\nOutput:\n\nPrint 1 if there exist three elements in A whose sum is exactly x, else 0.\n\nConstraints:\n\n1 T 100\n1 N 200\n1 A[i] 1000.

“TESTCASE_1”: “2\n6 13\n1 4 45 6 10 8\n5 10\n1 2 4 3 6\n###—###SEPERATOR—###—\n1\n1”, “TESTCASE_2”: “2\n6 13\n11 14 15 16 3 5\n8 1\n4 12 19 13 6\n###—###SEPERATOR—###—\n0\n0”, “TESTCASE_3”: “0\n###—###SEPERATOR—###—\n0”, “TESTCASE_4”: “0\n###—###SEPERATOR—###—\n0”, “TESTCASE_5”: “0\n###—###SEPERATOR—###—\n0

ANSWER

# include <stdio.h>
int main()
{
  int t;
  scanf("%d",&t);
  while(t--)
  {
    int n,sum,i,j,k;
    scanf("%d",&n);
    scanf("%d",&sum);
    int arr[n];
    for(i=0;i<n;i++)
      scanf("%d",&arr[i]);
    int flag=0;
    for(i=0;i<n;i++)
    {
      for(j=0;j<n;j++)
      {
        if(i!=j)
        {
          for(k=0;k<n;k++)
          {
            if(k!=i && k!=j)
            {
              if(arr[i]+arr[j]+arr[k]==sum)
                flag=1;
            }
          }
        }
      }
    }
    printf("%d",flag);
    printf("\n");
  }
  return 0;
}
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
Best Wordpress Adblock Detecting Plugin | CHP Adblock