easyArraysPattern: Two Pointers
Matched Sequence Pairs Solution
Problem Statement
Given two arrays, `arr1` and `arr2`, of the same length, where `arr2` is sorted in ascending order, pair each element in `arr1` with its corresponding element in `arr2` based on a custom matching function.
Examples
Example 1:
Input:[1, 3, 2], [2, 1, 3]
Output:[[1, 2], [3, 1], [2, 3]]
Explanation: Custom matching function pairs elements at corresponding indices.
Example 2:
Input:[5, 5, 5], [5, 5, 5]
Output:[[5, 5], [5, 5], [5, 5]]
Explanation: Custom matching function pairs elements at corresponding indices.
Example 3:
Input:[1, 2, 3], [3, 2, 1]
Output:[[1, 3], [2, 2], [3, 1]]
Explanation: Custom matching function pairs elements at corresponding indices.
Constraints
- 1 <= arr1.length <= 1000
- -1000 <= arr1[i] <= 1000
- arr2 is sorted in ascending order
- arr1 and arr2 have the same length
Time: O(n) Space: O(n)
Use a single loop to iterate through both arrays and pair elements at corresponding indices.
Run, Test & Submit Code
Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.
Solve on Interactive WorkspaceTested Solutions
def matched_sequence_pairs(arr1, arr2):
return [[x, y] for x, y in zip(arr1, arr2)]