I am solving this question from a contest on codeforces. How to solve this. Can we get any recurrence relation or generalized solution for this question?
What I have understood is we have to find
$$3\mid a_1+a_2+a_3+....+a_n$$ where $-1 < a_i < k$ for all $0 < i < (n+1)$ where $k$ is any positive integer.
Question link :http://codeforces.com/contest/1105/problem/C
Hint: For integers $l\le r$, $n\ge 0$, $c\in\{0,1,2\}$, let $f(l,r,n,c)$ be the number of arrays of $n$ integers $\in\{l,\ldots, r\}$ such that their sum is $\equiv c\pmod 3$. Now try to express $f(l,r,n,c)$ in terms of the $f(l,r,n-1,*)$.