给定两个数组arr[]和brr[] 分别由N和M 个正整数组成,任务是从数组brr[] 中找到所有元素,这些元素等于数组arr[]中任意两个元素的串联。如果不存在这样的元素,则打印“-1” 。
例子:
Input: arr[] = {2, 34, 4, 5}, brr[] = {26, 24, 345, 4, 22}
Output: 24 345 22
Explanation:
The elements from the array brr[] which are concatenation of any two elements from the array arr[] are:
- 24 is concatenation of 2 and 4.
- 345 is concatenation of 34 and 5.
- 22 is concatenation of 2 and 2.
Input: arr[] = {1, 2, 3}, brr[] = {1, 23}
Output: 23
朴素的方法:解决问题的最简单的方法是从给定的数组中生成所有可能的对,并检查数组brr[] 中是否存在来自数组 arr[] 的元素对的串联。如果发现为真,则打印形成的连接数。
时间复杂度: O(M * N 2 )
辅助空间: O(N 2 )
高效方法:上述方法可以通过检查在阵列BRR每个元素被优化[],BRR [I]是否可被划分成2个部分的左和右,使得两个部件存在于阵列ARR []。
Consider a number, b[i] = 2365
All possible combinations of left and right are:
Left Right
2 365
23 65
236 5
请按照以下步骤解决问题:
- 初始化一个 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)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live