Digital Sequence

Given an array A of length N. You need to find the maximum length of the subsequence of A which contains any one digit common in all the elements of that subsequence. You are not allowed to count leading zeroes.

Example:

Input:  n = 5, arr = [12, 11, 3, 4, 5]
Output: 2

Approach

C++

#include <bits/stdc++.h>
using namespace std;
set<intuniqueDigits(int a)
{
    set<intst;
    while (a)
    {
        st.insert(a % 10);
        a = a / 10;
    }
    return st;
}

int digitalSequence(int nint arr[])
{
    int max1 = INT_MIN;
    for (int i = 0i <= 9i++)
    {
        int cnt = 0;
        for (int j = 0j < nj++)
        {
            int x = arr[j];
            set<intst = uniqueDigits(x);
            if (st.find(i!= st.end())
                cnt++;
        }
        max1 = max(max1cnt);
    }
    return max1;
}
int main()
{
    int n = 5;

    int arr[n] = {1211345};

    cout << digitalSequence(narr<< "\n";

    return 0;
}


No comments:

Post a Comment