📌  相关文章
📜  检查一个数组元素是否是另一个数组中两个元素的串联

📅  最后修改于: 2021-04-17 18:52:52             🧑  作者: Mango

给定两个分别由NM个正整数组成的数组arr []brr [] ,任务是从数组brr []中查找所有元素,它们等于数组arr []中任意两个元素的串联。如果不存在这样的元素,则打印“ -1”

例子:

天真的方法:解决问题的最简单方法是从给定数组生成所有可能的对,并检查数组rr []中是否存在数组arr []中的元素对的串联。如果发现为真,则打印形成的级联数字。

时间复杂度: O(M * N 2 )
辅助空间: O(N 2 )

高效方法:上述方法可以通过检查在阵列BRR每个元素被优化[],BRR [I]是否可被划分成2个部分的左,使得两个部件存在于阵列ARR []。

请按照以下步骤解决问题:

  • 初始化HashMap M并存储数组arr []中存在的所有元素。
  • 遍历数组brr []并执行以下步骤:
    • 产生左右两部分,使得它们的级联结果BRR [I]的所有可能的组合。
    • 如果左图右图都以上述组合之一出现在Map M中,则打印brr [i]的值。否则,继续进行下一个迭代。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find elements present in
// the array b[] which are concatenation
// of any pair of elements in the array a[]
void findConcatenatedNumbers(vector a,
                             vector b)
{
    // Stores if there doesn't any such
    // element in the array brr[]
    bool ans = true;
 
    // Stored the size of both the arrays
    int n1 = a.size();
    int n2 = b.size();
 
    // Store the presence of an element
    // of array a[]
    unordered_map cnt;
 
    // Traverse the array a[]
    for (int i = 0; i < n1; i++) {
        cnt[a[i]] = 1;
    }
 
    // Traverse the array b[]
    for (int i = 0; i < n2; i++) {
 
        int left = b[i];
        int right = 0;
        int mul = 1;
 
        // Traverse over all possible
        // concatenations of b[i]
        while (left > 9) {
 
            // Update right and left parts
            right += (left % 10) * mul;
            left /= 10;
            mul *= 10;
 
            // Check if both left and right
            // parts are present in a[]
            if (cnt[left] == 1
                && cnt[right] == 1) {
                ans = false;
                cout << b[i] << " ";
            }
        }
    }
 
    if (ans)
        cout << "-1";
}
 
// Driver Code
int main()
{
    vector a = { 2, 34, 4, 5 };
    vector b = { 26, 24, 345, 4, 22 };
    findConcatenatedNumbers(a, b);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG
{
 
  // Function to find elements present in
  // the array b[] which are concatenation
  // of any pair of elements in the array a[]
  static void findConcatenatedNumbers(int[] a,
                                      int[] b)
  {
    // Stores if there doesn't any such
    // element in the array brr[]
    boolean ans = true;
 
    // Stored the size of both the arrays
    int n1 = a.length;
    int n2 = b.length;
 
    // Store the presence of an element
    // of array a[]
    int cnt[] = new int[100000];
 
    // Traverse the array
    for (int i = 0; i < n1; i++)
    {
      cnt[a[i]] = 1;
    }
 
    // Traverse the array b[]
    for (int i = 0; i < n2; i++) {
 
      int left = b[i];
      int right = 0;
      int mul = 1;
 
      // Traverse over all possible
      // concatenations of b[i]
      while (left > 9) {
 
        // Update right and left parts
        right += (left % 10) * mul;
        left /= 10;
        mul *= 10;
 
        // Check if both left and right
        // parts are present in a[]
        if (cnt[left] == 1
            && cnt[right] == 1) {
          ans = false;
          System.out.print(b[i] + " ");
        }
      }
    }
 
    if (ans)
      System.out.print("-1");
  }
 
 
  // Driver code
  public static void main(String[] args)
  {
    int[] a = { 2, 34, 4, 5 };
    int[] b = { 26, 24, 345, 4, 22 };
    findConcatenatedNumbers(a, b);
  }
}
 
// This code is contributed by sanjoy_62.


Python3
# Python3 program for the above approach
from collections import defaultdict
 
# Function to find elements present in
# the array b[] which are concatenation
# of any pair of elements in the array a[]
def findConcatenatedNumbers(a, b):
     
    # Stores if there doesn't any such
    # element in the array brr[]
    ans = True
 
    # Stored the size of both the arrays
    n1 = len(a)
    n2 = len(b)
 
    # Store the presence of an element
    # of array a[]
    cnt = defaultdict(int)
 
    # Traverse the array a[]
    for i in range(n1):
        cnt[a[i]] = 1
 
    # Traverse the array b[]
    for i in range(n2):
        left = b[i]
        right = 0
        mul = 1
 
        # Traverse over all possible
        # concatenations of b[i]
        while (left > 9):
 
            # Update right and left parts
            right += (left % 10) * mul
            left //= 10
            mul *= 10
 
            # Check if both left and right
            # parts are present in a[]
            if (cnt[left] == 1 and cnt[right] == 1):
                ans = False
                print(b[i], end = " ")
 
    if (ans):
        print("-1")
 
# Driver Code
if __name__ == "__main__":
 
    a = [ 2, 34, 4, 5 ]
    b = [ 26, 24, 345, 4, 22 ]
     
    findConcatenatedNumbers(a, b)
 
# This code is contributed by chitranayal


C#
// C# program for the above approach
using System;
class GFG
{
 
  // Function to find elements present in
  // the array b[] which are concatenation
  // of any pair of elements in the array a[]
  static void findConcatenatedNumbers(int[] a,
                                      int[] b)
  {
     
    // Stores if there doesn't any such
    // element in the array brr[]
    bool ans = true;
 
    // Stored the size of both the arrays
    int n1 = a.Length;
    int n2 = b.Length;
 
    // Store the presence of an element
    // of array a[]
    int []cnt = new int[100000];
 
    // Traverse the array
    for (int i = 0; i < n1; i++)
    {
      cnt[a[i]] = 1;
    }
 
    // Traverse the array b[]
    for (int i = 0; i < n2; i++) {
 
      int left = b[i];
      int right = 0;
      int mul = 1;
 
      // Traverse over all possible
      // concatenations of b[i]
      while (left > 9) {
 
        // Update right and left parts
        right += (left % 10) * mul;
        left /= 10;
        mul *= 10;
 
        // Check if both left and right
        // parts are present in a[]
        if (cnt[left] == 1
            && cnt[right] == 1) {
          ans = false;
          Console.Write(b[i] + " ");
        }
      }
    }
 
    if (ans)
      Console.Write("-1");
  }
 
  // Driver code
  public static void Main(String[] args)
  {
    int[] a = { 2, 34, 4, 5 };
    int[] b = { 26, 24, 345, 4, 22 };
    findConcatenatedNumbers(a, b);
  }
}
 
// This code is contributed by shivani


输出:
24 345 22

时间复杂度: O(M * log(X)),其中Xbrr []数组中最大元素
辅助空间: O(N)