
- 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
Represent a Number as Sum of Minimum Possible Pseudo-Binary Numbers in C++
This tutorial will discuss the representation of a number as a sum of minimum pseudo-binary numbers. Pseudo-binary numbers are the numbers that consist of only binary digits, i.e., 0 and 1. Examples of pseudo-binary numbers are 00, 11, 10, 100, 111, 1011, etc.
Below are some examples of numbers represented as the sum of pseudo-binary numbers.
Input : 23 Output : 11 + 11 + 1 Explanation : 23 = 11 + 11 + 1, sum of pseudo-binary numbers(11, 11, 1) is 23. Input : 50 Output : 10 + 10 + 10 + 10 + 10
Approach to Find the Solution
Below is one of the best approaches to find minimum pseudo-binary numbers to represent N.
Take a number X and update its digits to 1 or 0 according to the digits of the number N.
Check digit at each place of N,
If it is 0, then update that place of X to 0.
If it is not zero, update that place of X to 1.
Let’s say N = 32, then X will be 11
Then X will be one pseudo-binary number.
Now decrement N by X and repeat step1 until N becomes zero.
Example
C++ Code for the Above Approach
#include<iostream> using namespace std; int main(){ int N = 51; // find a pseudo-binary number until N becomes 0. cout << "pseudo-binary representation of " << N << " is: "; while (N > 0){ // finding X which contains 0's and 1's according to N. int temp = N; int X = 0, bit = 1; // checking each place of N for zero or non-zero. while (temp!=0){ int last_dig = temp % 10; temp = temp / 10; if (last_dig != 0) X += bit; bit *= 10; } // printing one pseudo-binary number. cout << X << " "; // Updating N by subtracting with X. N = N - X; } return 0; }
Output
pseudo-binary representation of 51 is: 11 10 10 10 10
Understanding the Code
An outer while loop for taking N and picking digits on every place to find X.
We are updating the value of the temp variable with N and Inner loop for checking each place of temp variable and updating that place of variable X.
Printing value of X because that is one pseudo-binary number.
We update N by subtracting with X and going to the outer loop again until N becomes 0.
Conclusion
In this tutorial, we have discussed how we can represent a number as a sum of minimum possible pseudo-binary numbers. We discussed the approach to find all the pseudo-binary numbers. We also discussed the C++ code for the same, which we can write in any other programming language like C, Java, Python, etc. We hope you find this tutorial helpful.
- Related Articles
- Represent a number as a Sum of Maximum Possible Number of Prime Numbers in C++
- C++ program to count minimum number of binary digit numbers needed to represent n
- Find minimum possible digit sum after adding a number d in C++
- Represent Int32 as a Binary String in C#
- Represent Int64 as a Binary string in C#
- All possible binary numbers of length n with equal sum in both halves?
- Program to partitioning into minimum number of Deci- Binary numbers in Python
- Express 18 as the sum of two prime numbers in all possible ways.
- Minimum number of palindromes required to express N as a sum using C++.
- Minimum number of operations required to sum to binary string S using C++.
- Minimum numbers needed to express every integer below N as a sum in C++
- Find the Minimum Number of Fibonacci Numbers Whose Sum Is K in C++
- Count ways to express a number as sum of consecutive numbers in C++
- Express an odd number as sum of prime numbers in C++
- Minimum sum path between two leaves of a binary trees in C++
