DEV Community

Nicholas Galante
Nicholas Galante

Posted on

Algorithms: Two Point Method with Ruby

The Two Point method is a commonly used approach for solving problems in computer science and algorithms. This method involves the use of two pointers, typically referred to as left and right pointers, which are initially positioned at opposite ends of a data structure, such as an array. By using these pointers, we can efficiently search, manipulate, and process the data structure in different ways.

Here, we will explore the Two Point method and its application in solving algorithms. We will then look at a a few example problems and solve them using Ruby.

The Two Point Method

The Two Point method involves initializing two pointers, typically the left and right pointers, and manipulating them to solve a problem. This approach is particularly useful when dealing with data structures that are sorted or partially sorted.

The basic steps of the Two Point method are as follows:

  1. Initialize two pointers at opposite ends of the data structure.
  2. Process the data structure by moving the pointers based on a set of conditions.
  3. Repeat step 2 until the problem is solved.

By using the Two Point method, we can solve a variety of problems, including searching for specific elements, finding the minimum or maximum value in a data structure, and detecting patterns within the data.

Example #1

Let's consider the following problem:

Given a sorted array of unique integers and a target integer, return true if there exists a pair of numbers that sum to target, false otherwise.

For example, given nums = [1, 2, 4, 6, 8, 9, 14, 15] and target = 13, return true because 4 + 9 = 13.

To solve this problem using the Two Point method in Ruby, we can initialize two pointers at opposite ends of the array and manipulate them until we find a pair of numbers that sum to the target.

Let's first take a look at the solution code, and then we'll walk through it step by step.

def two_sum(nums, target)
  left = 0
  right = nums.length - 1

  while left < right
    sum = nums[left] + nums[right]
    if sum == target
      return true
    elsif sum < target
      left += 1
    else
      right -= 1
    end
  end

  return false
end

nums = [1, 2, 4, 6, 8, 9, 14, 15]
target = 13

puts two_sum(nums, target) # Output: true
Enter fullscreen mode Exit fullscreen mode

In this code, we initialize the left and right pointers at opposite ends of the array. Then, we use a while loop to manipulate the pointers based on the sum of the two numbers at their respective positions.

If the sum of the two numbers is equal to the target, we return true. If the sum is less than the target, we move the left pointer to the right to increase the sum. If the sum is greater than the target, we move the right pointer to the left to decrease the sum.

If we have exhausted all possible pairs of numbers without finding a sum that equals the target, we return false.

Example #2

Let's look at another example problem that we can solve using the Two Point method:

Write a function that reverses a string. The input string is given as an array of characters s.

For example:
Input: s = ["h","e","l","l","o"]
Output: ["o","l","l","e","h"]

To solve this problem using the Two Point method in Ruby, we can initialize two pointers, left and right, to the first and last indices of the input array, respectively. We then can swap the values at these pointers and move the pointers towards each other until they meet in the middle of the array.

Again, let's first take a look at the solution code, and then we'll walk through it step by step.

def reverse_string(s)
  left = 0
  right = s.length - 1

  while left < right
    # Swap values at left and right pointers
    temp = s[left]
    s[left] = s[right]
    s[right] = temp

    # Move pointers towards each other
    left += 1
    right -= 1
  end

  return s
end

s = ["h","e","l","l","o"]
puts reverse_string(s) # Output: ["o","l","l","e","h"]
Enter fullscreen mode Exit fullscreen mode

The reverse_string function takes in an array of characters s. It initializes the left pointer to the first index of the array, and the right pointer to the last index of the array.

It then enters a while loop, which continues as long as the left pointer is less than the right pointer. Within the loop, it swaps the values at the left and right pointers using a temporary variable temp.

After swapping the values, it moves the left pointer one index to the right and the right pointer one index to the left.

If the loop completes, the function returns the reversed array s.

The function call at the end passes in the input array s to the reverse_string function and prints the returned value, which in this case is ["o","l","l","e","h"].

Conclusion

The Two Point method is a powerful tool in solving problems in computer science and algorithms. By using two pointers to manipulate a data structure, we can efficiently search, process, and solve problems. In this blog post, we explored the Two Point method and demonstrated two examples that can be solved using Ruby.

Top comments (0)