0

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.

deepakl.2000
  • 176
  • 1
  • 4
  • 19
  • please post the (real) code you have so far an pinpoint the areas where you're having difficulty. – Mat May 19 '11 at 09:54
  • (2) should return false according to the second condition. The first condition also doesn't make sense to me, why exactly are you adding `8` to the sum and not `1`? – wds May 19 '11 at 09:56
  • what if you have {{8,2,2,1,2,2,2,1},12}? how should we handle multiple groups of identical adjacent numbers? – MarcoS May 19 '11 at 09:56
  • @wds - 1) If I understand correctly, taking `2+2` you only need 8 to make the target 12. 2) You're not allowed to use `2 2`, because adjacent and identical. To make the target 9, you need both 8 and 1. – Ishtar May 19 '11 at 10:16
  • @Ishtar I get 1 now (you have to find the remaining numbers that sum to target), but 2) says that the remaining numbers should _not_ sum to target, so shouldn't this return false for example two then? – wds May 19 '11 at 11:33
  • @wds: `{{8, 2, 2, 1},9} --> returns true` is correct becoz adjacent pairs `2` are ignored and hence the subset of sum of remaining numbers which is `8+1=9` is true and is correct – deepakl.2000 May 19 '11 at 12:13
  • Too late in my place.... Maybe tomorrow.... – Dante May Code Jun 25 '11 at 16:22
  • `if (cand == 1 && k > 0) { final int prev = nums_another[k - 1];` What is this line for? `prev` is so local that it has no use. – Dante May Code Jun 26 '11 at 12:29
  • @Dante,so should i remove these lines of statements.??Please advice?im stuck up with this.could you suggest the code fix for this please. – deepakl.2000 Jun 26 '11 at 13:43
  • What's your intended use for that line? – Dante May Code Jun 27 '11 at 00:01
  • Another thing is: `List fixed` is always empty. What is that used for? – Dante May Code Jun 27 '11 at 00:48

2 Answers2

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'.

Kilian Foth
  • 13,904
  • 5
  • 39
  • 57
  • @Kilian.Could you provide me with the code fix for the same,Im struggling to solve this problem for a week now.will be obliged and thankful if you can provide the code snippet – deepakl.2000 May 19 '11 at 10:32
  • A method for the first part: `boolean cond(int[] a, int which, int target) { int paired = 0; List unpaired = new LinkedList(); for(int i = 0; i < a.length; i++) { int current = a[i]; if(i > 0 && a[i-1] == current || i < a.length-1 && a[i+1] == current) { paired += current; } else { unpaired.add(current); } } if(2 == which) { target -= paired; } return knapsack(target, unpaired); }` knapsack() requires recursion. Try it, and ask another question if you get stuck! – Kilian Foth May 19 '11 at 15:06
1

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

Dante May Code
  • 11,177
  • 9
  • 49
  • 81
  • @Dante,The code is written at http://www.ideone.com/Cdgs9 and im not getting the summation as you have explained.could you please elaborate ??? – deepakl.2000 May 19 '11 at 12:03
  • i think its outputting wrong value for everything it is equal to 1,check http://www.ideone.com/q4mmf – deepakl.2000 May 19 '11 at 12:44
  • @user756993, there is no `for` in your output code. By the way, the answer is edited a little for unusual cases. – Dante May Code May 19 '11 at 12:46
  • @Diante,it works for Original Questions Case 1) returning {{8,4,1,0},12},but for Original Question Case 2) its also returning {{8,4,1,0} ,actually it should return {8,1,0,0} which is not happening.Could you check whats wrong with this.?.Thanks for your immediate help.Appreciated and highly obliged to you.Code is at http://www.ideone.com/PTuH3 – deepakl.2000 May 19 '11 at 13:01
  • @user756993, this part only simplifies the array. Further operations are needed for the answer.... – Dante May Code May 19 '11 at 13:03
  • can u check Case 2 which is also returning {8,4,1,0} but it should return {8,1,0,0},becoz adjacent pairs `2` will not be considered and should be replaced by zero which is not happening in your code.if you can simplify the adjacent pairs such that they are replaced with zero.it would be easy for me to pass this array to do the remaining operations(such as finding that sums equals to target) – deepakl.2000 May 19 '11 at 13:10
  • @user756993, because this code only **simplifies**. Now you can use your algorithm in [the other question](http://stackoverflow.com/questions/6042679/java-grouping-algorithm). Because you have got an array of independent numbers (and there is no need to check for adjacency), you can just use the combination algorithm in the other question. – Dante May Code May 19 '11 at 13:16
  • @Dante,can u plz send me the modified algorithm to my email address deepakl.2000@gmail.com.I have been struggling to get it rite for a long time probably weeks now.could you consolidate and send it to me.Plzzzzz I need this algorithm..moreover i dont know how to handle Case 2 when 4 should not be selected as well.plzz help me this time – deepakl.2000 May 19 '11 at 13:23
  • @Dante,will you send me an email.? – deepakl.2000 May 19 '11 at 13:51
  • @user756993, see [the other question](http://stackoverflow.com/questions/6042679/java-grouping-algorithm) for the full answer, which is more suitable there. – Dante May Code May 19 '11 at 13:53
  • Thanks @Dante,In case of any clarifications,will come back to you. – deepakl.2000 May 19 '11 at 14:37
  • @Dante i could not see your answer in [the other question](http://stackoverflow.com/questions/6042679/java-grouping-algorithm),Could you please re-post it – deepakl.2000 Jun 25 '11 at 14:43
  • @user756993 there they are now. – Dante May Code Jun 25 '11 at 14:57
  • @Dante,i have written the complete code with your logic and used my logic as well,its failing for few test scenarios.i have edited and posted it here,Culd you look whats wrong – deepakl.2000 Jun 25 '11 at 15:53
  • @Dante,Posted the complete code in original question above.(edited for Dante) – deepakl.2000 Jun 25 '11 at 15:58
  • @Dante,did u get time to check what is wrong in the code.[checking conditions for array elements](http://www.ideone.com/xz7ll) – deepakl.2000 Jun 26 '11 at 05:39
  • ,i have couple of other problems.hope you could help me – deepakl.2000 Jul 18 '11 at 15:47