#2834
Find the Minimum Possible Sum of a Beautiful Array
expert · 1080 · lc medium +32 · verified · 35.2% accepted · 317 likes · top 13%
Description
Two positive integers n and target are given.
An array nums is beautiful when:
- nums.length == n.
- All elements are pairwise distinct positive integers.
- No two distinct indices i and j satisfy nums[i] + nums[j] == target.
Return the minimum possible sum of a beautiful array modulo 109 + 7.
Example 1:
Input: n = 2, target = 3
Output: 4
Explanation: We can see that nums = [1,3] is beautiful.
- The array nums has length n = 2.
- The array nums consists of pairwise distinct positive integers.
- There doesn't exist two distinct indices, i and j, with nums[i] + nums[j] == 3.
It can be proven that 4 is the minimum possible sum that a beautiful array could have.
Example 2:
Input: n = 3, target = 3
Output: 8
Explanation: We can see that nums = [1,3,4] is beautiful.
- The array nums has length n = 3.
- The array nums consists of pairwise distinct positive integers.
- There doesn't exist two distinct indices, i and j, with nums[i] + nums[j] == 3.
It can be proven that 8 is the minimum possible sum that a beautiful array could have.
Example 3:
Input: n = 1, target = 1
Output: 1
Explanation: We can see, that nums = [1] is beautiful.
Code
1
2
3