 GFG App
Open App Browser
Continue

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

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

Naive Approach:

The naive approach to solve this problem is to generate all possible pairs from 1 to N and then calculate their XOR and store it in a set. After calculating XOR of all pairs, the size of the set will be the count of distinct XOR of any possible pairs.

Algorithm:

1.    Initialize a variable count to 0.
2.    Loop from i = 1 to N, and within this loop, loop from j = i to N.
3.    For each pair (i, j), calculate the XOR value using i^j.
4.    If this XOR value is not already present in the set of distinct XOR values, add it to the set and increment the count              variable by 1.
5.    After both loops, return the count variable as the answer.

Below is the implementation of the approach:

## C++

 `// C++ code for the approach`   `#include ` `using` `namespace` `std;`   `// Function to coint distinct Xor` `int` `countDistinctXOR(``int` `n) {` `      ``// to store distinct XOR values` `    ``set<``int``> distinctXOR; ` `  `  `    ``for` `(``int` `i = 1; i <= n; i++) {` `        ``for` `(``int` `j = i; j <= n; j++) {` `          ``// calculating XOR of pair (i, j) ` `          ``int` `XOR = i ^ j;` `          `  `          ``// inserting XOR in set` `          ``distinctXOR.insert(XOR); ` `        ``}` `    ``}` `  `  `      ``// returning size of the set` `    ``return` `distinctXOR.size(); ` `}`   `// Driver's Code` `int` `main() {` `    ``int` `n = 3;` `  `  `    ``cout << countDistinctXOR(n) << endl;` `    ``return` `0;` `}`

Output

`4`

Time complexity: O(N^2) because the outer loop runs N times and the inner loop runs N-i times for each i, so the total number of iterations is 1+2+…+N = N*(N+1)/2.

Auxiliary Space: O(d) where d is number of distinct XOR possible. This is because set has been created to store values of XOR.

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(log(log(n)))
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up