mihaimarcel21

NrTripletes

Nov 27th, 2020
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.39 KB | None | 0 0
  1. int NrTriplete(int a[], int n, int S)
  2. {
  3.     sort(a, a+n);
  4.     int ans = 0;
  5.     for (int i = 0; i < n - 2; i++)
  6.     {
  7.         int j = i + 1, k = n - 1;
  8.         while (j < k)
  9.         {
  10.             if (a[i] + a[j] + a[k] >= S)
  11.                 k--;
  12.             else
  13.             {
  14.                 ans += (k - j);
  15.                 j++;
  16.             }
  17.         }
  18.     }
  19.     return ans;
  20. }
Add Comment
Please, Sign In to add comment