# 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++

 `// CPP program to print the length of the``// longest subarray such that adjacent elements``// of the subarray have at least one digit in``// common` `#include ``using` `namespace` `std;` `// Function to print the longest subarray``// such that adjacent elements have at least``// one digit in common``int` `longestSubarray(``int` `arr[], ``int` `n)``{``    ``int` `i, d;` `    ``// To mark presence of digit in current``    ``// element.``    ``int` `hash[2][10];``    ``memset``(hash, 0, ``sizeof``(hash));` `    ``// To store current row.``    ``int` `currRow;` `    ``// To store maximum length subarray length.``    ``int` `maxLen = 1;` `    ``// To store current subarray length.``    ``int` `len = 0;` `    ``// To store current array element.``    ``int` `tmp;` `    ``// Mark the presence of digits of first element.``    ``tmp = arr[0];``    ``while` `(tmp > 0) {``        ``hash[0][tmp % 10] = 1;``        ``tmp /= 10;``    ``}` `    ``currRow = 1;` `    ``// Find digits of each element and check if adjacent``    ``// elements have common digit and update len.``    ``for` `(i = 1; i < n; i++) {``        ``tmp = arr[i];` `        ``for` `(d = 0; d <= 9; d++)``            ``hash[currRow][d] = 0;` `        ``// Find all digits in element.``        ``while` `(tmp > 0) {``            ``hash[currRow][tmp % 10] = 1;``            ``tmp /= 10;``        ``}` `        ``// Find common digit in adjacent element.``        ``for` `(d = 0; d <= 9; d++) {``            ``if` `(hash[currRow][d] && hash[1 - currRow][d]) {``                ``len++;``                ``break``;``            ``}``        ``}` `        ``// If no common digit is found a new subarray``        ``// has to start from current element.``        ``if` `(d == 10) {``            ``len = 1;``        ``}` `        ``maxLen = max(maxLen, len);` `        ``currRow = 1 - currRow;``    ``}` `    ``return` `maxLen;``}` `// Driver Code``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

 `// Java program to print the length of the``// longest subarray such that adjacent elements``// of the subarray have at least one digit in``// common``import` `java.io.*;``public` `class` `GFG``{` `// Function to print the longest subarray``// such that adjacent elements have at least``// one digit in common``static` `int` `longestSubarray(``int` `arr[], ``int` `n)``{``    ``int` `i, d;` `    ``// To mark presence of digit in current``    ``// element.``    ``int` `hash[][] = ``new` `int``[``2``][``10``];``    ` `    ``for``( i = ``0``; i < ``2``; i++)``        ``for``(``int` `j = ``0``; j < ``10``; j++)``            ``hash[i][j] = ``0``;` `    ``// To store current row.``    ``int` `currRow;` `    ``// To store maximum length subarray length.``    ``int` `maxLen = ``1``;` `    ``// To store current subarray length.``    ``int` `len = ``0``;` `    ``// To store current array element.``    ``int` `tmp;` `    ``// Mark the presence of digits of first element.``    ``tmp = arr[``0``];``    ``while` `(tmp > ``0``)``    ``{``        ``hash[``0``][tmp % ``10``] = ``1``;``        ``tmp /= ``10``;``    ``}` `    ``currRow = ``1``;` `    ``// Find digits of each element and check if adjacent``    ``// elements have common digit and update len.``    ``for` `(i = ``1``; i < n; i++)``    ``{``        ``tmp = arr[i];` `        ``for` `(d = ``0``; d <= ``9``; d++)``            ``hash[currRow][d] = ``0``;` `        ``// Find all digits in element.``        ``while` `(tmp > ``0``)``        ``{``            ``hash[currRow][tmp % ``10``] = ``1``;``            ``tmp /= ``10``;``        ``}` `        ``// Find common digit in adjacent element.``        ``for` `(d = ``0``; d <= ``9``; d++)``        ``{``            ``if` `(hash[currRow][d] != ``0` `&& hash[``1` `- currRow][d] != ``0``)``            ``{``                ``len++;``                ``break``;``            ``}``        ``}` `        ``// If no common digit is found a new subarray``        ``// has to start from current element.``        ``if` `(d == ``10``)``        ``{``            ``len = ``1``;``        ``}` `        ``maxLen = Math.max(maxLen, len);` `        ``currRow = ``1` `- currRow;``    ``}` `    ``return` `maxLen;``}` `// Driver Code``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));``}``}` `// This code is contributed by Arnab Kundu`

## Python3

 `# Python3 program to print the length of the``# longest subarray such that adjacent elements``# of the subarray have at least one digit in``# common``import` `math` `# Function to print the longest subarray``# such that adjacent elements have at least``# one digit in common``def` `longestSubarray(arr, n):` `    ``i ``=` `d ``=` `0``;` `    ``# To mark presence of digit in current``    ``# element.``    ``HASH1 ``=` `[[``0` `for` `x ``in` `range``(``10``)]``                ``for` `y ``in` `range``(``2``)];` `    ``# To store current row.``    ``currRow ``=` `0``;` `    ``# To store maximum length subarray length.``    ``maxLen ``=` `1``;` `    ``# To store current subarray length.``    ``len1 ``=` `0``;` `    ``# To store current array element.``    ``tmp ``=` `0``;` `    ``# Mark the presence of digits``    ``# of first element.``    ``tmp ``=` `arr[``0``];``    ``while` `(tmp > ``0``):``        ``HASH1[``0``][tmp ``%` `10``] ``=` `1``;``        ``tmp ``=` `tmp ``/``/` `10``;` `    ``currRow ``=` `1``;` `    ``# Find digits of each element and check``    ``# if adjacent elements have common digit``    ``# and update len.``    ``for` `i ``in` `range``(``0``, n):``        ``tmp ``=` `arr[i];` `        ``for` `d ``in` `range``(``0``, ``10``):``            ``HASH1[currRow][d] ``=` `0``;` `        ``# Find all digits in element.``        ``while` `(tmp > ``0``):``            ``HASH1[currRow][tmp ``%` `10``] ``=` `1``;``            ``tmp ``=` `tmp ``/``/` `10``;` `        ``# Find common digit in adjacent element.``        ``for` `d ``in` `range``(``0``, ``10``):``            ``if` `(HASH1[currRow][d] ``and``                ``HASH1[``1` `-` `currRow][d]):``                ``len1 ``+``=` `1``;``                ``break``;` `        ``# If no common digit is found a new subarray``        ``# has to start from current element.``        ``if` `(d ``=``=` `10``):``            ``len1 ``=` `1``;` `        ``maxLen ``=` `max``(maxLen, len1);` `        ``currRow ``=` `1` `-` `currRow;` `    ``return` `maxLen;` `# Driver Code``arr ``=` `[ ``11``, ``22``, ``33``, ``44``, ``54``, ``56``, ``63` `];``n ``=` `len``(arr);` `print``(longestSubarray(arr, n));` `# This code is contributed by chandan_jnu`

## C#

 `// C# program to print the length of the``// longest subarray such that adjacent elements``// of the subarray have at least one digit in``// common``using` `System;` `class` `GFG``{` `// Function to print the longest subarray``// such that adjacent elements have at least``// one digit in common``static` `int` `longestSubarray(``int` `[]arr, ``int` `n)``{``    ``int` `i, d;` `    ``// To mark presence of digit in current``    ``// element.``    ``int``[,] hash = ``new` `int``[2,10];``    ` `    ``for``( i = 0; i < 2; i++)``        ``for``(``int` `j = 0; j < 10; j++)``            ``hash[i,j] = 0;` `    ``// To store current row.``    ``int` `currRow;` `    ``// To store maximum length subarray length.``    ``int` `maxLen = 1;` `    ``// To store current subarray length.``    ``int` `len = 0;` `    ``// To store current array element.``    ``int` `tmp;` `    ``// Mark the presence of digits of first element.``    ``tmp = arr[0];``    ``while` `(tmp > 0)``    ``{``        ``hash[0,tmp % 10] = 1;``        ``tmp /= 10;``    ``}` `    ``currRow = 1;` `    ``// Find digits of each element and check if adjacent``    ``// elements have common digit and update len.``    ``for` `(i = 1; i < n; i++)``    ``{``        ``tmp = arr[i];` `        ``for` `(d = 0; d <= 9; d++)``            ``hash[currRow,d] = 0;` `        ``// Find all digits in element.``        ``while` `(tmp > 0)``        ``{``            ``hash[currRow,tmp % 10] = 1;``            ``tmp /= 10;``        ``}` `        ``// Find common digit in adjacent element.``        ``for` `(d = 0; d <= 9; d++)``        ``{``            ``if` `(hash[currRow,d] != 0 &&``                ``hash[1 - currRow,d] != 0)``            ``{``                ``len++;``                ``break``;``            ``}``        ``}` `        ``// If no common digit is found a new subarray``        ``// has to start from current element.``        ``if` `(d == 10)``        ``{``            ``len = 1;``        ``}` `        ``maxLen = Math.Max(maxLen, len);` `        ``currRow = 1 - currRow;``    ``}` `    ``return` `maxLen;``}` `// Driver Code``static` `void` `Main()``{``    ``int` `[]arr = { 11, 22, 33, 44, 54, 56, 63 };``    ``int` `n = arr.Length;` `    ``Console.WriteLine( longestSubarray(arr, n));``}``}` `// This code is contributed by chandan_jnu`

## PHP

 ` 0)``    ``{``        ``\$hash``[0][``\$tmp` `% 10] = 1;``        ``\$tmp` `= (int)(``\$tmp` `/ 10);``    ``}` `    ``\$currRow` `= 1;` `    ``// Find digits of each element and check``    ``// if adjacent elements have common digit``    ``// and update len.``    ``for` `(``\$i` `= 1; ``\$i` `< ``\$n``; ``\$i``++)``    ``{``        ``\$tmp` `= ``\$arr``[``\$i``];` `        ``for` `(``\$d` `= 0; ``\$d` `<= 9; ``\$d``++)``            ``\$hash``[``\$currRow``][``\$d``] = 0;` `        ``// Find all digits in element.``        ``while` `(``\$tmp` `> 0)``        ``{``            ``\$hash``[``\$currRow``][``\$tmp` `% 10] = 1;``            ``\$tmp` `=(int)(``\$tmp``/10);``        ``}` `        ``// Find common digit in adjacent element.``        ``for` `(``\$d` `= 0; ``\$d` `<= 9; ``\$d``++)``        ``{``            ``if` `(``\$hash``[``\$currRow``][``\$d``] &&``                ``\$hash``[1 - ``\$currRow``][``\$d``])``            ``{``                ``\$len``++;``                ``break``;``            ``}``        ``}` `        ``// If no common digit is found a new subarray``        ``// has to start from current element.``        ``if` `(``\$d` `== 10)``        ``{``            ``\$len` `= 1;``        ``}` `        ``\$maxLen` `= max(``\$maxLen``, ``\$len``);` `        ``\$currRow` `= 1 - ``\$currRow``;``    ``}` `    ``return` `\$maxLen``;``}` `// Driver Code``\$arr` `= ``array``( 11, 22, 33, 44, 54, 56, 63 );``\$n` `= ``count``(``\$arr``);` `echo` `longestSubarray(``\$arr``, ``\$n``);` `// This code is contributed by chandan_jnu``?>`

## Javascript

 ``

Output:

`4`

Time Complexity: O(N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up