Given an array of integers representing batch quality scores, find the longest contiguous sequence where no two batch quality scores differ by more than 1.
Explanation: Step-by-step: Given the input [10, 11, 10, 11, 12], we initialize two pointers, left and right, to the start of the array. We then iterate through the array, expanding the window as long as the difference between the elements at the left and right pointers is less than or equal to 1. When the difference exceeds 1, we slide the left pointer to the right until the difference is less than or equal to 1 again. The maximum length of the window is our output, which is 5 in this case.
Explanation: Step-by-step: Given the input [1, 2, 3, 4, 5, 6, 7], we initialize two pointers, left and right, to the start of the array. We then iterate through the array, expanding the window as long as the difference between the elements at the left and right pointers is less than or equal to 1. Since the array is already sorted and the difference between consecutive elements is always 1, the maximum length of the window is the length of the array itself, which is 7 in this case.
Analyze constraints and compute optimal solutions step-by-step.
No dry run loaded.
🚀 Practice this problem
Run code, get AI hints & track streak