TWO POINTER ALGORITHM

Revision en1, by Unknown_712, 2021-03-08 00:16:37

THIS ALGORITHM IS VERY USEFUL.IF WE SEE TYPES OF PROBLEM LIKE FIND OF 2 NUMBERS FROM ARRAY WHOSE SUM IS EQUAL TO X.IF WE DON'T KNOW WHAT IS TWO POINTER ALGORITHM THEN THIS PROBLEM CAN BE SOLVED BY O(n^2) (ONE FOR LOOP INSIDE THE ANOTHER)where n is no. of element in array by two pointer algorithm we can solve such type of problem in o(n) for sorted array and o(nlogn) for unsorted array. if array is not sorted then we have to first sort the array because this algorithm is useful only for sorted arrays in this technique we use two pointer first one is pointing at the starting index and second one is at last index then we find sum of values at these pointers and we will compare this given sum if sum of values is less than given sum we will increment first pointer by 1 if our sum is greater than given sum then we will decrement the last pointer by 1; if we found our sum is equals to given sum then we will return iterate this while loop until(first pointer<last pointer)

BY THIS ALGORITHM WE CAN SOLVE SUCH TYPE OF PROBLEMS IN LESS TIME COMPLEXICITY AS WELL AS IN EASY WAY; FOR EACH BEGINNER ONE SHOULD N KNOW ABOUT THIS ALGORITHM

Tags 2 pointer algorithm

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English Unknown_712 2021-03-08 00:16:37 1181 Initial revision (published)