> For the complete documentation index, see [llms.txt](https://junnie.gitbook.io/nine-chapter/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://junnie.gitbook.io/nine-chapter/7.two-pointers/609two-sum-less-than-or-equal-to-target.md).

# 609.Two Sum - Less than or equal to target

## 1.Description(Medium)

Given an array of integers, find how many pairs in the array such that their sum is`less than or equal to`a specific target number. Please return the number of pairs.

**Example**

Given nums =`[2, 7, 11, 15]`, target =`24`.\
Return`5`.\
2 + 7 < 24\
2 + 11 < 24\
2 + 15 < 24\
7 + 11 < 24\
7 + 15 < 25

## 2.Code

跟two sum很像，一旦发现sum\<target就直接把tail-head全部加进去，再head++;

```
 public int twoSum5(int[] nums, int target) {
        if(nums==null || nums.length<2){
            return 0;
        }
        int result=0;
        int head=0;
        int tail=nums.length-1;
        Arrays.sort(nums);
        while(head<tail){
            int sum=nums[head]+nums[tail];
            if(sum>target){
                tail--;
            }else{
                result=result+(tail-head);
                head++;
            }
        }
        return result;

    }
```
