![]() |
VOOZH | about |
You are given an n-digit large number, you have to check whether it is divisible by 999 without dividing or finding modulo of number by 999.
Examples:
Input : 235764 Output : Yes Input : 23576 Output : No
Since input number may be very large, we cannot use n % 999 to check if a number is divisible by 999 or not, especially in languages like C/C++. The idea is based on following fact.
The solutions is based on below fact.
A number is divisible by 999 if sum of its 3-digit-groups (if required groups are formed by appending a 0s at the beginning) is divisible by 999.
Illustration:
Input : 235764 Output : Yes Explanation : Step I - read input : 235, 764 Step II - 235 + 764 = 999 As result is 999 then we can conclude that it is divisible by 999. Input : 1244633121 Output : Yes Explanation : Step I - read input : 1, 244, 633, 121 Step II - 001 + 244 + 633 + 121 = 999 As result is 999 then we can conclude that it is divisible by 999. Input : 999999999 Output : Yes Explanation : Step I - read input : 999, 999, 999 Step II - 999 + 999 + 999 = 2997 Step III - 997 + 002 = 999 As result is 999 then we can conclude that it is divisible by 999.
How does this work?
Let us consider 235764, we can write it as 235764 = 2*105 + 3*104 + 5*103 + 7*102 + 6*10 + 4 The idea is based on below observation: Remainder of 103 divided by 999 is 1 For i > 3, 10i % 999 = 10i-3 % 999 Let us see how we use above fact. Remainder of 2*105 + 3*104 + 5*103 + 7*102 + 6*10 + 4 Remainder with 999 can be written as : 2*100 + 3*10 + 5*1 + 7*100 + 6*10 + 4 The above expression is basically sum of groups of size 3. Since the sum is divisible by 999, answer is yes.
A simple and efficient method is to take input in form of string (make its length in form of 3*m by adding 0 to left of number if required) and then you have to add the digits in blocks of three from right to left until it become a 3 digit number and if that result is 999 we can say that number is divisible by 999.
As in the case of "divisibility by 9" we check that sum of all digit is divisible by 9 or not, the same thing follows within the case of divisibility by 999. We sum up all 3-digits group from right to left and check whether the final result is 999 or not.
Implementation:
Divisible
Time complexity : O(n)
Auxiliary Space : O(1)
Method 2: Checking given any number is divisible by 999 or not by using modulo division operator "%".
Implementation:
Yes
Time Complexity : O(1)
Auxiliary Space: O(1)
Method 3: The function returns a Boolean value indicating whether the input integer is divisible by 999 or not. The function calculates this by checking if the sum of the digits of num is divisible by 9 and if the last three digits of num are divisible by 27. This code then tests the function with the examples: 999, 998 and 9987 and outputs the values True, False and False respectively.
True False False
Time complexity: O(1),
Auxiliary Space : O(1)
1. Convert the number to a string.
2. Find the sum of every third digit from the right end to the left end.
3. If the sum is divisible by 3, then the original number is divisible by 999.
False True
The time complexity of the given function is_divisible_by_999 is O(N/3) where N is the number of digits in the input number n. This is because the loop iterates over every third digit of the input number, and performs constant time operations (such as string slicing and integer conversion) on each iteration.
The auxiliary space used by the function is O(1), since it uses a constant amount of extra space to store the loop variables and the running sum.