Count Subarrays with at Least K Distinct Values
Updated
Problem statement
You are given an integer array nums and an integer k.
Return the number of contiguous subarrays that contain at least k distinct values. Repeated occurrences of the same value count only once toward a subarray's distinct-value total.
Function
countSubarraysWithAtLeastKDistinct(nums: int[], k: int) → longExamples
Example 1
nums = [1,2,1]k = 2return = 3The valid subarrays are [1,2], [2,1], and [1,2,1].
Example 2
nums = [1,1,1]k = 2return = 0Every subarray contains only the value 1, so none has two distinct values.
Constraints
1 <= nums.length <= 2 * 105-109 <= nums[i] <= 1091 <= k <= nums.length- The answer fits in a signed 64-bit integer.
More Visa Online Assessment questions
- Count Prime Values in SubtreesOA · Seen Nov 2026
- Lock CodeOA · Seen Nov 2026
- Minimum-Sum Tree Fill with Unit StepsOA · Seen Nov 2026
- Longest Non-Decreasing Substring After Pair SwapsOA · Seen Oct 2026
- Count Attack Vectors by Divisor RangeOA · Seen Oct 2026
- Minimum Operations to Reduce a Transaction LedgerOA · Seen Oct 2026
- Count Valid Space-Delimited WordsOA · Seen Oct 2026
- Earliest Common Meeting StartOA · Seen Oct 2026
- Maximum Index-Weighted Sum with Disjoint SwapsOA · Seen Oct 2026
- Maximum Equalized Substrings After One Adjacent SwapOA · Seen Sep 2026
Similar questions at other companies
- Count Belt Sections With K Fruit Pairs (Forward Deployed Engineer)Boston Consulting Group · OA
- Count Servers With No Requests in a Time WindowIBM · OA
- Count Subarrays with at Least K Equal-Fruit PairsTikTok · OA
- Consistent LogsRippling · OA
- Minimum Length Good SubarrayMicrosoft · OA
- Auction Sniping Window MinimumCisco · OA
- Balanced Prefix Sets in a PermutationUber · OA
- Shortest Subarray Covering an Integer RangeMarshall Wace · OA
- Longest Frequency-Bounded SubarrayZipRecruiter · OA
- Find Subarray With Minimum Distinct IntegersAmazon · OA