Skip to main content

HACKER RANK - ARRAYS - MINIMUM SWAPS

 

HACKER RANK - ARRAYS - MINIMUM SWAPS


You are given an unordered array consisting of consecutive integers  [1, 2, 3, ..., n] without any duplicates. You are allowed to swap any two elements. Find the minimum number of swaps required to sort the array in ascending order.

Example

Perform the following steps:

i   arr                         swap (indices)
0   [7, 1, 3, 2, 4, 5, 6]   swap (0,3)
1   [2, 1, 3, 7, 4, 5, 6]   swap (0,1)
2   [1, 2, 3, 7, 4, 5, 6]   swap (3,4)
3   [1, 2, 3, 4, 7, 5, 6]   swap (4,5)
4   [1, 2, 3, 4, 5, 7, 6]   swap (5,6)
5   [1, 2, 3, 4, 5, 6, 7]

It took  swaps to sort the array.

Function Description

Complete the function minimumSwaps in the editor below.

minimumSwaps has the following parameter(s):

  • int arr[n]: an unordered array of integers

Returns

  • int: the minimum number of swaps to sort the array

Input Format

The first line contains an integer, , the size of .
The second line contains  space-separated integers .

Constraints

Sample Input 0

4
4 3 1 2

Sample Output 0

3

Explanation 0

Given array 
After swapping  we get 
After swapping  we get 
After swapping  we get 
So, we need a minimum of  swaps to sort the array in ascending order.

Sample Input 1

5
2 3 4 1 5

Sample Output 1

3

Explanation 1

Given array 
After swapping  we get 
After swapping  we get 
After swapping  we get 
So, we need a minimum of  swaps to sort the array in ascending order.

SOLUTION:

//python

def minimumSwaps(arr):

    a=dict(enumerate(arr,1))
    b={v:k for k,v in a.items()}
    count=0
    for i in a:
        x=a[i]
        if x!=i:
            y=b[i]
            a[y]=x
            b[x]=y
            count+=1
    return count     

Comments

Popular posts from this blog

POST ORDER TRAVERSAL -- HACKERRANK SOLUTIONS

  Complete the   function in the editor below. It received   parameter: a pointer to the root of a binary tree. It must print the values in the tree's postorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the   function. Constraints   Nodes in the tree    Output Format Print the tree's postorder traversal as a single line of space-separated values. Sample Input 1 \ 2 \ 5 / \ 3 6 \ 4 Sample Output 4 3 6 5 2 1 Explanation The postorder traversal is shown. Solution #include   < stdio.h > #include   < string.h > #include   < math.h > #include   < stdlib.h > struct  node {           int  data;      struct  node *left;      struct  node *ri...

HACKERRANK -- DELETE A NODE AT A GIVEN POSITION

  HACKERRANK --  DELETE A NODE AT A GIVEN POSITION Delete the node at a given position in a linked list and return a reference to the head node. The head is at position 0. The list may be empty after you delete the node. In that case, return a null value. Example After removing the node at position  ,  . Function Description Complete the  deleteNode  function in the editor below. deleteNode  has the following parameters: -  SinglyLinkedListNode pointer llist:  a reference to the head node in the list -  int position:  the position of the node to remove Returns -  SinglyLinkedListNode pointer:  a reference to the head of the modified list Input Format The first line of input contains an integer  , the number of elements in the linked list. Each of the next   lines contains an integer, the node data values in order. The last line contains an integer,  , the position of the node to delete. Constraints , wh...

TREE PREORDER TRAVERSAL -- HACKERRANK SOLUTIONS

  Complete the   function in the editor below, which has   parameter: a pointer to the root of a binary tree. It must print the values in the tree's preorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the  preOrder  function. Constraints  Nodes in the tree  Output Format Print the tree's preorder traversal as a single line of space-separated values. Sample Input 1 \ 2 \ 5 / \ 3 6 \ 4 Sample Output 1 2 5 3 4 6 Explanation The preorder traversal of the binary tree is printed. Solution #include   < stdio.h > #include   < string.h > #include   < math.h > #include   < stdlib.h > struct  node {           int  data;      struct  node *left;      struct ...