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

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Minimum Swaps 2
  2. Discussions

Minimum Swaps 2

Problem
Submissions
Leaderboard
Discussions
Editorial

    You are viewing a single comment's thread. Return to all comments →

  • edwynrangel
    4 months ago+ 0 comments

    This is my solution in Go

    func minimumSwaps(arr []int32) int32 {
    
        swaps := int32(0)
        i := 0
        for i < len(arr) {
            if arr[i] != int32(i+1) {
                arr[arr[i]-1], arr[i] = arr[i], arr[arr[i]-1]
                swaps++
            } else {
                i++
            }
        }
    
        return swaps
    }
    
    0|
    Permalink
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy