Given a list of integers and a target integer, return the number of triplets whose product is the target integer and two adjacent triplets Question: Given a list of integers (possibly negative) and a target integer, return the number of triplets whose product is the target integer and two of the triplets must be adjacent. 


More precisely, given a triplet $(i,j,k)$ with $i&lt;j&lt;k,$ it satisfies the question above if $A[i] \times A[j] \times A[k] = target$ and either ($j = i+1$ and $k &gt; j+1$) or ($k = j+1$ and $i &lt; j -1$.)

For example, if the list given is $A = [1,2,2,2,4]$ and target $= 8,$ then the answer is $2$ as $(0, 1, 4)$ and $(0, 3, 4)$ are the only triplets satisfying conditions above if we use $0$-based numbering.

I stucked at this question for 3 hours and not able to solve it. 

Any hint is appreciated.
Let $n$ be the number of integers and $t$ be the target integer.
You can solve your problem in $O(n \log n)$ time as follows:

For every $i=0, \dots, n-1$, let $p = A[i] \cdot A[i+1]$ and compute, in $O(\log n)$ time, the number $\eta(t/p)$ of integers in $A$ that are equal to $t/p$ (more on that later).
Then the number $\gamma_i$ of triplets formed by $A[i]$, $A[i+1]$ and a non-adjacent element is $\gamma_i = \eta(t/p) -  |\{A[j]=t/p \, : \, i-1 \le j \le i+2 \}|$.

It follows that the number of triplets of interest is $\sum_{i=1}^{n-1} \gamma_i$.

To find the values  $\eta(\cdot)$ do a preprocessing in which you create a copy $B$ of $A$, sort $B$ in $O(n \log n)$ time, and append an element with value $+\infty$ to $B$ (this is just to avoid edge cases, $+\infty$ can actually be any integer greater than $t$).

If $x \not\in\mathbb{Z}$, $\eta(x)=0$. Otherwise $\eta(x)$ can be found in $O(\log n)$ time by binary searching for the smallest indices $i$ and $j$ in $B$ such that $B[i]\ge x$ and $B[j] &gt; x$, respectively. Notice that $i$ and $j$ are always well defined and that $\eta(x) = j-i$.