Java > Warmup-2 > array667 (CodingBat Solution)

Problem:

Given an array of ints, 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.

array667({6, 6, 2}) → 1
array667({6, 6, 2, 6}) → 1
array667({6, 7, 2, 6}) → 1


Solution:

public int array667(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;
}


1 comment :

  1. With Java Stream

    public static int array667(int[] nums) {
    long count = IntStream.range(0, nums.length - 1)
    .filter(i -> nums[i] == 6 && nums[i + 1] == 6 || nums[i + 1] == 7)
    .count();
    return (int)count;
    }

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact