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.
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
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Minimum Swaps 2
You are viewing a single comment's thread. Return to all 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