Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C++ Data Types
  • C++ Input/Output
  • C++ Arrays
  • C++ Pointers
  • C++ OOPs
  • C++ STL
  • C++ Interview Questions
  • C++ Programs
  • C++ Cheatsheet
  • C++ MCQ
  • C++ Projects
  • C++ Exception Handling
  • C++ Memory Management
Open In App
Next Article:
How Can I Efficiently Sort a Large Array in C++?
Next article icon

Concatenate Two int Arrays into One Larger Array in C++

Last Updated : 06 Mar, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In C++, an array is a linear data structure we use to store data in contiguous order. It helps to efficiently access any element in O(1) time complexity using its index. In this article, we will learn how to concatenate two integer arrays into one larger array in C++.

Example:

Input: 
arr1: {1, 2, 3, 4}
arr2: {5, ,6, 7, 8}Output:
res: {1, 2, 3, 4, 5, 6, 7, 8}

Concatenate Two int Arrays into One Larger Array in C++

To concatenate two Int arrays, we can simply calculate the size of both arrays and we can make a larger array of sizes equal to the sum of both sizes. After that, we can copy the first array into a larger array and then the second array into a larger array.

Approach

  • Find the size of the first and second array
  • Make an array of size equal to the: size of the first array + the size of the second array
  • Copy elements of the first array into the larger array
  • Copy elements of the second array into the larger array

C++ Program to Concatenate Two int Arrays into One Larger Array

C++
// C++ Program to illustrate how to concatenate two int // arrays into one larger array #include <iostream> using namespace std;  void concatenate(int* arr1, int* arr2, int n, int m) {     int k = n + m;     // Made Array of size equal to the size of first Array +     // Size of second Array     int arr3[k];     // Copy element of first array into the larger array     for (int i = 0; i < n; i++) {         arr3[i] = arr1[i];     }     // Copy element of second array into the larger array     for (int i = 0; i < m; i++) {         arr3[n + i] = arr2[i];     }     // Printing element of larger array     for (int i = 0; i < k; i++) {         cout << arr3[i] << " ";     } } // Driver Code int main() {     int arr1[] = { 1, 2, 3, 4, 5 };     int arr2[] = { 6, 7, 8, 9, 10, 11, 12, 13, 14 };      // Size of first array     int n = sizeof(arr1) / sizeof(arr1[0]);      // Size of second array     int m = sizeof(arr2) / sizeof(arr2[0]);      concatenate(arr1, arr2, n, m);      return 0; } 

Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 

Time Complexity: O(M + M), where N is the size of first array and M is the size of second array
Auxiliary Space: O(N + M)




Next Article
How Can I Efficiently Sort a Large Array in C++?

S

satwiksuman
Improve
Article Tags :
  • C++ Programs
  • C++
  • cpp-array
  • CPP Examples
Practice Tags :
  • CPP

Similar Reads

  • How to Concatenate Two Arrays in C++?
    In C++, arrays store a fixed number of elements of the same type in contiguous memory locations. In this article, we will learn how to concatenate two arrays in C++. Example: Input: myArray1={10, 30, 40} myArray2 ={20, 50, 60}Output: Concatenated Array: 10 30 40 20 50 60 Concatenate Two Arrays in On
    2 min read
  • How to Create Array of Arrays in C++
    Arrays are basic C++ data structures that allow users to store the same data type in memory sequentially. To manage more complicated data structures, you may sometimes need to build an array of arrays, often called a 2D array or a matrix. In this article, we will learn how to create an array of arra
    3 min read
  • C++ Program to check if two Arrays are Equal or not
    Given two arrays arr1[] and arr2[] of length N and M respectively, the task is to check if the two arrays are equal or not. Note: Arrays are said to be equal if and only if both arrays contain the same elements and the frequencies of each element in both arrays are the same. Examples: Input: arr1[]
    4 min read
  • How Can I Efficiently Sort a Large Array in C++?
    In C++, sorting an array means rearranging the elements of an array in a logical order. In this article, we will learn how to efficiently sort a large array in C++. Example: Input: myArray = {5, 2, 3, 1, 4......};Output: Sorted array is : 1 2 3 4 5 ....Sorting a Very Large Array in C++ To efficientl
    2 min read
  • How to Create a Map of Arrays in C++?
    In C++, the std::map is a container that stores elements in a key-value pair, whereas std::array is a sequence container that stores elements in contiguous memory. In this article, we will learn how to create a map of arrays in C++. Example: Input: arr1 = {1, 2, 3};arr2 = {4, 5, 6};arr3 = {7, 8, 9};
    2 min read
  • How to Create a Set of Arrays in C++?
    In C++, the set container represents a collection of unique, sorted elements, and an array is a collection of items stored at contiguous memory locations. In this article, we will learn about how to create a set of arrays in C++. Set of Arrays in C++A set of arrays refers to a collection of arrays w
    2 min read
  • How to Find Largest Number in an Array in C++?
    In C++, arrays are used to store the collection of similar elements to be stored in adjacent memory locations. They can store data of any type such as int, char, float, etc. In this article, we will learn how to find the largest number in an array in C++. For Example,Input: myVector = {1, 3, 10, 7,
    2 min read
  • How to Find Common Elements in Two Arrays in C++?
    In C++, an array is a data structure that is used to store multiple values of similar data types in a contiguous memory location. In this article, we will learn how to find the common elements in two arrays in C++. Examples: Input:Arr1: {1, 2, 3, 4, 5}Arr2: {3, 4, 5, 6, 7}Output:Common Elements: 3 4
    4 min read
  • How to Create a Stack of Arrays in C++?
    In C++, the std::stack is a container that follows the LIFO (Last In, First Out) rule, whereas std::array is a sequence container that stores elements in contiguous memory. In this article, we will learn how to create a stack of an array in C++. Example: Input: arr1 = {1, 2, 3}; arr2 = {4, 5, 6}; ar
    2 min read
  • How to Create a Multimap of Arrays in C++?
    In C++, a multimap is similar to a map with the addition that multiple elements can have the same keys. Also, it is NOT required that the key-value and mapped value pair have to be unique in this case. In this article, we will learn how to create a multimap of arrays in C++ STL. Example Input: myArr
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences