Skip to content
Advertisement

The numbers never occur next to each other

I wrote a program that reads an array of integers and two numbers n and m. The program check that n and m never occur next to each other (in any order) in the array.

JavaScript

Test input:

JavaScript

Correct output: true

My output is blank. What am I doing wrong?

Advertisement

Answer

Your code will throw ArrayIndexOutOfBoundsException as you are using array[j+1] whereas you have loop condition as j < len. The condition should be j < len -1.

The following works as expected:

JavaScript

A sample run:

JavaScript
Advertisement