Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int test(int* arr, int len, int k) {
- int minIndex = 0;
- int maxIndex = 0;
- int start = 0;
- int end = 0;
- int maxLength = 0;
- for (int i = 1; i < len; i++) {
- end = i;
- // Update the minimum and maximum if necessary while scanning the array
- if (arr[i] < arr[minIndex])
- minIndex = i;
- if (arr[i] > arr[maxIndex])
- maxIndex = i;
- // Check the new maximum/minimum doesn't make the subarray invalid
- if (arr[maxIndex] - arr[minIndex] > k) {
- // We broke the subarray, check the length of the longest subarray until now
- if (end - start > maxLength)
- maxLength = end - start;
- // Restart from the second element, then the third, and so on
- start++;
- maxIndex = minIndex = start;
- i = start;
- }
- }
- return maxLength;
- }
Add Comment
Please, Sign In to add comment