15. Divisible by 13
β GFG solution to check if a large number (given as string) is divisible by 13 using modular arithmetic properties. π
The problem can be found at the following link: π Question Link
π§© Problem Description
Given a number represented as a string s
(which may be very large), check whether it is divisible by 13 or not.
Since the number can be extremely large (up to 10^5 digits), we cannot convert it to integer directly. We need to use modular arithmetic properties to solve this efficiently.
π Examples
Example 1
Input: s = "2911285"
Output: true
Explanation: 2911285 Γ· 13 = 223945, which is a whole number with no remainder.
Example 2
Input: s = "27"
Output: false
Explanation: 27 / 13 β 2.0769..., which is not a whole number (there is a remainder).
Example 3
Input: s = "169"
Output: true
Explanation: 169 Γ· 13 = 13, which is a whole number with no remainder.
π Constraints
$1 \le \text{s.size()} \le 10^5$
String
s
contains only digits
β
My Approach
The optimal approach uses Modular Arithmetic properties to process the large number digit by digit:
Modular Arithmetic Method
Key Insight:
For a number ABCD, we can write it as: AΓ1000 + BΓ100 + CΓ10 + D
Using modular arithmetic: (AΓ1000 + BΓ100 + CΓ10 + D) % 13
This is equivalent to: ((A%13)Γ(1000%13) + (B%13)Γ(100%13) + (C%13)Γ(10%13) + (D%13)) % 13
Algorithm Steps:
Initialize remainder
r = 0
For each digit from left to right:
Update remainder:
r = (r Γ 10 + digit) % 13
If final remainder is 0, the number is divisible by 13
Why This Works:
We build the number incrementally while keeping track of remainder
At each step, we maintain:
r = (current_number_so_far) % 13
The multiplication by 10 shifts the previous digits left by one position
Adding the new digit incorporates it into our running remainder
Mathematical Foundation:
If we have processed digits dβdβ...dβ with remainder r
Adding digit dβββ gives us: new_remainder = (r Γ 10 + dβββ) % 13
This maintains the invariant that r represents the remainder of the number formed so far
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(n), where n is the length of the string. We process each digit exactly once in a single pass through the string.
Expected Auxiliary Space Complexity: O(1), as we only use a constant amount of additional space to store the remainder variable regardless of the input size.
π§βπ» Code (C++)
class Solution {
public:
bool divby13(string &s) {
int r = 0;
for (int i = 0; i < s.length(); ++i) {
r = (r * 10 + s[i] - '0') % 13;
}
return !r;
}
};
π§βπ» Code (Java)
class Solution {
public boolean divby13(String s) {
int r = 0;
for (int i = 0; i < s.length(); ++i) {
r = (r * 10 + s.charAt(i) - '0') % 13;
}
return r == 0;
}
}
π Code (Python)
class Solution:
def divby13(self, s):
r = 0
for c in s:
r = (r * 10 + int(c)) % 13
return r == 0
π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated