The given is N non-negative numbers, select exactly K who provide minimal result of OR operation (binary OR operation).
Input The first line contains an integer N (1 ≤ K ≤ N ≤ 50). The next line contains N integers not exceeding 1000 000 000
Output
Print result of OR operation selected K numbers.
Examples:
entrance
2 2
1 3
output
3
entrance
6 3
5 2 4 1 7 5
output
5
Any ideas how to solve it?
Where I can submit it?
It is the task form Croatian contest, but if you want to submit you can use this link https://zatemas.zrs.hr/?app=evaluator&evlshw=solver&cd=Bo%C5%BEo%2F2013%2FDrugi+ogled%2F&evlsolve=Ili That link provide you on task at the evaluator. And you must be register to submit solution.
Can you submit this code?
16/160 points
backtracking should be enough if you prune the search when possible.
Could you please submit my solution
Thanks.
You can do this using a greedy algorithm. Let 2^x be the highest power of two smaller than or equal to the biggest of the integers in the input.
If there are less than k numbers with bit x not set (easy case): you will be forced to pick at least one number with bit x set, so answer will have bit x set no matter what you do.
If there are k or more numbers with bit x not set: it's always better to ignore all numbers in the input that have bit x set, because by not picking it you always get a smaller answer.
I submitted this solution and it was accepted, but I'll leave the implementation to you.