Maximum Bounded One-to-One Pairs
Updated
Problem statement
You are given two nonempty integer arrays arr1 and arr2, and a positive integer d.
Make as many one-to-one pairs (a, b) as you can. Each pair takes an unused element a from arr1 and an unused element b from arr2. A pair is valid only when a <= b <= a + d. Each element can be used in at most one pair.
Return the maximum number of valid pairs. The input arrays do not need to be sorted.
Function
maxValidPairs(arr1: int[], arr2: int[], d: int) → intExamples
Example 1
arr1 = [8,20,35,45]arr2 = [25,50]d = 10return = 2Pair 20 with 25 and 45 with 50. Both pairs are valid, because each b is at least its a and at most d above it. arr2 has only two elements, so these two pairs are the most you can make.
Example 2
arr1 = [5,1,3]arr2 = [3,6,2]d = 1return = 3Use the pairs (1,2), (3,3), and (5,6). In each one, b is at least a and at most d above it. Every element is used, so the answer is three pairs.
Example 3
arr1 = [1,2,3]arr2 = [10,11]d = 2return = 0Every value in arr2 is more than d above every value in arr1. No pair is valid, so the answer is zero.
Constraints
1 <= arr1.length, arr2.length <= 10^50 <= arr1[i], arr2[i] <= 10^91 <= d <= 10^5
More IBM Online Assessment questions
- Missing IntervalsOA · Seen Nov 2026
- Count Peaks and ValleysOA · Seen Nov 2026
- Last Object Exit TimeOA · Seen Nov 2026
- Count Strictly Increasing Contiguous WindowsOA · Seen Nov 2026
- Total Each Letter's Count in an Encoded StringOA · Seen Nov 2026
- Count Servers With No Requests in a Time WindowOA · Seen Nov 2026
- Maximum Easy Tasks After RearrangingOA · Seen Nov 2026
- Tree Pythagorean TriplesOA · Seen Oct 2026
- Minimum Power-of-Two Removal OperationsOA · Seen Oct 2026
- Maximum Compatible Time WindowsOA · Seen Oct 2026
Similar questions at other companies
- Latest Arrival to Catch a BusAutodesk · OAAutodesk interview questions
- Minimum Removals to Balance ArraySalesforce · OASalesforce interview questions
- Minimum Machines for TasksExpedia · OAExpedia interview questions
- Find Minimum Possible SizeMathWorks · OAMathWorks interview questions
- Minimum Days to Watch All MoviesAdobe · OAAdobe interview questions
- Efficient Janitor TripsRoblox · OARoblox interview questions
- Catch Fish with Reusable BaitsSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
- Can Every Package Fit in a BoxIMC Trading · OAIMC Trading interview questions
- Minimum Tunnel Crossing TimeArcesium · OAArcesium interview questions
- Get Maximum PairsAmazon · OA