Reduce the number to minimum multiple of 4 after removing the digits

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
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

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

-1

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Article Tags :
Practice Tags :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.