I was trying the the problem Almost Sorted Permutation which was given in October '15 Cook Off. The problem is pretty straight forward. I just checked if the each no. is at the same index, or one index before, on one index after. Below is the pseudo code:
a = seq of nos. (zero-based index)
if a[0] = 1 or a[1] == 1:
possible = True
for i in [1..n-2]:
if a[i-1] == i+1 or a[i] == i+1 or a[i+1] == i+1:
continue
else:
possible = False
break
if a[n-2] != n or a[n-1] != n:
possible = False
if possible == True:
print 'YES'
else:
print 'NO'
else:
print 'NO'
Link to submitted solution. Please help where the code is getting runtime error.