-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext.java
35 lines (34 loc) · 1.1 KB
/
text.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.util.Scanner;
public class Balanedsubarray {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int test = sc.nextInt();
for (int i = 0; i < test; i++) {
int count = 0;
int size = sc.nextInt();
// Try to use 0 as -1 and 1 as 1 so that the sum is 0 when they are equal.
int[] arr = new int[size];
for (int j = 0; j < size; j++) {
arr[j] = sc.nextInt();
}
System.out.println(findMaxLength(arr));
}
}
public static int findMaxLength(int[] nums) {
int maxlen = 0;
for (int start = 0; start < nums.length; start++) {
int zeroes = 0, ones = 0;
for (int end = start; end < nums.length; end++) {
if (nums[end] == 0) {
zeroes++;
} else {
ones++;
}
if (zeroes == ones) {
maxlen = Math.max(maxlen, end - start + 1);
}
}
}
return maxlen;
}
}