Examples:
Problem 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.
I thought I'd post this coding problem I found interesting to solved.
This problem is taken from CodingBat under Array2 section. Although,
this may not be the best solution, I learned something that
I have never used before, a while nested inside a loop.
public int sum67(int[] nums) { int count = 0; for (int i = 0; i < nums.length; i++) { // this applies to a 67 pattern only // if a number that is 6 is succeeded // by 7 immediately then run this if // otherwise run the else statement if (nums[i] == 6 && nums[i + 1] == 7) { nums[i] = 0; nums[i + 1] = 0; } else if (nums[i] == 6) { // only works when the sequence starts // with 6, a number that starts with // 7 is left untouched // while the next number is not 7 // move to the next element // initializing that element to // zero, repeat this until // 7 is found // [1, 6, 4, 3, 3, 7, 2] // [1, 0, 0, 0, 0, 7, 2] while (nums[i + 1] != 7) { nums[i] = 0; nums[i + 1] = 0; i++; } // while loop runs until the condition is met // no need to add - 1 in the for loop // [1, 0, 0, 0, 0, 7, 2] // the array is left with 7 // to initialize to 0 while (nums[i + 1] == 7) { nums[i + 1] = 0; } } count += nums[i]; } return count; }