![]() |
VOOZH | about |
Given an unsorted array and an integer x, the task is to find if there exists a pair of elements in the array whose absolute difference is x.
Examples:
Input: arr[] = [5, 20, 3, 2, 50, 80], x = 78
Output: true
Explanation: The pair is {2, 80}.Input: arr[] = [90, 70, 20, 80, 50], x = 45
Output: false
Explanation: No such pair exists.
Table of Content
The idea is to use two nested loops to compare every possible pair of elements in the array, checking their absolute difference against the target value.
Yes
The idea is to first sort the array in ascending order and then use two pointers to efficiently traverse the array, incrementing the second pointer (j) to find pairs with the exact target difference.
Step by step approach:
Yes
The idea is to use a hash set to track each number and simultaneously checking if its potential complement (current number +/- difference) exists in the set.
Step by step approach:
Yes