Mirror Reflection Problem

Problem

There is a special square room with mirrors on each of the four walls. Except for the southwest corner, there are receptors on each of the remaining corners, numbered 01, and 2.

The square room has walls of length p and a laser ray from the southwest corner first meets the east wall at a distance q from the 0th receptor.

Given the two integers p and q, return the number of the receptor that the ray meets first.

The test cases are guaranteed so that the ray will meet a receptor eventually.

Examples

Example 1:

1
2
3
4
5
Input:
p = 2, q = 1
Output:
 2
Explanation: The ray meets receptor 2 the first time it gets reflected back to the left wall.

Example 2:

1
2
3
4
Input:
p = 3, q = 1
Output:
 1

Solution

Method 1 – Mathematical Simulation with LCM

Intuition

The laser will hit a receptor when it reaches a corner. The path can be simulated by extending the room horizontally and vertically until the ray hits a receptor. The first meeting point is at the least common multiple (LCM) of p and q. The parity of the number of room extensions determines which receptor is hit.

Approach

  1. Compute L = LCM(p, q).
  2. The number of room extensions vertically is L / q, and horizontally is L / p.
  3. If vertical is even and horizontal is odd, return 0.
  4. If vertical is odd and horizontal is odd, return 1.
  5. If vertical is odd and horizontal is even, return 2.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution {
public:
    int mirrorReflection(int p, int q) {
        int g = gcd(p, q);
        int l = p * q / g;
        int m = l / q, n = l / p;
        if (m % 2 == 0 && n % 2 == 1) return 0;
        if (m % 2 == 1 && n % 2 == 1) return 1;
        if (m % 2 == 1 && n % 2 == 0) return 2;
        return -1;
    }
    int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
func mirrorReflection(p, q int) int {
    g := gcd(p, q)
    l := p * q / g
    m, n := l/q, l/p
    if m%2 == 0 && n%2 == 1 {
        return 0
    }
    if m%2 == 1 && n%2 == 1 {
        return 1
    }
    if m%2 == 1 && n%2 == 0 {
        return 2
    }
    return -1
}
func gcd(a, b int) int { if b == 0 { return a }; return gcd(b, a%b) }
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution {
    public int mirrorReflection(int p, int q) {
        int g = gcd(p, q);
        int l = p * q / g;
        int m = l / q, n = l / p;
        if (m % 2 == 0 && n % 2 == 1) return 0;
        if (m % 2 == 1 && n % 2 == 1) return 1;
        if (m % 2 == 1 && n % 2 == 0) return 2;
        return -1;
    }
    private int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution {
    fun mirrorReflection(p: Int, q: Int): Int {
        fun gcd(a: Int, b: Int): Int = if (b == 0) a else gcd(b, a % b)
        val l = p * q / gcd(p, q)
        val m = l / q
        val n = l / p
        return when {
            m % 2 == 0 && n % 2 == 1 -> 0
            m % 2 == 1 && n % 2 == 1 -> 1
            m % 2 == 1 && n % 2 == 0 -> 2
            else -> -1
        }
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution:
    def mirrorReflection(self, p: int, q: int) -> int:
        from math import gcd
        l = p * q // gcd(p, q)
        m, n = l // q, l // p
        if m % 2 == 0 and n % 2 == 1:
            return 0
        if m % 2 == 1 and n % 2 == 1:
            return 1
        if m % 2 == 1 and n % 2 == 0:
            return 2
        return -1
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
impl Solution {
    pub fn mirror_relection(p: i32, q: i32) -> i32 {
        fn gcd(a: i32, b: i32) -> i32 { if b == 0 { a } else { gcd(b, a % b) } }
        let l = p * q / gcd(p, q);
        let m = l / q;
        let n = l / p;
        if m % 2 == 0 && n % 2 == 1 { return 0; }
        if m % 2 == 1 && n % 2 == 1 { return 1; }
        if m % 2 == 1 && n % 2 == 0 { return 2; }
        -1
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution {
    mirrorReflection(p: number, q: number): number {
        function gcd(a: number, b: number): number { return b === 0 ? a : gcd(b, a % b); }
        const l = p * q / gcd(p, q);
        const m = l / q, n = l / p;
        if (m % 2 === 0 && n % 2 === 1) return 0;
        if (m % 2 === 1 && n % 2 === 1) return 1;
        if (m % 2 === 1 && n % 2 === 0) return 2;
        return -1;
    }
}

Complexity

  • ⏰ Time complexity: O(log(max(p, q))), for the GCD calculation.
  • 🧺 Space complexity: O(1), only constant extra space is used.