The given part of the code gives me an ArrayIndexOutofBoundsException
, but I don't actually know how to handle it.
int count = 0;
int n = sc.nextInt();
int[] a = new int[n];
for(int i=0;i<n;i++)
{
a[i] = sc.nextInt();
}
for(int i=1; i<n; i++)
{
if(a[i+2]>a[i+8] && a[i+3]>a[i+8] && a[i+4]>a[i+8])
{
count++;
}
}
System.out.println(count);
The array should stop working when it reaches a[n-1]
but it goes beyond that in the for loop and gives me the out of bounds error.