TWO POINTERS : to use or !to use ? this is the question . (and if to use : then how to use?)

Revision en1, by p0uya3767, 2024-07-19 13:12:15

Two pointers is a very simple and useful algorithm and actually using it is optional but, using it will make your algorithm easier and more simple.

but now, how to use it ? I think its good for you to look at https://www.geeksforgeeks.org/two-pointers-technique/ too. it'll be useful:blush:

so let go ...

As it's shown by its name this algorithm use two pointers on an array(or two arrays) which initially are located at position x, y . then if CONDITION1 move pointer1 one to left/right and if CONDITION2 move pointer2 one to left or right.

now let's switch to code.

QUESTION ANSWER SPOIL DANGER ================== to help you understand more I'll use one easy question and analyze them together.

QUESTION 1:( 1968B - Prefiquence )

so it says that we have two binary strings a,_ b_ and we want to find the maximum number of k which is the first k consecutive elements of a that are subsequence of b.

so what we do is we initially locate p1 (first pointer) at first of a and p2 (second pointer) at first of b.

if a[p1] == b[p2] : p1 ++
p2 ++

now p1 is the number of k.:sunglasses:

answer

this are some more easy questions that may help you: 1873D - 1D Eraser 1972A - Contest Proposal 1851B - Parity Sort

I hope you enjoyed reading this article.:blush: MAY THE CODE BE WITH YOU!:pray:

Tags two pointers, algorithms, learning, guide

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English p0uya3767 2024-07-19 13:12:15 1892 Initial revision (published)