#K79502. Pair with Given Difference
Pair with Given Difference
Pair with Given Difference
You are given an array of integers and an integer target value \(D\). Determine whether there exist two distinct indices \(i\) and \(j\) in the array such that the difference between the corresponding elements satisfies the equation:
\(A[i] - A[j] = D\)
Note that the difference can be computed in either order; i.e. the pair \(A[i]\) and \(A[j]\) qualifies if either \(A[i] - A[j] = D\) or \(A[j] - A[i] = D\). The order does not matter as long as the two indices are distinct.
Input: The first line contains an integer \(n\), the number of elements in the array. The second line contains \(n\) space-separated integers which represent the array. The third line contains the integer \(D\), the target difference.
Output: Print True
if such a pair exists; otherwise, print False
.
inputFormat
The input consists of three lines:
- First line: an integer \(n\) (the number of elements in the array).
- Second line: \(n\) space-separated integers representing the array.
- Third line: an integer \(D\) representing the target difference.
outputFormat
Output a single line: True
if there exists a pair of distinct indices \(i\) and \(j\) such that \(A[i] - A[j] = D\) (in either order), otherwise False
.
6
5 20 3 2 50 80
78
True