How to create divide-and-conquer algorithm?
I started to learn about divide-and-conquer algorithms in my data structures class and currently I having a problem with my homework. I have a given array/list of n integers and I need to find the element, for which sum of absolute values of differences of it and other's elements is the lowest. Something, that can be solved with brute-force algorithm similar to written-below one: def return_sum(element, my_list): sum_of_diff = 0 for number in my_list: sum_of_diff += abs(number - element) return sum_of_diff some_list = [10, 4, 8] sums = {elem: return_sum(elem, some_list) for elem in some_list} # {10: 8, 4: 10, 8: 6} for key in sums: if sums[key] is min(sums.values()): print(key) # 8 I know that this code is terrible, but it's purpose was to make clear, what algorithm should do. The problem is that I have to write an optimal divide-and-conquer algorithm in recursive and iterative version. And I have no idea how to make it. I'll be gratefull if you could give me any hint, how to create proper algorithm. Sincerely thanks for your help.