# Solving Leetcode Interviews in Seconds with AI: Append K Integers With Minimal Sum


	# Introduction
	In this blog post, we will explore how to solve the LeetCode problem "2195" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like [Chatmagic](https://www.chatmagic.app), we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

	# Problem Statement
	> 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 <= 105 1 <= nums[i] <= 109 1 <= k <= 108  

	# Explanation
	Here's a breakdown of the approach, complexity, and the Python code:

*   **Key Idea:** The most efficient way to minimize the sum is to append the smallest possible unique positive integers that are not already present in `nums`. We iterate, keeping track of the smallest missing integer and adding it until we've appended `k` integers.
*   **Leveraging Sorted Input:** Sorting the input array `nums` allows us to quickly identify the missing positive integers in a systematic manner. We can skip over duplicates efficiently too.
*   **Sum Calculation:** Once we identify the sequence of integers to be appended, we can compute their sum using the arithmetic series formula for efficient summation.

*   **Complexity:** O(n log n) time for sorting, O(1) space (excluding the space used to store the sorted array in place).

	
	# Code
	```python
	def minimalKSum(nums, k):
    nums = sorted(list(set(nums)))  # Remove duplicates and sort
    appended_sum = 0
    last_num = 0  # Initialize the last number seen (before the start)

    for num in nums:
        if num > last_num + 1:
            diff = num - last_num - 1
            if k >= diff:
                appended_sum += (last_num + 1 + num - 1) * diff // 2
                k -= diff
                last_num = num
            else:
                appended_sum += (last_num + 1 + last_num + k) * k // 2
                k = 0
                break
        last_num = num

    if k > 0:
        appended_sum += (last_num + 1 + last_num + k) * k // 2

    return appended_sum
	```
			
