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:



Below is the implementation of the above approach:

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

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

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

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.




Article Tags :
Practice Tags :