Java > Array-1 > unlucky1 (CodingBat Solution)

Problem:

We'll say that a 1 immediately followed by a 3 in an array is an "unlucky" 1. Return true if the given array contains an unlucky 1 in the first 2 or last 2 positions in the array.

unlucky1({1, 3, 4, 5}) → true
unlucky1({2, 1, 3, 4, 5}) → true
unlucky1({1, 1, 1}) → false


Solution:

public boolean unlucky1(int[] nums) {
  if (nums.length < 2) {
    return false;
  } else if ((nums[0] == 1 && nums[1] == 3) || (nums[nums.length-2] == 1 && nums[nums.length-1] == 3)) {
      return true;
    } else if (nums.length > 2 && nums[1] == 1 && nums[2] == 3) {
        return true;
    } else
      return false;
    
}


10 comments :

  1. public boolean unlucky1(int[] nums) {
    if (nums.length<2) return false;

    if ((nums[0]==1 && nums[1]==3)||
    (nums[1]==1 && nums[2]==3)||
    (nums[nums.length-2]==1 && nums[nums.length-1]==3))
    return true;

    return false;
    }

    ReplyDelete
  2. public boolean unlucky1(int[] nums) {
    return (nums.length >= 2) &&
    ( nums[0] == 1 && nums[1] == 3
    || nums[1] == 1 && nums[2] == 3
    || nums[nums.length - 2] == 1 && nums[nums.length - 1] == 3);
    }

    ReplyDelete
  3. public boolean unlucky1(int[] nums) {
    if (nums.length <= 1) {
    return false;
    }
    return
    ( nums[0] == 1 && nums[1] == 3
    || nums[1] == 1 && nums[2] == 3
    || nums[nums.length - 2] == 1 && nums[nums.length - 1] == 3);
    }

    ReplyDelete
  4. public boolean unlucky1(int[] nums) {
    for (int i = 0; i < nums.length-1; i++) {
    if (nums.length > 4 && i == 2) i = nums.length-2;
    if (nums[i] == 1 && nums[i+1] == 3) return true;
    }
    return false;
    }

    ReplyDelete
  5. public int[] make2(int[] a, int[] b) {
    if (a.length > 1) return new int[] {a[0], a[1]};
    if (a.length == 1) return new int[] {a[0], b[0]};
    return new int[]{b[0], b[1]};
    }

    ReplyDelete
  6. public boolean unlucky1(int[] arr) {
    if(arr.length>1){
    if(((arr[0]==1&&arr[1]==3)||(arr[1]==1&&arr[2]==3))
    ||((arr[arr.length-2]==1&&arr[arr.length-1]==3)))
    return true;
    return false;}
    return false;
    }

    ReplyDelete
  7. public boolean unlucky1(int[] nums) {

    for(int i=0;i<nums.length-1;i++)
    {
    if(nums[i]==1 && nums[i+1]==3)
    {
    if(nums[0]==1 && nums[1]==1 && nums[2]==1 && nums[3]==3 && nums[4]==1)
    {
    return false;
    }
    return true;
    }

    }

    return false;
    }

    ReplyDelete
  8. public boolean unlucky1(int[] nums) {
    int a=nums.length;
    if(a<2){
    return false;
    }
    else
    if(a>2 && (nums[0]==1&&nums[1]==3) || (nums[1]==1&&nums[2]==3)){
    return true;
    }
    else if(nums[a-2]==1 && nums[a-1]==3){
    return true;
    }
    else return false;
    }

    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