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. Data Structures gate1994 data-structures array normal descriptive + – Kathleen 9.9k views answer comment Share Follow Print See all 4 Comments 4 4 Comments reply Mizuki commented Nov 1, 2018 reply Follow flag What is Pascal like pseudo code? 0 0 replyShare Shivamsingh43 commented Sep 10, 2020 reply Follow flag As an alternative to compilers generating machine code or assembly language, a more portable alternative is to generate a pseudo machine language that can then be transported to various machines and converted to their native code. This code is called Pascal like pseudo code. 1 1 replyShare addressisvivek commented Jan 8, 2025 reply Follow flag Two-pointer technique:Start with two pointers: one at the beginning (left) and one at the end (right) of the array.Calculate the sum A[left]+A[right]If the sum is equal to M, you've found the solution.If the sum is less than M, move the left pointer to the right to increase the sum.If the sum is greater than M, move the right pointer to the left to decrease the sum.Continue until the pointers meet or a solution is found. 3 3 replyShare Jayvijay Chauhan commented Jul 18 reply Follow flag User Two Pointer Approach: Start ------ End 0 0 replyShare Please log in or register to add a comment.
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--; } ankitrokdeonsns answered Oct 11, 2014 • edited Jun 13, 2018 by Milicevic3306 ankitrokdeonsns comment Share Follow See all 11 Comments 11 11 Comments reply Show 8 previous comments Prashant. commented Mar 5, 2018 reply Follow flag :) ... 1 1 replyShare Mizuki commented Nov 1, 2018 reply Follow flag What is Pascal like pseudo code? 0 0 replyShare Neelam_$ingh_222 commented Jul 23, 2020 reply Follow flag Sir why can't it be in linear time as we can store indexes i,j or can print them and continue with our loop to find more such pairs in array ? 1 1 replyShare Please log in or register to add a comment.
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 } vermamayank564 answered Jul 14, 2017 vermamayank564 comment Share Follow 0 reply Please log in or register to add a comment.
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) Akash Kanase answered Dec 17, 2015 Akash Kanase comment Share Follow See all 3 Comments 3 3 Comments reply radha gogia commented Jan 3, 2016 reply Follow flag can u explain ur logic once again ,its really hard for me to understand. what is the meaning of this line: First use hash table, to has first half no of this array. 1 1 replyShare Aspi R Osa commented Jan 10, 2016 reply Follow flag @Akash : n/2<=k<=n, Why this condition? I think it could be from 1 to n anywhere? 0 0 replyShare anon1 commented Jul 8, 2021 reply Follow flag First use hash table, to has first half no of this array. You can do it using O(1) time. There is a typo. We can’t hash half of the elements of an array in O(1) time it will take O(n) time. 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). Suppose there are 10 elements in an array and only by adding A[1] and A[2] we are getting a. Your code will output there is no such pair exist. Sir, I think both of your points are wrong. But the time complexity will be O(n). I tried to explain with code. 1 1 replyShare Please log in or register to add a comment.
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 Bhagirathi answered Oct 11, 2014 • edited Dec 22, 2017 by Puja Mishra Bhagirathi comment Share Follow See all 2 Comments 2 2 Comments reply Arjun commented Oct 11, 2014 reply Follow flag But i and j need not be consecutive rt? if sum is > given element, we have to start with the next i rt? 2 2 replyShare Bhagirathi commented Oct 16, 2014 reply Follow flag yes you have got a point let me rethink... 0 0 replyShare Please log in or register to add a comment.
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"; } anon1 answered Jul 8, 2021 anon1 comment Share Follow 0 reply Please log in or register to add a comment.
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 prithatiti answered May 31, 2020 prithatiti comment Share Follow 0 reply Please log in or register to add a comment.