Number of Substrings divisible by 6 in a String of Integers in C++

C++Server Side ProgrammingProgramming

We'll look at a problem in which we're given an integer string and must determine how many substrings are divisible by 6 in integer format. It should be noted that input is in the form of a String made of numbers (integers). Still, the divisibility check will be performed considering it as an integer only (not using ASCII value of string input).

Input

str = “648”

Explanation

substring “6”, “48”, and “648” are divisible by 6.

Input

str = “38342”

Output

4

Explanation

substrings “3834”, “342”, ”834”, and “42” are divisible by 6.

Brute-Force Approach

Users can check every possible substring to see if it's divisible by six. If the substring is divisible, we additionally count it. This method will take longer to solve the problem and take O(n2) time to accomplish the task.

Example

#include <bits/stdc++.h>
using namespace std;
int
str_to_int (string str, int i, int j) {
   int temp = 0;
   for (; i <= j; i++) {
      temp = temp * 10 + (str[i] - '0');
   }
   return temp;
}
int main () {
   char str[] = "24661";
   int n = strlen (str);
   int count = 0;
   for (int i = 0; i < n; i++) {
      for (int j = i; j < n; j++) {
         int temp = str_to_int (str, i, j);
         if (temp % 6 == 0) count++;
      }
   }
   cout << count << endl;
   return 0;
}

Output

6

Efficient Approach

The last digit of a number must be divisible by 2 for it to be divisible by 6. The total number of digits should be 3. By tracking previously computed answers, we may utilize dynamic programming to discover solutions.

Let f(i,s) - number of strings from ith index whose digits sum modulo 3 is s which gives Σin-1 f(i,0).

Let a be the ith digit of a string; Now, from f(i,s), we need to find all substrings which are even and start with i + 1. If (a+s) is divisible by 3, an additional substring can be produced. So, our recurrence relation formed,

f(i,s) = f(i + 1, (s + a)%3) + ( a%2 == 0 AND (a+s)%3 == 0).

Example 2

#include <bits/stdc++.h>
using namespace std;
int find(int i, int s, char str[], int dp[][3]){
   // when reached end of string.
   if (i == strlen(str))
      return 0;
   // if already computed then return result.
   if (dp[i][s] != -1)
      return dp[i][s];
   int a = str[i] - '0';
   int ans = ((a+s)%3 == 0 && a%2 == 0) + find(i+1, (s+a)%3, str, dp);
   return dp[i][s] = ans;
}
int main(){
   char str[] = "24661";
   int n = strlen(str);
   // dp array to store all states.
   int dp[n+1][3];
   memset(dp, -1, sizeof dp);
   int count = 0;
   for (int i = 0; i < n; i++){
      // if any position contains 0 increment count.
      if (str[i] == '0')
         count++;
      // Passing previous sum modulo 3 = 0 to recursive function.
      else
         count += find(i, 0, str, dp);
   }
   cout << "Number of substrings divisible by 6: " << count << endl;
   return 0;
}

Output

Number of substrings divisible by 6: 6

Time complexity: O(N)

Conclusion

In this tutorial, we learned how to use dynamic programming to discover the number of substrings divisible by 6 in a string of integers. The same program may be written in different languages such as C, Java, Python, and others. We hope you found this lesson to be beneficial.

raja
Updated on 07-Mar-2022 06:47:16

Advertisements