Expected Run
sum13([1, 2, 2, 1]) → 6 6 OK
sum13([1, 1]) → 2 2 OK
sum13([1, 2, 2, 1, 13]) → 6 6 OK
sum13([1, 2, 13, 2, 1, 13]) → 4 6 X
sum13([13, 1, 2, 13, 2, 1, 13]) → 3 6 X
sum13([]) → 0 0 OK
sum13([13]) → 0 0 OK
sum13([13, 13]) → 0 0 OK
sum13([13, 0, 13]) → 0 0 OK
sum13([13, 1, 13]) → 0 1 X //why expected 0 i thought this is expected 1??
sum13([5, 7, 2]) → 14 14 OK
sum13([5, 13, 2]) → 5 7 X
sum13([0]) → 0 0 OK
sum13([13, 0]) → 0 0 OK
other tests
X
Correct for more than half the tests

How to improve my design, approach, code? please advise

I think it means "come immediately after a 13 [in the array]." So if there's a 13 in the array, the next element doesn't count. And there's no apparent reason to test for 12 at all.

This is a stupid question in any case. What does "and numbers that come immediately after a 13 also do not count." mean ?? IF the array contains the number 13, and it is not the last member of the array, then does the question mean that only the next array index number should not count, or all the remaining numbers in all the remaining index positions ??

Like so many of these Codingbat questions, this is another mess.

Return the sum of the numbers in the array, returning 0 for an empty array. Except the number 13 is very unlucky, so it does not count and numbers that come immediately after a 13 also do not count.

A corollary to this exercise would be to sum all of the integers in the array, identify any that are 13 and any that follow 13 in the array and subtract them.

nums = [13, 1, 2, 13, 2, 1, 13] ==> sum = 45
identify 13 plus following
[13, 1, 2, 13, 2, 1, 13] ==bold numbers add up to 42
so sum13[13, 1, 2, 13, 2, 1, 13] returns 3 (45 - 42)

I agree with Krakatoa that the description is not clear and subject to interpretation. But since they give several cases along with the expected result, you have to make a hypothesis about interpreting the wording of the description. After a couple of guesses, you come to the conclusion that the challenge meant to ignore the first number following 13 if there is a number.

In the job, you often get crappy description of what has to be done, and then I have to write up several guesses as to what the customer meant. Then they sometimes realize that what they wrote is crap and they revise it until it becomes clear.

public int sum13(int[] nums) { int sum=0; int sumTotal=0; int len=nums.length; int sumNon13=0; int sumNextTo13=0; int sumAll=0; int sumJunk=0; int sumActual=0;int result=0; for(int i=0;i<len-1;i++){ if(nums[i]==13){ sumTotal=sumTotal+nums[i]; sumNon13=sumNon13+nums[i]; sumNextTo13=sumNextTo13+nums[i+1]; sumJunk=sumNon13+sumNextTo13; sumActual=sumTotal-sumJunk; result=sumActual; } else { sumAll=sumAll+nums[i]; result=sumAll; //return sumAll; } } return result;}

You might be trying to do a little too much at once here, with all the adding and subtracting going on. Instead of running your code through all of the tests, pick one test and get it to pass.

First, since you're failing the tests that don't even have any 13's, you know that logic can't be right. Focus there, first, and get code working that successfully sums up the elements in an array. Then, add some code that skips elements that equal 13. That will let you pass tests where the 13 comes at the end of the array. Finally, add some more code that skips elements if they follow a 13.

When you get to that point, think about how best to skip the next element, when you see a 13. You could have a boolean whose value you change when you see a 13. You could also increment your index by an extra step when you see a 13.

But the key is to break the problem down and focus on one thing at a time.

I agree with Krakatoa that the description is not clear and subject to interpretation

the confusion is in the fact that for me that the word 'number' is in the plural - numbers. It is mitigated by the word 'immediately' I grant, but numberS plural implies that it could apply to everything after the 13 - ie the rest of the array. Because anything in the very index position after a 13 is a NUMBER, not a NUMBERS.

Meticulousness in describing objectives is not always at its best in Codingbat.

public static int sum13(int[] nums) { int sum = 0; for (int i = 0; i < nums.length; i++) { if (nums[i] != 13) sum += nums[i]; else if (nums[i] == 13 && i < nums.length - 1) { nums[i] = 0; nums[i + 1] = 0; } } return sum; }

That does pass all tests, but, in an actual project, other developers you work with might get mad at your code for modifying the array that's passed in.

A corollary to this exercise would be to sum all of the integers in the array, identify any that are 13 and any that follow 13 in the array and subtract them.

nums = [13, 1, 2, 13, 2, 1, 13] ==> sum = 45
identify 13 plus following
[13, 1, 2, 13, 2, 1, 13] ==bold numbers add up to 42
so sum13[13, 1, 2, 13, 2, 1, 13] returns 3 (45 - 42)

i tried above approach passed almost all tests except one

public int sum13(int[] nums) {/* * int sum = 0; for (int i = 0; i < nums.length; * i++) { if (nums[i] != 13) sum += nums[i]; else if * (nums[i] == 13 && i < nums.length - 1) { nums[i] * = 0; nums[i + 1] = 0; } } return sum; */ int sum = 0; int sumBold = 0; for (int i = 0; i < nums.length; i++) { /* * if (i<nums.length){ sum += nums[i]; System.out.println( * "sum is-->"+sum); } else */ sum += nums[i]; // System.out.println("sum is-->" + sum); if (nums[i] == 13) if (i < nums.length-1) { sumBold += nums[i] + nums[i + 1]; // System.out.println("sumBold is-->" + sumBold); } else { sumBold += nums[i]; } } return (sum - sumBold); }

That does pass all tests, but, in an actual project, other developers you work with might get mad at your code for modifying the array that's passed in.

I got you. How to fix above test. I am debugging in eclipse now to see why sum is coming only 13 instead of 26 and sumBold coming 26 hence difference is -13 instead of 0?
some edge case issue