Nobel Prize (March Codechef starter 2021 rated for div3)

import java.util.;
import java.lang.
;
import java.io.*;

/* Name of the class has to be “Main” only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
try{
// your code goes here
Scanner sc = new Scanner(System.in);

	int T = sc.nextInt();
	while(T!=0){
	    int N = sc.nextInt(), M = sc.nextInt();
	    int a[] = new int[N];
	    int j=0;
	    while(j<N){
	      a[j] = sc.nextInt(); 
	      j++;
	    }
	   
	   int t = sum(N);
	   int add=0;
	   
	   for(int k=0;k<N;k++){
	       add = a[k] + add;
	   }
	   if(t == add){
	       System.out.println("NO");
	       
	   }
	   else 
	   System.out.println("YES");
	   
	   	T--; 
		}
}
catch(Exception e){
    return ;
}
}
static int sum(int N){
    int sum = 0;
    for(int i=1;i<=N;i++){
        sum = sum + i;
    }
    return sum;
}

}

I Am getting time limit exceeded error .How can i reduce time limit for this code please help.

1 Like

You can simply observe that if the number of distinct elements is greater than or equal to m, then the chef won’t have the choice of picking a unique topic, which can be done in a single iteration.

1 Like

Thank you i got it.

One More thing i changed the code accordingly but even now i am getting same error

/* package codechef; // don’t place package name! */

import java.util.;
import java.lang.
;
import java.io.*;

/* Name of the class has to be “Main” only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
try{

Scanner sc = new Scanner(System.in);

int T = sc.nextInt();
while(T!=0){
    int N = sc.nextInt(), M = sc.nextInt();
    int a[] = new int[N];
    int j=0;
    while(j<N){
      a[j] = sc.nextInt(); 
      j++;
    }
	Set set = new HashSet<>();
	for(int t: a){
		set.add(t);
	}
	if(set.size() < M){
	    System.out.println("YES");
	}
	else
	System.out.println("NO");
   	T--; 
	}

}
catch(Exception e){
return ;
}
}

}

Sorry Asif but I don’t know Java and it looks like there must be some language specific error.

it’s Okay , no problem thank you for helping