Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //{ Driver Code Starts
- #include <bits/stdc++.h>
- using namespace std;
- // } Driver Code Ends
- class Solution
- {
- public:
- //Function to count the number of possible triangles.
- int findNumberOfTriangles(int arr[], int n)
- {
- sort(arr, arr+n);
- int count = 0;
- for (int i = n - 1; i >= 1; i--) {
- int l = 0, r = i - 1;
- while (l < r) {
- if (arr[l] + arr[r] > arr[i]) {
- // If it is possible with a[l], a[r]
- // and a[i] then it is also possible
- // with a[l+1]..a[r-1], a[r] and a[i]
- count += r - l;
- // checking for more possible solutions
- r--;
- }
- else
- // if not possible check for
- // higher values of arr[l]
- l++;
- }
- }
- return count;
- }
- };
- //{ Driver Code Starts.
- int main()
- {
- int T;
- cin>>T;
- while(T--)
- {
- int n;
- cin>>n;
- int arr[n];
- for(int i=0; i<n; i++)
- cin>>arr[i];
- Solution ob;
- cout<<ob.findNumberOfTriangles(arr,n) <<endl;
- }
- return 0;
- }
- // } Driver Code Ends
Advertisement
Add Comment
Please, Sign In to add comment