Find the Power of K-Size Subarrays I

Easy
1001.

Find the Power of K-Size Subarrays I

Find the Power of K-Size Subarrays I is an easy coding problem on arrays and sliding window, asked in Amazon, Google and TCS interviews.

The power of a subarray is its maximum element if the subarray is sorted ascending and consecutive — each element exactly one more than the previous — and -1 otherwise.

Return the power of every contiguous subarray of length k, in order.

Example 1

Input
nums = [1,2,3,4,3,2,5], k = 3
Output
[3,4,-1,-1,-1]
Explanation
Only `[1,2,3]` and `[2,3,4]` run consecutively.

Example 2

Input
nums = [2,2,2,2,2], k = 4
Output
[-1,-1]
Explanation
Equal values are not consecutive.

Example 3

Input
nums = [3,2,3,2,3,2], k = 2
Output
[-1,3,-1,3,-1]

Constraints

  • 1 <= n == nums.length <= 500
  • 1 <= nums[i] <= 10^5
  • 1 <= k <= n
Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {number[]} nums
 * @param {number} k
 * @return {number[]}
 */
var resultsArray = function(nums, k) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

Find the Power of K-Size Subarrays I — Problem & Solution

Find the Power of K-Size Subarrays I solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem.

Find the Power of K-Size Subarrays I is an easy coding problem on arrays and sliding window, asked in Amazon, Google and TCS interviews.

  • Difficulty: Easy
  • Topics: Arrays, Sliding Window
  • Asked at: Amazon, Google, TCS
  • Time limit: 2 s
  • Memory limit: 256 MB
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby

Problem statement

The power of a subarray is its maximum element if the subarray is sorted ascending and consecutive — each element exactly one more than the previous — and -1 otherwise.

Return the power of every contiguous subarray of length k, in order.

Example 1

Input: nums = [1,2,3,4,3,2,5], k = 3
Output: [3,4,-1,-1,-1]
Explanation: Only `[1,2,3]` and `[2,3,4]` run consecutively.

Example 2

Input: nums = [2,2,2,2,2], k = 4
Output: [-1,-1]
Explanation: Equal values are not consecutive.

Example 3

Input: nums = [3,2,3,2,3,2], k = 2
Output: [-1,3,-1,3,-1]

Constraints

  • 1 <= n == nums.length <= 500
  • 1 <= nums[i] <= 10^5
  • 1 <= k <= n

Hints for Find the Power of K-Size Subarrays I

  1. Hint 1

    A window qualifies only if every adjacent pair inside it differs by exactly 1, increasing.

  2. Hint 2

    When it does, the maximum is simply the last element.

  3. Hint 3

    A single-element window (k == 1) always qualifies.

How to solve Find the Power of K-Size Subarrays I

Check each window of length k for the "each element one more than the last" property; if it holds, the last element is the maximum.

Approach

  1. Slide a window of length k across the array.
  2. Verify nums[j] == nums[j-1] + 1 for every interior position.
  3. Record nums[i + k - 1] when it holds, and -1 otherwise.

Why it works

The consecutive-and-increasing condition makes the last element the maximum for free — no scan for a max is needed. Keeping a running count of consecutive steps turns this into one linear pass, but at n <= 500 the direct check per window is already comfortable.

Complexity

  • Time — O(n · k), or O(n) with a running count
  • Space — O(n) for the output

Pitfalls

  • Equal neighbours fail the test; the step must be exactly +1.
  • The output has n - k + 1 entries, not n.
  • With k == 1 every window trivially qualifies.

Reference solution

Python

from typing import List

def resultsArray(nums: List[int], k: int) -> List[int]:
    n = len(nums)
    out = []
    for i in range(n - k + 1):
        ok = all(nums[j] == nums[j - 1] + 1 for j in range(i + 1, i + k))
        out.append(nums[i + k - 1] if ok else -1)
    return out

Java

public static int[] resultsArray(int[] nums, int k) {
    int n = nums.length;
    int[] out = new int[n - k + 1];
    for (int i = 0; i + k <= n; i++) {
        boolean ok = true;
        for (int j = i + 1; j < i + k; j++) {
            if (nums[j] != nums[j - 1] + 1) { ok = false; break; }
        }
        out[i] = ok ? nums[i + k - 1] : -1;
    }
    return out;
}

C++

vector<int> resultsArray(vector<int>& nums, int k) {
    int n = (int) nums.size();
    vector<int> out;
    for (int i = 0; i + k <= n; i++) {
        bool ok = true;
        for (int j = i + 1; j < i + k; j++) {
            if (nums[j] != nums[j - 1] + 1) { ok = false; break; }
        }
        out.push_back(ok ? nums[i + k - 1] : -1);
    }
    return out;
}

JavaScript

var resultsArray = function(nums, k) {
    var n = nums.length;
    var out = [];
    for (var i = 0; i + k <= n; i++) {
        var ok = true;
        for (var j = i + 1; j < i + k; j++) {
            if (nums[j] !== nums[j - 1] + 1) { ok = false; break; }
        }
        out.push(ok ? nums[i + k - 1] : -1);
    }
    return out;
};

Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 1199 arrays problems · the whole catalogue

Learn the technique: Arrays · Sliding Window Technique