/*
The function f is defined on any finite sequence of integer numbers.
It returns total number of 3-length segments of the sequence containing “0 1 2”
(e.g. number of indexes k where a[k]=0, a[k+1]=1, and a[k+2]=2).
Find inductive extension of f.
*/
using System;
using System.Diagnostics;
public class Program
{
public static void Main()
// Just for testing - modify to test other cases
int [] a = { 0, 1, 2, 0, 0, 1, 1, 2, 0, 1, 2, 2, 0};
Console.WriteLine(f(a, 3));
}
// We'll solve more common problem and we'll find number of segments containing
// "0 1 .. s-1"
static int f(int [] a, int s)
Trace.Assert(s > 0);
// Inductive extension:
int n = 0; // number of completed sequences 0 .. s-1
int len = 0; // length of last incompleted sequence 0 .. s-1, len < s
foreach(int nextElem in a) // we can read array same way as we'd read user input
if(nextElem == len)
{ // nextElem extends last segment
len++;
else
{ // nextElem does not extend last segment
if(nextElem == 0)
len = 1;
len = 0;
// nextElem processed with respect to last segment
if(len == s)
{ // last segment is completed
n++;
return n;