the first element in the array arr1 has a garbage value
which during the do while loop is accessed as
arr2[j++]=arr1[“some garbage value”]
which is the reason for the error shown
SIGSEGV is mostly caused when you try accessing an invalid array index either a negative index or an index greater then the size of array.The way you have incremented/decremented index might have caused the issue.
I couldnt get the testcase which gives you the error.But here’s my AC soln:
do you think value of j would never go above 2010?
i think thats the problem. this could might pass simple testcase but not all and if j goes beyond 2010 it will give SIGSEGV
do
{
arr2[j++]=arr1[arr2[j-1]];
arr1[arr2[j-2]]=0;
}while(p!=arr2[j-1]);
this might make j go beyond 2010 if u can make proper testcases.
@infinitum j is initialized to 1…by the time you encounter arr[j-2]…j is already 3 so j-2 is a minimum of 1…because after the first time it is used…j only increases…