Sort Integers by The Power Value - Problem

The power of an integer is a fascinating concept based on the famous Collatz Conjecture! ๐ŸŽฏ

Given any positive integer x, its power value is defined as the number of steps needed to transform it into 1 using these simple rules:

  • If x is even: x = x / 2
  • If x is odd: x = 3 * x + 1

Example: The power of x = 3 is 7 because: 3 โ†’ 10 โ†’ 5 โ†’ 16 โ†’ 8 โ†’ 4 โ†’ 2 โ†’ 1 (7 steps)

Your task is to:

  1. Calculate the power value for all integers in the range [lo, hi]
  2. Sort them by power value in ascending order
  3. If two numbers have the same power, sort them by their numeric value
  4. Return the k-th number in this sorted list

๐Ÿ”ฅ Challenge: Can you optimize this using memoization to avoid recalculating the same power values?

Input & Output

example_1.py โ€” Basic Range
$ Input: lo = 12, hi = 15, k = 2
โ€บ Output: 13
๐Ÿ’ก Note: Powers: 12โ†’6โ†’3โ†’10โ†’5โ†’16โ†’8โ†’4โ†’2โ†’1 (9 steps), 13โ†’40โ†’20โ†’10โ†’5โ†’16โ†’8โ†’4โ†’2โ†’1 (9 steps), 14โ†’7โ†’22โ†’11โ†’34โ†’17โ†’52โ†’26โ†’13โ†’40โ†’20โ†’10โ†’5โ†’16โ†’8โ†’4โ†’2โ†’1 (17 steps), 15โ†’46โ†’23โ†’70โ†’35โ†’106โ†’53โ†’160โ†’80โ†’40โ†’20โ†’10โ†’5โ†’16โ†’8โ†’4โ†’2โ†’1 (17 steps). Sorted: [(9,12), (9,13), (17,14), (17,15)]. The 2nd element is 13.
example_2.py โ€” Small Range
$ Input: lo = 7, hi = 11, k = 4
โ€บ Output: 7
๐Ÿ’ก Note: Powers: 7 (16 steps), 8 (3 steps), 9 (19 steps), 10 (6 steps), 11 (14 steps). Sorted by power: [(3,8), (6,10), (14,11), (16,7), (19,9)]. The 4th element is 7.
example_3.py โ€” Single Element
$ Input: lo = 1, hi = 1, k = 1
โ€บ Output: 1
๐Ÿ’ก Note: Only one number in range [1,1]. The power of 1 is 0 (already at target). The 1st and only element is 1.

Constraints

  • 1 โ‰ค lo โ‰ค hi โ‰ค 1000
  • 1 โ‰ค k โ‰ค hi - lo + 1
  • All integers will eventually transform to 1
  • Power values fit in 32-bit signed integer

Visualization

Tap to expand
๐Ÿ”๏ธ The Power Journey VisualizationMountain Trail to Base Camp5Start168๐Ÿ“ Signpost3 steps left6Start3105โœ“ Use Signpost!5 steps total๐ŸŽฏ Final Ranking (Sorted by Total Steps)๐Ÿฅ‡ 1st8 (3)๐Ÿฅˆ 2nd10 (6)๐Ÿฅ‰ 3rd5 (5)4th6 (8)๐Ÿ’ก Key Insight: Memoization = Smart SignpostsInstead of each hiker calculating the full path, they reuse signposts placed by previous hikers!This transforms O(n*m) independent journeys into O(n*log k) with shared knowledge.
Understanding the Visualization
1
Start Journey
Each number begins its transformation journey following the rules
2
Place Signposts
As we calculate paths, we place signposts (memoization) at key points
3
Reuse Signposts
Future hikers can use existing signposts to skip calculations
4
Sort by Distance
Rank all hikers by their total journey length to base camp
Key Takeaway
๐ŸŽฏ Key Insight: Memoization transforms individual calculations into a collaborative effort where each computation benefits future ones, dramatically improving efficiency!
Asked in
Amazon 45 Google 38 Microsoft 32 Meta 25
47.2K Views
Medium-High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen