Minimum Swaps to Sort an Array
Updated
Problem statement
Given an array values of distinct integers, return the minimum number of swaps of any two positions needed to arrange the array in strictly increasing order.
Function
minimumSwaps(values: int[]) → intExamples
Example 1
values = [4,3,1,2]return = 3One optimal sequence swaps the values at positions 0 and 2, then positions 1 and 3, then positions 2 and 3, producing [1,2,3,4].
Example 2
values = [1,5,4,3,2]return = 2Swap 5 with 2, then swap 4 with 3.
Constraints
1 ≤ values.length ≤ 2 × 10^5-10^9 ≤ values[i] ≤ 10^9- All values are distinct.
More Postman interview questions
- Configuration SystemOA · Seen Sep 2020
- Large ResponsesOA · Seen Sep 2020
- Validate IP AddressOA · Seen Aug 2020
- Without WhitespacesOA · Seen Sep 2019
- Maximum Laptop Rating in a Price RangeOA · Seen Aug 2019
- Group Duplicate Files by ContentONSITE INTERVIEW · Seen Feb 2026
- Encode and Decode a String StreamONSITE INTERVIEW · Seen May 2024
Similar questions at other companies
- Mutual-Friend RecommendationsMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Deterministic Secret Santa AssignmentShopify · PHONE SCREENShopify interview questions
- Deterministic Depth-First Graph TraversalWaymo · PHONE SCREENWaymo interview questions
- Accounts MergeOracle · ONSITE INTERVIEWOracle interview questions
- Find All People With SecretIDFC Bank · ONSITE INTERVIEWIDFC Bank interview questions
- Earliest Meeting Time for All EmployeesCapital One · OACapital One interview questions
- Top K Frequent ElementsAmazon · OA · PHONE SCREEN
- Minimum Operations to Sort with Rotations Then SwapsBank of New York Mellon · OA
- Capping Portfolio WeightsCapstone Investment Advisors · OA
- Delivery Management SystemAkuna Capital · OAAkuna Capital interview questions