Greater and Smaller Array [Explained CPP/Python]  O(n^2) time
The idea is simple.\nAccording to Q we need three numbers A[i],A[j] and A[k] such that i<j<k and < < or > > property holds between the elements.\nFirst lets tak
The idea is simple.\nAccording to Q we need three numbers A[i],A[j] and A[k] such that i<j<k and < < or > > property holds between the elements.\n**First lets take A[i]>A[j]>A[k]**\nConsider the middle element if any element of the array will be the part of triplet and if we want it in middle then there should be at least one greater element present on right side of it and at least one smaller element on left of it.\n\n##### So total triplets with A[j] in middle will be (count of elements>A[j] on left) * (count of elements <A[j] on right) \n\nThus we make two arrays Greater and Smaller which will store count of elements greater than A[i] and smaller than A[i] in greater[i] and smaller[i] respectively.\n\n**Now for A[i]<A[j]<A[k] reverse the array and do the same thing**\n\n**CPP Version**\n```\n\nclass Solution {\npublic:\n    int numTeams(vector<int>& rating) {\n        int n=rating.size();\n        int k= count(rating);                           //            for < < case\n        reverse(rating.begin(),rating.end());\n        k+=(count(rating));                                   //             for > > case\n        return k;\n        \n    }\n    \n    int count(vector<int>& rating){\n        int n=rating.size();\n        vector<int> greater(n);             //greater [i] store count of elements greater than rating[i] on right side\n        vector<int> smaller(n);            // smaller [i] stores count of elements smaller than rating[i] on left side\n        for(int i=0;i<n;i++){\n            int target=rating[i];\n            int c=0;\n            for(int j=i+1;j<n;j++){\n                if(rating[j]>target)\n                    c++;\n            }\n            greater[i]=c;\n        }\n        \n        for(int i=0;i<n;i++){\n            int target=rating[i];\n            int c=0;\n            for(int j=i-1;j>=0;j--){\n                if(rating[j]<target)\n                    c++;\n            }\n            smaller[i]=c;\n        }        \n        int sum=0;\n        for(int i=0;i<n;i++)\n            sum+=(greater[i]*smaller[i]);                    //Total triplets with A[i] as middle element in (  >A[i] > ) this case\n        return sum;\n        \n    }\n};\n```\n\n**Python Version**\n```\nclass Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        def count(rating):\n            n= len(rating)\n            gre= [0]*n\n            sm= [0]*n\n            for (i,ele) in enumerate(rating):\n                c=0\n                for x in range(n-1,i,-1):\n                    if rating[x]>ele:\n                        c+=1\n                gre[i]=c\n                c=0\n                for x in range(0,i):\n                    if rating[x]<ele:\n                        c+=1\n                sm[i]=c\n            ans=0\n            for i in range(n):\n                ans+=(gre[i]*sm[i])\n            return ans;\n        \n        ans=0\n        ans+=count(rating)\n        rating.reverse()\n        ans+=count(rating)\n        return ans\n\t```\n**PLEASE UPVOTE IF YOU LIKE**
['C++']