Given an array of unique numbers and a value, a valid triplet is a set of three numbers (not necessarily continuous in the array) that add up to the value. Write a program to count all valid triplets. Print the number of valid triplets. Hint: Can you solve this in O(n^2) steps?
Input : First line contains N and Sum (Space separated). Second line contains space separated list of N numbers.
Output: Number of triplets such that sum possible
Constraints:
- 0 < N < 100
- All numbers in the list are unique and between -10^4 to 10^4
- -10^3 < Sum < 10^
3
# include <stdio.h>
int find3Numbers(int A[], int arr_size, int sum)
{
int l, r,c=0,i,j,k;
for (i = 0; i < arr_size-2; i++)
{
for (j = i+1; j < arr_size-1; j++)
{
for (k = j+1; k < arr_size; k++)
{
if (A[i] + A[j] + A[k] == sum)
{
c++;
}
}
}
}
return c;
}
int main()
{
int A[100];
int sum;
int ans;
int size,i;
scanf("%d%d",&size,&sum);
for(i=0;i<size;i++)
scanf("%d",&A[i]);
ans=find3Numbers(A, size, sum);
printf("%d",ans);
return 0;
}
IT WILL error: #include expects "FILENAME" or
ReplyDeleteNO , it wont
Delete