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

📅  最后修改于: 2021-10-26 06:28:43             🧑  作者: Mango

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

例子:

朴素的方法:解决问题的最简单的方法是从给定的数组中生成所有可能的对,并检查数组brr[] 中是否存在来自数组 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


Javascript


输出:
24 345 22

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程