• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.>
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Deepak Lal
Ranch Hand
Posts: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 5
VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is recursive solution for the given problem of summing ints with 5's multiples required and 1's excluded if after 5's multiples.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch and thank you for that solution.
 
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The question says you need to decide if any [i]subset[i] of the applicable numbers sum to the target value.

It is not sufficient to sum the numbers in sequence until the sum is greater than or equal to the target value because this will not check all of the subsets. You need to:

* Find the set of numbers that match the rules.
* Create a list of all non-empty sub-sets of those numbers.
* Sum each subset until you find one that sums to the target value.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic