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

Source: geeksforgeeks | Difficulty: EASY
Tags: Divide and conquer

Mathematics

Starter code: #User function Template for python3

class Solution:
	def find_median(self, v):
		# Code here

Solutions:
class Solution:

	def find_median(self, v):
		n = len(v)
		v.sort()
		if n == 0:
			return -1
		if n == 1:
			return int(v[0])
		if n == 2:
			return int((v[0] + v[1]) / 2)
		if n >= 3:
			if n % 2 == 0:
				p1 = int(n / 2)
				p2 = int(n / 2 - 1)
				return int((v[p1] + v[p2]) / 2)
			else:
				return int(v[int(n / 2)])


class Solution:

	def find_median(self, v):
		v.sort()
		n = len(v)
		if len(v) % 2 == 0:
			res = (v[int(n / 2 - 1)] + v[int(n / 2)]) / 2
			return int(res)
		else:
			return v[n // 2]


class Solution:

	def find_median(self, v):
		v.sort()
		n = len(v)
		if n % 2 == 1:
			median = v[n // 2]
		else:
			median = (v[n // 2 - 1] + v[n // 2]) / 2
		return int(median)


class Solution:

	def find_median(self, v):
		v.sort()
		if len(v) % 2 != 0:
			mid = len(v) // 2
			return v[mid]
		elif len(v) % 2 == 0:
			mid = len(v) // 2
			return (v[mid] + v[mid - 1]) // 2


class Solution:

	def find_median(self, arr):
		if len(arr) == 0:
			return
		import statistics
		return int(statistics.median(arr))


class Solution:

	def find_median(self, v):
		if len(v) == 0:
			return
		v.sort()
		return v[len(v) // 2] if len(v) % 2 == 1 else (v[len(v) // 2] + v[len(v) // 2 - 1]) // 2


class Solution:

	def find_median(self, v):
		temp = sorted(v)
		while len(temp) > 2:
			temp = temp[1:-1]
		if len(temp) == 2:
			return sum(temp) // 2
		return temp[0]


class Solution:

	def find_median(self, v):
		n = v.sort()
		k = len(v) // 2
		if len(v) % 2 != 0:
			return v[k]
		else:
			sum = (v[k - 1] + v[k]) // 2
			return sum


class Solution:

	def find_median(self, v):
		v.sort()
		m = 0
		n = len(v)
		if n % 2 != 0:
			m = v[int((n + 1) / 2) - 1]
		else:
			w = int(n / 2)
			x = int(w + 1)
			m = int((v[w - 1] + v[x - 1]) / 2)
		return m


class Solution:

	def find_median(self, v):
		v.sort()
		l = 0
		r = len(v) - 1
		mid = l + (r - l) // 2
		if len(v) % 2 != 0:
			return v[mid]
		elif len(v) % 2 == 0:
			return int((v[mid] + v[mid + 1]) / 2)


class Solution:

	def find_median(self, v):
		v.sort()
		l = len(v)
		if l % 2 != 0:
			a = l // 2
			return v[a]
		else:
			a = l // 2
			b = l // 2 - 1
			c = int((v[a] + v[b]) / 2)
			return c


class Solution:

	def find_median(self, v):
		arr = list(v)
		arr.sort()
		N = len(arr)
		if N % 2 != 0:
			return arr[N // 2]
		else:
			o = N // 2
			return (arr[o] + arr[o - 1]) // 2


import math

class Solution:

	def find_median(self, v):
		for i in range(1, len(v)):
			j = i - 1
			while v[i] < v[j] and j >= 0:
				j -= 1
			j += 1
			temp = v[i]
			k = i
			while k >= j:
				v[k] = v[k - 1]
				k -= 1
			v[j] = temp
		mid = math.floor(len(v) / 2)
		if len(v) % 2 == 0:
			return math.floor((v[mid - 1] + v[mid]) / 2)
		else:
			return v[mid]


class Solution:

	def find_median(self, v):
		d = sorted(v)
		if len(v) % 2 != 0:
			y = len(v) // 2
			i = d[y]
			return i
		else:
			y = len(v) // 2
			j = (d[y] + d[y - 1]) // 2
			return j


class Solution:

	def find_median(self, v):
		v.sort()
		if len(v) % 2 != 0:
			l = 0
			r = len(v)
			mid = l + r // 2
			return v[mid]
		else:
			l = 0
			r = len(v) // 2
			return (v[r] + v[r - 1]) // 2


class Solution:

	def find_median(self, v):
		sorted_v = sorted(v)
		n = len(sorted_v)
		m = n // 2
		if n % 2 == 0:
			median = (sorted_v[m] + sorted_v[m - 1]) // 2
		else:
			median = sorted_v[m]
		return median


class Solution:

	def find_median(self, v):
		v.sort()
		if len(v) % 2 != 0:
			mid = len(v) / 2 - 0.5
			mid_index = v[int(mid)]
		else:
			mid_ind = v[int(len(v) / 2)]
			permid_ind = v[int(len(v) / 2 - 1)]
			mid_index = int((mid_ind + permid_ind) / 2)
			[3, 4, 5, 6, 7, 8]
			[0, 1, 2, 3, 4, 5]
		return mid_index


class Solution:

	def find_median(self, v):
		l = len(v)
		if l == 1:
			return v[0]
		else:
			median = int(l / 2)
			v.sort()
			if l & 1:
				return v[median]
			else:
				return int((v[median - 1] + v[median]) / 2)


class Solution:

	def find_median(self, v):
		d = sorted(v)
		if len(d) % 2 == 0:
			x = d[len(d) // 2] + d[len(d) // 2 - 1]
			return x // 2
		else:
			return d[len(d) // 2]


class Solution:

	def find_median(self, v):
		l = list(v)
		l.sort()
		n = len(v)
		m = n // 2
		if n % 2 != 0:
			return l[m]
		else:
			return (l[m] + l[m - 1]) // 2


class Solution:

	def find_median(self, v):
		v.sort()
		if len(v) % 2 != 0:
			left = 0
			right = len(v)
			mid = (left + right) // 2
			return v[mid]
		else:
			left = 0
			right = len(v) // 2
			return (v[right] + v[right - 1]) // 2


class Solution:

	def find_median(self, v):
		v.sort()
		length = len(v)
		if length % 2 == 1:
			result = v[int(length / 2)]
		else:
			position = int(length / 2)
			result = (v[position] + v[position - 1]) / 2
		return int(result)


class Solution:

	def find_median(self, v):
		v = sorted(v)
		n = len(v)
		if n % 2 == 0:
			return int((v[int(n / 2)] + v[int(n / 2) - 1]) / 2)
		return v[int(n / 2)]


class Solution:

	def find_median(self, v):
		if len(v) % 2 != 0:
			v.sort()
			median = v[len(v) // 2]
			return median
		elif len(v) == 2:
			a = sum(v)
			return a // len(v)
		else:
			v.sort()
			return (v[len(v) // 2] + v[len(v) // 2 - 1]) // 2


class Solution:

	def find_median(self, v):
		v.sort()
		if n % 2 != 0:
			return int(v[n // 2])
		return int((v[int((n - 1) / 2)] + v[int(n / 2)]) / 2.0)


class Solution:

	def find_median(self, v):
		l = len(v)
		v.sort()
		s = l % 2
		d = l // 2
		if s != 0:
			return v[d]
		else:
			return int((v[d - 1] + v[d]) / 2)


class Solution:

	def find_median(self, v):
		v.sort()
		length = len(v)
		z = length // 2
		if length % 2 == 1:
			return v[z]
		elif length % 2 == 0:
			xy = (v[z - 1] + v[z]) // 2
			return xy


class Solution:

	def find_median(self, v):
		z = sorted(v)
		n = len(z)
		if n % 2 != 0:
			return z[n // 2]
		else:
			return (z[n // 2 - 1] + z[n // 2]) // 2


class Solution:

	def find_median(self, v):
		v.sort()
		a = len(v)
		if len(v) % 2 == 1:
			return v[a // 2]
		else:
			n = a // 2
			return (v[n] + v[n - 1]) // 2


import numpy as np

class Solution:

	def find_median(self, v):
		m = np.median(v)
		return int(m)


class Solution:

	def find_median(self, v):
		z = sorted(v)
		length = len(z)
		middle = length // 2
		if length % 2 == 0:
			return (z[middle - 1] + z[middle]) // 2
		else:
			return z[middle]


class Solution:

	def find_median(self, arr):
		arr.sort()
		if n % 2 == 0:
			a = int(arr[int(n / 2)] + arr[int(n / 2 - 1)]) / 2
			return int(a)
		else:
			return arr[int(n / 2)]


class Solution:

	def find_median(self, v):
		v.sort()
		s = 0
		n = len(v)
		if n % 2 == 0:
			d = n // 2
			s = v[d] + v[d - 1]
			r = s // 2
			return r
		else:
			p = n // 2
			f = v[p]
			return f


class Solution:

	def find_median(self, v):
		a = sorted(v)
		if len(v) % 2 != 0:
			n = len(a) // 2
			return a[n]
		else:
			m = len(a) // 2
			ans = (a[m] + a[m - 1]) // 2
			return ans


class Solution:

	def find_median(self, v):
		arr = sorted(v)
		mid = len(arr) // 2
		if len(arr) % 2 == 0:
			mid = (arr[mid - 1] + arr[mid]) // 2
			return mid
		else:
			return arr[mid]


class Solution:

	def find_median(self, v):
		arr = sorted(v)
		n = len(v)
		return (arr[n // 2] + arr[(n - 1) // 2]) // 2


import statistics

class Solution:

	def find_median(self, v):
		return int(statistics.median(v))


class Solution:

	def find_median(self, v):
		l = len(v)
		v1 = sorted(v)
		if l % 2 == 0:
			median = (v1[l // 2 - 1] + v1[l // 2]) // 2
		else:
			median = v1[l // 2]
		return median


class Solution:

	def find_median(self, v):
		v.sort()
		l = len(v)
		k = l // 2
		n = int(l / 2)
		if l % 2 == 0:
			return int((v[n - 1] + v[n]) / 2)
		else:
			return v[k]


import math

class Solution:

	def find_median(self, v):
		v.sort()
		l = len(v)
		if l % 2 == 0:
			mid = (v[l // 2] + v[l // 2 - 1]) // 2
		else:
			mid = v[l // 2]
		return mid


class Solution:

	def find_median(self, v):
		c = sorted(v)
		N = len(c)
		if N % 2 == 1:
			return c[N // 2]
		else:
			sum = (c[N // 2] + c[N // 2 - 1]) / 2
			return int(sum)


import statistics as sc

class Solution:

	def find_median(self, v):
		a = sc.median(v)
		return int(a)


import math

class Solution:

	def find_median(self, v):
		n = len(v)
		v.sort()
		if n <= 1:
			return v[n - 1]
		return v[n // 2] if n % 2 != 0 else (v[n // 2 - 1] + v[n // 2]) // 2