Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given an integer array nums and an integer k. Append k unique positive integers that do not appear in nums to nums such that the resulting total sum is minimum.
Return the sum of the k integers appended to nums.
Example 1:
Input: nums = [1,4,25,10,25], k = 2 Output: 5 Explanation: The two unique positive integers that do not appear in nums which we append are 2 and 3. The resulting sum of nums is 1 + 4 + 25 + 10 + 25 + 2 + 3 = 70, which is the minimum. The sum of the two integers appended is 2 + 3 = 5, so we return 5.
Example 2:
Input: nums = [5,6], k = 6 Output: 25 Explanation: The six unique positive integers that do not appear in nums which we append are 1, 2, 3, 4, 7, and 8. The resulting sum of nums is 5 + 6 + 1 + 2 + 3 + 4 + 7 + 8 = 36, which is the minimum. The sum of the six integers appended is 1 + 2 + 3 + 4 + 7 + 8 = 25, so we return 25.
Constraints:
1 <= nums.length <= 1051 <= nums[i] <= 1091 <= k <= 108Problem summary: You are given an integer array nums and an integer k. Append k unique positive integers that do not appear in nums to nums such that the resulting total sum is minimum. Return the sum of the k integers appended to nums.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math · Greedy
[1,4,25,10,25] 2
[5,6] 6
remove-k-digits)find-all-numbers-disappeared-in-an-array)kth-missing-positive-number)maximum-number-of-integers-to-choose-from-a-range-i)maximum-number-of-integers-to-choose-from-a-range-ii)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2195: Append K Integers With Minimal Sum
class Solution {
public long minimalKSum(int[] nums, int k) {
int n = nums.length;
int[] arr = new int[n + 2];
arr[1] = 2 * 1000000000;
System.arraycopy(nums, 0, arr, 2, n);
Arrays.sort(arr);
long ans = 0;
for (int i = 0; i < n + 1 && k > 0; ++i) {
int m = Math.max(0, Math.min(k, arr[i + 1] - arr[i] - 1));
ans += (arr[i] + 1L + arr[i] + m) * m / 2;
k -= m;
}
return ans;
}
}
// Accepted solution for LeetCode #2195: Append K Integers With Minimal Sum
func minimalKSum(nums []int, k int) (ans int64) {
nums = append(nums, []int{0, 2e9}...)
sort.Ints(nums)
for i, b := range nums[1:] {
a := nums[i]
m := max(0, min(k, b-a-1))
ans += int64(a+1+a+m) * int64(m) / 2
k -= m
}
return ans
}
# Accepted solution for LeetCode #2195: Append K Integers With Minimal Sum
class Solution:
def minimalKSum(self, nums: List[int], k: int) -> int:
nums.extend([0, 2 * 10**9])
nums.sort()
ans = 0
for a, b in pairwise(nums):
m = max(0, min(k, b - a - 1))
ans += (a + 1 + a + m) * m // 2
k -= m
return ans
// Accepted solution for LeetCode #2195: Append K Integers With Minimal Sum
/**
* [2195] Append K Integers With Minimal Sum
*
* You are given an integer array nums and an integer k. Append k unique positive integers that do not appear in nums to nums such that the resulting total sum is minimum.
* Return the sum of the k integers appended to nums.
*
* Example 1:
*
* Input: nums = [1,4,25,10,25], k = 2
* Output: 5
* Explanation: The two unique positive integers that do not appear in nums which we append are 2 and 3.
* The resulting sum of nums is 1 + 4 + 25 + 10 + 25 + 2 + 3 = 70, which is the minimum.
* The sum of the two integers appended is 2 + 3 = 5, so we return 5.
* Example 2:
*
* Input: nums = [5,6], k = 6
* Output: 25
* Explanation: The six unique positive integers that do not appear in nums which we append are 1, 2, 3, 4, 7, and 8.
* The resulting sum of nums is 5 + 6 + 1 + 2 + 3 + 4 + 7 + 8 = 36, which is the minimum.
* The sum of the six integers appended is 1 + 2 + 3 + 4 + 7 + 8 = 25, so we return 25.
*
*
* Constraints:
*
* 1 <= nums.length <= 10^5
* 1 <= nums[i] <= 10^9
* 1 <= k <= 10^8
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/append-k-integers-with-minimal-sum/
// discuss: https://leetcode.com/problems/append-k-integers-with-minimal-sum/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn minimal_k_sum(nums: Vec<i32>, k: i32) -> i64 {
0
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore]
fn test_2195_example_1() {
let nums = vec![1, 4, 25, 10, 25];
let k = 2;
let result = 5;
assert_eq!(Solution::minimal_k_sum(nums, k), result);
}
#[test]
#[ignore]
fn test_2195_example_2() {
let nums = vec![1, 4, 25, 10, 25];
let k = 2;
let result = 5;
assert_eq!(Solution::minimal_k_sum(nums, k), result);
}
}
// Accepted solution for LeetCode #2195: Append K Integers With Minimal Sum
function minimalKSum(nums: number[], k: number): number {
nums.push(...[0, 2 * 10 ** 9]);
nums.sort((a, b) => a - b);
let ans = 0;
for (let i = 0; i < nums.length - 1; ++i) {
const m = Math.max(0, Math.min(k, nums[i + 1] - nums[i] - 1));
ans += Number((BigInt(nums[i] + 1 + nums[i] + m) * BigInt(m)) / BigInt(2));
k -= m;
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.