The strange array access can also be written in a simpler and more readable way:
arr[n] = j;
As OriginalGriff already stated
else continue;
can be removed completely.
To find the error it makes sense to monitor the array size.
First I would calculate in a variable how many elements the array arr can hold:
int arr[20] = {};
size_t arr_size = sizeof(arr) / sizeof(arr[0]);
Before each access to the field, we can now check whether the index is still in the field or not:
if (n < arr_size) {
arr[n] = j;
}
else
printf("error: n is too big %d\n", n);
}
As you can easily see the field arr e.g. for N=360 with only 20 elements is not enough to store all numbers and the field overflows.
To look at the problem you can of course output all N that are too large:
if(n >= arr_size)
printf("for i=%d, n was %d\n", i, n);
if (i == sum) {
for i=360, n was 23
For N to 1000, the arr field would have to contain at least 32 elements.