小易定义一个数字序列是完美的,当且仅当对于任意,都满足,即每个数字都要大于等于前面所有数字的和。
现在给定数字序列,小易想请你从中找出最长的一段连续子序列,满足它是完美的。
第一行数据组数。对于每组数据,第一行一个整数,接下来一行个整数表示序列。
对于每组数据,一行一个数字表示最长完美的连续子序列的长度。
2 5 1 3 9 2 6 5 4 2 9 16 7
3 3
public static void main(String[] args) { Scanner sc = new Scanner(System.in); int m = sc.nextInt(); while(m-- > 0) { int n = sc.nextInt(); int[] nums = new int[n]; for(int i = 0; i < n; i++) { nums[i] = sc.nextInt(); } System.out.println(longestSub(nums)); } } public static int longestSub(int[] nums) { int i = 0, j = 1, max = 0, sum = nums[0],n = nums.length; while(j < n) { if(nums[j] >= sum) { sum += nums[j]; max = Math.max(max, j-i+1); j++; } else { sum -= nums[i]; i++; } } return max; }