C# Solution for Count Number of Teams Problem
Intuition\n Describe your first thoughts on how to solve this problem. \nThe intuition behind the optimized solution is to avoid a brute-force check of all trip
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe intuition behind the optimized solution is to avoid a brute-force check of all triplets, which would be computationally expensive (O(n^3)). Instead, we focus on breaking down the problem by leveraging the relationships between the soldiers\u2019 ratings. By counting how many soldiers before and after a given soldier have ratings lower or higher, we can efficiently determine the number of valid teams.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1.\tCounting Smaller and Larger Ratings:\n\t\u2022\tFor each soldier considered as the middle one (j), count how many soldiers to the left have a smaller rating (leftLess) and how many have a larger rating (leftGreater).\n\t\u2022\tSimilarly, count how many soldiers to the right have a smaller rating (rightLess) and how many have a larger rating (rightGreater).\n2.\tForming Teams:\n\t\u2022\tFor each soldier j, calculate the number of increasing teams as leftLess * rightGreater.\n\t\u2022\tCalculate the number of decreasing teams as leftGreater * rightLess.\n3.\tSumming the Results:\n\t\u2022\tSum the valid increasing and decreasing teams for all soldiers to get the total number of valid teams.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\u2022\tO(n^2):\n\t\u2022\tThe outer loop runs n times.\n\t\u2022\tFor each iteration of the outer loop, the two inner loops each run up to n-1 times, resulting in a total of O(n^2) operations.\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\u2022\tO(1):\n\t\u2022\tThe algorithm uses a fixed number of integer variables for counting and does not depend on the size of the input array. Thus, the space complexity is constant.\n\n# Code\n```\npublic class Solution {\n    public int NumTeams(int[] rating) {\n        int n = rating.Length;\n        int count = 0;\n        \n        for (int j = 0; j < n; j++) {\n            int leftLess = 0, leftGreater = 0;\n            int rightLess = 0, rightGreater = 0;\n            \n            // Count soldiers with lower and higher ratings before soldier j\n            for (int i = 0; i < j; i++) {\n                if (rating[i] < rating[j]) {\n                    leftLess++;\n                } else if (rating[i] > rating[j]) {\n                    leftGreater++;\n                }\n            }\n            \n            // Count soldiers with lower and higher ratings after soldier j\n            for (int k = j + 1; k < n; k++) {\n                if (rating[k] < rating[j]) {\n                    rightLess++;\n                } else if (rating[k] > rating[j]) {\n                    rightGreater++;\n                }\n            }\n            \n            // Calculate valid increasing teams with j as the middle soldier\n            count += leftLess * rightGreater;\n            // Calculate valid decreasing teams with j as the middle soldier\n            count += leftGreater * rightLess;\n        }\n        \n        return count;\n    }\n}\n```
['C#']