开发者

Checking conditions on array elements

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

    int nums_another[] = new int [nums.length];
    int i = 0;
    int j = 0;
    i++;
    int c = 1;
    while (i < nums.length){
        if (nums[i] == nums[i-1]) { // count identical numbers
            c++;
        }
        else { // not identical, store sum of previous identical numbers (possibly only 1 number)
            if (nums[i-1] != 0) {
                nums_another[j] = nums[i-1] * c;
                j++;
            }
            c = 1;
        }
        i++;
    }
    if (nums[i-1] != 0) { // store last
        nums_another [j] = nums[i-1] * c; 
    }

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


You can solve both conditions simultaneously with two local variable: a set of 'lonely' numbers and an accumulator for 'adjacent' values:

Step through the array.

For each value, check the previous value (if there is one) and the next value (if there is one).

If one of them is identical to the current value, increment the 'adjacent' accumulator, otherwise add the value to the 'lonely numbers' set.

To check condition 2, subtract the value of the 'adjacent' accumulator from the target, for condition 1 leave it unchanged.

The rest of the problem is to determine whether some subset of the values in the 'lonely set' sums to the target value. This is a well-known numerical problem which is expensive to compute (exponential effort), but not difficult to program. You can find many solutions if you search for its name: it's called the 'knapsack problem'.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜