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++
// 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 |
Java
// 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 |
Python3
# 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) |
C#
// 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 |
Javascript
<script> // javascript program of the above approach// Function to check if the number // is pair-sum of sum of first X // natural numbers function checkSumOfNatural(n) { var i = 1; var 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 var X = i * (i + 1); // t = 2 * Y var t = n * 2 - X; var k = parseInt(Math.sqrt(t)); // Condition to check if // Y is a special number if (k * (k + 1) == t) { flag = true ; break ; } i += 1; } if (flag) document.write( "YES" ); else document.write( "NO" ); } // Driver Code var n = 25; // Function call checkSumOfNatural(n); // This code is contributed by Princi Singh </script> |
YES
Time Complexity: O(N)
Auxiliary Space: O(1)
Please Login to comment...