

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
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 Questions & Answers
- C++ program to count minimum number of operations needed to make number n to 1
- Count of Binary Digit numbers smaller than N in C++
- C++ program to count number of minimum coins needed to get sum k
- Represent a Number as Sum of Minimum Possible Pseudo-Binary Numbers in C++
- C++ program to count number of operations needed to reach n by paying coins
- Count n digit numbers divisible by given number in C++
- Minimum number of letters needed to make a total of n in C++.
- Number of n digit stepping numbers in C++
- Program to partitioning into minimum number of Deci- Binary numbers in Python
- Program to count number of stepping numbers of n digits in python
- Count n digit numbers not having a particular digit in C++
- Find minimum number of Log value needed to calculate Log upto N in C++
- 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
- C++ program to find minimum how many coins needed to buy binary string
Advertisements