Skip to main content

Uber

Uber OA 2023 - Find the Rabbits Behind the Doors

Problem Description:

There are n doors, and behind each door is either a rabbit or a tortoise. You know the following:

  • There are x rabbits and y tortoises behind the doors.
  • The total number of doors is n = x+y.
  • No two rabbits are adjacent, meaning no two consecutive doors can have rabbits behind them.

Your task is to determine the minimum number of doors that need to be opened to find the positions of all the rabbits.

Example:

For x = 1 rabbit and y = 3 tortoises:

  • There are a total of 4 doors.
  • Opening the first door reveals a tortoise. Opening the third door reveals another tortoise. Once you've opened enough doors, the position of the rabbit can be deduced without needing to open all doors.
  • The answer for this case is 3 doors.

Given that the values of x (number of rabbits) and y (number of tortoises) will always form a valid combination, you need to find the minimum number of doors to open to locate the positions of all the rabbits.


Input Format:

  • x: Integer representing the number of rabbits.
  • y: Integer representing the number of tortoises.

The total number of doors is n = x + y.


Output Format:

  • A single integer represents the minimum number of doors that need to be opened to determine the position of all the rabbits.

Constraints:

  • 0 <= x <= 10^9
  • 0 <= y <= 10^9
  • n = x + y
  • No two rabbits are adjacent.
  • The combination of x and y will always form a valid arrangement.

Sample Testcase 1:

Input:

x = 1
y = 3

Output:

3

Explanation:

  • We have 1 rabbit and 3 tortoises, giving a total of 4 doors.
  • You can open doors one by one, and after opening 3 doors (which all turn out to have tortoises), you can deduce that the rabbit must be behind the last unopened door. Thus, the minimum number of doors to open is 3.

Sample Testcase 2:

Input:

x = 2
y = 2

Output:

3

Explanation:

  • There are 2 rabbits and 2 tortoises, so there are 4 doors in total. The arrangement is such that no two rabbits are adjacent. After opening 3 doors, you can identify the position of the rabbits.