Open In App

Zumkelle Number

Last Updated : 24 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given an integer N, the task is to check if N is a Zumkelle Number
 

Zumkelle Number is a whose divisors can be partitioned into two sets with the same sum.
For example, 12 is a Zumkeller number because its divisors 1, 2, 3, 4, 6, 12, can be partitioned in the two sets {12, 2}, and {1, 3, 4, 6} with same sum 14. 
 

Examples: 
 

Input: N = 12 
Output: Yes 
Explanation: 
12’s’ divisors 1, 2, 3, 4, 6, 12, can be partitioned 
in the two sets {12, 2}, and {1, 3, 4, 6} with same sum 14.
Input: N = 26 
Output: No 
 

 

Approach: The idea is to store all the factors of the number in an array and then Finally, partition the array into two subsets such that the sum of elements in both the subset is same. 
Partition problem for the same is explained in detail in this article: 
Partition problem | DP-18
Below is the implementation of the above approach:
 

C++




// C++ Program to check if n
// is an Zumkelle number
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to store divisors of N
// in a vector
void storeDivisors(int n, vector<int>& div)
{
    // Find all divisors which divides 'num'
    for (int i = 1; i <= sqrt(n); i++) {
  
        // if 'i' is divisor of 'n'
        if (n % i == 0) {
  
            // if both divisors are same
            // then store it once else store
            // both divisors
            if (i == (n / i))
                div.push_back(i);
            else {
                div.push_back(i);
                div.push_back(n / i);
            }
        }
    }
}
  
// Returns true if vector can be partitioned
// in two subsets of equal sum, otherwise false
bool isPartitionPossible(vector<int>& arr)
{
    int n = arr.size();
    int sum = 0;
    int i, j;
  
    // Calculate sum of all elements
    for (i = 0; i < n; i++)
        sum += arr[i];
  
    if (sum % 2 != 0)
        return false;
  
    bool part[sum / 2 + 1][n + 1];
  
    // initialize top row as true
    for (i = 0; i <= n; i++)
        part[0][i] = true;
  
    // initialize leftmost column,
    // except part[0][0], as 0
    for (i = 1; i <= sum / 2; i++)
        part[i][0] = false;
  
    // Fill the partition table
    // in bottom up manner
    for (i = 1; i <= sum / 2; i++) {
        for (j = 1; j <= n; j++) {
            part[i][j] = part[i][j - 1];
            if (i >= arr[j - 1])
                part[i][j] = part[i][j] || part[i - arr[j - 1]][j - 1];
        }
    }
  
    return part[sum / 2][n];
}
  
// Function to check if n
// is an Zumkelle number
bool isZumkelleNum(int N)
{
    // vector to store all
    // proper divisors of N
    vector<int> div;
    storeDivisors(N, div);
    return isPartitionPossible(div);
}
  
// Driver code
int main()
{
    int n = 12;
    if (isZumkelleNum(n))
        cout << "Yes";
    else
        cout << "No";
    return 0;
}


Java




// Java Program to check if n
// is an Zumkelle number
import java.util.*;
class GFG{
   
// Function to store divisors of N
// in a vector
static void storeDivisors(int n, 
                   Vector<Integer> div)
{
    // Find all divisors which divides 'num'
    for (int i = 1; i <= Math.sqrt(n); i++)
    {
   
        // if 'i' is divisor of 'n'
        if (n % i == 0)
        {
   
            // if both divisors are same
            // then store it once else store
            // both divisors
            if (i == (n / i))
                div.add(i);
            else 
            {
                div.add(i);
                div.add(n / i);
            }
        }
    }
}
   
// Returns true if vector can be partitioned
// in two subsets of equal sum, otherwise false
static boolean isPartitionPossible
                (Vector<Integer> arr)
{
    int n = arr.size();
    int sum = 0;
    int i, j;
   
    // Calculate sum of all elements
    for (i = 0; i < n; i++)
        sum += arr.get(i);
   
    if (sum % 2 != 0)
        return false;
   
    boolean [][]part = new boolean[sum / 2 + 1][n + 1];
   
    // initialize top row as true
    for (i = 0; i <= n; i++)
        part[0][i] = true;
   
    // initialize leftmost column,
    // except part[0][0], as 0
    for (i = 1; i <= sum / 2; i++)
        part[i][0] = false;
   
    // Fill the partition table
    // in bottom up manner
    for (i = 1; i <= sum / 2; i++) {
        for (j = 1; j <= n; j++) {
            part[i][j] = part[i][j - 1];
            if (i >= arr.get(j - 1))
                part[i][j] = part[i][j] || 
                               part[i - arr.get(j - 1)][j - 1];
        }
    }
   
    return part[sum / 2][n];
}
   
// Function to check if n
// is an Zumkelle number
static boolean isZumkelleNum(int N)
{
    // vector to store all
    // proper divisors of N
    Vector<Integer> div = new Vector<Integer>();
    storeDivisors(N, div);
    return isPartitionPossible(div);
}
   
// Driver code
public static void main(String[] args)
{
    int n = 12;
    if (isZumkelleNum(n))
        System.out.print("Yes");
    else
        System.out.print("No");
}
}
  
// This code is contributed by Amit Katiyar


Python3




# Python3 implementation
  
# Function to store divisors of N
# in a vector
def storeDivisors(n, div):
  
    # Find all divisors which divides 'num'
    for i in range(1, 1 + int(n ** 0.5)):
  
        # if 'i' is divisor of 'n'
        if (n % i == 0):
    
            # if both divisors are same
            # then store it once else store
            # both divisors
            if (i == int(n / i)):
                div.append(i);
            else:
                div.append(i);
                div.append(int(n / i));
                
# Returns true if vector can be partitioned
# in two subsets of equal sum, otherwise false
def isPartitionPossible( arr):
  
    n =  len(arr);
    sums = 0;
    
    # Calculate sum of all elements
    for i in range(n):
        sums += arr[i]
      
    if (sums % 2 != 0):
        return False;
    
    part = []; 
    for i in range(0, 1 + int(sums / 2)):
        ll = [];
        for j in range(n + 1):
            ll.append(False);
          
        part.append(ll);
        
    # initialize top row as true
    for i in range(1 + n):
        part[0][i] = True;
          
    # initialize leftmost column,
    # except part[0][0], as 0
    for i in range(1, 1 + int(sums / 2)):
        part[i][0] = False;
    
    # Fill the partition table
    # in bottom up manner
    for i in range(1, 1 + int(sums / 2)):
        for j in range(1, 1 + n):
            part[i][j] = part[i][j - 1];
            if (i >= arr[j - 1]):
                part[i][j] = part[i][j] or part[i - arr[j - 1]][j - 1];
          
    return 1; #part[(Math.floor(sum / 2))][n];
  
# Function to check if n
# is an Zumkelle number
def isZumkelleNum(N):
  
    # vector to store all
    # proper divisors of N
    div = [];
    storeDivisors(N, div);
    return isPartitionPossible(div);
  
  
# Driver Code 
# Given Number N
N = 12;
  
# Function Call
if (isZumkelleNum(N)):
    print("Yes");
else:
    print("No");
    
# This code is contributed by phasing17


C#




// C# Program to check if n
// is an Zumkelle number
using System;
using System.Collections.Generic;
  
class GFG{
  
// Function to store divisors of N
// in a vector
static void storeDivisors(int n, 
                  List<int> div)
{
    // Find all divisors which divides 'num'
    for (int i = 1; i <= Math.Sqrt(n); i++)
    {
  
        // if 'i' is divisor of 'n'
        if (n % i == 0)
        {
  
            // if both divisors are same
            // then store it once else store
            // both divisors
            if (i == (n / i))
                div.Add(i);
            else
            {
                div.Add(i);
                div.Add(n / i);
            }
        }
    }
}
  
// Returns true if vector can be partitioned
// in two subsets of equal sum, otherwise false
static bool isPartitionPossible
                 (List<int> arr)
{
    int n = arr.Count;
    int sum = 0;
    int i, j;
  
    // Calculate sum of all elements
    for (i = 0; i < n; i++)
        sum += arr[i];
  
    if (sum % 2 != 0)
        return false;
  
    bool [,]part = new bool[sum / 2 + 1, n + 1];
  
    // initialize top row as true
    for (i = 0; i <= n; i++)
        part[0, i] = true;
  
    // initialize leftmost column,
    // except part[0,0], as 0
    for (i = 1; i <= sum / 2; i++)
        part[i, 0] = false;
  
    // Fill the partition table
    // in bottom up manner
    for (i = 1; i <= sum / 2; i++)
    {
        for (j = 1; j <= n; j++)
        {
            part[i, j] = part[i, j - 1];
            if (i >= arr[j - 1])
                part[i, j] = part[i, j] || 
                             part[i - arr[j - 1], 
                                          j - 1];
        }
    }
  
    return part[sum / 2, n];
}
  
// Function to check if n
// is an Zumkelle number
static bool isZumkelleNum(int N)
{
    // vector to store all
    // proper divisors of N
    List<int> div = new List<int>();
    storeDivisors(N, div);
    return isPartitionPossible(div);
}
  
// Driver code
public static void Main(String[] args)
{
    int n = 12;
    if (isZumkelleNum(n))
        Console.Write("Yes");
    else
        Console.Write("No");
}
}
  
// This code is contributed by 29AjayKumar


Javascript




<script>
// Javascript implementation
  
// Function to store divisors of N
// in a vector
function storeDivisors(n, div)
{
    // Find all divisors which divides 'num'
    for (var i = 1; i <= Math.sqrt(n); i++) {
    
        // if 'i' is divisor of 'n'
        if (n % i == 0) {
    
            // if both divisors are same
            // then store it once else store
            // both divisors
            if (i == Math.floor(n / i))
                div.push(i);
            else {
                div.push(i);
                div.push(Math.floor(n / i));
            }
        }
    }
}
    
// Returns true if vector can be partitioned
// in two subsets of equal sum, otherwise false
function isPartitionPossible( arr)
{
    var n = arr.length;
    var sum = 0;
    var i, j;
    
    // Calculate sum of all elements
    for (i = 0; i < n; i++)
        sum += arr[i];
    
    if (sum % 2 != 0)
        return false;
    
    var part = []; 
    for (i = 0; i <= Math.floor(sum / 2); i++) {
        var ll = [];
        for (j = 0; j <= n; j++) {
            ll.push(false);
        }
        part.push(ll);
    }  
    // initialize top row as true
    for (i = 0; i <= n; i++)
        part[0][i] = true;
          
    // initialize leftmost column,
    // except part[0][0], as 0
    for (i = 1; i <= Math.floor(sum / 2); i++)
        part[i][0] = false;
    
    // Fill the partition table
    // in bottom up manner
    for (i = 1; i <= Math.floor(sum / 2); i++) {
        for (j = 1; j <= n; j++) {
            part[i][j] = part[i][j - 1];
            if (i >= arr[j - 1])
                part[i][j] = part[i][j] || part[i - arr[j - 1]][j - 1];
        }
    }
        
    return 1;//part[(Math.floor(sum / 2))][n];
}
    
// Function to check if n
// is an Zumkelle number
function isZumkelleNum(N)
{
    // vector to store all
    // proper divisors of N
    var div = [];
    storeDivisors(N, div);
    return isPartitionPossible(div);
}
  
// Driver Code 
// Given Number N
var N = 12;
  
// Function Call
if (isZumkelleNum(N))
    document.write("Yes");
else
    document.write("No");
    
// This code is contributed by shubhamsingh10
</script>


Output: 
Yes
 

Time Complexity: O(N * sum)

Reference: OEIS
 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads