Count distinct XOR values among pairs using numbers in range 1 to N

• Difficulty Level : Medium
• Last Updated : 07 Jan, 2022

Given a number N. The task is to count the number of distinct XOR of any possible pairs using numbers from 1 to N inclusive.

Examples:

Input: N = 3
Output: 4
Explanation: Following are the all possible pairs using elements from 1 to N inclusive.
1^1 = 0
1^2 = 3
1^3 = 2
2^2 = 0
2^3 = 1
3^3 = 0
Therefore, there are 4 distinct possible XOR values.

Input: N = 2
Output: 2

Approach: This problem is observation-based. Follow the steps below to solve the given problem.

• For N = 1 and N = 2 the require output are 1 and 2 respectively.
• Now for Nâ‰¥3, there are two possible cases:
• If N is not a power of two then,  there will be a total of ‘t’ numbers where t is the power of 2 which is just next to the number N. They vary from [0 to t – 1]. Because let’s say, N = 5 or 6 or 7 In all the above cases values varies from [0, 1, 2, 3, 4, 5, 6, 7].
• If N is a power of two then, all the numbers will be possible except the number itself. Because let’s say, N = 4 in binary it is “100” so as we know XOR operation gives 1″ if both bits are different else it gives 0. Possible values are [0, 1, 2, 3,    4, 5, 6, 7].

Below is the implementation of the above approach.

C++

 `// C++ code to implement above approach``#include ``#include ``using` `namespace` `std;` `int` `findDistinctXOR(``int` `n)``{` `    ``// Corner case``    ``if` `(n == 1) {``        ``cout << 1 << endl;``    ``}``    ``if` `(n == 2) {``        ``cout << 2 << endl;``    ``}` `    ``long` `long` `x, next;``    ``x = log2(n);` `    ``// if n is power of two``    ``if` `((n & (n - 1)) == 0) {` `        ``next = ``pow``(2, x + 1);``        ``cout << next - 1 << endl;``    ``}``    ``else` `{``        ``next = ``pow``(2, x + 1);``        ``cout << next << endl;``    ``}``}` `// Driver Code``int` `main()``{``    ``int` `N = 3;` `    ``findDistinctXOR(N);``    ``return` `0;``}`

Java

 `// Java code to implement above approach``//include ``import` `java.util.*;``class` `GFG``{` `static` `void` `findDistinctXOR(``int` `n)``{` `    ``// Corner case``    ``if` `(n == ``1``) {``        ``System.out.print(``1` `+``"\n"``);``    ``}``    ``if` `(n == ``2``) {``        ``System.out.print(``2` `+``"\n"``);``    ``}` `    ``long` `x, next;``    ``x = (``long``) Math.log(n);` `    ``// if n is power of two``    ``if` `((n & (n - ``1``)) == ``0``) {` `        ``next = (``long``) Math.pow(``2``, x + ``1``);``        ``System.out.print(next - ``1` `+``"\n"``);``    ``}``    ``else` `{``        ``next = (``long``) Math.pow(``2``, x + ``1``);``        ``System.out.print(next +``"\n"``);``    ``}``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``    ``int` `N = ``3``;` `    ``findDistinctXOR(N);``}``}` `// This code is contributed by 29AjayKumar`

Python3

 `# Python code for the above approach``import` `math as Math` `def` `findDistinctXOR(n):` `    ``# Corner case``    ``if` `(n ``=``=` `1``):``        ``print``(``1``)``    ``if` `(n ``=``=` `2``):``        ``print``(``2``)``    ``x ``=` `None``    ``next` `=` `None``    ``x ``=` `Math.floor(Math.log2(n))` `    ``# if n is power of two``    ``if` `((n & (n ``-` `1``)) ``=``=` `0``):``        ``next` `=` `Math.``pow``(``2``, x ``+` `1``)``        ``print``((``next` `-` `1``))``    ``else``:``        ``next` `=` `Math.``pow``(``2``, x ``+` `1``)``        ``print``(``int``(``next``))` `# Driver Code``N ``=` `3``findDistinctXOR(N)` `# This code is contributed by Saurabh Jaiswal`

C#

 `// C# code to implement above approach``using` `System;` `class` `GFG{` `static` `void` `findDistinctXOR(``int` `n)``{``    ` `    ``// Corner case``    ``if` `(n == 1)``    ``{``        ``Console.WriteLine(1);``    ``}``    ``if` `(n == 2)``    ``{``        ``Console.WriteLine(2);``    ``}` `    ``long` `x, next;``    ``x = (``long``)Math.Log(n, 2);` `    ``// If n is power of two``    ``if` `((n & (n - 1)) == 0)``    ``{``        ``next = (``long``)Math.Pow(2, x + 1);``        ``Console.WriteLine(next - 1);``    ``}``    ``else``    ``{``        ``next = (``long``)Math.Pow(2, x + 1);``        ``Console.WriteLine(next);``    ``}``}` `// Driver Code``public` `static` `void` `Main()``{``    ``int` `N = 3;``    ` `    ``findDistinctXOR(N);``}``}` `// This code is contributed by ukasp`

Javascript

 ``

Output

`4`

Time Complexity: O(1)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up