# How to use a 'two pointer' algorithm in Elixir

**URL:** <https://forum.elixirforum.com/t/how-to-use-a-two-pointer-algorithm-in-elixir/42513>\
**Category:** Questions\
**Tags:** functional-programming, algorithm, arrays\
**Created:** [September 21, 2021, 7:20am UTC](https://forum.elixirforum.com/t/how-to-use-a-two-pointer-algorithm-in-elixir/42513 "2021-09-21T07:20:19Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![stefanchrobot](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/stefanchrobot/32/7657_2.png) [@stefanchrobot](https://forum.elixirforum.com/u/stefanchrobot)\
**Post date:** [September 21, 2021, 8:50am UTC](https://forum.elixirforum.com/t/how-to-use-a-two-pointer-algorithm-in-elixir/42513/2 "2021-09-21T08:50:51Z")

</div>

`nums` is a list, so `Enum.at` has an O(N) access time. Switching `nums` to a map:

```elixir
faster_nums = 
  nums
  |> Enum.sort()
  |> Enum.with_index()
  |> Map.new(fn {value, index} -> {index, value} end) 

```

and using `Map.get` or the Access protocol:

```elixir
faster_nums[a] # instead of Enum.at(nums, a)

```

should do the trick, although that would be far from perfect. You can consider using Erlang’s [`array`](https://erlang.org/doc/man/array.html).

---

_[View the full topic](https://forum.elixirforum.com/t/how-to-use-a-two-pointer-algorithm-in-elixir/42513)._
