Reduce the number to minimum multiple of 4 after removing the digits
Last Updated :
01 Nov, 2023
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++
#include <bits/stdc++.h>
using namespace std;
const int TEN = 10;
int minNum(string str, int len)
{
int res = INT_MAX;
for ( int i = 0; i < len; i++) {
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 (num % 4 == 0) {
res = min(res, num);
}
}
}
return ((res == INT_MAX) ? -1 : res);
}
int main()
{
string str = "17" ;
int len = str.length();
cout << minNum(str, len);
return 0;
}
|
Java
import java.io.*;
public class GFG
{
static int TEN = 10 ;
static int minNum( char [] str, int len)
{
int res = Integer.MAX_VALUE;
for ( int i = 0 ; i < len; i++)
{
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 (num % 4 == 0 )
{
res = Math.min(res, num);
}
}
}
return ((res == Integer.MAX_VALUE) ? - 1 : res);
}
public static void main(String[] args)
{
String str = "17" ;
int len = str.length();
System.out.print(minNum(str.toCharArray(), len));
}
}
|
Python 3
import sys
TEN = 10
def minNum( str , len1):
res = sys.maxsize
for i in range (len1):
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 (num % 4 = = 0 ):
res = min (res, num)
if (res = = sys.maxsize):
return - 1
else :
return res
if __name__ = = '__main__' :
str = "17"
len1 = len ( str )
print (minNum( str , len1))
|
C#
using System;
class GFG
{
static int TEN = 10;
static int minNum( char [] str, int len)
{
int res = int .MaxValue;
for ( int i = 0; i < len; i++)
{
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 (num % 4 == 0)
{
res = Math.Min(res, num);
}
}
}
return ((res == int .MaxValue) ? -1 : res);
}
public static void Main(String[] args)
{
String str = "17" ;
int len = str.Length;
Console.Write(minNum(str.ToCharArray(), len));
}
}
|
Javascript
<script>
var TEN = 10;
function minNum( str , len) {
var res = Number.MAX_VALUE;
for ( var i = 0; i < len; i++) {
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 (num % 4 == 0) {
res = Math.min(res, num);
}
}
}
return ((res == Number.MAX_VALUE) ? -1 : res);
}
var str = "17" ;
var len = str.length;
document.write(minNum(str, len));
</script>
|
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.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...