Intuition¶
The problem requires identifying subarrays of length k where the elements form a consecutive sequence. This can be achieved by tracking streaks of consecutive numbers while iterating through the array.
Approach¶
- Special Case:
- If
k == 1, every element satisfies the condition, so return the input array. - Iterate Through the Array:
- Maintain a variable
streakto count the length of consecutive sequences. - Reset the streak to 1 whenever the consecutive sequence is broken.
- When the streak reaches
k, update the result array at the appropriate index. - Fill Results:
- Initialize the result array with
-1to indicate positions that do not satisfy the condition. - Store the last number of the streak in the result array for valid indices.
Complexity¶
-
Time complexity:
$O(n)$, wherenis the length of the input array. A single pass is sufficient to calculate the streaks and update the result. -
Space complexity:
$O(n)$, for the result array.
Code¶
class Solution {
public int[] resultsArray(int[] nums, int k) {
if (k == 1) {
return nums;
}
int n = nums.length;
int[] result = new int[n - k + 1];
Arrays.fill(result, -1);
int streak = 1;
for (int i = 0; i < n - 1; i++) {
if (nums[i] + 1 == nums[i + 1]) {
streak++;
} else {
streak = 1;
}
if (streak >= k) {
result[i - k + 2] = nums[i + 1];
}
}
return result;
}
}