Friday, May 30, 2014

CodingBat > firstLast6

Given an array of ints, return true if 6 appears as either the first or last element in the array. The array will be length 1 or more.

firstLast6({1, 2, 6}) → true
firstLast6({6, 1, 2, 3}) → true
firstLast6({13, 6, 1, 2, 3}) → false

Solution ::
public class FirstLast6 {
 public static void main(String[] args) {
  FirstLast6 fl = new FirstLast6();
  int[] testArray = new int[] { 1, 2, 6 };
  System.out.print(fl.firstLast6(testArray));
 }

 public boolean firstLast6(int[] nums) {
  if (nums[0] == 6 || nums[nums.length - 1] == 6)
   return true;
  return false;
 }
}

2 comments: