Untitled
#include <iostream> #include <unordered_set> #include <vector> using namespace std; bool findPairWithDifference(const vector<int>& arr, int n) { unordered_set<int> elements; for (int num : arr) { if (elements.find(num + n) != elements.end() || elements.find(num - n) != elements.end()) { return true; } elements.insert(num); } return false; } int main() { vector<int> arr = {5, 20, 3, 2, 50, 80}; int n = 78; if (findPairWithDifference(arr, n)) { cout << "Yes, a pair with the given difference exists." << endl; } else { cout << "No such pair exists." << endl; } return 0; }
Leave a Comment