class Solution{ public: // Function to check if array has 2 elements // whose sum is equal to the given value bool hasArrayTwoCandidates(int arr[], int n, int x) { sort(arr,arr+n); int i=0,j=n-1; while(i<j) { if(arr[i]+arr[j]==x) return 1; if(arr[i]+arr[j]>x) j--; if(arr[i]+arr[j]<x) i++; } return 0; // code here } };
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter