# Solving Leetcode Interviews in Seconds with AI: Find the Power of K-Size Subarrays I


	# Introduction
	In this blog post, we will explore how to solve the LeetCode problem "3254" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like [Chatmagic](https://www.chatmagic.app), we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

	# Problem Statement
	> 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 <= 500 1 <= nums[i] <= 105 1 <= k <= n  

	# Explanation
	Here's a breakdown of the approach, followed by the Python code:

*   **Sliding Window:** Iterate through the array using a sliding window of size `k`.
*   **Consecutive and Sorted Check:** For each window, check if the elements are consecutive and sorted in ascending order.
*   **Power Calculation:** If the conditions are met, determine the power (maximum element); otherwise, assign -1.

*   **Runtime Complexity:** O(n * k), where n is the length of the array and k is the size of the subarray. **Storage Complexity:** O(n - k + 1) for the `results` array.

	
	# Code
	```python
	def subarray_power(nums, k):
    """
    Calculates the power of all subarrays of size k in nums.

    Args:
        nums: A list of integers.
        k: The size of the subarrays.

    Returns:
        A list of integers representing the power of each subarray.
    """

    n = len(nums)
    results = []

    for i in range(n - k + 1):
        subarray = nums[i:i + k]
        is_consecutive = True
        is_sorted = True

        for j in range(1, k):
            if subarray[j] != subarray[j - 1] + 1:
                is_consecutive = False
                break
            if subarray[j] < subarray[j-1]:
                is_sorted = False
                break

        if is_consecutive and is_sorted:
            results.append(subarray[-1])
        else:
            results.append(-1)

    return results
	```
			
