Editorial for Maze Runner


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.

Submitting an official solution before solving the problem yourself is a bannable offence.
by: doncho

Solution with comments in Java:

static void solveMazeRunner() {
    Scanner in = new Scanner(System.in);
    int n = in.nextInt();

    for (int i = 0; i < n; i++) {
        int number = in.nextInt();
        int oddsSum = 0;
        int evensSum = 0;

        // Split the number to digits
        // When number becomes 0, there are no digits left
        //  This is result of integer division
        //  i.e. 1 / 0 = 0
        // Example with 123
        //  Get digit 3, the number becomes 12
        //  Get digit 2, the number becomes 1
        //  Get digit 1, the number becomes 0
        while (number > 0) {
            int digit = number % 10;
            if (digit % 2 == 0) {
                evensSum += digit;
            } else {
                oddsSum += digit;
            }
            number /= 10;
        }

        if (evensSum < oddsSum) {
            System.out.println("right");
        } else if (oddsSum < evensSum) {
            System.out.println("left");
        } else {
            System.out.println("straight");
        }
    }
}
`

Comments

There are no comments at the moment.