Untitled
unknown
plain_text
a year ago
660 B
10
Indexable
#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;
}
Editor is loading...
Leave a Comment