Problem · Dynamic Programming
Relative Sort (Google Singapore)
Learn this problemProblem statement
Relative sorting is defined as sorting two arrays (both in strictly ascending order) such that the only operation allowed is swapping i'th element of one array with the i'th element of the other array. An array is said to be in strictly ascending order if i'th element of the array is smaller than (i+1)'th element of the array. You are given two arrays of size N. print the minimum number of swaps required to make both arrays relatively sorted.
Note:
Input Format:
The input consist of 3 lines:
Output Format:
The output will be an integer i.e., the minimum number of swaps required to make both arrays relatively sorted.
Function
minSwapsToMakeSequencesIncreasing(N: int, A: int[], B: int[]) → intExamples
Example 1
N = 4A = [1, 4, 4, 9]B = [2, 3, 5, 10]return = 1To make both arrays strictly increasing we can swap 4 and 3 or 4 and 5.
Constraints
0 < N < 110000 < Elements in array <= 10^9More Google problems
- Deduplicate Logs: Keep FirstONSITE INTERVIEW · Seen Jul 2026
- Deduplicate Logs: Keep LatestONSITE INTERVIEW · Seen Jul 2026
- Find a Template Across Binary-Tree LeavesONSITE INTERVIEW · Seen Jul 2026
- Maximum Programmer-Problem MatchingONSITE INTERVIEW · Seen Jul 2026
- Minimum Direction ViolationsONSITE INTERVIEW · Seen Jul 2026
- Stream Latest Log VersionsONSITE INTERVIEW · Seen Jul 2026
- Stream Unique Logs in Timestamp OrderONSITE INTERVIEW · Seen Jul 2026
- Top-K IP Addresses from File RecordsONSITE INTERVIEW · Seen Jul 2026