📌  相关文章
📜  将前N个自然数的平方分成两组,且总和的绝对差最小

📅  最后修改于: 2021-04-22 08:33:18             🧑  作者: Mango

给定一个整数N ,任务是将前N个自然数(总是8的倍数)的平方划分为两个集合,以使子集和的差最小。将两个子集都打印为必需的答案。

例子:

天真的方法:想法是生成所有可能的子集对,将它们之间的前N个自然数的平方分开,并计算其子集之和之和的子集。打印发现其总和之差最小的那对子集。

时间复杂度: O(N * 2 N )
辅助空间: O(1)

高效方法:为了优化上述方法,其思想是使用连续平方数之和的数学属性。请注意,始终可以将8个连续的平方数分为2组,以使它们的子集和差为0

请按照以下步骤解决问题:

  • 初始化变量以存储最小子集差异并将其声明为0
  • 初始化变量cnt以存储大小为8的块数,然后将N除以8并将其存储在变量cnt中
  • 通过串联字符串“ ABBABAAB”来初始化字符串分区并存储N个元素的分区模式, cnt次数,其中“ ABBABAAB”代表大小为8的子数组中每个元素必须进入的子集。
  • 初始化2个向量AB以存储子集A和子集B的元素。
  • 如果pattern [i]‘A’,则在[0,N – 1]范围内循环循环,并将(i + 1) 2插入向量A中。否则,将(i +1) 2插入向量B中
  • 完成上述步骤后,打印最小子集差,向量A和向量B。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to partition squares of N
// natural number in two subset
void minimumSubsetDifference(int N)
{
    // Store the count of blocks of size 8
    int blockOfSize8 = N / 8;
 
    // Partition of block of 8 element
    string str = "ABBABAAB";
 
    // Store the minimum subset difference
    int subsetDifference = 0;
 
    // Partition of N elements to minimize
    // their subset sum difference
    string partition = "";
    while (blockOfSize8--) {
        partition += str;
    }
 
    // Store elements of subset A and B
    vector A, B;
 
    for (int i = 0; i < N; i++) {
 
        // If element is of type A
        if (partition[i] == 'A') {
            A.push_back((i + 1) * (i + 1));
        }
 
        // If the element is of type B
        else {
            B.push_back((i + 1) * (i + 1));
        }
    }
 
    // Print the minimum subset difference
    cout << subsetDifference << "\n";
 
    // Print the first subset
    for (int i = 0; i < A.size(); i++)
        cout << A[i] << " ";
 
    cout << "\n";
 
    // Print the second subset
    for (int i = 0; i < B.size(); i++)
        cout << B[i] << " ";
}
 
// Driver Code
int main()
{
    int N = 8;
 
    // Function Call
    minimumSubsetDifference(N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.Arrays;
 
class GFG{
 
// Function to partition squares of N
// natural number in two subset
static void minimumSubsetDifference(int N)
{
     
    // Store the count of blocks of size 8
    int blockOfSize8 = N / 8;
 
    // Partition of block of 8 element
    String str = "ABBABAAB";
 
    // Store the minimum subset difference
    int subsetDifference = 0;
 
    // Partition of N elements to minimize
    // their subset sum difference
    String partition = "";
    while (blockOfSize8-- > 0)
    {
        partition += str;
    }
 
    // Store elements of subset A and B
    int A[] = new int[N];
    int B[] = new int[N];
    int x = 0, y = 0;
 
    for(int i = 0; i < N; i++)
    {
         
        // If element is of type A
        if (partition.charAt(i) == 'A')
        {
            A[x++] = ((i + 1) * (i + 1));
        }
 
        // If the element is of type B
        else
        {
            B[y++] = ((i + 1) * (i + 1));
        }
    }
 
    // Print the minimum subset difference
    System.out.println(subsetDifference);
 
    // Print the first subset
    for(int i = 0; i < x; i++)
        System.out.print(A[i] + " ");
         
    System.out.println();
 
    // Print the second subset
    for(int i = 0; i < y; i++)
        System.out.print(B[i] + " ");
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 8;
     
    // Function Call
    minimumSubsetDifference(N);
}
}
 
// This code is contributed by shailjapriya


Python3
# Python3 program for the above approach
 
# Function to partition squares of N
# natural number in two subset
def minimumSubsetDifference(N):
     
    # Store the count of blocks of size 8
    blockOfSize8 = N // 8
 
    # Partition of block of 8 element
    str = "ABBABAAB"
 
    # Store the minimum subset difference
    subsetDifference = 0
     
    # Partition of N elements to minimize
    # their subset sum difference
    partition = ""
     
    while blockOfSize8 != 0:
        partition = partition + str
        blockOfSize8 = blockOfSize8 - 1
         
    # Store elements of subset A and B
    A = []
    B = []
     
    for i in range(N):
 
        # If element is of type A
        if partition[i] == 'A':
            A.append((i + 1) * (i + 1))
             
        # If the element is of type B
        else:
            B.append((i + 1) * (i + 1))
             
    # Print the minimum subset difference
    print(subsetDifference)
 
    # Print the first subset
    for i in A:
        print(i, end = " ")
    print()
     
    # Print the second subset
    for i in B:
        print(i, end = " ")
 
# Driver Code
N = 8
 
# Function call
minimumSubsetDifference(N)
 
# This code is contributed by shailjapriya


C#
// C# program for the above approach
using System;
 
class GFG{
 
    // Function to partition squares of N
    // natural number in two subset
    static void minimumSubsetDifference(int N)
    {
         
        // Store the count of blocks of size 8
        int blockOfSize8 = N / 8;
     
        // Partition of block of 8 element
        string str = "ABBABAAB";
     
        // Store the minimum subset difference
        int subsetDifference = 0;
     
        // Partition of N elements to minimize
        // their subset sum difference
        string partition = "";
        while (blockOfSize8-- > 0)
        {
            partition += str;
        }
     
        // Store elements of subset A and B
        int []A = new int[N];
        int []B = new int[N];
        int x = 0, y = 0;
     
        for(int i = 0; i < N; i++)
        {
             
            // If element is of type A
            if (partition[i] == 'A')
            {
                A[x++] = ((i + 1) * (i + 1));
            }
     
            // If the element is of type B
            else
            {
                B[y++] = ((i + 1) * (i + 1));
            }
        }
     
        // Print the minimum subset difference
        Console.WriteLine(subsetDifference);
     
        // Print the first subset
        for(int i = 0; i < x; i++)
            Console.Write(A[i] + " ");
             
        Console.WriteLine();
     
        // Print the second subset
        for(int i = 0; i < y; i++)
            Console.Write(B[i] + " ");
    }
     
    // Driver Code
    public static void Main(string[] args)
    {
        int N = 8;
         
        // Function Call
        minimumSubsetDifference(N);
    }
}
 
// This code is contributed by AnkitRai01


输出
0
1 16 36 49 
4 9 25 64

时间复杂度: O(N)
辅助空间: O(N)