-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGFG_CountNumOfPossibleTriangles.cpp
71 lines (61 loc) · 1.34 KB
/
GFG_CountNumOfPossibleTriangles.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
https://practice.geeksforgeeks.org/problems/count-possible-triangles-1587115620/1/#
Count the number of possible triangles
*/
// { 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)
{
int ans=0;
sort(arr, arr+n);
int l, r;
// for(int i=n-1; i>=2; i--)
// {
// l=0;
// r=i-1;
// while(l<r)
// {
// if(arr[l] + arr[r] > arr[i])
// {
// ans+=r-l;
// r--;
// }
// else
// l++;
// }
// }
for(int i=0; i<n-2; i++)
{
for(int j=i+1; j<n-1; j++)
{
int th = j+1;
while(th<n && arr[i]+arr[j] > arr[th]) ++th;
ans += th-1-j;
}
}
return ans;
}
};
// { 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