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.
Assuming a sorted list, everything revolves around the center of the list
What worked for me was first finding the mid point index of the list
Then define a start point index and an end point index from that mid point.
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.
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.
Then while K is greater than 1, I will keep reducing K by 2 and repeat the process.
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
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.
Then, I'm going to set the value of the start and end point indices to -1 in the parent list.
The midpoint will stay constant through each iteration
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.
Then I'm going to filter out all the -1 values from the parent list and sort it.
I'm going to sort the list the list containing K elements as well
Finally I'm going to calculate the absolute difference between the two lists and return that value.
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.
Fair Cut
You are viewing a single comment's thread. Return to all comments →
Here's what worked for me: