Skip to content

Latest commit

 

History

History
33 lines (27 loc) · 602 Bytes

twoTwo.md

File metadata and controls

33 lines (27 loc) · 602 Bytes

twoTwo

Given an array of ints, return true if every 2 that appears in the array is next to another 2.

twoTwo([4, 2, 2, 3]) → true
twoTwo([2, 2, 4]) → true
twoTwo([2, 2, 4, 2]) → false

Solution:

public boolean twoTwo(int[] nums) {
  boolean check = true;
  int lastValue = 0;
  for(int i=0; i<nums.length; i++){
    if(nums[i] == 2){
      if(lastValue != 2){
        check = false;
      }else{
        check = true;
      }
    }
    lastValue = nums[i];
  }
  return check;
}

codingbat

< back to readme