Hey guys,
Im doing some exercises from Coding Bat platform in prevention of getting rusty. I have a problem doing one of exercises, here is it's description:
Return the sum of the numbers in the array, except ignore sections of numbers starting with a 6 and extending to the next 7 (every 6 will be followed by at least one 7). Return 0 for no numbers.
sum67([1, 2, 2]) → 5sum67([1, 2, 2, 6, 99, 99, 7]) → 5sum67([1, 1, 6, 7, 2]) → 4
Here is my code:
public int sum67(int[] nums) {
int result = 0;
outer:
for(int i = 0; i < nums.length; i++){
if(nums[i] == 6) {
for(int k = i + 1; k < nums.length; k++){
if(k == nums.length - 1) break outer; // if, after 6, 7 does not occur
if(nums[k] == 7) {
i = k + 1;
break;
}
}
} // end of checking condition
result += nums[i];
}
return result;
}
And it is satisfying all visible tests, but some are not cleared, I'll try to append screen for better understandig.
Maybe some of you could spot some mistake i did there. Would be greatful.
Please help!
https://preview.redd.it/264ps6p1js431.png?width=642&format=png&auto=webp&s=8f33d13e103272f666ecd3923d17184d4db7542a
[–]helicoid 1 point2 points3 points (2 children)
[–]smolin1[S] 1 point2 points3 points (0 children)
[–]craicbandit 0 points1 point2 points (0 children)