Advertisement

Elimination Game - LeetCode 390 Solution

Elimination Game - Complete Solution Guide

Elimination Game is LeetCode problem 390, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You have a list arr of all integers in the range [1, n] sorted in a strictly increasing order. Apply the following algorithm on arr : Starting from left to right, remove the first number and every other number afterward until you reach the end of the list. Repeat the previous step again, but this time from right to left, remove the rightmost number and every other number from the remaining numbers. Keep repeating the steps again, alternating left to right and right to left, until a single number

Detailed Explanation

The problem asks us to simulate an elimination process on a list of numbers from 1 to `n`. We start by eliminating every other number from left to right. Then, we switch direction and eliminate every other number from right to left. We repeat this process, alternating directions, until only one number remains. The goal is to find this last remaining number.

Solution Approach

The solution simulates the elimination process without explicitly creating the array. It maintains four variables: `head` (the first remaining number), `step` (the gap between remaining numbers), `remaining` (the number of remaining numbers), and `from_left` (a boolean indicating the direction of elimination). In each iteration, the `head` is updated based on the elimination direction and whether the number of remaining numbers is odd or even. Then, the `remaining` numbers are halved, the `step` size is doubled, and the direction is reversed. This process continues until only one number remains.

Step-by-Step Algorithm

  1. Step 1: Initialize `head` to 1, `step` to 1, `remaining` to `n`, and `from_left` to `True`.
  2. Step 2: Enter a `while` loop that continues as long as `remaining` is greater than 1.
  3. Step 3: Inside the loop, check if we are eliminating from left to right (`from_left` is `True`) or if `remaining` is odd. If either condition is true, update `head` by adding `step` to it.
  4. Step 4: Divide `remaining` by 2 (integer division), since we eliminate half of the numbers in each iteration.
  5. Step 5: Multiply `step` by 2, since the gap between remaining numbers doubles in each iteration.
  6. Step 6: Toggle the value of `from_left` to switch the elimination direction.
  7. Step 7: After the loop terminates, return the value of `head`.

Key Insights

  • Insight 1: Instead of actually creating the array and simulating the elimination, we can track the head (the first remaining number), the step size (the gap between remaining numbers), the direction of elimination, and the number of remaining elements.
  • Insight 2: The key to solving this problem efficiently is understanding how the head changes after each elimination round. If we are eliminating from left to right, the head always advances by the current step size. If we are eliminating from right to left, the head advances by the current step size only when the number of remaining elements is odd.
  • Insight 3: The number of remaining elements is halved in each iteration, leading to a logarithmic time complexity. The step size doubles in each iteration, reflecting the increasing gap between remaining elements.

Complexity Analysis

Time Complexity: O(log(n))

Space Complexity: O(1)

Topics

This problem involves: Math, Recursion.

Companies

Asked at: Autodesk.