Can you help me figure out what is going wrong here, please?

I am practicing for CCDSAP Foundation Level. I got stuck while solving the DBOY problem. Following are my solutions, The first one got sigsegv error whereas the latter one got accepted.

The only change in both the code is
for(int i=0;i<n;i++){

Can anyone please tell me why the latter one gets AC.