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.
Examples:
Input: N = 78945666384
Output: 4
Remove all the digits except a single
occurrence of the digit ‘4’.Input: N = 17
Output: -1
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:
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std;
const int TEN = 10;
// Function to return the minimum number // that can be formed after removing // the digits which is a multiple of 4 int minNum(string str, int len)
{ int res = INT_MAX;
// For every digit of the number
for ( int i = 0; i < len; i++) {
// Check if the current digit
// is divisible by 4
if (str[i] == '4' || str[i] == '8' ) {
res = min(res, str[i] - '0' );
}
}
for ( int i = 0; i < len - 1; i++) {
for ( int j = i + 1; j < len; j++) {
int num = (str[i] - '0' ) * TEN
+ (str[j] - '0' );
// If any subsequence of two
// digits is divisible by 4
if (num % 4 == 0) {
res = min(res, num);
}
}
}
return ((res == INT_MAX) ? -1 : res);
} // Driver code int main()
{ string str = "17" ;
int len = str.length();
cout << minNum(str, len);
return 0;
} |
// Java implementation of the approach import java.io.*;
public class GFG
{ static int TEN = 10 ;
// Function to return the minimum number // that can be formed after removing // the digits which is a multiple of 4 static int minNum( char [] str, int len)
{ int res = Integer.MAX_VALUE;
// For every digit of the number
for ( int i = 0 ; i < len; i++)
{
// Check if the current digit
// is divisible by 4
if (str[i] == '4' || str[i] == '8' )
{
res = Math.min(res, str[i] - '0' );
}
}
for ( int i = 0 ; i < len - 1 ; i++)
{
for ( int j = i + 1 ; j < len; j++)
{
int num = (str[i] - '0' ) * TEN
+ (str[j] - '0' );
// If any subsequence of two
// digits is divisible by 4
if (num % 4 == 0 )
{
res = Math.min(res, num);
}
}
}
return ((res == Integer.MAX_VALUE) ? - 1 : res);
} // Driver code public static void main(String[] args)
{ String str = "17" ;
int len = str.length();
System.out.print(minNum(str.toCharArray(), len));
} } // This code is contributed by 29AjayKumar |
# Python3 implementation of the approach import sys
TEN = 10
# Function to return the minimum number # that can be formed after removing # the digits which is a multiple of 4 def minNum( str , len1):
res = sys.maxsize
# For every digit of the number
for i in range (len1):
# Check if the current digit
# is divisible by 4
if ( str [i] = = '4' or str [i] = = '8' ):
res = min (res, ord ( str [i]) - ord ( '0' ))
for i in range (len1 - 1 ):
for j in range (i + 1 , len1, 1 ):
num = ( ord ( str [i]) - ord ( '0' )) * TEN + \
( ord ( str [j]) - ord ( '0' ))
# If any subsequence of two
# digits is divisible by 4
if (num % 4 = = 0 ):
res = min (res, num)
if (res = = sys.maxsize):
return - 1
else :
return res
# Driver code if __name__ = = '__main__' :
str = "17"
len1 = len ( str )
print (minNum( str , len1))
# This code is contributed by Surendra_Gangwar |
// C# implementation of the approach using System;
class GFG
{ static int TEN = 10;
// Function to return the minimum number // that can be formed after removing // the digits which is a multiple of 4 static int minNum( char [] str, int len)
{ int res = int .MaxValue;
// For every digit of the number
for ( int i = 0; i < len; i++)
{
// Check if the current digit
// is divisible by 4
if (str[i] == '4' || str[i] == '8' )
{
res = Math.Min(res, str[i] - '0' );
}
}
for ( int i = 0; i < len - 1; i++)
{
for ( int j = i + 1; j < len; j++)
{
int num = (str[i] - '0' ) * TEN
+ (str[j] - '0' );
// If any subsequence of two
// digits is divisible by 4
if (num % 4 == 0)
{
res = Math.Min(res, num);
}
}
}
return ((res == int .MaxValue) ? -1 : res);
} // Driver code public static void Main(String[] args)
{ String str = "17" ;
int len = str.Length;
Console.Write(minNum(str.ToCharArray(), len));
} } // This code is contributed by Rajput-Ji |
<script> // javascript implementation of the approach var TEN = 10;
// Function to return the minimum number
// that can be formed after removing
// the digits which is a multiple of 4
function minNum( str , len) {
var res = Number.MAX_VALUE;
// For every digit of the number
for ( var i = 0; i < len; i++) {
// Check if the current digit
// is divisible by 4
if (str[i] == '4' || str[i] == '8' ) {
res = Math.min(res, str[i] - '0' );
}
}
for (i = 0; i < len - 1; i++) {
for (j = i + 1; j < len; j++) {
var num = (str[i] - '0' ) * TEN + (str[j] - '0' );
// If any subsequence of two
// digits is divisible by 4
if (num % 4 == 0) {
res = Math.min(res, num);
}
}
}
return ((res == Number.MAX_VALUE) ? -1 : res);
}
// Driver code
var str = "17" ;
var len = str.length;
document.write(minNum(str, len));
// This code is contributed by umadevi9616. </script> |
-1
Time Complexity: O(n2), where n is the length of the given string.
Auxiliary Space: O(1), no extra space is required, so it is a constant.