
- 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
C++ program to count minimum number of binary digit numbers needed to represent n
Suppose we have a number n. A number is a binary decimal if it's a positive integer and all digits in its decimal notation are either 0 or 1. For example, 1001 (one thousand and one) is a binary decimal, while 1021 are not. From the number n, we have to represent n as a sum of some (not necessarily distinct) binary decimals. Then compute the smallest number of binary decimals required for that.
So, if the input is like n = 121, then the output will be 2, because this can be represented as 110 + 11 or 111 + 10.
Steps
To solve this, we will follow these steps −
ans := -1 while n > 0, do: ans := maximum of ans and (n mod 10) n := n / 10 return ans
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; int solve(int n) { int ans = -1; while (n > 0) { ans = max(ans, n % 10); n /= 10; } return ans; } int main() { int n = 121; cout << solve(n) << endl; }
Input
121
Output
2
- Related Articles
- C++ program to count minimum number of operations needed to make number n to 1
- C++ program to count number of minimum coins needed to get sum k
- Count of Binary Digit numbers smaller than N in C++
- C++ program to count number of operations needed to reach n by paying coins
- Represent a Number as Sum of Minimum Possible Pseudo-Binary Numbers in C++
- Program to partitioning into minimum number of Deci- Binary numbers in Python
- Count n digit numbers divisible by given number in C++
- C++ program to find minimum how many coins needed to buy binary string
- Minimum number of letters needed to make a total of n in C++.
- Program to count number of stepping numbers of n digits in python
- Program to count minimum deletions needed to make character frequencies unique in Python
- Program to count minimum number of operations required to make numbers non coprime in Python?
- Program to find minimum number of Fibonacci numbers to add up to n in Python?
- Program to find minimum number of rocketships needed for rescue in Python
- Program to check minimum number of characters needed to make string palindrome in Python

Advertisements