Skip to content

Instantly share code, notes, and snippets.

@dovideh
Created January 12, 2020 12:38
Show Gist options
  • Save dovideh/cb570712921bd86dfd50c7de59643b56 to your computer and use it in GitHub Desktop.
Save dovideh/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