0

Somehow I managed to find one certain value with the below code but how can I search if an array has value 1 and the right next value is 3

unlucky1([1, 3, 4, 5]) → true
unlucky1([2, 1, 3, 4, 5]) → true
unlucky1([1, 1, 1]) → false

public boolean unlucky1(int[] nums) {
  //int[] a = new int[] {1,3};

  for(int i: nums)
  {
    if (i == 1 )

    return true;

  }
    return false;


}
gs650x
  • 383
  • 2
  • 22
  • 1
    This Question has already been answered here [How can I test if an array contains a certain value?](https://stackoverflow.com/questions/1128723/how-can-i-test-if-an-array-contains-a-certain-value) – Mustajeeb ur Rehman Apr 13 '18 at 04:48
  • The above question is for one certain value and I am asking for the 2 certain values together like whether 1,3 in the array are together – gs650x Apr 13 '18 at 05:04

4 Answers4

1

The for-each loop hides the iterator (or index here), so you can't get the current value and check the next value at the same time. Use a standard loop. Also, since you don't access any field's - you could make the method static. Like,

public static 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;
}

And, in Java 8+, you could express that with an IntStream and anyMatch like

public static boolean unlucky1(int[] nums) {
    return IntStream.range(0, nums.length - 1)
            .anyMatch(i -> nums[i] == 1 && nums[i + 1] == 3);
}

Note the nums.length - 1 in both cases, that is to ensure the nums[i + 1] doesn't exceed the length of the nums array on the conditional check.

Updated because you did not add the next condition - which was

in the first 2 or last 2 positions in the array.

That checker is strict. Follow the instructions carefully. I get (working)

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

Which you could also implement without loops at all, like

public boolean unlucky1(int[] nums) {
    int len = (nums != null) ? nums.length : 0;
    if (len < 2) {
        return false;
    }
    if ((nums[0] == 1 && nums[1] == 3) || (nums[1] == 1 && nums[2] == 3)) {
        return true;
    }
    return len > 3 && ((nums[len - 4] == 1 && nums[len - 3] == 3) //
            || (nums[len - 2] == 1 && nums[len - 1] == 3));
}

I swapped the ternary order, but that's not important.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Thank you so much for responding but your above code is throwing ArrayIndexOutOfBoundsException: – gs650x Apr 13 '18 at 05:00
  • @Gurjit With what input? Make sure you have `nums.length - 1`. The `- 1` is important. – Elliott Frisch Apr 13 '18 at 05:08
  • I am using the below web site to practice the core concepts it throws random input and show the results http://codingbat.com/prob/p197308 – gs650x Apr 13 '18 at 05:13
1

You can use a state machine:

boolean wasOne = false;
for (int i: nums) {
   if (i == 3 && wasOne) {
       return true;
   }
   wasOne = i == 1;
}
return false;
Maurice Perry
  • 9,261
  • 2
  • 12
  • 24
0

try this updated code.

arroding to your question

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

OR

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

  if ((nums[0] == 1 && nums[1]==3)||(len>2 && nums[1] == 1 && nums[2]==3)){
       return true; 
   }

  if (nums[len-2] == 1 && nums[len-1]==3){
     return true; 
  }

    return false;
}
Ravi Sevta
  • 2,817
  • 21
  • 37
0

Try with basic for loop:

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