Given an array of N integers, write a program that prints the length of the longest subarray such that adjacent elements of the subarray have at least one digit in common.
Input : 12 23 45 43 36 97 Output : 3 Explanation: The subarray is 45 43 36 which has 4 common in 45, 43 and 3 common in 43, 36. Input : 11 22 33 44 54 56 63 Output : 4 Explanation: Subarray is 44, 54, 56, 63
A normal approach will be to check for all the subarrays possible. But the time complexity will be O(n2).
An efficient approach will be to create a hash[n] array which marks the occurrence of digits in the i-th index number. We iterate for every element and check if adjacent elements have a digit common in between. If they have a common digit, we keep the count of the length. If the adjacent elements do not have a digit in common, we initialize the count to zero and start counting again for a subarray. Print the maximum count which is obtained while iteration. We use a hash array to minimize the time complexity as the number can be of range 10^18 which will take 18 iterations in worst case.
Given below is the illustration of the above approach:
Time Complexity: O(n*10)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Longest subarray such that adjacent elements have at least one common digit | Set - 2
- Longest subsequence such that adjacent elements have at least one common digit
- Count divisors of n that have at-least one digit common with n
- Count pairs in an array which have at least one digit common
- Longest Subsequence with at least one common digit in every element
- Maximum length subsequence such that adjacent elements in the subsequence have a common factor
- Maximum subset sum such that no two elements in set have same digit in them
- Number of sub-arrays that have at least one duplicate
- Find all elements in array which have at-least two greater elements
- Maximum sub-sequence sum such that indices of any two adjacent elements differs at least by 3
- Longest subarray such that the difference of max and min is at-most one
- First subarray having sum at least half the maximum sum of any subarray of size K
- Count pairs (p, q) such that p occurs in array at least q times and q occurs at least p times
- Minimum sum subsequence such that at least one of every four consecutive elements is picked
- Count numbers up to N which contains at least one repeated digit
- Minimum subarray reversals required such that sum of all pairs of adjacent elements is odd
- Length of longest subarray of length at least 2 with maximum GCD
- Arrange array elements such that last digit of an element is equal to first digit of the next element
- Count of N-digit numbers having digit XOR as single digit
- Longest common subarray in the given two arrays
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.