Your task is to implement  2 stacks in one array efficiently. You need to implement 4 methods.
push1 : pushes element into first stack.
push2 : pushes element into second stack.
pop1 : pops element from first stack and returns the popped element. If first stack is empty, it should return -1.
pop2 : pops element from second stack and returns the popped element. If second stack is empty, it should return -1.
Example 1:
Input:
push1(2)
push1(3)
push2(4)
pop1()
pop2()
pop2()
Output:
3 4 -1
Explanation:
push1(2) the stack1 will be {2}
push1(3) the stack1 will be {2,3}
push2(4) the stack2 will be {4}
pop1()   the poped element will be 3 from stack1 and stack1 will be {2}
pop2()   the poped element will be 4 from stack2 and now stack2 is empty
pop2()   the stack2 is now empty hence returned -1.
Example 2:
Input:
push1(1)
push2(2)
pop1()
push1(3)
pop1()
pop1()
Output:
3 1 -1
Explanation:
push1(1) the stack1 will be {1}
push2(2) the stack2 will be {2}
pop1()   the poped element will be 1 from stack1 and stack1 will be empty
push1(3) the stack1 will be {3}
pop1()   the poped element will be 3 from stack1 and stack1 will be empty
pop1()   the stack1 is now empty hence returned -1.
Your Task:
You don't need to read input or print anything. You are required to complete the 4 methods push1, push2 which takes one argument an integer 'x' to be pushed into stack one and two and pop1, pop2 which returns the integer poped out from stack one and two. If no integer is present in the stack return -1.
Expected Time Complexity: O(1) for all the four methods.
Expected Auxiliary Space: O(1) for all the four methods.
Constraints:
1 <= Number of queries <= 10^{4}
1 <= Number of elements in the stack <= 100
The sum of elements in both the stacks < size of the given array

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Starter code: #User function Template for python3

class twoStacks:
    def __init__(self, n=100):
        self.size = n
        self.arr = [0] * n
        self.top1 = -1
        self.top2 = n

    # Function to push an integer into stack 1
    def push1(self, x):
        pass

    # Function to push an integer into stack 2
    def push2(self, x):
        pass

    # Function to remove an element from top of stack 1
    def pop1(self):
        pass

    # Function to remove an element from top of stack 2
    def pop2(self):
        pass


Solutions:
def push1(a, x):
	a.insert(0, x)

def push2(a, x):
	a.insert(len(a), x)

def pop1(a):
	if a:
		return a.pop(0)
	return -1

def pop2(a):
	if a:
		return a.pop(len(a) - 1)
	return -1


def push1(a, x):
	a.insert(0, x)

def push2(a, x):
	a.append(x)

def pop1(a):
	return a.pop(0)

def pop2(a):
	return a.pop(-1)


def push1(a, x):
	global top1
	global top2
	if top1 + 1 == top2:
		print('stack is full')
		return
	top1 += 1
	a[top1] = x
	return

def push2(a, x):
	global top1
	global top2
	if top1 + 1 == top2:
		print('stack is full')
		return
	top2 -= 1
	a[top2] = x
	return

def pop1(a):
	global top1
	if top1 == -1:
		return -1
	else:
		elem = a[top1]
		top1 -= 1
	return elem

def pop2(a):
	global top2
	if top2 == len(a):
		return -1
	else:
		elem = a[top2]
		top2 += 1
	return elem


top1 = -1
top2 = -1

def push1(a, x):
	global top1
	if top1 < top2 - 1 or (top2 == -1 and top1 < len(a) - 1):
		top1 += 1
		a[top1] = x
	else:
		return 'Stack Overflow'

def push2(a, x):
	global top2
	if top2 == -1:
		top2 = len(a)
	if top1 < top2 - 1:
		top2 -= 1
		a[top2] = x
	else:
		return 'Stack Overflow'

def pop1(a):
	global top1
	if top1 >= 0:
		x = a[top1]
		top1 -= 1
		return x
	else:
		return -1

def pop2(a):
	global top2
	if top2 < len(a):
		x = a[top2]
		top2 += 1
		return x
	else:
		return -1


def push1(a, x):
	if len(a) == 0:
		a.append('#')
		a.insert(0, x)
	else:
		a.insert(0, x)

def push2(a, x):
	if len(a) == 0:
		a.append('#')
		a.append(x)
	else:
		a.append(x)

def pop1(a):
	if len(a) == 0:
		return -1
	i = 0
	while a[i] == '#':
		i += 1
	return a.pop(i)

def pop2(a):
	return a.pop()
top1 = 0
top2 = 0


def push1(a, x):
	global top1, top2
	top1 += 1
	if top1 < top2:
		a[top1] = x

def push2(a, x):
	global top1, top2
	top2 -= 1
	if top1 < top2:
		a[top2] = x

def pop1(a):
	global top1
	if top1 > -1:
		value = a[top1]
		top1 -= 1
		return value
	else:
		return -1

def pop2(a):
	global top2
	if top2 < 101:
		value = a[top2]
		top2 += 1
		return value
	else:
		return -1


def push1(a, x):
	global top1, top2
	top1 += 1
	a[top1] = x

def push2(a, x):
	global top1, top2
	top2 -= 1
	a[top2] = x

def pop1(a):
	global top1, top2
	if top1 == -1:
		return -1
	ans = a[top1]
	top1 -= 1
	return ans

def pop2(a):
	global top1, top2
	if top2 == 101:
		return -1
	ans = a[top2]
	top2 += 1
	return ans


def push1(a, x):
	global top1
	top1 += 1
	a[top1] = x

def push2(a, x):
	global top2
	top2 -= 1
	a[top2] = x

def pop1(a):
	global top1
	if top1 > -1:
		item = a[top1]
		top1 -= 1
		return item
	return -1

def pop2(a):
	global top2
	if top2 < len(a):
		item = a[top2]
		top2 += 1
		return item
	return -1


def push1(a, x):
	if a[0] < 0:
		a[0] = 0
	if a[-1] < 0:
		a[-1] = 0
	count1 = a[0]
	count2 = a[-1] if a[-1] > 0 else 0
	if count1 + count2 + 2 >= len(a):
		return
	a[count1 + 1] = x
	a[0] = count1 + 1

def push2(a, x):
	if a[0] < 0:
		a[0] = 0
	if a[-1] < 0:
		a[-1] = 0
	count1 = a[0] if a[0] > 0 else 0
	count2 = a[-1] if a[-1] > 0 else 0
	if count1 + count2 + 2 >= len(a):
		return
	a[-count2 - 2] = x
	a[-1] = count2 + 1

def pop1(a):
	count = a[0]
	if count <= 0:
		return -1
	else:
		a[0] = count - 1
		return a[count]

def pop2(a):
	count = a[-1]
	if count <= 0:
		return -1
	else:
		a[-1] = count - 1
		return a[-count - 1]


top1 = -1
top2 = 99

def push1(a, x):
	a.insert(0, x)

def push2(a, x):
	a.insert(len(a), x)

def pop1(a):
	if len(a):
		return a.pop(0)
	return -1

def pop2(a):
	if len(a):
		return a.pop(len(a) - 1)
	return -1


def push1(a, x):
	i = 0
	while a[i] != -1:
		i += 1
	a[i] = x

def push2(a, x):
	a.append(x)

def pop1(a):
	i = 0
	while a[i] != -1:
		i += 1
	if i > 0:
		x = a[i - 1]
		a[i - 1] = -1
		return x
	return -1

def pop2(a):
	if a[-1] != -1:
		return a.pop()
	return -1


def __init__(self):
	self.arr = []

def push1(a, x):
	a.insert(0, x)

def push2(a, x):
	a.insert(len(a), x)

def pop1(a):
	l = []
	if len(a) == 0:
		return -1
	return a.pop(0)

def pop2(a):
	l = []
	if len(a) == 0:
		return -1
	return a.pop(-1)


def push1(a, x):
	i = 0
	while i < 100 and a[i] != -1:
		i += 1
	a[i] = x

def push2(a, x):
	i = 99
	while i >= 0 and a[i] != -1:
		i -= 1
	a[i] = x

def pop1(a):
	i = 0
	while i < 100 and a[i] != -1:
		i += 1
	result = a[i - 1]
	a[i - 1] = -1
	return result

def pop2(a):
	i = 99
	while i >= 0 and a[i] != -1:
		i -= 1
	result = a[i + 1]
	a[i + 1] = -1
	return result


def push1(a, x):
	a[:] = [x] + a

def push2(a, x):
	a[:] = a + [x]

def pop1(a):
	return a.pop(0)

def pop2(a):
	return a.pop()


top3 = -1
top4 = 0

def push1(a, x):
	global top3
	if top3 == -1:
		a.insert(0, x)
		top3 = 0
	else:
		a.insert(0, x)
		top3 += 1

def push2(a, x):
	global top4
	if top4 == 0:
		a.append(x)
		top4 = -1
	else:
		a.append(x)
		top4 = top4 - 1

def pop1(a):
	global top3
	if top3 == -1:
		return -1
	popped_val = a.pop(0)
	top3 -= 1
	return popped_val

def pop2(a):
	global top4
	if top4 == 0:
		return -1
	popped_value = a.pop()
	top4 += 1
	return popped_value


def push1(a, x):
	a.append('a')
	a.append(x)

def push2(a, x):
	a.append('b')
	a.append(x)

def pop1(a):
	if len(a) > 0:
		for i in range(len(a) - 1, -1, -1):
			if a[i] == 'a':
				a.pop(i)
				return a[i]
		return -1

def pop2(a):
	if len(a) > 0:
		for i in range(len(a) - 1, -1, -1):
			if a[i] == 'b':
				a.pop(i)
				return a[i]
		return -1


def push1(a, x):
	global top1
	global top2
	top1 += 1
	a[top1] = x

def push2(a, x):
	global top1
	global top2
	top2 -= 1
	a[top2] = x

def pop1(a):
	global top1
	global top2
	if top1 != -1:
		z = a[top1]
		top1 -= 1
		return z
	else:
		return -1

def pop2(a):
	global top1
	global top2
	if top2 != 101:
		r = a[top2]
		top2 += 1
		return r
	else:
		return -1


def push1(a, x):
	global top1
	global top2
	if top1 < top2 - 1:
		top1 += 1
		a[top1] = x

def push2(a, x):
	global top1
	global top2
	if top1 < top2 - 1:
		top2 -= 1
		a[top2] = x

def pop1(a):
	global top1
	if top1 == -1:
		return -1
	else:
		val = a[top1]
		top1 -= 1
		return val

def pop2(a):
	global top2
	if top2 == len(a):
		return -1
	else:
		val = a[top2]
		top2 += 1
		return val