zco matched brackets

Can someone pls help me. I wrote a code for this but It only gives the correct answer in a few cases. Thanks in advance
import java.util.*;

class PP5 {

public static void nested(int b[]) {
	long c = 0;
	long max = 0;
	int pos = 0;
	for (int i = 0; i < b.length; i++) {
		if (b[i] == 1) {
			c++;
		} else {
			if (c > max) {
				max = c;
				pos = i;
			}
			c = 0;
		}
	}
	System.out.print(max + " " + pos + " ");
}

public static void maxSeq(int b[]) {
	int c = 0;
	int x1 = 0;
	int max = 0;
	int pos = 0;
	for (int j = 0; j < b.length; j++) {
		if (b[j] == 1) {
			c++;
			// j++;
			//System.out.println(j);
			x1++;
		} else if (x1 > 1){
			x1--;
			c++;
			//System.out.println(x1 + " ll " + j);
		} else if (b[j] == 2 && x1 <= 1) {
			x1 = 0;
			if (c > max) {
				max = c + 1;
				pos = j + 1;
			}
			c = 0;
		}
	}
	pos -= max;
	pos++;
	System.out.println(max + " " + pos);
}

public static void main(String args[]) {
	Scanner sc = new Scanner(System.in);
	int n = sc.nextInt();
	int b[] = new int[n];
	for (int i = 0; i < n; i++) {
		b[i] = sc.nextInt();
	}
	nested(b);
	maxSeq(b);
}

}

Sorry, you can not ask questions related to live contest!!