Related Articles

# Concatenate the Array of elements into a single element

• Last Updated : 16 Jul, 2021

Given an array, arr[] consisting of N integers, the task is to print the single integer value obtained by joining the array elements.

Examples:

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Input: arr[] = {1, 23, 345}
Output: 12345

Input: arr[] = {123, 45, 6, 78}
Output: 12345678

Approach: The given problem can be solved based on the following observations:

1. Considering X and Y as the two integer values to be joined. And also considering the length of the integer Y as l.
2. Then two integers X and Y can be joined together as following:
•  X×10l +Y

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++

 `// C++ program for the above approach` `#include ``using` `namespace` `std;` `// Function to find the integer value``// obtained by joining array elements``// together``int` `ConcatenateArr(``int` `arr[], ``int` `N)``{``    ``// Stores the resulting integer value``    ``int` `ans = arr;` `    ``// Traverse the array arr[]``    ``for` `(``int` `i = 1; i < N; i++) {` `        ``// Stores the count of digits of``        ``// arr[i]``        ``int` `l = ``floor``(``log10``(arr[i]) + 1);` `        ``// Update ans``        ``ans = ans * ``pow``(10, l);` `        ``// Increment ans by arr[i]``        ``ans += arr[i];``    ``}``    ``// Return the ans``    ``return` `ans;``}` `// Driver Code``int` `main()``{``    ``// Input``    ``int` `arr[] = { 1, 23, 456 };``    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr);` `    ``// Function call``    ``cout << ConcatenateArr(arr, N);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.util.*;` `class` `GFG{``    ` `// Function to find the integer value``// obtained by joining array elements``// together``static` `int` `ConcatenateArr(``int``[] arr, ``int` `N)``{``    ` `    ``// Stores the resulting integer value``    ``int` `ans = arr[``0``];` `    ``// Traverse the array arr[]``    ``for``(``int` `i = ``1``; i < N; i++)``    ``{``        ` `        ``// Stores the count of digits of``        ``// arr[i]``        ``int` `l = (``int``)Math.floor(Math.log10(arr[i]) + ``1``);` `        ``// Update ans``        ``ans = ans * (``int``)Math.pow(``10``, l);` `        ``// Increment ans by arr[i]``        ``ans += arr[i];``    ``}``    ` `    ``// Return the ans``    ``return` `ans;``}` `// Driver Code``public` `static` `void` `main(String args[])``{``    ` `    ``// Input``    ``int` `arr[] = { ``1``, ``23``, ``456` `};``    ``int` `N = arr.length;` `    ``// Function call``    ``System.out.println(ConcatenateArr(arr, N));``}``}` `// This code is contributed by avijitmondal1998`

## Python3

 `# Python3 program for the above approach``import` `math` `# Function to find the integer value``# obtained by joining array elements``# together``def` `ConcatenateArr(arr,  N):` `    ``# Stores the resulting integer value``    ``ans ``=` `arr[``0``]` `    ``# Traverse the array arr[]``    ``for` `i ``in` `range``(``1``,  N):` `        ``# Stores the count of digits of``        ``# arr[i]``        ``l ``=` `math.floor(math.log10(arr[i]) ``+` `1``)` `        ``# Update ans``        ``ans ``=` `ans ``*` `math.``pow``(``10``, l)` `        ``# Increment ans by arr[i]``        ``ans ``+``=` `arr[i]` `    ``# Return the ans``    ``return` `int``( ans)` `# Driver Code``if` `__name__ ``=``=` `"__main__"``:` `    ``# Input``    ``arr ``=` `[``1``, ``23``, ``456``]``    ``N ``=` `len``(arr)` `    ``# Function call``    ``print``(ConcatenateArr(arr, N))` `    ``# This code is contributed by ukasp.`

## C#

 `// C# program for the above approach``using` `System;` `class` `GFG{` `// Function to find the integer value``// obtained by joining array elements``// together``static` `int` `ConcatenateArr(``int``[] arr, ``int` `N)``{``    ` `    ``// Stores the resulting integer value``    ``int` `ans = arr;` `    ``// Traverse the array arr[]``    ``for``(``int` `i = 1; i < N; i++)``    ``{``        ` `        ``// Stores the count of digits of``        ``// arr[i]``        ``int` `l = (``int``)Math.Floor(Math.Log10(arr[i]) + 1);` `        ``// Update ans``        ``ans = ans * (``int``)Math.Pow(10, l);` `        ``// Increment ans by arr[i]``        ``ans += arr[i];``    ``}``    ` `    ``// Return the ans``    ``return` `ans;``}` `// Driver Code``public` `static` `void` `Main()``{``    ``// Input``    ``int``[] arr = { 1, 23, 456 };``    ``int` `N = arr.Length;` `    ``// Function call``    ``Console.Write(ConcatenateArr(arr, N));` `}``}` `// This code is contributed by sanjoy_62.`

## Javascript

 ``
Output
`123456`

Time Complexity: O(N*log(M)), where M is the maximum element of the array.
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up