Problem · Array
Beautiful Subarrays
Learn this problemProblem statement
A beautiful subarray contains exactly k odd elements.
Given an integer array nums and a non-negative integer k, return the number of distinct beautiful subarrays. Subarrays are distinct by their inclusive start and end indices.
Function
beautifulSubarrays(nums: int[], k: int) → longExamples
Example 1
nums = [1,2,3,4,5]k = 2return = 4The four beautiful subarrays are [1,2,3], [1,2,3,4], [2,3,4,5], and [3,4,5].
Example 2
nums = [2,5,4,9]k = 2return = 2The beautiful subarrays are [5,4,9] and the complete array.
Constraints
1 <= nums.length <= 200000-10^9 <= nums[i] <= 10^90 <= k <= nums.length- The answer fits a signed
long.