3255. Find the Power of K-Size Subarrays II LeetCode Solution

In this guide, you will get 3255. Find the Power of K-Size Subarrays II LeetCode Solution with the best time and space complexity. The solution to Find the Power of K-Size Subarrays II problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Find the Power of K-Size Subarrays II solution in C++
  4. Find the Power of K-Size Subarrays II solution in Java
  5. Find the Power of K-Size Subarrays II solution in Python
  6. Additional Resources
3255. Find the Power of K-Size Subarrays II LeetCode Solution image

Problem Statement of Find the Power of K-Size Subarrays II

You are given an array of integers nums of length n and a positive integer k.
The power of an array is defined as:

Its maximum element if all of its elements are consecutive and sorted in ascending order.
-1 otherwise.

You need to find the power of all subarrays of nums of size k.
Return an integer array results of size n – k + 1, where results[i] is the power of nums[i..(i + k – 1)].

Example 1:

Input: nums = [1,2,3,4,3,2,5], k = 3
Output: [3,4,-1,-1,-1]
Explanation:
There are 5 subarrays of nums of size 3:

[1, 2, 3] with the maximum element 3.
[2, 3, 4] with the maximum element 4.
[3, 4, 3] whose elements are not consecutive.
[4, 3, 2] whose elements are not sorted.
[3, 2, 5] whose elements are not consecutive.

Example 2:

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

Example 3:

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

Constraints:

1 <= n == nums.length <= 105
1 <= nums[i] <= 106
1 <= k <= n

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

3255. Find the Power of K-Size Subarrays II LeetCode Solution in C++

class Solution {
 public:
  // Same as 3254. Find the Power of K-Size Subarrays I
  vector<int> resultsArray(vector<int>& nums, int k) {
    vector<int> ans;
    int start = 0;

    for (int i = 0; i < nums.size(); ++i) {
      if (i > 0 && nums[i] != nums[i - 1] + 1)
        start = i;
      if (i >= k - 1)
        ans.push_back(i - start + 1 >= k ? nums[i] : -1);
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

3255. Find the Power of K-Size Subarrays II LeetCode Solution in Java

class Solution {
  // Same as 3254. Find the Power of K-Size Subarrays I
  public int[] resultsArray(int[] nums, int k) {
    final int n = nums.length;
    int[] ans = new int[n - k + 1];
    int start = 0;

    for (int i = 0; i < n; ++i) {
      if (i > 0 && nums[i] != nums[i - 1] + 1)
        start = i;
      if (i >= k - 1)
        ans[i - k + 1] = i - start + 1 >= k ? nums[i] : -1;
    }

    return ans;
  }
}
// code provided by PROGIEZ

3255. Find the Power of K-Size Subarrays II LeetCode Solution in Python

class Solution:
  # Same as 3254. Find the Power of K-Size Subarrays I
  def resultsArray(self, nums: list[int], k: int) -> list[int]:
    ans = []
    start = 0

    for i, num in enumerate(nums):
      if i > 0 and num != nums[i - 1] + 1:
        start = i
      if i >= k - 1:
        ans.append(num if i - start + 1 >= k else -1)

    return ans
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.