Number of pairs with constant difference and bitwise AND zero

前端 未结 2 576
猫巷女王i
猫巷女王i 2021-01-27 11:31

How to find the number of pairs whose difference is a given constant and their bitwise AND is zero? Basically, all (x,y) such that x-y = k; where k is a given constant and x&

2条回答
  •  迷失自我
    2021-01-27 12:27

    Java implementation would be like this ...

    import java.util.ArrayList;
    
    public class FindPairs {
    
        public static void main(String args[]) {
            int arr[] = {1,3,4,5,6,9};
            int k = 3;
            ArrayList out = new ArrayList();
    
            for(int i=0; i0) {
                for(String pair:out) {
                    System.out.println(pair);
                }
            }else {
                System.out.println("No such pair !");
            }   
        }
    }
    

提交回复
热议问题