[LeetCode] Maximum Possible Number by Binary Concatenation

3309. 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
int build(vector<int>& A) {
int res = 0;
for(auto& x : A) {
int shift = 32 - __builtin_clz(x);
res = res<<shift | x;
}
return res;
}
public:
int maxGoodNumber(vector<int>& nums) {
sort(begin(nums), end(nums));
int res = 0;
do {
res = max(res, build(nums));
}while(next_permutation(begin(nums), end(nums)));
return res;
}
};


Author: Song Hayoung
Link: https://songhayoung.github.io/2024/10/06/PS/LeetCode/maximum-possible-number-by-binary-concatenation/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.