fixed the issue by only reseting flag[i] in the final peterson