How to get rid of getting NZEC error while submitting solution in java?

this Code works well in my Laptop but giving runtime error(NZEC) while submitting solution. PLEASE HELP!!!
here is my code---->

import java.util.*;

public class Devu_Charu_Game {

public static void main(String args[]) {
 
       Scanner sc = new Scanner(System.in);
    if (sc.hasNext()) {
        int N = sc.nextInt();
        int M = sc.nextInt();
        int a[] = new int[N];
        for (int i = 0; i < N; i++) {
            a[i] = sc.nextInt();
        }

        int x[] = Get(a, N);
        for (int j = 0; j < M; j++) {
            char C = sc.next().charAt(0);
            int Q = sc.nextInt();
            char W = sc.next().charAt(0);
            int temp = 0;
            if (C == '>') {
                for (int i = 0; i < x.length; i++) {
                    if (x[i] > Q) {
                        temp++;
                    }
                }
            }

            if (C == '<') {
                for (int i = 0; i < x.length; i++) {
                    if (x[i] < Q) {
                        temp++;
                    }
                }
            }

            if (C == '=') {
                for (int i = 0; i < x.length; i++) {
                    if (x[i] == Q) {
                        temp++;
                    }
                }
            }
            if (W == 'D') {
                if (temp % 2 == 0) {
                    System.out.println("C");
                } else {
                    System.out.println(W);
                }
            }
            if (W == 'C') {
                if (temp % 2 == 0) {
                    System.out.println("D");
                } else {
                    System.out.println(W);
                }
            }

        }
    }


}

public static int[] Get(int a[], int N) {
    int na[] = new int[(N * (N + 1) / 2)];
    int x = 0;
    for (int i = 0; i < N; i++) {
        int max = a[i];
        for (int j = i; j < N; j++) {
            if (max >= a[j]) {
                na[x] = max;
                x++;
            }
            if (a[i] < a[j]) {
                max = a[j];
                na[x] = max;
                x++;
            }
        }
    }
   
    return na;
}

}

N can be as large as 10^6.

2 Likes

use try and catch block.

follow this part by

throws java.lang.Exception

try using this before you take any input:

if (!sc.hasNextInt())
System.exit(0);

you may also try:

throws java.lang.Exception

thanks buddy!!
but i am unable to use for(long i=0;i<N;i++) with array.
index value of array is long and stored values is int.
its showing lossy conversion from long to int for—> (x[i]<Q)

its like I have to use different data structure? :sweat_smile:

I have tried this!!
its giving ArrayIndexOutOfBoundException.

can you send the sepcifc solution link? it’ll help in assessing the problem.

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 s = new Scanner(System.in);
	

		int n = s.nextInt();
		
		int factorial = 1;

	
	for(int i = n; i>= 1; i--) {
	 
		 factorial = factorial * i;
	}
	
	System.out.println(factorial);

}

}

I use this try catch method but although this method removes NZEC , it does not provide any solution at all .Please resolve this issue someone.

Thanks, it’s working now. I guess java’s still tryna catch itself!

Watch my detailed video on NZEC error, 100% your problem will be solved!!
Click here to watch video

yeah both the answers are coming as wrong, it is very weird.

This post was flagged by the community and is temporarily hidden.

am too not able to submit my java code…getting NZEC error
I used try catch and fast I/O methods too

Before using nextInt() , use sc.hasNext()
inside if and true accept input and initialise every variable with default values

For example
int g = 0
if( sc.hasNext())
{
g = sc.nextInt();
}
// proceed using g

thanks! @manvi_03 the first one worked!

1 Like

It helped…man

Yaa … U are correct

mine also same problem…But it not getting right after using this **


** command

IN JAVA,
if ur error is in line where u take input then try in this format ,

Scanner sc=new Scanner(System.in);
int takingInput=0; 
if(sc.hasNextInt()){
takingInput= sc.nextInt();
}