Find the Median

Sort by

recency

|

561 Discussions

|

  • + 0 comments

    def findMedian(arr): n=len(arr) arr.sort() return arr[n//2]

  • + 0 comments

    def findMedian(arr): arr = sorted(arr,key=int) median = statistics.median(arr) return median

    python solution function

  • + 0 comments
    def findMedian(arr):
        # Write your code here
        low = 0
        high = len(arr) - 1
        mid = (low + high) // 2
        
        return sorted(arr)[mid]
    
  • + 0 comments

    Here is my c++ solution, you can watch the explanation here https://youtu.be/CbEYUUopR-8

    int findMedian(vector<int> arr) {
        sort(arr.begin(), arr.end());
        return arr[arr.size() / 2];
    }
    
  • + 0 comments

    My Java solution:

    public static int findMedian(List<Integer> arr) {
            //goal: find median from a sorted array
            //sort the arr
            Collections.sort(arr);
            
            //return char at middle idx
            int middleIdx = arr.size() / 2;
            return arr.get(middleIdx);
        }