Wednesday, 9 November 2022

Leetcode + hackerrank good questions

Question link- 

https://www.hackerrank.com/challenges/minimum-swaps-2/problem

You are given an unordered array consisting of consecutive integers  [1, 2, 3, ..., n] without any duplicates. You are allowed to swap any two elements. Find the minimum number of swaps required to sort the array in ascending order.


Example



Perform the following steps:


i   arr                         swap (indices)

0   [7, 1, 3, 2, 4, 5, 6]   swap (0,3)

1   [2, 1, 3, 7, 4, 5, 6]   swap (0,1)

2   [1, 2, 3, 7, 4, 5, 6]   swap (3,4)

3   [1, 2, 3, 4, 7, 5, 6]   swap (4,5)

4   [1, 2, 3, 4, 5, 7, 6]   swap (5,6)

5   [1, 2, 3, 4, 5, 6, 7]

It took  swaps to sort the array.


Function Description


Complete the function minimumSwaps in the editor below.


minimumSwaps has the following parameter(s):


int arr[n]: an unordered array of integers

Returns


int: the minimum number of swaps to sort the array

Input Format


The first line contains an integer, , the size of .

The second line contains  space-separated integers .


Constraints


Sample Input 0


4

4 3 1 2

Sample Output 0


3

Explanation 0


Given array 

After swapping  we get 

After swapping  we get 

After swapping  we get 

So, we need a minimum of  swaps to sort the array in ascending order.


Sample Input 1


5

2 3 4 1 5

Sample Output 1


3

Explanation 1


Given array 

After swapping  we get 

After swapping  we get 

After swapping  we get 

So, we need a minimum of  swaps to sort the array in ascending order.


Sample Input 2


7

1 3 5 2 4 6 7

Sample Output 2


3

Explanation 2


Given array 

After swapping  we get 

After swapping  we get 

After swapping  we get 

So, we need a minimum of  swaps to sort the array in ascending order.


SOLUTION -


import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {

    // Complete the minimumSwaps function below.
    static int minimumSwaps(int[] arr) {

        int count = 0;

        int start = 1;
        for(int i=0;i<arr.length;i++)
        {

            if(arr[i]==start)
            {

            }
            else
            {
                int val = start;
                int index = find(arr,val);
                //System.out.println(val+","+index);

                int val2 = arr[i];
                //System.out.println("replaceing:" +val+",with"+val2+",indexes:"+index+","+i);
                arr[i] = val;
                arr[index] = val2;
                count = count+1;
                


            }
            start = start+1;

        }
        return count;
    }

    static int find(int arr[],int no)
    {
        for(int i=0;i<arr.length;i++)
        {
            if(no==arr[i])
            {
                return i;
            }
        }
        return 0;
    }


    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) throws IOException {
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        int n = scanner.nextInt();
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

        int[] arr = new int[n];

        String[] arrItems = scanner.nextLine().split(" ");
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

        for (int i = 0; i < n; i++) {
            int arrItem = Integer.parseInt(arrItems[i]);
            arr[i] = arrItem;
        }

        int res = minimumSwaps(arr);

        bufferedWriter.write(String.valueOf(res));
        bufferedWriter.newLine();

        bufferedWriter.close();

        scanner.close();
    }
}


No comments:

Post a Comment

GPT4ALL - A new LLaMa (Large Language Model)

posted 29th March, 2023 - 11:50, GPT4ALL launched 1 hr ago  What if we use AI generated prompt and response to train another AI - Exactly th...