Minimum number of digits required to be removed to make a number divisible by 4


In this article, we will explore an intriguing computational problem - "Minimum number of digits required to be removed to make a number divisible by 4". This problem is a common one in coding contests and algorithm-based interviews and offers excellent practice for enhancing your problem-solving skills.

First, let's understand the problem statement: We have a number, and our task is to remove the minimum number of digits such that the remaining number is divisible by 4.

Conceptual Understanding

The problem lies in the realm of number theory. One key fact to understand is that a number is divisible by 4 if and only if the number formed by its last two digits is divisible by 4. This fact is critical in solving our problem.

Algorithm Explanation

The algorithm to solve this problem involves the following steps −

  • Convert the number to a string.

  • Check from the end of the string if the number formed by the last two characters is divisible by 4.

  • If yes, return the count of digits removed. If no, remove the last character and increment the count.

  • Repeat until the number is divisible by 4 or only one digit is left.

Example

Here're the programs of the algorithm −

#include <stdio.h>
#include <string.h>

int minRemovals(char* num) {
   int n = strlen(num); // Get the length of the string
   int count = 0;

   for (int i = n - 1; i > 0; i--) {
      if ((num[i] - '0' + (num[i - 1] - '0') * 10) % 4 == 0) {
         return count; // Return the count if the number is divisible by 4
      }
      count++;
   }
   return n - 1; // If no divisible number found, return n - 1
}
int main() {
   char num[] = "1351";
   printf("Minimum number of digits to be removed to make the number divisible by 4 is: %d\n", minRemovals(num));
   return 0;
}

Output

Minimum number of digits to be removed to make the number divisible by 4 is: 3
#include<bits/stdc++.h>
using namespace std;

int minRemovals(string num) {
   int n = num.size();
   int count = 0;
   
   for (int i = n - 1; i > 0; i--) {
      if ((num[i] - '0' + (num[i - 1] - '0') * 10) % 4 == 0) {
         return count;
      }
      count++;
   }
   
   return n - 1;
}
int main() {
   string num = "1351";
   cout << "Minimum number of digits to be removed to make the number divisible by 4 is: ";
   cout << minRemovals(num) << endl;
   
   return 0;
}

Output

Minimum number of digits to be removed to make the number divisible by 4 is: 3
public class Main {
   public static int minRemovals(String num) {
      int n = num.length(); // Get the length of the string
      int count = 0;

      for (int i = n - 1; i > 0; i--) {
         if ((num.charAt(i) - '0' + (num.charAt(i - 1) - '0') * 10) % 4 == 0) {
            return count; // Return the count if the number is divisible by 4
         }
         count++;
      }

      return n - 1; // If no divisible number found, return n - 1
   }

   public static void main(String[] args) {
      String num = "1351";
      System.out.println("Minimum number of digits to be removed to make the number divisible by 4 is: " + minRemovals(num));
   }
}

Output

Minimum number of digits to be removed to make the number divisible by 4 is: 3
def min_removals(num):
   n = len(num)  # Get the length of the string
   count = 0

   for i in range(n - 1, 0, -1):
      if (int(num[i]) + int(num[i - 1]) * 10) % 4 == 0:
         return count  # Return the count if the number is divisible by 4
      count += 1

   return n - 1  # If no divisible number found, return n - 1


num = "1351"
print(
   "Minimum number of digits to be removed to make the number divisible by 4 is:",
   min_removals(num))

Output

Minimum number of digits to be removed to make the number divisible by 4 is: 3

In the minRemovals function, we initialize a counter count to 0, which will keep track of the number of digits removed. We then iterate from the end of the number (string), and check if the number formed by the last two digits is divisible by 4. If it is, we return the count; if not, we increment the count and continue to the next iteration.

The main function serves as the entry point for our program, where we define our input number and print the minimum number of digits to be removed to make the number divisible by 4.

Testcase Example

Let's take the number 1351 as an example. When we check the last two digits (51), we see that it is not divisible by 4. Hence, we remove the last digit (1), resulting in the number 135. We check again and find that the last two digits (35) are still not divisible by 4. Hence, we remove the last digit (5), leaving us with the number 13. The last two digits (13) are not divisible by 4, so we remove the last digit (3). Now, we are left with the number 1, which is not divisible by 4, but we can't remove any more digits. Thus, the minimum number of digits required to be removed is 3.

Time and Space Complexity

The time complexity of the algorithm is O(n), where n is the number of digits in the number. The space complexity is O(1), as we are not using any additional data structures in our algorithm.

Conclusion

In this article, we delved into a common computational problem - determining the minimum number of digits required to be removed to make a number divisible by 4. We developed the concise various programs, leveraging key insights from number theory.

Updated on: 27-Oct-2023

140 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements