|
1 | 1 | package com.thealgorithms.backtracking;
|
2 | 2 |
|
3 |
| -/* |
4 |
| - * Problem Statement : |
5 |
| - * Find the number of ways that a given integer, N , can be expressed as the sum of the Xth powers |
6 |
| - * of unique, natural numbers. For example, if N=100 and X=3, we have to find all combinations of |
7 |
| - * unique cubes adding up to 100. The only solution is 1^3+2^3+3^3+4^3. Therefore output will be 1. |
| 3 | +/** |
| 4 | + * Problem Statement: |
| 5 | + * Find the number of ways that a given integer, N, can be expressed as the sum of the Xth powers |
| 6 | + * of unique, natural numbers. |
| 7 | + * For example, if N=100 and X=3, we have to find all combinations of unique cubes adding up to 100. |
| 8 | + * The only solution is 1^3 + 2^3 + 3^3 + 4^3. Therefore, the output will be 1. |
| 9 | + * |
| 10 | + * N is represented by the parameter 'targetSum' in the code. |
| 11 | + * X is represented by the parameter 'power' in the code. |
8 | 12 | */
|
9 | 13 | public class PowerSum {
|
10 | 14 |
|
11 |
| - private int count = 0; |
12 |
| - private int sum = 0; |
13 |
| - |
14 |
| - public int powSum(int n, int x) { |
15 |
| - sum(n, x, 1); |
16 |
| - return count; |
| 15 | + /** |
| 16 | + * Calculates the number of ways to express the target sum as a sum of Xth powers of unique natural numbers. |
| 17 | + * |
| 18 | + * @param targetSum The target sum to achieve (N in the problem statement) |
| 19 | + * @param power The power to raise natural numbers to (X in the problem statement) |
| 20 | + * @return The number of ways to express the target sum |
| 21 | + */ |
| 22 | + public int powSum(int targetSum, int power) { |
| 23 | + // Special case: when both targetSum and power are zero |
| 24 | + if (targetSum == 0 && power == 0) { |
| 25 | + return 1; // by convention, one way to sum to zero: use nothing |
| 26 | + } |
| 27 | + return sumRecursive(targetSum, power, 1, 0); |
17 | 28 | }
|
18 | 29 |
|
19 |
| - // here i is the natural number which will be raised by X and added in sum. |
20 |
| - public void sum(int n, int x, int i) { |
21 |
| - // if sum is equal to N that is one of our answer and count is increased. |
22 |
| - if (sum == n) { |
23 |
| - count++; |
24 |
| - return; |
25 |
| - } // we will be adding next natural number raised to X only if on adding it in sum the |
26 |
| - // result is less than N. |
27 |
| - else if (sum + power(i, x) <= n) { |
28 |
| - sum += power(i, x); |
29 |
| - sum(n, x, i + 1); |
30 |
| - // backtracking and removing the number added last since no possible combination is |
31 |
| - // there with it. |
32 |
| - sum -= power(i, x); |
| 30 | + /** |
| 31 | + * Recursively calculates the number of ways to express the remaining sum as a sum of Xth powers. |
| 32 | + * |
| 33 | + * @param remainingSum The remaining sum to achieve |
| 34 | + * @param power The power to raise natural numbers to (X in the problem statement) |
| 35 | + * @param currentNumber The current natural number being considered |
| 36 | + * @param currentSum The current sum of powered numbers |
| 37 | + * @return The number of valid combinations |
| 38 | + */ |
| 39 | + private int sumRecursive(int remainingSum, int power, int currentNumber, int currentSum) { |
| 40 | + int newSum = currentSum + (int) Math.pow(currentNumber, power); |
| 41 | + |
| 42 | + if (newSum == remainingSum) { |
| 43 | + return 1; |
33 | 44 | }
|
34 |
| - if (power(i, x) < n) { |
35 |
| - // calling the sum function with next natural number after backtracking if when it is |
36 |
| - // raised to X is still less than X. |
37 |
| - sum(n, x, i + 1); |
| 45 | + if (newSum > remainingSum) { |
| 46 | + return 0; |
38 | 47 | }
|
39 |
| - } |
40 | 48 |
|
41 |
| - // creating a separate power function so that it can be used again and again when required. |
42 |
| - private int power(int a, int b) { |
43 |
| - return (int) Math.pow(a, b); |
| 49 | + return sumRecursive(remainingSum, power, currentNumber + 1, newSum) + sumRecursive(remainingSum, power, currentNumber + 1, currentSum); |
44 | 50 | }
|
45 | 51 | }
|
0 commit comments