给定一个数组。任务是找到最长子序列的长度,其中所有元素必须至少有一个共同的数字。
例子:
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
常规方法:查找数组的所有子序列,并找到每个元素必须具有相同数字的子序列。然后,我们必须找到最长的子序列并打印该子序列的长度。此方法将花费指数时间。
高效方法:这个想法是采用大小为10的哈希数组来存储出现在数组元素中的0到9之间的数字计数。遍历数组,对于数组的每个元素,找到该元素中的唯一数字,并在哈希数组中增加其计数。现在,哈希数组中具有最大计数的数字表示它是数组元素之间出现的最大公共数字。因此,所需的最长子序列的长度将是哈希数组中的最大计数。
让我们举个例子
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
下面是上述方法的实现:
C++
// C++ program to find the length of subsequence which has
// atleast one digit common among all its elements
#include
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
// 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
# 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#
// 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
Javascript
3
时间复杂度: O(N)
辅助空间: O(N)
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。