// i // nums: [5,7,2,8] // j int longestWindow(int[] nums) { int max; for (int i = 0, j = 0; i < nums.length; i++) { // code using nums[i] to update the state // that might invalidate the window for (; invalid(); j++) { // code using nums[j] to update the state // and shrink the left edge while the window is invalid } // longest window so far = length([i, j]) max = max(ans, i - j + 1); } return max; }