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

📅  最后修改于: 2021-06-25 13:19:11             🧑  作者: 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

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

Java脚本


输出
1

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