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 worst case.

Given below is the illustration of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 <bits/stdc++.h>
using namespace std;
  
// function to print the longest subarray
// such that adjacent elements have at least
// one digit in common
int longestSubarray(int a[], int n)
{
    // remembers the occurrence of digits in
    // i-th index number
    int hash[n][10];
    memset(hash, 0, sizeof(hash));
  
    // marks the presence of digit in i-th
    // index number
    for (int i = 0; i < n; i++) {
        int num = a[i];
        while (num) {
            // marks the digit
            hash[i][num % 10] = 1;
            num /= 10;
        }
    }
  
    // counts the longest Subarray
    int longest = INT_MIN;
    // counts the subarray
    int count = 0;
  
    // check for all adjacent elements
    for (int i = 0; i < n - 1; i++) {
        int j;
        for (j = 0; j < 10; j++) {
  
            // if adjacent elements have digit j 
            // in them count and break as we have
            // got at-least one digit
            if (hash[i][j] and hash[i + 1][j]) {
                count++;
                break;
            }
        }
        // if no digits are common
        if (j == 10) {
            longest = max(longest, count + 1);
            count = 0;
        }
    }
  
    longest = max(longest, count + 1);
  
    // returns the length of the longest subarray
    return longest;
}
// Driver Code
int main()
{
    int a[] = { 11, 22, 33, 44, 54, 56, 63 };
  
    int n = sizeof(a) / sizeof(a[0]);
    // function call
    cout << longestSubarray(a, n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 a[], int n) {
        // remembers the occurrence of digits in
        // i-th index number
        int hash[][] = new int[n][10];
  
        // marks the presence of digit in i-th
        // index number
        for (int i = 0; i < n; i++) {
            int num = a[i];
            while (num != 0) {
                // marks the digit
                hash[i][num % 10] = 1;
                num /= 10;
            }
        }
  
        // counts the longest Subarray
        int longest = Integer.MIN_VALUE;
        // counts the subarray
        int count = 0;
  
        // check for all adjacent elements
        for (int i = 0; i < n - 1; i++) {
            int j;
            for (j = 0; j < 10; j++) {
  
                // if adjacent elements have digit j 
                // in them count and break as we have
                // got at-least one digit
                if (hash[i][j] == 1 & hash[i + 1][j] == 1) {
                    count++;
                    break;
                }
            }
            // if no digits are common
            if (j == 10) {
                longest = Math.max(longest, count + 1);
                count = 0;
            }
        }
  
        longest = Math.max(longest, count + 1);
  
        // returns the length of the longest subarray
        return longest;
    }
// Driver Code
  
    public static void main(String[] args) {
        int a[] = {11, 22, 33, 44, 54, 56, 63};
  
        int n = a.length;
        // function call
        System.out.println(longestSubarray(a, n));
  
    }
  
// This code is contributed by 29AjayKumar

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 program to print the length of the
# longest subarray such that adjacent elements
# of the subarray have at least one digit in 
# common.
import sys
  
# function to print the longest subarray
# such that adjacent elements have at least
# one digit in common
def longestSubarray(a, n):
      
    # remembers the occurrence of digits 
    # in i-th index number
    hash = [[0 for i in range(10)]
               for j in range(n)]
  
    # marks the presence of digit in
    # i-th index number
    for i in range(n):
        num = a[i]
        while (num):
              
            # marks the digit
            hash[i][num % 10] = 1
            num = int(num / 10)
      
    # counts the longest Subarray
    longest = -sys.maxsize-1
      
    # counts the subarray
    count = 0
  
    # check for all adjacent elements
    for i in range(n - 1):
        for j in range(10):
              
            # if adjacent elements have digit j 
            # in them count and break as we have
            # got at-least one digit
            if (hash[i][j] and hash[i + 1][j]):
                count += 1
                break
          
        # if no digits are common
        if (j == 10):
            longest = max(longest, count + 1)
            count = 0
      
    longest = max(longest, count + 1)
  
    # returns the length of the longest 
    # subarray
    return longest
  
# Driver Code
if __name__ == '__main__':
    a = [11, 22, 33, 44, 54, 56, 63]
  
    n = len(a)
      
    # function call
    print(longestSubarray(a, n))
      
# This code is contributed by
# Sanjit_Prasad

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

      
// 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;
public class GFG { 
  
// function to print the longest subarray 
// such that adjacent elements have at least 
// one digit in common 
    static int longestSubarray(int []a, int n) { 
        // remembers the occurrence of digits in 
        // i-th index number 
        int [,]hash = new int[n,10]; 
  
        // marks the presence of digit in i-th 
        // index number 
        for (int i = 0; i < n; i++) { 
            int num = a[i]; 
            while (num != 0) { 
                // marks the digit 
                hash[i,num % 10] = 1; 
                num /= 10; 
            
        
  
        // counts the longest Subarray 
        int longest = int.MinValue; 
        // counts the subarray 
        int count = 0; 
  
        // check for all adjacent elements 
        for (int i = 0; i < n - 1; i++) { 
            int j; 
            for (j = 0; j < 10; j++) { 
  
                // if adjacent elements have digit j 
                // in them count and break as we have 
                // got at-least one digit 
                if (hash[i,j] == 1 & hash[i + 1,j] == 1) { 
                    count++; 
                    break
                
            
            // if no digits are common 
            if (j == 10) { 
                longest = Math.Max(longest, count + 1); 
                count = 0; 
            
        
  
        longest = Math.Max(longest, count + 1); 
  
        // returns the length of the longest subarray 
        return longest; 
    
// Driver Code 
  
    public static void Main() { 
        int []a = {11, 22, 33, 44, 54, 56, 63}; 
  
        int n = a.Length; 
        // function call 
        Console.Write(longestSubarray(a, n)); 
  
    
// This code is contributed by Rajput-Ji//

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
// PHP program to print the length of the
// longest subarray such that adjacent 
// elements of the subarray have at least 
// one digit in common.
  
// function to print the longest subarray
// such that adjacent elements have at 
// least one digit in common
function longestSubarray(&$a, $n)
{
    // remembers the occurrence of 
    // digits in i-th index number
    $hash = array_fill(0, $n,
            array_fill(0, 10, NULL));
  
    // marks the presence of digit in 
    // i-th index number
    for ($i = 0; $i < $n; $i++)
    {
        $num = $a[$i];
        while ($num
        {
            // marks the digit
            $hash[$i][$num % 10] = 1;
            $num = intval($num / 10);
        }
    }
  
    // counts the longest Subarray
    $longest = PHP_INT_MIN;
      
    // counts the subarray
    $count = 0;
  
    // check for all adjacent elements
    for ($i = 0; $i < $n - 1; $i++) 
    {
        for ($j = 0; $j < 10; $j++) 
        {
  
            // if adjacent elements have digit j 
            // in them count and break as we have
            // got at-least one digit
            if ($hash[$i][$j] and $hash[$i + 1][$j]) 
            {
                $count++;
                break;
            }
        }
          
        // if no digits are common
        if ($j == 10) 
        {
            $longest = max($longest, $count + 1);
            $count = 0;
        }
    }
  
    $longest = max($longest, $count + 1);
  
    // returns the length of the 
    // longest subarray
    return $longest;
}
  
// Driver Code
$a = array(11, 22, 33, 44, 54, 56, 63 );
  
$n = sizeof($a);
  
// function call
echo longestSubarray($a, $n);
  
// This code is contributed by ChitraNayal
?>

chevron_right



Output:

4

Time Complexity: O(n*10)

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



My Personal Notes arrow_drop_up

The function of education is to teach one to think intensively and to think critically

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.





Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.