Check if a given number can be expressed as pair-sum of sum of first X natural numbers
Given an integer N, the task is to check if N is the sum of a pair of integers which can be expressed as the sum of first X natural numbers, where X can be any positive integer. If satisfies the required condition. Print “YES”. Otherwise, print “NO”.
Examples:
Input: N = 25
Output: YES
Explanation:
=> 10 + 15 = 25
Since 10 and 15 are the sum of first 4 and 5 natural numbers respectively, the answer is YES.
Input: N = 512
Output: NO
Approach: The idea is to choose a sum of natural numbers M which is less than equal to N and check if M and N – M are the sums of the sequence of the first few natural numbers. Follow the steps below to solve the problem:
- Iterate over a loop to calculate the sum of K natural numbers:
Sum of K natural numbers = K * (K + 1) / 2
- Then, calculate the remaining sum and check if the sum is the sum by the following equation:
Y = N – Sum of K Natural number
=> Y = N – (K * (K + 1) / 2)
- Check if the number calculated above satisfies the required condition by calculating the square root of the twice of the number and check if the product of consecutive numbers is equal to the twice of the number.
M * (M + 1) == 2 * Y, where M = √ (2 * Y)
- If the above condition is satisfied, print “YES”. Otherwise, print “NO”.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
void checkSumOfNatural( int n)
{
int i = 1;
bool flag = false ;
while (i * (i + 1) < n * 2)
{
int X = i * (i + 1);
int t = n * 2 - X;
int k = sqrt (t);
if (k * (k + 1) == t)
{
flag = true ;
break ;
}
i += 1;
}
if (flag)
cout << "YES" ;
else
cout << "NO" ;
}
int main()
{
int n = 25;
checkSumOfNatural(n);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
class GFG{
static void checkSumOfNatural( int n)
{
int i = 1 ;
boolean flag = false ;
while (i * (i + 1 ) < n * 2 )
{
int X = i * (i + 1 );
int t = n * 2 - X;
int k = ( int )Math.sqrt(t);
if (k * (k + 1 ) == t)
{
flag = true ;
break ;
}
i += 1 ;
}
if (flag)
System.out.println( "YES" );
else
System.out.println( "NO" );
}
public static void main (String[] args)
{
int n = 25 ;
checkSumOfNatural(n);
}
}
|
Python3
import math
def checkSumOfNatural(n):
i = 1
flag = False
while i * (i + 1 ) < n * 2 :
X = i * (i + 1 )
t = n * 2 - X
k = int (math.sqrt(t))
if k * (k + 1 ) = = t:
flag = True
break
i + = 1
if flag:
print ( 'YES' )
else :
print ( 'NO' )
if __name__ = = "__main__" :
n = 25
checkSumOfNatural(n)
|
C#
using System;
class GFG{
static void checkSumOfNatural( int n)
{
int i = 1;
bool flag = false ;
while (i * (i + 1) < n * 2)
{
int X = i * (i + 1);
int t = n * 2 - X;
int k = ( int )Math.Sqrt(t);
if (k * (k + 1) == t)
{
flag = true ;
break ;
}
i += 1;
}
if (flag)
Console.WriteLine( "YES" );
else
Console.WriteLine( "NO" );
}
public static void Main(String[] args)
{
int n = 25;
checkSumOfNatural(n);
}
}
|
Javascript
<script>
function checkSumOfNatural(n)
{
var i = 1;
var flag = false ;
while (i * (i + 1) < n * 2)
{
var X = i * (i + 1);
var t = n * 2 - X;
var k = parseInt(Math.sqrt(t));
if (k * (k + 1) == t)
{
flag = true ;
break ;
}
i += 1;
}
if (flag)
document.write( "YES" );
else
document.write( "NO" );
}
var n = 25;
checkSumOfNatural(n);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Approach 2 :
This code checks whether a given number n can be expressed as the sum of two special numbers, where a special number is defined as the sum of the first X natural numbers for some positive integer X.
The approach used in the code is as follows:
- Given a number n, calculate the largest value of X such that X*(X+1)/2 <= n. This is done using the quadratic formula for finding roots of the equation X*(X+1)/2 = n. We take the floor of the result because X must be an integer.
- For each value of i from 1 to X, calculate j = n – (i*(i+1))/2. This is the other number that n must be paired with to form the sum of two special numbers.
- Check whether j is greater than i and less than or equal to X. This ensures that i and j are distinct positive integers that are both less than or equal to X.
- If there exists such a pair of i and j, set the flag to true and break out of the loop.
- If flag is true, output “YES“, indicating that n can be expressed as the sum of two special numbers. Otherwise, output “NO”.
The time complexity of this algorithm is O(sqrt(n)), since the loop from 1 to X runs for at most sqrt(n) iterations.
C++
#include<bits/stdc++.h>
using namespace std;
void checkSumOfNatural( int n)
{
int X = floor ((-1 + sqrt (1 + 8 * n))/2);
bool flag = false ;
for ( int i = 1; i <= X; i++) {
int j = n - (i*(i+1))/2;
if (j > i && j <= X) {
flag = true ;
break ;
}
}
if (flag)
cout << "YES" ;
else
cout << "NO" ;
}
int main()
{
int n = 25;
checkSumOfNatural(n);
return 0;
}
|
Java
import java.util.*;
public class GFG {
public static void checkSumOfNatural( int n) {
int X = ( int ) Math.floor((- 1 + Math.sqrt( 1 + 8 * n)) / 2 );
boolean flag = false ;
for ( int i = 1 ; i <= X; i++) {
int j = n - (i * (i + 1 )) / 2 ;
if (j > i && j <= X) {
flag = true ;
break ;
}
}
if (flag)
System.out.println( "YES" );
else
System.out.println( "NO" );
}
public static void main(String[] args) {
int n = 25 ;
checkSumOfNatural(n);
}
}
|
Python3
import math
def checkSumOfNatural(n):
X = int (( - 1 + math.sqrt( 1 + 8 * n)) / 2 )
flag = False
for i in range ( 1 , X + 1 ):
j = n - (i * (i + 1 )) / / 2
if j > i and j < = X:
flag = True
break
if flag:
print ( "YES" )
else :
print ( "NO" )
if __name__ = = "__main__" :
n = 25
checkSumOfNatural(n)
|
C#
using System;
class GFG
{
static void CheckSumOfNatural( int n)
{
int X = ( int )((-1 + Math.Sqrt(1 + 8 * n)) / 2);
bool flag = false ;
for ( int i = 1; i <= X; i++)
{
int j = n - (i * (i + 1)) / 2;
if (j > i && j <= X)
{
flag = true ;
break ;
}
}
if (flag)
{
Console.WriteLine( "YES" );
}
else
{
Console.WriteLine( "NO" );
}
}
static void Main( string [] args)
{
int n = 25;
CheckSumOfNatural(n);
}
}
|
Javascript
function checkSumOfNatural(n) {
let X = Math.floor((-1 + Math.sqrt(1 + 8 * n)) / 2);
let flag = false ;
for (let i = 1; i <= X; i++) {
let j = n - (i * (i + 1)) / 2;
if (j > i && j <= X) {
flag = true ;
break ;
}
}
if (flag) {
console.log( "YES" );
} else {
console.log( "NO" );
}
}
let n = 25;
checkSumOfNatural(n);
|
Time Complexity : O(N)
Auxiliary Space : O(1)
Last Updated :
28 Jul, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...