#76 π’ 3254. Find the Power of K-Size Subarrays I π§ π
Welcome to another exciting blog post! π In this problem, weβre exploring the power of subarrays. Itβs all about finding whether elements are sorted and consecutive while dealing with dynamic arrays of size k
. Along the way, youβll enhance your problem-solving skills and coding mastery! Letβs jump into it! π»π€
π§© Problem Statement
You are given:
1οΈβ£ An array of integers nums
of length n
.
2οΈβ£ A positive integer k
.
The power of an array is defined as:
- Its maximum element if all elements are consecutive integers and sorted in ascending order.
-1
otherwise.
Your goal is to find the power of all subarrays of size k
and return the results as an array of size n - k + 1
.
π Examples
Example 1
Input:
nums = [1, 2, 3, 4, 3, 2, 5]
k = 3
Output:
[3, 4, -1, -1, -1]
Explanation:
- Subarray
[1, 2, 3]
: Consecutive, max = 3 β - Subarray
[2, 3, 4]
: Consecutive, max = 4 β - Subarray
[3, 4, 3]
: Not consecutive β - Subarray
[4, 3, 2]
: Not sorted β - Subarray
[3, 2, 5]
: Not consecutive β
Example 2
Input:
nums = [2, 2, 2, 2, 2]
k = 4
Output:
[-1, -1]
Explanation:
- Subarray
[2, 2, 2, 2]
: Not consecutive β - Subarray
[2, 2, 2, 2]
: Not consecutive β
Example 3
Input:
nums = [3, 2, 3, 2, 3, 2]
k = 2
Output:
[-1, 3, -1, 3, -1]
π οΈ Constraints
- \[1 \leq n = \text{nums.length} \leq 500\]
- \[1 \leq \text{nums}[i] \leq 10^5\]
- \[1 \leq k \leq n\]
π‘ Approach
The problem asks us to analyze consecutive subarrays of size k
to determine their βpower.β This involves:
1οΈβ£ Checking if the subarray is sorted in ascending order.
2οΈβ£ Verifying if elements are consecutive integers.
3οΈβ£ Returning the maximum element if conditions are met; otherwise, return -1
.
π Basic Solution: Brute Force
In the brute force approach:
- Iterate through all subarrays of size
k
. - For each subarray, check if itβs sorted and consecutive.
- Return the maximum element if valid; else, return
-1
.
π» Code for Brute Force Solution
class Solution:
def resultsArray(self, nums: List[int], k: int) -> List[int]:
n = len(nums)
results = []
for i in range(n - k + 1):
subarray = nums[i:i + k]
sorted_subarray = sorted(subarray)
# Check if sorted and consecutive
if sorted_subarray == list(range(sorted_subarray[0], sorted_subarray[0] + k)):
results.append(max(subarray))
else:
results.append(-1)
return results
β³ Time Complexity of Brute Force
- Sorting each subarray of size
k
: \(O(k \log k)\) - For \(n - k + 1\) subarrays: \(O((n - k + 1) \cdot k \log k)\).
- Overall Time Complexity: \(O(n \cdot k \log k)\).
π Optimized Solution
Using an efficient prefix-based approach, we can track if numbers are consecutive:
- Use a prefix array
f
, wheref[i]
indicates if the current number is part of a consecutive sequence. - Traverse the array only once, making the solution linear.
π» Code for Optimized Solution
class Solution:
def resultsArray(self, nums: List[int], k: int) -> List[int]:
n = len(nums)
f = [1] * n # Prefix array to track consecutive sequence
# Build the prefix array
for i in range(1, n):
if nums[i] == nums[i - 1] + 1:
f[i] = f[i - 1] + 1
# Determine results for subarrays of size k
return [nums[i] if f[i] >= k else -1 for i in range(k - 1, n)]
β³ Time Complexity of Optimized Solution
- Building the prefix array: \(O(n)\)
- Constructing the results array: \(O(n)\).
- Overall Time Complexity: \(O(n)\).
π Example Walkthrough
Input:
nums = [1, 2, 3, 4, 3, 2, 5]
k = 3
Step 1: Build Prefix Array
- Initialize
f = [1, 1, 1, 1, 1, 1, 1]
. - Traverse
nums
to check consecutive elements:- \(f[1] = 2\) since \(nums[1] = nums[0] + 1\).
- \(f[2] = 3\) since \(nums[2] = nums[1] + 1\).
- \(f[3] = 4\) since \(nums[3] = nums[2] + 1\).
- \(f[4] = 1\) since \(nums[4] \neq nums[3] + 1\).
Step 2: Construct Results Array
- For \(i = 2\) (subarray
[1, 2, 3]
): Power = 3. - For \(i = 3\) (subarray
[2, 3, 4]
): Power = 4. - For \(i = 4\) (subarray
[3, 4, 3]
): Power = -1.
Final Output:
[3, 4, -1, -1, -1]
π Edge Cases
1οΈβ£ Smallest Inputs:
- \(nums = [1], k = 1\): Always return
nums
.
2οΈβ£ No Consecutive Elements:
- \(nums = [10, 20, 30], k = 2\): All elements will return
-1
.
3οΈβ£ All Same Elements:
- \(nums = [5, 5, 5], k = 2\): Always return
-1
.
π Conclusion
In this post, we explored how to efficiently compute the power of \(k\)-size subarrays. By leveraging prefix arrays, we reduced the time complexity from \(O(n \cdot k \log k)\) to \(O(n)\). Whether youβre preparing for an interview or just expanding your coding arsenal, this problem is a great way to practice sliding window, prefix arrays, and array manipulation! πβ¨
Happy coding! π»π