
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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 Articles
- Find number of pairs in an array such that their XOR is 0 using C++.
- Count pairs in array whose sum is divisible by K in C++
- Count all Quadruples from four arrays such that their XOR equals to ‘x’ in 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.
- Count pairs with Odd XOR in C++
- Count numbers whose sum with x is equal to XOR with x in C++
- Count pairs of numbers from 1 to N with Product divisible by their Sum in C++
- Count number of ordered pairs with Even and Odd Product in C++
- Count number of ordered pairs with Even and Odd Sums in C++
- Count all pairs with given XOR in C++
- Count of pairs of (i, j) such that ((n % i) % j) % n is maximized in C++
- Count all pairs of adjacent nodes whose XOR is an odd number in C++
- Maximum sum of distinct numbers such that LCM of these numbers is N in C++
- Count pairs in an array such that at least one element is prime in C++
