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.
More Atlassian problems
- Planning ProductionOA · Seen Feb 2025
- K-Means ClusteringOA · Seen Feb 2025
- Minimum Sorted Erasure OperationsOA · Seen Jun 2024
- Count Analogous ArraysOA · Seen Mar 2024
- Get Maximum ScoreOA · Seen Mar 2024
- Better Compression 🦀OA · Seen Mar 2024
- Romanizer 🐡OA · Seen Mar 2024
- Flower Bouquets 💐OA · Seen Mar 2024