# Solution to Min-Max-Division by codility

6 Apr

Question Name: Min-Max-Division or MinMaxDivision

Logically, it seems to be similar with the boron2013 (Flags) challenge by Codility. And the following is my Python solution.

UPDATE – 2014/09/19: thanks to Martin Kysel. The function signature is changed to:
def solution(K, M, A):
Don’t be confused by the new parameter M. Ignore the M, use the original solution, and you will pass all the test.

In addition, thanks to @Guillermo (StackOverflow Profile), here is the C++ solution from @Guillermo, which inspired me. Thanks again for his sharing!

### 21 Replies to “Solution to Min-Max-Division by codility”

1. wongiseng says:

Java solution, greedy checking :

• Sheng says:

Thanks for sharing your solution. If it was with some key comments, it would be more helpful.

• Sheng says:

I am sorry. Everytime I access your website with Firefox, the page makes FF die. I have to remove it for security consideration for my visitors.

2. Martin Kysel says:

Hey Sheng,
there has been a change to the function signature which I fixed but your solution nevertheless no longer works https://codility.com/demo/results/demoDJSP8K-U9H/
I get the same errors and can not explain them.

• Sheng says:

That is fine. And thank for telling me the change to the function signature.
I will add some explanation to help the others to understand the solutions.

3. Zheng Xu says:

I’m confused regarding the time complexity. Why is the time complexity O(N*log(N+M)) instead of O(N*log(N*M))? The upper bound of the binary search is equal to the sum of the elements in A, which is at most N*M.

• Sheng says:

Yes, you are right. The complexity is not exactly O(N*log(N+M)). O(N*log(N*M)) = O(N*(log(N) + log(M))). Therefore, when N is close to M, O(N*log(N*M)) is the same as O(N*log(N+M)). In generaly case, I do not know the answer.

• Yixuan Wang says:

“when N is close to M, O(N*log(N*M)) is the same as O(N*log(N+M))”
this seems wrong…

4. Hazem Farouk says:

Thank you very much for your solution. Really enlightening 🙂
Here’s the Java version of your Python solution:

5. Raf says:

What is throwing me off is that the exercise is asking for “expected worst-case time complexity is O(N*log(N+M))”
If I’m reading your solution correctly you’re first looping through all the elements followed by the binary search section. Isn’t that O(n) + log(n+m) ?

• Sheng says:

You are partly right. The binary search at #37 is inside the while loop around #35: so the complexity is O(N) * O(log(N+M)) = O(N*log(N+M)).

6. micropentium6 says:

I was tricked by M…”no greater than M” made me made the assumption that the largest element in the array must be M, which is not true, well, at least in Codility test cases. I got 43% just because of failing to recognize this “fact”…
If we consider the smallest max as a monotonic sequence, the lower bound of the sequence is the largest element in the array, and the higher bound is the sum of all elements. Then we choose the possible “smallest max” at the middle of the sequence and always greedily drops half of of the sequence by comparing the number of partitions based upon current “smallest max” with the proposed K. If our partition L is less than K, we probably can find a even smaller “smallest max” by dropping the lager half of the sequence, otherwise, drop the smaller half…

• Sheng says:

Agree that the parameter M is misleading.

7. Sailingfish says:

M is not misleading. In fact it can be proved that the result is between (mean，mean+M).
Updated by Sailingfish on Aug 19, 2017.

8. Boshu says:

The initial lower bound could be max( sum(A)/K, max(A) )

9. tripe 101 says:

In the sample given in the question; K=3, A=[2, 1, 5, 1, 2, 2, 2] should the answer not be 5 instead of the given answer 6? You can group the elements as [2,2,1].[2,2,1],

• Sheng says:

>> “You should divide this array into K blocks of consecutive elements.”
You cannot shuffle the input array. Thanks!

10. Evgenii says:

Sheng, thank you for great work!
I have one question, how can you prove that resultMaxMid is the minimal large sum?

• Sheng says:

Essentially, the solution is a binary search among all possible answers. So yes, we get the right answer 🙂
To better understand the solution, you could read the official answer for a very similar question boron2013 (Flags) : https://codility.com/media/train/solution-flags.pdf

11. ruisong says:

Great work. I am always inspired by your ideas and skills.
For those who are still studying in 2021, you need to get the ceiling value of the average like this (using the ceil from math)