How to Find the Median of 2D Array in C++? Last Updated : 14 Feb, 2024 Comments Improve Suggest changes Like Article Like Report 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: Input: myArray = { {1, 6, 5}, {2, 4, 6}, {7, 22, 9} } Output: Median is: 5Finding Median of 2D Array in C++ To find the median of the 2D array, we first have to convert it to a 1D array by placing row after row. After then we will sort it and find the median in that sorted array. This approach is explained below in detail: Approach Create a 1D array with a size equal to the total number of elements in the 2D array.Traverse the 2D array using nested loops and add each element to flattenedArray.Sort flattenedArray in ascending order.If the total number of elements is even, the median is the average of the two middle numbers.If the total number of elements is odd, the median is the middle number.C++ Program to Find Median of 2D Array C++ // C++ program to find the median of a 2D array #include <algorithm> #include <iostream> using namespace std; // Function to find the median of a 2D array double findMedian(int matrix[3][3], int m, int n) { int mergedArray[m * n]; int count = 0; // Merge elements into a 1D array for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { mergedArray[count++] = matrix[i][j]; } } // Sort the merged array sort(mergedArray, mergedArray + m * n); // Calculate the median if ((m * n) % 2 == 0) { return (mergedArray[(m * n) / 2 - 1] + mergedArray[(m * n) / 2]) / 2.0; } else { return mergedArray[(m * n) / 2]; } } // Main function int main() { // Given 2D array int matrix[3][3] = { { 1, 6, 5 }, { 2, 4, 6 }, { 7, 22, 9 } }; // Find and display the median cout << "Median of the 2D array is " << findMedian(matrix, 3, 3) << endl; return 0; } OutputMedian of the 2D array is 6 Time complexity: O(n*m log(n*m) ) where n is the number of rows and m is the number of columns.Space complexity: O(n*m) Comment More infoAdvertise with us A anushamahajan5 Follow Improve Article Tags : C++ Programs C++ cpp-array CPP Examples Practice Tags : CPP Explore C++ Programming Language 5 min read C++ OverviewIntroduction to C++ Programming Language 3 min read Features of C++ 5 min read History of C++ 7 min read Interesting Facts about C++ 2 min read Setting up C++ Development Environment 8 min read Difference between C and C++ 3 min read C++ BasicsUnderstanding First C++ Program 4 min read C++ Basic Syntax 4 min read C++ Comments 3 min read Tokens in C 4 min read C++ Keywords 2 min read Difference between Keyword and Identifier in C 3 min read C++ Variables and ConstantsC++ Variables 4 min read Constants in C 4 min read Scope of Variables in C++ 7 min read Storage Classes in C++ with Examples 6 min read Static Keyword in C++ 5 min read C++ Data Types and LiteralsC++ Data Types 7 min read Literals in C 4 min read Derived Data Types in C++ 4 min read User Defined Data Types in C++ 4 min read Data Type Ranges and Their Macros in C++ 3 min read C++ Type Modifiers 4 min read Type Conversion in C++ 4 min read Casting Operators in C++ 5 min read C++ OperatorsOperators in C++ 9 min read C++ Arithmetic Operators 4 min read Unary Operators in C 5 min read Bitwise Operators in C 6 min read Assignment Operators in C 4 min read C++ sizeof Operator 3 min read Scope Resolution Operator in C++ 4 min read C++ Input/OutputBasic Input / Output in C++ 5 min read cin in C++ 4 min read cout in C++ 2 min read Standard Error Stream Object - cerr in C++ 2 min read Manipulators in C++ 4 min read C++ Control StatementsDecision Making in C (if , if..else, Nested if, if-else-if ) 7 min read C++ if Statement 3 min read C++ if else Statement 3 min read C++ if else if Ladder 3 min read Switch Statement in C++ 5 min read Jump statements in C++ 4 min read C++ Loops 7 min read for Loop in C++ 6 min read Range-Based for Loop in C++ 3 min read C++ While Loop 3 min read C++ do while Loop 4 min read C++ FunctionsFunctions in C++ 8 min read return Statement in C++ 4 min read Parameter Passing Techniques in C 3 min read Difference Between Call by Value and Call by Reference in C 4 min read Default Arguments in C++ 5 min read Inline Functions in C++ 6 min read Lambda Expression in C++ 4 min read C++ Pointers and ReferencesPointers and References in C++ 5 min read C++ Pointers 8 min read Dangling, Void , Null and Wild Pointers in C 6 min read Applications of Pointers in C 4 min read Understanding nullptr in C++ 3 min read References in C++ 5 min read Can References Refer to Invalid Location in C++? 2 min read Pointers vs References in C++ 5 min read Passing By Pointer vs Passing By Reference in C++ 5 min read When do we pass arguments by pointer? 5 min read Like