In this task you will give a sequence of numbers $S$ of length $N$. Every element will be greater than or equal to 1. We write $S[i]$ to refer to the $i$-th element of the sequence. For example $S = (1, 2, 3, 4, 5, 4)$ is of length $6$ and $S[2] = 2, S[6] = 4$ and so on. You will also be given a number $T$, called the target.
We would like to find four elements of the sequence $S$ whose sum is $T$. More precisely, we would like to find four numbers $i, j, k, \ell$, with $1 ≤ i < j < k < \ell ≤ N$ such that $$T = S[i] + S[j] + S[k] + S[\ell].$$
If $T = 16$ then there is a unique way to do this which is to take $i = 3, j = 4$, $k = 5$ and $\ell = 6$. Finally, $T = 9$ cannot be written as the sum of $4$ elements of the sequence $S$ and thus the number of ways in this case is $0$.
For the following $S$ and $T$ find the number of ways of expressing $T$ as the sum of four elements of $S$.
(a) $S = (2, 1, 1, 1, 2, 1, 2, 2, 1)$ and $T = 6$
(b) $S = (2, 1, 2, 1, 3, 3, 1, 2, 1)$ and $T = 9$
(c) $S = (1, 3, 2, 1, 1, 3, 4, 2, 2)$ and $T = 8$
Is there any mathematical way of finding the number of ways, because counting manually will take a long time.
Ans:- (a) 60
(b) 14
(c) 30