This is pure logic based issue, you just need to look for next element to find the gap, see below working sample:
public class GapFinder {
private static int[] myIntArr = {9, 10, 11, 12, 13, 15};
public static void main(String[] args) {
int a = 0,b = 0;
for (int i = 0; i < myIntArr.length; i++) {
if(((i + 1) < myIntArr.length) && myIntArr[i] != myIntArr[i + 1] - 1) {
a = myIntArr[i];
b = myIntArr[i + 1];
break;
}
}
if(a == 0 && b == 0){
System.out.println("There were no gaps.");
} else{
System.out.println("Gaps: " + a + " | " + b);
}
}
}
Please note:
- This is not a concept which I could possibly explain, this is pure logic and I have said this in my opening statement, and logic is also straight forward which needs no explaination, its quiet implicit and not a complex algorithmic logic; OP just needed to loop for next element to identify the gap, until next/previous element is checked, a gap cannot be identified with getting expected values, this was just a small step OP was missing and OP got it
- This is a working solution, there could be corner cases like what @9000 mentioned in his comment, it is basic assumption that I cannot provide answer handling all corner cases, I am just highlighting the logic which would help solve the problem.