📜  数组中以数字N结尾的数字计数

📅  最后修改于: 2021-05-17 03:09:10             🧑  作者: Mango

给定一个数字N和一个由K个数字组成的数组arr [] ,任务是在数组中找到数字的计数,该计数以数字N中存在的任何数字结尾。
例子:

幼稚的方法:针对此问题的幼稚的方法是,对于数组arr []中的每个数字,检查其最后一位是否等于N中的任何数字。增加每个数字的计数并在最后打印。
时间复杂度: O(N * K) ,其中N是数字,K是数组arr []中的元素数。
高效方法:解决此问题的有效方法是执行预处理。

  • 最初,创建大小为10的数组A []。
  • 该数组用作存储存储在数字N中的所有数字的哈希。
  • 此后,对于数组arr []中的每个数字,提取最后一个数字,并检查该最后一个数字是否出现在数组中。
  • 递增每个数字的计数并在最后打印。

下面是上述方法的实现:

C++
// C++ program to find the count
// of numbers in Array ending
// with digits of number N
  
#include 
using namespace std;
  
// Array to keep the
// track of digits occurred
// Initially all are 0(false)
int digit[10] = { 0 };
  
// Function to initialize true
// if the digit is present
void digitsPresent(int n)
{
    // Variable to store the last digit
    int lastDigit;
  
    // Loop to iterate through every
    // digit of the number N
    while (n != 0) {
        lastDigit = n % 10;
  
        // Updating the array according
        // to the presence of the
        // digit in n at the array index
        digit[lastDigit] = true;
        n /= 10;
    }
}
  
// Function to check if the
// numbers in the array
// end with the digits of
// the number N
int checkLastDigit(int num)
{
  
    // Variable to store the count
    int count = 0;
  
    // Variable to store the last digit
    int lastDigit;
    lastDigit = num % 10;
  
    // Checking the presence of
    // the last digit in N
    if (digit[lastDigit] == true)
        count++;
  
    return count;
}
  
// Function to find
// the required count
void findCount(int N, int K, int arr[])
{
  
    int count = 0;
  
    for (int i = 0; i < K; i++) {
  
        count = checkLastDigit(arr[i]) == 1
                    ? count + 1
                    : count;
    }
    cout << count << endl;
}
  
// Driver code
int main()
{
    int N = 1731;
  
    // Preprocessing
    digitsPresent(N);
  
    int K = 5;
    int arr[] = { 57, 6786,
                  1111, 3, 9812 };
  
    findCount(N, K, arr);
    return 0;
}


Java
// Java program to find the count
// of numbers in Array ending
// with digits of number N
class GFG{
  
// Array to keep the
// track of digits occurred 
// Initially all are 0(false)
public static int[] digit = new int[10];
  
// Function to initialize 1(true)
// if the digit is present
public static void digitsPresent(int n)
{
      
    // Variable to store the last digit
    int lastDigit;
  
    // Loop to iterate through every
    // digit of the number N
    while (n != 0)
    {
        lastDigit = n % 10;
  
        // Updating the array according
        // to the presence of the
        // digit in n at the array index
        digit[lastDigit] = 1;
        n /= 10;
    }
}
  
// Function to check if the
// numbers in the array
// end with the digits of
// the number N
public static int checkLastDigit(int num)
{
  
    // Variable to store the count
    int count = 0;
  
    // Variable to store the last digit
    int lastDigit;
    lastDigit = num % 10;
  
    // Checking the presence of
    // the last digit in N
    if (digit[lastDigit] == 1)
        count++;
  
    return count;
}
  
// Function to find
// the required count
public static void findCount(int N, int K,
                             int arr[])
{
    int count = 0;
  
    for(int i = 0; i < K; i++) 
    {
       count = checkLastDigit(arr[i]) == 1 ? 
               count + 1 : count;
    }
    System.out.println(count);
}
  
// Driver code
public static void main(String[] args)
{
    int N = 1731;
  
    // Preprocessing
    digitsPresent(N);
  
    int K = 5;
    int arr[] = { 57, 6786, 1111, 3, 9812 };
  
    findCount(N, K, arr);
}
}
  
// This code is contributed by Sayantan Pal


Python3
# Python3 program to find the count 
# of numbers in Array ending 
# with digits of number N 
  
# Array to keep the 
# track of digits occurred 
# Initially all are 0(false) 
digit = [0] * 10
  
# Function to initialize true 
# if the digit is present
def digitsPresent(n):
      
    # Variable to store the last digit
    lastDigit = 0;
  
    # Loop to iterate through every 
    # digit of the number N
    while (n != 0):
        lastDigit = n % 10;
          
        # Updating the array according 
        # to the presence of the 
        # digit in n at the array index 
        digit[int(lastDigit)] = 1;
        n /= 10;
  
# Function to check if the numbers 
# in the array end with the digits 
# of the number N
def checkLastDigit(num):
  
    # Variable to store the count
    count = 0;
  
    # Variable to store the last digit
    lastDigit = 0;
    lastDigit = num % 10;
  
    # Checking the presence of 
    # the last digit in N
    if (digit[int(lastDigit)] == 1):
        count += 1
  
    return count;
  
# Function to find the required count
def findCount(N, K, arr):
  
    count = 0;
    for i in range(K):
        if checkLastDigit(arr[i]) == 1:
            count += 1
        else:
            count
              
    print(count)
  
# Driver code
N = 1731;
  
# Preprocessing
digitsPresent(N);
  
K = 5;
arr = [ 57, 6786, 1111, 3, 9812 ];
  
findCount(N, K, arr);
  
# This code is contributed by grand_master


C#
// C# program to find the count
// of numbers in Array ending
// with digits of number N
using System;
  
class GFG{
  
// Array to keep the track of digits occurred 
// Initially all are 0(false)
public static int []digit = new int[10];
  
// Function to initialize 1(true)
// if the digit is present
public static void digitsPresent(int n)
{
  
    // Variable to store the last digit
    int lastDigit;
  
    // Loop to iterate through every
    // digit of the number N
    while (n != 0)
    {
        lastDigit = n % 10;
  
        // Updating the array according to the 
        // presence of the digit in n at the
        // array index
        digit[lastDigit] = 1;
        n /= 10;
    }
}
  
// Function to check if the numbers in the
// array end with the digits of the number N
public static int checkLastDigit(int num)
{
  
    // Variable to store the count
    int count = 0;
  
    // Variable to store the last digit
    int lastDigit;
    lastDigit = num % 10;
  
    // Checking the presence of
    // the last digit in N
    if (digit[lastDigit] == 1)
        count++;
  
    return count;
}
  
// Function to find the required count
public static void findCount(int N, int K,
                             int []arr)
{
    int count = 0;
  
    for(int i = 0; i < K; i++) 
    {
        count = checkLastDigit(arr[i]) == 1 ?
                count + 1 : count;
    }
    Console.WriteLine(count);
}
  
// Driver code
static public void Main()
{
    int N = 1731;
  
    // Preprocessing
    digitsPresent(N);
  
    int K = 5;
    int []arr = { 57, 6786, 1111, 3, 9812 };
  
    findCount(N, K, arr);
}
}
  
// This code is contributed by piyush3010


输出:
3

时间复杂度:

  • O(N),其中N是用于预处理的给定数字。
  • O(K),其中K是为查询找到答案的查询数。