Find XOR of all numbers in a given range

前端 未结 5 1341
温柔的废话
温柔的废话 2020-11-30 16:02

You are given a large range [a,b] where \'a\' and \'b\' can be typically between 1 and 4,000,000,000 inclusive. You have to find out the XOR of all the numbers in the given

5条回答
  •  囚心锁ツ
    2020-11-30 16:40

    To support XOR from 0 to N the code given needed to be modified as below,

    int f(int a) {
        int []res = {a, 1, a+1, 0};
        return res[a % 4];
    }
    
    int getXor(int a, int b) {
        return f(b) ^ f(a);
    }
    

提交回复
热议问题