Minimum Swaps 2

  • + 2 comments

    just normalize the input while reading, for example we have the following input:

    5

    1 2 77 3 18

    1st step: While reading we are finding the maximum number that fits our constraints < N.

    2nd step: If it >= N, then add it to a map (key-value), where the key is that number you read and the value is the position of this element in the array

    So here we are done with reading the array.

    3rd step: Order this map by key ascending and replace all numbers in your array to the fictive ones (like foreach (var pair in sortedMap) { Array[pair.Value] = maximumNumber++})

    So we will get a new one array:

    1 2 5 3 4

    And then everything will be alright