We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. Prepare
  2. Data Structures
  3. Heap
  4. Find the Running Median
  5. Discussions

Find the Running Median

Problem
Submissions
Leaderboard
Discussions
Editorial

    You are viewing a single comment's thread. Return to all comments →

  • pmtatar
    3 months ago+ 0 comments
    def calculate_median(a):
        mid1 = a[len(a) // 2]
        mid2 = a[(len(a) - 1) // 2]
        return (mid1 + mid2) / 2
    
    def sort_insert(arr, num):
        if not arr:
            arr.append(num)
            return
    
        start = 0
        end = len(arr) -1
        while start < end:
            mid = (end + start) // 2
            if arr[mid] < num:
                start = mid + 1
            elif arr[mid] >= num:
                end = mid - 1
        else:
            if num < arr[start]:
                arr.insert(start, num)
            else:
                arr.insert(start+1, num)
     
    def runningMedian(a):
        arr = []
        result = []
        for i in range(len(a)):
            sort_insert(arr, a[i])
            result.append(calculate_median(arr))
        return result
    
    0|
    Permalink
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy