📌  相关文章
📜  为使给定数字可被 3 整除所需的最小和最大数字位数

📅  最后修改于: 2021-09-07 02:18:21             🧑  作者: Mango

给定一个数字字符串S ,任务是找到必须从S 中删除的最小和最大位数,以便它可以被3整除。如果不可能,则打印“-1”

例子:

解决方法:根据观察,对于任何能被 3 整除的数,其数字之和也必须能被 3 整除,可以解决该问题。请按照以下步骤解决此问题:

  • 将给定字符串的每个数字插入一个数组,比如arr[] ,如(S[i] – ‘0’) % 3
  • 在数组arr[] 中查找0秒、 1秒和2秒的计数。
  • 然后,计算数字的总和,即(arr[0] % 3) + (arr[1] % 3) + (arr[2] % 3)…。 .更新总和 = 总和 % 3
  • 根据sum的值,会出现以下三种情况:
    • 如果 sum = 0:需要删除的最小位数为0
    • 如果 sum = 1:应删除那些数字,其总和在除以3 时得到余数1 。因此,需要考虑以下情况:
      • 如果1 s 的计数大于或等于1 ,则需要删除的最小位数为 1。
      • 如果2 s 的计数大于或等于2 ,则需要删除的最小位数为2 [Since (2 + 2) % 3 = 1]
    • 如果 sum = 3:应删除那些数字,其总和在除以3 时得到余数2 。因此,会出现以下情况:
      • 如果2 s 的计数大于或等于1 ,则需要删除的最小位数为1
      • 否则,如果1 s 的计数大于或等于2 ,则要删除的最小位数将为2 [(1 + 1) % 3 = 2]
  • 要找到最大要删除的位数,需要考虑以下三种情况:
    • 如果0 s 的计数大于或等于1 ,则需要删除的最大位数将是(number of digits – 1)
    • 如果1 s 和2 s 的计数都大于或等于1 ,则需要删除的最大位数将为(number of digits – 2) [(1+2) % 3 = 0]
    • 如果1 s 或2 s 的计数大于或等于3 ,则需要删除的最大数字将为(数字数 – 3) [(1+1+1) % 3 = 0, (2+ 2+2) % 3 = 0]

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the maximum and
// minimum number of digits to be
// removed to make str divisible by 3
void minMaxDigits(string str, int N)
{
    // Convert the string into
    // array of digits
    int arr[N];
    for (int i = 0; i < N; i++)
        arr[i] = (str[i] - '0') % 3;
 
    // Count of 0s, 1s, and 2s
    int zero = 0, one = 0, two = 0;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
        if (arr[i] == 0)
            zero++;
        if (arr[i] == 1)
            one++;
        if (arr[i] == 2)
            two++;
    }
 
    // Find the sum of digits % 3
    int sum = 0;
 
    for (int i = 0; i < N; i++) {
        sum = (sum + arr[i]) % 3;
    }
 
    // Cases to find minimum number
    // of digits to be removed
    if (sum == 0) {
        cout << 0 << ' ';
    }
    if (sum == 1) {
        if (one && N > 1)
            cout << 1 << ' ';
        else if (two > 1 && N > 2)
            cout << 2 << ' ';
        else
            cout << -1 << ' ';
    }
    if (sum == 2) {
        if (two && N > 1)
            cout << 1 << ' ';
        else if (one > 1 && N > 2)
            cout << 2 << ' ';
        else
            cout << -1 << ' ';
    }
 
    // Cases to find maximum number
    // of digits to be removed
    if (zero > 0)
        cout << N - 1 << ' ';
    else if (one > 0 && two > 0)
        cout << N - 2 << ' ';
    else if (one > 2 || two > 2)
        cout << N - 3 << ' ';
    else
        cout << -1 << ' ';
}
 
// Driver Code
int main()
{
    string str = "12345";
    int N = str.length();
 
    // Function Call
    minMaxDigits(str, N);
 
    return 0;
}


Java
// Java program for the above approach
class GFG{
      
// Function to find the maximum and
// minimum number of digits to be
// removed to make str divisible by 3
static void minMaxDigits(String str, int N)
{
     
    // Convert the string into
    // array of digits
    int arr[] = new int[N];
    for(int i = 0; i < N; i++)
        arr[i] = (str.charAt(i) - '0') % 3;
  
    // Count of 0s, 1s, and 2s
    int zero = 0, one = 0, two = 0;
  
    // Traverse the array
    for(int i = 0; i < N; i++)
    {
        if (arr[i] == 0)
            zero++;
        if (arr[i] == 1)
            one++;
        if (arr[i] == 2)
            two++;
    }
  
    // Find the sum of digits % 3
    int sum = 0;
  
    for(int i = 0; i < N; i++)
    {
        sum = (sum + arr[i]) % 3;
    }
  
    // Cases to find minimum number
    // of digits to be removed
    if (sum == 0)
    {
        System.out.print(0 + " ");
    }
    if (sum == 1)
    {
        if ((one != 0) && (N > 1))
            System.out.print(1 + " ");
        else if (two > 1 && N > 2)
            System.out.print(2 + " ");
        else
            System.out.print(-1 + " ");
    }
    if (sum == 2)
    {
        if (two != 0 && N > 1)
            System.out.print(1 + " ");
        else if (one > 1 && N > 2)
            System.out.print(2 + " ");
        else
            System.out.print(-1 + " ");
    }
  
    // Cases to find maximum number
    // of digits to be removed
    if (zero > 0)
        System.out.print(N - 1 + " ");
    else if (one > 0 && two > 0)
        System.out.print(N - 2 + " ");
    else if (one > 2 || two > 2)
        System.out.print(N - 3 + " ");
    else
        System.out.print(-1 + " ");
}
  
// Driver code
public static void main(String[] args)
{
    String str = "12345";
    int N = str.length();
     
    // Function Call
    minMaxDigits(str, N);
}
}
  
// This code is contributed by sanjoy_62


Python3
# Python3 program for the above approach
 
# Function to find the maximum and
# minimum number of digits to be
# removed to make str divisible by 3
def minMaxDigits(str, N):
     
    # Convert the string into
    # array of digits
    arr = [0]* N
     
    for i in range(N):
        arr[i] = (ord(str[i]) -
                  ord('0')) % 3
  
    # Count of 0s, 1s, and 2s
    zero = 0
    one = 0
    two = 0
  
    # Traverse the array
    for i in range(N):
        if (arr[i] == 0):
            zero += 1
        if (arr[i] == 1):
            one += 1
        if (arr[i] == 2):
            two += 1
             
    # Find the sum of digits % 3
    sum = 0
  
    for i in range(N):
        sum = (sum + arr[i]) % 3
     
    # Cases to find minimum number
    # of digits to be removed
    if (sum == 0):
        print("0", end = " ")
     
    if (sum == 1):
        if (one and N > 1):
            print("1", end = " ")
        elif (two > 1 and N > 2):
            print("2", end = " ")
        else:
            print("-1", end = " ")
     
    if (sum == 2):
        if (two and N > 1):
            print("1", end = " ")
        elif (one > 1 and N > 2):
            print("2", end = " ")
        else:
            print("-1", end = " ")
     
    # Cases to find maximum number
    # of digits to be removed
    if (zero > 0):
        print(N - 1, end = " ")
    elif (one > 0 and two > 0):
        print(N - 2, end = " ")
    elif (one > 2 or two > 2):
        print(N - 3, end = " ")
    else :
        print("-1", end = " ")
 
# Driver Code
str = "12345"
N = len(str)
  
# Function Call
minMaxDigits(str, N)
 
# This code is contributed by susmitakundugoaldanga


C#
// C# program for the above approach
using System;
 
class GFG{
       
// Function to find the maximum and
// minimum number of digits to be
// removed to make str divisible by 3
static void minMaxDigits(string str, int N)
{
     
    // Convert the string into
    // array of digits
    int[] arr = new int[N];
    for(int i = 0; i < N; i++)
        arr[i] = (str[i] - '0') % 3;
         
    // Count of 0s, 1s, and 2s
    int zero = 0, one = 0, two = 0;
   
    // Traverse the array
    for(int i = 0; i < N; i++)
    {
        if (arr[i] == 0)
            zero++;
        if (arr[i] == 1)
            one++;
        if (arr[i] == 2)
            two++;
    }
   
    // Find the sum of digits % 3
    int sum = 0;
   
    for(int i = 0; i < N; i++)
    {
        sum = (sum + arr[i]) % 3;
    }
   
    // Cases to find minimum number
    // of digits to be removed
    if (sum == 0)
    {
        Console.Write(0 + " ");
    }
    if (sum == 1)
    {
        if ((one != 0) && (N > 1))
            Console.Write(1 + " ");
        else if (two > 1 && N > 2)
            Console.Write(2 + " ");
        else
            Console.Write(-1 + " ");
    }
    if (sum == 2)
    {
        if (two != 0 && N > 1)
            Console.Write(1 + " ");
        else if (one > 1 && N > 2)
            Console.Write(2 + " ");
        else
            Console.Write(-1 + " ");
    }
   
    // Cases to find maximum number
    // of digits to be removed
    if (zero > 0)
        Console.Write(N - 1 + " ");
    else if (one > 0 && two > 0)
        Console.Write(N - 2 + " ");
    else if (one > 2 || two > 2)
        Console.Write(N - 3 + " ");
    else
        Console.Write(-1 + " ");
}
   
// Driver code
public static void Main()
{
    string str = "12345";
    int N = str.Length;
      
    // Function Call
    minMaxDigits(str, N);
}
}
 
// This code is contributed by code_hunt


Javascript


输出:
0 4

时间复杂度: O(log 10 N)
辅助空间: O(log 10 N)