Recursion & O(N^2) Solution
\nApproach 1 : Recursion (Include/Exclude Fashion)\n\nTLE Solution\n\nclass Solution {\npublic:\n    int count;\n\t\n     bool criteria_fulfilled(const vector<i
\n**Approach 1 : Recursion (Include/Exclude Fashion)**\n\n*TLE Solution*\n```\nclass Solution {\npublic:\n    int count;\n\t\n     bool criteria_fulfilled(const vector<int>& result) {\n        return (result[0] < result[1] && result[1] < result[2]) ||\n               (result[0] > result[1] && result[1] > result[2]);\n    }\n    \n     void solve(const vector<int>& rating, int curr_idx, int n, vector<int>& result) {\n        if (result.size() == 3) {\n            if (criteria_fulfilled(result)) {\n                count++;\n            }\n            return; \n        }\n\n        if (curr_idx > n) {\n            return;\n        }\n\n        // Include the current rating in the result\n        result.push_back(rating[curr_idx]);\n        solve(rating, curr_idx + 1, n, result);\n        result.pop_back(); // Backtrack\n\n        // Exclude the current rating and move to the next\n        solve(rating, curr_idx + 1, n, result);\n    }\n\n    \n    int numTeams(vector<int>& rating) {\n        int n = rating.size() - 1;\n        vector<int> result;\n        count = 0;\n        solve(rating , 0, n, result);\n        \n        return count;\n    }\n};\n\n```\n\n**Approach 2 :  Simple Combination Approach** \n\n**Basic Idea :**\nKeeping an element fixed, and looking for count of smaller elements on left side , and larger elements on right side and vice-versa. This problem now boils down to finding the number of combinations.\n\n```\nclass Solution {\npublic:\n    int numTeams(vector<int>& rating) {\n        int n = rating.size();\n\n        int teams = 0;\n\n        for(int j = 1; j < n-1; j++) {\n\n            int countSmallerLeft = 0;\n            int countLargerLeft  = 0;\n            int countSmallerRight = 0;\n            int countLargerRight = 0;\n\n            for(int i = 0; i < j; i++) {\n                if(rating[i] < rating[j]) {\n                    countSmallerLeft++;\n                } else if(rating[i] > rating[j]) {\n                    countLargerLeft++;\n                }\n            }\n\n            for(int k = j+1; k < n; k++) {\n                if(rating[j] < rating[k]) {\n                    countLargerRight++;\n                } else if(rating[j] > rating[k]) {\n                    countSmallerRight++;\n                }\n            }\n\n            teams += (countLargerLeft * countSmallerRight) + (countSmallerLeft * countLargerRight);\n\n\n        }\n\n        return teams;\n    }\n};\n\n```\n
['Array', 'Recursion', 'C']