• + 1 comment

    Here's what worked for me:

    1. Assuming a sorted list, everything revolves around the center of the list
    2. What worked for me was first finding the mid point index of the list
    3. Then define a start point index and an end point index from that mid point.
    4. Then if K is an even number, then the start point index will be midpoint - k + 1 and the end point index will be midpoint + k - 2.
    5. If K is an odd number, then the start point index will be midpoint - k and the end point index will be midpoint + k - 1.
    6. Then while K is greater than 1, I will keep reducing K by 2 and repeat the process.
    7. Since I'm using python I'm going to store the value of the start index , midpoint index and end point index in a tuple for each iteration
    8. Once I have the tuple which will contain 3 elements, I'm only going to grab the value of the start and end point indices from the parent and store those in a separate list.
    9. Then, I'm going to set the value of the start and end point indices to -1 in the parent list.
    10. The midpoint will stay constant through each iteration
    11. If K is odd, then I'm going to store the value of the midpoint index in the list and set that value to -1 on the parent list after the loop ends.
    12. Then I'm going to filter out all the -1 values from the parent list and sort it.
    13. I'm going to sort the list the list containing K elements as well
    14. Finally I'm going to calculate the absolute difference between the two lists and return that value.
    15. If K is 1, then I'm going to store the value of the midpoint in the list and set that value to -1 on the parent list and just go ahead and calculate the absolute difference between the two lists.