Leetcode Problem 2195. Append K Integers With Minimal Sum

2195. Append K Integers With Minimal Sum

Leetcode Solutions

Using Sorting and Sum of Arithmetic Progression

  1. Sort the input array nums in ascending order.
  2. Initialize sum to 0 and current to 1 (the smallest positive integer).
  3. Iterate through the sorted nums and for each number num in nums: a. If current is less than num, calculate the number of missing elements between current and num and update sum and k accordingly. b. If k becomes 0, break out of the loop as we have found enough integers. c. Update current to num + 1.
  4. If k is still greater than 0 after the loop, add the sum of the next k integers starting from current to sum.
  5. Return sum.
UML Thumbnail

Using Set to Track Unique Integers

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...