package com.interview.array;
import java.util.Arrays;
/**
* Given an array of n integers nums and a target, find the number of index triplets i, j, k
* with 0 <= i < j < k < n that satisfy the condition nums[i] + nums[j] + nums[k] < target.
*
* https://leetcode.com/problems/3sum-smaller/
*/
public class ThreeSumSmallerThanTarget {
public int threeSumSmaller(int[] nums, int target) {
if (nums.length < 3) {
return 0;
}
Arrays.sort(nums);
int count = 0;
for (int i = 0; i < nums.length; i++) {
int j = i + 1;
int k = nums.length - 1;
while (j < k) {
if (nums[i] + nums[j] + nums[k] >= target) {
k--;
} else {
count += k - j;
j++;
}
}
}
return count;
}
}