How to Find the Median of Sorted Vector in C++? Last Updated : 12 Mar, 2024 Comments Improve Suggest changes Like Article Like Report In C++, vectors are dynamic arrays and the median is a middle value when data is sorted in ascending order. In this article, we will learn how to find the median of all elements in a sorted vector in C++. Example Input: myVector = {10, 20, 30, 40, 50}; Output: Median is 30Finding Median of Sorted Vector in C++To find the median of a sorted vector in C++, first check if the size of the vector is even or odd. If the size is odd, the median is the element at the middle index.If the size is even, the median is the average of the two middle elements.C++ Program to Find the Median of Sorted VectorThe below example demonstrates how we can find the median of elements stored in a sorted vector in C++. C++ // C++ Program to illustrate how to find the median of all // elements in a sorted vector #include <algorithm> #include <iostream> #include <vector> using namespace std; // Function to find the median of a sorted vector int findMedian(vector<int>& vec) { // Find the size of the vector int size = vec.size(); if (size % 2 == 0) { // If the size is even return (vec[size / 2 - 1] + vec[size / 2]) / 2; } else { // If the size is odd return vec[size / 2]; } } int main() { // Intializing two vectors vector<int> vecOdd = { 10, 20, 30, 40, 50 }; vector<int> vecEven = { 10, 20, 30, 40, 50, 60 }; // Find the median of the odd vector int median1 = findMedian(vecOdd); cout << "Median of oddVec: " << median1 << endl; // Find the median of the even vector int median2 = findMedian(vecEven); cout << "Median of evenVec: " << median2 << endl; return 0; } OutputMedian of oddVec: 30 Median of evenVec: 35 Time Complexity: O(1), as the vector is already sortedAuxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Find the Median of Sorted Vector in C++? G gaurav472 Follow Improve Article Tags : C++ Programs C++ cpp-vector CPP Examples Practice Tags : CPP Similar Reads How to Find the Mode of Vector in C++? In C++, a vector is a dynamic array that resizes itself when needed. It also allows random access to its elements. In this article, we will learn how to find the mode of a vector in C++. The mode of a vector is the element that highest number of occurrences in the vector. Example: Input: myVector = 2 min read How to Find the Median of a Sorted Array in C++? In C++, the median of a sorted array is the middle element if the array size is odd, or the average of the two middle elements if the array size is even. In this article, we will learn how to find the median of a sorted array in C++. Example Input: myArray: {1, 2, 3, 4, 5} Output: Median of the Arra 2 min read How to Find the Median of Vector Elements in C++? Median of a dataset is defined as the middle element when N(size of vector) is odd and the average of the middle two elements when N is even in the sorted dataset. In this article, we will learn how to find the median of all the elements of a vector in C++. For Example Input:myVector = {3, 1, 6, 2, 2 min read How to Find the Size of a Vector in C++? The size of a vector means the number of elements currently stored in the vector container. In this article, we will learn how to find the size of vector in C++.The easiest way to find the size of vector is by using vector size() function. Letâs take a look at a simple example:C++#include <bits/s 2 min read How to Find the Size of a Vector in Bytes in C++? In C++, Vectors are dynamic containers that can change their size during the insertion or deletion of elements. In this article, we will explore how we can find the size of the vector in bytes in C++. Example: Input:myVector = {10,20,30,40,50}Output:Size of the vector in bytes is : 20 bytesFind the 2 min read Like