[LeetCode] Maximum Valid Pair Sum

3979. Maximum Valid Pair Sum

You are given an integer array nums of length n and an integer k.

A pair of indices (i, j) is called valid if:

  • 0 <= i < j < n
  • j - i >= k

Return the maximum value of nums[i] + nums[j] among all valid pairs.

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
int maxValidPairSum(vector<int>& nums, int k) {
int res = 0, n = nums.size();
for(int i = n - k - 1, ma = 0; i >= 0; i--) {
ma = max(ma, nums[i+k]);
res = max(res, nums[i] + ma);
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2026/09/04/PS/LeetCode/maximum-valid-pair-sum/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.