Split first N natural numbers into two sets with minimum absolute difference of their sums
Given an integer N, split the first N natural numbers into two sets such that the absolute difference between their sum is minimum. The task is to print the minimum absolute difference that can be obtained.
Examples:
Input: N = 5
Output: 1
Explanation:
Split the first N (= 5) natural numbers into sets {1, 2, 5} (sum = 8) and {3, 4} (sum = 7).
Therefore, the required output is 1.Input: N = 6
Output: 1
Naive Approach: This problem can be solved using the Greedy technique. Follow the steps below to solve the problem:
- Initialize two variables, say sumSet1 and sumSet2 to store the sum of the elements from the two sets.
- Traverse first N natural numbers from N to 1. For every number, check if the current sum of elements in set1 is less than or equal to the sum of elements in set2. If found to be true, add the currently traversed number into set1 and update sumSet1.
- Otherwise, add the value of the current natural number to set2 and update sumSet2.
- Finally, print abs(sumSet1 – sumSet2) as the required answer.
Below is the implementation of the above approach:
C++14
// C++ program to implement // the above approach #include <bits/stdc++.h> 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
<script> // Javascript program to implement // the above approach // Function to split the first N // natural numbers into two sets // having minimum absolute // difference of their sums function minAbsDiff(N) { // Stores the sum of // elements of set1 var sumSet1 = 0; // Stores the sum of // elements of set2 var sumSet2 = 0; // Traverse first N // natural numbers for (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 var N = 6; document.write(minAbsDiff(N)); // This code is contributed by umadevi9616 </script> |
1
Time Complexity: O(N)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach the idea is based on the following observations:
Splitting any 4 consecutive integers into 2 sets gives the minimum absolute difference of their sum equal to 0.
Mathematical proof:
Considering 4 consecutive integers {a1, a2, a3, a4}
a4 = a3 + 1
a1=a2 – 1
=> a4 + a1 = a3 + 1 + a2 – 1
=> a4 + a1 = a2 + a3
Follow the steps below to solve the problem:
- If N % 4 == 0 or N % 4 == 3, then print 0.
- Otherwise, print 1.
Below is the implementation of the above approach:
C++
// C++ program to implement // the above approach #include <bits/stdc++.h> 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
<script> // javascript program to implement // the above approach // Function to split the first N // natural numbers into two sets // having minimum absolute // difference of their sums function minAbsDiff(N) { if (N % 4 == 0 || N % 4 == 3) { return 0; } return 1; } // Driver Code var N = 6; document.write(minAbsDiff(N)); // This code contributed by gauravrajput1 </script> |
1
Time Complexity: O(1)
Auxiliary Space: O(1)