fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int getCount(int n,int arr[]){
  4. vector<int>pre(n+1,0);
  5. vector<int>suf(n+1,0);
  6. for(int j=1;j<n;j++){
  7. int count=0;
  8. for(int i=0;i<=j-1;i++){ //as i<k
  9. if(arr[i]>arr[j]){
  10. count++;
  11. }
  12. }
  13. pre[j]=count;
  14. }
  15. for(int j=n-2;j>=0;j--){
  16. int count=0;
  17. for(int k=j+1;k<n;k++){ //as i<k
  18. if(arr[j]<arr[k]){
  19. count++;
  20. }
  21. }
  22. suf[j]=count;
  23. }
  24. int f_sum=0;
  25. int sum;
  26. for(int i=0;i<n;i++){
  27. sum=0;
  28. sum=pre[i]+suf[i];
  29. f_sum+=sum;
  30. }
  31. return f_sum;
  32.  
  33. }
  34.  
  35. int main() {
  36. // your code goes here
  37. int n;
  38. cin>>n;
  39. int arr[n];
  40. for(int i=0;i<n;i++){
  41. cin>>arr[i];
  42. }
  43. cout<<"The count of valid triplets are:"<<getCount(n,arr);
  44.  
  45. return 0;
  46. }
Success #stdin #stdout 0.01s 5312KB
stdin
6
8 1 2 3 4 5
stdout
The count of valid triplets are:15