2

I’m a senior in high school taking a computer science class. For homework, we have to create solutions to certain CodingBat (practice coding website) problems. I am experiencing problems with this question, some of which include OutOfBounds for the array. Based on my code, I can’t quite figure out why this is happening. The following attached code (below) is what I have created as a solution to the CodingBat problem for unlucky1 in Array-1 (java), which describes the challenge as: “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 a in the first 2 or last 2 positions in the array.

public boolean unlucky1(int[] nums) {
  int i = 0;
  for(i = 0; i < nums.length; i++)
    if(nums[i-1] == 1 && nums[i] == 3)
    {
      return true;
    }
    return false;
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
Jacob
  • 23
  • 3
  • 1
    Please do not post pictures of code, instead edit your question with the _text_ of the code in a code formatted block. – Nexevis Jan 05 '23 at 19:08
  • I apologize, this is my first time on the website. Thank you for letting me know. – Jacob Jan 05 '23 at 19:09
  • @Jacob: then please [edit] your question to fix the issue. Also, if you get an exception, it's a good idea to post the complete stack trace of the exception (again, as text). And realistically you probably find everything you need in [this question](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it). – Joachim Sauer Jan 05 '23 at 19:10
  • Sounds like you could use [Boyer-Moore](https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_string-search_algorithm) to efficiently solve this. – Neil Jan 05 '23 at 19:11
  • 2
    @Neil Seems like overkill since he only needs to check first 2 and last 2 of the array? He doesn't even need a loop – Nexevis Jan 05 '23 at 19:12
  • @Nexevis I have changed the photo to text. I’m on my phone at the moment, sorry for the slight delay. – Jacob Jan 05 '23 at 19:19
  • Oh, yes, I guess another thing is to read the directions carefully. :] Much better with your edit -- thanks. What happens if the number of items is small? (0, 1, 2, 3?) – Neil Jan 05 '23 at 19:19
  • @Neil In regards to the Boyer-Moore, I’m sure we will learn about this in class, but because it’s the second day of learning arrays, he has stated not to use any “more advanced” tools in our code. – Jacob Jan 05 '23 at 19:21
  • just to answer why the out of bounds you start i = 0 and then you do on the if i - 1 on the first iteration of the loop which would give you a -1 index which is out of bounds – Anon Jan 05 '23 at 19:23
  • @Anon Yes, but if I were to do i and i+1 instead of i-1 and i, it would do the same thing, just becoming one more than the bounds of the array. – Jacob Jan 05 '23 at 19:26
  • @Jacob This is why the answer by Mureinik has conditions that check that length of the array is at least 2, because his `nums[1]` would fail with the same error if the length is 1, so he has a check that short circuits the operation before the comparison. You need to define conditions for edge cases, and allow your code to operate within those conditions – Nexevis Jan 05 '23 at 19:30

2 Answers2

1

The problem statement is "Return true if the given array contains an unlucky a in the first 2 or last 2 positions in the array.", so you don't even need a loop - you just need to examine the first two and last two elements of the array:

public boolean unlucky1(int[] nums) {
    return nums != null &&
           nums.length >= 2 &&
           (nums[0] == 1 && nums[1] == 3 ||
            nums[nums.length - 2] == 1 && nums[nums.length -1] == 3);
}
Nexevis
  • 4,647
  • 3
  • 13
  • 22
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • Thanks, I thought I may have been doing a slight overkill with the loop. It was a bit unsure about what they had meant there. – Jacob Jan 05 '23 at 19:23
  • 1
    @Jacob I recommend still attempting to understand what was failing with your code, as it will be a common problem in the future with loops. Think about how your code starts at position 0, and you immediately try access position `0 - 1` with `nums[i-1]` which would be position `-1`, which obviously will error since that is not possible. – Nexevis Jan 05 '23 at 19:24
  • @Nexevis Will do, I was thinking about a way to access the two positions in the way, which I haven’t quite figured out how to do. It seems if I did it the other way (nums[i] && nums[i+1]), it would do the same thing, but over the boundaries instead. – Jacob Jan 05 '23 at 19:29
-1

The following code is the correct method.

0.public static boolean unlucky1(int[] nums){  //Firstly,declare the method 
                                               "static"
    1.  int length = nums.length;//Get the array length.
 
    2.if((nums[0] == 1 && nums[1] == 3) && ( nums[length - 2] == 1 && 
                                             nums[length] -1  == 3)){
    3.      return true;        
                } 
    4.  return false;       
                }

In row 2,your code was: "if(nums[i-1] == 1 && nums[i] == 3)";

It showed arrayoutofbound because the starting array index is 0 and you decalred in the if statement

" if(nums[0-1]...)which says if nums[-1] which is out of bounds."

Also to check the last 2 numbers of the array you do the following:

      ( nums[length - 1] == 1 && nums[length] == 3)) where : 

               " nums[length - 2] == 1" 
      checks 1 value before the last array value 
                         
                     and 
               " nums[length] - 1 == 3 "
             checks the last array value
Noname
  • 15
  • 6
  • 1
    This answer is hard to understand, and just wrong is some parts, there is no reason to declare the method `static`, the OP does not specify how he is calling the method. Also what happens to your method when the array is length of 1? You would get the same index error. – Nexevis Jan 05 '23 at 20:45