3309. Maximum Possible Number by Binary Concatenation LeetCode Solution

In this guide, you will get 3309. Maximum Possible Number by Binary Concatenation LeetCode Solution with the best time and space complexity. The solution to Maximum Possible Number by Binary Concatenation problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Maximum Possible Number by Binary Concatenation solution in C++
  4. Maximum Possible Number by Binary Concatenation solution in Java
  5. Maximum Possible Number by Binary Concatenation solution in Python
  6. Additional Resources
3309. Maximum Possible Number by Binary Concatenation LeetCode Solution image

Problem Statement of Maximum Possible Number by Binary Concatenation

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 == 3
1 <= nums[i] <= 127

Complexity Analysis

  • Time Complexity: O(\texttt{sort})
  • Space Complexity: O(\texttt{sort})

3309. Maximum Possible Number by Binary Concatenation LeetCode Solution in C++

class Solution {
 public:
  int maxGoodNumber(vector<int>& nums) {
    int ans = 0;

    ranges::sort(nums,
                 [this](int a, int b) { return concat(a, b) > concat(b, a); });

    for (const int num : nums)
      ans = concat(ans, num);

    return ans;
  }

 private:
  // Returns the concatenation of the binary representations of a and b.
  int concat(int a, int b) {
    return (a << static_cast<int>(log2(b)) + 1) + b;
  }
};
/* code provided by PROGIEZ */

3309. Maximum Possible Number by Binary Concatenation LeetCode Solution in Java

class Solution {
  public int maxGoodNumber(int[] nums) {
    int ans = 0;
    Integer[] A = Arrays.stream(nums).boxed().toArray(Integer[] ::new);

    Arrays.sort(A, (a, b) -> Integer.compare(concat(b, a), concat(a, b)));

    for (final int a : A)
      ans = concat(ans, a);

    return ans;
  }

  // Returns the concatenation of the binary representations of a and b.
  private int concat(int a, int b) {
    return a * (Integer.highestOneBit(b) << 1) + b;
  }
}
// code provided by PROGIEZ

3309. Maximum Possible Number by Binary Concatenation LeetCode Solution in Python

class Solution:
  def maxGoodNumber(self, nums: list[int]) -> int:
    ans = 0

    def concat(a: int, b: int) -> int:
      """Returns the concatenation of the binary representations of a and b."""
      return (a << b.bit_length()) + b

    nums.sort(key=functools.cmp_to_key(
        lambda a, b: concat(b, a) - concat(a, b)))

    for num in nums:
      ans = concat(ans, num)

    return ans
# code by PROGIEZ

Additional Resources

See also  1959. Minimum Total Space Wasted With K Resizing Operations LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.