class Solution{ public: //Function to find if there exists a triplet in the //array A[] which sums up to X. bool find3Numbers(int A[], int n, int X) { //Your Code Here int j; int k; //Your Code Here sort(A,A+n); for(int i=0;i<n-2;i++) { j=i+1; k=n-1; while(j<k) { if(A[i]+A[j]+A[k]==X) return 1; if(A[i]+A[j]+A[k]>X) k--; else j++; } } return 0; } };
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