# Count pairs from 1 to N such that their Sum is divisible by their XOR

• Last Updated : 28 May, 2022

Given a number , the task is to count pairs (x, y) such that their sum (x+y) is divisible by their xor value (x^y) and the condition 1 ≤ x < y < N holds true.
Examples

```Input: N = 3
Output: 3
Explanation:
(1, 2), (1, 3), (2, 3) are the valid pairs

Input: N = 6
Output: 11```

Approach:

• After taking the array as input, first we need to find out all the possible pairs in that array.
• So, find out the pairs from the array
• Then for each pair, check whether the sum of the pair is divisible by the xor value of the pair. If it is, then increase the required count by one.
• When all the pairs have been checked, return or print the count of such pair.

Below is the implementation of the above approach:

## C++

 `// C++ program to count pairs from 1 to N` `// such that their Sum is divisible by their XOR`   `#include ` `using` `namespace` `std;`   `// Function to count pairs` `int` `countPairs(``int` `n)` `{` `    ``// variable to store count` `    ``int` `count = 0;`   `    ``// Generate all possible pairs such that` `    ``// 1 <= x < y < n` `    ``for` `(``int` `x = 1; x < n; x++) {` `        ``for` `(``int` `y = x + 1; y <= n; y++) {` `            ``if` `((y + x) % (y ^ x) == 0)` `                ``count++;` `        ``}` `    ``}`   `    ``return` `count;` `}`   `// Driver code` `int` `main()` `{` `    ``int` `n = 6;`   `    ``cout << countPairs(n);`   `    ``return` `0;` `}`

## Java

 `// Java program to count pairs from 1 to N ` `// such that their Sum is divisible by their XOR ` `class` `GFG ` `{` `    `  `    ``// Function to count pairs ` `    ``static` `int` `countPairs(``int` `n) ` `    ``{ ` `        ``// variable to store count ` `        ``int` `count = ``0``; ` `    `  `        ``// Generate all possible pairs such that ` `        ``// 1 <= x < y < n ` `        ``for` `(``int` `x = ``1``; x < n; x++) ` `        ``{ ` `            ``for` `(``int` `y = x + ``1``; y <= n; y++) ` `            ``{ ` `                ``if` `((y + x) % (y ^ x) == ``0``) ` `                    ``count++; ` `            ``} ` `        ``} ` `        ``return` `count; ` `    ``} ` `    `  `    ``// Driver code ` `    ``public` `static` `void` `main (String[] args)` `    ``{ ` `        ``int` `n = ``6``; ` `        ``System.out.println(countPairs(n)); ` `    ``} ` `}`   `// This code is contributed by AnkitRai01`

## Python3

 `# Python3 program to count pairs from 1 to N ` `# such that their Sum is divisible by their XOR `   `# Function to count pairs ` `def` `countPairs(n) : `   `    ``# variable to store count ` `    ``count ``=` `0``; `   `    ``# Generate all possible pairs such that ` `    ``# 1 <= x < y < n ` `    ``for` `x ``in` `range``(``1``, n) :` `        ``for` `y ``in` `range``(x ``+` `1``, n ``+` `1``) : ` `            ``if` `((y ``+` `x) ``%` `(y ^ x) ``=``=` `0``) :` `                ``count ``+``=` `1``; `   `    ``return` `count; `   `# Driver code ` `if` `__name__ ``=``=` `"__main__"` `: `   `    ``n ``=` `6``; `   `    ``print``(countPairs(n)); `   `# This code is contributed by AnkitRai01`

## C#

 `// C# program to count pairs from 1 to N ` `// such that their Sum is divisible by their XOR ` `using` `System;`   `public` `class` `GFG ` `{` `    `  `    ``// Function to count pairs ` `    ``static` `int` `countPairs(``int` `n) ` `    ``{ ` `        ``// variable to store count ` `        ``int` `count = 0; ` `    `  `        ``// Generate all possible pairs such that ` `        ``// 1 <= x < y < n ` `        ``for` `(``int` `x = 1; x < n; x++) ` `        ``{ ` `            ``for` `(``int` `y = x + 1; y <= n; y++) ` `            ``{ ` `                ``if` `((y + x) % (y ^ x) == 0) ` `                    ``count++; ` `            ``} ` `        ``} ` `        ``return` `count; ` `    ``} ` `    `  `    ``// Driver code ` `    ``public` `static` `void` `Main()` `    ``{ ` `        ``int` `n = 6; ` `        ``Console.WriteLine(countPairs(n)); ` `    ``} ` `}`   `// This code is contributed by AnkitRai01`

## Javascript

 ``

Output:

`11`

Time Complexity: O(N2),  as we are using nested loops to traverse N*N times.
Auxiliary Space: O(1), as we are not using any extra space.

My Personal Notes arrow_drop_up
Recommended Articles
Page :