Created
January 22, 2018 09:30
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
using namespace std; | |
int a[101010]; | |
void two_pointer_triplet_sum(int n) | |
{ | |
sort(a, a+n); | |
for(int i = n-1; i >= 0; i--) | |
{ | |
int l = 0; | |
int r = i-1; | |
int val = a[i]; | |
while(l < r) | |
{ | |
if(a[l] + a[r] == val) | |
{ | |
cout<< "Triplet : " << val << "," << a[l] << "," << a[r] << endl; | |
return; | |
} | |
else if(val > a[l] + a[r])l++; | |
else r--; | |
} | |
} | |
cout << "No triplet found" << endl; | |
} | |
int main() | |
{ | |
int n; | |
cin >> n; | |
for(int i = 0; i < n; i++)cin >> a[i]; | |
two_pointer_triplet_sum(n); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment