Thursday, August 6, 2015

No triples interger in a row

Given an array of integers, we'll say that a triple is a value appearing 3 times in a row in the array. Return true if the array does not contain any triples.

function({1, 1, 2, 2, 1}) → true
function({1, 1, 2, 2, 2, 1}) → false
function({1, 1, 1, 2, 2, 2, 1}) → false



1
2
3
4
5
6
7
8
public boolean noTriples(int[] nums) {
  for(int x=0;x<nums.length-2;x++){
      if(nums[x]==nums[x+1] && nums[x]==nums[x+2])
        return false;
  }
  
  return true;
}

No comments:

Post a Comment