
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Number of Substrings divisible by 6 in a String of Integers in C++
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.
- Related Articles
- Number of substrings divisible by 8 and not by 3 in C++
- Find the sum of the first 40 positive integers divisible by 6.
- Number of even substrings in a string of digits in C++
- Prove that the product of three consecutive positive integers is divisible by 6.
- Find number of substrings of length k whose sum of ASCII value of characters is divisible by k in C++
- Find the Number of Substrings of a String using C++
- Find the sum of the first 40 positive integers divisible by (a) 3 (b) 5 (c) 6.
- Find permutation of n which is divisible by 3 but not divisible by 6 in C++
- Program to find out number of distinct substrings in a given string in python
- Program to find number of different substrings of a string for different queries in Python
- Find the Number of Substrings of One String Present in Other using C++
- Rearrange the string to maximize the number of palindromic substrings in C++
- Program to find number of different integers in a string using Python
- Program to find total sum of all substrings of a number given as string in Python
- C++ code to count number of even substrings of numeric string
