---
# System prepended metadata

title: 1099. Two Sum Less Than K
tags: [Leetcode, Two Pointers, Easy]

---

# 1099. Two Sum Less Than K
###### tags: `Leetcode` `Easy` `Two Pointers`
Link: https://leetcode.com/problems/two-sum-less-than-k/
## 思路
### 思路一 Two Pointers 
Time Complexity: $O(NlogN)$ 
Space Complexity: from $O(logn)$ to $O(n)$, depending on the implementation of the sorting algorithm.
先sort然后常规two pointers
### 思路二 
## Code
### 思路一 
```java=
class Solution {
    public int twoSumLessThanK(int[] nums, int k) {
        Arrays.sort(nums);
        int left = 0;
        int right = nums.length-1;
        int ans = -1;
        while(left<right){
            int sum = nums[left]+nums[right];
            if(sum < k){
                ans = Math.max(sum, ans);
                left++;
            }
            else{
                right--;
            }
        }
        return ans;
    }
}
```