Javatpoint Logo
Javatpoint Logo

Zumkeller Numbers in Java

A number N is said to be the Zumkeller number if one can partition all the factors of the number N into two sets, such that the sum of the numbers (factors) in the first set is equal to the sum of the numbers (factors)in the second set. The sets must be non-empty. Let's see a few examples to understand it. In the input, the number N will be given to us.

Example 1:

Input: N = 84

Output: Yes, 84 is a Zumkeller number.

Explanation: The factors of 84 are: 1, 2, 3, 4, 6, 7, 12, 14, 21, 28, 42, 84. Now, if we partition these factors into two sets like the following, we get

(28, 84) and (1, 2, 3, 4, 6, 7, 12, 14, 21, 42). Now, the sum of elements of the first set gives 28 + 84 = 112

The sum of elements of the second set gives:

1 + 2 + 3 + 4 + 6 + 7 + 12 + 14 + 21 + 42 = 112. Since the sum of elements of these two sets is the same, the number 84 is a Zumkeller number.

Example 2:

Input: N = 108

Output: Yes, 108 is a Zumkeller number.

Explanation: The factors of 108 are: 1, 2, 3, 4, 6, 9, 12, 18, 27, 36, 54, 108. Now, if we partition these factors into two sets like the following, we get

(2, 3, 27, 108) and (1, 4, 6, 9, 12, 18, 36, 54). Now, the sum of elements of the first set gives 2 + 3 + 27 + 108 = 140

The sum of elements of the second set gives:

1 + 4 + 6 + 9 + 12 + 18 + 36 + 54 = 140. Since the sum of elements of these two sets is the same, the number 108 is a Zumkeller number.

Example 3:

Input: N = 200

Output: No, 200 is not a Zumkeller number.

Explanation: The factors of 200 are: 1, 2, 4, 5, 8, 10, 20, 25, 40, 50, 100, 200. Now, if we partition these factors into any two sets, we will not get that pair of sets whose sum of elements will be equal to each other. Hence, 200 is not a Zumkeller number.

Naïve Approach

The approach is straightforward. First of all, we will find all of the factors of the number N and store them in an array. Also, compute the sum of the elements stored in the array and store it in a variable called totalSum. Now, we will compute all of the possible sets that are possible and store them in the 2-dimensional array. Iterate over the 2-dimensional array and find the sum of the sets one by one in each iteration. Let's store it in a variable called setSum. Now, subtract totalSum and setSum, and whatever value is obtained if it matches with the setSum, then the number N is the Zumkeller number. If not, repeat the above-mentioned process for the other iterations too. If all the iteration is exhausted and at not a single iteration, we get the value of totalSum - setSum equal to setSum, then we can say that N is not the Zumkeller number.

FileName: ZumkellerNaive.java

Output:

Yes, 84 is a Zumkeller number.
Yes, 108 is a Zumkeller number.
No, 200 is not a Zumkeller number.

Complexity Analysis: The above program uses tree-recursion, making the time complexity of the program exponential, which is O(2f), and the space complexity of the program is also exponential, which is O(f * 2f), where f is the total number of factors of the input number n.

The space and time complexities of the above program are very high and is not suitable for those numbers which have a large number of factors. For such numbers, we may get the time limit exceeded, or the memory limit exceeded. Therefore, some optimization is required.

Approach: Using Dynamic Programming

We can do better with the help of dynamic programming. Observe the steps to solve the problem with the help of dynamic programming.

Step 1: First, check whether the sum of factors of the number N is even or not. If the sum is not even, we can conclude that the number is not the Zumkeller number.

Step 2: Declare a 2-D array dp[][] of size (sumofFactors / 2) + 1 * (size + 1), where size is the total number of factors.

Step 3: Run a for loop for 0 <= j <= size and set dp[0][j] with a true value. It is because zero-sum is always feasible.

Step 4: Using a for loop, for 1 <= j <= sumofFactors / 2 and set dp[j][0] as zero as any sum with the sum of elements as zero is not possible.

Step 5: Run a nested for loop for 1 <= k <= sumofFactors / 2 and 1 <= k <= size, and dp[j][k] equal to dp[j][k - 1]

Step 6: If j is larger than or equal to arr[k-1], if dp[j - arr[k - 1]][k - 1] is true, then assign a value true to dp[j][k].

Now, let's do the implementation of the above steps.

FileName: ZumkellerDP.java

Output:

Yes, 84 is a Zumkeller number.
Yes, 108 is a Zumkeller number.
No, 200 is not a Zumkeller number.

Complexity Analysis: The program uses a nested for-loop. The outer loops run sumOfFactors / 2 times, and the inner loop runs from 1 to size times. Thus making the time complexity of the program O(size * sumOfFactors). The space complexity of the program is also O(size * sumOfFactors) because of the two-dimensional array used in the program.

We can still do optimization to reduce the space complexity. The following program illustrates the same.

FileName: ZumkellerDP.java

Output:

Yes, 84 is a Zumkeller number.
Yes, 108 is a Zumkeller number.
No, 200 is not a Zumkeller number.

Complexity Analysis: The time complexity of the program is the same as the previous program. The space complexity of the program is the O(sumOfFactor), where sumOfFactor is the total sum of factors of the input number n.

Finding Zumkeller Numbers Within a Range

The following program illustrates how one can find the Zumkeller numbers within a range.

FileName: ZumkellerNumbersRange.java

Output:

Zumkeller Numbers from 1 to 100 are:
6 12 20 24 28 30 40 42 48 54 56 60 66 70 78 80 84 88 90 96






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA