- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Count ordered pairs of positive numbers such that their sum is S and XOR is K in C++

We are given two numbers S and K. The goal is to find ordered pairs of positive numbers such that their sum is S and XOR is K.

We will do this by starting from i=1 to i<S-1 and j=i+1 to j<S. If any pair (i,j) has sum==S and i^j==K (XOR). Then increment count by 2 as (i,j) and (j,i) both will be counted as different pairs.

Let’s understand with examples.

**Input**

S=10 K=4

**Output**

Ordered pairs such that sum is S and XOR is K: 2

**Explanation**

Pairs will be (3,7) and (7,3)

**Input**

S=12 K=6

**Output**

Ordered pairs such that sum is S and XOR is K: 0

**Explanation**

No such pairs possible.

## Approach used in the below program is as follows

We take integers S and K.

Function sumXOR(int s, int k) takes s and k and returns the count of ordered pairs with sum=s and xor=k

Take the initial variable count as 0 for pairs.

Traverse using two for loops for making pairs.

Start from i=1 to i<s-1. And j=i+1 to j<s-1.

Now for each pair (i,j) check if (i+j==s) && (i^j==k). If true increment count by 2 as (i,j) and (j,i) both are different pairs.

At the end of all loops count will have a total number of such pairs.

Return the count as result.

## Example

#include <bits/stdc++.h> using namespace std; int sumXOR(int s, int k){ int count = 0; for (int i = 1; i < s; i++){ for(int j=i+1; j<s-1; j++){ if( (i+j)==s && (i^j)==k){ count+=2; //(i,j) and (j,i) are two pairs } } } return count; } int main(){ int S = 9, K = 5; cout <<"Ordered pairs such that sum is S and XOR is K: "<< sumXOR(S,K); return 0; }

## Output

If we run the above code it will generate the following output −

Ordered pairs such that sum is S and XOR is K: 4

- Related Questions & Answers
- Find number of pairs in an array such that their XOR is 0 using C++.
- Print n numbers such that their sum is a perfect square
- Print numbers with digits 0 and 1 only such that their sum is N in C Program.
- Maximum sum of distinct numbers such that LCM of these numbers is N in C++
- Count pairs in array whose sum is divisible by K in C++
- Count of pairs of (i, j) such that ((n % i) % j) % n is maximized in C++
- Count all Quadruples from four arrays such that their XOR equals to ‘x’ in C++
- Count pairs in an array such that at least one element is prime in C++
- Count pairs (i,j) such that (i+j) is divisible by both A and B in C++
- Count pairs of numbers from 1 to N with Product divisible by their Sum in C++
- Count pairs of parentheses sequences such that parentheses are balanced in C++
- Count pairs in an array such that frequency of one is at least value of other in C++
- Count unordered pairs (i,j) such that product of a[i] and a[j] is power of two in C++
- Count numbers whose sum with x is equal to XOR with x in C++
- Count pairs with Odd XOR in C++