MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Solved

Posted on 2016-09-27

Hi,

I am working on below challenge

http://codingbat.com/prob/p185204

I was not clear on how many elements each group should have? please advise

I am working on below challenge

http://codingbat.com/prob/p185204

splitArray

prev | next | chance

Given an array of ints, is it possible to divide the ints into two groups, so that the sums of the two groups are the same. Every int must be in one group or the other. Write a recursive helper method that takes whatever arguments you like, and make the initial call to your recursive helper from splitArray(). (No loops needed.)

splitArray([2, 2]) → true

splitArray([2, 3]) → false

splitArray([5, 2, 3]) → true

I was not clear on how many elements each group should have? please advise

6 Comments

You need to take as many as you need for each group BUT you must use all given ints.

Beyond that I'd be happy to comment on pseudo code of java once you get started on this, there area number of ways to attack it. You might get some inspiration from the following:

https://en.wikipedia.org/wiki/Partition_problem

As Bill pointed out, you know that if there is a solution then S = sum(all the integers) must be even. Now that you have established that, Let T = S/2. Now, if you can find a subset of the integers whose sum is T, then you are done, since the other integers not in your chosen subset must also have a sum equal to T (since the sum of both subsets must be 2*T, which is equal to S.

Taking one of your examples..

splitArray([5, 2, 3]) → true

S = 10 --> T = 10/2 = 5.

Since 5 is an element in the set of integers, it must be that the remaining elements not in the subset of {5} must sum up to 5. Check: {2,3} --> Sum is also 5 (no surprise). This is one of the simpler examples in determining the solution, since, in general, you cannot expect that the number T to appear in the set of integers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Title | # Comments | Views | Activity |
---|---|---|---|

Understanding factorial | 4 | 31 | |

Java array sort | 10 | 30 | |

Java Inharitance subclass/child extends, reference variable from base/parent class | 9 | 30 | |

How to fix socket closed error | 11 | 25 |

The viewer will learn how to implement Singleton Design Pattern in Java.

This video teaches viewers about errors in exception handling.

Join the community of 500,000 technology professionals and ask your questions.