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”.
Input: N = 25
=> 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
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:
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.
- Find ways an Integer can be expressed as sum of n-th power of unique natural numbers
- Check if a number can be expressed as a sum of consecutive numbers
- Check if a prime number can be expressed as sum of two Prime Numbers
- Check if a number can be expressed as sum two abundant numbers
- Count prime numbers that can be expressed as sum of consecutive prime numbers
- Check whether a number can be expressed as a product of single digit numbers
- Check if a number can be expressed as sum of two Perfect powers
- Numbers within a range that can be expressed as power of two numbers
- Check if N can be expressed as product of 3 distinct numbers
- Check if an integer can be expressed as a sum of two semi-primes
- Check if a number can be expressed as power | Set 2 (Using Log)
- Check if a number can be expressed as 2^x + 2^y
- Check if a number can be expressed as a product of exactly K prime divisors
- Check if a number N can be expressed in base B
- Check if a number can be expressed as x^y (x raised to power y)
- Check if a number can be expressed as a^b | Set 2
- Count of primes in a given range that can be expressed as sum of perfect squares
- Elements of Array which can be expressed as power of prime numbers
- Count primes that can be expressed as sum of two consecutive primes and 1
- Count of primes below N which can be expressed as the sum of two primes
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.