Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Sorted Array python
import bisect
class SortedArray:
def __init__(self, array):
self.array = sorted(array)
self.d = len(array)
def add(self, x):
bisect.insort(self.array, x)
def remove(self, x):
del self.array[bisect.bisect_left(self.array, x)]
def median(self):
d = self.d
if d % 2 == 1:
return self.array[d//2]
return (self.array[(d-1)//2] + self.array[(d+1)//2])/2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment