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.
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).
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 firstname.lastname@example.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.
- Sub-string Divisibility by 3 Queries
- Check divisibility by 7
- Repeated Unit Divisibility
- Divisibility by 12 for a large number
- Check the divisibility of Hexadecimal numbers
- To check divisibility of any large number by 999
- Check divisibility in a binary stream
- Check if a large number is divisibility by 15
- Divisibility by 64 with removal of bits allowed
- Divisibility by 3 where each digit is the sum of all prefix digits modulo 10
- Maximize profit when divisibility by two numbers have associated profits
- Maximum path sum for each position with jumps under divisibility condition
- Find the Substring with maximum product
- Smallest non-zero substring which has any permutation divisible by 2^K
- Print all substring of a number without any conversion
- XOR of a submatrix queries
- Lexicographically smallest substring with maximum occurrences containing a's and b's only
- Length of the longest substring with no consecutive same letters
- Number of binary strings such that there is no substring of length ≥ 3
- Find the String having each substring with exactly K distinct characters
Improved By : Rajput-Ji