Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Given a number X , find two elements in two sorted arrays such A[i]+B[j] = X in O(n+m)

Given the following problem , I'd appreciate for any corrections since I have no solution for the current question (taken from one of my professor's exams !!!) :

Remark: this is no homework !

Problem:

Given two sorted arrays A (with length n) & B (with length m) , where each

element (in both arrays) is a real number , and a number X (also a real number) ,

we'd like to know whether or not exists a ∈ A and b ∈ B , such as :

a + b = X , in O(n+m) running time .

Solution :

First , we start to check from the end of both arrays , since we don't need the numbers that are bigger than X :

  • i = n
  • k = m

  • while A[i] > X , i = i -1

  • while B[k] > X , k = k -1

Define j = 0 . Now start running from the current i in A , and from j in B :

  • while i > 0 , j < k :
  • if A[i]+B[j] == X , then return both cells
  • else j = j+1 , i = i -1

In the end we'd have either the two elements , or we'd reach out of bounds in one or both of the arrays , which means that no two elements such a + b = X are indeed exist .

Any remarks would be much appreciated

Thanks

like image 893
JAN Avatar asked Jun 27 '12 14:06

JAN


People also ask

How do I find an element in a sorted array?

The idea is to find the pivot point, divide the array into two sub-arrays and perform a binary search. For a sorted (in increasing order) and rotated array, the pivot element is the only element for which the next element to it is smaller than it. Using binary search based on the above idea, pivot can be found.

How do you find the median of two sorted arrays?

Let us see both the cases: If the size of the array is odd (i.e. m + n is odd) then median is obtained as (m + n) / 2. If the size of the array is even (i.e. m + n is even) then the median is obtained as the average of (m + n)/2 - 1 and (m + n)/2.

How do I find the kth element in a sorted array?

Merge the Two Arrays. Similar to one single step of the Merge Sort sorting algorithm, we can merge the two arrays and then directly retrieve the kth element. The basic idea of the merge algorithm is to start with two pointers, which point to the first elements of the first and second arrays (a).


3 Answers

You shouldn't adjust i and j at the same time. If the sum is too big, you should decrease i. If it is too small, increase j.

like image 147
MvG Avatar answered Sep 22 '22 17:09

MvG


This problem is a special case of the following question: Find number in sorted matrix (Rows n Columns) in O(log n)

Consider a matrix filled with the C[i,j] = A[i] + B[j], then starting from one of the corners, you decrease i if the sum is too big, and if it's too small, increase j. Of course you don't have to create and/or fill this matrix C in your program, just assume you know any element of this matrix: it's A[i] + B[j], you can compute it immediately at any moment. The resulting algorithm is O(m+n).

like image 23
unkulunkulu Avatar answered Sep 22 '22 17:09

unkulunkulu


I have the same question for homework. I worked out before I check it on the internet. Here is my solution(Python), hope some big guys see it and help me to improve it

# 1.3. Given two sorted arrays a and b and number S. Find two elements such that sum a[i] + b[j] = S.Time O(n).

def find_elem_sum(alist, blist, S): # O(n)

if alist is None or alist == [] or blist is None or blist == []:
    return None

# find the numbers which are less than S in the lists
#
#
# pretend it is done

a_length = len(alist)
b_length = len(blist)
a_index, b_index = 0, 0
count = 0
while alist and a_index < a_length and b_index < b_length:
    count += 1
    if alist[a_index] + blist[b_index] < S:
        if a_index < a_length - 1:
            a_index += 1
        if a_index == a_length - 1:
            b_index += 1;
        continue
    elif alist[a_index] + blist[b_index] > S:
        alist = alist[:a_index]
        a_length = len(alist)
        a_index = a_length - 1
    elif alist[a_index] + blist[b_index] == S:
        return [a_index, b_index, count]

return None
like image 32
Guan Avatar answered Sep 25 '22 17:09

Guan