I have an array of numbers nums[] and target such that it satisfies the below condition
{{nums[],target}
1> {{8, 2, 2, 1},12} --> returns true
2> {{8, 2, 2, 1},9} --> returns true
1 condition> identical adjacent values with a subset of remaining numbers sum to target (or)
2 condition> identical adjacent values are not chosen such that subset of other numbers sum to target.
so that in this example
1> 8+2+2 = 12.
2> 8+1=9.
how do i handle the above 2 conditions in Java.
EDITED FOR DANTE:
Expected This Run
groupSumClump(0, {2, 4, 8}, 10) → true true OK
groupSumClump(0, {1, 2, 4, 8, 1}, 14) → true true OK
groupSumClump(0, {2, 4, 4, 8}, 14) → false false OK
groupSumClump(0, {8, 2, 2, 1}, 9) → true false X
groupSumClump(0, {8, 2, 2, 1}, 11) → false false OK
groupSumClump(0, {1}, 1) → true false X
groupSumClump(0, {9}, 1) → false false OK
other tests false X
*Code for Dante:
http://www.ideone.com/xz7ll
@Dante,Please check the above link,it fails for test scenarios mentioned.
I’ve seen you struggling with this question for a long time, so, here are some codes….
EDITed
Now nums_another includes:
the sums of the groups of the adjacent identical numbers (in your case 4 = 2 + 2)
not identical numbers (in your case 8, 1)
0’s at last (thus in all 8 4 1 0)
By the way, the problem with your code is that:
because you set the next identical number to 0 immediately, it will fail for 3 or more,
for example, 8 2 2 2 1 -> 8 4 0 2 1 instead of -> 8 6 0 0 1