Beats 99.83% users... Result attached in the soln....
My Result:\n\n\n\n# Code\nPython []\nclass Solution:\n\n    # alt, for perf test\n\n    def numTeams(self, rating: List[int]) -> int:\n\n        l = []\n\n     
# My Result:\n![1000040494.jpg](https://assets.leetcode.com/users/images/9624a22d-3828-4a76-8464-04c5a5303174_1722211814.8420675.jpeg)\n\n\n# Code\n``` Python []\nclass Solution:\n\n    # alt, for perf test\n\n    def numTeams(self, rating: List[int]) -> int:\n\n        l = []\n\n        sr = sorted(rating)\n\n        low = {}\n\n        for idx,r in enumerate(sr):\n\n            low[r] = idx\n\n        res = 0\n\n        n = len(rating)\n\n        for idx,r in enumerate(rating):\n\n            i = bisect.bisect(l, r)\n\n            l.insert(i,r)\n\n            j = low[r] - i\n\n            res+=i*(n-1-idx-j)+j*(idx-i)\n\n        return res\n\n    def numTeams2(self, rating: List[int]) -> int:\n\n        ans,n = 0,len(rating)\n\n        for j in range(n):\n\n            # for any middle position j find counts less than rating[j] in  [0..j) and (j..n) ranges\n\n            llt,lgt = 0,0\n\n            for i in range(j):\n\n                llt += rating[i] < rating[j]\n\n                lgt += rating[i] > rating[j]\n\n            rlt,rgt = 0,0\n\n            for k in range(j+1,n):\n\n                rlt += rating[k] < rating[j]\n\n                rgt += rating[k] > rating[j]\n\n            ans += llt*rgt + lgt*rlt \n\n        return ans\n\n    # bf,tle\n\n    def numTeams1(self, rating: List[int]) -> int:\n\n        ans,n = 0,len(rating)\n\n        for i in range(n):\n\n            for j in range(i+1,n):\n\n                for k in range(j+1,n):\n\n                    ans += 1 if rating[i] < rating[j] < rating[k] or rating[i] > rating[j] > rating[k] else 0\n\n        return ans      \n```\n``` Java []\nclass Solution {\n    public int numTeams(int[] rating) {\n        int n = rating.length;\n        if (n < 3) return 0;\n\n        int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;\n        for (int num : rating) {\n            min = Math.min(min, num);\n            max = Math.max(max, num);\n        }\n        \n        int[] leftTree = new int[max - min + 2];\n        int[] rightTree = new int[max - min + 2];\n        \n        for (int num : rating) {\n            update(rightTree, num - min, 1);\n        }\n        \n        int count = 0;\n        for (int i = 0; i < n; i++) {\n            update(rightTree, rating[i] - min, -1);\n            \n            int lessLeft = query(leftTree, rating[i] - min - 1);\n            int greaterLeft = i - lessLeft;\n            \n            int lessRight = query(rightTree, rating[i] - min - 1);\n            int greaterRight = (n - i - 1) - lessRight;\n            \n            count += lessLeft * greaterRight + greaterLeft * lessRight;\n            \n            update(leftTree, rating[i] - min, 1);\n        }\n        \n        return count;\n    }\n\n    private void update(int[] tree, int index, int value) {\n        index++;\n        while (index < tree.length) {\n            tree[index] += value;\n            index += index & (-index);\n        }\n    }\n\n    private int query(int[] tree, int index) {\n        int sum = 0;\n        index++;\n        while (index > 0) {\n            sum += tree[index];\n            index -= index & (-index);\n        }\n        return sum;\n    }\n}\n```\n``` C++ []\nclass Solution {\npublic:\n    int numTeams(vector<int>& ratings) {\n        // Find the maximum rating\n        int maxRating = 0;\n        for (int rating : ratings) {\n            maxRating = max(maxRating, rating);\n        }\n\n        // Initialize Binary Indexed Trees for left and right sides\n        vector<int> leftBIT(maxRating + 1, 0);\n        vector<int> rightBIT(maxRating + 1, 0);\n\n        // Populate the right BIT with all ratings initially\n        for (int rating : ratings) {\n            updateBIT(rightBIT, rating, 1);\n        }\n\n        int teams = 0;\n        for (int currentRating : ratings) {\n            // Remove current rating from right BIT\n            updateBIT(rightBIT, currentRating, -1);\n\n            // Count soldiers with smaller and larger ratings on both sides\n            int smallerRatingsLeft = getPrefixSum(leftBIT, currentRating - 1);\n            int smallerRatingsRight = getPrefixSum(rightBIT, currentRating - 1);\n            int largerRatingsLeft = getPrefixSum(leftBIT, maxRating) -\n                                    getPrefixSum(leftBIT, currentRating);\n            int largerRatingsRight = getPrefixSum(rightBIT, maxRating) -\n                                     getPrefixSum(rightBIT, currentRating);\n\n            // Count increasing and decreasing sequences\n            teams += (smallerRatingsLeft * largerRatingsRight);\n            teams += (largerRatingsLeft * smallerRatingsRight);\n\n            // Add current rating to left BIT\n            updateBIT(leftBIT, currentRating, 1);\n        }\n\n        return teams;\n    }\n\nprivate:\n    // Update the Binary Indexed Tree\n    void updateBIT(vector<int>& BIT, int index, int value) {\n        while (index < BIT.size()) {\n            BIT[index] += value;\n            index +=\n                index & (-index);  // Move to the next relevant index in BIT\n        }\n    }\n\n    // Get the sum of all elements up to the given index in the BIT\n    int getPrefixSum(vector<int>& BIT, int index) {\n        int sum = 0;\n        while (index > 0) {\n            sum += BIT[index];\n            index -= index & (-index);  // Move to the parent node in BIT\n        }\n        return sum;\n    }\n};\n```
['Array', 'Dynamic Programming', 'Binary Indexed Tree', 'Python', 'C++', 'Java', 'Python3']