The Wayback Machine - https://web.archive.org/web/20210219092753/https://www.geeksforgeeks.org/minimum-number-of-digits-required-to-be-removed-to-make-a-number-divisible-by-4/
Skip to content
Related Articles

Related Articles

Minimum number of digits required to be removed to make a number divisible by 4
  • Last Updated : 19 Feb, 2021

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).
  • 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

1

Time Complexity: O((log10N)2)
Auxiliary Space:O (1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :