fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int findValidQuadruplets(vector<int> &arr, int k1, int k2){
  5. int totalCount = 0, n = arr.size();
  6. for (int j = 1; j <= n-3; j++){
  7. int leftCount = 0;
  8. for (int i = 0; i <= j-1; i++){
  9. if(arr[i] + arr[j] > k1){
  10. leftCount++;
  11. }
  12. }
  13. int rightCount = 0;
  14. int k = j+1, l = n-1;
  15. while(k < l){
  16. if(arr[k] + arr[l] > k2){
  17. rightCount += l-k;
  18. l--;
  19. }
  20. else {
  21. k++;
  22. }
  23. }
  24.  
  25. totalCount += (leftCount * rightCount);
  26. }
  27.  
  28. return totalCount;
  29. }
  30.  
  31. int main() {
  32. // your code goes here
  33. vector<int> arr = {1, 1, 1, 1, 2, 2};
  34. cout << findValidQuadruplets(arr, 1, 3);
  35.  
  36. return 0;
  37. }
Success #stdin #stdout 0s 5312KB
stdin
Standard input is empty
stdout
6