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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program of the above approach
#include<bits/stdc++.h>
using namespace std;
  
// Function to check if the number 
// is pair-sum of sum of first X 
// natural numbers 
void checkSumOfNatural(int n)
{
    int i = 1;
    bool flag = false;
      
    // Check if the given number 
    // is sum of pair of special numbers 
    while (i * (i + 1) < n * 2)
    
          
        // X is the sum of first 
        // i natural numbers 
        int X = i * (i + 1);
          
        // t = 2 * Y 
        int t = n * 2 - X; 
        int k = sqrt(t); 
          
        // Condition to check if 
        // Y is a special number 
        if (k * (k + 1) == t)
        
            flag = true;
            break;
        }
        i += 1;
    }
      
    if (flag) 
        cout << "YES"
    else
        cout << "NO";
}
  
// Driver Code
int main()
{
    int n = 25;
      
    // Function call 
    checkSumOfNatural(n);
  
    return 0;
}
  
// This code is contributed by rutvik_56

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program of the above approach
import java.util.*;
import java.lang.*;
  
class GFG{
  
// Function to check if the number 
// is pair-sum of sum of first X 
// natural numbers 
static void checkSumOfNatural(int n)
{
    int i = 1;
    boolean flag = false;
      
    // Check if the given number 
    // is sum of pair of special numbers 
    while (i * (i + 1) < n * 2)
    
          
        // X is the sum of first 
        // i natural numbers 
        int X = i * (i + 1);
          
        // t = 2 * Y 
        int t = n * 2 - X; 
        int k = (int)Math.sqrt(t); 
          
        // Condition to check if 
        // Y is a special number 
        if(k * (k + 1) == t)
        
            flag = true;
            break;
        }
        i += 1;
    }
      
    if (flag) 
        System.out.println("YES"); 
    else
        System.out.println("NO");
}
  
// Driver Code
public static void main (String[] args)
{
    int n = 25;
      
    // Function call 
    checkSumOfNatural(n);
}
}
  
// This code is contributed by offbeat

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program of the 
# above approach
  
import math
  
# Function to check if the number
# is pair-sum of sum of first X 
# natural numbers
def checkSumOfNatural(n):
    i = 1
    flag = False
      
    # Check if the given number 
    # is sum of pair of special numbers
    while i*(i + 1) < n * 2:
          
        # X is the sum of first
        # i natural numbers
        X = i*(i + 1)
          
        # t = 2 * Y
        t = n * 2 - X
        k = int(math.sqrt(t))
          
        # Condition to check if
        # Y is a special number
        if k*(k + 1) == t:
            flag = True
            break
        i += 1
      
    if flag:
        print('YES')
    else:
        print('NO')
  
# Driver Code        
if __name__ == "__main__":
    n = 25
      
    # Function Call
    checkSumOfNatural(n)

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program of 
// the above approach
using System;
class GFG{
  
// Function to check if the number 
// is pair-sum of sum of first X 
// natural numbers 
static void checkSumOfNatural(int n)
{
  int i = 1;
  bool flag = false;
  
  // Check if the given number 
  // is sum of pair of special numbers 
  while (i * (i + 1) < n * 2)
  
    // X is the sum of first 
    // i natural numbers 
    int X = i * (i + 1);
  
    // t = 2 * Y 
    int t = n * 2 - X; 
    int k = (int)Math.Sqrt(t); 
  
    // Condition to check if 
    // Y is a special number 
    if(k * (k + 1) == t)
    
      flag = true;
      break;
    }
    i += 1;
  }
  
  if (flag) 
    Console.WriteLine("YES"); 
  else
    Console.WriteLine("NO");
}
  
// Driver Code
public static void Main(String[] args)
{
  int n = 25;
  
  // Function call 
  checkSumOfNatural(n);
}
}
  
// This code is contributed by Rajput-Ji

chevron_right


Output: 

YES

Time Complexity: O(N) 
Auxiliary Space: O(1) 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : offbeat, Rajput-Ji, rutvik_56