Given an array, A, having N integers A1, A2,..., An.Two elements of the array Ai and Aj are called similar iff Ai =Aj +1 or Aj= Ai +1 Also, the similarity follows transitivity. If Ai and Aj are similar and Aj and Ak are similar, then Ai and Ak are also similar.You need to find number of pairs i,j such that i<j and Ai is similar Aj..
Input :
N :Number of elements in array
N space separated elements denoting Ai th element
Constraints:
1<=N<=10^6
10^-9<=Ai<=10^-9
example:
Input :
8
1 3 5 7 8 2 5 7
Output :
6
How can I solve the problem ?