# Number of digits to be removed to make a number divisible by 3

Given a very large number num (1 <= num <= 10^1000), print the number of digits that needs to be removed to make the number exactly divisible by 3. If it is not possible then print -1.

**Examples :**

Input:num = "1234"Output:1 Explanation: we need to remove one digit that is 1 or 4, to make the number divisible by 3.onInput:num = "11"Output:-1 Explanation: It is not possible to remove any digits and make it divisible by 3.

The idea is based on the fact that a number is multiple of 3 if and only if sum of its digits is multiple of 3 (See this for details).

One important observation used here is that the answer is at most 2 if an answer exists. So here are the only options for the function:

- Sum of digits is already equal to 0 modulo 3. Thus, we don’t have to erase any digits.
- There exists such a digit that equals sum modulo 3. Then we just have to erase a single digit
- All the digits are neither divisible by 3 nor equal to sum modulo 3. So two of such digits will sum up to number, which equals sum modulo 3, (2+2) mod 3=1, (1+1) mod 3=2

## C++

`// CPP program to find the minimum number of` `// digits to be removed to make a large number` `// divisible by 3.` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// function to count the no of removal of digits` `// to make a very large number divisible by 3` `int` `divisible(string num)` `{` ` ` `int` `n = num.length();` ` ` `// add up all the digits of num` ` ` `int` `sum = accumulate(begin(num),` ` ` `end(num), 0) - ` `'0'` `* 1;` ` ` `// if num is already is divisible by 3` ` ` `// then no digits are to be removed` ` ` `if` `(sum % 3 == 0)` ` ` `return` `0;` ` ` `// if there is single digit, then it is` ` ` `// not possible to remove one digit.` ` ` `if` `(n == 1)` ` ` `return` `-1;` ` ` `// traverse through the number and find out` ` ` `// if any number on removal makes the sum` ` ` `// divisible by 3` ` ` `for` `(` `int` `i = 0; i < n; i++)` ` ` `if` `(sum % 3 == (num[i] - ` `'0'` `) % 3)` ` ` `return` `1;` ` ` `// if there are two numbers then it is` ` ` `// not possible to remove two digits.` ` ` `if` `(n == 2)` ` ` `return` `-1;` ` ` `// Otherwise we can always make a number` ` ` `// multiple of 2 by removing 2 digits.` ` ` `return` `2;` `}` `// Driver Code` `int` `main()` `{` ` ` `string num = ` `"1234"` `;` ` ` `cout << divisible(num);` ` ` `return` `0;` `}` |

## Java

`// Java program to find the` `// minimum number of digits` `// to be removed to make a` `// large number divisible by 3.` `import` `java.io.*;` `// function to count the no` `// of removal of digits` `// to make a very large` `// number divisible by 3` `class` `GFG {` ` ` `static` `int` `divisible(String num)` ` ` `{` ` ` `int` `n = num.length();` ` ` `// add up all the` ` ` `// digits of num` ` ` `int` `sum = ` `0` `;` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++)` ` ` `sum += (` `int` `)(num.charAt(i));` ` ` `// if num is already is` ` ` `// divisible by 3 then` ` ` `// no digits are to be` ` ` `// removed` ` ` `if` `(sum % ` `3` `== ` `0` `)` ` ` `return` `0` `;` ` ` `// if there is single digit,` ` ` `// then it is not possible` ` ` `// to remove one digit.` ` ` `if` `(n == ` `1` `)` ` ` `return` `-` `1` `;` ` ` `// traverse through the number` ` ` `// and find out if any number` ` ` `// on removal makes the sum` ` ` `// divisible by 3` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++)` ` ` `if` `(sum % ` `3` `== (num.charAt(i) - ` `'0'` `) % ` `3` `)` ` ` `return` `1` `;` ` ` `// if there are two numbers` ` ` `// then it is not possible` ` ` `// to remove two digits.` ` ` `if` `(n == ` `2` `)` ` ` `return` `-` `1` `;` ` ` `// Otherwise we can always` ` ` `// make a number multiple` ` ` `// of 2 by removing 2 digits.` ` ` `return` `2` `;` ` ` `}` ` ` `// Driver Code` ` ` `public` `static` `void` `main(String[] args)` ` ` `{` ` ` `String num = ` `"1234"` `;` ` ` `System.out.println(divisible(num));` ` ` `}` `}` `// This code is contributed by Raj` |

## Python3

`# Python3 program to find the` `# minimum number of digits to` `# be removed to make a large` `# number divisible by 3.` `# function to count the` `# no of removal of digits` `# to make a very large` `# number divisible by 3` `def` `divisible(num):` ` ` `n ` `=` `len` `(num)` ` ` `# add up all the digits of num` ` ` `sum_ ` `=` `0` ` ` `for` `i ` `in` `range` `(n):` ` ` `sum_ ` `+` `=` `int` `(num[i])` ` ` `# if num is already is` ` ` `# divisible by 3 then no` ` ` `# digits are to be removed` ` ` `if` `(sum_ ` `%` `3` `=` `=` `0` `):` ` ` `return` `0` ` ` `# if there is single digit,` ` ` `# then it is not possible` ` ` `# to remove one digit.` ` ` `if` `(n ` `=` `=` `1` `):` ` ` `return` `-` `1` ` ` `# traverse through the number` ` ` `# and find out if any number` ` ` `# on removal makes the sum` ` ` `# divisible by 3` ` ` `for` `i ` `in` `range` `(n):` ` ` `if` `(sum_ ` `%` `3` `=` `=` `int` `(num[i]) ` `%` `3` `):` ` ` `return` `1` ` ` `# if there are two numbers` ` ` `# then it is not possible` ` ` `# to remove two digits.` ` ` `if` `(n ` `=` `=` `2` `):` ` ` `return` `-` `1` ` ` `# Otherwise we can always` ` ` `# make a number multiple of` ` ` `# 2 by removing 2 digits.` ` ` `return` `2` `# Driver Code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` `num ` `=` `"1234"` ` ` `print` `(divisible(num))` `# This code is contributed by mits` |

## C#

`// C# program to find the` `// minimum number of digits` `// to be removed to make a` `// large number divisible by 3.` `using` `System;` `// function to count the no` `// of removal of digits` `// to make a very large` `// number divisible by 3` `class` `GFG {` ` ` `static` `int` `divisible(String num)` ` ` `{` ` ` `int` `n = num.Length;` ` ` `// add up all the` ` ` `// digits of num` ` ` `int` `sum = 0;` ` ` `for` `(` `int` `i = 0; i < n; i++)` ` ` `sum += (` `int` `)(num[i]);` ` ` `// if num is already is` ` ` `// divisible by 3 then` ` ` `// no digits are to be` ` ` `// removed` ` ` `if` `(sum % 3 == 0)` ` ` `return` `0;` ` ` `// if there is single digit,` ` ` `// then it is not possible` ` ` `// to remove one digit.` ` ` `if` `(n == 1)` ` ` `return` `-1;` ` ` `// traverse through the number` ` ` `// and find out if any number` ` ` `// on removal makes the sum` ` ` `// divisible by 3` ` ` `for` `(` `int` `i = 0; i < n; i++)` ` ` `if` `(sum % 3 == (num[i] - ` `'0'` `) % 3)` ` ` `return` `1;` ` ` `// if there are two numbers` ` ` `// then it is not possible` ` ` `// to remove two digits.` ` ` `if` `(n == 2)` ` ` `return` `-1;` ` ` `// Otherwise we can always` ` ` `// make a number multiple` ` ` `// of 2 by removing 2 digits.` ` ` `return` `2;` ` ` `}` ` ` `// Driver Code` ` ` `public` `static` `void` `Main()` ` ` `{` ` ` `string` `num = ` `"1234"` `;` ` ` `Console.WriteLine(divisible(num));` ` ` `}` `}` `// This code is contributed by mits` |

## PHP

`<?php` `// PHP program to find the` `// minimum number of digits to` `// be removed to make a large` `// number divisible by 3.` `// function to count the` `// no of removal of digits` `// to make a very large` `// number divisible by 3` `function` `divisible(` `$num` `)` `{` ` ` `$n` `= ` `strlen` `(` `$num` `);` ` ` `// add up all the digits of num` ` ` `$sum` `= (` `$num` `); (` `$num` `); 0 - ` `'0'` `;` ` ` `// if num is already is` ` ` `// divisible by 3 then no` ` ` `// digits are to be removed` ` ` `if` `(` `$sum` `% 3 == 0)` ` ` `return` `0;` ` ` `// if there is single digit,` ` ` `// then it is not possible` ` ` `// to remove one digit.` ` ` `if` `(` `$n` `== 1)` ` ` `return` `-1;` ` ` `// traverse through the number` ` ` `// and find out if any number` ` ` `// on removal makes the sum` ` ` `// divisible by 3` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$n` `; ` `$i` `++)` ` ` `if` `(` `$sum` `% 3 == (` `$num` `[` `$i` `] - ` `'0'` `) % 3)` ` ` `return` `1; ` ` ` `// if there are two numbers` ` ` `// then it is not possible` ` ` `// to remove two digits.` ` ` `if` `(` `$n` `== 2)` ` ` `return` `-1;` ` ` `// Otherwise we can always` ` ` `// make a number multiple of` ` ` `// 2 by removing 2 digits.` ` ` `return` `2;` `}` `// Driver Code` `$num` `= ` `"1234"` `;` `echo` `divisible(` `$num` `);` `// This code is contributed by ajit` `?>` |

## Javascript

`<script>` `// JavaScript program to find the minimum number of` `// digits to be removed to make a large number` `// divisible by 3.` `// function to count the no of removal of digits` `// to make a very large number divisible by 3` `function` `divisible(num)` `{` ` ` `let n = num.length;` ` ` `// add up all the digits of num` ` ` `let sum = 0;` ` ` `for` `(let i = 0; i < n; i++)` ` ` `sum += (num.charAt(i));` ` ` `// if num is already is divisible by 3` ` ` `// then no digits are to be removed` ` ` `if` `(sum % 3 == 0)` ` ` `return` `0;` ` ` `// if there is single digit, then it is` ` ` `// not possible to remove one digit.` ` ` `if` `(n == 1)` ` ` `return` `-1;` ` ` `// traverse through the number and find out` ` ` `// if any number on removal makes the sum` ` ` `// divisible by 3` ` ` `for` `(let i = 0; i < n; i++)` ` ` `if` `(sum % 3 == (num.charAt(i) - ` `'0'` `) % 3)` ` ` `return` `1;` ` ` `// if there are two numbers then it is` ` ` `// not possible to remove two digits.` ` ` `if` `(n == 2)` ` ` `return` `-1;` ` ` `// Otherwise we can always make a number` ` ` `// multiple of 2 by removing 2 digits.` ` ` `return` `2;` `}` `// Driver Code` ` ` `let num = ` `"1234"` `;` ` ` `document.write(divisible(num));` `// This code is contributed by Surbhi Tyagi.` `</script>` |

**Output :**

1

**Time Complexity:** O(n) where n is the length of the number.

This article is contributed by **Striver**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.