📌  相关文章
📜  将前 N 个自然数拆分为两个集合,它们的和的绝对差最小

📅  最后修改于: 2021-10-25 09:18:22             🧑  作者: Mango

给定一个整数N ,将前N 个自然数分成两组,使得它们和之间的绝对差最小。任务是打印可以获得的最小绝对差值。

例子

朴素的方法:这个问题可以使用贪婪技术来解决。请按照以下步骤解决问题:

  • 初始化两个变量,比如sumSet1sumSet2来存储两个集合中元素的总和。
  • N1遍历前N 个自然数。对于每个数字,检查当前 set1 中元素的总和是否小于或等于 set2 中元素的总和。如果发现为真,则将当前遍历的数字添加到set1并更新sumSet1
  • 否则,将当前自然数的值添加到set2并更新sumSet2
  • 最后,打印abs(sumSet1 – sumSet2)作为所需答案。

下面是上述方法的实现:

C++14
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to split the first N
// natural numbers into two sets
// having minimum absolute
// difference of their sums
int minAbsDiff(int N)
{
    // Stores the sum of
    // elements of set1
    int sumSet1 = 0;
 
    // Stores the sum of
    // elements of set2
    int sumSet2 = 0;
 
    // Traverse first N
    // natural numbers
    for (int i = N; i > 0; i--) {
 
        // Check if sum of elements of
        // set1 is less than or equal
        // to sum of elements of set2
        if (sumSet1 <= sumSet2) {
            sumSet1 += i;
        }
        else {
            sumSet2 += i;
        }
    }
    return abs(sumSet1 - sumSet2);
}
 
// Driver Code
int main()
{
    int N = 6;
    cout << minAbsDiff(N);
}


Java
// Java program to implement
// the above approach
import java.io.*;
  
class GFG{
     
// Function to split the first N
// natural numbers into two sets
// having minimum absolute
// difference of their sums
static int minAbsDiff(int N)
{
     
    // Stores the sum of
    // elements of set1
    int sumSet1 = 0;
  
    // Stores the sum of
    // elements of set2
    int sumSet2 = 0;
  
    // Traverse first N
    // natural numbers
    for(int i = N; i > 0; i--)
    {
         
        // Check if sum of elements of
        // set1 is less than or equal
        // to sum of elements of set2
        if (sumSet1 <= sumSet2)
        {
            sumSet1 += i;
        }
        else
        {
            sumSet2 += i;
        }
    }
    return Math.abs(sumSet1 - sumSet2);
}
 
// Driver code
public static void main (String[] args)
{
    int N = 6;
     
    System.out.println(minAbsDiff(N));
}
}
 
// This code is contributed by offbeat


Python3
# Python3 program to implement
# the above approach
 
# Function to split the first N
# natural numbers into two sets
# having minimum absolute
# difference of their sums
def minAbsDiff(N):
     
    # Stores the sum of
    # elements of set1
    sumSet1 = 0
 
    # Stores the sum of
    # elements of set2
    sumSet2 = 0
 
    # Traverse first N
    # natural numbers
    for i in reversed(range(N + 1)):
         
        # Check if sum of elements of
        # set1 is less than or equal
        # to sum of elements of set2
        if sumSet1 <= sumSet2:
           sumSet1 = sumSet1 + i
        else:
           sumSet2 = sumSet2 + i
       
    return abs(sumSet1 - sumSet2)
 
# Driver Code
N = 6
 
print(minAbsDiff(N))
 
# This code is contributed by sallagondaavinashreddy7


C#
// C# program to implement
// the above approach
using System;
 
class GFG{
 
// Function to split the first N
// natural numbers into two sets
// having minimum absolute
// difference of their sums
static int minAbsDiff(int N)
{
     
    // Stores the sum of
    // elements of set1
    int sumSet1 = 0;
  
    // Stores the sum of
    // elements of set2
    int sumSet2 = 0;
  
    // Traverse first N
    // natural numbers
    for(int i = N; i > 0; i--)
    {
         
        // Check if sum of elements of
        // set1 is less than or equal
        // to sum of elements of set2
        if (sumSet1 <= sumSet2)
        {
            sumSet1 += i;
        }
        else
        {
            sumSet2 += i;
        }
    }
    return Math.Abs(sumSet1 - sumSet2);
}
 
// Driver code
static void Main()
{
    int N = 6;
     
    Console.Write(minAbsDiff(N));
}
}
 
// This code is contributed by divyeshrabadiya07


Javascript


C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to split the first N
// natural numbers into two sets
// having minimum absolute
// difference of their sums
int minAbsDiff(int N)
{
    if (N % 4 == 0 || N % 4 == 3) {
        return 0;
    }
    return 1;
}
 
// Driver Code
int main()
{
    int N = 6;
    cout << minAbsDiff(N);
}


Java
// Java program to implement
// the above approach
import java.io.*;
import java.util.*;
 
class GFG{
     
// Function to split the first N
// natural numbers into two sets
// having minimum absolute
// difference of their sums
static int minAbsDiff(int N)
{
    if (N % 4 == 0 || N % 4 == 3)
    {
        return 0;
    }
    return 1;
}
 
// Driver Code
public static void main (String[] args)
{
    int N = 6;
     
    System.out.println(minAbsDiff(N));
}
}
 
// This code is contributed by sallagondaavinashreddy7


Python3
# Python3 program to implement
# the above approach
 
# Function to split the first N
# natural numbers into two sets
# having minimum absolute
# difference of their sums
def minAbsDiff(N):
     
    if (N % 4 == 0 or N % 4 == 3):
        return 0
         
    return 1
 
# Driver Code
N = 6
 
print(minAbsDiff(N))
 
# This code is contributed by sallagondaavinashreddy7


C#
// C# program to implement
// the above approach
using System;
class GFG{
     
// Function to split the first N
// natural numbers into two sets
// having minimum absolute
// difference of their sums
static int minAbsDiff(int N)
{
  if (N % 4 == 0 ||
      N % 4 == 3)
  {
    return 0;
  }
  return 1;
}
 
// Driver Code
public static void Main(String[] args)
{
  int N = 6;
  Console.WriteLine(minAbsDiff(N));
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出
1

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

高效的方法:为了优化上述方法,该想法基于以下观察:

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

  • 如果N % 4 == 0N % 4 == 3 ,则打印0
  • 否则,打印1

下面是上述方法的实现:

C++

// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to split the first N
// natural numbers into two sets
// having minimum absolute
// difference of their sums
int minAbsDiff(int N)
{
    if (N % 4 == 0 || N % 4 == 3) {
        return 0;
    }
    return 1;
}
 
// Driver Code
int main()
{
    int N = 6;
    cout << minAbsDiff(N);
}

Java

// Java program to implement
// the above approach
import java.io.*;
import java.util.*;
 
class GFG{
     
// Function to split the first N
// natural numbers into two sets
// having minimum absolute
// difference of their sums
static int minAbsDiff(int N)
{
    if (N % 4 == 0 || N % 4 == 3)
    {
        return 0;
    }
    return 1;
}
 
// Driver Code
public static void main (String[] args)
{
    int N = 6;
     
    System.out.println(minAbsDiff(N));
}
}
 
// This code is contributed by sallagondaavinashreddy7

蟒蛇3

# Python3 program to implement
# the above approach
 
# Function to split the first N
# natural numbers into two sets
# having minimum absolute
# difference of their sums
def minAbsDiff(N):
     
    if (N % 4 == 0 or N % 4 == 3):
        return 0
         
    return 1
 
# Driver Code
N = 6
 
print(minAbsDiff(N))
 
# This code is contributed by sallagondaavinashreddy7

C#

// C# program to implement
// the above approach
using System;
class GFG{
     
// Function to split the first N
// natural numbers into two sets
// having minimum absolute
// difference of their sums
static int minAbsDiff(int N)
{
  if (N % 4 == 0 ||
      N % 4 == 3)
  {
    return 0;
  }
  return 1;
}
 
// Driver Code
public static void Main(String[] args)
{
  int N = 6;
  Console.WriteLine(minAbsDiff(N));
}
}
 
// This code is contributed by 29AjayKumar

Javascript


输出
1

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