给定数字N ,任务是计算要从N中删除的最小位数,以使其能够被4整除。
例子:
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
方法:这个想法是基于除以4的基本规则,即如果一个数字中最后两位数字形成的数字可以被4整除,那么原始数字可以被4整除。现在,想法是从最后开始检查由两位数形成的数字是否可被4整除。请按照以下步骤解决问题:
- 将数字N转换为字符串并将其存储在S中。
- 使用字符串S的长度初始化变量ans ,以存储所需的最少删除次数。
- 使用变量i从头开始遍历字符串S。
- 使用变量j在[i – 1,0]范围内迭代。
- 如果S [j]和S [i]形成的数字可被4整除,则执行以下步骤:
- 将索引i和j之间的位数存储在变量K1中,该变量等于(i – j – 1) ,将索引i之前的位数存储在变量K2中,该变量等于(N –我– 1) 。 K1和K2的总和表示要删除的位数,以使S [j]和S [i]成为新数字的最后两位。
- 如果(K1 + K2)的值小于ans的值,则将ans更新为(K1 + K2) 。
- 如果S [j]和S [i]形成的数字可被4整除,则执行以下步骤:
- 使用变量j在[i – 1,0]范围内迭代。
- 在遍历字符串,如果ans的值仍保持不变,请检查是否有任何S [i]被4整除。如果发现真为真,则将ans更新为(S – 1的长度) 。
- 完成上述步骤后,输出ans的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count the minimum number
// of digits required to be removed to
// make a given number divisible by 4
void minimumDeletions(string s)
{
// Store the size of the string
int n = s.length();
// Stores the required result
int ans = n;
// Check for every pair of digits
// if the number formed by them
// is divisible by 4 or not
for (int i = n - 1; i >= 0; i--) {
// Store s[i] in a variable
int t = s[i] - '0';
// If it is divisible by 2
if (t % 2 == 0) {
for (int j = i - 1;
j >= 0; j--) {
// Store the number formed
// by s[j] and s[i]
int num = (s[j] - '0')
* 10
+ t;
// Check if it is
// divisible by 4
if (num % 4 == 0) {
// Store the number of digits
// required to be deleted
int k1 = i - j - 1;
int k2 = n - i - 1;
// Update ans
ans = min(ans,
k1 + k2);
}
}
}
}
// If value of ans is unchanged, then
// check if any s[i] is divisible by 4
if (ans == n) {
for (int i = 0; i < n; i++) {
int num = s[i] - '0';
// If true, update ans to n - 1
if (num % 4 == 0) {
ans = n - 1;
}
}
}
// Print the result
cout << ans;
}
// Driver Code
int main()
{
string str = "12367";
minimumDeletions(str);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG {
// Function to count the minimum number
// of digits required to be removed to
// make a given number divisible by 4
static void minimumDeletions(String s)
{
// Store the size of the string
int n = s.length();
// Stores the required result
int ans = n;
// Check for every pair of digits
// if the number formed by them
// is divisible by 4 or not
for (int i = n - 1; i >= 0; i--) {
// Store s[i] in a variable
int t = s.charAt(i) - '0';
// If it is divisible by 2
if (t % 2 == 0) {
for (int j = i - 1; j >= 0; j--) {
// Store the number formed
// by s[j] and s[i]
int num = (s.charAt(j) - '0') * 10 + t;
// Check if it is
// divisible by 4
if (num % 4 == 0) {
// Store the number of digits
// required to be deleted
int k1 = i - j - 1;
int k2 = n - i - 1;
// Update ans
ans = Math.min(ans, k1 + k2);
}
}
}
}
// If value of ans is unchanged, then
// check if any s[i] is divisible by 4
if (ans == n) {
for (int i = 0; i < n; i++) {
int num = s.charAt(i) - '0';
// If true, update ans to n - 1
if (num % 4 == 0) {
ans = n - 1;
}
}
}
// Print the result
System.out.println(ans);
}
// Driver Code
static public void main(String[] args)
{
String str = "12367";
minimumDeletions(str);
}
}
// This code is contributed by ukasp.
Python3
# Python3 program for the above approach
# Function to count the minimum number
# of digits required to be removed to
# make a given number divisible by 4
def minimumDeletions(s):
# Store the size of the string
n = len(s)
# Stores the required result
ans = n
# Check for every pair of digits
# if the number formed by them
# is divisible by 4 or not
for i in range(n - 1, -1, -1):
# Store s[i] in a variable
t = ord(s[i]) - ord('0')
# If it is divisible by 2
if (t % 2 == 0):
for j in range(i - 1, -1, -1):
# Store the number formed
# by s[j] and s[i]
num = (ord(s[j]) - ord('0'))* 10 + t
# Check if it is
# divisible by 4
if (num % 4 == 0):
# Store the number of digits
# required to be deleted
k1 = i - j - 1
k2 = n - i - 1
# Update ans
ans = min(ans, k1 + k2)
# If value of ans is unchanged, then
# check if any s[i] is divisible by 4
if (ans == n):
for i in range(n):
num = ord(s[i]) - ord('0')
# If true, update ans to n - 1
if (num % 4 == 0):
ans = n - 1
# Prthe result
print (ans)
# Driver Code
if __name__ == '__main__':
str = "12367"
minimumDeletions(str)
# This code is contributed by mohit kumar 29.
C#
// C# program for the above approach
using System;
class GFG
{
// Function to count the minimum number
// of digits required to be removed to
// make a given number divisible by 4
static void minimumDeletions(string s)
{
// Store the size of the string
int n = s.Length;
// Stores the required result
int ans = n;
// Check for every pair of digits
// if the number formed by them
// is divisible by 4 or not
for (int i = n - 1; i >= 0; i--) {
// Store s[i] in a variable
int t = s[i] - '0';
// If it is divisible by 2
if (t % 2 == 0) {
for (int j = i - 1;
j >= 0; j--) {
// Store the number formed
// by s[j] and s[i]
int num = (s[j] - '0')
* 10
+ t;
// Check if it is
// divisible by 4
if (num % 4 == 0) {
// Store the number of digits
// required to be deleted
int k1 = i - j - 1;
int k2 = n - i - 1;
// Update ans
ans = Math.Min(ans,
k1 + k2);
}
}
}
}
// If value of ans is unchanged, then
// check if any s[i] is divisible by 4
if (ans == n) {
for (int i = 0; i < n; i++) {
int num = s[i] - '0';
// If true, update ans to n - 1
if (num % 4 == 0) {
ans = n - 1;
}
}
}
// Print the result
Console.WriteLine(ans);
}
// Driver Code
static public void Main()
{
string str = "12367";
minimumDeletions(str);
}
}
// This code is contributed by sanjoy_62.
Javascript
输出:
1
时间复杂度: O((log 10 N) 2 )
辅助空间: O(1)