#K50782. Check Difference Pair
Check Difference Pair
Check Difference Pair
Given an array of integers and an integer \( k \), determine whether there exist two distinct indices \( i \) and \( j \) such that the absolute difference between \( nums[i] \) and \( nums[j] \) is exactly \( k \). In other words, check whether there exists a pair \( (i, j) \) (with \( i \neq j \)) such that \( |nums[i] - nums[j]| = k \).
The problem tests the ability to efficiently search for a pair with a given difference. For example, in the array [1, 9, 5, 3, 7] with \( k=4 \), there exists at least one valid pair (1 and 5, or 5 and 9) satisfying the condition.
Note: When \( k=0 \), the task reduces to checking whether there is any duplicate in the array.
inputFormat
The input is read from standard input (stdin) and consists of three lines:
- The first line contains a single integer \( n \) representing the number of elements in the array.
- The second line contains \( n \) space-separated integers representing the array \( nums \).
- The third line contains a single integer \( k \).
outputFormat
Output a single line to standard output (stdout) containing either True
if there exists a pair of indices \( i \) and \( j \) such that \( |nums[i] - nums[j]| = k \), or False
otherwise.
5
1 9 5 3 7
4
True