Determine the Minimum Sum of a k-avoiding Array - Problem
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.
Input & Output
Example 1 — Basic Case
$
Input:
n = 3, k = 4
›
Output:
8
💡 Note:
The k-avoiding array [1,2,5] has the minimum sum. We skip 3 and 4 because 1+3=4 and 2+2=4 (but we need distinct elements, so we avoid the pair (1,3)).
Example 2 — Large k
$
Input:
n = 2, k = 6
›
Output:
3
💡 Note:
Since k=6 is large, we can take the first n=2 numbers: [1,2]. Their sum is 1+2=3, and 1+2≠6 so it's k-avoiding.
Example 3 — Small k
$
Input:
n = 2, k = 3
›
Output:
4
💡 Note:
We cannot use [1,2] because 1+2=3=k. So we take [1,3] with sum 4, avoiding the forbidden pair.
Constraints
- 1 ≤ n, k ≤ 50
Visualization
Tap to expand
Understanding the Visualization
1
Input
Given n=3, k=4 - need 3 distinct positive integers
2
Constraint
No two elements should sum to k=4
3
Output
Minimum possible sum of such array
Key Takeaway
🎯 Key Insight: Greedily select smallest numbers while checking for forbidden pairs
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code