Maximum Possible Number by Binary Concatenation - Problem

You are given an array of integers nums of size 3. Return the maximum possible number whose binary representation can be formed by concatenating the binary representation of all elements in nums in some order.

Note that the binary representation of any number does not contain leading zeros.

Input & Output

Example 1 — Basic Case
$ Input: nums = [1,2,3]
Output: 30
💡 Note: Binary representations: 1→"1", 2→"10", 3→"11". Optimal arrangement [1,3,2] gives "1"+"11"+"10"="11110" which equals 30 in decimal.
Example 2 — Larger Numbers
$ Input: nums = [2,8,16]
Output: 2576
💡 Note: Binary: 2→"10", 8→"1000", 16→"10000". Optimal order [2,8,16] gives "10"+"1000"+"10000"="101000010000" = 2576.
Example 3 — Single Digit Focus
$ Input: nums = [5,6,7]
Output: 501
💡 Note: Binary: 5→"101", 6→"110", 7→"111". Best arrangement [7,5,6] produces "111"+"101"+"110"="111101110" = 494.

Constraints

  • nums.length == 3
  • 1 ≤ nums[i] ≤ 107

Visualization

Tap to expand
Maximum Possible Number by Binary Concatenation INPUT Array nums of size 3: 1 [0] 2 [1] 3 [2] Binary Representations: 1 --> "1" 2 --> "10" 3 --> "11" Goal: Find max concatenation ALGORITHM STEPS 1 Convert to Binary Get binary string for each num 2 Custom Sort Compare a+b vs b+a 3 Concatenate Join sorted strings 4 Convert to Decimal Binary to integer Sorting Comparisons: "11"+"10" = "1110" > "1011" "11"+"1" = "111" > "111" "10"+"1" = "101" > "110" Order: [3, 1, 2] FINAL RESULT Optimal Order: 3 1 2 Binary Concatenation: "11" + "1" + "10" "11110" Output: 30 Key Insight: To maximize the result, sort numbers by comparing concatenations: for a and b, if a+b > b+a (as strings), then a should come before b. This greedy approach ensures the largest possible binary number is formed. TutorialsPoint - Maximum Possible Number by Binary Concatenation | Optimized Sorting Approach
Asked in
Google 25 Meta 18 Amazon 15
8.5K Views
Medium Frequency
~15 min Avg. Time
420 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