Skip to content

Instantly share code, notes, and snippets.

@ovidiucs
Created January 12, 2020 12:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ovidiucs/cb570712921bd86dfd50c7de59643b56 to your computer and use it in GitHub Desktop.
Save ovidiucs/cb570712921bd86dfd50c7de59643b56 to your computer and use it in GitHub Desktop.
#!/bin/python
import math
import os
import random
import re
import sys
# Complete the minimumSwaps function below.
def minimumSwaps(arr):
swp = 0
n = len(arr)
for i,j in enumerate(arr):
if(j != i+1):
source_idx = arr.index(i+1)
destination_idx = arr.index(j)
swp += 1
arr[destination_idx], arr[source_idx] = arr[source_idx], arr[destination_idx]
return swp
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
n = int(input())
arr = list(map(int, input().rstrip().split()))
res = minimumSwaps(arr)
fptr.write(str(res) + '\n')
fptr.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment