VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-ways-integer-can-expressed-sum-n-th-power-unique-natural-numbers/

⇱ Find ways an Integer can be expressed as sum of n-th power of unique natural numbers - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Find ways an Integer can be expressed as sum of n-th power of unique natural numbers

Last Updated : 23 Jul, 2025

Given two numbers x and n, find a number of ways x can be expressed as sum of n-th power of unique natural numbers.

Examples : 

Input  : x = 10, n = 2
Output : 1
Explanation: 10 = 12 + 32, Hence total 1 possibility

Input  : x = 100, n = 2
Output : 3
Explanation: 
100 = 102 OR 62 + 82 OR 12 + 32 + 42 + 52 + 72 Hence total 3 possibilities

The idea is simple. We iterate through all number starting from 1. For every number, we recursively try all greater numbers and if we are able to find sum, we increment result


Output
1

Time Complexity: O(x^(1/n)), which is the maximum possible value of curr_num.
Space Complexity: O(log(x)) for the recursion stack.

Alternate Solution :

Below is an alternate simpler solution provided by Shivam Kanodia.


Output
1

Time Complexity: O(x^(1/n)),
Space Complexity: O(log(x)) 

Simple Recursive Solution:

contributed by Ram Jondhale.


Output
1


 

Comment
Article Tags: