Skip to content

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

  1. Special Case:
  2. If k == 1, every element satisfies the condition, so return the input array.
  3. Iterate Through the Array:
  4. Maintain a variable streak to count the length of consecutive sequences.
  5. Reset the streak to 1 whenever the consecutive sequence is broken.
  6. When the streak reaches k, update the result array at the appropriate index.
  7. Fill Results:
  8. Initialize the result array with -1 to indicate positions that do not satisfy the condition.
  9. Store the last number of the streak in the result array for valid indices.

Complexity

  • Time complexity:
    $O(n)$, where n is 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;
    }
}