C++ Simple logic with detailed explanation
Explanation:\nWhile going through loop from 2nd element to n-1 we just trying to find out how many elements are greater or less than at the left side of the ele
**Explanation:**\nWhile going through loop from 2nd element to n-1 we just trying to find out how many elements are greater or less than at the left side of the element and how many element are less or greater than the element at his right side.\nAnd calculate the teams by using this logic.\n```teams += (leftLess*rightGreater) + (leftGreater*rightLess)```\n\n**Intiution behind the logic**\n\tFor better understanding Let\'s take an example :   [2,5,3,4,1]\n\t\nSo if we wanted to make a team **including 3** then we just need to find the **one less or greater element at the left of 3 and one less or greater element at the right of 3** as mention in the problem statement:\n(consider rating[j] as a current element here current element is 3)\n\t\t(```(rating[i] < rating[j] < rating[k]) or (rating[i] > rating[j] > rating[k]) where (0 <= i < j < k < n)```)\n\t\t\tHere if we count all the parameters written above for ```3``` then \n\t\t\tleftLess = 1;\n\t\t\tleftGreater = 1;\n\t\t\trightLess = 1;\n\t\t\trightGreater = 1;\n\t\t\nFor 3 we have 1 element leftLess and 1 element rightGreater (2 < 3 < 4) so we can make a team and also we have 1 element rightLess and 1 element leftGreater (5 > 3 > 1) and this is also a valid team so now we have 2 teams in which 3 is member.\n\nSo ```(leftLess*rightGreater)``` this term is for if we have more than 1 less element at left and greater element at right of the element (Because all these element can make a seperate team with current element) and ```(leftGreater*rightLess)``` for this term if we have more than 1 greater element at the left of the current element and 1 less element at the right of the currrent element (These can also make a seperate team including current element)  and at last i am adding both terms because both ways are valid to make a team.\n\nSo by using this logic we calculate for all the element and added in variable and at last return this.\n```\n    int numTeams(vector<int>& rating) {\n        int n = rating.size();\n        int teams = 0;\n        for(int u=1;u<n-1;u++){\n            int leftLess=0,leftGreater=0,rightLess=0,rightGreater=0;\n            for(int j=0;j<u;j++){\n                if(rating[j]>rating[u])leftGreater++;\n                else leftLess++;\n            }\n            for(int j=u+1;j<n;j++){\n                if(rating[j]>rating[u])rightGreater++;\n                else rightLess++;\n            }\n            teams += (leftLess*rightGreater) + (leftGreater*rightLess);\n        }\n        return teams;\n    }\n```\nTime comlexity : O(n^2)
['C', 'C++']