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 →

  • ayouz_asfa45
    5 months ago+ 0 comments
    def minimumSwaps(arr):
        num_swap = 0
        ordred = sorted(arr)
        for i in range(len(arr)):
            if arr == ordred:
                break
            if ordred[i] == arr[i]:
                continue
            else:
                j = arr.index(ordred[i])
                arr[i], arr[j] = arr[j], arr[i]
                num_swap += 1
        return num_swap
    
    0|
    Permalink
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy