Find the median | HackerRank -Problem | QuickSort-3

admin
By -
1 minute read
0

 

solution:

using quicksort to  solve this first sort the array  after that  find the median  index value from that sorted array, for this solution you  have to  create and separate quicksort function.

def qsort(arr):
    if len(arr)<=1:
        return arr    
    pivot = arr[0]
    left = [x for x in arr if x < pivot]
    middle = [x for x in arr if x  == pivot]
    right  = [x for x in arr if x > pivot]
    return qsort(left) + middle + qsort(right)
   
def findMedian(arr):
    # Write your code here
    sorted_arr = qsort(arr)
    median = sorted_arr[len(sorted_arr)//2]
    return median

Post a Comment

0Comments

Today | 8, April 2025