Java solution with explanation
Here,we are traversing the array from 1st index to 2nd last index. Inside the loop ,we are tracking the elements which are greater or less on each side.\nSince 
Here,we are traversing the array from 1st index to 2nd last index. Inside the loop ,we are tracking the elements which are greater or less on each side.\nSince two pattern selection are possible,so the no. of elements less than the current element and greater than the current will give no. of accending pattern. Similarly the descending one!\n\n```\nclass Solution {\n    public int numTeams(int[] rating) {\n        int n=rating.length;\n        \n        int total=0;\n        for(int i=1;i<n-1;i++){\n            int leftless=0; // for accending pattern\n            int rightgreater=0; // for accending pattern\n            \n            int leftgreater=0; // for descending pattern\n            int rightless=0;  //for descending pattern\n            \n            for(int j=i-1;j>=0;j--){\n                if(rating[i]>rating[j]){\n                    leftless++;\n                }\n                else{\n                    leftgreater++;\n                }\n            }\n            for(int j=i+1;j<n;j++){\n                if(rating[i]>rating[j]){\n                    rightless++;\n                }else{\n                    rightgreater++;\n                }\n            }\n            total+= leftless*rightgreater + rightless*leftgreater; // no of counts possible from the ith index.\n        }\n        return total;\n    }\n}\n```\n **Hope you understand! Do upvote if you find it helpful!**
['Java']