Problem · Array

Beautiful Subarrays

Learn this problem
MediumAtlassian logoAtlassianNEW GRADOA

Problem 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) → long

Examples

Example 1

nums = [1,2,3,4,5]k = 2return = 4

The 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 = 2

The beautiful subarrays are [5,4,9] and the complete array.

Constraints

  • 1 <= nums.length <= 200000
  • -10^9 <= nums[i] <= 10^9
  • 0 <= k <= nums.length
  • The answer fits a signed long.

More Atlassian problems

drafts saved locally
public long beautifulSubarrays(int[] nums, int k) {
  // Write your code here.
}
nums[1,2,3,4,5]
k2
expected4
checking account