Say No To Drugs Problem Runtime Error

Hello,

I am trying to solve the Say No to Drugs problem (problem code : NODRUGS)
(link : CodeChef).

With my code i am able to complete the test cases but on submitting i am getting a runtime error.
Can any one please help on this?

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
		Scanner sc = new Scanner(System.in);
		int t,n,k,l,max = 0;
		t = sc.nextInt();
		while(t-- > 0) {
		    n = sc.nextInt();
		    k = sc.nextInt();
		    if(k <= 0) {
		        System.out.println("No");
		        continue;
		    }
		    l = sc.nextInt();
		    if(n == 1) {
		        System.out.println("Yes");
		        continue;
		    }
		    int[] arr = new int[n];
		    for(int i = 0;i < n;i++) {
		        arr[i] = sc.nextInt();
		        if(max < arr[i])
		            max = arr[i];
		    }
		    System.out.println("Checkpoint 1");
		    if(max == arr[n-1]) {
		        System.out.println("Yes");
		        continue;
		    }
		    System.out.println("Checkpoint 2");
		    if(l == 1) {
		        System.out.println("No");
		        continue;
		    }
		    System.out.println("Checkpoint 3");
		    if(arr[n-1]*k*(l-1) == max) {
		        System.out.println("No");
		        continue;
		    }
		    System.out.println("Checkpoint 4");
		    System.out.println("Yes");
		    max = 0;
		}
	}
}

Your code is not taking the whole input of a test case. For instance, if the test case is
N=2 K = -1 L=5 and S = [100, 200]
Then your code will not take L and S as input in the test case because you have a condition before taking L and S[] as input, that
if (k<=0)
continue;
so they will be passed as input to the next test case.
Furthermore, in every case where you have written continue, the max is not getting reinitialized to 0 because you are re-initializing it at last.
I have modified your solution, it is now giving Correct Answer. here
Note: Take the whole input of every testcase, before processing it.

Ok will be noted.
Thanks for the quick help

1 Like