Minimum Operations to Convert Array
Updated
Problem statement
Given an array arr of length n, the following types of operations can be performed on it:
- Operation type 1: Shift the array cyclically by 1 towards the left.
- Operation type 2: Swap any 2 elements of the array.
However, once an operation of type 2 is performed, operations of type 1 cannot be performed again. The participants are required to find the minimum number of operations in which the given array can be converted into the sequence (1, 2, 3, ..., n).
Note: It is guaranteed that the array arr is a permutation of the target array [1, 2, 3, ..., n] and hence, the answer will always exist.
Function
minimumOperationsToConvertArray(arr: int[]) → int
Complete the function minimumOperationsToConvertArray in the editor.
minimumOperationsToConvertArray has the following parameter:
int[] arr: an array of integers
Returns
int: the minimum number of operations required
Examples
Example 1
arr = [5, 3, 2, 1, 4]return = 2The following sequence of operations can be performed:
- Type 1: Shift the array cyclically left once to obtain
(3, 2, 1, 4, 5). - Type 2: Swap the first and third elements, the array becomes
(1, 2, 3, 4, 5)which is the required target.
It can be shown that the array cannot be transformed in less than 2 operations. Thus, the answer is 2.
More Nutanix interview questions
- Print FooBar AlternatelyPHONE SCREEN · Seen Nov 2026
- Design Hit CounterONSITE INTERVIEW · Seen Oct 2026
- Peak Process MemoryONSITE INTERVIEW · Seen Aug 2026
- Good SubsequencesOA · Seen Jul 2026
- Min Cost to Connect All PointsOA · Seen Jun 2026
- Minimum Operations to Transform ArrayONSITE INTERVIEW · Seen Jun 2026
- Count Connected Components in an Undirected GraphONSITE INTERVIEW · Seen Aug 2025
- Digit Changes for a Multiple of 9OA · Seen Aug 2025
- Gas Station Loop StartOA · Seen Aug 2025
- LRU CacheONSITE INTERVIEW · Seen Aug 2025
Similar questions at other companies
- Earn the Most 🐝Capital One · OACapital One interview questions
- Minimum Cost to Merge Values into OnePayPal · OAPayPal interview questions
- Minimum Operations to Sort with Rotations Then SwapsBank of New York Mellon · OA
- Minimum Cost of Left and Right PropagationAmazon · OA
- Minimum Rounds to Complete All TasksInfosys · OAInfosys interview questions
- Reschedule Meetings for Maximum Free TimeFivetran · OAFivetran interview questions
- Maximum Easy Tasks After RearrangingIBM · OAIBM interview questions
- Minimum Cost K-Capable ModelsMicrosoft · OAMicrosoft interview questions
- Maximum ScoreNVIDIA · OANVIDIA interview questions
- Maximum Distinct Values After Unit AdjustmentsBirla Pivot · OA
Nutanix interview experiences
- Nutanix interviews · November 2026
- Nutanix SWE Intern Phone Screens: 20 Experiences (Part 3 of 3)
- Nutanix SWE Intern OA Questions: 16 Experiences (Part 2 of 3)
- Nutanix U.S. SWE Intern Interviews: 54 Experiences (Part 1 of 3)
- Nutanix SWE OA and Interviews, Level Not Reported: 9 Experiences
- Nutanix Intern Debugging Round and Interviews: 25 Experiences