- Related Questions & Answers
- Count smaller values whose XOR with x is greater than x in C++
- Count numbers whose difference with N is equal to XOR with N in C++
- Count numbers whose XOR with N is equal to OR with N in C++
- Count the nodes whose sum with X is a Fibonacci number in C++
- Find a Number X whose sum with its digits is equal to N in C++
- Count pairs from two BSTs whose sum is equal to a given value x in C++
- Count pairs in a binary tree whose sum is equal to a given value x in C++
- Count quadruples from four sorted arrays whose sum is equal to a given value x in C++
- Count pairs from two sorted arrays whose sum is equal to a given value x in C++
- Count triplets in a sorted doubly linked list whose sum is equal to a given value x in C++
- Count smaller numbers whose XOR with n produces greater value in C++
- Count pairs in a sorted array whose sum is less than x in C++
- Count of values of x <= n for which (n XOR x) = (n – x) in C++
- Count elements such that there are exactly X elements with values greater than or equal to X in C++
- Program to find X for special array with X elements greater than or equal X in Python

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

We are a number X. The goal is to find numbers between 0 and X whose sum with X is equal to XOR with X.

We will do this by traversing no. from i=0 to i<=X and for each i, if (i+X==i^X) then increment count.

Let us understand with examples.

**Input** − X=6

**Output** − Count of numbers whose sum with X == XOR with X: 2

**Explanation** − Numbers are 0 and 1 only.

**Input** − X=20

**Output** − Count of numbers whose sum with X == XOR with X: 8

**Explanation** − Numbers are 0 1 2 3 8 9 10 11

We take integer X.

Function sumisXOR(int x) takes x and returns a count of numbers whose sum with x is equal to xor with x.

Take the initial count as 0.

Traverse from i=0 to i<=x.

If i+x==i^x. Increment count

At the end of for loop count will have the desired result...

Return count and print.

#include <bits/stdc++.h> #include <math.h> using namespace std; int sumisXOR(int x){ int count = 0; for (int i = 0; i <= x; i++){ if((i+x)==(i^x)) { count++; } } return count; } int main(){ int X = 15; int nums=sumisXOR(X); cout <<endl<<"Count of numbers whose sum with X == XOR with X: "<<nums; return 0; }

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

Count of numbers whose sum with X == XOR with X: 1

Advertisements