Subset of array A in which if we do AND of all elements of that subset then output should be in power of two

房东的猫 提交于 2020-01-03 04:46:11

问题


I got this solution to a problem where it states:

Given an array A. Is there any subset of array A in which if we do AND of all elements of that subset then output should be in power of two (for example : 1,2,4,8,16 and so on ).

Input: First line contains number of test cases T. Each test first line contains N size of array A and next line contains N space separated integers.

Output: For each test case print YES if there is any subset of array A in which if we do AND of all elements of that subset then output should be in power of two else print NO.

The solution is like this but I could not understand the below solution. Please help.

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line = br.readLine();
        int N = Integer.parseInt(line);
        for (int i = 0; i < N; i++) {
            int num = Integer.parseInt(br.readLine());
            int[] arr = new int[num];
            String arrCnts = br.readLine();
            String[] arrStr = arrCnts.split(" ");
            boolean flag = false;
            int max = Integer.MIN_VALUE;
            for(int j = 0; j < num; j++) {
                arr[j] = Integer.parseInt(arrStr[j]);
                if(max < arr[j]) {
                    max = arr[j];
                }
            }

            int len = (int) (java.lang.Math.log10(max) / java.lang.Math.log10(2));

            for (int k = 0; k <= len; k++) {
                int mask = 1 << k, mul = -1;//ffffffff
                for (int j = 0; j < num; j++) {
                    if ((arr[j] & mask) != 0) {
                        if (mul == -1)
                            mul = arr[j];
                        else
                            mul &= arr[j];
                    }
                }
                if (mul == -1)
                    continue;
                else if (mul == mask) {
                    flag = true;
                    break;
                }
            }

            if(flag) {
                System.out.println("YES");
            } else {
                System.out.println("NO");
            }
        }


SAMPLE INPUT 
2
3
1 2 3
2
10 20

SAMPLE OUTPUT 
YES
NO

回答1:


The basic idea for the algorithm is this :

Let's note A_i the set of all the elements of A that have '1' in the ith position of their binary representation. If there is a subset S={s1,...,sn} of A such that AND(s1,...sn) = 2^i = 100...0 in binary (i zeroes), then the AND of all the elements of A_i is equal to 2^i, since S must be a subset of A_i.

What the algorithm does is compute the AND of the elements of A_i, for i from 0 to len, the number of bits used to write the biggest number of A minus 1. To do that, it uses a mask aptly named mask, equal to 2^i for i from 0 to len. Then, for each number in A, it checks if that number has a 1 in ith position by doing the test (arr[j]&mask)!=0. If that is the case, it updates mul, which originally is just all ones, with the AND of arr[j] with mul.

At the end of the inner loop, mul contains the AND of the elements of A_i. If for some i, mask==mul, then a subset that satisfies the property exists, otherwise no such subset exists.



来源:https://stackoverflow.com/questions/35990794/subset-of-array-a-in-which-if-we-do-and-of-all-elements-of-that-subset-then-outp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!