Given an integer N, the task is to reduce the number to a smallest positive integer X after removing some of the digits (possibly none) such that X is divisible by 4. Print -1 if it cannot be reduced to such multiple.
Input: N = 78945666384
Remove all the digits except a single
occurrence of the digit ‘4’.
Input: N = 17
Approach: Since the resultant number has to be minimized. So, check whether there is any digit in the number which is equal to either ‘4’ or ‘8’ because these are the digits divisible by 4 in the ascending order. If there are no such digits then check all the subsequences of digits of length 2 for any multiple of 4. If there is still no multiple of 4 then the number is not possible because any number with more than 2 digits which is a multiple of 4 will definitely have a subsequence divisible by 4 with digits less 3.
Below is the implementation of the above approach:
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Min number of operations to reduce N to 0 by subtracting any digits from N
- Reduce number to a single digit by subtracting adjacent digits repeatedly
- Build Lowest Number by Removing n digits from a given number
- Reduce N to 1 with minimum number of given operations
- Smallest multiple of a given number made of digits 0 and 9 only
- Reduce the string by removing K consecutive identical characters
- Minimum number of digits to be removed so that no two consecutive digits are same
- Minimum number with digits as 4 and 7 only and given sum
- Minimum digits to remove to make a number Perfect Square
- First N terms whose sum of digits is a multiple of 10
- Reduce the string to minimum length with the given operation
- Minimum reduce operations to convert a given string into a palindrome
- Minimum sum of squares of character counts in a given string after removing k characters
- Find smallest number with given number of digits and sum of digits under given constraints
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Maximize the given number by replacing a segment of digits with the alternate digits given
- Number formed by deleting digits such that sum of the digits becomes even and the number odd
- Find smallest number with given number of digits and sum of digits
- Find the Largest number with given number of digits and sum of digits
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.