Skip to content

S2Q2 · Hand Cricket Match Runs

⚡ Quick Reference

Type: Full I/O problem

Core idea: read ball by ball, add runs per the rules, stop when out condition is met.

import sys

balls = 0
runs  = 0
prev_bat = None

for line in sys.stdin:
    line = line.strip()
    if not line:
        continue
    bat, bowl = map(int, line.split())
    balls += 1

    # Out conditions: same as bowler OR batsman plays same twice in a row
    if bat == bowl or bat == prev_bat:
        break

    runs += bowl if bat == 0 else bat
    prev_bat = bat

print(balls, runs)

Key rules: - Out if bat == bowl (same as bowler) - Out if bat == prev_bat (batsman plays same number twice consecutively) - If bat == 0 → add bowler's number to runs - Otherwise → add batsman's number to runs - The ball that ends the game counts toward balls but not toward runs


Problem Statement

Problem (I/O type)

Read ball-by-ball numbers for batsman and bowler. Compute total balls and total runs when the innings ends.

Example 1:

Input
1 4
2 3
6 2
5 1
0 2
3 3
Output
6 16

Example 2:

Input
1 4
5 3
0 3
5 1
5 4
Output
5 14

Tracing both examples

Example 1:

Ball Bat Bowl Out? Runs this ball Total runs
1 1 4 1 1
2 2 3 2 3
3 6 2 6 9
4 5 1 5 14
5 0 2 2 (bowl) 16
6 3 3 ✅ (bat==bowl) - 16

Output: 6 16

Example 2:

Ball Bat Bowl prev_bat Out? Runs Total
1 1 4 None 1 1
2 5 3 1 5 6
3 0 3 5 3 (bowl) 9
4 5 1 0 5 14
5 5 4 5 ✅ (bat==prev) - 14

Output: 5 14


Solution approaches

import sys

balls    = 0
runs     = 0
prev_bat = None

for line in sys.stdin:
    line = line.strip()
    if not line:
        continue
    bat, bowl = map(int, line.split())
    balls += 1

    if bat == bowl or bat == prev_bat:
        break

    runs    += bowl if bat == 0 else bat
    prev_bat = bat

print(balls, runs)
import sys

balls    = 0
runs     = 0
prev_bat = None

for line in sys.stdin:
    line = line.strip()
    if not line:
        continue
    bat, bowl = map(int, line.split())
    balls += 1

    # Check out conditions
    if bat == bowl:       # batsman plays same as bowler
        break
    if bat == prev_bat:   # batsman plays same number twice in a row
        break

    # Add runs
    if bat == 0:
        runs += bowl   # bowler's number counts
    else:
        runs += bat    # batsman's number counts

    prev_bat = bat

print(balls, runs)
import sys

lines    = [l.strip() for l in sys.stdin if l.strip()]
pairs    = [tuple(map(int, l.split())) for l in lines]

prev_bat = None
balls    = 0
runs     = 0

for bat, bowl in pairs:
    balls += 1
    if bat == bowl or bat == prev_bat:
        break
    runs    += bowl if bat == 0 else bat
    prev_bat = bat

print(balls, runs)

Key takeaways

01

Two out conditions - bat==bowl OR bat==prev_bat

The innings ends if the batsman plays the same number as the bowler or plays the same number as their previous ball. Track prev_bat across iterations and check both before adding runs.

02

bat == 0 → use bowl's number

When the batsman plays 0, the bowler's number is added instead. This is an exception to the normal rule - always check if bat == 0 before adding runs.

03

The out ball counts for balls but not for runs

Increment balls before checking out conditions. Break immediately after incrementing - the out ball is counted but no runs are added for it.