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.

Finally solved it! Spent some time trying to figure out the hidden details. Would appreciate if there had been another test case. Things i learnt - Java specifics (might be useful if you're stuck):

You have to sort the array in both ascending and descending order as both will yield a minimum sum.

Remember to use a LinkedHashMap (if you are using one) and not a HashMap to store the element key and index value (to preserve insertion order).

A copy of an ArrayList is a shallow copy. You need to addAll(originalArray) to create another array with the same values as the originalArray.

The algorithm is pretty simple: (just a brief run-through
, note the details, figure out the implementation)

compare every element of original array with a copy of the sorted array (refer 3)

when there's a mismatch, swap elements into their respective positions

count the total number of swaps

repeat the above with the array reversed (refer 1)

use a hashmap as a cache/lookup to speed things up O(1) (refer 2).

Very nice explanation but I have one doubt what if the array have duplicate elements like: 7 3 0 4 1 0 5 0
because now "0" is present at index [2], [5], [7] , so in the first swap at which index we are going to swap "7" with.

## Lily's Homework

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

Finally solved it! Spent some time trying to figure out the hidden details. Would appreciate if there had been another test case. Things i learnt - Java specifics (might be useful if you're stuck):ascendinganddescendingorder as both will yield a minimum sum.LinkedHashMap(if you are using one) and not a HashMap to store the element key and index value (to preserve insertion order).shallow copy. You need to`addAll(originalArray)`

to create another array with the same values as the originalArray.The algorithm is pretty simple: (just a brief run-through , note the details, figure out the implementation)

compareevery element of original array with a copy of the sorted array (refer 3)swapelements into their respective positionscountthe total number of swapsExample:

7 3 0 4 1 6 5 2 - original array0 1 2 3 4 5 6 7 - sorted array0374 1 6 5 2 - after first swap0

17 436 5 2 - after second swap0 1

24 3 6 57- after third swap0 1 2

346 5 7 - after fourth swap0 1 2 3 4

567 - after fifth swapDid you notice the pattern? All the best!

Great, thank you for the help, I will check it later!

hey, if the numbers in the unsorted array are not serial then what do we do? for example: original array: 72 3 0 4 1 6 5 2 thanks

You explained it very nicely .... Thank u so much for helping me...

Very nice explanation but I have one doubt what if the array have duplicate elements like: 7 3 0 4 1 0 5 0 because now "0" is present at index [2], [5], [7] , so in the first swap at which index we are going to swap "7" with.

The elements are distinct.