Divide the array into two parts, so that the two parts are the closest and return the difference between the two parts.

Source: Internet
Author: User

[Problem] divides the array into two parts so that the sum of the two parts is the closest, and returns the difference between the two parts. For example:

Int [] array = {,}, divided into two parts: {, 4}, {7}, the difference is 1.

Reference 1: Section 2.18 of the beauty of programming, but the problem is different. Section 2.18 requires an array with a length of 2n to be divided into two arrays with a length of N, so that the two parts and the nearest.

Reference 2: http://www.tuicool.com/articles/ZF73Af

[Idea] a solution for dynamic planning. Obtain the sum of the array. The problem is converted to: select several elements in the array so that the sum of these elements is <= sum/2, and is a set of elements closest to sum/2.

Open an array: int [] [] f = new int [Length + 1] [sum/2 + 1]

State equation: F [I] [J] = max (F [I-1] [J-array [I] + array [I], F [I-1] [J])

Explanation: F [I] [J] indicates the sum of the I elements in the array <= J, and is the element set closest to J. F [I-1] [J-array [I] represents the sum of the I-1 elements in an array that is closest to J-array [I], so f [I] [J] should be the biggest one in [I-1] [J-array [I] + array [I] and f [I-1] [J. It's a bit like a 0-1 backpack problem.

* ** Creation Time: 8:52:57, January 1, October 7, 2014 Project name: Test ** @ author Cao yanfeng * @ since JDK 1.6.0 _ 21 Description: The array is divided into two parts, returns the difference between the two parts. Note: do not divide the array equally */public class dividearraytest {/*** @ Param ARGs */public static void main (string [] ARGs) {// todo auto-generated method stubint [] array = {1, 0, 1, 7, 2, 4}; system. out. println (getmaxdiff (array ));} /* f [I] [J] indicates the maximum capacity that a backpack with an I-element capacity of J can hold */public static int getmaxdiff (INT [] array) {int sum = getsum (array); int length = array. length; int [] [] f = new int [Length + 1] [sum/2 + 1]; for (INT I = 0; I <length; I ++) {for (Int J = 1; j <= sum/2; j ++) {f [I + 1] [J] = f [I] [J]; if (array [I] <= J & F [I] [J-array [I] + array [I]> F [I] [J]) {f [I + 1] [J] = f [I] [J-array [I] + array [I] ;}} return sum-2 * f [length] [sum/2];} public static int getsum (INT [] array) {int sum = 0; For (INT I = 0; I <array. length; I ++) {sum + = array [I] ;}return sum ;}}

Divide the array into two parts, so that the two parts are the closest and return the difference between the two parts.

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: info-contact@alibabacloud.com 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.