A pattern of the integers 2,1,4 is irregularly repeated in an array of 5000 elements, with rest of the elements in the array being 0. But 2 is always followed by 1, and 1 is always followed by 4, then 2 again and so on...
For example:-
0 0 0 0 0 0 0 2 0 0 0 0 0 1 0 0 0 0 0 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 1 0 0 0 4 0 0 0 0 0 2 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 4 0 0 0 0 0......and so on.
I want to check if this 2-1-4 pattern is repeated in the array, irrespective of the number of 0's between the elements.
If this pattern, however irregular, is repeated, then I have a correct array. Else I have missed some elements.
How can I go about this ??
Edit:- This be done without traversing the array multiple times with simple C language code.
C
ORC++
The best answer will differ considerably based on which language it is, so choose one or the other, since they are quite different.