I was solving a simple problem on spoj called Ambiguous Permutations(http://www.spoj.com/problems/PERMUT2/), it worked fine when I tested for small inputs, but on submission it shows runtime error - segmentation fault. I'm not able to figure it out (though I've wasted a lot of time, and gained frustration only). Please help.
#include <iostream>
#include <stdlib.h>
#include <string.h>
char arr1[200000];//stores original string.
char arr2[200000];//stores inverse permutation.
long int n;
using namespace std;
int main()
{
    while (1)
    {
        cin >> n;
        if (n == 0)
        {
            exit(0);
        }
        getchar();
        gets(arr1);
        //creating inverse permutation.
        for (long int i = 0; i < 2 * n - 1; i++)
        {
            if (i % 2 == 1)
            {
                arr2[i] = ' ';
            }
            else
            {
                arr2[2 * (arr1[i] - '0') - 2] = i / 2 + '1';
            }
        }
        arr2[2 * n - 1] = '\0';
        //comparing string with it's inverse permutation.
        if (strcmp(arr1, arr2) == 0)
        {
            cout << endl << "ambiguous";
        }
        else
        {
            cout << endl << "not ambiguous";
        }
    }
    return 0;
}
 
    