Count of different numbers divisible by 3 that can be obtained by changing at most one digit

Given a string str[] number N, 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: 7Explanation: Below are the numbers that can be made from the string which are divisible by 3 – 03, 21, 24, 27, 33, 63, 931.Change 2 to 0 (0+3)=3 divisible by 32.Change 3 to 1 (2+1)=3 divisible by 33 change 3 to 4 (2+4)=6 divisible by 34 change 2 to 3 sum is 6 divisible by 3Similarly there are total 7 number of ways to make the given number divisible by 3Input: str[] = “235”Output: 9Approach: 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.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..Javaimport 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;                  for (int i = 0; i < number.length(); ++i) {                          int remaining_sum                = sum - (number.charAt(i) - 48);                          for (int j = 0; j