Pair with given sum in sorted array

Run Settings
LanguageC++
Language Version
Run Command
#include <iostream> #include<array> using namespace std; bool pairSum(int arr[],int d) { // int n = arr::arr.size(); int l=0; int r=5; while(l!=r) { if(arr[l]+arr[r] == d) return true; else if(arr[l]+arr[r] < d) l++; else if(arr[l]+arr[r] > d) r--; } return false; } int main() { int arr[] = {2,5,7,11,26,50}; int d = 18; if (pairSum(arr, d)) cout << "Array has two elements with sum 16"; else cout << "Array doesn't have two elements with sum 16 "; return 0; }
Editor Settings
Theme
Key bindings
Full width
Lines