Home / C / Find the elements which created the smallest partial sum modulo P that is at least K

Find the elements which created the smallest partial sum modulo P that is at least K

In this problem, you will be given such a sequence and two integers P and K. Your task is to find the elements which created the smallest partial sum modulo P that is at least K.
 
For example, consider the following sequence of integers:
12     13     15     11     16     26     11
Here N = 7. Suppose K = 0 the answer is 0 since 12 + 13 + 15 + 11 = 5 and 68 mod 17 is 0.
You may assume 1 ≤ N ≤ 100000.Input
The first line of the input contains the number of test cases, T.
Each test case begins with a line containing three integers, NK and P. This is followed by the values of a1,a2, …, aN, one per line. 

Output
Output one line per test case, containing the elements which created the smallest partial sum modulo P that is at least K, as described above.

Example
Input:
1
7 2 17
12
13
15
11
16
26
11
 
Output:
11 16 26
 

solution in c
#include "stdio.h"
#include "conio.h"

int main()
{
int i,j,k,br,temp,t,l,n,p,min=9999,test[100],sum=0,a[1000],res[100][100];

i=scanf("%d",&t);
temp=0;
while(temp<t)
{
i=scanf("%d %d %d",&n,&k,&p);
br=n;
for(i=0;i<n;j=scanf("%d",&a[i++])){}
for(j=1;j<=n;j++)
{
for(l=0;l<n-j+1;l++)
{sum=0;
for(i=0;i<j;i++)
{
sum+=a[l+i];
test[i]=a[l+i];
}
if(sum % p==k && sum<min)
{
min=sum;br=j;
for(i=0;i<j;res[temp][i]=test[i++]){}
}
}
if(br!=n)
goto A;
}
A: &nbsp; temp++;
}
for(i=-1;++i<t;printf("\n"))
for(j=0;res[i][j]!='\0';printf("%d ",res[i][j++])){}
return 1;
}

Check Also

How to Solve Linear Equation in One Variable In C Programming?

C Program to Solve any Linear Equation in One Variable Find complete C program to ...

Leave a Reply

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