isPaired will define whenever the array has paired that equal to k
| 3 | |
| 4 | // isPaired will define whenever the array has paired that equal to k |
| 5 | bool isPaired(vector<long long int> A, int length, int k) { |
| 6 | int left=0, right = length - 1; // init left and right pointer for upper lower bound |
| 7 | |
| 8 | while(left < right) { |
| 9 | if (A[left] + A[right] < k) left++; // increase lower bound if result < k |
| 10 | else if (A[left] + A[right] > k) right--; // deacrease upper bound if result > k |
| 11 | else { // pair number is found. A[left] + A[right] = k |
| 12 | return true; |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | return false; |
| 17 | } |
| 18 | |
| 19 | int main() { |
| 20 | int t, n; |