Simple Java Solution comparing optimization with brute-force version
optimization version\n\n    public int numTeams(int[] rating) {\n        int res=0;\n        for(int i=1; i<rating.length-1; i++) {\n            int inc1=0, inc
1. optimization version\n```\n    public int numTeams(int[] rating) {\n        int res=0;\n        for(int i=1; i<rating.length-1; i++) {\n            int inc1=0, inc2=0;\n            int dec1=0, dec2=0;\n            for(int j=0; j<rating.length;j++) {\n                if(i>j) {\n                    if(rating[j] < rating[i]) inc1++;\n                    else dec1++;\n                }\n                if(i<j) {\n                    if(rating[j] > rating[i]) inc2++;\n                    else dec2++;\n                }\n            }\n            res = res + (inc1*inc2) + (dec1*dec2);\n        }\n        return res;\n    }\n``` \n\n2. brute-force version\n```\n    public int numTeams(int[] rating) {\n        int res=0;\n        for(int i=0; i<rating.length-2; i++) {\n            for(int j=i+1; j<rating.length-1; j++) {\n                if(rating[i] < rating[j]) {\n                    for(int k=j+1; k<rating.length; k++) {\n                        if(rating[j] < rating[k]) {\n                            res++;\n                        }\n                    }\n                }\n                if(rating[i] > rating[j]) {\n                    for(int k=j+1; k<rating.length; k++) {\n                        if(rating[j] > rating[k]) {\n                            res++;\n                        }\n                    }\n                }\n            }\n        }\n        return res;\n    }\n```
['Java']