Find the Minimum Possible Sum of a Beautiful Array
MediumUpdated: Aug 2, 2025
Practice on:
Problem
You are given positive integers n and target.
An array nums is beautiful if it meets the following conditions:
nums.length == n.numsconsists of pairwise distinct positive integers.- There doesn't exist two distinct indices,
iandj, in the range[0, n - 1], such thatnums[i] + nums[j] == target.
Return _theminimum possible sum that a beautiful array could have modulo _10^9 + 7.
Examples
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.
Constraints
1 <= n <= 10^91 <= target <= 10^9
Solution
Method 1 – Greedy Construction
Intuition
To minimize the sum, pick the smallest possible distinct positive integers, but avoid pairs that sum to target. The best way is to pick numbers from 1 upwards, skipping any number that would form a forbidden pair with a previously chosen number.
Approach
- Initialize an empty set to track chosen numbers and a variable for the sum.
- Iterate from 1 upwards, for each number:
- If
target - xis not already chosen, pickx. - Add
xto the sum and the set. - Stop when
nnumbers are chosen.
- If
- Return the sum modulo
10^9 + 7.
Code
C++
class Solution {
public:
int minimumPossibleSum(int n, int target) {
const int MOD = 1e9+7;
set<int> used;
long long sum = 0;
for(int x=1, cnt=0; cnt<n; ++x) {
if(used.count(target-x)) continue;
sum = (sum + x) % MOD;
used.insert(x);
++cnt;
}
return sum;
}
};
Go
func minimumPossibleSum(n int, target int) int {
const MOD = 1_000_000_007
used := make(map[int]bool)
sum, cnt := 0, 0
for x := 1; cnt < n; x++ {
if used[target-x] { continue }
sum = (sum + x) % MOD
used[x] = true
cnt++
}
return sum
}
Java
class Solution {
public int minimumPossibleSum(int n, int target) {
final int MOD = 1_000_000_007;
Set<Integer> used = new HashSet<>();
long sum = 0;
for(int x=1, cnt=0; cnt<n; ++x) {
if(used.contains(target-x)) continue;
sum = (sum + x) % MOD;
used.add(x);
++cnt;
}
return (int)sum;
}
}
Kotlin
class Solution {
fun minimumPossibleSum(n: Int, target: Int): Int {
val MOD = 1_000_000_007
val used = mutableSetOf<Int>()
var sum = 0L; var cnt = 0
var x = 1
while (cnt < n) {
if ((target-x) in used) { x++; continue }
sum = (sum + x) % MOD
used.add(x)
cnt++
x++
}
return sum.toInt()
}
}
Python
class Solution:
def minimumPossibleSum(self, n: int, target: int) -> int:
MOD = 10**9+7
used = set()
ans = 0
x = 1
while n:
if target-x not in used:
ans = (ans + x) % MOD
used.add(x)
n -= 1
x += 1
return ans
Rust
impl Solution {
pub fn minimum_possible_sum(n: i32, target: i32) -> i32 {
let mut used = std::collections::HashSet::new();
let mut sum = 0i64;
let mut cnt = 0;
let mut x = 1;
while cnt < n {
if !used.contains(&(target-x)) {
sum = (sum + x as i64) % 1_000_000_007;
used.insert(x);
cnt += 1;
}
x += 1;
}
sum as i32
}
}
TypeScript
class Solution {
minimumPossibleSum(n: number, target: number): number {
const MOD = 1_000_000_007;
const used = new Set<number>();
let sum = 0, cnt = 0, x = 1;
while(cnt < n) {
if(!used.has(target-x)) {
sum = (sum + x) % MOD;
used.add(x);
cnt++;
}
x++;
}
return sum;
}
}
Complexity
- ⏰ Time complexity:
O(n), as we pick n numbers. - 🧺 Space complexity:
O(n), for the set of used numbers