Given an array arr[] of N elements, the task is to concatenate it twice, i.e. create an array of size 2*N by appending the copy of the given array to itself.
Example:
Input: arr[] = {1, 2, 1}
Output: 1 2 1 1 2 1
Explanation: The given array arr[] = {1, 2, 1}, can be appended to itself resulting in arr[] = {1, 2, 1, 1, 2, 1}.Input: arr[] = {1, 3, 2, 1}
Output: 1 3 2 1 1 3 2 1
Approach: The given problem is an implementation-based problem. It can be solved by creating a array newArr[] of size 2*N. Iterate the given array arr[] using a variable i in the range [0, N) and append the assign newArr[i] = arr[i] and newArr[i + N] = arr[i].
Below is the implementation of the above approach:
C++
// C++ program of the above approach #include <bits/stdc++.h> using namespace std; // Function to concatenate the // given array arr[] twice void concatTwice( int * arr, int N) { // Stores array after // concatenation int newArr[2 * N]; // Loop to iterate arr[] for ( int i = 0; i < N; i++) { newArr[i] = arr[i]; newArr[i + N] = arr[i]; } // Print Answer for ( int i = 0; i < 2 * N; i++) { cout << newArr[i] << " " ; } } // Driver Code int main() { int arr[] = { 1, 2, 3 }; int N = sizeof (arr) / sizeof (arr[0]); concatTwice(arr, N); return 0; } |
Java
// Java program for the above approach import java.io.*; import java.lang.*; import java.util.*; class GFG { // Function to concatenate the // given array arr[] twice static void concatTwice( int arr[], int N) { // Stores array after // concatenation int newArr[] = new int [ 2 * N]; // Loop to iterate arr[] for ( int i = 0 ; i < N; i++) { newArr[i] = arr[i]; newArr[i + N] = arr[i]; } // Print Answer for ( int i = 0 ; i < 2 * N; i++) { System.out.print(newArr[i] + " " ); } } // Driver Code public static void main (String[] args) { int arr[] = { 1 , 2 , 3 }; int N = arr.length; concatTwice(arr, N); } } // This code is contributed by hrithikgarg03188 |
Python3
# Python program of the above approach # Function to concatenate the # given array arr[] twice def concatTwice(arr, N): # Stores array after # concatenation newArr = [ 0 ] * ( 2 * N) # Loop to iterate arr[] for i in range (N): newArr[i] = arr[i] newArr[i + N] = arr[i] # Print Answer for i in range ( 0 , 2 * N): print (newArr[i], end = " " ) # Driver Code arr = [ 1 , 2 , 3 ] N = len (arr) concatTwice(arr, N) # This code is contributed by gfgking |
C#
// C# program for the above approach using System; class GFG { // Function to concatenate the // given array arr[] twice static void concatTwice( int []arr, int N) { // Stores array after // concatenation int []newArr = new int [2 * N]; // Loop to iterate arr[] for ( int i = 0; i < N; i++) { newArr[i] = arr[i]; newArr[i + N] = arr[i]; } // Print Answer for ( int i = 0; i < 2 * N; i++) { Console.Write(newArr[i] + " " ); } } // Driver Code public static void Main () { int []arr = { 1, 2, 3 }; int N = arr.Length; concatTwice(arr, N); } } // This code is contributed by Samim Hossain Mondal |
Javascript
<script> // JavaScript program of the above approach // Function to concatenate the // given array arr[] twice const concatTwice = (arr, N) => { // Stores array after // concatenation let newArr = new Array(2 * N).fill(0); // Loop to iterate arr[] for (let i = 0; i < N; i++) { newArr[i] = arr[i]; newArr[i + N] = arr[i]; } // Print Answer for (let i = 0; i < 2 * N; i++) { document.write(`${newArr[i]} `); } } // Driver Code let arr = [1, 2, 3]; let N = arr.length; concatTwice(arr, N); // This code is contributed by rakeshsahni </script> |
1 2 3 1 2 3
Time Complexity: O(N)
Auxiliary Space: O(N)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!