Array segmentation in the beauty of programming-personal thoughts and proofs

Source: Internet
Author: User

I have been watching the beauty of programming recently. My thoughts are really important. I am not confident in this article today. I hope to discuss it with like-minded people!





There is an unordered positive integer array with 2n element numbers. Requirement: how can this array be split into two arrays with N as the number of elements and make the two subarrays closer to the nearest one?

For example, the following Array


First line: source array

Row 2: Destination Array

I have discussed many methods in the book, and I will not go into details here. My method is also very simple. I believe many people have come up with it, but there is no way to prove the reliability of this method. Today we will focus on the reliability of this method.


Let's talk about the following method:

1. Sort unordered arrays first (time complexity nlogn)

2. Take the largest element from the current source array and place it in one or the smallest destination array (both are 0 at first, so you can place it at will). At the same time, delete the largest element from the source array.

3. Repeat Step 1 until the number of source arrays is 0.


The result of the above method:


43 on the left and 44 on the right. Match the question.


Many people first feel that the question is not so simple. ② this solution may have vulnerabilities. I will give my personal understanding about this solution.


1. I think of this problem as putting chips on the balance. The final requirement is that the difference between the two ends of the balance is the least;

2. Sort the chips to be put in order first;

3. First place the maximum chip to any segment of the balance (the balance at first );

4. Place the second chip to the lighter part of the balance chip;

5. Place the third chip to the lighter part of the balance chip (it must be with the second chip ). At this time, there are two possibilities: ①: the balance is not moving, at this time, we should continue to put one piece in accordance with the rules; ② the balance of the balance has changed (including the balance between the two sides and the inverter), we can be sure that, at this moment, the weight difference between the two sides of the balance must be less than or equal to the one just put (currently the lightest chip on the Balance)

6. According to the above method, if the balance of the balance changes after the last part, we can ensure that the difference between the quantitative change of the balance is always less than or equal to the smallest of all chips. When there is no change, we can also ensure that our results are correct (consider it yourself)


Currently, I can only think of this. According to the preceding rule, the time complexity should be nlogn (sorting) + n = O (nlogn)


If I say something wrong, I can leave a message and hope I can meet some friends to discuss this issue.






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.