Skip to content

Instantly share code, notes, and snippets.

@tatsuyax25
Created August 29, 2025 17:05
Show Gist options
  • Save tatsuyax25/294500f45e03aec43f11b934c1465835 to your computer and use it in GitHub Desktop.
Save tatsuyax25/294500f45e03aec43f11b934c1465835 to your computer and use it in GitHub Desktop.
Alice and Bob are playing a turn-based game on a field, with two lanes of flowers between them. There are x flowers in the first lane between Alice and Bob, and y flowers in the second lane between them. The game proceeds as follows: Alice takes th
/**
* @param {number} n
* @param {number} m
* @return {number}
*/
var flowerGame = function(n, m) {
// Count how many odd numbers are in [1, n]
const oddX = Math.floor(n / 2) + (n % 2); // e.g., 1, 3, 5...
const evenX = Math.floor(n / 2); // e.g., 2, 4, 6...
// count how many odd numbers are in [1, m]
const oddY = Math.floor(m / 2) + (m % 2);
const evenY = Math.floor(m / 2);
// Valid pairs are:
// - odd x with even y
// - even x with odd y
const validPairs = (oddX * evenY) + (evenX * oddY);
return validPairs;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment