# Algorithmic Toolbox | Week 4

Course Name: Algorithmic Toolbox

#### Programming Assignment 4: Divide and Conquer

4-1: Binary Search

``````def binary_search(keys, query):
left = 0
right = len(keys) - 1
while right >= left:
middle = (left + right) // 2
if keys[middle] == query:
return middle
elif keys[middle] < query:
left = middle + 1
else:
right = middle - 1
return -1

if __name__ == '__main__':
num_keys = int(input())
input_keys = list(map(int, input().split()))
assert len(input_keys) == num_keys

num_queries = int(input())
input_queries = list(map(int, input().split()))
assert len(input_queries) == num_queries

results = []
for q in input_queries:
results.append(binary_search(input_keys, q))

print(' '.join(map(str, results)))``````

These are Algorithmic Toolbox Week 4 Programming Assignment Coursera Answers

4-2: Binary Search with Duplicates

These are Algorithmic Toolbox Week 4 Programming Assignment Coursera Answers

4-3: Majority Element

These are Algorithmic Toolbox Week 4 Programming Assignment Coursera Answers

4-4: Improving QuickSort

These are Algorithmic Toolbox Week 4 Programming Assignment Coursera Answers

4-5: Inversions

These are Algorithmic Toolbox Week 4 Programming Assignment Coursera Answers

4-6: Organizing a Lottery

These are Algorithmic Toolbox Week 4 Programming Assignment Coursera Answers

4-7:Closest Points