# The Two Pointers Technique in JavaScriptself.__wrap_n=self.__wrap_n||(self.CSS&&CSS.supports("text-wrap","balance")?1:2);self.__wrap_b=(r,n,e)=>{e=e||document.querySelector(`[data-br="\${r}"]`);let o=e.parentElement,l=u=>e.style.maxWidth=u+"px";e.style.maxWidth="";let s,i=o.clientWidth,p=o.clientHeight,a=i/2-.25,d=i+.5;if(i){for(l(a),a=Math.max(e.scrollWidth,a);a+1<d;)s=Math.round((a+d)/2),l(s),o.clientHeight===p?d=s:a=s;l(d*n+i*(1-n))}e.__wrap_o||typeof ResizeObserver<"u"&&(e.__wrap_o=new ResizeObserver(()=>{self.__wrap_b(0,+e.dataset.brr,e)})).observe(o)};self.__wrap_n!=1&&self.__wrap_b(":R4p4sm:",1)

November 8, 2023

The two pointers technique is an efficient method used to solve problems involving sequences, such as arrays, by using two references to traverse the data structure in one pass. This approach can lead to more elegant and less resource-intensive solutions.

## Understanding the two pointers technique

The essence of this technique is to take two references—pointers—to elements in a structure like an array and move them towards or away from each other to analyze or manipulate the elements. The pointers could move from the opposite ends of the array towards the center, or they could start at the same position and move in the same direction, depending on the problem at hand.

## When to use two pointers

This technique comes in handy for problems that involve paired elements, like finding a pair that sums up to a particular value or merging two sorted arrays. It's especially useful when you need to minimize space complexity, often allowing for an in-place solution that doesn't require additional data structures.

You could ship faster.

Imagine the time you'd save if you never had to build another internal tool, write a SQL report, or manage another admin panel again. Basedash is built by internal tool builders, for internal tool builders. Our mission is to change the way developers work, so you can focus on building your product.

## How two pointers work

### Opposite direction movement

```function findSumPair(arr, sum) {
let left = 0;
let right = arr.length - 1;

while (left < right) {
const currentSum = arr[left] + arr[right];
if (currentSum === sum) {
return [left, right];
} else if (currentSum < sum) {
left++;
} else {
right--;
}
}

return null;
}```

### Same direction movement

```function maxConsecutiveSum(arr, k) {
let maxSum = 0;
let tempSum = 0;
let start = 0;

for (let end = 0; end < arr.length; end++) {
tempSum += arr[end];
if (end >= k - 1) {
maxSum = Math.max(maxSum, tempSum);
tempSum -= arr[start];
start++;
}
}

return maxSum;
}```

## Examples of two pointers

### Removing duplicates from sorted array

```function removeDuplicates(arr) {
if (arr.length === 0) return 0;

let i = 0;
for (let j = 1; j < arr.length; j++) {
if (arr[j] !== arr[i]) {
i++;
arr[i] = arr[j];
}
}

return i + 1;
}```

### Reversing a string in place

```function reverseString(s) {
let left = 0;
let right = s.length - 1;

while (left < right) {
[s[left], s[right]] = [s[right], s[left]];
left++;
right--;
}
}```

## Benefits and considerations

Using the two pointers technique simplifies code and reduces the need for nested loops, thus cutting down on time complexity. It's important to consider this approach when dealing with linear data structures and when optimizing for efficiency. However, it requires a good understanding of the problem's constraints to apply effectively.

TOC

Understanding the two pointers technique
When to use two pointers
How two pointers work
Examples of two pointers
Benefits and considerations

November 8, 2023

The two pointers technique is an efficient method used to solve problems involving sequences, such as arrays, by using two references to traverse the data structure in one pass. This approach can lead to more elegant and less resource-intensive solutions.

## Understanding the two pointers technique

The essence of this technique is to take two references—pointers—to elements in a structure like an array and move them towards or away from each other to analyze or manipulate the elements. The pointers could move from the opposite ends of the array towards the center, or they could start at the same position and move in the same direction, depending on the problem at hand.

## When to use two pointers

This technique comes in handy for problems that involve paired elements, like finding a pair that sums up to a particular value or merging two sorted arrays. It's especially useful when you need to minimize space complexity, often allowing for an in-place solution that doesn't require additional data structures.

You could ship faster.

Imagine the time you'd save if you never had to build another internal tool, write a SQL report, or manage another admin panel again. Basedash is built by internal tool builders, for internal tool builders. Our mission is to change the way developers work, so you can focus on building your product.

## How two pointers work

### Opposite direction movement

```function findSumPair(arr, sum) {
let left = 0;
let right = arr.length - 1;

while (left < right) {
const currentSum = arr[left] + arr[right];
if (currentSum === sum) {
return [left, right];
} else if (currentSum < sum) {
left++;
} else {
right--;
}
}

return null;
}```

### Same direction movement

```function maxConsecutiveSum(arr, k) {
let maxSum = 0;
let tempSum = 0;
let start = 0;

for (let end = 0; end < arr.length; end++) {
tempSum += arr[end];
if (end >= k - 1) {
maxSum = Math.max(maxSum, tempSum);
tempSum -= arr[start];
start++;
}
}

return maxSum;
}```

## Examples of two pointers

### Removing duplicates from sorted array

```function removeDuplicates(arr) {
if (arr.length === 0) return 0;

let i = 0;
for (let j = 1; j < arr.length; j++) {
if (arr[j] !== arr[i]) {
i++;
arr[i] = arr[j];
}
}

return i + 1;
}```

### Reversing a string in place

```function reverseString(s) {
let left = 0;
let right = s.length - 1;

while (left < right) {
[s[left], s[right]] = [s[right], s[left]];
left++;
right--;
}
}```

## Benefits and considerations

Using the two pointers technique simplifies code and reduces the need for nested loops, thus cutting down on time complexity. It's important to consider this approach when dealing with linear data structures and when optimizing for efficiency. However, it requires a good understanding of the problem's constraints to apply effectively.

November 8, 2023

The two pointers technique is an efficient method used to solve problems involving sequences, such as arrays, by using two references to traverse the data structure in one pass. This approach can lead to more elegant and less resource-intensive solutions.

## Understanding the two pointers technique

The essence of this technique is to take two references—pointers—to elements in a structure like an array and move them towards or away from each other to analyze or manipulate the elements. The pointers could move from the opposite ends of the array towards the center, or they could start at the same position and move in the same direction, depending on the problem at hand.

## When to use two pointers

This technique comes in handy for problems that involve paired elements, like finding a pair that sums up to a particular value or merging two sorted arrays. It's especially useful when you need to minimize space complexity, often allowing for an in-place solution that doesn't require additional data structures.

You could ship faster.

Imagine the time you'd save if you never had to build another internal tool, write a SQL report, or manage another admin panel again. Basedash is built by internal tool builders, for internal tool builders. Our mission is to change the way developers work, so you can focus on building your product.

## How two pointers work

### Opposite direction movement

```function findSumPair(arr, sum) {
let left = 0;
let right = arr.length - 1;

while (left < right) {
const currentSum = arr[left] + arr[right];
if (currentSum === sum) {
return [left, right];
} else if (currentSum < sum) {
left++;
} else {
right--;
}
}

return null;
}```

### Same direction movement

```function maxConsecutiveSum(arr, k) {
let maxSum = 0;
let tempSum = 0;
let start = 0;

for (let end = 0; end < arr.length; end++) {
tempSum += arr[end];
if (end >= k - 1) {
maxSum = Math.max(maxSum, tempSum);
tempSum -= arr[start];
start++;
}
}

return maxSum;
}```

## Examples of two pointers

### Removing duplicates from sorted array

```function removeDuplicates(arr) {
if (arr.length === 0) return 0;

let i = 0;
for (let j = 1; j < arr.length; j++) {
if (arr[j] !== arr[i]) {
i++;
arr[i] = arr[j];
}
}

return i + 1;
}```

### Reversing a string in place

```function reverseString(s) {
let left = 0;
let right = s.length - 1;

while (left < right) {
[s[left], s[right]] = [s[right], s[left]];
left++;
right--;
}
}```

## Benefits and considerations

Using the two pointers technique simplifies code and reduces the need for nested loops, thus cutting down on time complexity. It's important to consider this approach when dealing with linear data structures and when optimizing for efficiency. However, it requires a good understanding of the problem's constraints to apply effectively.

### What is Basedash?

Ship faster, worry less with Basedash

Ship faster, worry less with Basedash

Ship faster, worry less with Basedash

You're busy enough with product work to be weighed down building, maintaining, scoping and developing internal apps and admin panels. Forget all of that, and give your team the admin panel that you don't have to build. Launch in less time than it takes to run a standup.

You're busy enough with product work to be weighed down building, maintaining, scoping and developing internal apps and admin panels. Forget all of that, and give your team the admin panel that you don't have to build. Launch in less time than it takes to run a standup.

You're busy enough with product work to be weighed down building, maintaining, scoping and developing internal apps and admin panels. Forget all of that, and give your team the admin panel that you don't have to build. Launch in less time than it takes to run a standup.

### Dashboards and charts

Edit data, create records, oversee how your product is running without the need to build or manage custom software.

## Related posts

SOC II
Type 2 Certified

### Features

Proudly carbon-neutral.

SOC II
Type 2 Certified

### Features

Proudly carbon-neutral.

SOC II
Type 2 Certified