Pages

Thursday, August 6, 2015

Count two 6's or 7's are next to each other

Given an array of integers, return the number of times that two 6's are next to each other in the array. Also count instances where the second "6" is actually a 7.

function({6, 6, 2}) → 1
function({6, 6, 2, 6}) → 1
function({6, 7, 2, 6}) → 1
function({6, 6, 2, 6, 7}) → 2
function({6, 1}) → 0
function({1, 2, 3, 5, 6}) → 0



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
public int function667(int[] nums) {
  int count = 0;
  for (int i=0; i < (nums.length-1); i++) {
    if (nums[i] == 6) {
      if (nums[i+1] == 6 || nums[i+1] == 7) {
        count++;
      }
    }
  }
  return count;
}

No comments:

Post a Comment