I think the main issue is that you are doing int *used = new int(p_n);
, this will create a pointer to an integer that has initial value equal to p_n
. But, the problem comes here: used[i_used]=p_arr[i][1];
— this may lead to a segmentation fault, as you may end up using memory outside the valid bounds.
I’m not sure about the algorithm, but based on the code, I think you should use this instead:
used = new int[p_n]; // assuming p_n > 0
CLICK HERE to find out more related problems solutions.