C++ || O(n3) || 104 ms || O(n2) || 4 ms || Easy to understand
Runtime: 104 ms, faster than 40.56% of C++ online submissions for Count Number of Teams.\nMemory Usage: 7.6 MB, less than 52.71% of C++ online submissions for C
***Runtime: 104 ms, faster than 40.56% of C++ online submissions for Count Number of Teams.\nMemory Usage: 7.6 MB, less than 52.71% of C++ online submissions for Count Number of Teams.***\n```\nclass Solution {\npublic:\n    int numTeams(vector<int>& r) {\n        int n = r.size();\n        int result = 0;\n        for(int i = 0 ; i < n-2 ; i++){\n            for(int j = i+1 ; j < n-1 ; j++){\n                for(int k = j+1 ; k < n ; k++){\n                    if(((r[i] < r[j]) && (r[j] < r[k])) || ((r[i] > r[j]) && (r[j] > r[k]))){\n                        result++;\n                    }\n                }\n            }\n        }\n        return result;\n    }\n};\n```\n***Runtime: 4 ms, faster than 98.59% of C++ online submissions for Count Number of Teams.\nMemory Usage: 7.5 MB, less than 93.78% of C++ online submissions for Count Number of Teams.***\n```\nclass Solution {\npublic:\n    int numTeams(vector<int>& arr) {\n        int n = arr.size();\n        int result = 0;\n        for(int i = 1 ; i < n-1 ; i++){\n            int leftSmall = 0, leftLarge = 0;\n            int rightSmall = 0, rightLarge = 0;\n            //left part\n            for(int j = 0 ; j < i ; j++){\n                if(arr[j] < arr[i]){\n                    leftSmall++;\n                }\n                if(arr[j] > arr[i]){\n                    leftLarge++;\n                }\n            }\n            //right part\n            for(int j = i+1 ; j < n ; j++){\n                if(arr[j] < arr[i]){\n                    rightSmall++;\n                }\n                if(arr[j] > arr[i]){\n                    rightLarge++;\n                }\n            }\n            result += leftSmall * rightLarge + leftLarge * rightSmall;\n        }\n        return result;\n    }\n};\n```
['C', 'C++']