#3782

Last Remaining Integer After Alternating Deletion Operations

hard · premium · 48.6% accepted · 38 likes · top 35%

math · recursion

Description

You are given an integer n.

We write the integers from 1 to n in a sequence from left to right. Then, alternately apply the following two operations until only one integer remains, starting with operation 1:

- Operation 1: Starting from the left, delete every second number.

- Operation 2: Starting from the right, delete every second number.

Return the last remaining integer.

Solution