BEATS 100%🔥 [ C++ / Py / Java / Go ] ✅ SIMPLE
Approach\n1. Initialize Counters: For each soldier at index i, initialize counters to count soldiers with ratings less than or greater than rating[i], both to t
# Approach\n1. **Initialize Counters:** For each soldier at index i, initialize counters to count soldiers with ratings less than or greater than rating[i], both to the left and right of i.\n\n2. **Count Soldiers:**\n  - Traverse the array to count soldiers with ratings less than or greater than rating[i] to the right of i.\n  - Traverse the array to count soldiers with ratings less than or greater than rating[i] to the left of i.\n3. **Calculate Valid Teams:** For each soldier i, the number of valid teams can be calculated as:\n- Teams with increasing order: right_less * left_more\n- Teams with decreasing order: right_more * left_less\n4. **Sum the Counts:** Sum these counts for all soldiers to get the total number of valid teams.\n\n```C++ []\nclass Solution {\npublic:\n    int numTeams(vector<int>& rating) {\n        int total = 0;\n\n        for ( int i = 0 ; i < rating.size() ; i ++ ){\n            int right_less = 0 , right_more = 0 , left_less = 0 , left_more = 0;\n\n            for ( int j = i + 1 ; j < rating.size() ; j ++ ){\n                if ( rating[j] < rating[i] ) right_less++;\n                else if ( rating[i] < rating[j] ) right_more++;\n            }\n            for ( int j = 0 ; j < i ; j ++ ){\n                if ( rating[j] < rating[i] ) left_less++;\n                else if ( rating[i] < rating[j] ) left_more ++;\n            }\n\n            total += right_less*left_more + right_more*left_less;\n        }\n\n        return total;\n    }\n};\n```\n```python []\nclass Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        total = 0\n        n = len(rating)\n        \n        for i in range(n):\n            right_less = 0\n            right_more = 0\n            left_less = 0\n            left_more = 0\n\n            # Count soldiers after i\n            for j in range(i + 1, n):\n                if rating[j] < rating[i]:\n                    right_less += 1\n                elif rating[j] > rating[i]:\n                    right_more += 1\n            \n            # Count soldiers before i\n            for j in range(i):\n                if rating[j] < rating[i]:\n                    left_less += 1\n                elif rating[j] > rating[i]:\n                    left_more += 1\n\n            # Calculate the total number of valid teams\n            total += right_less * left_more + right_more * left_less\n\n        return total\n\n```\n```Java []\nclass Solution {\n    public int numTeams(int[] rating) {\n        int total = 0;\n        int n = rating.length;\n        \n        for (int i = 0; i < n; i++) {\n            int rightLess = 0, rightMore = 0, leftLess = 0, leftMore = 0;\n\n            // Count soldiers after i\n            for (int j = i + 1; j < n; j++) {\n                if (rating[j] < rating[i]) rightLess++;\n                else if (rating[j] > rating[i]) rightMore++;\n            }\n            \n            // Count soldiers before i\n            for (int j = 0; j < i; j++) {\n                if (rating[j] < rating[i]) leftLess++;\n                else if (rating[j] > rating[i]) leftMore++;\n            }\n\n            // Calculate the total number of valid teams\n            total += rightLess * leftMore + rightMore * leftLess;\n        }\n\n        return total;\n    }\n}\n\n```\n```Go []\nfunc numTeams(rating []int) int {\n    total := 0\n    n := len(rating)\n    \n    for i := 0; i < n; i++ {\n        rightLess, rightMore := 0, 0\n        leftLess, leftMore := 0, 0\n        // Count soldiers after i\n        for j := i + 1; j < n; j++ {\n            if rating[j] < rating[i] {\n                rightLess++\n            } else if rating[j] > rating[i] {\n                rightMore++\n            }\n        } \n        // Count soldiers before i\n        for j := 0; j < i; j++ {\n            if rating[j] < rating[i] {\n                leftLess++\n            } else if rating[j] > rating[i] {\n                leftMore++\n            }\n        }\n        // Calculate the total number of valid teams\n        total += rightLess * leftMore + rightMore * leftLess\n    }\n\n    return total\n}\n```\n
['C++', 'Java', 'Go', 'Python3']