Splitting array of numbers into two arrays with same average in JavaScript

We are required to write a JavaScript function that takes in an array of integers as the first and the only argument.

The function needs to determine whether there exists a combination of elements of the input array that when they are divided into two groups (may/may not have equal elements), the average of both the groups is just the same. If there exists any such condition the function should return true, false otherwise.

For example −

If the input array is −

const arr = [6, 3, 2, 8, 1, 5, 7, 4];

Then the output should be −

const output = true;

because the combination is [8, 1, 5, 4] and [6, 3, 2, 7] as both the groups have an average of 4.5

How It Works

The algorithm uses dynamic programming to check if we can partition the array into two subsets with equal averages. The key insight is that if two groups have the same average, then:

sum1 / count1 = sum2 / count2

This means: sum1 × count2 = sum2 × count1

Example

Following is the code −

const arr = [6, 3, 2, 8, 1, 5, 7, 4];

const canHaveEqualAveragePartition = (arr = []) => {
    const sum = arr.reduce((acc, val) => acc + val);
    const array = Array(sum+1).fill(false).map(() =>
    Array(arr.length+1).fill(false));
    array[0][0] = true;
    
    for(let i=0; i < arr.length; ++i){
        for(let j=sum - arr[i];j>=0;--j){
            for(let k=arr.length-2;k>=0;--k){
                if(array[j][k]){
                    array[j + arr[i]][k+1] = true;
                    if((j + arr[i]) * (arr.length - k - 1) == (sum - j -arr[i]) * (k + 1)){
                        return true;
                    }
                }
            }
        }
    }
    return false;
};

console.log(canHaveEqualAveragePartition(arr));

Output

true

Simpler Approach Using Brute Force

For better understanding, here's a simpler brute force approach that checks all possible combinations:

const arr = [6, 3, 2, 8, 1, 5, 7, 4];

const canPartitionEqualAverage = (nums) => {
    const totalSum = nums.reduce((sum, num) => sum + num, 0);
    const n = nums.length;
    
    // Try all possible subset sizes from 1 to n-1
    for (let size = 1; size < n; size++) {
        // If total average equals subset average, check if such subset exists
        if ((totalSum * size) % n === 0) {
            const targetSum = (totalSum * size) / n;
            if (canFormSubset(nums, 0, size, targetSum)) {
                return true;
            }
        }
    }
    return false;
};

const canFormSubset = (nums, index, size, targetSum) => {
    if (size === 0) return targetSum === 0;
    if (index >= nums.length || targetSum < 0) return false;
    
    // Include current number or exclude it
    return canFormSubset(nums, index + 1, size - 1, targetSum - nums[index]) ||
           canFormSubset(nums, index + 1, size, targetSum);
};

console.log(canPartitionEqualAverage(arr));

Output

true

Key Points

  • The problem requires partitioning an array into two groups with equal averages
  • Dynamic programming approach is more efficient for larger arrays
  • The condition for equal averages: sum1 × count2 = sum2 × count1
  • Brute force approach is easier to understand but less efficient

Conclusion

Both approaches solve the equal average partition problem. The dynamic programming solution is more efficient, while the brute force method is more intuitive for understanding the core logic.

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

286 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements