Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
671 B
3
Indexable
#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