# Longest subarray such that adjacent elements have at least one common digit | Set – 2

• Last Updated : 21 Apr, 2021

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.
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``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