Can array be divided into n partitions with equal sums in JavaScript

We are required to write a JavaScript function that takes in an array of numbers, arr, as the first argument and a number, num, as the second argument.

The function should determine whether there exists a way of distributing the elements of the array arr into num groups such that all the groups have equal sum. If there exists any such way, our function should return true, false otherwise.

For example −

If the input array and the number are −

const arr = [4, 6, 3, 3, 7, 4, 1];
const num = 4;

Then the output should be −

const output = true;

because the four groups are: [7], [1, 6], [4, 3], [4, 3]

Algorithm Overview

The solution uses backtracking to explore all possible partitions. First, we check if partitioning is mathematically possible, then recursively build each subset with the target sum.

Example

The code for this will be −

const arr = [4, 6, 3, 3, 7, 4, 1];
const num = 4;

const canDivide = (arr = [], num = 1) => {
    const sum = arr.reduce((acc, val) => acc + val);
    
    // Check if division is possible
    if (sum % num !== 0 || arr.some(val => val > sum / num)) {
        return false;
    }
    
    const used = new Set();
    const targetSum = sum / num;
    
    return (function find(start, target) {
        // All numbers used successfully
        if (used.size === arr.length) {
            return true;
        }
        
        // Current subset sum exceeded target
        if (target < 0) {
            return false;
        }
        
        // Found one complete subset, start next one
        if (target === 0) {
            return find(0, targetSum);
        }
        
        // Try adding each unused number to current subset
        for (let i = start; i < arr.length; i++) {
            if (!used.has(i)) {
                used.add(i);
                if (find(i + 1, target - arr[i])) {
                    return true;
                }
                used.delete(i); // Backtrack
            }
        }
        
        return false;
    })(0, targetSum);
};

console.log(canDivide(arr, num));

Output

true

How the Algorithm Works

The solution follows these key steps:

  • Step 1: Check feasibility - If total sum cannot be divided by num or any number is greater than sum/num, return false.

  • Step 2: Use a HashSet to track used numbers during backtracking.

  • Step 3: Recursively build subsets with target sum (total sum / num).

  • Step 4: When a subset is complete (target = 0), start building the next subset.

  • Step 5: Backtrack when a path doesn't lead to a solution.

Time Complexity

The time complexity is O(2^n) in the worst case, where n is the array length, as we may need to explore all possible combinations. The space complexity is O(n) for the recursion stack and the used set.

Conclusion

This backtracking solution efficiently determines if an array can be partitioned into equal-sum groups. The algorithm first validates feasibility, then uses recursive exploration with backtracking to find valid partitions.

Updated on: 2026-03-15T23:19:00+05:30

291 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements