Find the median | Practice | GeeksforGeeks

Given an array arr[] of N integers, calculate the median
 
Example 1:
Input: N = 5
arr[] = 90 100 78 89 67
Output: 89
Explanation: After sorting the array 
middle element is the median 


Example 2:
Input: N = 4
arr[] = 56 67 30 79
Output: 61
Explanation: In case of even number of 
elements, average of two middle elements 
is the median.


 
Your Task:
You don't need to read or print anything. Your task is to complete the function find_median() which takes the array as input parameter and returns the floor value of the median.
 
Expected Time Complexity: O(n * log(n))
Expected Space Complexity: O(1)
 
Constraints:
1 <= Length of Array <= 100
1 <= Elements of Array <= 100
0
goelradhika2000in 10 hours
public int find_median(int[] v)   {       // Code here       Arrays.sort(v);       int  x ;       if(v.length % 2 !=0){           return v[v.length / 2];       }       else        {           int m = v[v.length /2 ] + v[v.length / 2 -1];           m/= 2;            x = m ;       }       return x ;   }
0
sheetalmehta1374 hours ago
public: int find_median(vector<int> v) {     int n = v.size();     int median;   //Sorting using function   sort(v.begin(), v.end());     if(n%2==0){         median = (v[n/2] +v[n/2 -1])/2;         return (int)median;     }else{         return v[n/2];     } }};
0
prakashanalyst1 week ago
C++ Solution with 0.1 / 1.23
int find_median(vector<int> v)
		{
		    sort(v.begin(), v.end());
		    int noElements = v.size();
		    if(noElements % 2 == 0) {
		        int middleElements = v[noElements / 2] + v[(noElements / 2) - 1];
		        return (int) middleElements / 2;
		    } 
		    else {
		        return (int) v[noElements / 2];
		    }
		}
0
rohitsinha9692 weeks ago
JAVA Solution using Insertion Sort
       // Insertion Sort :-       int N = v.length;        for (int i = 0; i < v.length; i++) {           int current = v[i];           int j = i - 1;           while (j >= 0 && current < v[j]) {               v[j + 1] = v[j];               j--;           }           v[j + 1] = current;       }              // median fetching       if (v.length % 2 == 0){           //int avg = ((v.length/2) + ((v.length/2)-1)/2);  // first we find the average of local travesed values           int median_Index_based = Math.floorDiv(v.length, 2);           int median = v[median_Index_based] + v[median_Index_based-1];           return Math.floorDiv(median,2);       }       else {           int median = Math.floorDiv(v.length, 2);           return v[median];       }
0
mayank180919993 weeks ago
		int find_median(vector<int> v)
		{
		    // Code here.
		    int n=v.size();
		    sort(v.begin(),v.end());
		    if(n%2==0){
		        return (v[n/2]+v[n/2-1])/2;
		    }else{
		        return v[n/2];
		    }
		}
0
rsbly7300954 weeks ago
javascript solution
class Solution {
   find_median(arr){       arr.sort((a,b)=>a-b);       let len = arr.length;       let n = Math.trunc(len/2);       if (len % 2 === 0) return Math.trunc((arr[n] + arr[n-1]) / 2);       else return arr[n];   }}
0
rsbly7300954 weeks ago
class Solution {
   find_median(arr){       arr.sort((a,b)=>a-b);       let len = arr.length;       let n = Math.trunc(len/2);       if (len % 2 === 0) return Math.trunc((arr[n] + arr[n-1]) / 2);       else return arr[n];   }}
0
badgujarsachin831 month ago
	int find_median(vector<int> v)
		{
		    // Code here.
		    int n=v.size();
		    sort(v.begin(),v.end());
		    if(v.size()%2!=0){
		        return v[v.size()/2];
		    }
	       	else{
		    int avg=(v[(n/2)-1]+v[n/2])/2;
		    return avg;
	       	}
		}
0
rabab0771 month ago
Python Solution:
class Solution:def find_median(self, v): # Code here v.sort() low = min(v) high = max(v) if (len(v)%2==0):     n = (len(v)//2)     x = (len(v)//2)+1     return int((v[n-1]+v[x-1])//2)      elif(len(v)%2!=0):     m = (len(v)+1)//2     return v[m-1]
0
writetoafzhalahmeds2 months ago
//C++

class Solution
{
public:
	public:
		int find_median(vector<int> v)
		{
		    int size= v.size();
		    //cout<<size<<endl;
		    sort(v.begin(), v.end());
		    if(size%2!=0)
		    {
		        return v[((size/2))];
		    }
		    else
		    {
		    int avg= (v[(size/2)-1]+v[((size/2))])/2;
		    return avg;
		    }
		}
};
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.