Lower this value: max(array) - min(array) by setting any three cells in an array to any value.
I felt like the interviewer was looking for an O(N) solution. At the end gave up and brute forced it.
def minDistance(nums):
if len(nums) <= 3:
return 0
nums.sort()
def helper(index, nums):
left = 0
right = len(nums)-1
for i in range(3):
if left == index:
nums[right] = nums[index]
right -= 1
elif right == index:
nums[left] = nums[index]
else:
a = abs(nums[index] - nums[left])
b = abs(nums[index] - nums[right])
if a < b:
nums[right] = nums[index]
right -= 1
else:
nums[left] = nums[index]
left += 1
ans = float('inf')
for i in range(len(nums)):
cloned = nums[::]
helper(i, cloned)
ans = min(ans, max(cloned) - min(cloned))
return ans
After giving the brute force solution, there was 15 minutes left in the interview to ask questions.