A week ago I got my homework, where I have to write a function in C. The function gets a single array of positive integers, and it has to return the next number in the array. The arrays look something like this: {1,2,3,1,2,3,4,1,2,3,1,2,3,4,1,2,3,-1}; -1 means the end of the array.
I know that the number which has to be returned by the function is 1, however, how can I code a pattern finding algorithm? I haven't found any solution on the internet, since every other question about pattern searching is with strings, where the pattern which has to be found is already given.
if the pattern has a length of
1
, you will havea[k+1] == a[k]
for all possiblek
.more generally, you will have
a[k+plen] == a[k]
for the correctplen
(pattern length) and all possiblek
.so determine
plen
starting with1
... in your case you get7
becausea[7]==a[0]
,a[8]==a[1]
, ...a[16]==a[9]
, so just returna[17-7]
.