ENTEXAM - Editorial

Hi,i wrote this program and it runs fine on my pc with the given example values but when i upload it, it shows wrong answer.can anybody please find the bug,please !!!
#include<stdio.h>
#include<stdlib.h>
void qs(int*,int,int);
int main()
{
int t;
scanf("%d",&t);
(t<=0||t>5)?exit(0):1;//constraint
while(t–)
{
int n,k,e,m;
scanf("%d%d%d%d",&n,&k,&e,&m);
(k<=0||k>1000)?exit(0):1;//constraint
(n<=0||n>1000)?exit(0):1;//constraint
(n<=k)?exit(0):1;//constraint
(e<=0||e>4)?exit(0):1;//constraint
(m<=0||m>1000000000)?exit(0):1;//constraint
int g[n][e];
int i,j;
for(i=0;i<n;i++)
for(j=0;j<e;j++)
{
if((i==(n-1))&&(j==(e-1))) break;
scanf("%d",&g[i][j]);
}
int h[n];
for(i=0;i<n;i++) //summation of each student grades
{
int sum=0;
for(j=0;j<e;j++)
{
if((i==(n-1))&&(j==(e-1))) break;
sum=sum+g[i][j];
}
h[i]=sum;
}
qs(h,0,(n-2));
int check;//keeps difference
if(h[n-1]>h[k-1]) printf(“0\n”);
else
{
check=(h[k-1]-h[n-1])+1;
if(check>m) printf(“Impossible\n”);
else printf("%d\n",check);

		}
	
		
		
	}
return(0);
}
void qs(int a[],int start,int end) //quicksort
{
	int i,swap,pivot=end,index=start;
	if(start>=end) return;
	for(i=start;i<end;i++)
	{
		if(a[pivot]<=a[i])
		{
			swap=a[index];
			a[index]=a[i];
			a[i]=swap;
			++index;
		}
	}
	swap=a[index];
	a[index]=a[pivot];
	a[pivot]=swap;
	pivot=index;
	qs(a,start,(pivot-1));
	qs(a,(pivot+1),end);
}

#include<stdio.h>
void sort(unsigned long*,int);
void sort(unsigned long *a,int n)
{
int i,j;
unsigned long t;

for(i=0;i<n-1;i++)
{
    for(j=i+1;j<n;j++)
    {
        if(a[i]<a[j])
        {
            t=a[i];
            a[i]=a[j];
            a[j]=t;
        }
    }
}

}
int main()
{

 int T,N,K,E,i,j,k1;unsigned long M,s2,s,val;
unsigned long a[10000][4],sum[10000];
 scanf("%d",&T);
 for(i=0;i<T;i++)
 {
     scanf("%d",&N);
     scanf("%d",&K);
     scanf("%d",&E);
     scanf("%u",&M);
     for(j=0;j<N-1;j++)
     {
         for(k1=0;k1<E;k1++)
         {
             scanf("%u",&a[j][k1]);
         }
     }
     s2=0.0d;
     for(k1=0;k1<E-1;k1++)
     {
         scanf("%u",&a[N-1][k1]);s2=s2+a[N-1][k1];
     }
     for(j=0;j<N-1;j++)
     {
         s=0.0d;
         for(k1=0;k1<E;k1++)
         {
             s=s+a[j][k1];
         }
         sum[j]=s;
     }
     sort(sum,N-1);
     val=sum[K-1];
     if((val-s2)>M)
        printf("\n Impossible");
     else
        printf("\n %u",(val-s2+1));

 }
 return 0;

}

Why ami I getting WA?

//Whats wrong in this code???

#include<stdio.h>

int main()
{
long long int a,n,m,e,k,i,j,temp;
scanf("%lld",&a);
while(a–)
{scanf("%lld%lld%lld%lld",&n,&k,&e,&m);

long long int z[e],s[n],v[e],show=0,s2=0,marks=n-k-1;;

for(i=0;i<n-1;i++)
{   s[i]=0;

    {
    for(j=0;j<e;j++)

        {   scanf("%lld",&z[j]);
        s[i]+=z[j];
        }

    }
}
for(j=0;j<e-1;j++)
    {scanf("%lld",&v[j]);
    s2+=v[j];
    }



for (i = 0; i < n-1; ++i)
{
    for (j = i + 1; j < n-1; ++j)
    {
        if (s[i] < s[j])
        {
            temp =  s[i];
            s[i] = s[j];
            s[j] = temp;
        }

    }

}

if(s2+m>s[marks])
{
show=s[marks]+1-s2;
printf("%lld\n",show);
}
else if(s2+m<=s[marks])
printf(“Impossible\n”);

}
return 0;

}

can anyone tell what is wrong?

#include <stdio.h>

int main(void) {
int T,N,K,E,M;
scanf("%d",&T);
int i,k,j,l,m;
for(i=0;i<T;i++)
{
scanf("%d %d %d %d",&N,&K,&E,&M);
int a,arr2[N];
int sum;
for(j=0;j<N-1;j++)
{
sum=0;
for(k=0;k<E;k++)
{
scanf("%d",&a);
sum=sum+a;
}
arr2[j]=sum;
// printf("%d\n",arr2[j]);
}
sum=0;
for(k=0;k<E-1;k++)
{
scanf("%d",&a);
sum=sum+a;
}
arr2[j]=sum;
// printf("%d\n",arr2[j]);

    for(l=0;l<N-1;l++)
    {
        for(m=l+1;m<N;m++)
        {
            if(arr2[l]<arr2[m])
            {
                int temp=arr2[l];
                arr2[l]=arr2[m];
                arr2[m]=temp;
            }
        }
     //  printf("   %d   ",arr2[l]);
    }
    int result=arr2[K-1]-arr2[N-1]+1;
    result=abs(result);
    if(result<=M)
    printf("%d\n",result);
    else
    printf("Impossible\n");
    
}
return 0;

}

#include
#include
using namespace std;
int main()
{
int t;
cin>>t;
int t1=t;
long long int n,k,e,m,temp,sum;
while(t–)
{
cin>>n>>k>>e>>m;
long long int a[n];

    //Take input
    for(long long int i=0;i<n-1;i++)
    {
        sum = 0;
        for(long long int j=0;j<e;j++)
        {
            cin>>temp;
            sum += temp;
        }
        a[i] = sum;
    }
    sum = 0;
    for(long long int j=0;j<e-1;j++)
    {
        cin>>temp;
        sum += temp;
    }
    a[n-1] = sum;

    //Do processing
    sort(a,a+n-1,greater<int>());
    long long int value = a[k-1];
    if(value<a[n-1])
    {
        cout<<"0"<<endl;
    }
    else if(value-a[n-1]+1<=m)
    {
        cout<<value-a[n-1]+1<<endl;
    }
    else
    {
        cout<<"Impossible"<<endl;
    }
}
return 0;

}
What is wrong in this code?

All those who are getting wrong answers, its highly suggested to do a dry run of some test case which is giving a wrong answer, checking the corner cases. Because this is a simple ad-hoc problem and most of your simple logic implemented is correct. Though, you can also check someone else’s code if you are not able to figure out the mistake in your code.

1 Like

Hi,
Can you please help me find the bug in my code sumbmitted in the following link.
CodeChef: Practical coding for everyone
It works fine, but not accepted when submittd.
Thanks in advance!

#include<stdio.h>
main()
{
int T,N,E,K,M,i,j,c,temp=0,sm,kl,kpl,smm=0;
scanf("%d",&T);
while(T!=0)
{
scanf("%d",&N);
scanf("%d",&K);
scanf("%d",&E);
scanf("%d",&M);
int a[N][E],ca[N];
for(i=0;i<n-1;i++) {="" c=“0;” for(j=“0;j<E;j++)” {="" scanf("%d",&a[i][j]);="" c+=“a[i][j];” }="" ca[i]=“c;” }="" c=“0;” for(j=“0;j<E-1;j++)” {="" scanf("%d",&a[i][j]);="" c+=“a[i][j];” }="" ca[i]=“c;” sm=“c;” for(i=“0;i<N;i++)” {="" for(j=“0;j<N-i;j++)” {="" if(ca[j]<ca[j+1])="" {="" temp=“ca[j];” ca[j]=“ca[j+1];” ca[j+1]=“temp;” }="" }="" }="" kl=“ca[K-1];” kpl=“ca[K-2];” for(i=“M;i”>=0;i–)
{ int x=sm+i;
if((x>kl) && (x<kpl)) {="" smm=“i;” }="" }="" printf("%d\n",smm);="" t–;="" }="" }="" <="" code="">

Please tell me why my code is giving a wrong answer`import java.util.*;

class Entexam{

public static void main(String[] args){
	Scanner scanner = new Scanner(System.in);
	int T = scanner.nextInt();
	int N,K,E,M;
	for (int p=0;p<T;p++){
		N=scanner.nextInt(); //No. of students
		K=scanner.nextInt(); //These many students shall pass
		E=scanner.nextInt(); //no of tests
		M=scanner.nextInt(); // max marks in a test

		int[] marksOfOthers = new int[N-1];	
		for(int j=0;j<N-1;j++){

			marksOfOthers[j]=0;
			for(int k=0;k<E;k++){

				marksOfOthers[j]+=scanner.nextInt();
			}

		}
		//Now marks have been taken of N-1 guys
		int mySum=0;
		for(int i=0;i<E-1;i++){
			mySum+=scanner.nextInt();
		}	

		//I need to sort the marks array - I am using bubble sort for now
		Arrays.sort(marksOfOthers);
		//Now array sorted  , now I just need to get its score more than the kth person's score	

		int targetScore = marksOfOthers[N-K-1] - mySum + 1;
		if(targetScore>M){
		//impossible
		System.out.println("Impossible");
		}else if (targetScore<0){

		//possible
		System.out.println(0);
		}else{
			System.out.println(targetScore);
		}



			
	}

	scanner.close();

}

}
`

This ruby code works fine for me elsewhere but gives NZEC while submission, any suggestions ?

t = gets.to_i
t.times do
  merit_list = []
  input = gets.split.map(&:to_i)
  n, k, e, m = input[0], input[1], input[2], input[3]
  (n-1).times do
    student_marks = gets.split.map(&:to_i)
    merit_list.push(student_marks.sum)
  end
  my_marks = gets.split.map(&:to_i)
  my_sum = my_marks.sum
  merit_list = merit_list.sort.reverse
  marks_to_get = merit_list[k-1] - my_sum + 1
  if (marks_to_get <= m)
    puts marks_to_get
  else
    puts "Impossible"
  end
end

#include <bits/stdc++.h>

using namespace std;
int main()
{
    int t,k,n,e;
    long int o,ma;
    vector<long int> m(100011);
    cin>>t;
    while(t--)
    {
        long int total;
        cin >> n>>k>>e>>ma;
        for(int i=0;i<n-1;i++)
	    {
	        total=0;
	        for(int j=0;j<e;j++)
		   {
		        cin>>o;
		        total+=o;
		    }
	        m[i]=total;
	    }
        total=0;
        sort(m.begin(),m.end());
        reverse(m.begin(),m.end());
        for(int i=0;i<e-1;i++)
	    {
	        cin>>o;
	        total+=o;
     	    }
	        long int x=total;
        long int y=m[n-k-1];
        long int mx=max(0L,y-x+1);
        //cout<<mx;
        if(mx>ma)
	    cout<<"Impossible"<<endl;
        else
	    cout<<mx<<endl;
        m.clear(); 
    }
    return 0;
}

what’s wrong with my code?

Can someone take a look at my code. I’ve been trying to refine it all day!
If you want to be updated with my changes, kindly take a look over https://gist.github.com/salman-bhai/f51412bd45aa4c61c07071e089f9954e:

#include <bits/stdc++.h>
using namespace std;

int main() {
	int t;
	scanf("%d",&t);

	while(t--) {
		long long int n, k, e;
		long long int m;
		scanf("%lld %lld %lld %lld",&n,&k,&e,&m);

		long long int arr[n][e];
		long long int total[n-1]={0};

		for(long long int i = 0; i < n-1; i ++) {
			for(long long int j = 0; j < e; j ++) {
				scanf("%lld",&arr[i][j]);
				total[i] += arr[i][j];
			}
		}

		long long int sergey=0;
		for(long long int i = 0; i < e-1; i ++) {	
			long long int temp;
			scanf("%lld",&temp);
			sergey += temp;
		}

		sort(total, total+n-1);

		if(sergey > total[n-k-1])
			printf("1\n");
		else if(sergey <= total[n-k-1] && (total[n-k-1]-sergey+1 <= m))
			printf("%lld\n",(total[n-k-1]-sergey+1));
		else
			printf("Impossible\n");

	}	

	return 0;
}

[1]: http:// https://gist.github.com/salman-bhai/f51412bd45aa4c61c07071e089f9954e

@sbshah

The first error I noticed in your code is for cases where his total score is already large enough, that he can take get admission despite getting 0 in final exam.

Eg-

Compilation Successful
Input (stdin)
1
4 2 3 10
1 1 1
2 2 2
3 4 5
10 10
Your Output
1
Expected Output-
0

In case where his total score exceeds everyone else’s score by such a large factor, that he need not score any mark in final exam, the output should be 0, as its possible to score 0 in exam. Your program is printing 1 for that case which is incorrect (if my interpretation of Q’s line “In total there are E entrance exams, in each of them one can score between 0 and M points, inclusively” is correct)

**Anybody help me out with my code it gives wrong answers on submission **

`#include<stdio.h>

int main()
{
long long int t,n,k,e,m,numb,i,j,z;
long long int na[10000];
scanf("%d",&t);
while(t–)
{
scanf("%lld%lld%lld%lld",&n,&k,&e,&m);
for(i=0;i<n;i++)
{ na[i]=0;
if(i==(n-1))
{ for(j=0;j<e-1;j++)
{
scanf("%lld",&z);
na[i]=na[i]+z;
}
}
else
{
for(j=0;j<e;j++)
{
scanf("%lld",&z);
na[i]=na[i]+z;
}
}

    }
    for(i=0;i<(n-1);i++)
    { 
        for(j=(i+1);j<(n-1);j++)
        {
            if(na[i]<na[j])
            { numb=na[i];
             na[i]=na[j];
             na[j]=numb;
                
            }
        }
    }
    
    numb=na[k-1]+1;
    numb=numb-na[n-1];
    if(numb<=m)
    printf("%lld\n",numb);
    else
    printf("Impossible\n");
    
}
return 0;

}`

enter code here
hhg
jvvv
#include<stdio.h>

int main()
{

long long int t,n,k,e,m,numb,i,j,z;

long long int na[10000];

scanf("%d",&t);

while(t–)
{

scanf("%lld%lld%lld%lld",&n,&k,&e,&m);

    for(i=0;i<n;i++)
    { 

na[i]=0;

       if(i==(n-1))
       {  
        `for(j=0;j<e-1;j++)
         {

          scanf("%lld",&z);

          na[i]=na[i]+z;
          }
        }
        else
        {

           for(j=0;j<e;j++)
         {

            scanf("%lld",&z);

           na[i]=na[i]+z;
          }
        } 
        
    }

      for(i=0;i<(n-1);i++)
    { 

               for(j=(i+1);j<(n-1);j++)
        {
               if(na[i]<na[j])

         { numb=na[i];

             na[i]=na[j];

             na[j]=numb;
                
            }
        }
    }
    

numb=na[k-1]+1;

numb=numb-na[n-1];

   if(numb<=m)

    printf("%lld\n",numb);

    else

    printf("Impossible\n");
    
}
return 0;

}

#include<stdio.h>
#include<algorithm>
int main(){
long long int t,n,k,e,m,i,j,sum,x,y,z,ans;
scanf("%lld",&t);
while(t--){
scanf("%lld %lld %lld %lld",&n,&k,&e,&m);int a[n-1];
for(i=0;i<(n-1);i++)
{sum=0;
for(j=0;j<e;j++){ scanf("%lld",&x);sum+=x;}
a[i]=sum;
}std::sort(a,a+n-1);
y=a[n-k-1];ans=y;
for(j=0;j<e-1;j++){scanf("%lld",&z);ans=ans-z;}
if(ans=>0 && ans<=m)printf("%lld\n",ans);else if(ans>m)printf("Impossible\n"); else printf("0\n"); 
}
return 0;
} 

https://www.codechef.com/viewsolution/13147275/
@Sergey Kulik @Vasya Antoniuk @Kevin Atienza
where is my solution going wrong ?

Help me what wrong with this???

#include<stdio.h>
#include<stdlib.h>
int main(){
long o,N,K,E,M,i,j,t,u,y=1;
scanf("%ld",&o);
while(o--){
int z=0;
y=1;
scanf(" %ld %ld %ld %ld",&N,&K,&E,&M);
long s[N];
for(i=0;i<(N-1);i++){
s[i]=0;
for(j=0;j<E;j++){
scanf(" %ld",&t); s[i]+=t;}}
s[N-1]=0;
for(i=0;i<(E-1);i++){scanf(" %ld",&t); s[N-1]+=t;}
for(i=0;i<(N-1);i++){
if(s[N-1]>s[i]) z++;}  if(z>=(N-K)) {printf("%d",0);} else{
u=s[N-1];
while(z<(N-K)){
for(j=0;j<(N-1);j++){if(s[j]>=s[N-1]){t=s[j]; break;}}
for(i=j;i<(N-1);i++){
if((s[i]>=s[N-1])&&(s[i]<t)) t=s[i];}
if((t-u)<M) s[N-1]=t+1; else{printf("Impossible"); y=0; break;}
z=0;
for(i=0;i<(N-1);i++){
if(s[N-1]>s[i]) z++;}
}if(y==1) printf("%d",(t-u+1));}}return 0;}

Can someone please help what is wrong in this code?

1 Like

@monsij

Your code fails at this test case

 Compilation Successful
Input (stdin)
1
4 2 3 11
10 10 10
10 10 10
10 10 10
10 10
Your Output
Impossible
Expected Output 
11

I feel the error is here-

for(i=0; i<=10;i++)
		{
			if((sum + i) > mark[n-k-1])
			{
				ans=i;
				flag=1;
				break;
			}
		}

I think i should be less than or equal to m, instead of 10 in loop condition. Try it and tell. :slight_smile:

Could someone please help me find the error?

#include
#include
using namespace std;

int main(){
  long long t,n,m,e,k;
  long long sum[10000]={};
  long s;
  int pos=0;
  cin>>t;
  for(int i=0;i<t;i++){
    cin >> n >> k>>e>>m;
    long int j;
    for(j=0;j<n-1;j++){
      s=0;
      for(int l=0;l<e;l++){
        cin>>s;
        sum[j]+=s;
      }
    }
    s=0;pos=j;
    for(int l=0;l<e-1;l++){
      cin>>s;
      sum[j]+=s;
    }
    sort(sum,sum+pos);
    long long x = sum[n-k-1]-sum[pos]+1;
    if(x<0)         cout<<0<<endl;
    else if(x<m)   cout<<x<<endl;
    else            cout<<"Impossible"<<endl;
  }
  return 0;
}

Thanks!