Count Pairs with K Divisible Values in the Interval
Updated
Problem statement
Given an integer array arr, a nonnegative integer k, and a positive integer x, count index pairs (i, j) with i < j and arr[i] <= arr[j] such that the inclusive integer interval [arr[i], arr[j]] contains exactly k multiples of x.
Pairs are counted by index, so equal values at different positions are distinct. Zero is divisible by every positive x. Return the number of valid pairs.
Function
countValidPairs(arr: int[], k: int, x: int) → longExamples
Example 1
arr = [1,2,3,4,5]k = 2x = 2return = 4The valid pairs have value intervals [1,4], [1,5], [2,4], and [2,5], each containing the multiples 2 and 4.
Example 2
arr = [-3,0,2]k = 2x = 3return = 2The intervals [-3,0] and [-3,2] each contain -3 and 0. The interval [0,2] contains only 0.
Constraints
1 <= arr.length <= 200000.-1000000 <= arr[i] <= 1000000.0 <= k <= 2000000.1 <= x <= 1000000.- The answer fits in a signed 64-bit integer.
More Chubb Online Assessment questions
- Index of the Maximum After Left RotationsOA · Seen Oct 2026
- Maximize Revenue from Decreasing InventoryOA · Seen Oct 2026
- Minimize the Maximum Array ElementOA · Seen Oct 2026
- Minimum Distinct Prefix CostOA · Seen Oct 2026
- Flat JSON Value DifferencesOA · Seen Sep 2026
- Maximum Student-Teacher DistanceOA · Seen Sep 2026
- Minimum Cost to Reach a Target WeightOA · Seen Sep 2026
- Minimum Last-Element RelocationsOA · Seen Sep 2026
- Most Visited Sprint MarkerOA · Seen Sep 2026
- How Many Sentences?OA · Seen Sep 2026
Similar questions at other companies
- Power Update After K-th Largest InsertionGoogle · OAGoogle interview questions
- At Most K Jumps with Quadratic Transition CostInfosys · OAInfosys interview questions
- Maximize Shipment Order by Sorting One WindowAmazon · OA
- Delete and Freeze GameRubrik · OARubrik interview questions
- Lock CodeVisa · OAVisa interview questions
- Fastest Average Speed over a Rolling KilometerOptiver · OAOptiver interview questions
- Most Items Within Budget from a Start IndexHudson River Trading · OAHudson River Trading interview questions
- Jump Game with Prime-3 StepsUber · OAUber interview questions
- Lock CodeGoldman Sachs · OAGoldman Sachs interview questions
- Minimum Sorted Erasure OperationsAtlassian · OAAtlassian interview questions