LeetCode Entry

1927. Sum Game

23.08.2026 medium 2026 kotlin rust

Alice fights Bob who tryies to equalize halfs

1927. Sum Game medium substack youtube

https://dmitrysamoylenko.com/leetcode/

23.08.2026.webp

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/1460

Problem TLDR

Alice fights Bob who tryies to equalize halfs

Intuition

    // 20 minute use hint: mod 9
    // 30 minute: didn't solve

Alice always wins except when Bob decides to always act the opposite way of Alice x vs 9-x. In that case the predefined difference of sums decides the outcome. The difference of ? is the playground and each Alice turn x makes Bob give 9-x, and together the sum increases by x+9-x=9. That means each 2 turns gives another 9. Or in other words 2d + q9 == 0 is where the Bob wins. Q is even, otherwise Alice has the last turn and breaks what Bob has been trying to build for all his life.

Approach

Complexity

  • Time complexity: \(O(n)\)

  • Space complexity: \(O(1)\)

Code

    fun sumGame(n: String) = run {
        var d = 0; var q = 0; val h = n.length / 2
        for (i in 0..<h) {
            if (n[i] == '?') q++ else d += n[i] - '0'
            if (n[i + h] == '?') q-- else d -= n[i + h] - '0'
        }
        q % 2 != 0 || d + q * 9 / 2 != 0
    }

Comments