Skip to main content

Featured

Merge Sort in C++

  Merge Sort in C++ Introduction Merge Sort is a popular sorting algorithm that follows the Divide and Conquer approach. It divides an array into smaller subarrays, recursively sorts those subarrays, and finally merges the sorted subarrays to produce a completely sorted array. In this tutorial, we will learn how to implement Merge Sort in C++ . The program divides the array into two halves using the mid index, recursively sorts both halves, and then combines them using the merge() function. Merge Sort has a time complexity of O(n log n) in the best, average, and worst cases. Table of Contents Algorithm C++ Program Input Sample Output Output Explanation Dry Run Flow of Execution Time Complexity Space Complexity Applications Key Points Interview Questions Frequently Asked Questions Keywords Conclusion Algorithm Start the program. Read the size of the array. Read the array elements from the user. Call the mer...

C++ Program to Separate Even and Odd Numbers from a Vector

 

C++ Program to Separate Even and Odd Numbers from a Vector

Introduction

In this C++ program, we will learn how to separate even and odd numbers from a vector. The program takes the size of the vector as input, stores the elements, and then divides them into two separate arrays—one for even numbers and another for odd numbers. Finally, it displays both groups separately.


C++ Program

#include<bits/stdc++.h>
using namespace std;

int main()
{
    int num;
    cout << "Enter the size of the vector:" << endl;
    cin >> num;

    vector<int> v(num);

    int even[num], odd[num];
    int evencount = 0, oddcount = 0;

    cout << "Enter " << num << " elements in vector:" << endl;

    for(int i = 0; i < v.size(); i++)
    {
        cin >> v[i];

        if(v[i] % 2 == 0)
        {
            even[evencount++] = v[i];
        }
        else
        {
            odd[oddcount++] = v[i];
        }
    }

    cout << "Even numbers are:" << endl;
    for(int i = 0; i < evencount; i++)
    {
        cout << even[i] << " ";
    }

    cout << "\nOdd numbers are:" << endl;
    for(int i = 0; i < oddcount; i++)
    {
        cout << odd[i] << " ";
    }

    return 0;
}

Sample Output

Enter the size of the vector:
6
Enter 6 elements in vector:
10 15 8 21 6 11

Even numbers are:
10 8 6

Odd numbers are:
15 21 11

Explanation

Step 1

Read the size of the vector from the user.

int num;
cin >> num;

Step 2

Create a vector of the given size.

vector<int> v(num);

Step 3

Create two arrays to store even and odd numbers separately.

int even[num], odd[num];

Step 4

Initialize counters to keep track of how many even and odd numbers are stored.

int evencount = 0;
int oddcount = 0;

Step 5

Read each element of the vector and check whether it is even or odd.

if(v[i] % 2 == 0)
{
    even[evencount++] = v[i];
}
else
{
    odd[oddcount++] = v[i];
}
  • If the number is divisible by 2, it is stored in the even array.

  • Otherwise, it is stored in the odd array.


Step 6

Display all even numbers.

for(int i = 0; i < evencount; i++)
{
    cout << even[i] << " ";
}

Step 7

Display all odd numbers.

for(int i = 0; i < oddcount; i++)
{
    cout << odd[i] << " ";
}

Dry Run

Input

Vector = {5, 8, 10, 13, 2}

Processing

NumberEven/OddStored In
5Oddodd[]
8Eveneven[]
10Eveneven[]
13Oddodd[]
2Eveneven[]

Output

Even numbers:
8 10 2

Odd numbers:
5 13

Time Complexity

OperationComplexity
Reading InputO(n)
Separating Even and Odd NumbersO(n)
Printing Even NumbersO(n)
Printing Odd NumbersO(n)

Overall Time Complexity: O(n)


Space Complexity

  • Vector: O(n)

  • Even Array: O(n)

  • Odd Array: O(n)

Overall Auxiliary Space Complexity: O(n)


Key Points

  • Uses a vector to store input elements.

  • Separates even and odd numbers in a single traversal.

  • Maintains the original order of elements.

  • Uses two counters to track the number of even and odd elements.

  • Efficient solution with O(n) time complexity.


Keywords

C++ Vector, Even and Odd Numbers in C++, Separate Even and Odd Numbers, Vector Program in C++, C++ Arrays, STL Vector, C++ Programming Examples, C++ Interview Programs, Data Structures in C++, C++ Coding Practice.


Conclusion

This program demonstrates how to separate even and odd numbers from a vector efficiently using a single loop. It is a simple yet useful example for understanding vectors, arrays, conditional statements, and basic data manipulation in C++. The algorithm runs in O(n) time, making it efficient for processing large collections of numbers.

Comments

Popular Posts

🌙