Arrays January 01 ,2026

Copy One Array into Another

Problem Statement

Given an array of elements, the task is to copy all elements from one array into another array and display the copied array.

This problem helps in understanding:

  • Array traversal
  • Memory allocation
  • Index-wise assignment
  • Difference between reference copy and element-wise copy (conceptually)

Why This Problem Is Important

Copying arrays is a fundamental operation used in:

  • Data transformation
  • Creating backups of data
  • Preventing modification of original data
  • Intermediate processing in algorithms

Almost every real-world program uses array copying at some level.

Input and Output Format

Input

Original Array: [5, 10, 15, 20, 25]

Output

Copied Array: [5, 10, 15, 20, 25]

Key Concept

  • Elements are copied one by one
  • Both arrays have the same size
  • Each index i in destination array receives value from source array at index i

Step-by-Step Algorithm

  1. Create the source array
  2. Create a destination array of the same size
  3. Traverse the source array from index 0 to n-1
  4. Assign dest[i] = source[i]
  5. Print the destination array

Pseudocode

for i from 0 to n-1:
    dest[i] = source[i]

Dry Run Example

Source: [5, 10, 15]
Destination (initial): [_, _, _]

i = 0 → dest[0] = 5
i = 1 → dest[1] = 10
i = 2 → dest[2] = 15

Destination: [5, 10, 15]

Time and Space Complexity

MetricValue
Time ComplexityO(n)
Space ComplexityO(n)
Copying an array and changing the size of an array

Language-wise Implementation

C Implementation

#include 

int main() {
    int source[] = {5, 10, 15, 20, 25};
    int n = sizeof(source) / sizeof(source[0]);
    int dest[n];

    for(int i = 0; i < n; i++) {
        dest[i] = source[i];
    }

    printf("Copied Array: ");
    for(int i = 0; i < n; i++) {
        printf("%d ", dest[i]);
    }

    return 0;
}

Output

Copied Array: 5 10 15 20 25

C++ Implementation

#include 
using namespace std;

int main() {
    int source[] = {5, 10, 15, 20, 25};
    int n = sizeof(source) / sizeof(source[0]);
    int dest[n];

    for(int i = 0; i < n; i++) {
        dest[i] = source[i];
    }

    cout << "Copied Array: ";
    for(int i = 0; i < n; i++) {
        cout << dest[i] << " ";
    }

    return 0;
}

Output

Copied Array: 5 10 15 20 25

Java Implementation

public class Main {
    public static void main(String[] args) {
        int[] source = {5, 10, 15, 20, 25};
        int[] dest = new int[source.length];

        for(int i = 0; i < source.length; i++) {
            dest[i] = source[i];
        }

        System.out.print("Copied Array: ");
        for(int value : dest) {
            System.out.print(value + " ");
        }
    }
}

Output

Copied Array: 5 10 15 20 25

Python Implementation

source = [5, 10, 15, 20, 25]
dest = [0] * len(source)

for i in range(len(source)):
    dest[i] = source[i]

print("Copied Array:", dest)

Output

Copied Array: [5, 10, 15, 20, 25]

C# Implementation

using System;

class Program {
    static void Main() {
        int[] source = {5, 10, 15, 20, 25};
        int[] dest = new int[source.Length];

        for(int i = 0; i < source.Length; i++) {
            dest[i] = source[i];
        }

        Console.Write("Copied Array: ");
        foreach(int value in dest) {
            Console.Write(value + " ");
        }
    }
}

Output

Copied Array: 5 10 15 20 25

JavaScript Implementation

let source = [5, 10, 15, 20, 25];
let dest = [];

for (let i = 0; i < source.length; i++) {
    dest[i] = source[i];
}

console.log("Copied Array:", dest);

Output

Copied Array: [5, 10, 15, 20, 25]

Important Notes

  • This is a deep copy (element-wise), not a reference copy
  • Changes in destination array will NOT affect source array
  • Always ensure destination array has sufficient size

Common Mistakes

  • Forgetting to initialize destination array size
  • Using wrong loop condition
  • Printing source instead of destination
  • Confusing shallow copy with deep copy

Interview Perspective

Interviewers may ask:

  • Difference between reference copy and element-wise copy
  • How to copy arrays using built-in methods
  • Copy only even/odd elements
  • Reverse copy

Summary

Copying one array into another strengthens understanding of array traversal and memory handling. It is a basic yet critical building block for solving advanced array problems and real-world applications.

Next Problem in the Series

Reverse an Array

Sanjiv
0

You must logged in to post comments.

Related Blogs

Find the S...
Arrays February 02 ,2026

Find the Second Smal...

Find the S...
Arrays January 01 ,2026

Find the Sum of All...

Find the M...
Arrays January 01 ,2026

Find the Maximum Ele...

Find the M...
Arrays January 01 ,2026

Find the Minimum Ele...

Count Even...
Arrays January 01 ,2026

Count Even and Odd N...

Search an...
Arrays January 01 ,2026

Search an Element in...

Reverse an...
Arrays January 01 ,2026

Reverse an Array

Print Alte...
Arrays January 01 ,2026

Print Alternate Elem...

Find the L...
Arrays January 01 ,2026

Find the Length of a...

Check if a...
Arrays January 01 ,2026

Check if an Array is...

Find the F...
Arrays January 01 ,2026

Find the First Eleme...

Find the L...
Arrays January 01 ,2026

Find the Last Elemen...

Count the...
Arrays January 01 ,2026

Count the Number of...

Replace Al...
Arrays January 01 ,2026

Replace All Elements...

Sum of Ele...
Arrays January 01 ,2026

Sum of Elements at E...

Sum of Ele...
Arrays January 01 ,2026

Sum of Elements at O...

Find the A...
Arrays January 01 ,2026

Find the Average of...

Count the...
Arrays January 01 ,2026

Count the Number of...

Remove Dup...
Arrays January 01 ,2026

Remove Duplicate Ele...

Move All Z...
Arrays January 01 ,2026

Move All Zeros to th...

Rotate an...
Arrays January 01 ,2026

Rotate an Array by K...

Rotate an...
Arrays January 01 ,2026

Rotate an Array by O...

Check if T...
Arrays January 01 ,2026

Check if Two Arrays...

Merge Two...
Arrays January 01 ,2026

Merge Two Sorted Arr...

Find Missi...
Arrays January 01 ,2026

Find Missing Number...

Find Dupli...
Arrays January 01 ,2026

Find Duplicate Eleme...

Count Freq...
Arrays January 01 ,2026

Count Frequency of E...

Find the M...
Arrays January 01 ,2026

Find the Majority El...

Find All U...
Arrays January 01 ,2026

Find All Unique Elem...

Insert an...
Arrays January 01 ,2026

Insert an Element at...

Delete an...
Arrays January 01 ,2026

Delete an Element fr...

Find the I...
Arrays January 01 ,2026

Find the Index of an...

Find Union...
Arrays January 01 ,2026

Find Union of Two Ar...

Find Inter...
Arrays January 01 ,2026

Find Intersection of...

Sort an Ar...
Arrays January 01 ,2026

Sort an Array of 0s...

Find the L...
Arrays January 01 ,2026

Find the Largest Sum...

Kadane’s A...
Arrays January 01 ,2026

Kadane’s Algorithm (...

Two Sum Pr...
Arrays January 01 ,2026

Two Sum Problem

Subarray w...
Arrays January 01 ,2026

Subarray with Given...

Longest Su...
Arrays January 01 ,2026

Longest Subarray wit...

Rearrange...
Arrays January 01 ,2026

Rearrange Array Alte...

Leaders in...
Arrays January 01 ,2026

Leaders in an Array

Equilibriu...
Arrays January 01 ,2026

Equilibrium Index of...

Stock Buy...
Arrays January 01 ,2026

Stock Buy and Sell (...

Stock Buy...
Arrays January 01 ,2026

Stock Buy and Sell (...

Sort an Ar...
Arrays January 01 ,2026

Sort an Array of 0s,...

Find the M...
Arrays January 01 ,2026

Find the Majority El...

Find All P...
Arrays January 01 ,2026

Find All Pairs with...

Longest Co...
Arrays January 01 ,2026

Longest Consecutive...

Product of...
Arrays January 01 ,2026

Product of Array Exc...

Maximum Pr...
Arrays January 01 ,2026

Maximum Product Suba...

Find the F...
Arrays January 01 ,2026

Find the First Missi...

Count Inve...
Arrays January 01 ,2026

Count Inversions in...

Rearrange...
Arrays January 01 ,2026

Rearrange Array by S...

Check if A...
Arrays January 01 ,2026

Check if Array Can B...

Trapping R...
Arrays January 01 ,2026

Trapping Rain Water

Find Minim...
Arrays January 01 ,2026

Find Minimum in Rota...

Search in...
Arrays January 01 ,2026

Search in Rotated So...

Median of...
Arrays January 01 ,2026

Median of Two Sorted...

Merge Inte...
Arrays January 01 ,2026

Merge Intervals

Count Reve...
Arrays January 01 ,2026

Count Reverse Pairs

Longest Su...
Arrays January 01 ,2026

Longest Subarray wit...

Largest Re...
Arrays January 01 ,2026

Largest Rectangle in...

Maximum Su...
Arrays January 01 ,2026

Maximum Sum Rectangl...

Subarray S...
Arrays January 01 ,2026

Subarray Sum Equals...

Count Dist...
Arrays January 01 ,2026

Count Distinct Eleme...

Sliding Wi...
Arrays January 01 ,2026

Sliding Window Maxim...

Find K Max...
Arrays January 01 ,2026

Find K Maximum Eleme...

Minimum Nu...
Arrays January 01 ,2026

Minimum Number of Ju...

Chocolate...
Arrays January 01 ,2026

Chocolate Distributi...

Find All T...
Arrays January 01 ,2026

Find All Triplets Wi...

Kth Smalle...
Arrays January 01 ,2026

Kth Smallest Element...

Maximum Le...
Arrays January 01 ,2026

Maximum Length Biton...

Find the S...
Arrays February 02 ,2026

Find the Second Larg...

Get In Touch

Kurki bazar Uttar Pradesh

+91-8808946970

techiefreak87@gmail.com