Problem
Alice and Bob are playing a turn-based game on a circular field surrounded by flowers. The circle represents the field, and there are x
flowers in the clockwise direction between Alice and Bob, and y
flowers in the anti-clockwise direction between them.
The game proceeds as follows:
- Alice takes the first turn.
- In each turn, a player must choose either the clockwise or anti-clockwise direction and pick one flower from that side.
- At the end of the turn, if there are no flowers left at all, the current player captures their opponent and wins the game.
Given two integers, n
and m
, the task is to compute the number of possible pairs (x, y)
that satisfy the conditions:
- Alice must win the game according to the described rules.
- The number of flowers
x
in the clockwise direction must be in the range[1,n]
. - The number of flowers
y
in the anti-clockwise direction must be in the range[1,m]
.
Return the number of possible pairs (x, y)
that satisfy the conditions mentioned in the statement.
Examples
Example 1:
|
|
Example 2:
|
|
Constraints:
1 <= n, m <= 10^5
Solution
Method 1 - Naive
To determine the number of pairs (x, y
) that satisfy the conditions:
-
Alice’s First Choice: Alice wins the game if she can ensure that the sum of flowers
x + y
is odd. An odd total ensures that Bob gets fewer turns than Alice and eventually Alice makes the last move, leading to her victory. -
Range Constraints: Both
x
andy
are restricted to specific ranges:1 ≤ x ≤ n
and1 ≤ y ≤ m
. -
Iterative Checking:
- Loop through all possible values of
x
within[1, n]
. - For each value of
x
, loop through all possible values ofy
within[1, m]
. - For every pair
(x, y)
, check if the sumx + y
is odd. - Count all such valid pairs.
- Loop through all possible values of
Code
|
|
|
|
Complexity
- ⏰ Time complexity:
O(n * m)
(Two nested loops to iterate through all possible values forx
andy
). - 🧺 Space complexity: -
O(1)
(No extra space used apart from a variable to store the count).
Method 2 - Using Maths
To improve the performance and avoid TLE (Time Limit Exceeded), we need to optimise the brute-force approach with mathematical insights instead of iterating through all pairs (x, y)
.
From the problem:
- Alice wins if the sum
x + y
is odd. - Odd or even totals alternate between pairs
(x, y)
, so we can directly count how many such pairs satisfy the condition:- Total pairs
(x, y)
=n * m
. - Half of these pairs have odd sums and half have even sums, as odd and even sums alternate evenly in a grid.
- Total pairs
Key Insight:
- Count of odd sums:
- Compute the count of odd
x
values and oddy
values. - Compute the count of even
x
values and eveny
values. - Pairs with
(odd x, even y)
or(even x, odd y)
give odd sums.
- Compute the count of odd
Steps:
- Count odd and even values in the ranges
[1, n]
and[1, m]
.- Odd numbers in
[1, n]
→(n + 1) / 2
. - Even numbers in
[1, n]
→n / 2
. - Similarly for
[1, m]
.
- Odd numbers in
- Compute odd-sum pairs:
odd_x * even_y + even_x * odd_y
.
Code
|
|
|
|
Complexity
- ⏰ Time complexity:
O(1)
— Direct computations using arithmetic operations only. - 🧺 Space complexity:
O(1)
— Uses only constant space for variables.