Skip to main content

Featured

Solve LeetCode 3 Longest Substring Without Repeating Characters in C++ using the sliding window

Longest Substring Without Repeating Characters in C++ LeetCode 3 · sliding window · last index array Table of Contents Problem Statement Approach 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 Conclusion Problem Statement 3. Longest Substring Without Repeating Characters Given a string s , find the length of the longest substring that contains no repeating characters. A substring is a continuous part of the string, so "pwke" is a subsequence of "pwwkew" but not a substring. Approach We use a sliding window . The window is the part of the string between start and end , and we always keep it free of repeated characters. An array lastIndex of size 128 remembers the most recent position of every ASCII ...

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

🌙