Given an array of positive integers nums of length N, we need to find the maximum sum of two integers that do not have any common digit between them.
1<=N<=2e5 1<=nums[i]<=1e9
Eg. N = 6 nums = 53 1 36 103 53 5 ans = 103+5 = 108
This question was asked as part of Microsoft Online Assessment








you can use bit masking here.
for any number i from 1 — 2^10
ans[i] = maximum number in the original array which consist of digits same as the set bits in number i,
ex — i = 7, ans[i] = maximum number in the original array which consists of digits 0, 1, 2 as 2^0 + 2^1 + 2^2 = 7
now after calculating maximums you can find maximum ans by considering two numbers i,j in range 1 — 2^10
if((i&j)==0) final_ans = max(final_ans,ans[i]+ans[j])
Thanks a lot! Really appreciate!
Nice solution..but how do we find maximum number in the array consisting of digits same as set bits faster?
iterate over all elements of the array, for any element get all the digits in it and update the corresponding mask value
Can you please explain for Testcase [15,0,105] Output is 15
sum of 15 and 0 is 15
15 and 0 have no common digits between them. Since 105 can't be paired with any other element, and we have to output sum of two elements, we print 15.
def max_sum_without_common_digit(nums): max_num = {} max_sum = -1
for num in nums: max_val = int(''.join(sorted(str(num), reverse=True))) max_num[max_val] = max(max_num.get(max_val, 0), num) for num in nums: for key in max_num.keys(): if not any(digit in str(num) for digit in str(key)): max_sum = max(max_sum, num + max_num[key]) return max_sumnums = [15, 0, 105] print(max_sum_without_common_digit(nums))
you can use five
~symbols before and after your code to make it look more readable.`~~~~~
Your code here...
~~~~~`
(ignore ` symbols)
(However, you have to write a code before a text).
Working well on TC's
I see you are using two cycles, so I assume it will be TL under large constraints such as $$$n=2e5$$$ with a time limit of (usually) 1 second. Since there may be hidden constants and interruptions, the running time of this code may differ from the calculations.
I actually Submitted this code after getting tle on brute force code and it went well foe hidden cases too !!
It worked well on OA
Can you share the problem link?
It was asked in an Online Assessment and I submitted this code there and it passed all hidden cases too
sorry, I didn't know everything about your code, so I just wrote what I saw and thought it would get Time Limit verdict.
No worries :)
We can use Bitmasking to represent the digits present in each number. There are only $$$2^{10} = 1024$$$ possible unique digit combinations (masks) :
Maximum Time Complexity -> O(1024 * 1024)
Hello, I'm a beginner in competitive programming, and for the given question I find your solution quite intuitive and also valid (I might be wrong), but despite that why do you happen to have 6 downvotes? Is something wrong with your solution that I'm missing?
Solution is correct, I dont know why I got downvotes. Maybe because its a intuitive solution.
Approach: Bitmasking
We need to select two numbers such that: 1. Their sum is maximum. 2. They do not share any common digit. Example:
123and45can be selected because their digit sets{1, 2, 3}and{4, 5}are disjoint. Since decimal digits range from0to9, we use 10 bits to represent the digits present in a number. - Bit0represents digit0. - Bit1represents digit1. - ... - Bit9represents digit9. If a digit is present in a number, its corresponding bit is set to1. Example: Number = 123cpp mask = (1 << 1) | (1 << 2) | (1 << 3); mask = 2 + 4 + 8 = 14;Code
Code Explanation
1. Initialize all possible masks
cpp vector<int> masks(1 << 10, 0);There are (2^{10} = 1024) possible masks, ranging from0to1023.masks[mask]stores the maximum input number having that digit mask. We only need the maximum number for each mask because numbers with the same mask have identical digit compatibility with other numbers. 2. Build the digit maskcpp while (temp) { int last_digit = temp % 10; mask |= (1 << last_digit); temp /= 10; }For123: - Extract digit3and set bit3. - Extract digit2and set bit2. - Extract digit1and set bit1. The resulting mask is14.cpp masks[mask] = max(masks[mask], x);This stores the largest number having that mask. 3. Check whether two masks share digitscpp if ((i & j) == 0)The bitwise AND operator&identifies bits set in both masks. - If(i & j) != 0, the masks share at least one digit. - If(i & j) == 0, the masks have no common digits. 4. Calculate the maximum sumcpp ans = max(ans, masks[i] + masks[j]);We check every pair of compatible masks and updateanswith the maximum sum.Complexity Analysis
Let
nbe the number of input elements andDthe maximum number of digits in a number. - Building masks: (O(nD)) - Checking all pairs of masks: (O(1024^2) = O(2^{20})) - Extra space: (O(1024)) Overall time complexity: (O(nD + 2^{20})) Note: This implementation assumes non-negative input numbers and initializes the answer to0. If the input can contain0, handle it explicitly by setting its digit mask to1(bit0). Also, if two distinct input elements must be selected, the code should account for that requirement when considering identical masks.