I am working on below challenge

http://codingbat.com/prob/p159979

Psedo code description of approach :

1. Loop throguh given array

2. check if array adjacent three elements are even or odd

3. if yes return true

4. if no return false

I wrote my code as below

I am failing couple of tests

http://codingbat.com/prob/p159979

Psedo code description of approach :

1. Loop throguh given array

2. check if array adjacent three elements are even or odd

3. if yes return true

4. if no return false

I wrote my code as below

```
public boolean modThree(int[] nums) {
boolean result=false;
for(int i=0;i<nums.length-2;i++){
if(nums[i]%2==0&&nums[i+1]%2==0&&nums[i+2]%2==0){
result=true;
return result;
}
else if(nums[i+1]%2==0&&nums[i+2]%2==0&&nums[i+3]%2==0){
result=true;
return result;
}
else if(nums[i]%2==1&&nums[i+1]%2==1&&nums[i+2]%2==1){
result=true;
return result;
}
else if(nums[i+1]%2==1&&nums[i+2]%2==1&&nums[i+3]%2==1){
result=true;
return result;
}
else{
result=false;
}
//return result;
}
return result;
}
```

I am failing couple of tests

Expected RunHow to improve my design, approach, code? please advise

modThree([2, 1, 3, 5]) → true true OK

modThree([2, 1, 2, 5]) → false false OK

modThree([2, 4, 2, 5]) → true true OK

modThree([1, 2, 1, 2, 1]) → false false OK

modThree([9, 9, 9]) → true true OK

modThree([1, 2, 1]) → false false OK

modThree([1, 2]) → false false OK

modThree([1]) → false false OK

modThree([]) → false false OK

modThree([9, 7, 2, 9]) → false false OK

modThree([9, 7, 2, 9, 2, 2]) → false Exception:java.lang.ArrayIndexOutOfB oundsExcep tion: 6 (line number:10) X

modThree([9, 7, 2, 9, 2, 2, 6]) → true true OK

```
public boolean modThree(int[] nums) {
boolean result=false;
for(int i=0;i<nums.length-2;i++){
if(nums[i]%2==0&&nums[i+1]%2==0&&nums[i+2]%2==0){
result=true;
return result;
}
else if(nums[i]%2==1&&nums[i+1]%2==1&&nums[i+2]%2==1){
result=true;
return result;
}
//return result;
}
return result;
}
```

above passed all tests
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.

As the message says, ArrayIndexOutOfBoundsExcep

This is because of the reference to

nums[i+3].modThree([9, 7, 2, 9, 2, 2])would give a array of length 6. Sincei<nums.length-2is the loop condition, max 'i' value would be 3. So nums[3+3] would give error because highest index is 5.