Here is the interview question prompt, presented for reference.
We're given a set in the form of an array with a unique integers. Can you write a function that tells us whether it can be separated into two subsets
whose elements have equal sums?
Here's an example: [5, 9, 7, 21]
:
The below would return true
because newSet
can be divided into [5, 9, 7]
and [21]
. Both subsets
sum to 21
and are equal.
function hasEqualSubsets(arr) {
// fill
}
const newSet = [5, 7, 21, 9]
console.log(hasEqualSubsets(newSet)); // true
And in another example, the below returns false
since newSet
cannot be divided into equal subsets.
const newSet = [9, 3, 1]
console.log(hasEqualSubsets(newSet)); // false
1000
-1000
and 1000
O(n^2)
O(n^2)
You can see the full challenge with visuals at this link.
Challenges • Asked almost 7 years ago by Jake from AlgoDaily
This is the main discussion thread generated for Split Set to Equal Subsets.