Loading...
Find Pair some of a target value | Majority element of an Array
To find the pair sum for a target value the give code looks like this
#include <iostream> #include <vector> using namespace std; int main() { vector<int> vec = { 2, 7, 11, 15 }; int target = 22; int st = 0, end = vec.size() - 1; while (st < end) { if (target > (vec[st] + vec[end])) { st++; } else if (target < (vec[st] + vec[end])) { end--; } else { cout << st << ", " << end << endl; return 0; } } return -1; }