Minimum number of digits required to be removed to make a number divisible by 4
Given a number N, the task is to count the minimum number of digits to be removed from N to make it divisible by 4.
Examples:
Input: N = 12367
Output: 1
Explanation: Removing 7 from the number 1236 make the number divisible by 4. Therefore, the minimum count of digit to be removed is 1.Input: N = 243775
Output: 4
Approach: The idea is based on the basic rule for divisibility by 4 that if the number formed by the last two digits in a number is divisible by 4, then the original number is divisible by 4. Now, the idea is to check from the last if the number formed by two digits is divisible by 4 or not. Follow the steps below to solve the problem:
- Convert the number N to a string and store it in S.
- Initialize a variable ans with the length of string S, to store the minimum number of deletions required.
- Traverse the string S from the end using the variable i.
- Iterate over the range [i – 1, 0] using the variable j.
- If the number formed by S[j] and S[i] is divisible by 4, then perform the following steps:
- Store the number of digits between index i and j in a variable, say K1, which is equal to (i – j – 1) and store the number of digits preceding index i in a variable, say K2, which is equal to (N – i – 1). The sum of K1 and K2 represents the number of digits to be deleted such that S[j] and S[i] becomes the last two digits of the new number.
- If the value of (K1 + K2) is less than the value of ans, then update ans to (K1 + K2).
- If the number formed by S[j] and S[i] is divisible by 4, then perform the following steps:
- Iterate over the range [i – 1, 0] using the variable j.
- After traversing the string if the value of ans is still unchanged, check if any S[i] is divisible by 4. If found to be true, update ans as the (length of S – 1).
- After completing the above steps, print the value of ans as the result.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to count the minimum number // of digits required to be removed to // make a given number divisible by 4 void minimumDeletions(string s) { // Store the size of the string int n = s.length(); // Stores the required result int ans = n; // Check for every pair of digits // if the number formed by them // is divisible by 4 or not for ( int i = n - 1; i >= 0; i--) { // Store s[i] in a variable int t = s[i] - '0' ; // If it is divisible by 2 if (t % 2 == 0) { for ( int j = i - 1; j >= 0; j--) { // Store the number formed // by s[j] and s[i] int num = (s[j] - '0' ) * 10 + t; // Check if it is // divisible by 4 if (num % 4 == 0) { // Store the number of digits // required to be deleted int k1 = i - j - 1; int k2 = n - i - 1; // Update ans ans = min(ans, k1 + k2); } } } } // If value of ans is unchanged, then // check if any s[i] is divisible by 4 if (ans == n) { for ( int i = 0; i < n; i++) { int num = s[i] - '0' ; // If true, update ans to n - 1 if (num % 4 == 0) { ans = n - 1; } } } // Print the result cout << ans; } // Driver Code int main() { string str = "12367" ; minimumDeletions(str); return 0; } |
Java
// Java program for the above approach import java.util.*; class GFG { // Function to count the minimum number // of digits required to be removed to // make a given number divisible by 4 static void minimumDeletions(String s) { // Store the size of the string int n = s.length(); // Stores the required result int ans = n; // Check for every pair of digits // if the number formed by them // is divisible by 4 or not for ( int i = n - 1 ; i >= 0 ; i--) { // Store s[i] in a variable int t = s.charAt(i) - '0' ; // If it is divisible by 2 if (t % 2 == 0 ) { for ( int j = i - 1 ; j >= 0 ; j--) { // Store the number formed // by s[j] and s[i] int num = (s.charAt(j) - '0' ) * 10 + t; // Check if it is // divisible by 4 if (num % 4 == 0 ) { // Store the number of digits // required to be deleted int k1 = i - j - 1 ; int k2 = n - i - 1 ; // Update ans ans = Math.min(ans, k1 + k2); } } } } // If value of ans is unchanged, then // check if any s[i] is divisible by 4 if (ans == n) { for ( int i = 0 ; i < n; i++) { int num = s.charAt(i) - '0' ; // If true, update ans to n - 1 if (num % 4 == 0 ) { ans = n - 1 ; } } } // Print the result System.out.println(ans); } // Driver Code static public void main(String[] args) { String str = "12367" ; minimumDeletions(str); } } // This code is contributed by ukasp. |
Python3
# Python3 program for the above approach # Function to count the minimum number # of digits required to be removed to # make a given number divisible by 4 def minimumDeletions(s): # Store the size of the string n = len (s) # Stores the required result ans = n # Check for every pair of digits # if the number formed by them # is divisible by 4 or not for i in range (n - 1 , - 1 , - 1 ): # Store s[i] in a variable t = ord (s[i]) - ord ( '0' ) # If it is divisible by 2 if (t % 2 = = 0 ): for j in range (i - 1 , - 1 , - 1 ): # Store the number formed # by s[j] and s[i] num = ( ord (s[j]) - ord ( '0' )) * 10 + t # Check if it is # divisible by 4 if (num % 4 = = 0 ): # Store the number of digits # required to be deleted k1 = i - j - 1 k2 = n - i - 1 # Update ans ans = min (ans, k1 + k2) # If value of ans is unchanged, then # check if any s[i] is divisible by 4 if (ans = = n): for i in range (n): num = ord (s[i]) - ord ( '0' ) # If true, update ans to n - 1 if (num % 4 = = 0 ): ans = n - 1 # Print the result print (ans) # Driver Code if __name__ = = '__main__' : str = "12367" minimumDeletions( str ) # This code is contributed by mohit kumar 29. |
C#
// C# program for the above approach using System; class GFG { // Function to count the minimum number // of digits required to be removed to // make a given number divisible by 4 static void minimumDeletions( string s) { // Store the size of the string int n = s.Length; // Stores the required result int ans = n; // Check for every pair of digits // if the number formed by them // is divisible by 4 or not for ( int i = n - 1; i >= 0; i--) { // Store s[i] in a variable int t = s[i] - '0' ; // If it is divisible by 2 if (t % 2 == 0) { for ( int j = i - 1; j >= 0; j--) { // Store the number formed // by s[j] and s[i] int num = (s[j] - '0' ) * 10 + t; // Check if it is // divisible by 4 if (num % 4 == 0) { // Store the number of digits // required to be deleted int k1 = i - j - 1; int k2 = n - i - 1; // Update ans ans = Math.Min(ans, k1 + k2); } } } } // If value of ans is unchanged, then // check if any s[i] is divisible by 4 if (ans == n) { for ( int i = 0; i < n; i++) { int num = s[i] - '0' ; // If true, update ans to n - 1 if (num % 4 == 0) { ans = n - 1; } } } // Print the result Console.WriteLine(ans); } // Driver Code static public void Main() { string str = "12367" ; minimumDeletions(str); } } // This code is contributed by sanjoy_62. |
Javascript
<script> // Javascript program for the above approach // Function to count the minimum number // of digits required to be removed to // make a given number divisible by 4 function minimumDeletions(s) { // Store the size of the string let n = s.length; // Stores the required result let ans = n; // Check for every pair of digits // if the number formed by them // is divisible by 4 or not for (let i = n - 1; i >= 0; i--) { // Store s[i] in a variable let t = s[i] - '0' ; // If it is divisible by 2 if (t % 2 == 0) { for (let j = i - 1; j >= 0; j--) { // Store the number formed // by s[j] and s[i] let num = (s[j] - '0' ) * 10 + t; // Check if it is // divisible by 4 if (num % 4 === 0) { // Store the number of digits // required to be deleted let k1 = i - j - 1; let k2 = n - i - 1; // Update ans ans = Math.min(ans, k1 + k2); } } } } // If value of ans is unchanged, then // check if any s[i] is divisible by 4 if (ans === n) { for (let i = 0; i < n; i++) { let num = s[i] - '0' ; // If true, update ans to n - 1 if (num % 4 === 0) { ans = n - 1; } } } // Print the result document.write(ans); } // Driver Code let str = "12367" ; minimumDeletions(str); // This code is contributed by Manoj. </script> |
Output:
1
Time Complexity: O((log10N)2)
Auxiliary Space:O (1)