Longest Subsequence with at least one common digit in every element
Last Updated :
06 Sep, 2022
Given an array. The task is to find the length of the longest subsequence in which all elements must have at least one digit in common.
Examples:
Input : arr[] = { 11, 12, 23, 74, 13 }
Output : 3
Explanation: The elements 11, 12, and 13 have the digit ‘1’ as common. So it is the required longest sub-sequence.
Input : arr[] = { 12, 90, 67, 78, 45 }
Output : 2
Normal Approach: Find all the subsequences of the array and find the subsequence in which every element must have a common digit. Then we have to find the longest such subsequence and print the length of that subsequence. This method will take exponential time complexity.
Efficient Approach: The idea is to take a hash array of size 10 to store the count of digits from 0-9 appearing in the elements of the array. Traverse the array and for every element of the array, find the unique digits in that element and increment their count in the hash array. Now, the digit with the maximum count in the hash array indicates that it is the maximum occurring common digit among the elements of the array. So, the length of the required longest subsequence will be the count of maximum in the hash array.
Let us take an example,
Let the array be arr[] = {11, 12, 13, 24}
Initially the count array is { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
1st element is 11 so digits are 1 and 1 (but 1 will be counted once}
count array is { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0 }
2nd element is 12 so digits are 1, 2
count array is { 0, 2, 1, 0, 0, 0, 0, 0, 0, 0 }
3rd element is 13 so digits are 1, 3
count array is { 0, 3, 1, 1, 0, 0, 0, 0, 0, 0 }
4th element is 24 so digits are 2, 4
count array is { 0, 3, 2, 1, 1, 0, 0, 0, 0, 0 }
So the maximum value in count array is 3
Therefore, 3 will be the answer
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void count_( int count[], int e)
{
bool hash[10];
memset (hash, false , sizeof (hash));
while (e > 0) {
if (hash[e % 10] == false )
count[e % 10]++;
hash[e % 10] = true ;
e /= 10;
}
}
void find_subsequence( int arr[], int n)
{
int count[10];
memset (count, 0, sizeof (count));
for ( int i = 0; i < n; i++) {
count_(count, arr[i]);
}
int longest = 0;
for ( int i = 0; i < 10; i++)
longest = max(count[i], longest);
cout << longest << endl;
}
int main()
{
int arr[] = { 11, 12, 23, 74, 13 };
int n = sizeof (arr) / sizeof (arr[0]);
find_subsequence(arr, n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void count_( int count[], int e)
{
boolean hash[] = new boolean [ 10 ];
while (e > 0 ) {
if (hash[e % 10 ] == false )
count[e % 10 ]++;
hash[e % 10 ] = true ;
e /= 10 ;
}
}
static void find_subsequence( int arr[], int n)
{
int count[] = new int [ 10 ];
for ( int i = 0 ; i < n; i++) {
count_(count, arr[i]);
}
int longest = 0 ;
for ( int i = 0 ; i < 10 ; i++)
longest = Math.max(count[i], longest);
System.out.print( longest);
}
public static void main (String[] args) {
int arr[] = { 11 , 12 , 23 , 74 , 13 };
int n =arr.length;
find_subsequence(arr, n);
}
}
|
Python3
def count_(count, e):
hash = [ False ] * 10
while (e > 0 ):
if ( hash [e % 10 ] = = False ) :
count[e % 10 ] + = 1
hash [e % 10 ] = True
e / / = 10
def find_subsequence(arr, n) :
count = [ 0 ] * 10
for i in range ( n) :
count_(count, arr[i])
longest = 0
for i in range ( 10 ) :
longest = max (count[i], longest)
print (longest)
if __name__ = = "__main__" :
arr = [ 11 , 12 , 23 , 74 , 13 ]
n = len (arr)
find_subsequence(arr, n)
|
C#
using System;
class GFG
{
static void count_( int []count, int e)
{
bool []hash = new bool [10];
while (e > 0)
{
if (hash[e % 10] == false )
count[e % 10]++;
hash[e % 10] = true ;
e /= 10;
}
}
static void find_subsequence( int []arr,
int n)
{
int []count = new int [10];
for ( int i = 0; i < n; i++)
{
count_(count, arr[i]);
}
int longest = 0;
for ( int i = 0; i < 10; i++)
longest = Math.Max(count[i], longest);
Console.WriteLine(longest);
}
public static void Main ()
{
int []arr = { 11, 12, 23, 74, 13 };
int n = arr.Length;
find_subsequence(arr, n);
}
}
|
Javascript
<script>
function count_(count, e)
{
var hash = Array(10).fill( false );
while (e > 0) {
if (hash[e % 10] == false )
count[e % 10]++;
hash[e % 10] = true ;
e /= 10;
}
}
function find_subsequence(arr, n)
{
var count = Array(10).fill(0);
for ( var i = 0; i < n; i++) {
count_(count, arr[i]);
}
var longest = 0;
for ( var i = 0; i < 10; i++)
longest = Math.max(count[i], longest);
document.write( longest);
}
var arr = [ 11, 12, 23, 74, 13 ];
var n = arr.length;
find_subsequence(arr, n);
</script>
|
Complexity Analysis:
- Time Complexity: O(N)
- Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...