I recently took an OA with a question involving n arrays of size m. The task was to create an array A of size n by choosing one element from each array. Then, we had to find the minimum value of A0 | A1 | A2 | ... | An-1 (bitwise OR of all elements in A) and n<1000,m<1000 and each element in those n arrays is less than 100000.

I used recursive DP, considering two options: either go forward in the same array or pick the current element and move to the start of the next array. However, this approach gave incorrect results for 3 out of 10 test cases. Can anyone help me identify what I missed or if I made an unforced error?

Auto comment: topic has been updated by bus_u_he (previous revision, new revision, compare).Share your code

github link

If you managed to take some minimum value from the first array, that does not mean that it is the best idea to use that value. Ex: first array: [1, 2], second array: [2, 2]. If you take 1 from the first array because it is smaller, then you get the answer of 3, but the minimum value is 2.

One corect solution is as follows:

For each bit from most signifficant to least signifficant check if there is some element in each array that has that bit off. If so, then there is some way to pick the numbers so you don't have this bit set in the answer. Then you need to reduce each array. Remove the numbers that have that bit set since if you take them, you break the minimality.

If however there is some array where no element has the bit off then there is no way to get this bit off in the final solution. No array reduction is required, as you can take either on or off numbers without influencing this bit.

This way you build both the minimum value, and (can then build) all the posibilities (each array only has "useful" values at the end so picking any one of them from each array constitutes a good solution)

The time complexity is $$$O(N\cdot M\cdot \log A)$$$ where $$$A$$$ is the biggest value in the input.

thanks a lot mate, now I know why I was wrong

But where is his solution wrong? He is checking all possible cases, his test case may be failing because of max limit of integer.

No, he is not. He is doing a greedy and masking it as a dp.

I am trying to code up your logic but couldn't come up with a solution can you please send the code or give a little more detailed answer please

The breaking logic can also be made with some booleans but I personally prefer this way. Tell me if you still don't understand something

Thanks a lot ! Now I understand the code and its intuition as well, all thanks to you. You are truly "The-Winner." By the way, was this logic part of some theoretical stuff or ad-hoc? Again, orz

The logic/intuition comes from a data structure called a trie (I think it is also known as a prefix tree). Here is a wikipedia article. It is very useful

what were the constraints? I have an O(32 * n * m * log(m)) algorithm. Idea is similar to above comment but I use sets, so additional log(m) factor.

for the 2nd if statement return INT_MAX instead of 1e7.

i have edited the blog to clarify the constraints

Your expected output is wrong, I don't know how you got there. Stress test it or find my implementation below, both produces 520093663.

Auto comment: topic has been updated by bus_u_he (previous revision, new revision, compare).Implementing The-Winner's solution cuz I liked it. Other than that, maybe it can be useful for some people:

CodeIf you want to solve another problem that uses an idea similar to the one suggested by The-Winner you can try this.

I think we can use a greedy idea.