Longest subsequence such that adjacent elements have at least one common digit
Given an array arr[] of N integers, the task is to find the length of the longest sub-sequence such that adjacent elements of the sub-sequence have at least one digit in common.
Examples:
Input: arr[] = {1, 12, 44, 29, 33, 96, 89}
Output: 5
The longest sub-sequence is {1 12 29 96 89}Input: arr[] = {12, 23, 45, 43, 36, 97}
Output: 4
The longest sub-sequence is {12 23 43 36}
Approach: The idea is to store the length of longest sub-sequence for each digit present in an element of the array.
E.g. Consider arr[] = {24, 49, 96}.
The local maximum for 49 is 2 obtain from digit 4.
This local maximum will be used in finding the local maximum for 96 with common digit 9.
For that it is required for all digits in 49, dp[i][d] should be set to local maximum.
Below is the implementation of the above approach:
// C++ program to find maximum length subsequence // such that adjacent elements have at least // one common digit #include <bits/stdc++.h> using namespace std; // Returns length of maximum length subsequence int findSubsequence(int arr[], int n) { // To store the length of the // maximum length subsequence int len = 1; // To store current element arr[i] int tmp; int i, j, d; // To store the length of the sub-sequence // ending at index i and having common digit d int dp[n][10]; memset(dp, 0, sizeof(dp)); // To store digits present in current element int cnt[10]; // To store length of maximum length subsequence // ending at index i int locMax; // For first element maximum length is 1 for // each digit tmp = arr[0]; while (tmp > 0) { dp[0][tmp % 10] = 1; tmp /= 10; } // Find digits of each element, then find length // of subsequence for each digit and then find // local maximum for (i = 1; i < n; i++) { tmp = arr[i]; locMax = 1; memset(cnt, 0, sizeof(cnt)); // Find digits in current element while (tmp > 0) { cnt[tmp % 10] = 1; tmp /= 10; } // For each digit present find length of // subsequence and find local maximum for (d = 0; d <= 9; d++) { if (cnt[d]) { dp[i][d] = 1; for (j = 0; j < i; j++) { dp[i][d] = max(dp[i][d], dp[j][d] + 1); locMax = max(dp[i][d], locMax); } } } // Update value of dp[i][d] for each digit // present in current element to local maximum // found. for (d = 0; d <= 9; d++) { if (cnt[d]) { dp[i][d] = locMax; } } // Update maximum length with local maximum len = max(len, locMax); } return len; } // Driver code int main() { int arr[] = { 1, 12, 44, 29, 33, 96, 89 }; int n = sizeof(arr) / sizeof(arr[0]); cout << findSubsequence(arr, n); return 0; } |
5
Time Complexity: O(n2)
Auxiliary Space: O(n)
The auxiliary space required for above solution can be further reduced. Observe that for each digit d present in arr[i], it is required to find maximum length sub-sequence upto that digit irrespective of the fact that at which element the sub-sequence end. This reduces auxiliary space required to O(1). For each arr[i], find local maximum and update dig[d] for each digit d in arr[i] to local maximum.
Below is the implementation of the above approach:
C++
// C++ program to find maximum length subsequence // such that adjacent elements have at least // one common digit #include <bits/stdc++.h> using namespace std; // Returns length of maximum length subsequence int findSubsequence(int arr[], int n) { // To store length of maximum length subsequence int len = 1; // To store current element arr[i] int tmp; int i, j, d; // To store length of subsequence // having common digit d int dp[10]; memset(dp, 0, sizeof(dp)); // To store digits present in current element int cnt[10]; // To store local maximum for current element int locMax; // For first element maximum length is 1 for // each digit tmp = arr[0]; while (tmp > 0) { dp[tmp % 10] = 1; tmp /= 10; } // Find digits of each element, then find length // of subsequence for each digit and then find // local maximum for (i = 1; i < n; i++) { tmp = arr[i]; locMax = 1; memset(cnt, 0, sizeof(cnt)); // Find digits in current element while (tmp > 0) { cnt[tmp % 10] = 1; tmp /= 10; } // For each digit present find length of // subsequence and find local maximum for (d = 0; d <= 9; d++) { if (cnt[d]) { dp[d]++; locMax = max(locMax, dp[d]); } } // Update value of dp[d] for each digit // present in current element to local maximum // found for (d = 0; d <= 9; d++) { if (cnt[d]) { dp[d] = locMax; } } // Update maximum length with local maximum len = max(len, locMax); } return len; } // Driver code int main() { int arr[] = { 1, 12, 44, 29, 33, 96, 89 }; int n = sizeof(arr) / sizeof(arr[0]); cout << findSubsequence(arr, n); return 0; } |
Python3
# Python3 program to find maximum length # subsequence such that adjacent elements # have at least one common digit # Returns length of maximum # length subsequence def findSubsequence(arr, n) : # To store length of maximum # length subsequence length = 1; # To store length of subsequence # having common digit d dp = [0] * 10; # For first element maximum length # is 1 for each digit tmp = arr[0]; while (tmp > 0) : dp[tmp % 10] = 1; tmp //= 10; # Find digits of each element, then # find length of subsequence for each # digit and then find local maximum for i in range(1, n) : tmp = arr[i]; locMax = 1; cnt = [0] * 10 # Find digits in current element while (tmp > 0) : cnt[tmp % 10] = 1; tmp //= 10; # For each digit present find length of # subsequence and find local maximum for d in range(10) : if (cnt[d]) : dp[d] += 1; locMax = max(locMax, dp[d]); # Update value of dp[d] for each digit # present in current element to local # maximum found for d in range(10) : if (cnt[d]) : dp[d] = locMax; # Update maximum length with local # maximum length = max(length, locMax); return length; # Driver code if __name__ == "__main__" : arr = [ 1, 12, 44, 29, 33, 96, 89 ]; n = len(arr) print(findSubsequence(arr, n)); # This code is contributed by Ryuga |
PHP
0)
{
$dp[$tmp % 10] = 1;
$tmp = intval($tmp / 10);
}
// Find digits of each element, then
// find length of subsequence for each
// digit and then find local maximum
for ($i = 1; $i < $n; $i++)
{
$tmp = $arr[$i];
$locMax = 1;
$cnt = array_fill(0, 10, NULL);
// Find digits in current element
while ($tmp > 0)
{
$cnt[$tmp % 10] = 1;
$tmp = intval($tmp / 10);
}
// For each digit present find length of
// subsequence and find local maximum
for ($d = 0; $d <= 9; $d++)
{
if ($cnt[$d])
{
$dp[$d]++;
$locMax = max($locMax, $dp[$d]);
}
}
// Update value of dp[d] for each digit
// present in current element to local
// maximum found
for ($d = 0; $d <= 9; $d++)
{
if ($cnt[$d])
{
$dp[$d] = $locMax;
}
}
// Update maximum length with
// local maximum
$len = max($len, $locMax);
}
return $len;
}
// Driver code
$arr = array( 1, 12, 44, 29, 33, 96, 89 );
$n = sizeof($arr);
echo findSubsequence($arr, $n);
// This code is contributed by ita_c
?>
5
Time Complexity: O(n)
Auxiliary Space: O(1)
Recommended Posts:
- Longest subarray such that adjacent elements have at least one common digit | Set 1
- Longest subarray such that adjacent elements have at least one common digit | Set - 2
- Maximum length subsequence such that adjacent elements in the subsequence have a common factor
- Longest Subsequence with at least one common digit in every element
- Longest Common Subsequence | DP-4
- Longest Common Anagram Subsequence
- Longest Common Subsequence | DP using Memoization
- Longest Common Subsequence with at most k changes allowed
- C++ Program for Longest Common Subsequence
- LCS (Longest Common Subsequence) of three strings
- Printing Longest Common Subsequence
- Longest Common Increasing Subsequence (LCS + LIS)
- Longest common subsequence with permutations allowed
- Length of longest common subsequence containing vowels
- Python Program for Longest Common Subsequence
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



