Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is two pointer problem same as sliding window

I was wondering about the significant difference between 'sliding window' and 'two pointer' problem.

It is giving me a hard time to differentiate between the two.

like image 602
mubir Avatar asked Sep 26 '20 13:09

mubir


People also ask

Is two pointers the same as sliding window?

Edit PagePage History. The two pointer method is a helpful technique to keep in mind when working with strings and arrays. It's a clever optimization that can help reduce time complexity with no added space complexity (a win-win!)

What is a two pointer problem?

Two pointers is really an easy and effective technique that is typically used for searching pairs in a sorted array. Given a sorted array A (sorted in ascending order), having N integers, find if there exists any pair of elements (A[i], A[j]) such that their sum is equal to X.

What is a sliding window in code?

Introduction. The Sliding window is a problem-solving technique of data structure and algorithm for problems that apply arrays or lists. These problems are painless to solve using a brute force approach in O(n²) or O(n³). However, the Sliding window technique can reduce the time complexity to O(n).


1 Answers

Sliding window algorithms can be implemented with a single pointer and a variable for window size. Typically we use all of the elements within the window for the problem (for eg - sum of all elements in the window).

Two pointer technique is quite similar but we usually compare the value at the two pointers instead of all the elements between the pointers.

Two pointers can also have variations like fast-slow pointer.

Hope it answers your question.

like image 156
pykam Avatar answered Oct 16 '22 08:10

pykam