Looking for good programming challenges?

Use the search below to find our solutions for selected questions!

Minimum Swaps Challenge

Sharing is caring!

Problem Statement
You are given an unordered array consisting of consecutive integers \left[1, 2, 3, \dots, n\right] without any duplicates. You are allowed to swap any two elements. You need to find the minimum number of swaps required to sort the array in ascending order.

For example, given the array \left[7,1,3,2,4,5,6\right] we perform the following steps:

It took 5 swaps to sort the array.

Function Description
Write a function minimumSwaps that returns an integer representing the minimum number of swaps to sort the array.

minimumSwaps must have the following parameter(s):
* arr: an unordered array of integers

Solution