Edit: https://stackoverflow.com/a/58756962/12335228 was quicker.
Edit2: As the comment suggests, my assumption that n would be zero initialized is indeed wrong.
Oh? Wrong output you say? It works for me, even tho it shouldnt.
While the logic is correct, you are well into undefined-behaviour land here:
You are declaring n and then declare arr[n] without assigning n an actual value. That means n has its default value, in the case of int 0. So your array has a capacity of 0.
C++ let's you write into that anyway, since you as the programmer are responsible for your memory. Just think of the [] operator in this case as syntactic sugar for (*arr+i), meaning the address of your array + i