#C11535. Harmonious Pairs of Houses
Harmonious Pairs of Houses
Harmonious Pairs of Houses
You are given N houses in a row, each painted in a certain color. The colors are represented by integers. You are also given an integer K, which defines the target difference between the colors. A pair of houses (i, j) (with i < j) is considered harmonious if
\( |\text{color}_i - \text{color}_j| = K \). Your task is to count the total number of harmonious pairs among the houses.
Note: Each harmonious pair should be counted exactly once.
Example:
For example, with N = 5, K = 3 and colors [1, 5, 2, 2, 8]
, the harmonious pairs are formed by the indices corresponding to the pairs of colors (1,4), (3,4) and (2,5) (based on 1-indexing), hence the answer is 3.
inputFormat
The input is read from stdin and consists of two lines:
- The first line contains two space-separated integers N and K, where N is the number of houses and K is the target difference between house colors.
- The second line contains N space-separated integers representing the color of each house.
outputFormat
Output a single integer to stdout — the number of harmonious pairs of houses.
## sample5 3
1 5 2 2 8
3