Longest subarray such that adjacent elements have at least one common digit | Set – 2
Last Updated :
18 Feb, 2023
Given an array of N integers, the task is to find the length of the longest subarray such that adjacent elements of the subarray have at least one digit in common.
Examples:
Input : arr[] = {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 : arr[] = {11, 22, 33, 44, 54, 56, 63}
Output : 4
Explanation: Subarray is 44, 54, 56, 63
The solution discussed in previous post uses O(N) extra space. The problem can be solved using constant space. A hashmap of constant size is used to store whether a digit is present in a given array element or not. To check if adjacent elements have a common digit, only count of digits for two adjacent elements is required. So the number of rows required in hashmap can be reduced to 2. The variable currRow represents current row and 1 – currRow represents previous row in hashmap. If adjacent elements have common digit then increase current length by 1 and compare it with maximum length. Otherwise set current length to 1.
Steps to solve this problem:
1. Initialize a two-dimensional array ‘hash’ of size 2×10 to store the presence of digits in each element of the input array.
2. Initialize the variable ‘currRow’ to store the current row, ‘maxLen’ to store the maximum length of the subarray and ‘len’ to store the length of the current subarray.
3. Initialize the variable ‘tmp’ to store the current array element.
4. Find the digits in the first element of the input array and mark the presence of each digit in the ‘hash’ array.
5. Repeat steps 6-15 for each element of the input array:
*Set the value of ‘tmp’ to the current array element.
*Initialize ‘d’ with 0 and repeat steps 8-10 for each digit (0 to 9):
*Set the value of the current row of ‘hash’ for the current digit to 0.
*Repeat steps 10-13 until ‘tmp’ becomes 0:
*Mark the presence of the current digit in the current row of ‘hash’.
*Divide ‘tmp’ by 10 to get the next digit.
*Initialize ‘d’ with 0 and repeat steps 13-15 for each digit (0 to 9):
*If the current digit is present in both the current row and the previous row of ‘hash’, increment the value of ‘len’ by 1 and break the loop.
*If no common digit is found between the current and previous elements, reset the value of ‘len’ to 1.
*Update the value of ‘maxLen’ to be the maximum of ‘maxLen’ and ‘len’.
*Update the value of ‘currRow’ to be the alternate of its current value (either 0 or 1).
6. Return the value of ‘maxLen’ as the length of the longest subarray where adjacent elements have at least one digit in common.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int longestSubarray( int arr[], int n)
{
int i, d;
int hash[2][10];
memset (hash, 0, sizeof (hash));
int currRow;
int maxLen = 1;
int len = 0;
int tmp;
tmp = arr[0];
while (tmp > 0) {
hash[0][tmp % 10] = 1;
tmp /= 10;
}
currRow = 1;
for (i = 1; i < n; i++) {
tmp = arr[i];
for (d = 0; d <= 9; d++)
hash[currRow][d] = 0;
while (tmp > 0) {
hash[currRow][tmp % 10] = 1;
tmp /= 10;
}
for (d = 0; d <= 9; d++) {
if (hash[currRow][d] && hash[1 - currRow][d]) {
len++;
break ;
}
}
if (d == 10) {
len = 1;
}
maxLen = max(maxLen, len);
currRow = 1 - currRow;
}
return maxLen;
}
int main()
{
int arr[] = { 11, 22, 33, 44, 54, 56, 63 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << longestSubarray(arr, n);
return 0;
}
|
Java
import java.io.*;
public class GFG
{
static int longestSubarray( int arr[], int n)
{
int i, d;
int hash[][] = new int [ 2 ][ 10 ];
for ( i = 0 ; i < 2 ; i++)
for ( int j = 0 ; j < 10 ; j++)
hash[i][j] = 0 ;
int currRow;
int maxLen = 1 ;
int len = 0 ;
int tmp;
tmp = arr[ 0 ];
while (tmp > 0 )
{
hash[ 0 ][tmp % 10 ] = 1 ;
tmp /= 10 ;
}
currRow = 1 ;
for (i = 1 ; i < n; i++)
{
tmp = arr[i];
for (d = 0 ; d <= 9 ; d++)
hash[currRow][d] = 0 ;
while (tmp > 0 )
{
hash[currRow][tmp % 10 ] = 1 ;
tmp /= 10 ;
}
for (d = 0 ; d <= 9 ; d++)
{
if (hash[currRow][d] != 0 && hash[ 1 - currRow][d] != 0 )
{
len++;
break ;
}
}
if (d == 10 )
{
len = 1 ;
}
maxLen = Math.max(maxLen, len);
currRow = 1 - currRow;
}
return maxLen;
}
public static void main(String args[])
{
int arr[] = { 11 , 22 , 33 , 44 , 54 , 56 , 63 };
int n = arr.length;
System.out.println( longestSubarray(arr, n));
}
}
|
Python3
import math
def longestSubarray(arr, n):
i = d = 0 ;
HASH1 = [[ 0 for x in range ( 10 )]
for y in range ( 2 )];
currRow = 0 ;
maxLen = 1 ;
len1 = 0 ;
tmp = 0 ;
tmp = arr[ 0 ];
while (tmp > 0 ):
HASH1[ 0 ][tmp % 10 ] = 1 ;
tmp = tmp / / 10 ;
currRow = 1 ;
for i in range ( 0 , n):
tmp = arr[i];
for d in range ( 0 , 10 ):
HASH1[currRow][d] = 0 ;
while (tmp > 0 ):
HASH1[currRow][tmp % 10 ] = 1 ;
tmp = tmp / / 10 ;
for d in range ( 0 , 10 ):
if (HASH1[currRow][d] and
HASH1[ 1 - currRow][d]):
len1 + = 1 ;
break ;
if (d = = 10 ):
len1 = 1 ;
maxLen = max (maxLen, len1);
currRow = 1 - currRow;
return maxLen;
arr = [ 11 , 22 , 33 , 44 , 54 , 56 , 63 ];
n = len (arr);
print (longestSubarray(arr, n));
|
C#
using System;
class GFG
{
static int longestSubarray( int []arr, int n)
{
int i, d;
int [,] hash = new int [2,10];
for ( i = 0; i < 2; i++)
for ( int j = 0; j < 10; j++)
hash[i,j] = 0;
int currRow;
int maxLen = 1;
int len = 0;
int tmp;
tmp = arr[0];
while (tmp > 0)
{
hash[0,tmp % 10] = 1;
tmp /= 10;
}
currRow = 1;
for (i = 1; i < n; i++)
{
tmp = arr[i];
for (d = 0; d <= 9; d++)
hash[currRow,d] = 0;
while (tmp > 0)
{
hash[currRow,tmp % 10] = 1;
tmp /= 10;
}
for (d = 0; d <= 9; d++)
{
if (hash[currRow,d] != 0 &&
hash[1 - currRow,d] != 0)
{
len++;
break ;
}
}
if (d == 10)
{
len = 1;
}
maxLen = Math.Max(maxLen, len);
currRow = 1 - currRow;
}
return maxLen;
}
static void Main()
{
int []arr = { 11, 22, 33, 44, 54, 56, 63 };
int n = arr.Length;
Console.WriteLine( longestSubarray(arr, n));
}
}
|
PHP
<?php
function longestSubarray( $arr , $n )
{
$i = $d = 0;
$hash = array_fill (0, 2, array_fill (0, 10, 0));
$currRow = 0;
$maxLen = 1;
$len = 0;
$tmp = 0;
$tmp = $arr [0];
while ( $tmp > 0)
{
$hash [0][ $tmp % 10] = 1;
$tmp = (int)( $tmp / 10);
}
$currRow = 1;
for ( $i = 1; $i < $n ; $i ++)
{
$tmp = $arr [ $i ];
for ( $d = 0; $d <= 9; $d ++)
$hash [ $currRow ][ $d ] = 0;
while ( $tmp > 0)
{
$hash [ $currRow ][ $tmp % 10] = 1;
$tmp =(int)( $tmp /10);
}
for ( $d = 0; $d <= 9; $d ++)
{
if ( $hash [ $currRow ][ $d ] &&
$hash [1 - $currRow ][ $d ])
{
$len ++;
break ;
}
}
if ( $d == 10)
{
$len = 1;
}
$maxLen = max( $maxLen , $len );
$currRow = 1 - $currRow ;
}
return $maxLen ;
}
$arr = array ( 11, 22, 33, 44, 54, 56, 63 );
$n = count ( $arr );
echo longestSubarray( $arr , $n );
?>
|
Javascript
<script>
function longestSubarray(arr, n)
{
var i, d;
var hash = Array.from(Array(2), () => Array(10));
var currRow;
var maxLen = 1;
var len = 0;
var tmp;
tmp = arr[0];
while (tmp > 0) {
hash[0][tmp % 10] = 1;
tmp = parseInt(tmp/10);
}
currRow = 1;
for (i = 1; i < n; i++) {
tmp = arr[i];
for (d = 0; d <= 9; d++)
hash[currRow][d] = 0;
while (tmp > 0) {
hash[currRow][tmp % 10] = 1;
tmp = parseInt(tmp/10);
}
for (d = 0; d <= 9; d++) {
if (hash[currRow][d] && hash[1 - currRow][d]) {
len++;
break ;
}
}
if (d == 10) {
len = 1;
}
maxLen = Math.max(maxLen, len);
currRow = 1 - currRow;
}
return maxLen;
}
var arr = [ 11, 22, 33, 44, 54, 56, 63 ];
var n = arr.length;
document.write( longestSubarray(arr, n));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...