Resize Array

Medium Dynamic Memory Allocation
Solve in Playground →

Problem Statement

Write a C program to resize a dynamically allocated array using `realloc`, adding new elements and printing the updated array.

Input Format

First line: initial size n1. Second line: n1 elements. Third line: new size n2. Fourth line: n2 - n1 new elements.

Output Format

Print the resized array elements separated by spaces.

Constraints

1 <= n1 < n2 <= 100

Sample Input

3
1 2 3
5
4 5

Sample Output

1 2 3 4 5

Explanation

Use `realloc` to resize the existing heap memory block, read additional elements into the newly allocated space, and print the entire array.

Starter Code

#include <stdio.h>
#include <stdlib.h>

int main() {
    int n1;
    if (scanf("%d", &n1) == 1) {
        int *arr = (int *)malloc(n1 * sizeof(int));
        for (int i = 0; i < n1; i++) {
            scanf("%d", &arr[i]);
        }
        int n2;
        scanf("%d", &n2);
        // Write your code here to resize array using realloc and read new elements
    }
    return 0;
}

Limits

  • Time Limit: 1s
  • Memory Limit: 256MB

Embedded C Programming

Updated: March 15, 2026
Intermediate

Embedded systems rely on efficient low-level programming to interact directly with hardware. In this course, you will learn how to write practical Embedded C programs used in real microcontroller-based systems. Rather than focusing only on theory, this course follows a practice-driven approach. Each lesson includes hands-on coding exercises that simulate real firmware development tasks used