Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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 summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Bit Manipulation
[1,2,3]
[2,8,16]
concatenation-of-consecutive-binary-numbers)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3309: Maximum Possible Number by Binary Concatenation
class Solution {
private int[] nums;
public int maxGoodNumber(int[] nums) {
this.nums = nums;
int ans = f(0, 1, 2);
ans = Math.max(ans, f(0, 2, 1));
ans = Math.max(ans, f(1, 0, 2));
ans = Math.max(ans, f(1, 2, 0));
ans = Math.max(ans, f(2, 0, 1));
ans = Math.max(ans, f(2, 1, 0));
return ans;
}
private int f(int i, int j, int k) {
String a = Integer.toBinaryString(nums[i]);
String b = Integer.toBinaryString(nums[j]);
String c = Integer.toBinaryString(nums[k]);
return Integer.parseInt(a + b + c, 2);
}
}
// Accepted solution for LeetCode #3309: Maximum Possible Number by Binary Concatenation
func maxGoodNumber(nums []int) int {
f := func(i, j, k int) int {
a := strconv.FormatInt(int64(nums[i]), 2)
b := strconv.FormatInt(int64(nums[j]), 2)
c := strconv.FormatInt(int64(nums[k]), 2)
res, _ := strconv.ParseInt(a+b+c, 2, 64)
return int(res)
}
ans := f(0, 1, 2)
ans = max(ans, f(0, 2, 1))
ans = max(ans, f(1, 0, 2))
ans = max(ans, f(1, 2, 0))
ans = max(ans, f(2, 0, 1))
ans = max(ans, f(2, 1, 0))
return ans
}
# Accepted solution for LeetCode #3309: Maximum Possible Number by Binary Concatenation
class Solution:
def maxGoodNumber(self, nums: List[int]) -> int:
ans = 0
for arr in permutations(nums):
num = int("".join(bin(i)[2:] for i in arr), 2)
ans = max(ans, num)
return ans
// Accepted solution for LeetCode #3309: Maximum Possible Number by Binary Concatenation
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3309: Maximum Possible Number by Binary Concatenation
// class Solution {
// private int[] nums;
//
// public int maxGoodNumber(int[] nums) {
// this.nums = nums;
// int ans = f(0, 1, 2);
// ans = Math.max(ans, f(0, 2, 1));
// ans = Math.max(ans, f(1, 0, 2));
// ans = Math.max(ans, f(1, 2, 0));
// ans = Math.max(ans, f(2, 0, 1));
// ans = Math.max(ans, f(2, 1, 0));
// return ans;
// }
//
// private int f(int i, int j, int k) {
// String a = Integer.toBinaryString(nums[i]);
// String b = Integer.toBinaryString(nums[j]);
// String c = Integer.toBinaryString(nums[k]);
// return Integer.parseInt(a + b + c, 2);
// }
// }
// Accepted solution for LeetCode #3309: Maximum Possible Number by Binary Concatenation
function maxGoodNumber(nums: number[]): number {
const f = (i: number, j: number, k: number): number => {
const a = nums[i].toString(2);
const b = nums[j].toString(2);
const c = nums[k].toString(2);
const res = parseInt(a + b + c, 2);
return res;
};
let ans = f(0, 1, 2);
ans = Math.max(ans, f(0, 2, 1));
ans = Math.max(ans, f(1, 0, 2));
ans = Math.max(ans, f(1, 2, 0));
ans = Math.max(ans, f(2, 0, 1));
ans = Math.max(ans, f(2, 1, 0));
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Sort the array in O(n log n), then scan for the missing or unique element by comparing adjacent pairs. Sorting requires O(n) auxiliary space (or O(1) with in-place sort but O(n log n) time remains). The sort step dominates.
Bitwise operations (AND, OR, XOR, shifts) are O(1) per operation on fixed-width integers. A single pass through the input with bit operations gives O(n) time. The key insight: XOR of a number with itself is 0, which eliminates duplicates without extra space.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.