DEV Community

dev.to staff
dev.to staff

Posted on

7

Daily Challenge #284 - Remove K

Task

Given a number k, find the smallest integer n such that if any k elements are removed from the set {1, 2, ..., n}, one can still find k distinct numbers among the remaining elements with sum of n.

[input] integer k
The number of elements to be removed.

[set] 2 ≤ k ≤ 10000

[output] an integer
The smallest value of n.

Example

For k = 2, the output should be 7.

The initial set is {1, 2, 3, 4, 5, 6, 7}.

No matter what 2 elements you remove, (1, 6), (2, 5), or (3, 4) will sum up to 7.

Why isn't the result 6? Because: In set {1, 2, 3, 4, 5, 6}, if we remove 2 elements (1,2), or (4,5) the remaining elements do not contain 2 elements that sum up to 6.

Tests

removeK(2)

removeK(3)

removeK(10)

removeK(25)

Good luck!


This challenge comes from myjinxin2015 on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!

DevCycle image

Fast, Flexible Releases with OpenFeature Built-in

Ship faster on the first feature management platform with OpenFeature built-in to all of our open source SDKs.

Start shipping

Top comments (0)

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

👋 Kindness is contagious

If this **helped, please leave a ❤️ or a friendly comment!

Okay