Minimum number of digits required to be removed to make a number divisible by 4
Last Updated :
28 Feb, 2022
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++
#include <bits/stdc++.h>
using namespace std;
void minimumDeletions(string s)
{
int n = s.length();
int ans = n;
for ( int i = n - 1; i >= 0; i--) {
int t = s[i] - '0' ;
if (t % 2 == 0) {
for ( int j = i - 1;
j >= 0; j--) {
int num = (s[j] - '0' )
* 10
+ t;
if (num % 4 == 0) {
int k1 = i - j - 1;
int k2 = n - i - 1;
ans = min(ans,
k1 + k2);
}
}
}
}
if (ans == n) {
for ( int i = 0; i < n; i++) {
int num = s[i] - '0' ;
if (num % 4 == 0) {
ans = n - 1;
}
}
}
cout << ans;
}
int main()
{
string str = "12367" ;
minimumDeletions(str);
return 0;
}
|
Java
import java.util.*;
class GFG {
static void minimumDeletions(String s)
{
int n = s.length();
int ans = n;
for ( int i = n - 1 ; i >= 0 ; i--) {
int t = s.charAt(i) - '0' ;
if (t % 2 == 0 ) {
for ( int j = i - 1 ; j >= 0 ; j--) {
int num = (s.charAt(j) - '0' ) * 10 + t;
if (num % 4 == 0 ) {
int k1 = i - j - 1 ;
int k2 = n - i - 1 ;
ans = Math.min(ans, k1 + k2);
}
}
}
}
if (ans == n) {
for ( int i = 0 ; i < n; i++) {
int num = s.charAt(i) - '0' ;
if (num % 4 == 0 ) {
ans = n - 1 ;
}
}
}
System.out.println(ans);
}
static public void main(String[] args)
{
String str = "12367" ;
minimumDeletions(str);
}
}
|
Python3
def minimumDeletions(s):
n = len (s)
ans = n
for i in range (n - 1 , - 1 , - 1 ):
t = ord (s[i]) - ord ( '0' )
if (t % 2 = = 0 ):
for j in range (i - 1 , - 1 , - 1 ):
num = ( ord (s[j]) - ord ( '0' )) * 10 + t
if (num % 4 = = 0 ):
k1 = i - j - 1
k2 = n - i - 1
ans = min (ans, k1 + k2)
if (ans = = n):
for i in range (n):
num = ord (s[i]) - ord ( '0' )
if (num % 4 = = 0 ):
ans = n - 1
print (ans)
if __name__ = = '__main__' :
str = "12367"
minimumDeletions( str )
|
C#
using System;
class GFG
{
static void minimumDeletions( string s)
{
int n = s.Length;
int ans = n;
for ( int i = n - 1; i >= 0; i--) {
int t = s[i] - '0' ;
if (t % 2 == 0) {
for ( int j = i - 1;
j >= 0; j--) {
int num = (s[j] - '0' )
* 10
+ t;
if (num % 4 == 0) {
int k1 = i - j - 1;
int k2 = n - i - 1;
ans = Math.Min(ans,
k1 + k2);
}
}
}
}
if (ans == n) {
for ( int i = 0; i < n; i++) {
int num = s[i] - '0' ;
if (num % 4 == 0) {
ans = n - 1;
}
}
}
Console.WriteLine(ans);
}
static public void Main()
{
string str = "12367" ;
minimumDeletions(str);
}
}
|
Javascript
<script>
function minimumDeletions(s)
{
let n = s.length;
let ans = n;
for (let i = n - 1; i >= 0; i--)
{
let t = s[i] - '0' ;
if (t % 2 == 0)
{
for (let j = i - 1;
j >= 0; j--)
{
let num = (s[j] - '0' ) * 10 + t;
if (num % 4 === 0)
{
let k1 = i - j - 1;
let k2 = n - i - 1;
ans = Math.min(ans,
k1 + k2);
}
}
}
}
if (ans === n)
{
for (let i = 0; i < n; i++)
{
let num = s[i] - '0' ;
if (num % 4 === 0)
{
ans = n - 1;
}
}
}
document.write(ans);
}
let str = "12367" ;
minimumDeletions(str);
</script>
|
Time Complexity: O((log10N)2)
Auxiliary Space:O (1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...