The problem is to find an algorithm (preferably using the divide and conquer approach) to count the number of all ordered pairs (i,j) in an array with i < j and A[i] >= 2*A[j].
I already know an O(²) way but I want a more efficient algorithm in or log order.
This is my code in python:
ans = 0
def recursion(arr):
global ans
if len(arr) > 1:
left = arr[:len(arr)//2]
right = arr[len(arr)//2:]
recursion(left)
recursion(right)
for i in left:
for j in right:
if i >= 2*j:
ans += 1
a = list(map(int, input().split()))
recursion(a)
print(ans)
Sample input: [8, 1, 9 ,4, 1]
Expected output: 6