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.
Example 1:
Input: nums = [1,2,3]
Output: 30
Explanation:
Concatenate the numbers in the order [3, 1, 2] to get the result "11110", which is the binary representation of 30.
Example 2:
Input: nums = [2,8,16]
Output: 1296
Explanation:
Concatenate the numbers in the order [2, 8, 16] to get the result "10100010000", which is the binary representation of 1296.
Constraints:
nums.length == 31 <= nums[i] <= 127Problem Overview: You are given an array of integers. Convert each number to its binary representation and concatenate them in some order. The resulting binary string represents a number. The task is to arrange the numbers so this binary concatenation produces the maximum possible value.
The challenge is not the binary conversion itself. The tricky part is choosing the order of elements. Different permutations produce different concatenated binary strings, and the one with the largest binary value (or lexicographically largest binary string) is the answer.
Approach 1: Lexicographic Concatenation (Enumeration) (Time: O(n! * k), Space: O(k))
This approach tries every possible ordering of the array. For each permutation, convert every integer to binary using bin(x) (or equivalent), concatenate the binary strings, and interpret the result as a binary number. Track the maximum value seen across all permutations.
The key idea is brute-force enumeration: since the concatenation result depends entirely on order, checking every permutation guarantees the optimal answer. If each binary string has length k, building and evaluating each candidate takes O(k). The overall complexity becomes O(n! * k).
This method is straightforward and useful for understanding the problem structure. It works well when the array size is very small (as in the original constraint). It also highlights that the problem reduces to ordering elements based on how their binary strings interact when concatenated.
Approach 2: Direct Comparison with Custom Sorting (Time: O(n log n * k), Space: O(n))
A more scalable approach treats the problem like the classic “largest number by concatenation” problem. Convert each integer to its binary string first. When deciding the order of two elements a and b, compare the concatenated strings a + b and b + a. If a + b is larger, place a before b; otherwise place b first.
This comparison rule ensures that every pair of elements contributes to the globally optimal concatenation. Sorting the array with this custom comparator yields the best order without checking every permutation. After sorting, concatenate all binary strings and convert the final binary value if needed.
The sorting step dominates the complexity at O(n log n). Each comparison may examine up to O(k) characters, where k is the binary length. This approach generalizes well and is the preferred strategy when the array size grows.
Relevant concepts include array manipulation, binary string handling from bit manipulation, and permutation reasoning from enumeration.
Recommended for interviews: Start by explaining the permutation idea to show you understand why ordering matters. Then move to the custom sorting strategy. Interviewers usually expect the comparator-based solution because it reduces factorial exploration to O(n log n) while preserving the correct ordering rule.
This approach involves treating the binary representations of the numbers as strings and finding the permutation that forms the maximum possible concatenated binary string.
Since there are only 3 numbers, we can afford to try all permutations and select the best one by comparing lexicographically string concatenations in each permutation.
This Python implementation uses itertools.permutations to generate all possible permutations of the input array. For each permutation, it converts each number to its binary form using format(num, 'b'), concatenates the strings, converts the result back to a decimal integer using int with base 2, and tracks the maximum value found.
Time Complexity: O(3!), as there are 6 permutations of the 3 numbers, and for each permutation, concatenating binary strings takes constant time.
Space Complexity: O(1), as we use constant space apart from the input and output size.
Instead of generating all permutations, this approach involves sorting the numbers based on a custom comparison. We compare which order of two numbers creates a larger concatenated binary number, and sort based on that for a greedy solution to find the order that gives the maximal number.
The JavaScript solution uses Array.sort with a custom comparator to decide the order based on which concatenated result is larger. After sorting, we concatenate the binary strings and convert it to a decimal number for the result.
JavaScript
C
Time Complexity: O(n log n), due to sorting, but with n being constant (3 elements), it's O(1).
Space Complexity: O(1), as we just store intermediate results and re-use input space.
According to the problem description, the length of the array nums is 3. We can enumerate all permutations of nums, which has 3! = 6 permutations. Then, we convert the elements of the permuted array into binary strings, concatenate these binary strings, and finally convert the concatenated binary string into a decimal number to get the maximum value.
The time complexity is O(log M), where M represents the maximum value of the elements in nums. The space complexity is O(1).
Python
Java
C++
Go
TypeScript
| Approach | Complexity |
|---|---|
| Lexicographic Concatenation | Time Complexity: O(3!), as there are 6 permutations of the 3 numbers, and for each permutation, concatenating binary strings takes constant time. Space Complexity: O(1), as we use constant space apart from the input and output size. |
| Direct Comparison with Custom Sorting | Time Complexity: O(n log n), due to sorting, but with n being constant (3 elements), it's O(1). Space Complexity: O(1), as we just store intermediate results and re-use input space. |
| Enumeration | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Lexicographic Concatenation (Permutation Enumeration) | O(n! * k) | O(k) | When the array size is very small and you want a simple brute‑force solution to verify correctness. |
| Custom Sorting with Binary Concatenation Comparison | O(n log n * k) | O(n) | General case. Preferred approach for interviews and larger arrays. |
Leetcode Weekly Contest 418 | 3309. Maximum Possible Number by Binary Concatenation | • CodeFod • 467 views views
Watch 8 more video solutions →Practice Maximum Possible Number by Binary Concatenation with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor