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.

Thanks for the explanation and the code but I have a concern. How does the reverse part work? You are reversing the list and then calling the sort function which will again sort it in ascending order. So ultimately you are checking swaps in just one direction. Please share your views.

The key appears to be that the map is created prior to the sorting, so it indeed is different for the two passes and results in the swaps being done in a different order.

Specifically i in the first for loop is different for the two passes.

If I understand your question, you are asking why the need to run the algorithm twice, once forward and once in reverse.

The reason for the need to check the reverse case is in the definition of the problem. The requirements are that the array is sorted such the sum of the differences between each two consecutive numbers is minimized. There are two ways to acheive that goal -- ascending and descending sorting the list. The descending sort is the "reverse" in this case.

What he's effectively doing (albeit a little indirectly) is first comparing the input array with an asc-sorted reference, then with a desc-sorted reference and taking the smallest number of swaps of the two. That's how I did it.

To solve it take care of the descending ones too. A counter case to show we also have to take the descending one.
Suppose if the array is-:
3 4 2 5 1
then the output should be 2.
5 4 2 3 1(swap 5 and 3)
5 4 3 2 1(swap 2 and 3)
Hence the answer will be 2.

To solve it take care of the descending ones too. A counter case to show we also have to take the descending one.
Suppose if the array is-:
3 4 2 5 1
then the output should be 2.
5 4 2 3 1(swap 5 and 3)
5 4 3 2 1(swap 2 and 3)
Hence the answer will be 2.

I had the same issue, even though my solution wasn't the same.
It was caused by the fact that I was calling my solving function with arr directly instead of a copy of it. As in the function itself, arr is modified, the second call wasn't made using initial data but using modified data.

I see that you're comparing elements that are out of place and incrementing number of swaps (ret) value. But that is not neccessarily, number of minimum of swaps required.

for example,
array1: 2 5 3 1
array2: 1 2 3 5

have 3 elements out of place. but minimum number of swaps required are 2. I would appreciate you can help me understand the algorithm.

Can someone please tell me why my initial attempt timeouts? Here, a histogram array was used to construct a cumulative frequency array of the elements, which was then used as the keys for the correct positions of the numbers in the original array (similar to the Minimum Swaps 2 problem at https://www.hackerrank.com/challenges/minimum-swaps-2/ ):

deflilysHomework(arr):arr_original=arr.copy()b=[None]*(max(arr)+1)l=len(arr)k=(max(arr))hist=(k+1)*[0]#indicesrunsfrom0tomax(array)inclusiveforiinarr:hist[i]=1#Initialfrequencyarray,jthvalueofcountisthefrequencyofnumberjcumfreq=(k+1)*[0]cumfreq[0]=hist[0]invcumfreq={}forjinrange(1,k+1):cumfreq[j]+=cumfreq[j-1]+hist[j]ifnot(cumfreq[j]ininvcumfreq):invcumfreq[cumfreq[j]]=j## Create an array to get the inverse cumulative frequencies array:counter1=0forj,vinenumerate(arr_original):b[v]=jforjinrange(l):ifcumfreq[arr[j]]-1!=j:old=arr[j]#oldarr[j]arr[j],arr[b[invcumfreq[j+1]]]=arr[b[invcumfreq[j+1]]],arr[j]#arr[j],arr[b[cumfreq.index(j+1)]]=arr[b[cumfreq.index(j+1)]],arr[j]b[old],b[arr[j]]=b[arr[j]],b[old]counter1+=1else:continuearr=arr_original.copy()##Reset the b-array of indices!!forj,vinenumerate(arr_original):b[v]=jcounter2=0forjinrange(0,l):ifcumfreq[arr[j]]!=l-j:old=arr[j]#oldarr[j]arr[j],arr[b[invcumfreq[l-j]]]=arr[b[invcumfreq[l-j]]],arr[j]#arr[j], arr[b[revfreq.index(l-j)]] = arr[b[revfreq.index(l-j)]], arr[j] b[old],b[arr[j]]=b[arr[j]],b[old]counter2+=1else:continuereturncounter1#(min(counter1,counter2))

## Lily's Homework

You are viewing a single comment's thread. Return to all comments →

Above described solution in python:

Thanks. Creating map was the key to get past those time outs.

Thanks for the explanation and the code but I have a concern. How does the reverse part work? You are reversing the list and then calling the sort function which will again sort it in ascending order. So ultimately you are checking swaps in just one direction. Please share your views.

The key appears to be that the map is created

priorto the sorting, so it indeed is different for the two passes and results in the swaps being done in a different order.Specifically

`i`

in the first`for`

loop is different for the two passes.i didn't get your explanation of reverse work.can you please explain me in brief manner?

If I understand your question, you are asking why the need to run the algorithm twice, once forward and once in reverse.

The reason for the need to check the reverse case is in the definition of the problem. The requirements are that the array is sorted such the sum of the differences between each two consecutive numbers is minimized. There are two ways to acheive that goal -- ascending and descending sorting the list. The descending sort is the "reverse" in this case.

What he's effectively doing (albeit a little indirectly) is first comparing the input array with an asc-sorted reference, then with a desc-sorted reference and taking the smallest number of swaps of the two. That's how I did it.

To solve it take care of the descending ones too. A counter case to show we also have to take the descending one. Suppose if the array is-: 3 4 2 5 1 then the output should be 2. 5 4 2 3 1(swap 5 and 3) 5 4 3 2 1(swap 2 and 3) Hence the answer will be 2.

To solve it take care of the descending ones too. A counter case to show we also have to take the descending one. Suppose if the array is-: 3 4 2 5 1 then the output should be 2. 5 4 2 3 1(swap 5 and 3) 5 4 3 2 1(swap 2 and 3) Hence the answer will be 2.

This is my code in python3.

I failed to pass TC #1. What am I doing wrong here?

I had the same issue, even though my solution wasn't the same. It was caused by the fact that I was calling my solving function with

`arr`

directly instead of a copy of it. As in the function itself,`arr`

is modified, the second call wasn't made using initial data but using modified data.also, I like the syntax:

Thanks a lot!

Thanks. Creating map was the key to get past those time outs.

nice pun

can you explain this piece of code

I see that you're comparing elements that are out of place and incrementing number of swaps (ret) value. But that is not neccessarily, number of minimum of swaps required.

for example, array1: 2 5 3 1 array2: 1 2 3 5

have 3 elements out of place. but minimum number of swaps required are 2. I would appreciate you can help me understand the algorithm.

Thanks

Check it for reverse array1: 1 3 5 2 - it require two swaps. We are only taking min of ascending and descending.

Correction:

Tried and tested - passes all cases.

Can someone please tell me why my initial attempt timeouts? Here, a histogram array was used to construct a cumulative frequency array of the elements, which was then used as the keys for the correct positions of the numbers in the original array (similar to the Minimum Swaps 2 problem at https://www.hackerrank.com/challenges/minimum-swaps-2/ ):