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. pre[0]=0;
  7. for(int j=1;j<n;j++){
  8. int count=0;
  9. for(int i=0;i<=j-1;i++){ //as i<k
  10. if(arr[i]>arr[j]){
  11. count++;
  12. }
  13. }
  14. pre[j]=count;
  15. }
  16.  
  17. suf[n-1]=0;
  18. for(int k=n-2;k>=0;k--){
  19. int count=0;
  20. for(int l=k+1;l<n;l++){
  21. if(arr[k]>arr[l]){
  22. count++;
  23. }
  24. }
  25. suf[k]=count;
  26. }
  27.  
  28.  
  29. int f_count=0;
  30. for(int j=0;j<n;j++){
  31. int count=0;
  32. for(int k=j+1;k<n;k++){
  33. if(arr[j]<arr[k]){
  34. f_count+=pre[j]*suf[k];
  35. }
  36. }
  37. }
  38. return f_count;
  39.  
  40.  
  41.  
  42. }
  43.  
  44. int main() {
  45. // your code goes here
  46. int n;
  47. cin>>n;
  48. int arr[n];
  49. for(int i=0;i<n;i++){
  50. cin>>arr[i];
  51. }
  52. cout<<"The count of valid triplets are:"<<getCount(n,arr);
  53.  
  54. return 0;
  55. }
Success #stdin #stdout 0.01s 5304KB
stdin
5
1 5 2 11 5
stdout
The count of valid triplets are:1