📌  相关文章
📜  最长子数组,以便相邻元素具有至少一个公共数字|套装1

📅  最后修改于: 2021-06-01 02:24:12             🧑  作者: Mango

给定一个由N个整数组成的数组,编写一个程序来打印最长子数组的长度,以使子数组的相邻元素具有至少一位相同的数字。

例子:

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

通常的方法是检查所有可能的子阵列。但是时间复杂度将是O(n 2 )。

一种有效的方法是创建一个hash [n] [10]数组,该数组标记第i个索引号中数字的出现。我们对每个元素进行迭代,并检查相邻元素之间是否有相同的数字。如果他们有一个共同的数字,我们保留长度的计数。如果相邻元素没有相同的数字,我们将计数初始化为零,然后再次开始对子数组进行计数。打印在迭代时获得的最大计数。我们使用哈希数组来最大程度地减少时间复杂度,因为该数量可以在10 ^ 18的范围内,在最坏的情况下将需要进行18次迭代。

下面给出了上述方法的说明:

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 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;
}


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


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


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;
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//


PHP


输出:

4

时间复杂度: O(n * 10)

最长子数组,以使相邻元素具有至少一个公共数字|套装– 2

想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”