- 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

# 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 Questions & Answers
- 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
- 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?
- Number of substrings divisible by 8 and not by 3 in C++
- 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
- Find the number of boxes to be removed in C++
- Minimum number of elements that should be removed to make the array good using C++.
- Number of Groups of Sizes Two Or Three Divisible By 3 in C++
- Find N digits number which is divisible by D in C++
- Largest number with the given set of N digits that is divisible by 2, 3 and 5 in C++