Untitled
unknown
plain_text
21 days ago
660 B
3
Indexable
Never
#include <iostream> #include <vector> using namespace std; bool findPairWithSum(const vector<int>& arr, int x) { int left = 0, right = arr.size() - 1; while (left < right) { int sum = arr[left] + arr[right]; if (sum == x) { return true; } else if (sum < x) { left++; } else { right--; } } return false; } int main() { vector<int> arr = {1, 2, 4, 4, 7, 11, 15}; int x = 9; if (findPairWithSum(arr, x)) { cout << "Yes, a pair with the given sum exists." << endl; } else { cout << "No such pair exists." << endl; } return 0; }
Leave a Comment