After each loop count and count1 are updated. After giving inputs in Scanner, I'm not getting any output.
Scanner sc = new Scanner(System.in);
int t = sc.nextInt(); // t=1
while (t != 0) {
int n = sc.nextInt(); // n=5
int a[] = new int[n]; // a = { 1,2,5,6,7 }
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
int count = 0, count1 = 0;
for (int i = 0; i < n; i++) {
if ((a[i + 1] - a[i]) > 2) {
count++;
} else {
count1++;
}
}
// this doesn't get printed
System.out.println(count + 1 + " " + count1);
t--;
}
ArrayIndexOutOfBoundsException? Because theforloop containing it runs untili == n-1, the statementif ((a[i + 1] - a[i]) > 2)(specifically, thea[i+1]bit) will blow up right whenidoes reach the value ofn-1and the program will have never reached theSystem.out.printlnthat follows the loop.