Finding Sum – TCS CodeVita

The program must accept N integers and an integer P as the input. The program must print the number of subsets of size 3, the sum of whose elements is divisible by P. Since the number K of such subsets can be huge, the program must print K modulo 1009 as the output.
Note: The integer P is always a prime number.

Boundary Condition(s):
1 <= N <= 10000
1 <= P <= 50
1 <= Each integer value <= 1000

Input Format:
The first line contains N and P separated by a comma.
The second line contains N integers separated by a comma.

Output Format:
The first line contains K modulo 1009.

Example Input/Output 1:
Input:
4,5
5,10,15,20

Output:
4

Explanation:
The 4 possible subsets are given below.
5 10 15
5 10 20
5 15 20
10 15 20

Example Input/Output 2:
Input:
6,7
4,3,8,6,5,2

Output:
2

Explanation:
The 2 possible subsets are given below.
3 6 5
4 8 2

#include<stdio.h>
int main(){
  int n,i,j,k,sum=0,a[100],p,c=0;
  scanf("%d,%dn",&n,&p);
  for(i=0;i<n;i++)
  {
    scanf("%d,",&a[i]);
  }
  for (i = 0; i < n; i++) {
      for (j = i + 1; j < n;) {
         if (a[j] == a[i]) {
            for (k = j; k < n; k++) {
               a[k] = a[k + 1];
            }
            n--;
         } else
            j++;
      }
   }
  for(i=0;i<n;i++)
  {
    for(j=i+1;j<n;j++)
    {
      for(k=j+1;k<n;k++)
      {
        sum=a[i]+a[j]+a[k];
        if(sum%p==0)
          c++;
        sum=0;
      }
    }
  }
  printf("%d",c);
  return 0;
}

Leave a Reply

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

More posts. You may also be interested in.