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 = 3Output: 3Explanation: (1, 2), (1, 3), (2, 3) are the valid pairsInput: N = 6Output: 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:

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 <bits/stdc++.h> ` `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; ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

**Output:**

11

**Time Complexity :** O(N^{2})

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count ordered pairs of positive numbers such that their sum is S and XOR is K
- Count the number of pairs (i, j) such that either arr[i] is divisible by arr[j] or arr[j] is divisible by arr[i]
- Count of pairs in a given range with sum of their product and sum equal to their concatenated number
- Find number of pairs in an array such that their XOR is 0
- Count pairs of numbers from 1 to N with Product divisible by their Sum
- Queries to count integers in a range [L, R] such that their digit sum is prime and divisible by K
- Count all Quadruples from four arrays such that their XOR equals to 'x'
- Largest number M having bit count of N such that difference between their OR and XOR value is maximized
- Print all the combinations of N elements by changing sign such that their sum is divisible by M
- Possible values of Q such that, for any value of R, their product is equal to X times their sum
- Count pairs (i,j) such that (i+j) is divisible by A and B both
- Count pairs of elements such that number of set bits in their AND is B[i]
- Count pairs of elements such that number of set bits in their OR is B[i]
- Count the pairs in an array such that the difference between them and their indices is equal
- Count of pairs in an array such that the highest power of 2 that divides their product is 1
- Count of pairs upto N such whose LCM is not equal to their product for Q queries
- Choose X such that (A xor X) + (B xor X) is minimized
- Find a number M < N such that difference between their XOR and AND is maximum
- Split an array into groups of 3 such that X3 is divisible by X2 and X2 is divisible by X1
- Find two numbers made up of a given digit such that their difference is divisible by N

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.