Two Pointer Technique

Sarv Jyoti
3 min readNov 19, 2023

“Welcome to the world of logic and syntax, where innovation is written in the language of code…”.

In this article, you will get more knowledge about algorithms (Two-pointer technique). I explain what is, use cases, and solve one leetCode problem in different approaches and also explain TC(Time complexity) & SC(Space complexity).

Introduction:

We can solve various approaches to array problems, the Two-Pointer Technique stands out as a frequent and effective pattern.

Basic Idea:

Initialization: You initialize two pointers, often called left and right or i and j, to specific positions in the array. Sometimes both pointers start at the beginning, or one at the beginning and one at the end, or position depending on the problems.

Uses:

  • With Array DS(data structure).
  • Linked-List DS(data structure).
  • etc.

Benefits:

  • To reduce Time complexity.
  • Solve array problems in an efficient way.
  • etc.

Let’s start problem: Two Sum(LeetCode):

Approach 01: →

Bruteforce:

class Solution {
public int[] twoSum(int[] nums, int target) {
//I take outer loop stating from 0 index
for(int i=0;i<nums.length-1;i++){
//I take inner loop stating from i+1 (1)index
for(int j=i+1;j<nums.length;j++){
//if nums of i index & nums of j index of element equals to our target value than retun indices
if(nums[i]+nums[j]==target){
return new int[] {i,j};
}
}
}
return null;
}
}

Approach 02: →

Two-Pointer:

class Solution {
public int[] twoSum(int[] nums, int target) {
//Take left pointer starting from beginning & right starting from end
int left = 0, right = nums.length - 1;
// left & right pointers not cross to another
while(left<right){
//Check left & rigth nums element of sum is equal our target value
if(nums[left]+nums[right]==target){
// if yes than retun indices
return new int[] {i,j};
}
// if no than ckeck else condition left & rigth nums element of sum is less than our target value than increase left pointer
else if(nums[left]+nums[right]<target){
left++;
}
// if no than ckeck else condition left & rigth nums element of sum is greater than our target value than m decrease right pointer
else{
right--;
}
}
}
// otherwise return null
return null;
}
}

Approach 03: →

HashMap:

class Solution {
public int[] twoSum(int[] nums, int target) {
// Create a HashMap to store the complement and its index
Map<Integer, Integer> complementMap = new HashMap<>();

// Iterate through the array
for (int i = 0; i < nums.length; i++) {
int complement = target - nums[i];

// Check if the complement exists in the HashMap
if (complementMap.containsKey(complement)) {
// Return the indices of the two elements whose sum is equal to the target
return new int[]{complementMap.get(complement), i};
}

// Put the current element and its index into the HashMap
complementMap.put(nums[i], i);
}

// If no solution is found
return null;
}
}

Analysis of TC(Time Complexity) & SC(Space Complexity):

Conclusion:

I hope you have liked the article and you got a solid concept about the Two-Pointer algorithm.

References:

You can solve some problems on LeeteCode with the help of a Two-pointer, and go to LeetCode, and solve it. 👍

thank you 🙏

#Array # Two-Pointer #LeeteCode # DSA #Problem-Solving

--

--