EaglePB2's Competitive Programming Writeups
  • Home Page
  • Why Python?
  • Why 10^9 + 7?
  • General Formatting Title
  • šŸ‡²šŸ‡¾CodeNection
    • 2021
      • Closed Cateogry
        • Attend Talks
        • Distant Relatives
        • Concert
        • Mamak
        • Fair Contest
      • Open Preliminary Round
        • f(Aibohphobia)^-1
        • Did they cheat?
        • Semester Breaks
      • Open Final Round
        • Ways 1
        • Circular Campus
        • A joke
        • 🄰Last year when life was better
        • Thank You Pizza
    • 2023
      • Test Round
        • Codey and Alphabetical Scoring
        • Codey and Binary Guesser
      • Preliminary Round
        • Codey and CodeNection
        • Codey and Hide-and-Seek
        • Codey and Math
        • Codey and Textbooks
        • Codey and Money
        • Codey and Team Selection
        • Codey and Painted Tree
        • Codey and Number Grid
        • Codey and Crimes
      • Final Round
        • Codey and CodeNection 2
        • Codey and Connection
        • Codey and Schedule
        • Codey and School Supplies
        • Codey and Zombies
        • Codey and Sightseeing
        • Codey and Apples
        • Codey and Facto
        • Codey and Zoey
    • 2024
      • Test Round
        • Codey and Sunday
        • Codey and Takoyaki
      • Preliminary Round
        • Codey and CodeNection
        • Codey and Pebbles
        • Codey and Spam
        • Codey and Coins
        • Codey and Rectangles
        • Codey and Manuscript
        • Codey and Recipes
        • Codey and Toy Kingdom
        • Codey and Peaks
      • Final Round
        • Codey and Exit
        • Codey and Gardening
        • Codey and Symbol
        • Codey and Rectangles 2
        • Codey and Jutsu
        • Codey and Toy Kingdom 2
        • Codey and Speeches
  • ABaKoDa
    • 2023
      • Round 1
        • Problem Letters
        • Problem Statistics
        • Rankings Order
        • Rankings Search
      • Round 2
        • Abakoda Letters
        • Borrowed Words
        • Kensorship
        • Duel Languages
  • Meta Coding Competitions
    • 2011
      • Qualification Round
        • Double Squares
        • Peg Game
        • Studious Student
      • Round 1A
        • Diversity Number
        • Turn on the Lights
        • Wine Tasting
      • Round 1B
        • Chess 2
        • Diminishing Circle
        • Slot Machine Hacker
      • Round 1C
        • N-Factorful
        • Polynomial Factoring
        • Risky Slide
      • Round 2
        • Bonus Assignments
        • Scott's New Trick
        • Studious Student II
      • Final Round
        • Alien Game
        • Party Time
        • Safest Place
  • EaglePB2's Special
    • Hong Kong Identity card
    • Cycle Prefix Averages
    • Word Squares
Powered by GitBook
On this page
  • Question
  • Input Format
  • Constraints
  • Output Format
  • Sample Inputs:
  1. CodeNection
  2. 2024
  3. Preliminary Round

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

1≤n,x≤1051 \le n, x \le 10^51≤n,x≤105

Output Format

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

Sample Inputs:

Input

5 16

Output

6

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:

n, m = map(int, input().strip().split())

total_spam = 0
lines = 0
steps = 1

while steps <= n:
    total_spam += steps
    lines += 1
    if total_spam >= m:
        break
    steps += 1

if total_spam < m:
    steps = n - 1
    while total_spam < m and steps > 0:
        total_spam += steps
        lines += 1
        steps -= 1

print(lines)

The time complexity should be O(n).

PreviousCodey and PebblesNextCodey and Coins

Last updated 2 months ago

šŸ‡²šŸ‡¾