- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
First N natural can be divided into two sets with given difference and co-prime sums in C++
In this tutorial, we have to find whether the natural numbers from 1 to n is divided into two halves or not. It has to satisfy the following conditions.
The absolute difference between the two series sum should be m.
And the GCD of two sums should be 1 i.e.., co-primes.
The sum of first n natural numbers is (n*(n+1))/2. We can find the sumOne and sumTwo as we have total sum and difference m. See the below equations.
sumOne + sumTwo = (n*(n+1))/2 sumOne - sumTwo = m
Example
Check whether the absolute sum is equal to m or not. And then check for GCD.
#include <bits/stdc++.h> using namespace std; bool canSplitIntoTwoHalves(int n, int m) { int total_sum = (n * (n + 1)) / 2; int sumOne = (total_sum + m) / 2; int sumTwo = total_sum - sumOne; if (total_sum < m) { return false; } if (sumOne + sumTwo == total_sum &&sumOne - sumTwo == m) { return (__gcd(sumOne, sumTwo) == 1); } return false; } int main() { int n = 10, m = 17; if (canSplitIntoTwoHalves(n, m)) { cout << "Can split"; } else { cout << "Can't split"; } return 0; }
Output
If you run the above code, then you will get the following result.
Can split
Conclusion
If you have any queries in the tutorial, mention them in the comment section.
- Related Articles
- Possible two sets from first N natural numbers difference of sums as D in C++
- Can array be divided into n partitions with equal sums in JavaScript
- C++ program to find minimum difference between the sums of two subsets from first n natural numbers
- Sum of square-sums of first n natural numbers
- Count of distinct sums that can be obtained by adding prime numbers from given arrays in C++
- Find the sums for which an array can be divided into subarrays of equal sum in Python
- Find if array can be divided into two subarrays of equal sum in C++
- Can parasites be divided into partial and complete parasites?
- Check if any square (with one colored cell) can be divided into two equal parts in Python
- Find if given number is sum of first n natural numbers in C++
- Check if array can be divided into two sub-arrays such that their absolute difference is Ks in Python
- PHP program to find the first natural number whose factorial can be divided by a number ‘x’
- Find permutation of first N natural numbers that satisfies the given condition in C++
- Check if a large number can be divided into two or more segments of equal sum in C++
- Program to find number of magic sets from a permutation of first n natural numbers in Python

Advertisements