Open In App

Longest Subsequence with at least one common digit in every element

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++ program to find the length of subsequence which has
// atleast one digit common among all its elements
#include <bits/stdc++.h>
using namespace std;
 
// If the number contains a digit increase
// the count by 1 (even if it has multiple
// same digit the count should be increased
// by only once)
void count_(int count[], int e)
{
    // Hash to make it sure that a digit
    // is counted only once
    bool hash[10];
 
    // Set the hash to its initial value
    memset(hash, false, sizeof(hash));
 
    // Extract the digits
    while (e > 0) {
 
        // If the digit did not appear before
        if (hash[e % 10] == false)
 
            // Increase the count
            count[e % 10]++;
 
        // Mark the digit as visited
        hash[e % 10] = true;
 
        // Delete the digit
        e /= 10;
    }
}
 
// Function to find the length of subsequence which has
// atleast one digit common among all its elements
void find_subsequence(int arr[], int n)
{
    // Count of digits
    int count[10];
 
    // Set the initial value to zero
    memset(count, 0, sizeof(count));
    for (int i = 0; i < n; i++) {
 
        // Extract the digits of the element
        // and increase the count
        count_(count, arr[i]);
    }
 
    // Longest subsequence
    int longest = 0;
 
    // Get the longest subsequence
    for (int i = 0; i < 10; i++)
        longest = max(count[i], longest);
 
    // Print the length of longest subsequence
    cout << longest << endl;
}
 
// Driver code
int main()
{
    int arr[] = { 11, 12, 23, 74, 13 };
 
    int n = sizeof(arr) / sizeof(arr[0]);
 
    find_subsequence(arr, n);
 
    return 0;
}




// Java program to find the length of subsequence which has
// atleast one digit common among all its elements
 
import java.io.*;
 
class GFG {
 
// If the number contains a digit increase
// the count by 1 (even if it has multiple
// same digit the count should be increased
// by only once)
static void count_(int count[], int e)
{
    // Hash to make it sure that a digit
    // is counted only once
    boolean hash[] = new boolean[10];
 
    // Set the hash to its initial value
    //memset(hash, false, sizeof(hash));
 
    // Extract the digits
    while (e > 0) {
 
        // If the digit did not appear before
        if (hash[e % 10] == false)
 
            // Increase the count
            count[e % 10]++;
 
        // Mark the digit as visited
        hash[e % 10] = true;
 
        // Delete the digit
        e /= 10;
    }
}
 
// Function to find the length of subsequence which has
// atleast one digit common among all its elements
static void find_subsequence(int arr[], int n)
{
    // Count of digits
    int count[] = new int[10];
 
    // Set the initial value to zero
    //memset(count, 0, sizeof(count));
    for (int i = 0; i < n; i++) {
 
        // Extract the digits of the element
        // and increase the count
        count_(count, arr[i]);
    }
 
    // Longest subsequence
    int longest = 0;
 
    // Get the longest subsequence
    for (int i = 0; i < 10; i++)
        longest = Math.max(count[i], longest);
 
    // Print the length of longest subsequence
    System.out.print( longest);
}
 
        // Driver code
    public static void main (String[] args) {
            int arr[] = { 11, 12, 23, 74, 13 };
 
    int n =arr.length;
 
    find_subsequence(arr, n);
 
    }
}
// This code is contributed
// by shs




# Python3 program to find the length
# of subsequence which has atleast
# one digit common among all its elements
 
# If the number contains a digit increase
# the count by 1 (even if it has multiple
# same digit the count should be increased
# by only once)
def count_(count, e):
 
    # Hash to make it sure that a digit
    # is counted only once
    hash = [False] * 10
 
    # Extract the digits
    while (e > 0):
 
        # If the digit did not
        # appear before
        if (hash[e % 10] == False) :
 
            # Increase the count
            count[e % 10] += 1
 
        # Mark the digit as visited
        hash[e % 10] = True
 
        # Delete the digit
        e //= 10
 
# Function to find the length of
# subsequence which has atleast
# one digit common among all its elements
def find_subsequence(arr, n) :
 
    # Count of digits
    count = [0] * 10
 
    for i in range ( n) :
 
        # Extract the digits of the element
        # and increase the count
        count_(count, arr[i])
 
    # Longest subsequence
    longest = 0
 
    # Get the longest subsequence
    for i in range(10) :
        longest = max(count[i], longest)
 
    # Print the length of
    # longest subsequence
    print (longest)
 
# Driver code
if __name__ == "__main__":
 
    arr = [ 11, 12, 23, 74, 13 ]
 
    n = len(arr)
 
    find_subsequence(arr, n)
 
# This code is contributed
# by ChitraNayal




// C# program to find the length
// of subsequence which has atleast
// one digit common among all its elements
using System;
 
class GFG
{
 
// If the number contains a digit increase
// the count by 1 (even if it has multiple
// same digit the count should be increased
// by only once)
static void count_(int []count, int e)
{
    // Hash to make it sure that a
    // digit is counted only once
    bool []hash = new bool[10];
 
    // Set the hash to its initial value
    //memset(hash, false, sizeof(hash));
 
    // Extract the digits
    while (e > 0)
    {
 
        // If the digit did not
        // appear before
        if (hash[e % 10] == false)
 
            // Increase the count
            count[e % 10]++;
 
        // Mark the digit as visited
        hash[e % 10] = true;
 
        // Delete the digit
        e /= 10;
    }
}
 
// Function to find the length of
// subsequence which has atleast
// one digit common among all its elements
static void find_subsequence(int []arr,
                             int n)
{
    // Count of digits
    int []count = new int[10];
 
    // Set the initial value to zero
    //memset(count, 0, sizeof(count));
    for (int i = 0; i < n; i++)
    {
 
        // Extract the digits of the element
        // and increase the count
        count_(count, arr[i]);
    }
 
    // Longest subsequence
    int longest = 0;
 
    // Get the longest subsequence
    for (int i = 0; i < 10; i++)
        longest = Math.Max(count[i], longest);
 
    // Print the length of
    // longest subsequence
    Console.WriteLine(longest);
}
 
// Driver code
public static void Main ()
{
    int []arr = { 11, 12, 23, 74, 13 };
 
    int n = arr.Length;
 
    find_subsequence(arr, n);
}
}
 
// This code is contributed
// by Shashank




<script>
 
// Javascript program to find the length of subsequence which has
// atleast one digit common among all its elements
 
// If the number contains a digit increase
// the count by 1 (even if it has multiple
// same digit the count should be increased
// by only once)
function count_(count, e)
{
    // Hash to make it sure that a digit
    // is counted only once
    var hash = Array(10).fill(false);
 
    // Extract the digits
    while (e > 0) {
 
        // If the digit did not appear before
        if (hash[e % 10] == false)
 
            // Increase the count
            count[e % 10]++;
 
        // Mark the digit as visited
        hash[e % 10] = true;
 
        // Delete the digit
        e /= 10;
    }
}
 
// Function to find the length of subsequence which has
// atleast one digit common among all its elements
function find_subsequence(arr, n)
{
    // Count of digits
    var count = Array(10).fill(0);
 
    for (var i = 0; i < n; i++) {
 
        // Extract the digits of the element
        // and increase the count
        count_(count, arr[i]);
    }
 
    // Longest subsequence
    var longest = 0;
 
    // Get the longest subsequence
    for (var i = 0; i < 10; i++)
        longest = Math.max(count[i], longest);
 
    // Print the length of longest subsequence
    document.write( longest);
}
 
// Driver code
var arr = [ 11, 12, 23, 74, 13 ];
var n = arr.length;
find_subsequence(arr, n);
 
</script>   

Output
3

Complexity Analysis:


Article Tags :