We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. Minimum Swaps 2
  2. Discussions

Minimum Swaps 2

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 2394 Discussions, By:

recency

Please Login in order to post a comment

  • justin_irabor
    6 days ago+ 0 comments

    JavaScript solution:

    function minimumSwaps(arr) {
        let sortedArray = [...arr].sort((a,b) => (a-b));
        let unsortedArray = [...arr];
        let holder;
        let counter = 0;
    
        while (sortedArray.join() !== unsortedArray.join()) {
            for (let i = 0; i < unsortedArray.length; i++) {
                if (unsortedArray[i] !== (i+1)) {
                    holder = unsortedArray[i];
                    unsortedArray[i] = unsortedArray[holder - 1];
                    unsortedArray[holder - 1] = holder;
                    counter++;
                }
            }
        }
        
        return counter;
    
    }
    
    0|
    Permalink
  • yugash
    6 days ago+ 0 comments
    import math
    import os
    import random
    import re
    import sys
    
    # Complete the minimumSwaps function below.
    def minimumSwaps(arr):
        ans = 0
        i = 0
        while i < len(arr):
            p = arr[i]
            if p < i+1:
                temp = arr[p-1]
                arr[p-1] = p
                arr[i] = temp
                ans+=1
            else:
                i+=1
        return ans
    
    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()
    
    0|
    Permalink
  • andrewtocchi
    6 days ago+ 0 comments

    JavaScript by comparing array to presorted as strings

    function minimumSwaps(arr) { let count = 0; let sorted = [...arr].sort((a,b) => a-b) let temp = 0; while(arr.join() !== sorted.join()){ for(let i =0; i < arr.length; i++){ if(arr[i] !== i + 1){ temp = arr[i] arr[i] = arr[arr[i] - 1] arr[temp -1] = temp count = count + 1 } } } return count;

    }

    0|
    Permalink
  • kursatko
    1 week ago+ 0 comments

    My cpp solution is here :

    int minimumSwaps(vector<int> arr) {
        vector<int> sorted{arr};
        sort(sorted.begin(),sorted.end());
        int swapCount{0};
        for (int i=0; i < sorted.size(); i++) {
            if(sorted[i] != arr[i]) {
                auto result = std::find(arr.begin()+i, arr.end(), sorted[i]);
                int index = std::distance(arr.begin(), result);
                int swap = arr[i];
                arr[i] = arr[index];
                arr[index] = swap;
                swapCount++;
            }
        }
        return swapCount;
    }
    
    0|
    Permalink
  • tomek_juszczysz1
    3 weeks ago+ 0 comments

    Hey, this python code works!

    def minimumSwaps(arr):
        swaps = 0
        for i in range(len(arr)):
            running_index = i
            is_loop = False
            while arr[running_index] != running_index+1:
                is_loop=True
                next_index = arr[running_index]-1
                arr[running_index] = running_index+1
                running_index = next_index
                swaps +=1
            if is_loop:
                swaps -= 1
        return swaps
    
    0|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy