Check if an array element is concatenation of two elements from another array
Last Updated :
31 May, 2021
Given two arrays arr[] and brr[] consisting of N and M positive integers respectively, the task is to find all the elements from the array brr[] which are equal to the concatenation of any two elements from the array arr[]. If no such element exists, then print “-1”.
Examples:
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
Naive Approach: The simplest approach to solve the problem is to generate all possible pairs from the given array and check if the concatenation of pairs of elements from the array arr[] is present in the array brr[] or not. If found to be true, then print the concatenated number formed.
Time Complexity: O(M * N2)
Auxiliary Space: O(N2)
Efficient Approach: The above approach can be optimized by checking for each element in the array brr[], whether brr[i] can be divided into 2 parts left and right such that both the parts exists in the array arr[].
Consider a number, b[i] = 2365
All possible combinations of left and right are:
Left Right
2 365
23 65
236 5
Follow the steps below to solve the problem:
- Initialize a HashMap M and store all elements present in the array arr[].
- Traverse the array brr[] and perform the following steps:
- Generate all possible combinations of left and right parts, such that their concatenation results to brr[i].
- If both the left and the right parts are present in Map M in one of the above combinations, then print the value of brr[i]. Otherwise, continue to the next iteration.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findConcatenatedNumbers(vector< int > a,
vector< int > b)
{
bool ans = true ;
int n1 = a.size();
int n2 = b.size();
unordered_map< int , int > cnt;
for ( int i = 0; i < n1; i++) {
cnt[a[i]] = 1;
}
for ( int i = 0; i < n2; i++) {
int left = b[i];
int right = 0;
int mul = 1;
while (left > 9) {
right += (left % 10) * mul;
left /= 10;
mul *= 10;
if (cnt[left] == 1
&& cnt[right] == 1) {
ans = false ;
cout << b[i] << " " ;
}
}
}
if (ans)
cout << "-1" ;
}
int main()
{
vector< int > a = { 2, 34, 4, 5 };
vector< int > b = { 26, 24, 345, 4, 22 };
findConcatenatedNumbers(a, b);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
static void findConcatenatedNumbers( int [] a,
int [] b)
{
boolean ans = true ;
int n1 = a.length;
int n2 = b.length;
int cnt[] = new int [ 100000 ];
for ( int i = 0 ; i < n1; i++)
{
cnt[a[i]] = 1 ;
}
for ( int i = 0 ; i < n2; i++) {
int left = b[i];
int right = 0 ;
int mul = 1 ;
while (left > 9 ) {
right += (left % 10 ) * mul;
left /= 10 ;
mul *= 10 ;
if (cnt[left] == 1
&& cnt[right] == 1 ) {
ans = false ;
System.out.print(b[i] + " " );
}
}
}
if (ans)
System.out.print( "-1" );
}
public static void main(String[] args)
{
int [] a = { 2 , 34 , 4 , 5 };
int [] b = { 26 , 24 , 345 , 4 , 22 };
findConcatenatedNumbers(a, b);
}
}
|
Python3
from collections import defaultdict
def findConcatenatedNumbers(a, b):
ans = True
n1 = len (a)
n2 = len (b)
cnt = defaultdict( int )
for i in range (n1):
cnt[a[i]] = 1
for i in range (n2):
left = b[i]
right = 0
mul = 1
while (left > 9 ):
right + = (left % 10 ) * mul
left / / = 10
mul * = 10
if (cnt[left] = = 1 and cnt[right] = = 1 ):
ans = False
print (b[i], end = " " )
if (ans):
print ( "-1" )
if __name__ = = "__main__" :
a = [ 2 , 34 , 4 , 5 ]
b = [ 26 , 24 , 345 , 4 , 22 ]
findConcatenatedNumbers(a, b)
|
C#
using System;
class GFG
{
static void findConcatenatedNumbers( int [] a,
int [] b)
{
bool ans = true ;
int n1 = a.Length;
int n2 = b.Length;
int []cnt = new int [100000];
for ( int i = 0; i < n1; i++)
{
cnt[a[i]] = 1;
}
for ( int i = 0; i < n2; i++) {
int left = b[i];
int right = 0;
int mul = 1;
while (left > 9) {
right += (left % 10) * mul;
left /= 10;
mul *= 10;
if (cnt[left] == 1
&& cnt[right] == 1) {
ans = false ;
Console.Write(b[i] + " " );
}
}
}
if (ans)
Console.Write( "-1" );
}
public static void Main(String[] args)
{
int [] a = { 2, 34, 4, 5 };
int [] b = { 26, 24, 345, 4, 22 };
findConcatenatedNumbers(a, b);
}
}
|
Javascript
<script>
function findConcatenatedNumbers(a, b)
{
var ans = true ;
var n1 = a.length;
var n2 = b.length;
var cnt = new Map();
for ( var i = 0; i < n1; i++) {
cnt.set(a[i], 1);
}
for ( var i = 0; i < n2; i++) {
var left = b[i];
var right = 0;
var mul = 1;
while (left > 9) {
right += (left % 10) * mul;
left = parseInt(left/10);
mul *= 10;
if (cnt.has(left)
&& cnt.has(right)) {
ans = false ;
document.write( b[i] + " " );
}
}
}
if (ans)
document.write( "-1" );
}
var a = [2, 34, 4, 5 ];
var b = [26, 24, 345, 4, 22 ];
findConcatenatedNumbers(a, b);
</script>
|
Time Complexity: O(M*log(X)), where X is the largest element in the array brr[].
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...