Your question is Reduce Runtime Complexity. Start with the requirements on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
MobiKwik's analytics pipeline needs to count pairs of distinct transaction amounts whose combined value lies within an inclusive range. A provided nested-loop implementation takes O(n²) time and does not scale for large batches.
Implement count_amount_pairs(amounts, low, high) to return the number of index pairs (i, j) such that i < j and low <= amounts[i] + amounts[j] <= high.
You must improve the runtime to O(n log n) or better. The returned count may be large, so Python's integer arithmetic should be used.
amounts, and integers low and high.def count_amount_pairs(amounts, low, high):