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 →

  • professorParadox
    5 months ago+ 0 comments

    C solution

    int minimumSwaps(int arr_count, int* arr) {
        int swaps = 0;
        for (int i = 1; i <= arr_count; i++)
        {
            if (arr[i-1] != i)
            {
                for (int j = i; j < arr_count; j++)
                {
                    if (i == arr[j])
                    {
                        int temp = arr[i-1];
                        arr[i-1] = arr[j];
                        arr[j] = temp;
                        swaps++;
                        break;
                    }
                }
            }
        }
        
        return swaps;
    }
    
    0|
    Permalink
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy