Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Move from brute-force thinking to an efficient approach using math strategy.
There is a regular convex polygon with n vertices. The vertices are labeled from 0 to n - 1 in a clockwise direction, and each vertex has exactly one monkey. The following figure shows a convex polygon of 6 vertices.
Simultaneously, each monkey moves to a neighboring vertex. A collision happens if at least two monkeys reside on the same vertex after the movement or intersect on an edge.
Return the number of ways the monkeys can move so that at least one collision happens. Since the answer may be very large, return it modulo 109 + 7.
Example 1:
Input: n = 3
Output: 6
Explanation:
There are 8 total possible movements.
Two ways such that they collide at some point are:
Example 2:
Input: n = 4
Output: 14
Constraints:
3 <= n <= 109Problem summary: There is a regular convex polygon with n vertices. The vertices are labeled from 0 to n - 1 in a clockwise direction, and each vertex has exactly one monkey. The following figure shows a convex polygon of 6 vertices. Simultaneously, each monkey moves to a neighboring vertex. A collision happens if at least two monkeys reside on the same vertex after the movement or intersect on an edge. Return the number of ways the monkeys can move so that at least one collision happens. Since the answer may be very large, return it modulo 109 + 7.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math
3
4
powx-n)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2550: Count Collisions of Monkeys on a Polygon
class Solution {
public int monkeyMove(int n) {
final int mod = (int) 1e9 + 7;
return (qpow(2, n, mod) - 2 + mod) % mod;
}
private int qpow(long a, int n, int mod) {
long ans = 1;
for (; n > 0; n >>= 1) {
if ((n & 1) == 1) {
ans = ans * a % mod;
}
a = a * a % mod;
}
return (int) ans;
}
}
// Accepted solution for LeetCode #2550: Count Collisions of Monkeys on a Polygon
func monkeyMove(n int) int {
const mod = 1e9 + 7
qpow := func(a, n int) int {
ans := 1
for ; n > 0; n >>= 1 {
if n&1 == 1 {
ans = ans * a % mod
}
a = a * a % mod
}
return ans
}
return (qpow(2, n) - 2 + mod) % mod
}
# Accepted solution for LeetCode #2550: Count Collisions of Monkeys on a Polygon
class Solution:
def monkeyMove(self, n: int) -> int:
mod = 10**9 + 7
return (pow(2, n, mod) - 2) % mod
// Accepted solution for LeetCode #2550: Count Collisions of Monkeys on a Polygon
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #2550: Count Collisions of Monkeys on a Polygon
// class Solution {
// public int monkeyMove(int n) {
// final int mod = (int) 1e9 + 7;
// return (qpow(2, n, mod) - 2 + mod) % mod;
// }
//
// private int qpow(long a, int n, int mod) {
// long ans = 1;
// for (; n > 0; n >>= 1) {
// if ((n & 1) == 1) {
// ans = ans * a % mod;
// }
// a = a * a % mod;
// }
// return (int) ans;
// }
// }
// Accepted solution for LeetCode #2550: Count Collisions of Monkeys on a Polygon
function monkeyMove(n: number): number {
const mod = 10 ** 9 + 7;
const qpow = (a: number, n: number): number => {
let ans = 1n;
for (; n; n >>>= 1) {
if (n & 1) {
ans = (ans * BigInt(a)) % BigInt(mod);
}
a = Number((BigInt(a) * BigInt(a)) % BigInt(mod));
}
return Number(ans);
};
return (qpow(2, n) - 2 + mod) % mod;
}
Use this to step through a reusable interview workflow for this problem.
Simulate the process step by step — multiply n times, check each number up to n, or iterate through all possibilities. Each step is O(1), but doing it n times gives O(n). No extra space needed since we just track running state.
Math problems often have a closed-form or O(log n) solution hidden behind an O(n) simulation. Modular arithmetic, fast exponentiation (repeated squaring), GCD (Euclidean algorithm), and number theory properties can dramatically reduce complexity.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.