Count of different numbers divisible by 3 that can be obtained by changing at most one digit
Last Updated :
02 Mar, 2022
Given a string str representing a number having N digits, the task is to calculate the number of ways to make the given number divisible by 3 by changing at most one digit of the number.
Examples:
Input: str[] = “23”
Output: 7
Explanation: Below are the numbers that can be made from the string which are divisible by 3 – 03, 21, 24, 27, 33, 63, 93
1.Change 2 to 0 (0+3)=3 divisible by 3
2.Change 3 to 1 (2+1)=3 divisible by 3
3 change 3 to 4 (2+4)=6 divisible by 3
4 change 2 to 3 sum is 6 divisible by 3
Similarly there are total 7 number of ways to make the given number divisible by 3
Input: str[] = “235”
Output: 9
Approach: The idea is very simple to solve this problem. Calculate the sum of digits of a given number and then for each index, remove that digit and try all possible digits from 0 to 9 and see if the sum is divisible by 3 or not. Follow the steps below to solve the problem:
- Initialize the variable sum as 0 to store the sum of digits of the number.
- Iterate over a range [0, N] using the variable i and perform the following steps:
- Add the value of digit at i-th index in the variable sum.
- Initialize the variable count as 0 to store the answer.
- If the number itself is divisible by 3 then increment count by one.
- Iterate over a range [0, N] using the variable i and perform the following steps:
- Initialize the variable remaining_sum as sum-(number.charAt(i)-48).
- Iterate over a range [0, 9] using the variable j and perform the following steps:
- Add the value of j to the variable remaining_sum and if remaining_sum is divisible by 3 and j is not equal to the digit at i-th index, then add the value of count by 1.
- After performing the above steps, print the value of count as the answer.
Below is the implementation of the above approach..
C++
#include <bits/stdc++.h>
using namespace std;
void findCount(string number)
{
int sum = 0;
for ( int i = 0; i < number.length(); ++i) {
sum += number[i] - 48;
}
int count = 0;
if (sum % 3 == 0)
count++;
for ( int i = 0; i < number.length(); ++i) {
int remaining_sum = sum - (number[i] - 48);
for ( int j = 0; j <= 9; ++j) {
if ((remaining_sum + j) % 3 == 0
&& j != number[i] - 48) {
++count;
}
}
}
cout << count;
}
int main()
{
string number = "235" ;
findCount(number);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static void findCount(String number)
{
int sum = 0 ;
for ( int i = 0 ; i < number.length(); ++i) {
sum += number.charAt(i) - 48 ;
}
int count = 0 ;
if (sum % 3 == 0 )
count++;
for ( int i = 0 ; i < number.length(); ++i) {
int remaining_sum
= sum - (number.charAt(i) - 48 );
for ( int j = 0 ; j <= 9 ; ++j) {
if ((remaining_sum + j) % 3 == 0
&& j != number.charAt(i) - 48 ) {
++count;
}
}
}
System.out.println(count);
}
public static void main(String[] args)
{
String number = "235" ;
findCount(number);
}
}
|
Python3
def findCount(number):
sum = 0
for i in range ( len (number)):
sum + = int (number[i]) - 48
count = 0
if ( sum % 3 = = 0 ):
count + = 1
for i in range ( len (number)):
remaining_sum = sum - ( int (number[i]) - 48 )
for j in range ( 10 ):
if ((remaining_sum + j) % 3 = = 0 and j ! = int (number[i]) - 48 ):
count + = 1
print (count)
number = "235"
findCount(number)
|
C#
using System;
using System.Collections.Generic;
class GFG {
static void findCount( string number)
{
int sum = 0;
for ( int i = 0; i < number.Length; ++i) {
sum += ( int )number[i] - 48;
}
int count = 0;
if (sum % 3 == 0)
count++;
for ( int i = 0; i < number.Length; ++i) {
int remaining_sum = sum - (( int )number[i] - 48);
for ( int j = 0; j <= 9; ++j) {
if ((remaining_sum + j) % 3 == 0
&& j != number[i] - 48) {
++count;
}
}
}
Console.Write(count);
}
public static void Main()
{
string number = "235" ;
findCount(number);
}
}
|
Javascript
<script>
function findCount(number) {
let sum = 0;
for (let i = 0; i < number.length; ++i) {
sum += number[i] - 48;
}
let count = 0;
if (sum % 3 == 0) count++;
for (let i = 0; i < number.length; ++i) {
let remaining_sum = sum - (number[i] - 48);
for (let j = 0; j <= 9; ++j) {
if ((remaining_sum + j) % 3 == 0 && j != number[i] - 48) {
++count;
}
}
}
document.write(count);
}
let number = "235" ;
findCount(number);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...