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++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find the integer value // obtained by joining array elements // together int ConcatenateArr( int arr[], int N) { // Resultant string string res = "" ; // Traverse the array arr[] for ( int i = 0; i < N; i++) { // Convert the integer to string string curr = to_string(arr[i]); // Concat the curr to res res += curr; } // Convert the res to integer int ans = stoi(res); // Return the ans return ans; } // Driver Code int main() { // Input int arr[] = { 1, 23, 456 }; int N = sizeof (arr) / sizeof (arr[0]); // Function call cout << ConcatenateArr(arr, N); return 0; } |
Java
public class ConcatenateArray { // Function to find the integer value // obtained by joining array elements together static int concatenateArr( int [] arr) { // Resultant string StringBuilder res = new StringBuilder(); // Traverse the array arr[] for ( int num : arr) { // Convert the integer to string String curr = Integer.toString(num); // Concatenate curr to res res.append(curr); } // Convert the res to an integer int ans = Integer.parseInt(res.toString()); // Return the ans return ans; } public static void main(String[] args) { // Input int [] arr = { 1 , 23 , 456 }; // Function call System.out.println(concatenateArr(arr)); } } // This code is contributed by shivamgupta0987654321 |
Python
# Function to find the integer value obtained by joining array elements together def concatenate_arr(arr): # Initialize an empty string to store the result res = "" # Traverse the array arr[] for num in arr: # Convert the integer to a string curr = str (num) # Concatenate curr to res res + = curr # Convert the res to an integer ans = int (res) # Return the ans return ans # Driver Code if __name__ = = "__main__" : # Input arr = [ 1 , 23 , 456 ] # Function call print (concatenate_arr(arr)) |
C#
using System; class Program { // Function to find the integer value obtained by joining array elements together static int ConcatenateArr( int [] arr) { // Resultant string string res = "" ; // Traverse the array arr[] foreach ( int num in arr) { // Convert the integer to string string curr = num.ToString(); // Concatenate curr to res res += curr; } // Convert the res to an integer int ans = int .Parse(res); // Return the ans return ans; } static void Main() { // Input int [] arr = { 1, 23, 456 }; // Function call Console.WriteLine(ConcatenateArr(arr)); } } |
Javascript
// Function to find the integer value // obtained by joining array elements // together function concatenateArray(arr) { // Resultant string let res = "" ; // Traverse the array arr[] for (let i = 0; i < arr.length; i++) { // Convert the integer to string let curr = arr[i].toString(); // Concatenate the curr to res res += curr; } // Convert the res to integer let ans = parseInt(res); // Return the ans return ans; } // Driver Code function main() { // Input let arr = [1, 23, 456]; // Function call console.log(concatenateArray(arr)); } // Execute the main function main(); |
123456
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
Concatenate the Array of elements into a single element
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: 12345Input: arr[] = {123, 45, 6, 78}
Output: 12345678