Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Two Pointers Algorithm added in PHP/Searches/TwoPointers.php #152

Merged
merged 9 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
* [Lowerbound](./Searches/LowerBound.php)
* [Ternarysearch](./Searches/TernarySearch.php)
* [Upperbound](./Searches/UpperBound.php)
* [TwoPointers](./Searches/TwoPointers.php)

## Sorting
* [Arraykeyssort](./Sorting/ArrayKeysSort.php)
Expand Down
61 changes: 61 additions & 0 deletions Searches/TwoPointers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/**
* Two Pointers Approach
darwinz marked this conversation as resolved.
Show resolved Hide resolved
* This method is designed to improve the efficiency of algorithms by processing two elements per
* loop instead of just one, thereby reducing both time and space complexity
* The technique involves using two pointers, which can start from the beginning and the end of the array
* or list, or one pointer moving at a slower pace while the other moves at a faster pace.
*/

/**
* To understand the technique let's solve a simple problem,
* Problem Statement:
* There is an array of distinct integers "$list" and a target integer "$target". We have to find how many pairs
* of elements of $list sum up to $target.
* For example: $list = [2,4,3,8,6,9,10], $target = 12
* in this array 2+10 = 12, 4+8 = 12, 3+9 = 12, therefore, there are 3 pairs which sum up to 12.
*
* If we solve this problem using two nested loops, the time complexity would be O(n^2),
for($i=0; $i<sizeof($list); ++$i){
for($j=$i+1; $j<sizeof($list); ++$j){
if($list[$i] + $list[$j] == $target)
$ans++;
}
}
* To reduce the time complexity to O(n), we need Two Pointers technique.
*/

// Two Pointers step by step solution to that problem is given below:
function twoPointers($list, $target)
{
// First we need to sort the array so that we can take advantage of the order
sort($list);
// Now we are having two pointers "$left" and "$right"
// $left at the start and $right at the end of the array
$n = sizeof($list);
$left = 0;
$right = $n - 1;

$ans = 0; # we will store the number of pairs in $ans

// Now we will scan the array until $left and $right meet
while ($left < $right) {
# Now carefully understand how we move one of our pointers
if ($list[$left] + $list[$right] < $target) {
// it means the sum is less than $target and we need to increase our sum
// to increase the sum we will move $left pointer to the right
$left++;
} else if ($list[$left] + $list[$right] > $target) {
// the sum is greater than the target, so we need to decrease the sum
// to decrease the sum we will move our $right pointer to the left
$right--;
} else if ($list[$left] + $list[$right] == $target) {
// if it's true, we have found a pair
$ans++;
// now we will move one of our pointers, otherwise it'll run forever
$left++; # doesn't matter which one we move
}
// The loop will go until the pointers point to the same element
}
return $ans; # returning the number of pairs
}
15 changes: 15 additions & 0 deletions tests/Searches/SearchesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
require_once __DIR__ . '/../../Searches/ExponentialSearch.php';
require_once __DIR__ . '/../../Searches/TernarySearch.php';
require_once __DIR__ . '/../../Searches/InterpolationSearch.php';
require_once __DIR__ . '/../../Searches/TwoPointers.php';


class SearchesTest extends TestCase
Expand Down Expand Up @@ -202,4 +203,18 @@ public function testInterpolationSearch()
$result = interpolationSearch($list, $target);
$this->assertEquals(12, $result);
}

public function testTwoPointers()
{
$list = [1, 2, 4, 7, 8, 10, 11, 12, 15];
$target = 3;
$result = twoPointers($list, $target);
$this->assertEquals(1, $result);
$target = 12;
$result = twoPointers($list, $target);
$this->assertEquals(3, $result);
$target = 18;
$result = twoPointers($list, $target);
$this->assertEquals(2, $result);
}
}
Loading