Pairs Having Similar Elements
Practice
4 (138 votes)
Arrays
Data structures
Easy
One Dimensional
Problem
37% Success 22337 Attempts 20 Points 1s Time Limit 256MB Memory 1024 KB Max Code

Given an array, \(A\), having \(N\) integers \(A_1,A_2,...,A_N\).Two elements of the array \(A_i\) and \(A_j\) are called similar iff \(A_i = A_j+1\) or \(A_j = A_i + 1\) .
Also, the similarity follows transitivity. If \(A_i\) and \(A_j\) are similar and \(A_j\) and \(A_k\) are similar, then \(A_i\) and \(A_k\) are also similar. 
Note: \(i\), \(j\), and \(k\) are all distinct.

You need to find number of pairs of indices \((i,j)\) such that \(i<j \) and \(A_i\) is similar to \(A_j\).

Input

The first line contains a single integer \(N\) denoting number of elements in the array.
The second line contains \(N\) space separated integers where \(i^{th}\) elements indicate\(A_i\).

Output

Output the number of pairs of indices \((i,j)\) such that \(i<j \) and \(A_i\) is similar to \(A_j\) in a single line.

Constraints

\( 1 \le N \le 10^6\\ 10^{-9} \le A_i \le 10^9 \)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:20
44 votes
Tags:
1-D ArrayArraysData StructuresEasyeasy
Points:20
90 votes
Tags:
ArraysBrute-force searchData StructuresEasyHash TablesImplementationMathOne-dimensional
Points:20
30 votes
Tags:
ArraysData StructuresEasyOne-dimensional