'CodingBat-Excercise: Return true if the given array contains an unlucky 1 in the first 2 or last 2 positions in the array

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.

The excercise can be found here:

http://codingbat.com/prob/p197308

My own approach works like this:

public boolean unlucky1(int[] nums) {

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

This is true for every array except for [1, 1, 1, 3, 1]. Now I understand why it doesn't work for this array, but why does it work for [2, 1, 3, 4, 5] then? This array doesn't have a 1 followed by a 3 in the first two or last positions either. Am I getting this excercise wrong?



Solution 1:[1]

It doesn't look like you are following the rules. You don't need to iterate over the entire array. You should only test the first two and last two elements.

EDIT :

Reading the exercise again, it seems you should return true even if the 1 followed by 3 are in indices 1 and 2 of the array (since the requirement is that the unlucky 1 is either in the first two or last two positions - the 3 doesn't have to be in the first two positions).

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

Solution 2:[2]

For example :

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

Solution 3:[3]

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


 }
 return false;
}

Solution 4:[4]

I'm trying to solve those CodingBat problems with fewer code lines as possible. Let me share my solution for this problems using the ternary operator.

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

Solution 5:[5]

This code works fine:-

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

Solution 6:[6]

My solution passes all the tests:

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 AlexSan
Solution 3 Rares
Solution 4 Jailton Alkimin Louzada
Solution 5 ritesh_ratn
Solution 6 Eugene Khlebnikov