Under construction

Still being built. Things may change

A Plus B

Interview

Time limit1sMemory limit1024 MB

Summary
Given two sorted arrays A and B of length N, output the N smallest of all N^2 pairwise sums A[i] + B[j] in increasing order.
Level

Medium5 of 10

Topics
Heap, Sorting
Solved
No attempts yet

Problem

Borcsa has two arrays, each containing NN non-negative integers. The first array holds A[0],A[1],…,A[N−1]A[0], A[1], \dots, A[N-1], and the second holds B[0],B[1],…,B[N−1]B[0], B[1], \dots, B[N-1]. Both arrays are sorted in increasing order:

  • A[0]≤A[1]≤⋯≤A[N−1]A[0] ≤ A[1] ≤ \dots ≤ A[N-1]
  • B[0]≤B[1]≤⋯≤B[N−1]B[0] ≤ B[1] ≤ \dots ≤ B[N-1]

Borcsa computed the sum A[i]+B[j]A[i] + B[j] for every pair of indices ii and jj from 00 to N−1N-1. Let CC be the list of all N2N^2 sums, sorted in increasing order. Find the first NN values of CC.

Constraints

  • 1≤N≤100,0001 ≤ N ≤ 100{,}000
  • 0≤A[i]≤1090 ≤ A[i] ≤ 10^9 for each ii with 0≤i<N0 ≤ i < N
  • 0≤B[i]≤1090 ≤ B[i] ≤ 10^9 for each ii with 0≤i<N0 ≤ i < N
  • AA and BB are sorted in increasing order.

Examples1

  1. Example 1

    Input
    1
    0
    0
    
    Expected output
    0