Number of digits to be removed to make a number divisible by 3
Last Updated :
18 Sep, 2023
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.on
Input: num = "11"
Output: -1
Explanation: It is not possible to
remove any digits and make it divisible
by 3.
Approach 1:
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++
#include <bits/stdc++.h>
using namespace std;
int divisible(string num)
{
int n = num.length();
int sum = accumulate(begin(num), end(num), 0) - '0' * 1;
if (sum % 3 == 0)
return 0;
if (n == 1)
return -1;
for ( int i = 0; i < n; i++)
if (sum % 3 == (num[i] - '0' ) % 3)
return 1;
if (n == 2)
return -1;
return 2;
}
int main()
{
string num = "1234" ;
cout << divisible(num);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int divisible(String num)
{
int n = num.length();
int sum = 0 ;
for ( int i = 0 ; i < n; i++)
sum += ( int )(num.charAt(i));
if (sum % 3 == 0 )
return 0 ;
if (n == 1 )
return - 1 ;
for ( int i = 0 ; i < n; i++)
if (sum % 3 == (num.charAt(i) - '0' ) % 3 )
return 1 ;
if (n == 2 )
return - 1 ;
return 2 ;
}
public static void main(String[] args)
{
String num = "1234" ;
System.out.println(divisible(num));
}
}
|
Python3
def divisible(num):
n = len (num)
sum_ = 0
for i in range (n):
sum_ + = int (num[i])
if (sum_ % 3 = = 0 ):
return 0
if (n = = 1 ):
return - 1
for i in range (n):
if (sum_ % 3 = = int (num[i]) % 3 ):
return 1
if (n = = 2 ):
return - 1
return 2
if __name__ = = '__main__' :
num = "1234"
print (divisible(num))
|
C#
using System;
class GFG {
static int divisible(String num)
{
int n = num.Length;
int sum = 0;
for ( int i = 0; i < n; i++)
sum += ( int )(num[i]);
if (sum % 3 == 0)
return 0;
if (n == 1)
return -1;
for ( int i = 0; i < n; i++)
if (sum % 3 == (num[i] - '0' ) % 3)
return 1;
if (n == 2)
return -1;
return 2;
}
public static void Main()
{
string num = "1234" ;
Console.WriteLine(divisible(num));
}
}
|
PHP
<?php
function divisible( $num )
{
$n = strlen ( $num );
$sum = ( $num ); ( $num ); 0 - '0' ;
if ( $sum % 3 == 0)
return 0;
if ( $n == 1)
return -1;
for ( $i = 0; $i < $n ; $i ++)
if ( $sum % 3 == ( $num [ $i ] - '0' ) % 3)
return 1;
if ( $n == 2)
return -1;
return 2;
}
$num = "1234" ;
echo divisible( $num );
?>
|
Javascript
<script>
function divisible(num)
{
let n = num.length;
let sum = 0;
for (let i = 0; i < n; i++)
sum += (num.charAt(i));
if (sum % 3 == 0)
return 0;
if (n == 1)
return -1;
for (let i = 0; i < n; i++)
if (sum % 3 == (num.charAt(i) - '0' ) % 3)
return 1;
if (n == 2)
return -1;
return 2;
}
let num = "1234" ;
document.write(divisible(num));
</script>
|
Output :
1
Time Complexity: O(N), as we are using a loop to traverse N times.
Auxiliary Space: O(1), as we are not using any extra space.
Approach 2: Without STL Functions:
We can count the sum of digits in the number and check whether it is divisible by 3 or not. If it is divisible by 3, then no digits need to be removed. Otherwise, we need to remove some digits to make it divisible by 3.
This implementation first calculates the sum of digits in the number. Given below is the step-by-step procedure of the above approach:
- If the sum is divisible by 3, then no digits need to be removed, so it returns 0. Otherwise, it tries to remove digits one by one from the right end of the number.
- If a digit can be removed without changing the remainder of the sum modulo 3, then it is removed, and the sum is updated accordingly.
- If removing a single digit is not enough, it tries to remove two digits by choosing the smallest possible digits. If no such combination of digits can make the sum divisible by 3, then the function returns -1 to indicate that it is impossible to make the number divisible by 3 by removing digits.
C++
#include <bits/stdc++.h>
using namespace std;
int divisible(string num) {
int n = num.length();
int sum = 0;
for ( int i = 0; i < n; i++) {
sum += num[i] - '0' ;
}
if (sum % 3 == 0) {
return 0;
}
int cnt1 = 0, cnt2 = 0;
for ( int i = n-1; i >= 0; i--) {
int x = num[i] - '0' ;
if (x % 3 == sum % 3) {
cnt1++;
sum -= x;
if (sum % 3 == 0) {
return cnt1;
}
} else if (cnt2 < 2 && x <= sum) {
cnt2++;
sum -= x;
if (sum % 3 == 0) {
return cnt1 + cnt2;
}
}
}
return -1;
}
int main()
{
string num = "1234" ;
cout << divisible(num);
return 0;
}
|
Java
import java.util.*;
public class Main {
public static int divisible(String num) {
int n = num.length();
int sum = 0 ;
for ( int i = 0 ; i < n; i++) {
sum += num.charAt(i) - '0' ;
}
if (sum % 3 == 0 ) {
return 0 ;
}
int cnt1 = 0 , cnt2 = 0 ;
for ( int i = n- 1 ; i >= 0 ; i--) {
int x = num.charAt(i) - '0' ;
if (x % 3 == sum % 3 ) {
cnt1++;
sum -= x;
if (sum % 3 == 0 ) {
return cnt1;
}
} else if (cnt2 < 2 && x <= sum) {
cnt2++;
sum -= x;
if (sum % 3 == 0 ) {
return cnt1 + cnt2;
}
}
}
return - 1 ;
}
public static void main(String[] args) {
String num = "1234" ;
System.out.println(divisible(num));
}
}
|
Python3
def divisible(num: str ) - > int :
n = len (num)
sum = 0
for i in range (n):
sum + = int (num[i])
if sum % 3 = = 0 :
return 0
cnt1, cnt2 = 0 , 0
for i in range (n - 1 , - 1 , - 1 ):
x = int (num[i])
if x % 3 = = sum % 3 :
cnt1 + = 1
sum - = x
if sum % 3 = = 0 :
return cnt1
elif cnt2 < 2 and x < = sum :
cnt2 + = 1
sum - = x
if sum % 3 = = 0 :
return cnt1 + cnt2
return - 1
num = "1234"
print (divisible(num))
|
C#
using System;
public class Program
{
public static int Divisible( string num)
{
int n = num.Length;
int sum = 0;
for ( int i = 0; i < n; i++)
{
sum += num[i] - '0' ;
}
if (sum % 3 == 0)
{
return 0;
}
int cnt1 = 0, cnt2 = 0;
for ( int i = n-1; i >= 0; i--)
{
int x = num[i] - '0' ;
if (x % 3 == sum % 3)
{
cnt1++;
sum -= x;
if (sum % 3 == 0)
{
return cnt1;
}
}
else if (cnt2 < 2 && x <= sum)
{
cnt2++;
sum -= x;
if (sum % 3 == 0)
{
return cnt1 + cnt2;
}
}
}
return -1;
}
public static void Main()
{
string num = "1234" ;
Console.WriteLine(Divisible(num));
}
}
|
Javascript
function divisible(num) {
let n = num.length;
let sum = 0;
for (let i = 0; i < n; i++) {
sum += parseInt(num[i]);
}
if (sum % 3 === 0) {
return 0;
}
let cnt1 = 0, cnt2 = 0;
for (let i = n - 1; i >= 0; i--) {
let x = parseInt(num[i]);
if (x % 3 === sum % 3) {
cnt1++;
sum -= x;
if (sum % 3 === 0) {
return cnt1;
}
} else if (cnt2 < 2 && x <= sum) {
cnt2++;
sum -= x;
if (sum % 3 === 0) {
return cnt1 + cnt2;
}
}
}
return -1;
}
let num = "1234" ;
console.log(divisible(num));
|
Time Complexity: O(N), as we use a loop to traverse N times.
Auxiliary Space: O(1), as we are not using any extra space.
Share your thoughts in the comments
Please Login to comment...