Recently i came across a question during a programming contest on codeforces. Problem tags referred that the problems could be solved by using two pointer method. What exactly is the Two Pointer Method?
-
6Never heard of that. Please post a link. – Fred Foo Dec 07 '12 at 10:23
-
2What is the problem? (I'm guessing it's determining if a linked list has a cycle - in which case: http://stackoverflow.com/a/34255/12711) – Michael Burr Dec 07 '12 at 10:24
-
I found the codeforces page, I think, and neither of the links have anything to to with pointers, so I'm mystified. – ams Dec 07 '12 at 10:27
-
http://www.codeforces.com/contest/190/problem/D – Krishnabhadra Dec 07 '12 at 10:29
-
http://codeforces.com/blog/entry/4612 – Krishnabhadra Dec 07 '12 at 10:29
4 Answers
From what I can tell at those links, the "two pointer method" simply refers to indexing into two different arrays using two different indices (they refer to the array indices as pointers, which is somewhat different from how most C programmers use the term).
They were using it in context of a problem like
if (a[i] + b[j] == X)
// do something with i and j
where i
and j
were the pointers (in the generic sense of the term "pointer", not the C datatype sense).
This isn't anything terribly exotic, and until today I had no idea that anyone had coined a specific term for it.
When you talk to most C programmers, a term like "two pointer method" would imply something involving a double dereference, such as
x = **p;
which is nothing like what they're talking about at the codeforces link.

- 119,563
- 19
- 122
- 198
-
I was talking about http://www.codeforces.com/contest/252/problem/C..... – rohangulati Dec 08 '12 at 18:15
-
1
Google reports 2350 hits for "two pointer method" but the first couple pages use the phrase to refer to a variety of algorithms. It's seldom capitalized. Probably they were referring to one of several alternatives that had already been established in other discussion, literature, etc specific to the group hosting the competition.

- 134,909
- 25
- 265
- 421
Probably they meant having a pointer to a pointer.
In C this is often used when you e.g. need a called function to modify a pointer that the caller owns.
One example could be a function that inserts a tree node in a binary tree:
void tree_insert(Node **root, int value)
{
Node *here = *root;
if(here == NULL)
{
if((*root = malloc(sizeof ***root)) != NULL)
(*root)->value = value;
}
else if(value < here->value)
tree_insert(&root->left, value);
else if(value > here->value)
tree_insert(&root->right, value);
}
By passing a pointer to the tree's root (itself a pointer) the function is able to change it.
Using this, a tree can be initialized by:
Node *tree = NULL;
tree_insert(&tree, 42);
tree_insert(&tree, 4711);
In this example we could of course have used the return value from the function too, but hopefully you get the idea.

- 391,730
- 64
- 469
- 606
-
1
-
2
-
@MrLister I did, but not in the C++ sense, of course. But that was obviously confusing, so I edited it out. Thanks. – unwind Dec 07 '12 at 10:29
-
IMHO this example is terrible, since it does not use the possibilities of the pointer to pointer construct. The whole point is that the recursion can be avoided by using a pointer to pointer, and the `if (... NULL)` can be avoided too. – wildplasser Dec 07 '12 at 10:39
I guess you are talking about something like this:
int **allocation(int n, int m) {
int **matrix;
int i;
matrix = (int **) malloc(sizeof(int *) * n);
for (i = 0; i < n; i++)
matrix[i] = (int *) malloc(sizeof(int) * m);
return matrix;
}

- 1,026
- 14
- 32