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.
sigsegv
Correct

The only change in both the code is
for(int i=0;i<n;i++){
dp[K[i]]=1;
}

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