fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. bool avail(int n,int arr[]){
  4. unordered_map<int,int>preHash;
  5. unordered_map<int,int>suffHash;
  6. preHash[arr[0]]++;
  7. suffHash[arr[n-1]]++;
  8. for(int i=1;i<n-1;i++){
  9. suffHash[arr[i+1]]++;
  10. if(preHash.find(arr[i]-1)!=preHash.end() && suffHash.find(arr[i]+1)!=suffHash.end()){
  11. return true;
  12. }
  13. preHash[arr[i]]++;
  14. }
  15. return false;
  16. }
  17.  
  18. int main() {
  19. // your code goes here
  20. int n;
  21. cin>>n;
  22. int arr[n];
  23. for(int i=0;i<n;i++){
  24. cin>>arr[i];
  25. }
  26. if(avail(n,arr)){
  27. cout<<"There exits triplets";
  28. }
  29. else{
  30. cout<<"There does exits triplets";
  31. }
  32. return 0;
  33. }
Success #stdin #stdout 0.01s 5316KB
stdin
3
2 3 6
stdout
There does exits triplets