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

📅  最后修改于: 2021-05-17 19:34:43             🧑  作者: Mango

给定一个数字字符串S ,任务是找到必须从S除去的最小位数和最大位数,以使其能被3整除。如果无法这样做,请打印“ -1”

例子:

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

  • 将给定字符串的每个数字插入到数组中,例如arr [] ,形式为(S [i] –’0’)%3
  • 在数组arr []中找到0 s, 1 s和2 s的计数。
  • 然后,计算数字总和,即(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 ,则需要删除的最大位数为(位数– 1)
    • 如果1 s和2 s的计数都大于或等于1 ,则需要删除的最大位数为(位数– 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)