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++
#include <bits/stdc++.h>
using namespace std;
int digit[10] = { 0 };
void digitsPresent( int n)
{
int lastDigit;
while (n != 0) {
lastDigit = n % 10;
digit[lastDigit] = true ;
n /= 10;
}
}
int checkLastDigit( int num)
{
int count = 0;
int lastDigit;
lastDigit = num % 10;
if (digit[lastDigit] == true )
count++;
return 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;
}
int main()
{
int N = 1731;
digitsPresent(N);
int K = 5;
int arr[] = { 57, 6786,
1111, 3, 9812 };
findCount(N, K, arr);
return 0;
}
|
Java
import java.io.*;
public class GFG{
public static int [] digit = new int [ 10 ];
public static void digitsPresent( int n)
{
int lastDigit;
while (n != 0 )
{
lastDigit = n % 10 ;
digit[lastDigit] = 1 ;
n /= 10 ;
}
}
public static int checkLastDigit( int num)
{
int count = 0 ;
int lastDigit;
lastDigit = num % 10 ;
if (digit[lastDigit] == 1 )
count++;
return 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);
}
public static void main(String[] args)
{
int N = 1731 ;
digitsPresent(N);
int K = 5 ;
int arr[] = { 57 , 6786 , 1111 , 3 , 9812 };
findCount(N, K, arr);
}
}
|
Python3
digit = [ 0 ] * 10
def digitsPresent(n):
lastDigit = 0 ;
while (n ! = 0 ):
lastDigit = n % 10 ;
digit[ int (lastDigit)] = 1 ;
n / = 10 ;
def checkLastDigit(num):
count = 0 ;
lastDigit = 0 ;
lastDigit = num % 10 ;
if (digit[ int (lastDigit)] = = 1 ):
count + = 1
return count;
def findCount(N, K, arr):
count = 0 ;
for i in range (K):
if checkLastDigit(arr[i]) = = 1 :
count + = 1
else :
count
print (count)
N = 1731 ;
digitsPresent(N);
K = 5 ;
arr = [ 57 , 6786 , 1111 , 3 , 9812 ];
findCount(N, K, arr);
|
C#
using System;
class GFG{
public static int []digit = new int [10];
public static void digitsPresent( int n)
{
int lastDigit;
while (n != 0)
{
lastDigit = n % 10;
digit[lastDigit] = 1;
n /= 10;
}
}
public static int checkLastDigit( int num)
{
int count = 0;
int lastDigit;
lastDigit = num % 10;
if (digit[lastDigit] == 1)
count++;
return 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);
}
static public void Main()
{
int N = 1731;
digitsPresent(N);
int K = 5;
int []arr = { 57, 6786, 1111, 3, 9812 };
findCount(N, K, arr);
}
}
|
Javascript
<script>
let digit = new Uint8Array(10);
function digitsPresent(n)
{
let lastDigit;
while (n != 0) {
lastDigit = n % 10;
digit[lastDigit] = true ;
n = Math.floor(n/10);
}
}
function checkLastDigit(num)
{
let count = 0;
let lastDigit;
lastDigit = num % 10;
if (digit[lastDigit] == true )
count++;
return count;
}
function findCount(N, K, arr)
{
let count = 0;
for (let i = 0; i < K; i++) {
count = checkLastDigit(arr[i]) == 1
? count + 1
: count;
}
document.write(count + "<br>" );
}
let N = 1731;
digitsPresent(N);
let K = 5;
let arr = [ 57, 6786,
1111, 3, 9812 ];
findCount(N, K, arr);
</script>
|
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.
Auxiliary Space: O(1)
Last Updated :
09 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...