Given an array with the size of $n$ of integer numbers. Our goal will be to analyze the expected running time of an algorithm that uses a hash table to find (if exists) a pair $(a,b)$ subject to $2<|a-b|<5$.
The answer is $O(n)$ but I have no idea to convince myself. Some thoughts that come to my mind are as follows: consider $a$ to find a $b$ in the hash table maybe I have a cost $1+2+\dots+n$ so for each $a$ the average cost will be $O(n^2)$. But the answer is $O(n)$. Anyone can correct my mistake?