Ctrl K

Ranking by Hailstone Steps

Medium
Take a positive integer and keep changing it by this rule: an even number is halved, an odd number x becomes 3x + 1. The **path length** of a number is how many such steps it takes to reach 1 (1 itself has path length 0). For example, 6 goes 6 → 3 → 10 → 5 → 16 → 8 → 4 → 2 → 1, which is 8 steps. You are given low, high and place. Write down every integer from low to high inclusive and order them by path length, shortest first; when two numbers have the same path length, the smaller one comes first. Return the number that ends up at position place (counting from 1). Every number in the range is guaranteed to reach 1.
Example 1
low=6, high=9, place=1 → 8

Steps: 6 takes 8, 7 takes 16, 8 takes 3, 9 takes 19. By steps: 8, 6, 7, 9. First place goes to 8.

Example 2
low=6, high=9, place=3 → 7

In the same order 8, 6, 7, 9, the third one is 7.

Example 3
low=1, high=4, place=2 → 2

1 takes 0 steps, 2 takes 1, 4 takes 2 and 3 takes 7 (3 → 10 → 5 → 16 → 8 → 4 → 2 → 1). The order is 1, 2, 4, 3, so second place is 2.

Constraints
  • 1 ≤ low ≤ high ≤ 1000
  • 1 ≤ place ≤ high − low + 1
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsClimbing Along Rows and Columns HardSplit Into Dictionary Words MediumEvery Bracketing Result MediumChain of Multiples MediumSteps Down to One Medium
loading Python…
Input
low=6, high=9, place=1
Expected
8

Run checks the first 3 tests in your browser. Submit checks all 96 tests on the server, hidden ones included.