Skip to content

Instantly share code, notes, and snippets.

@roxsula
Created September 29, 2018 00:06
Show Gist options
  • Save roxsula/3577d97fc5a015b1cc5cab0e48db523e to your computer and use it in GitHub Desktop.
Save roxsula/3577d97fc5a015b1cc5cab0e48db523e to your computer and use it in GitHub Desktop.
#!/bin/python3
import math
import os
import random
import re
import sys
# Complete the minimumSwaps function below.
def minimumSwaps(arr):
swaps = 0
i = 0
while i < len(arr):
if arr[i] != i+1:
idx_fixed = arr[i] - 1
arr[i],arr[idx_fixed] = arr[idx_fixed],arr[i]
i -= 1
swaps += 1
i += 1
return swaps
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