1

Problem: Write a method that will check an array for an odd number followed by an even number. Find the first odd number. Then, after finding the odd number, start looking at that position for an even number and then return the distance between them. Return -1 if there are no odd numbers or no odd numbers followed by an even number.

Data: An array containing 0 or more values. The array will not point at null.

Output: Return the distance or -1.

Sample Data:

7, 5, 10, 12

3, 5, 7, 1

2, 4, 3, 8, 10, 12

11

5, 3, 11, 6, 7, 11, 8

Sample Output

2

-1

1

-1

3


Here is the introduction to a problem from apluscompsci.com. This problem is one that was assigned to the students in my AP Computer Science class. So, my code to this problem goes as such:

int go( int[] array ) 
{
    int o = 1;
    int e = 0;
    int distance = e-o;
    if(array.length==0)
    {
        return distance;
    }
    for(int a = 0, y = 0; a < array.length || y<1; a++)
    {
        if(array[a]%2==1)
        {
            y++;
            o = a;
            for(int b = a + 1, x = 0; b < array.length || x<1; b++)
            {
                if(array[b]%2==0)
                {
                    x++;
                    e = b;
                }
            }
        }
    }
    if(e==0)
    {
        o=1;
    }
    return distance;
}

For some reason, the code that I've constructed to solve the problem keeps firing a "java.lang.ArrayIndexOutOfBoundsException" error.

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 13
at Submission.go(typed.java:30)
at Submission.main(typed.java:9)

This couldn't be possible, though, as I've coded this solution in the most abstract way possible and my for loops never request an index that doesn't exist. Not even the teacher or the smartest kids in the room could help me find out and I've tweaked this code for almost 3 days. I've tried troubleshooting by reconstructing the code in Eclipse, in the case that this website's JavaScript may not support some of the extra techniques I used such as the multiple variable initiations and the or operators in my for loops, but it spits back the same error.

Also, this is my first question here on this site! I actually just made an account to ask this, lol! Hope I laid everything out plain and simple - thanks in advance for any help!

Tradell
  • 21
  • 3
  • You don't need two for loops for this. Take care of this and I am sure your exception will go away. – Joakim Danielson Mar 02 '19 at 07:35
  • @CertainPerformance Sorry about that! The lines distinguishing the two are still a little blurry for me. – Tradell Mar 02 '19 at 07:37
  • @JoakimDanielson I need two loops for both odd and even numbers, no? – Tradell Mar 02 '19 at 07:38
  • No, since you are only looking for an even number after you have found an odd number. Look for an odd number, if you find one remember its position and start looking for an even one. You can use a boolean to "switch state" between looking for an odd or looking for an even – Joakim Danielson Mar 02 '19 at 07:41

4 Answers4

0

Here is a pseudo-code solution

for each number in array
     if is looking for odd and number is odd
         remember position
         set is looking for odd to false

     if is looking for even and number is even
        distance = remembered position - current position
        quit loop
Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52
  • What stops this for loop from going to the next odd number and remembering that one? I need a way to stop its search for odd numbers at the first odd number that it finds. – Tradell Mar 02 '19 at 07:54
  • @Tradell You switch the flag for looking for odd numbers to false. I see someone has already done the homework for you and used the the position value instead of a boolean flag which is an equally good solution IMO. – Joakim Danielson Mar 02 '19 at 08:35
  • Yes, I'm still amazed at how simple that was made, lol, but you both had some great suggestions! Appreciate you for your help! – Tradell Mar 02 '19 at 08:38
0

You seem to misunderstand what distance = e - o means. It does not mean that distance will change depending on e and o. It just means that whatever value e and o holds at the moment, are used to compute the value of distance, and distance will have that same value, until you change it. Changing e or o after that line has no effect on distance. Therefore, distance will always be -1.

You also seem to be using nested loops. That is not needed here. Here is a general algorithm:

for each element x in the array
    if x is odd then
        record x's position in y
    if an odd number has been found and x is even then
        return x's position minus y

And here is some code:

public static int oddEven(int[] numbers) {
    int oddPosition = -1;
    for (int i = 0 ; i < numbers.length ; i++) {
        if (numbers[i] % 2 == 1) {
            oddPosition = i;
        }
        if (oddPosition != -1 && numbers[i] % 2 == 0) {
            return i - oddPosition;
        }
    }
    return -1;
}
Sweeper
  • 213,210
  • 22
  • 193
  • 313
0

Look at three (simplified) lines of your code:

for (int a = 0; a < arrays.length; a++) {

    for (int b = a+1; ...) {

        array[b]

Now, as soon as a is at the end of the array and you do b = a+1, b is after the end of the array, and doing array[b] will throw the exception.

Example:

  • array = [1]
  • a = 0
  • b = 1
  • array[1] ??? out of bounds
Ridcully
  • 23,362
  • 7
  • 71
  • 86
0

Here is a solution. You don't need 2 loops

public class App 
{
    public static void main( String[] args )
    {
        int arr[] = {3, 5, 7, 1};
        System.out.println( go(arr) );
    }

    static int go(int[] array) {
        int oddPos = -1;
        for (int i = 0; i < array.length; i++) {
            //check if already din't find an odd number and if current number is odd
            if (oddPos == -1 && array[i] % 2 == 1) {
                oddPos = i;
            }
            // check if already found an odd number and current number is even
            if (oddPos != -1 && array[i] % 2 == 0) {
                return i - oddPos;
            }
        }
        return -1;
    }
}
Thanthu
  • 4,399
  • 34
  • 43