Given a large number, n (having number digits up to 10^6) and various queries of the below form :
Query(l, r) : find if the sub-string between the
indices l and r (Both inclusive)
are divisible by 11.
Examples:
Input: n = 122164154695
Queries: l = 0 r = 3, l = 1 r = 2, l = 5 r = 9,
l = 0 r = 11
Output:
True
False
False
True
Explanation:
In the first query, 1221 is divisible by 11
In the second query, 22 is divisible by 11 and so on.
We know that any number is divisible by 11 if the difference between sum of odd indexed digits and the sum of even indexed digits is divisible by 11, i.e.,
Sum(digits at odd places) – Sum(digits at even places) should be divisible by 11.
Hence the idea is to pre-process an auxiliary array that would store sum of digits at odd and even places.
To evaluate a query we can use the auxiliary array to answer it in O(1).
C++
// C++ program to check divisibility by 11 in // substrings of a number string #include <iostream> using namespace std; const int MAX = 1000005; // To store sums of even and odd digits struct OddEvenSums { // Sum of even placed digits int e_sum; // Sum of odd placed digits int o_sum; }; // Auxiliary array OddEvenSums sum[MAX]; // Utility function to evaluate a character's // integer value int toInt(char x) { return int(x) - 48; } // This function receives the string representation // of the number and precomputes the sum array void preCompute(string x) { // Initialize everb sum[0].e_sum = sum[0].o_sum = 0; // Add the respective digits depending on whether // they're even indexed or odd indexed for (int i=0; i<x.length(); i++) { if (i%2==0) { sum[i+1].e_sum = sum[i].e_sum+toInt(x[i]); sum[i+1].o_sum = sum[i].o_sum; } else { sum[i+1].o_sum = sum[i].o_sum+toInt(x[i]); sum[i+1].e_sum = sum[i].e_sum; } } } // This function receives l and r representing // the indices and prints the required output bool query(int l,int r) { int diff = (sum[r+1].e_sum - sum[r+1].o_sum) - (sum[l].e_sum - sum[l].o_sum); return (diff%11==0); } //driver function to check the program int main() { string s = "122164154695"; preCompute(s); cout << query(0, 3) << endl; cout << query(1, 2) << endl; cout << query(5, 9) << endl; cout << query(0, 11) << endl; return 0; } |
Java
// Java program to check divisibility by 11 in // subStrings of a number String class GFG { static int MAX = 1000005; // To store sums of even and odd digits static class OddEvenSums { // Sum of even placed digits int e_sum; // Sum of odd placed digits int o_sum; }; // Auxiliary array static OddEvenSums []sum = new OddEvenSums[MAX]; // Utility function to evaluate a character's // integer value static int toInt(char x) { return x - 48; } // This function receives the String representation // of the number and precomputes the sum array static void preCompute(String x) { // Initialize everb sum[0].e_sum = sum[0].o_sum = 0; // Add the respective digits depending on whether // they're even indexed or odd indexed for (int i = 0; i < x.length(); i++) { if (i % 2 == 0) { sum[i + 1].e_sum = sum[i].e_sum + toInt(x.charAt(i)); sum[i + 1].o_sum = sum[i].o_sum; } else { sum[i + 1].o_sum = sum[i].o_sum + toInt(x.charAt(i)); sum[i + 1].e_sum = sum[i].e_sum; } } } // This function receives l and r representing // the indices and prints the required output static boolean query(int l, int r) { int diff = (sum[r + 1].e_sum - sum[r + 1].o_sum) - (sum[l].e_sum - sum[l].o_sum); return (diff % 11 == 0); } //driver function to check the program public static void main(String[] args) { for (int i = 0; i < MAX; i++) { sum[i] = new OddEvenSums(); } String s = "122164154695"; preCompute(s); System.out.println(query(0, 3) ? 1 : 0); System.out.println(query(1, 2) ? 1 : 0); System.out.println(query(5, 9) ? 1 : 0); System.out.println(query(0, 11) ? 1 : 0); } } // This code is contributed by Rajput-Ji |
Output:
1 1 0 1
This article is contributed by Ashutosh Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Sub-string Divisibility by 3 Queries
- Check divisibility by 7
- Divisibility by 12 for a large number
- Repeated Unit Divisibility
- To check divisibility of any large number by 999
- Divisibility by 64 with removal of bits allowed
- Check if a large number is divisibility by 15
- Check the divisibility of Hexadecimal numbers
- Check divisibility in a binary stream
- Maximize profit when divisibility by two numbers have associated profits
- Divisibility by 3 where each digit is the sum of all prefix digits modulo 10
- Maximum path sum for each position with jumps under divisibility condition
- Find the Substring with maximum product
- Print all substring of a number without any conversion
- Smallest non-zero substring which has any permutation divisible by 2^K
- XOR of a submatrix queries
- Find the String having each substring with exactly K distinct characters
- Number of binary strings such that there is no substring of length ≥ 3
- Length of the longest substring with no consecutive same letters
- Lexicographically smallest substring with maximum occurrences containing a's and b's only
Improved By : Rajput-Ji

