سوال ۶۳ کنکور ارشد مهندسی کامپیوتر ۱۴۰۱
آرایه A شامل n عدد داده شده است. هدف پیدا کردن تعداد جفت اندیسهای i و j است، بهگونهای که . این کار در چه زمانی قابل انجام است؟ (بهترین گزینه را انتخاب کنید.)
پاسخ صحیح: گزینهٔ C —
Reformulate the condition
We must count index pairs with
Divide both sides by the positive quantity (indices are positive). Equivalently, define for each position
Then the condition becomes
Taking logarithms, with , this is exactly
So the problem reduces to: count the number of pairs whose values sum to more than a threshold (here the threshold is ).
Counting pairs with sum above a threshold
This is a standard task:
- Compute all values in .
- Sort them: .
- Use a two-pointer sweep (or, for each element, a binary search) over the sorted array to count, in total, how many pairs satisfy .
The two-pointer step: with pointers , on the sorted array, if then all elements between and also pair with to exceed , so add and decrement ; otherwise increment . Each pointer moves at most times, giving .
The dominant cost is the sort, so the whole task is
Why not the others
- / — the brute-force "check every pair" is , but the question asks for the best achievable time, which is faster.
- — achievable (e.g., merge-sort-style counting), but not the tightest; a single sort plus a linear two-pointer scan already gives .
Exam trick: "product of two array entries exceeds something" almost always becomes a pair-sum problem after taking a ratio or a logarithm, and counting pairs whose sum beats a threshold is the textbook sort + two-pointer pattern.