TWTCLOSE - Editorial

Can anyone help me out here? I’m getting wrong answer status. What’s wrong in my code?
https://www.codechef.com/viewsolution/25035176

you didn’t consider elements bigger than 9
your code will fail on those test cases

1 Like

Thank you for pointing it out. It was kind of a silly mistake. Here’s the correct one.
https://www.codechef.com/viewsolution/25048355

This code is giving NZEC error Please help me :woozy_face:

class TWTCLOSE {

    int result = 0;
static int Program(String str, int val, int[] arr) {
	if (arr[val] == 0) {
		arr[val] = 1;
		result++;
	} else {
		arr[val] = 0;
		result--;
	}
	return result;
}

public static void main(String[] args) throws IOException {
	java.io.BufferedReader r = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
	int n = 0;
	int k = 0;
	n = r.read();
	r.readLine();
	k = r.read();
	r.readLine();
	int[] arr = new int[Character.getNumericValue(n)];
	k = Character.getNumericValue(k);
	if (n >= 1 && k <= 1000) {
		while (k > 0) {
			String str = r.readLine();
			int length = str.length();
			str.trim();
			if (str.equalsIgnoreCase("closeAll")) {
				System.out.println("0");
				Arrays.fill(arr, 0);
				result = 0;
			} else {
				int val = Integer.parseInt((str.substring(length - 1, length)));
				System.out.println(Program(str, val-1, arr));
			}
			k--;
		}
	}
}

}