From 974de3611c7e065d4b687af8eeec0edb6bc17de4 Mon Sep 17 00:00:00 2001 From: pragyakapoor <38473918+pragyakapoor@users.noreply.github.com> Date: Thu, 1 Oct 2020 13:42:38 +0530 Subject: [PATCH] Solution to Minimum swaps 2 in python --- algorithms/implementation/minimum swaps 2.py | 23 ++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 algorithms/implementation/minimum swaps 2.py diff --git a/algorithms/implementation/minimum swaps 2.py b/algorithms/implementation/minimum swaps 2.py new file mode 100644 index 0000000..69755ef --- /dev/null +++ b/algorithms/implementation/minimum swaps 2.py @@ -0,0 +1,23 @@ + +import math +import os +import random +import re +import sys + +# Complete the minimumSwaps function below. + +n = int(input()) + +arr = list(map(int, input().rstrip().split())) + +numSwaps = 0 +i = 0 +while(i < len(arr)-1): + if arr[i] != i+1: + tmp = arr[i] + arr[i], arr[tmp-1] = arr[tmp-1], arr[i] + numSwaps += 1 + else: + i += 1 +print(numSwaps) \ No newline at end of file