
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Check if a number can be represented as sum of non zero powers of 2 in C++
In this article, our task is to check if we can represent a given number as the sum of two non-zero powers of 2 in C++. For this, we will check whether the given number N can be represented as (2^x + 2^y) where x, y > 0. Here are some example scenarios:
Scenario 1
Input:10 Output: Can be represented Explanation: 10 = 8 + 2 10 = 2^3 + 2^1 => 10 can be represented as 2x + 2y
Scenario 2
Input:3 Output: Can be represented Explanation: 10 = 2 + 1 10 = 2^1 + 2^0 (Power should be greater than 0) => 3 cannot be represented as 2x + 2y
Checking if a number can be represented as sum of non zero powers of 2 in C++
The approaches to check if we can represent a given number as 2^x + 2^y are given below:
Using Brute Force Technique
In the brute force technique, we have used a nested for loop to check if the given number can be represented as 2^x + 2^y. The outer and inner for loops calculate 2^x and 2^y, respectively. Here are the steps:
- Define a boolean function check() which accepts the integer value that you want to check.
- Use a nested for loop where the outer for loop calculates 2^x until it reaches n.
- Similarly, the inner loop calculates 2^y until it reaches n.
- Then, using an if statement, check if the summation of any combination of 2^x and 2^y equals the given n.
- Return true if the sum of any combination of 2^x and 2^y is equal to the given n.
Example
Here is the code example of the above-mentioned steps using a nested for loop to represent the given number as 2^x + 2^y:
#include <iostream> using namespace std; bool check(int n) { for (int x = 1; (1 << x) <= n; x++) // calculates 2^x { for (int y = 1; (1 << y) <= n; y++) // calculates 2^x { if ((1 << x) + (1 << y) == n) return true; } } return false; } int main() { int n = 14; if (check(n)) { cout << "Yes, " << n << " can be expressed as 2^x + 2^y"; } else { cout << "No, " << n << " cannot be expressed as 2^x + 2^y"; } return 0; }
The output of the above code is as follows:
No, 14 cannot be expressed as 2^x + 2^y
Using Mathematical Method
In this approach, we use a for loop to calculate the value of 2^x. Then the value of 2^x is subtracted from the given n, and it is checked if it is also a power of 2. If both the values are a power of 2, then it can be represented as 2^x + 2^y. The steps to implement this are given below:
- The boolean function checkSum() calculates the 2^x using the left shift operator until it reaches the given integer value n.
- Then, calculate the remaining value after subtracting the value of 2^x from the number n.
- The remaining value is then used in the checkPower() to check if it is a power of 2. It returns true if the remaining is a power of 2.
- The above three steps are repeated for each value of 2^x in the first step until it reaches n.
- For any value of 2^x, if the remaining is also a power of 2, the checkSum() function returns true. It means the given number n can be represented as 2^x + 2^y.
Example
The code example of the above-mentioned steps is given below using a single for loop:
#include <iostream> using namespace std; bool checkPower(int num) { if (num <= 1) return false; while (num % 2 == 0) { num /= 2; } return num == 1; } bool checkSum(int n) { for (int x = 1; (1 << x) < n; x++) { int remaining = n - (1 << x); if (checkPower(remaining)) return true; } return false; } int main() { int n = 10; if (checkSum(n)) { cout << "Yes, " << n << " can be expressed as 2^x + 2^y"; } else { cout << "No, " << n << " cannot be expressed as 2^x + 2^y"; } return 0; }
The output of the above code is as follows:
Yes, 10 can be expressed as 2^x + 2^y
Using Bit Manipulation
To represent the given number n as 2^x + 2^y, we have used the bit manipulation method where we check if the number in binary form has 2 set bits(1) except at the unit place. If the set bit is at the unit place, then it becomes odd (5 = 0101). Here are the steps:
- First, check if the given number is non-zero and even.
- Then perform the AND operation on the given number n and (n-1). This removes one set bit from the number and is stored in the reduced.
- In the previous step, the set bit is reduced by 1, i.e., if earlier the number had 2 set bits, now it will have 1 set bit.
- Then we check if the reduced is non-zero and is a power of 2.
- For checking if reduced is a power of 2, we check if reduced has one set bit by performing an AND operation on reduced and (reduced - 1).
- If the value of this AND operation is equal to 0, then it is a power of 2, and the number n can be represented in the form 2^x + 2^y.
Example
Following is the code implementation of the above-mentioned steps using bit manipulation to check if the given number can be represented as 2^x + 2^y:
#include <iostream> using namespace std; bool check(int n) { if (n <= 0 || (n & 1)) return false; int reduced = n & (n - 1); bool checkPower = (reduced != 0) && ((reduced & (reduced - 1)) == 0); return checkPower; } int main() { int n = 20; if (check(n)) cout << n << " can be represented as 2^x + 2^y " << endl; else cout << n << " cannot be represented " << endl; return 0; }
The output of the above code is as follows:
20 can be represented as 2^x + 2^y
Complexity Comparison
Here is a comparison of the time and space complexity of all the above approaches.
Approach | Time Complexity | Space Complexity |
---|---|---|
Brute Force | O(log n)2 | O(1) |
Mathematical Method | O(log n)2 | O(1) |
Bit Manipulation | O(1) | O(1) |