• recategorized by
9,915 views
37 37 votes
An array $A$ contains $n$ integers in non-decreasing order, $A[1] \leq A[2] \leq \cdots \leq A[n]$. Describe, using Pascal like pseudo code, a linear time algorithm to find $i, j,$ such that $A[i]+A[j]=a$ given integer $M$, if such $i, j$ exist.

6 Answers

Best answer
60 60 votes
i = 1;
j = n;
while(i != j) {
   if(A[i] + A[j] == M) break;
   else if(A[i] + A[j] < M) i++;
   else j--;
}
• edited by
7 7 votes
i=1;
j=n;
if((a[1]+a[2]>M) OR (A[n]+a[n-1]<M))
  print Element not found
else
   {
    while(a[i]+a[j]!=M)
        { 
        if(a[i]+[J]<M) i++;
        else j--;
        }
   print i ,j
   }
   
5 5 votes
This algorithm can be written easily with help of hash data structure. (Here they have not said whetehr we can use other DS or not , So I will use it )

1. First use hash table, to has first half no of this array. You can do it using O(1) time.

2. Then check for a-A[k], where n/2<=k<=n, by lookup using Hash table. You can do this is O(1) each lookup( Ideal Time compexity).

TIme compexity => O(N)
3 3 votes

our works become simpler when we read the word its in increasing order...keep on adding the 2 consecutive  numbers

case 1:the sum is less than given element then continue

case 2:the sum is greater than given element then terminate and return -1

case 3:the sum is equal to given element then terminate and return the position of i and j

• edited by
3 3 votes

1. Two pointer Technique: Two Pointers Technique - GeeksforGeeks .  Time complexity: O(n) means Linear and Space Complexity is O(1) means constant. It will only work for sorted arrays.

2. Hashing: We can use an extra array to solve this problem in linear time. It will also work for unsorted arrays. Time complexity: O(n)  and Space Complexity: O(n).

Using Map in C++ STL :

void Find_Pair(int arr[], int n, int a)

{

    // create an empty map

    unordered_map<int, int> map;

    // do for each element

    for (int i = 0; i < n; i++)

    {

        // check if pair `(arr[i], a - arr[i])` exists

        // if the difference is seen before, print the pair

        if (map.find(a - arr[i]) != map.end())

        {

            cout << "Pair found (" << arr[map[a - arr[i]]] << ", " << arr[i] << ")";

            return;

        }

  // store index of the current element in the map

        map[arr[i]] = i;

    }

 // we reach here if the pair is not found

    cout << "Pair not found";

}

0 0 votes

Array A is in non-decreasing order (2 or more same elements can be present).
∴ A[1] ≼ A[2] ≼ A[3] ≼ ... ≼ A[n]

Pascal-like pseudo-code:
begin
        i:= 1, j:= n;
        while (i != j) do
        begin
                 if (A[i] + A[j] == M) then break;
                 else if begin 
                                    (A[i] + A[j] < M) then i:= i + 1;
                                    else begin
                                                    j:= j - 1;

                                    end
                 end
       end
end

Position:
Show:

Related questions

103 103 votes
15 answers 15 answers
47.7k
47.7k views
Kathleen asked Oct 4, 2014
47,737 views
In a compact single dimensional array representation for lower triangular matrices (i.e all the elements above the diagonal are zero) of size $n \times n$, non-zero eleme...
39 39 votes
5 answers 5 answers
12.3k
12.3k views
Kathleen asked Oct 5, 2014
12,267 views
A queue $Q$ containing $n$ items and an empty stack $S$ are given. It is required to transfer all the items from the queue to the stack, so that the item at the front of ...
46 46 votes
3 answers 3 answers
14.8k
14.8k views
Kathleen asked Oct 5, 2014
14,844 views
A rooted tree with $12$ nodes has its nodes numbered $1$ to $12$ in pre-order. When the tree is traversed in post-order, the nodes are visited in the order $3, 5, 4, 2, 7...
49 49 votes
5 answers 5 answers
30.7k
30.7k views
Kathleen asked Oct 4, 2014
30,729 views
Linked lists are not suitable data structures for which one of the following problems?Insertion sortBinary searchRadix sortPolynomial manipulation