vlade087's blog

By vlade087, 11 years ago, In English

Hello anybody can say me what mean when the problem have a tag two pointers. What mean this approach?

  • Vote: I like it
  • 0
  • Vote: I do not like it

»
11 years ago, # |
  Vote: I like it +1 Vote: I do not like it

Look here

»
11 years ago, # |
  Vote: I like it +3 Vote: I do not like it

One of the recent problems involving two pointers is problem I:Pirate Chest from ICPC world finals 2013

This problem reduces to one-dimensional case: in integer array, for each element you want to find element that is lower than current on the left and right sides.

For every element you keep left and right positions for the element which is less than this element. (initially set to itselft). Iterate over descendant sorted array and update your info about pointers.

The problem's time limit pretty large. But involving some other NlogN approaches (such as fenwick tree) gives TL. By pointers you achieve lower constant.

»
5 months ago, # |
Rev. 2   Vote: I like it +10 Vote: I do not like it

+1

  • »
    »
    5 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    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.

    Illustration :

    Let us do discuss the working of two pointer algorithm in brief which is as follows. The algorithm basically uses the fact that the input array is sorted. We start the sum of extreme values (smallest and largest) and conditionally move both pointers. We move left pointer ‘i’ when the sum of A[i] and A[j] is less than X. We do not miss any pair because the sum is already smaller than X. Same logic applies for right pointer j.

    Code

    Time Complexity: O(n2). Auxiliary Space: O(1)

    Lets solve EDU problem:

    step 1:

    Merging Arrays

    Code

    Number of Smaller

    Code

    Number of Equal

    Code

    step 2:

    Segment with Small Sum

    Code

    Segment with Big Sum

    Code

    Number of Segments with Small Sum

    Code

    Number of Segments with Big Sum

    Code

    Segments with Small Set

    Code

    Segments with Small Spread

    Code 1
    Code 2

    Coprime Segment

    Code

    step 3:

    Looped Playlist

    Code

    Total Length

    Code

    Che city

    Code

    Stylish clothes

    Code

    Knapsack on a Segment

    Code

    Card Substrings

    Code

    Not Very Rude Substring

    Code

    A-B Knapsack

    Code

    Segment with the Required Subset

    Code

    all you need to two pointer