
- 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
Number of digits to be removed to make a number divisible by 3 in C++
You are given a number in string. You need to find how many digits need to be removed to make it divisible by 3.
We make a number divisible by removing at most 2 digits. So, the maximum number of digits to be removed to make it divisible by 3 is 2.
Let's see some examples.
Input
92
Output
1
We can remove 2 to make it divisible by 3.
Input
999
Output
0
The given number itself is divisible by 3.
Algorithm
Initialise the number in string.
Find the sum of the number.
If the sum is divisible by 3, then return 0.
If the sum is not divisible by 3 and the length of the number is 1, then we can't make it divisible by 3. Return -1.
Iterate over the number.
Remove one digit from the number and check the divisibility.
If the above condition satisfies, then return 1.
Check the length of the number again. If the length is 2, then return -1.
Else return 2.
Implementation
Following is the implementation of the above algorithm in C++
#include <bits/stdc++.h> using namespace std; int getNumSum(string n) { int len = n.length(), sum = 0; for (int i = 0; i < len; i++) { sum += (int)n[i]; } return sum; } int getDigitsCount(string num) { int n = num.length(); int sum = getNumSum(num); if (sum % 3 == 0) { return 0; } if (n == 1) { return -1; } for (int i = 0; i < n; i++) { int currentDigit = num[i] - '0'; if (sum % 3 == currentDigit % 3) { return 1; } } if (n == 2) { return -1; } return 2; } int main() { string num = "7536836"; cout << getDigitsCount(num) << endl; return 0; }
Output
If you run the above code, then you will get the following result.
1
- Related Articles
- Print digit’s position to be removed to make a number divisible by 6 in C++
- Possible to make a divisible by 3 number using all digits in an array in C++
- Python Program to check whether it is possible to make a divisible by 3 number using all digits in an array
- Java Program to check whether it is possible to make a divisible by 3 number using all digits in an array
- Replace m by smallest whole number to make the number resulting number divisible\n46 m 46 (by 3)
- C/C++ Program to check whether it is possible to make a divisible by 3 number using all digits in an array?
- Minimum number of elements to be removed to make XOR maximum using C++.
- C/C++ Program to check whether it is possible to make the divisible by 3 number using all digits in an array?
- If a number is divisible by 3 it must be divisible by 9. (True/False)
- C Program to check if a number is divisible by sum of its digits
- C Program to check if a number is divisible by any of its digits
- A number is divisible by $12$. By what other numbers will that number be divisible?
- A number is divisible by both 5 and 12. By which other number will that number be always divisible?
- Minimum number of elements that should be removed to make the array good using C++.
- Activity : Ask all the students in your class to write a 3-digit number. Choose any student from the room at random. What is the probability that the number written by her/him is divisible by 3 ? Remember that a number is divisible by 3 , if the sum of its digits is divisible by 3 .
