Counting Triangles Time : Space : 12345678910111213141516int Solution::nTriang(vector<int> &A) { sort(begin(A), end(A)); long long res = 0, mod = 1e9 + 7; for(int i = A.size() - 1; i >= 2; i--) { int l = 0, r = i - 1; while(l < r) { int sum = A[l] + A[r]; if(sum > A[i]) { res = (res + (r - l) % mod) % mod; r--; } else l++; } } return res;}