
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
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]
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, num) => acc + num); if (sum % num !== 0 || arr.some(num => num > sum / num)) { return false; } const used = new Set(); return (function find(start, target) { if (used.size === arr.length) { return true; } if (target < 0) { return false; } if (target === 0) { return find(0, sum / num); } 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); } } return false; })(0, sum / num); }; console.log(canDivide(arr,num));
The steps we took in our solution are −
Step 1. If total sum cannot be divided by num or one of the number is greater than sum/num, we return false.
Step 2.We used a HashSet to track used numbers.
Step 3. We started finding the sub partitions.
If all the numbers are used, we are done.
If the subset sum was too large, we stopped searching.
If we have found one subset, we continued the search till we use all the numbers.
And lastly, we tried every unused number.
Output
And the output in the console will be −
true
- Related Articles
- Find the sums for which an array can be divided into subarrays of equal sum in Python
- First N natural can be divided into two sets with given difference and co-prime sums in C++
- Array thirds with equal sums in JavaScript
- Find if array can be divided into two subarrays of equal sum in C++
- Check if an array of 1s and 2s can be divided into 2 parts with equal sum in Python
- Subarray pairs with equal sums in JavaScript
- Check if any square (with one colored cell) can be divided into two equal parts in Python
- Can parasites be divided into partial and complete parasites?
- Check if a large number can be divided into two or more segments of equal sum in C++
- Check if an array can be divided into pairs whose sum is divisible by k in Python
- Divide a string into n equal parts - JavaScript
- Prefix sums (Creating an array with increasing sum) with Recursion in JavaScript
- Check if array can be divided into two sub-arrays such that their absolute difference is Ks in Python
- Program to check whether we can partition a list with k-partitions of equal sum in C++
- Array index to balance sums in JavaScript
