Created
January 12, 2020 12:38
-
-
Save dovideh/cb570712921bd86dfd50c7de59643b56 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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