import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int N = in.nextInt(); int[] arr = new int[N]; for (int i = 0; i < N; i++) { arr[i] = in.nextInt(); } int len = getLength(arr); System.out.print(len); } private static int getLength(int[] arr) { if (arr == null) { return 0; } int N = arr.length; //key 正数个数与负数个数的差值, value index Map<Integer,Integer> map = new HashMap<>(); int Z = 0,F = 0;//正数个数,负数个数 int res = 0; for (int i = 0; i < N; i++) { if (arr[i] == 0) { //0 跳过,相当于取i之前的数 continue; } Z += arr[i] > 0 ? 1 : 0; F += arr[i] < 0 ? 1 : 0; int cha = Z - F; if (map.containsKey(cha)) { res = Math.max(res, i - map.get(cha)); }else { map.put(cha,i); } } return res; } }