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
k-avoiding Array: Minimum Sum ProblemInput: n=3, k=4Need 3 distinctpositive integersConstraint: No pair sums to 41 + 3 = 4 ✗2 + 2 = 4 ✗Solution Process:1✓ Safe2✓ Safe3✗ 1+3=44✗ Skip5✓ SafeGreedy Selection: Take smallest valid numbersOptimal Array: [1, 2, 5]Sum = 1 + 2 + 5 = 8No pairs sum to k=4: ✓ 1+2=3 ✓ 1+5=6 ✓ 2+5=7Minimum Sum: 8
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
Asked in
Google 15 Meta 12 Amazon 8
3.2K Views
Medium Frequency
~15 min Avg. Time
145 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen