Concatenate the Array of elements into a single element
Last Updated :
06 Dec, 2023
Given an array, arr[] consisting of N integers, the task is to print the single integer value obtained by joining the array elements.
Examples:
Input: arr[] = {1, 23, 345}
Output: 12345
Input: arr[] = {123, 45, 6, 78}
Output: 12345678
Method 1 :
Approach: The given problem can be solved based on the following observations:
- Considering X and Y as the two integer values to be joined. And also considering the length of the integer Y as l.
- Then two integers X and Y can be joined together as following:
Follow the steps below to solve the problem:
- Initialize a variable, say ans as 0, to store the resulting value.
- Traverse the array arr[] using the variable i, and then in each iteration multiply ans by 10 to the power of the count of the digit in the integer arr[i] and increment ans by arr[i].
- Finally, after the above step, print the answer obtained in ans.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int ConcatenateArr( int arr[], int N)
{
int ans = arr[0];
for ( int i = 1; i < N; i++) {
int l = floor ( log10 (arr[i]) + 1);
ans = ans * pow (10, l);
ans += arr[i];
}
return ans;
}
int main()
{
int arr[] = { 1, 23, 456 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << ConcatenateArr(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int ConcatenateArr( int [] arr, int N)
{
int ans = arr[ 0 ];
for ( int i = 1 ; i < N; i++)
{
int l = ( int )Math.floor(Math.log10(arr[i]) + 1 );
ans = ans * ( int )Math.pow( 10 , l);
ans += arr[i];
}
return ans;
}
public static void main(String args[])
{
int arr[] = { 1 , 23 , 456 };
int N = arr.length;
System.out.println(ConcatenateArr(arr, N));
}
}
|
Python3
import math
def ConcatenateArr(arr, N):
ans = arr[ 0 ]
for i in range ( 1 , N):
l = math.floor(math.log10(arr[i]) + 1 )
ans = ans * math. pow ( 10 , l)
ans + = arr[i]
return int ( ans)
if __name__ = = "__main__" :
arr = [ 1 , 23 , 456 ]
N = len (arr)
print (ConcatenateArr(arr, N))
|
C#
using System;
class GFG{
static int ConcatenateArr( int [] arr, int N)
{
int ans = arr[0];
for ( int i = 1; i < N; i++)
{
int l = ( int )Math.Floor(Math.Log10(arr[i]) + 1);
ans = ans * ( int )Math.Pow(10, l);
ans += arr[i];
}
return ans;
}
public static void Main()
{
int [] arr = { 1, 23, 456 };
int N = arr.Length;
Console.Write(ConcatenateArr(arr, N));
}
}
|
Javascript
<script>
function ConcatenateArr(arr, N)
{
let ans = arr[0];
for (let i = 1; i < N; i++) {
let l = Math.floor(Math.log10(arr[i]) + 1);
ans = ans * Math.pow(10, l);
ans += arr[i];
}
return ans;
}
let arr = [1, 23, 456];
let N = arr.length;
document.write(ConcatenateArr(arr, N));
</script>
|
Time Complexity: O(N*log(M)), where M is the maximum element of the array.
Auxiliary Space: O(1)
Method 2 :
Approach : The given problem can be solved using the string functions.
Follow the below processes :
- Create a resultant string and initialize as an empty string.
- Simply traverse over each element of the array and convert each of them as string using to_string() function .
- Now concatenate each string to the resultant string.
- Then at the end convert the resultant string to integer using the stoi() function.
Refer to the following code for better understanding.
C++
#include <bits/stdc++.h>
using namespace std;
int ConcatenateArr( int arr[], int N)
{
string res = "" ;
for ( int i = 0; i < N; i++) {
string curr = to_string(arr[i]);
res += curr;
}
int ans = stoi(res);
return ans;
}
int main()
{
int arr[] = { 1, 23, 456 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << ConcatenateArr(arr, N);
return 0;
}
|
Java
public class ConcatenateArray {
static int concatenateArr( int [] arr) {
StringBuilder res = new StringBuilder();
for ( int num : arr) {
String curr = Integer.toString(num);
res.append(curr);
}
int ans = Integer.parseInt(res.toString());
return ans;
}
public static void main(String[] args) {
int [] arr = { 1 , 23 , 456 };
System.out.println(concatenateArr(arr));
}
}
|
Python
def concatenate_arr(arr):
res = ""
for num in arr:
curr = str (num)
res + = curr
ans = int (res)
return ans
if __name__ = = "__main__" :
arr = [ 1 , 23 , 456 ]
print (concatenate_arr(arr))
|
C#
using System;
class Program
{
static int ConcatenateArr( int [] arr)
{
string res = "" ;
foreach ( int num in arr)
{
string curr = num.ToString();
res += curr;
}
int ans = int .Parse(res);
return ans;
}
static void Main()
{
int [] arr = { 1, 23, 456 };
Console.WriteLine(ConcatenateArr(arr));
}
}
|
Javascript
function concatenateArray(arr) {
let res = "" ;
for (let i = 0; i < arr.length; i++) {
let curr = arr[i].toString();
res += curr;
}
let ans = parseInt(res);
return ans;
}
function main() {
let arr = [1, 23, 456];
console.log(concatenateArray(arr));
}
main();
|
Time Complexity : O(N*N), N for traversal and N for using the to_string method
Space Complexity : O(k), where k is the length of the total resultant string
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...