The problem begins at your do while loop.
Say arr.length() == 3 (so that means the loop should run 3 times, 0 < 3, 1 < 3, 2 < 3)
The problem is when it reaches the last time - it checks if 2 (n) is less than 3 (arr.length) and it executes the do block of code again -
this time when it starts the last time of the loop it will increase n to 3 - which causes the problem:
There is no arr[3] - because if the length of the array is 3 - the highest one will be arr[2] (0, 1, 2).
I'd recommend you set n to 0 at the beginning and do a simplistic while loop without the do section to not confuse yourself.
Hope you get it.