Count triplets with sum smaller than a given value

Given an array of distinct integers and a sum value. Find count of triplets with sum smaller than given sum value. Expected Time Complexity is O(n2).

Examples:

Input : arr[] = {-2, 0, 1, 3}
sum = 2.
Output : 2
Explanation : Below are triplets with sum less than 2
(-2, 0, 1) and (-2, 0, 3)

Input : arr[] = {5, 1, 3, 4, 7}
sum = 12.
Output : 4
Explanation : Below are triplets with sum less than 4
(1, 3, 4), (1, 3, 5), (1, 3, 7) and
(1, 4, 5)

Java Code:-
class test{
public static void main(String[] args){
int[] a={5, 1, 3, 4, 7};
int len=a.length;
int flag=12;
int cnt=0;
for(int i=0;i<len;i++){
for(int j=i+1;j<len;j++){
for(int k=j+1;k<len;k++){
int sum=a[i]+a[j]+a[k];
if(sum<flag)
cnt++;
}
}
}
System.out.println(cnt);
}
}