Stick Game Problem: Solution and Code in Python, C, Java
Stick game on an n x m grid: derive why min(n, m) decides the winner, verify all three sample inputs, and get clean code in Python, C, and Java.
Given a grid of n horizontal and m vertical sticks, the stick game asks which player wins under optimal play. The answer reduces to one comparison: check whether min(n, m) is odd or even.
The full solution is five lines of code. The derivation behind it is the useful part, because the same pattern of reasoning applies to a family of grid-based competitive programming problems.
What the Stick Game Looks Like
The game runs on a rectangular grid formed by n horizontal sticks and m vertical sticks. Where these sticks cross, they create intersection points. An n × m grid produces exactly n × m intersection points.
Two players alternate turns; the first player moves first. On each turn, the current player picks any remaining intersection point and removes all sticks that pass through it. Every intersection lies on exactly one horizontal stick and one vertical stick, so each move removes:
- The entire horizontal stick for that row, which also eliminates every other intersection on that row.
- The entire vertical stick for that column, which also eliminates every other intersection on that column.
In a 3 × 3 grid, picking the top-left intersection removes the top-row stick and the left-column stick. That leaves four remaining intersections arranged in a 2 × 2 pattern.
A player loses when no intersection points remain at the start of their turn.
This falls under the category of problems studied in combinatorial game theory: two players alternate moves on a finite state, and the player who faces an empty state loses.
Why the Total Move Count Is Always min(n, m)
Here is the core derivation. Trace the game state step by step:
- Before move 1:
nrows,mcolumns,n × mintersections. - After move 1: one row and one column are gone. Now
n - 1rows andm - 1columns remain, with(n-1) × (m-1)intersections. - After move 2:
n - 2rows andm - 2columns remain. - After move k:
n - krows andm - kcolumns remain. - The game ends when either
n - k = 0orm - k = 0, whichever comes first. - That happens at
k = min(n, m).
Every move removes exactly one row and one column. There is no branching, no choice that changes the total. The game always ends in exactly min(n, m) moves.
Parity then determines the winner. Move 1 is Arun Gupta’s, move 2 is Mani Iyer’s, alternating from there:
- If
min(n, m)is odd: Arun Gupta makes the last move. Mani Iyer faces an empty grid and loses. - If
min(n, m)is even: Mani Iyer makes the last move. Arun Gupta faces an empty grid and loses.
No game tree. No recursion. One comparison.
This is why “both players play optimally” is a red herring for this problem. Every intersection is equivalent in terms of game outcome. There is no move that improves or worsens either player’s position. The result is determined the moment n and m are known.
Verified Sample Walkthroughs
The derivation above predicts all three provided sample outputs. Verify each:
Sample 1: n = 2, m = 2
min(2, 2) = 2(even)- Predicted winner: Mani Iyer
- Sample output: Mani Iyer ✓
Walkthrough: Arun picks any intersection, say the top-left. That removes the top row and left column, leaving only the bottom-right intersection. Mani takes it, removing the remaining sticks. Arun faces an empty grid and loses.
Sample 2: n = 2, m = 3
min(2, 3) = 2(even)- Predicted winner: Mani Iyer
- Sample output: Mani Iyer ✓
The narrower dimension is 2 horizontal sticks. The game ends in 2 moves regardless of which column each player targets.
Sample 3: n = 3, m = 3
min(3, 3) = 3(odd)- Predicted winner: Arun Gupta
- Sample output: Arun Gupta ✓
Three moves in a symmetric grid. Move 3 belongs to Arun, leaving Mani with no intersections.
All three match. The algorithm is correct and the legacy WP derivation has no arithmetic errors.
Algorithm and Code
The algorithm has three steps:
- Read
nandm. - Compute
res = min(n, m). - Print “Mani Iyer” if
res % 2 == 0; otherwise print “Arun Gupta”.
Python
n, m = map(int, input().split())
res = min(n, m)
if res % 2 == 0:
print("Mani Iyer")
else:
print("Arun Gupta")
C
#include <stdio.h>
int main() {
int n, m, res;
scanf("%d %d", &n, &m);
res = (n < m) ? n : m;
if (res % 2 == 0)
printf("Mani Iyer\n");
else
printf("Arun Gupta\n");
return 0;
}
Java
import java.util.Scanner;
public class StickGame {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int res = Math.min(n, m);
if (res % 2 == 0)
System.out.println("Mani Iyer");
else
System.out.println("Arun Gupta");
}
}
All three handle the constraint 1 ≤ n, m ≤ 100 without additional edge-case logic. The min computation covers every valid input.
Time and Space Complexity
Both are O(1). The solution reads two integers, performs one comparison, and outputs a string. Memory usage stays constant regardless of how large n and m are. For a full walkthrough of how to derive and report complexity in coding rounds, see space and time complexity of algorithms.
A naive game-tree search on a 100 × 100 grid would need to explore exponentially many move sequences. The O(1) parity solution sidesteps that entirely by identifying the game’s structure before writing a single line of code.
The stick game belongs to the broader family of two-player problems where the total move count is fixed regardless of strategy. The GeeksforGeeks game theory section covers Nim, Sprague-Grundy, and related patterns that placement tests draw from.
For other programming problems where a single insight replaces an expensive algorithm, see the Wordakshari game problem and the equilibrium index problem. Both reward the same approach: understand the structure first, then write five lines of code.
The min(n, m) parity check is straightforward once you see it, but recognising that structure quickly in a new problem takes practice across varied problem sets. TinkerLLM is a coding playground where you can build and test variations of this type of problem without IDE setup; a month’s access starts at ₹299.
Primary sources
Frequently asked questions
Why does the stick game always end in exactly min(n, m) moves?
Each move removes exactly one horizontal stick (an entire row) and one vertical stick (an entire column). The game ends when no intersections remain, which happens as soon as either all rows or all columns are gone. That takes exactly min(n, m) moves.
Does player strategy affect who wins in the stick game?
No. Every intersection point is equivalent in terms of game outcome. Regardless of which point a player picks, exactly one row and one column disappear. The total move count is always min(n, m), and the winner is fixed by that count's parity before the first move.
What happens when n equals m in the stick game?
When n equals m, min(n, m) equals n. If n is odd, Arun Gupta wins. If n is even, Mani Iyer wins. The symmetric grid changes nothing about the parity logic.
Can Arun Gupta win if only one row exists (n = 1)?
Yes. With n = 1, min(1, m) = 1 for any valid m. Since 1 is odd, Arun Gupta always wins when n = 1 (or symmetrically, when m = 1).
Which placement tests include game theory problems like the stick game?
Game theory and combinatorial problems appear in competitive programming rounds at companies including D.E. Shaw, Directi, and Mu Sigma, as well as in Olympiad-style national tests. The stick game is a clean example of a zero-sum combinatorial problem where the winning condition reduces to a parity check rather than a recursive game-tree search.
A self-paced playground for building with LLMs.
TinkerLLM is FACE Prep's sister property. A guided environment for shipping real LLM applications, the kind of project that earns a paragraph on your resume, not a line.
Try TinkerLLM (₹299 launch)