Minimum Swaps 2

  • + 1 comment
    // Complete the minimumSwaps function below.
    static int minimumSwaps(int[] arr) {
        int count = 0;
        int diff = 0;
        int temp = 0;
        for(int i=0; i<arr.Length; i++){
            if(arr[i] != i+1){
                diff = arr[i] - 1;
                temp = arr[diff];
                arr[diff] = arr[i];
                arr[i] = temp;
                count++;
                i--;
            }
        }
    
        return count;
    }