# Count of numbers in Array ending with digits of number N

Given a number **N** and an array **arr[]** consisting of **K** numbers, the task is to find the count of numbers in the array which ends with any of the digit present in the number N.

**Examples:**

Input:N = 1731 arr[] = {57, 6786}

Output:1

Explanation:

For 57, the last digit is 7 and since 7 is present is N, so the count is 1.

For 6786, the last digit is 6 and since 6 is not present in N, the count remains 1.

Input:N = 1324, arr[] = {23, 25, 12, 121}

Output:3

**Naive Approach:** The naive approach for this problem is that for every number in the array **arr[]**, check if its last digit is equal to any of the digits in N. Increment the count for each of such number and print it at the end.

**Time Complexity:*** O(N * K)*, where N is the number and K is the number of elements in the array arr[].

**Efficient Approach:** The efficient approach for this problem is to perform a preprocessing.

- Initially, create an array A[] of size 10.
- This array acts as a hash which stores all the digits occurred in the number N.
- After this, for every number in the array arr[], extract the last digit and check if this last digit has occurred or not in the array.
- Increment the count for each of such number and print it at the end.

Below is the implementation of the above approach:

## C++

`// C++ program to find the count ` `// of numbers in Array ending ` `// with digits of number N ` ` ` `#include <bits/stdc++.h> ` `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; ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

**Output:**

3

**Time Complexity:**

*O(N),*where N is the given number for preprocessing.*O(K),*where K is the number of queries to find answers for the queries.

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.

## Recommended Posts:

- Minimum count of numbers required ending with 7 to sum as a given number
- Count the number of digits of palindrome numbers in an array
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Count of Numbers in Range where the number does not contain more than K non zero digits
- Count Numbers with N digits which consists of even number of 0’s
- Count Numbers with N digits which consists of odd number of 0's
- Count total number of N digit numbers such that the difference between sum of even and odd digits is 1
- Count of numbers whose sum of increasing powers of digits is equal to the number itself
- Sum of two numbers where one number is represented as array of digits
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Array range queries to count the number of Fibonacci numbers with updates
- Count numbers with same first and last digits
- Sum of all the prime numbers with the count of digits ≤ D
- Count of numbers with all digits same in a given range
- Count of numbers from range [L, R] whose sum of digits is Y
- Count of numbers from range [L, R] that end with any of the given digits
- Count of Octal numbers upto N digits
- Count of n digit numbers whose sum of digits equals to given sum
- Count different numbers possible using all the digits their frequency times

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.