Codey and Spam

https://www.hackerrank.com/contests/codenection-2024-preliminary-round-closed-category/challenges/cn24-3

Question

Codey is too excited for CodeNection 2024! It wanted to spam a series of lines arranged in a triangle on the Discord server. The height (vertex) of the triangle is n, and it contains 2n-1 lines.

Example of n = 3 containing 5 lines:

CODENECTION
CODENECTIONCODENECTION
CODENECTIONCODENECTIONCODENECTION
CODENECTIONCODENECTION
CODENECTION

However, Zoey will kick Codey out immediately if it spam at least x CODENECTION word consecutively. How many lines can Codey spam before Zoey kicks it out of the server?

Input Format

The first line contains two integers, n and x , where n represents the height of the triangle and x represents the maximum number of consecutive CODENECTION allowed before Zoey kicks Codey out.

Constraints

1n,x1051 \le n, x \le 10^5

Output Format

Output an integer representing the maximum number of lines Codey can spam before getting kicked out.

Sample Inputs:

Input

Output

Explanation

Codey can spam a maximum of 6 lines, and since 1 + 2 + 3 + 4 + 5 + 4 >= 16, Codey will get kicked out by Zoey after that.


Solution - Triangle Half

Break this into 2 conditions:

  1. From 1 to n

  2. From n-1 to 1

My logic is first check if the sum of 1 to n is >= x. If it does, just add the numbers 1 by 1 until it >= x.

Otherwise, subtract the n with x, and do it reversely.

Here's my solution:

The time complexity should be O(n).

Last updated