3Sum Smaller


I have a question for the twoSumSmaller:
private int twoSumSmaller(int[] nums, int startIndex, int target) { int sum = 0; for (int i = startIndex; i < nums.length  1; i++) { int j = binarySearch(nums, i, target  nums[i]); sum += j  i; } return sum; }
Why we use binary search for target  nums[i] from index i, rather than i+1? I think we need to find the right most position after i, then it is
int j = binarySearch(nums, i+1, target  nums[i]);
, but the answer is wrong. Thx!

@anku There is no need to consider the duplicates of numbers , only index i, j, k are different is enough.

def threeSumSmaller(self, nums, target): """ :type nums: List[int] :type target: int :rtype: int """ result = 0 nums.sort() for i in range(len(nums)  2): l, r = i + 1, len(nums)  1 while l < r: s = nums[i] + nums[l] + nums[r] if s < target: result += r  l l += 1 else: r = 1 return result