📌  相关文章
📜  最小化两个子集之和的绝对差

📅  最后修改于: 2021-04-27 20:23:04             🧑  作者: Mango

给定数字n,将前n个自然数(1、2,… n)分成两个子集,以使两个子集的总和之间的差异最小。
例子:

Input : n = 4
Output : First subset sum = 5, 
         Second subset sum = 5.
         Difference = 0
Explanation:
Subset 1: 1 4 
Subset 2: 2 3 

Input : n = 6 
Output: First subset sum = 10, 
        Second subset sum = 11.
        Difference = 1
Explanation : 
Subset 1: 1 3 6 
Subset 2: 2 4 5 

方法:
该方法基于以下事实:通过将中间的两个元素放在一组中而将角元素放在另一组中,可以将任何四个连续的数字分为两组。因此,如果n是4的倍数,则它们的差将为0,因此一组的总和将是N个元素的总和的一半,这可以通过使用sum = n *(n + 1)/ 2来计算
在其他三种情况下,我们不能分为4组,剩下的1、2和3:
a)如果剩下的余数为1,则所有其他n-1个元素合并为4组,因此它们的总和为int(sum / 2),另一半的总和为int(sum / 2 + 1),并且他们的差异永远是1。
b)在n%4 == 2的情况下,也将遵循上述步骤。在这里,我们为3以后的元素形成大小为4的组。其余元素将是1和2。1进入一组,而2进入另一组。
c)当n%4 == 3时,将n-3个元素合并为4个。剩下的元素将为1、2和3,其中1和2进入一组,而3进入另一组,最终使差为0,每个集合的总和为sum / 2。
下面是上述方法的实现:

CPP
// CPP program to Minimize the absolute
// difference of sum of two subsets
#include 
using namespace std;
 
// function to print difference
void subsetDifference(int n)
{
    // summation of n elements
    int s = n * (n + 1) / 2;
 
    // if divisible by 4
    if (n % 4 == 0) {
        cout << "First subset sum = "
             << s / 2;
        cout << "\nSecond subset sum = "
             << s / 2;
        cout << "\nDifference = " << 0;
    }
    else {
 
        // if remainder 1 or 2. In case of remainder
        // 2, we divide elements from 3 to n in groups
        // of size 4 and put 1 in one group and 2 in
        // group. This also makes difference 1.
        if (n % 4 == 1 || n % 4 == 2) {
 
            cout << "First subset sum = "
                 << s / 2;
            cout << "\nSecond subset sum = "
                 << s / 2 + 1;
            cout << "\nDifference = " << 1;
        }
 
        // We put elements from 4 to n in groups of
        // size 4. Remaining elements 1, 2 and 3 can
        // be divided as (1, 2) and (3).
        else
        {
            cout << "First subset sum = "
                 << s / 2;
            cout << "\nSecond subset sum = "
                 << s / 2;
            cout << "\nDifference = " << 0;
        }
    }
}
 
// driver program to test the above function
int main()
{
    int n = 6;
    subsetDifference(n);
    return 0;
}


Java
// Java program for Minimize the absolute
// difference of sum of two subsets
import java.util.*;
 
class GFG {
     
    // function to print difference
    static void subsetDifference(int n)
    {
        // summation of n elements
        int s = n * (n + 1) / 2;
      
        // if divisible by 4
        if (n % 4 == 0) {
 
                 System.out.println("First subset sum = " + s / 2);
                 System.out.println("Second subset sum = " + s / 2);
                 System.out.println("Difference = " + 0);
        }
        else {
      
            // if remainder 1 or 2. In case of remainder
            // 2, we divide elements from 3 to n in groups
            // of size 4 and put 1 in one group and 2 in
            // group. This also makes difference 1.
            if (n % 4 == 1 || n % 4 == 2) {
      
                  System.out.println("First subset sum = " + s / 2);
                  System.out.println("Second subset sum = " + ((s / 2) + 1));
                  System.out.println("Difference = " + 1);
            }
      
            // We put elements from 4 to n in groups of
            // size 4. Remaining elements 1, 2 and 3 can
            // be divided as (1, 2) and (3).
            else
            {
                 System.out.println("First subset sum = " + s / 2);
                 System.out.println("Second subset sum = " + s / 2);
                 System.out.println("Difference = " + 0);
            }
        }
    }
     
    /* Driver program to test above function */
    public static void main(String[] args)
    {
         int n = 6;
         subsetDifference(n);
    }
}
         
// This code is contributed by Arnav Kr. Mandal.


Python3
# Python3 code to Minimize the absolute
# difference of sum of two subsets
 
# function to print difference
def subsetDifference( n ):
 
    # summation of n elements
    s = int(n * (n + 1) / 2)
     
    # if divisible by 4
    if n % 4 == 0:
        print("First subset sum = ", int(s / 2))
        print("Second subset sum = ",int(s / 2))
        print("Difference = " , 0)
 
    else:
 
        # if remainder 1 or 2. In case of remainder
        # 2, we divide elements from 3 to n in groups
        # of size 4 and put 1 in one group and 2 in
        # group. This also makes difference 1.
        if n % 4 == 1 or n % 4 == 2:
            print("First subset sum = ",int(s/2))
            print("Second subset sum = ",int(s/2)+1)
            print("Difference = ", 1)
             
        # We put elements from 4 to n in groups of
        # size 4. Remaining elements 1, 2 and 3 can
        # be divided as (1, 2) and (3).
        else:
            print("First subset sum = ", int(s / 2))
            print("Second subset sum = ",int(s / 2))
            print("Difference = " , 0)
             
# driver code to test the above function
n = 6
subsetDifference(n)
 
# This code is contributed by "Sharad_Bhardwaj".


C#
// C# program for Minimize the absolute
// difference of sum of two subsets
using System;
 
class GFG {
     
    // function to print difference
    static void subsetDifference(int n)
    {
         
        // summation of n elements
        int s = n * (n + 1) / 2;
     
        // if divisible by 4
        if (n % 4 == 0) {
 
            Console.WriteLine("First "
            + "subset sum = " + s / 2);
                 
            Console.WriteLine("Second "
            + "subset sum = " + s / 2);
                 
            Console.WriteLine("Difference"
                              + " = " + 0);
        }
        else {
     
            // if remainder 1 or 2. In case
            // of remainder 2, we divide
            // elements from 3 to n in groups
            // of size 4 and put 1 in one
            // group and 2 in group. This
            // also makes difference 1.
            if (n % 4 == 1 || n % 4 == 2) {
     
                Console.WriteLine("First "
                + "subset sum = " + s / 2);
                 
                Console.WriteLine("Second "
                + "subset sum = " + ((s / 2)
                                      + 1));
                                       
                Console.WriteLine("Difference"
                               + " = " + 1);
            }
     
            // We put elements from 4 to n
            // in groups of size 4. Remaining
            // elements 1, 2 and 3 can
            // be divided as (1, 2) and (3).
            else
            {
                Console.WriteLine("First "
                + "subset sum = " + s / 2);
                 
                Console.WriteLine("Second "
                 + "subset sum = " + s / 2);
                        
                Console.WriteLine("Difference"
                                + " = " + 0);
            }
        }
    }
     
    /* Driver program to test above
    function */
    public static void Main()
    {
        int n = 6;
         
        subsetDifference(n);
    }
}
         
// This code is contributed by vt_m.


PHP


Javascript


输出:

First subset sum = 10
Second subset sum = 11
Difference = 1