Concatenate the Array of elements into a single element - GeeksforGeeks

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:  
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: 
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: X×10l +Y
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: X×10l +Y
 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++
Java
Python3
C#
Javascript
// C++ program for the above approach #include <bits/stdc++.h>using namespace std; // Function to find the integer value// obtained by joining array elements// togetherint 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 = 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 Codeint main(){    // Input    int arr[] = { 1, 23, 456 };    int N = sizeof(arr) / sizeof(arr[0]);     // Function call    cout << ConcatenateArr(arr, N);     return 0;}
// Java program for the above approachimport java.util.*; class GFG{     // Function to find the integer value// obtained by joining array elements// togetherstatic 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 Codepublic 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 program for the above approachimport math # Function to find the integer value# obtained by joining array elements# togetherdef 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 Codeif __name__ == "__main__":     # Input    arr = [1, 23, 456]    N = len(arr)     # Function call    print(ConcatenateArr(arr, N))     # This code is contributed by ukasp.
// C# program for the above approachusing System; class GFG{ // Function to find the integer value// obtained by joining array elements// togetherstatic 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 Codepublic 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.
<script>       // JavaScript program for the above approach        // Function to find the integer value       // obtained by joining array elements       // together       function ConcatenateArr(arr, N)       {                   // Stores the resulting integer value           let ans = arr[0];            // Traverse the array arr[]           for (let i = 1; i < N; i++) {                // Stores the count of digits of               // arr[i]               let 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 ans;       }        // Driver Code        // Input       let arr = [1, 23, 456];       let N = arr.length;        // Function call       document.write(ConcatenateArr(arr, N));    // This code is contributed by Potta Lokesh    </script>
123456
Time Complexity: O(N*log(M)), where M is the maximum element of the array.Auxiliary Space: O(1)
 
lokeshpotta20
ukasp
avijitmondal1998
sanjoy_62
Arrays
Mathematical
Arrays
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Count pairs with given sum
Chocolate Distribution Problem
Window Sliding Technique
Reversal algorithm for array rotation
Next Greater Element
Program for Fibonacci numbers
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7