Maximum Possible Number by Binary Concatenation - Problem
Binary Puzzle: You're given an array of exactly 3 integers, and your task is to create the largest possible number by concatenating their binary representations in any order you choose.

For example, if you have [1, 2, 3], their binary forms are ["1", "10", "11"]. You could arrange them as "11101" (binary) = 29 (decimal), or "11011" (binary) = 27 (decimal). Your goal is to find the arrangement that gives the maximum decimal value.

Key Points:
  • You must use all 3 numbers exactly once
  • Binary representations never have leading zeros
  • Return the final result as a decimal integer

Input & Output

example_1.py โ€” Basic Case
$ Input: [1, 2, 3]
โ€บ Output: 30
๐Ÿ’ก Note: Binary representations: 1โ†’"1", 2โ†’"10", 3โ†’"11". Best arrangement is [1,3,2] or [3,1,2] giving "11110" = 30 in decimal.
example_2.py โ€” Larger Numbers
$ Input: [2, 8, 16]
โ€บ Output: 1000108
๐Ÿ’ก Note: Binary representations: 2โ†’"10", 8โ†’"1000", 16โ†’"10000". Best arrangement is [16,8,2] giving "1000010001" = 529 in decimal.
example_3.py โ€” Edge Case with Zero
$ Input: [0, 1, 2]
โ€บ Output: 102
๐Ÿ’ก Note: Binary representations: 0โ†’"0", 1โ†’"1", 2โ†’"10". Best arrangement is [1,0,2] giving "1010" = 10 in decimal.

Visualization

Tap to expand
Binary Concatenation: Finding Maximum ValueStep 1: Convert to Binary Representation1 โ†’12 โ†’103 โ†’11Step 2: Try All 6 PermutationsPermutation Results:[1,2,3]: "1" + "10" + "11" = "11011" =27[1,3,2]: "1" + "11" + "10" = "11110" =30 โ˜…[2,1,3]: "10" + "1" + "11" = "10111" =23[2,3,1]: "10" + "11" + "1" = "10111" =23[3,1,2]: "11" + "1" + "10" = "11110" =30 โ˜…[3,2,1]: "11" + "10" + "1" = "11101" =29Maximum Result: 30Two permutations achieve the maximum: [1,3,2] and [3,1,2]
Understanding the Visualization
1
Binary Conversion
Transform each decimal number into its binary LED pattern
2
Arrangement Testing
Try all 6 possible ways to arrange the 3 binary segments
3
Maximum Selection
Compare all decimal values and select the largest result
Key Takeaway
๐ŸŽฏ Key Insight: With only 3 elements, brute force enumeration of all 6 permutations is the optimal solution - no advanced algorithm can do better than O(1) constant time!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(1)

Since we always have exactly 3 elements, we generate exactly 6 permutations, making this constant time

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

We only store binary strings and intermediate results, with fixed size input

n
2n
โœ“ Linear Space

Constraints

  • nums.length == 3
  • 1 โ‰ค nums[i] โ‰ค 107
  • Each number will have a valid binary representation without leading zeros
Asked in
Google 15 Meta 12 Amazon 8 Microsoft 6
38.2K Views
Medium Frequency
~12 min Avg. Time
1.5K 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