HackerRank C++ Solution – Tower Breakers

Опубликовано: 11 Март 2026
на канале: JustWriteTheCode
2,481
15

I JustWriteTheCode of the solution to the "Tower Breakers" problem present on HackerRank (1 Week Preparation Kit - Day 3).

Programming Language: C++.

Problem:
Two players are playing a game of Tower Breakers! Player 1 always moves first, and both players always play optimally. The rules of the game are as follows:
Initially there are n towers.
Each tower is of height m.
The players move in alternating turns.
In each turn, a player can choose a tower of height x and reduce its height to y, where y belongs to [1,x) and y evenly divides x.
If the current player is unable to make a move, they lose the game.

Given the values of n and m, determine which player will win. If the first player wins, return 1. Otherwise, return 2.