[LeetCode] Determine the Minimum Sum of a k-avoiding Array

2829. Determine the Minimum Sum of a k-avoiding Array

You are given two integers, n and k.

An array of distinct positive integers is called a k-avoiding array if there does not exist any pair of distinct elements that sum to k.

Return the minimum possible sum of a k-avoiding array of length n.

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public:
int minimumSum(int n, int k) {
unordered_set<int> us;
int res = 0;
for(int i = 1; us.size() < n; i++) {
if(us.count(k - i)) continue;
us.insert(i);
res += i;
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/08/20/PS/LeetCode/determine-the-minimum-sum-of-a-k-avoiding-array/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.