Given a number N, remove exactly one digit to make the number divisible by 6 (make it the largest possible). Print the position that has to be removed, If not possible then print -1.
Input: 123 Output: 3 Explanation: Remove 3rd position element and hence the number is 12, which is divisible by 6 and is the greatest possible. Input: 134 Output: -1 Explanation: Not possible to remove any and make it divisible by 6. Input: 4510222 Output: 1 Explanation: Remove either 4 or 1 to make it divisible by 6. The numbers after removing 4 and 1 are 510222 and 450222 respectively. So, remove 1st position to make it the greatest possible.
Traverse for every element and check if by subtracting it the number is divisible by 6 or not and then store the max possible number. This won’t work when N is a very large number whose divisibility cannot be checked by % operator. It will only work for smaller N.
Take the input as a string and think of the possible cases. You can check any larger number’s divisibility by 6 if it is divisible by 2 and 3, then it is divisible by 6 also. The two cases that will arise are :-
1) When the unit’s digit is odd
When the last digit is odd, the only possible way is to remove the last digit to make it divisible by 6. So remove the last digit and check if the sum % 3 == 0 to make sure that after removing the last digit, the second last digit of N is even and its sum % 3 == 0, then you get your answer as Nth position that has to be removed. If any of the cases fail, then you cannot remove any digit to make it divisible by 6.
2) When the unit’s digit is even
In this case, multiple choices are there. Let sum be sum of digits of given number. We can delete any digit d (except unit place digit if ten’s place digit is odd, so that the number remains a multiple of 2) for which sum % 3 == d % 3. This works because after deleting that digit, sum is a multiple of 3.
Now, to maximize this number, we need to find the digit at largest place satisfying above condition.
1. Number = 4510222
sum of digits = 4 + 5 + 1 + 2 + 2 + 2 = 16
sum % 3 = 1
Now, we can delete digits 1 and 4 (because 1 % 3 = 3 and 4 % 3 = 1)
If we delete 1, we get number 450222. If we delete 4, we get number 510222
We delete the largest digit (leftmost) for which the next digit is greater than deleted digit.
2. Number = 7510222
sum of digits = 7 + 5 + 1 + 0 + 2 + 2 + 2 = 19
sum % 3 = 1
Digits 7 and 1 can be deleted according to above solution, giving numbers 510222 and 750222 respectively. Here, deleting largest (leftmost) index give smaller result because 7 > 5. This worked in above case because 4 < 5.
Find leftmost digit satisfying both the constraints
1. sum % 3 == digit % 3
2. digit < Immediate next digit
If you can’t maximize the addition of something, try to minimize it’s reduction. In case no digit is found such that digit is less than Immediate right digit than follow the above approach. If you remove something from right, the number will be the maximum possible as you are removing the lowest place value digit.
Once done print the index if any such element is found else simply print -1.
Below is the implementation of above approach:-
Time Complexity: O(no of digits)