Amazon Online Assessment (OA) 2021 - Number of Swaps to Sort | HackerRank

Given an array and a sorting algorithm, the sorting algorithm will do a selection swap. Find the number of swaps to sort the array.

Example 1:

Input: [5, 4, 1, 2]

Output: 5

Explanation:

  • Swap index 0 with 1 to form the sorted array [4, 5, 1, 2].
  • Swap index 0 with 2 to form the sorted array [1, 5, 4, 2].
  • Swap index 1 with 2 to form the sorted array [1, 4, 5, 2].
  • Swap index 1 with 3 to form the sorted array [1, 2, 5, 4].
  • Swap index 2 with 3 to form the sorted array [1, 2, 4, 5].

Solution

Title

Script

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.

Contrary to popular belief, Lorem Ipsum is not simply random text.

  >>> a = [1, 2, 3]
  >>> a[-1]
  3

Get premium for instant access to all content and solutions

Upgrade