-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProblem-1.java
45 lines (34 loc) · 893 Bytes
/
Problem-1.java
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
/**
* @author AkashGoyal
*/
/**
--------------------- Problem----------->> Count triplet with sum smaller than a given value
Problem Link :- https://practice.geeksforgeeks.org/problems/count-triplets-with-sum-smaller-than-x5549/1
*/
class Solution
{
long countTriplets(long arr[], int n,int sum)
{
Arrays.sort(arr);
long count=0;
for(int i=0;i<n-2;i++)
{
int left=i+1;
int right=n-1;
while(left<right)
{
long total=arr[i]+arr[left]+arr[right];
if(total<(long)sum)
{
count+=right-left;
left++;
}
else
{
right--;
}
}
}
return count;
}
}