Longest subarray such that adjacent elements have at least one common digit | Set 1
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.
Examples:
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][10] 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 the worst case.
Steps to solve this problem:
1. Create an array hash. hash is an 2-dimensional array with n rows and 10 columns, where each row represents the presence of each digit (0 to 9) in the corresponding index of the input array a.
2. Initialize hash to all zeros.
3. Loop through the elements of the input array a. For each element, extract the digits from the number by repeatedly dividing the number by 10 until it becomes zero, and mark the corresponding digit in the corresponding row of hash as 1.
4. Initialize longest to the minimum integer value and count to 0.
5. Loop through the elements of the input array a and check for every two consecutive elements. If they have at least one digit in common, increment the count by 1. If they don’t have any digit in common, update longest to the maximum of longest and count + 1, and reset count to 0.
6. After the loop, update longest to the maximum of longest and count + 1.
7. Return longest.
Given below is the illustration of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int longestSubarray( int a[], int n)
{
int hash[n][10];
memset (hash, 0, sizeof (hash));
for ( int i = 0; i < n; i++) {
int num = a[i];
while (num) {
hash[i][num % 10] = 1;
num /= 10;
}
}
int longest = INT_MIN;
int count = 0;
for ( int i = 0; i < n - 1; i++) {
int j;
for (j = 0; j < 10; j++) {
if (hash[i][j] and hash[i + 1][j]) {
count++;
break ;
}
}
if (j == 10) {
longest = max(longest, count + 1);
count = 0;
}
}
longest = max(longest, count + 1);
return longest;
}
int main()
{
int a[] = { 11, 22, 33, 44, 54, 56, 63 };
int n = sizeof (a) / sizeof (a[0]);
cout << longestSubarray(a, n);
return 0;
}
|
Java
class GFG {
static int longestSubarray( int a[], int n) {
int hash[][] = new int [n][ 10 ];
for ( int i = 0 ; i < n; i++) {
int num = a[i];
while (num != 0 ) {
hash[i][num % 10 ] = 1 ;
num /= 10 ;
}
}
int longest = Integer.MIN_VALUE;
int count = 0 ;
for ( int i = 0 ; i < n - 1 ; i++) {
int j;
for (j = 0 ; j < 10 ; j++) {
if (hash[i][j] == 1 & hash[i + 1 ][j] == 1 ) {
count++;
break ;
}
}
if (j == 10 ) {
longest = Math.max(longest, count + 1 );
count = 0 ;
}
}
longest = Math.max(longest, count + 1 );
return longest;
}
public static void main(String[] args) {
int a[] = { 11 , 22 , 33 , 44 , 54 , 56 , 63 };
int n = a.length;
System.out.println(longestSubarray(a, n));
}
}
|
Python3
import sys
def longestSubarray(a, n):
hash = [[ 0 for i in range ( 10 )]
for j in range (n)]
for i in range (n):
num = a[i]
while (num):
hash [i][num % 10 ] = 1
num = int (num / 10 )
longest = - sys.maxsize - 1
count = 0
for i in range (n - 1 ):
for j in range ( 10 ):
if ( hash [i][j] and hash [i + 1 ][j]):
count + = 1
break
if (j = = 10 ):
longest = max (longest, count + 1 )
count = 0
longest = max (longest, count + 1 )
return longest
if __name__ = = '__main__' :
a = [ 11 , 22 , 33 , 44 , 54 , 56 , 63 ]
n = len (a)
print (longestSubarray(a, n))
|
C#
using System;
public class GFG {
static int longestSubarray( int []a, int n) {
int [,]hash = new int [n,10];
for ( int i = 0; i < n; i++) {
int num = a[i];
while (num != 0) {
hash[i,num % 10] = 1;
num /= 10;
}
}
int longest = int .MinValue;
int count = 0;
for ( int i = 0; i < n - 1; i++) {
int j;
for (j = 0; j < 10; j++) {
if (hash[i,j] == 1 & hash[i + 1,j] == 1) {
count++;
break ;
}
}
if (j == 10) {
longest = Math.Max(longest, count + 1);
count = 0;
}
}
longest = Math.Max(longest, count + 1);
return longest;
}
public static void Main() {
int []a = {11, 22, 33, 44, 54, 56, 63};
int n = a.Length;
Console.Write(longestSubarray(a, n));
}
}
|
PHP
<?php
function longestSubarray(& $a , $n )
{
$hash = array_fill (0, $n ,
array_fill (0, 10, NULL));
for ( $i = 0; $i < $n ; $i ++)
{
$num = $a [ $i ];
while ( $num )
{
$hash [ $i ][ $num % 10] = 1;
$num = intval ( $num / 10);
}
}
$longest = PHP_INT_MIN;
$count = 0;
for ( $i = 0; $i < $n - 1; $i ++)
{
for ( $j = 0; $j < 10; $j ++)
{
if ( $hash [ $i ][ $j ] and $hash [ $i + 1][ $j ])
{
$count ++;
break ;
}
}
if ( $j == 10)
{
$longest = max( $longest , $count + 1);
$count = 0;
}
}
$longest = max( $longest , $count + 1);
return $longest ;
}
$a = array (11, 22, 33, 44, 54, 56, 63 );
$n = sizeof( $a );
echo longestSubarray( $a , $n );
?>
|
Javascript
<script>
function longestSubarray(a,n)
{
let hash = new Array(n);
for (let i=0;i<n;i++)
{
hash[i]= new Array(10);
for (let j=0;j<10;j++)
{
hash[i][j]=0;
}
}
for (let i = 0; i < n; i++) {
let num = a[i];
while (num != 0) {
hash[i][num % 10] = 1;
num = Math.floor(num/ 10);
}
}
let longest = Number.MIN_VALUE;
let count = 0;
for (let i = 0; i < n - 1; i++) {
let j;
for (j = 0; j < 10; j++) {
if (hash[i][j] == 1 & hash[i + 1][j] == 1) {
count++;
break ;
}
}
if (j == 10) {
longest = Math.max(longest, count + 1);
count = 0;
}
}
longest = Math.max(longest, count + 1);
return longest;
}
let a=[11, 22, 33, 44, 54, 56, 63];
let n = a.length;
document.write(longestSubarray(a, n));
</script>
|
Time Complexity: O(n*10)
Longest subarray such that adjacent elements have at least one common digit | Set – 2
Last Updated :
18 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...