
- 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
Minimum removals in a number to be divisible by 10 power raised to K in C++
Problem statement
Given two positive integers N and K. Find the minimum number of digits that can be removed from the number N such that after removals the number is divisible by 10K. Print -1 if it is impossible.
Example
If N = 10203027 and K = 2 then we have to remove 3 digits. If we remove 3, 2 and 7 then number become 10200 which is divisible by 102
Algorithm
1. Start traversing number from end. If the current digit is not zero, increment the counter variable, otherwise decrement variable K 2. If K is zero, then return counter as answer 3. After traversing the whole number, check if the current value of K is zero or not. If it is zero, return counter as answer, otherwise return answer as number of digits in N –1 4. If the given number does not contain any zero, return -1 as answer
Example
#include <bits/stdc++.h> using namespace std; int getBitsToBeRemoved(int n, int k) { string s = to_string(n); int result = 0; int zeroFound = 0; for (int i = s.size() - 1; i >= 0; --i) { if (k == 0) { return result; } if (s[i] == '0') { zeroFound = 1; --k; } else { ++result; } } if (!k) { return result; } else if (zeroFound) { return s.size() - 1; } return - 1; } int main() { int n = 10203027; int k = 2; cout << "Minimum required removals = " << getBitsToBeRemoved(n, k) << endl; return 0; }
When you compile and execute above program. It generates following output
Output
Minimum required removals = 3
- Related Articles
- EXPONENTS AND POWER:If the question is that 10 raised by power 22 + 10 raised by power 20, divided by 10 raised by power 20, then calculate the answer.
- K-th digit in ‘a’ raised to power ‘b’ in C++
- Number of digits in 2 raised to power n in C++
- Check if a number can be expressed as x^y (x raised to power y) in C++
- Maximum and Minimum element of a linked list which is divisible by a given number k in C++
- Program to count number of trailing zeros of minimum number x which is divisible by all values from 1 to k in Python
- Number of digits to be removed to make a number divisible by 3 in C++
- Largest K digit number divisible by X in C++
- Minimum removals to make array sum even in C++
- Minimum removals to make array sum odd in C++
- Largest number smaller than or equal to N divisible by K in C++
- A number is divisible by $12$. By what other numbers will that number be divisible?
- Minimum removals from array to make GCD Greater in Python
- Minimum removals from array to make GCD Greater in C++
- Print digit’s position to be removed to make a number divisible by 6 in C++

Advertisements