How to Find the Median of a Sorted Array in C++? Last Updated : 12 Mar, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 Array: 3Finding the Average of a Vector in C++If the array size is odd, the median is the middle element. If the array size is even, the median is the average of the two middle elements. We can simply access the middle element(s) of the array by dividing the size by 2 and using the value we get as array index. C++ Program to Find the Median of a Sorted ArrayThe below example demonstrates how we can find the median of a sorted array in C++. C++ // C++ Program to illustrate how to find the median of a // sorted array #include <iostream> using namespace std; int main() { // Initialize an array int array[] = { 1, 2, 3, 4, 5 }; int n = sizeof(array) / sizeof(array[0]); // Find the median of the array double median; if (n % 2 == 0) { median = (array[n / 2 - 1] + array[n / 2]) / 2.0; } else { median = array[n / 2]; } // Print the median cout << "Median of the Array: " << median << endl; return 0; } OutputMedian of the Array: 3 Time Complexity: O(1)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Find the Median of Array 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 in a Sorted Array in C++? The mode of the given numbers can be defined as the value that occurs the most in the given dataset or the value with the highest frequency. In this article, we will discuss how to calculate the mode of the numbers in a sorted array in C++. Example: Input: myVector = {1, 2, 2, 3, 3, 3, 4, 4, 5} Outp 3 min read How to Find the Median of Array in C++? In C++, the array is a collection of elements of the same type, In this article, we will learn how to find the median of the array in C++. The median of the array will be the middle element if the number of elements is odd or the average of two middle elements if the number of elements is even in th 2 min read How to Find the Median of 2D Array in C++? The median can be defined as the middle element of a sorted array in the case of an odd number of elements in an array and the average of the middle two elements when the number of elements in an array is even. In this article, we will see how to calculate the median of a 2D array in C++. Example: I 3 min read How to Find the Median of Sorted Vector in C++? 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 Ve 2 min read How to Find the Smallest Number in an Array in C++? In C++, arrays are the data types that store the collection of the elements of other data types such as int, float, etc. In this article, we will learn how to find the smallest number in an array using C++. For Example,Input: myVector = {10, 3, 10, 7, 1, 5, 4} Output: Smallest Number = 1Find the Sma 2 min read How to Find the Second Smallest Element in an Array in C++? In C++, arrays are data structures that store the collection of data elements of the same type in contiguous memory locations. In this article, we will learn how to find the second smallest element in an array in C++. Example:Input:myArray = {10, 5, 8, 2, 7, 3, 15};Output:The second smallest element 3 min read Like