×

# LOWSUM - Editorial

Author: Vineet Paliwal
Tester: Roman Rubanenko
Editorialist: Jingbo Shang

EASY

# PREREQUISITES:

Sort, Priority Queue, Binary Search

# PROBLEM:

Given two arrays A[1..K], B[1..K], deal with Q queries of finding the n-th smallest pair sum in all K^2 pair sums (A[i] + B[j]).

# EXPLANATION:

A brute force enumeration can give us an O(K^2 logK) - O(1) algorithm: just simply store all possible sums and use the some sorting algorithm such as quick sort. And then, for each query, return the n-th number of the stored sorted array. This brute force algortihm’s time complexity is O(K^2 + Q). Also, it needs O(K^2) space. Both time and memory are exceeded.

There are 2 ways to improve this brute force algorithm. The common key point to improve this brute force algorithm is as following: Suppose A[] and B[] are sorted ascendingly, A[i] + B[j] is smaller than or equal to any A[i] + B[k] if k > j.

For instance, we use quick sort to sort A[] and B[] in O(K logK) time. And then, 2 possible solutions are here.

The first solution is that we can find the smallest sum among at most K candidates (one for each A[i]) and remove it. After n removes, the n-th smallest sum is found. More specifically, we can maintain K pointers for each A[i]. Let’s say ptr[1..K] (equals 1 initially). First, we can use a binary heap (or other priority queues, balanced binary search trees, etc...) to find the smallest sum among A[i] + B[ptr[i]]. Second, suppose the smallest is A[p] + B[ptr[p]]. We remove it from the heap, then increase the pointer ptr[p] by 1 and insert a new element A[p] + B[ptr[p]] if it exists. Repeat this process n times, the n-th smallest sum is got. This algorithm’s time complexity is O(n log K) for each query, and thus O(K logK + Q n logK) in total.

The second solution is more tricky and useful. Consider the dual problem: given a number X, find how many pair sums are smaller than or equal to X (The answer of the original problem is that the smallest X such that there are at least n pair sums smaller than or equal to X). To solve the dual problem, based on the previous observation, there exists limit[i] such that A[i] + B[1..limit[i]] are all smaller than or equal to X while A[i] + B[limit[i] + 1 … K] are all greater than X. Furthermore, limit[i] >= limit[i + 1] since A[i] <= A[i + 1]. Using these two properties, we can simply get the rank of X in O(K) time. Through binary search, we can get the answer of original problem in O(K logAnswer), and thus O(K logK + Q K logAnswer) in total.

# AUTHOR'S AND TESTER'S SOLUTIONS:

Author's solution can be found here.
Tester's solution can be found here.

This question is marked "community wiki".

15.9k347484508
accept rate: 35%

161446172

 2 I solved this after the contest and used a very simple approach. First sort both the arrays. Since the limit on q is 10000, this can be done with the following code.  for(j=1;j<=n;++j) { k=10001/j; ind=min(k,n); num=a[j]; for(k=1;k<=ind;++k) v.pb(num+b[k]); }  This will ensure that atleast the first 10000 sums will be stored in v. Then we just have to sort the vector v and print the answer of every query. answered 21 Nov '13, 02:08 1.5k●8●21●30 accept rate: 0% can u please explain me ur approach? (12 Dec '13, 10:32) @sikander_nsit please explain your solution more. It would be great for us to get a solution which is very simple and sweet. Thanks, (14 Mar '14, 00:13) your trick is great .. can you give proof of correctness of your algo..please?? (21 Aug '15, 00:58)
 1 @arcturus I used binary search to search for X and then binary search to count the pairs, but also a trick (when count exceeds 10.000 break, since qi is maximum 10.000). Without the trick it gave TLE. answered 18 Nov '13, 14:17 4★lazzrov 136●1●1●8 accept rate: 0% Ah, I see. But I guess if the testcase was really evil, the second solution will also get TLE even with that trick. Probably it was not the intended way, as both tester and setter used the first approach. (18 Nov '13, 22:25) arcturus5★
 1 can anybody please explain me the first approach? not getting it :( answered 12 Dec '13, 10:24 296●8●21●22 accept rate: 11%
 1 though editorial has been provided for this problem but still i am not able to understand it. it would be great if someone explain me the correct approach to solve this problem.......... answered 28 Dec '13, 14:40 4★zealf 1.1k●6●11●26 accept rate: 3% well,i am requesting again especially to the editorialist of this problem to explain his solution given above... (29 Dec '13, 18:12) zealf4★
 0 Hmm, I tried both approach in the contest. However, only the first one get AC (http://www.codechef.com/viewsolution/2998337 ). The second solution gave me TLE (http://www.codechef.com/viewsolution/2997409 ). Is the time limit too strict for the second solution or it is just me that didn't implement the algorithm efficiently? answered 18 Nov '13, 08:14 5★arcturus 1●2 accept rate: 0%
 0 I'm getting wrong answer for this solution. Can someone help me out? http://www.codechef.com/viewsolution/3718821 answered 08 Apr '14, 04:43 3★anndr31 16●1 accept rate: 16%
 0 Though editorial has been delivered for this tricky but quiet i am not competent to comprehend it. it would be good if somebody clarify me the exact method to resolve this problem pay to get a research paper done answered 21 Jun '16, 11:35 1 accept rate: 0%
 0 Despite the fact that publication has been conveyed for this dubious however calm i am not capable to grasp it. somebody should illuminate me the precise strategy to determine this issue. startup demo video answered 15 Jul '16, 11:47 1 accept rate: 0%
 0 despite the fact that publication has been accommodated this issue yet at the same time i am not ready to comprehend it. Explainer Video Companies someone should clarify me the right way to deal with take care of this issue. answered 29 Mar, 17:49 1 accept rate: 0%
 0 editorial has been delivered for this tricky but quiet i am not competent to comprehend it. it would be great if someone explain me the correct approach to solve this problem Ouran HighSchool Host Club Season 2 is going to be release soon. answered 20 Jul, 17:36 0★bubu12 1●1 accept rate: 0%
 0 The way that production has been passed on for this questionable however quiet i am not able to get a handle on it. someone ought to light up me the Purchase Coursework Online at Coursework Camp exact technique to decide this issue. article has been conveyed for this dubious yet calm i am not able to grasp it. answered 07 Aug, 19:29 0★gracie2 1 accept rate: 0%
 0 I am a tattoo artist and this is my recommended list of best tattoos for girls http://tattoosboygirl.com/tattoos-for-girls/ here. link This answer is marked "community wiki". answered 30 Aug, 19:20 1 accept rate: 0%
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×11,561
×2,607
×594
×534
×16
×11

question asked: 18 Nov '13, 00:17

question was seen: 14,989 times

last updated: 30 Aug, 19:20