[LeetCode] Find the Minimum Possible Sum of a Beautiful Array

2834. Find the Minimum Possible Sum of a Beautiful Array

You are given positive integers n and target.

An array nums is beautiful if it meets the following conditions:

  • nums.length == n.
  • nums consists of pairwise distinct positive integers.
  • There doesn’t exist two distinct indices, i and j, in the range [0, n - 1], such that nums[i] + nums[j] == target.

Return the minimum possible sum that a beautiful array could have.

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