File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Summation of `multiples of 5` which sums to a given target Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Summation of `multiples of 5` which sums to a given target" Watch "Summation of `multiples of 5` which sums to a given target" New topic
Author

Summation of `multiples of 5` which sums to a given target

Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

Write a java function such that given an array of ints, is it possible to choose a group of some of the ints, such that the group sums to the given target with these additional constraints: all multiples of 5 in the array must be included in the group. If the value immediately following a multiple of 5 is 1, it must not be chosen.

groupSum5(0, {2, 5, 10, 4}, 19) => true
groupSum5(0, {2, 5, 10, 4}, 17) => true
groupSum5(0, {2, 5, 10, 4}, 12) => false

I have written the partial code as below but is not working properly.could you check what might be wrong.




Im unable to proceed further.Code fix provided will be appreciated.>


When The Going Gets Tougher,The Tougher gets Going
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10040
    
    6

What exactly do you mean by "Not working properly"? Please tell us the details. What have you tried that works, what have you tried that doesn't work, etc?


Never ascribe to malice that which can be adequately explained by stupidity.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3011
    
    9
Two problems that I can see

1. At line 8 and 12 you return true if the target has been matched, yet this doesn't allow for numbers later in the list being multiples of 5. You need to get the sum of all your multiples of 5 before you even begin looking at the other numbers.

2. Line 13 will only skip the value 5, not all multiples of 5.

Fix those and then if you still have problems, see Fred's post


Joanne
Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

I have refined my code as below



All the test cases are still failing.could you help me out to fix this piece of code.>
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3011
    
    9
You still haven't done anything about the first problem I mentioned. You need a separate loop that sums all the values that are multiples of 5. If this sum is greater than the target value you can then immediately return false without checking the other numbers. If not you then use it as a base value when checking the other numbers.

What are you doing on line 7 ? You only increase the value of start if its current value is a multiple of 5.

Lines 10 and 11 can be replaced by
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Summation of `multiples of 5` which sums to a given target
 
Similar Threads
easier way to tell whether or not an object is an int or char...
algorithm in java
More assignments
Backtracking recursion problems.
Homework Assignment