Find X-Sum of All K-Long Subarrays I - Practice Coding | SlaveCode
0
0123456789
0
0123456789
:
0
0123456789
0
0123456789
3318. Find X-Sum of All K-Long Subarrays I
Easy
10 Points
Array
Hash Table
Sliding Window
Heap (Priority Queue)
You are given an array nums of n integers and two integers k and x.
The x-sum of an array is calculated by the following procedure:
Note that if an array has less than x distinct elements, its x-sum is the sum of the array.
Return an integer array answer of length n - k + 1 where answer[i] is the x-sum of the subarray nums[i..i + k - 1].
Examples
Example 1
Input: nums = [1,1,2,2,3,4,2,3], k = 6, x = 2
Output: [6,10,12]
Explanation:
Example 2
Input: nums = [3,8,7,8,7,5], k = 2, x = 2
Output: [11,15,15,15,12]
Explanation:
Since k == x , answer[i] is equal to the sum of the subarray nums[i..i + k - 1] .
Constraints
1 <= n == nums.length <= 50
1 <= nums[i] <= 50
1 <= x <= k <= nums.length
3318. Find X-Sum of All K-Long Subarrays I
Easy
10 Points
Array
Hash Table
Sliding Window
Heap (Priority Queue)
You are given an array nums of n integers and two integers k and x.
The x-sum of an array is calculated by the following procedure:
Note that if an array has less than x distinct elements, its x-sum is the sum of the array.
Return an integer array answer of length n - k + 1 where answer[i] is the x-sum of the subarray nums[i..i + k - 1].
Examples
Example 1
Input: nums = [1,1,2,2,3,4,2,3], k = 6, x = 2
Output: [6,10,12]
Explanation:
Example 2
Input: nums = [3,8,7,8,7,5], k = 2, x = 2
Output: [11,15,15,15,12]
Explanation:
Since k == x , answer[i] is equal to the sum of the subarray nums[i..i + k - 1] .