Skip to content

Instantly share code, notes, and snippets.

@hamzahamidi
Created April 17, 2020 23:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hamzahamidi/cafc5103694d5f4086395124ada1ce53 to your computer and use it in GitHub Desktop.
Save hamzahamidi/cafc5103694d5f4086395124ada1ce53 to your computer and use it in GitHub Desktop.
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]
else:
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