![]() |
VOOZH | about |
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 possibilityInput : 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
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.
1
Time Complexity: O(x^(1/n)),
Space Complexity: O(log(x))
Simple Recursive Solution:
contributed by Ram Jondhale.
1