Teaching Kids Programming – Two Pointer Algorithm to Solve Four Sum Problem


Teaching Kids Programming: Videos on Data Structures and Algorithms

A few days ago, we learned to solve the four sum problem using the Bruteforce Algorithm or Depth First Search Algorithm: Teaching Kids Programming – Sum of Four Numbers using Depth First Search Algorithm

Given a list of integers nums and an integer k, return whether there are four distinct elements in the list that add up to k.

Constraints
n ≤ 100 where n is length of nums.
Example 1
Input
nums = [10, 3, 5, 9, 4, 0]
k = 17
Output
True
Explanation
We can use [10, 3, 4, 0] to get 17

Example 2
Input
nums = [2]
k = 8
Output
False
Explanation
There’s no 4 numbers that sum up to 2.

Example 3
Input
nums = [2, 2, 2, 2]
k = 8
Output
True

Hint: Find 2 elements sum up to k

We can use the Two Pointer (Two Sum) on the last two numbers when we have bruteforce the first two numbers.

Four Sum via Two Pointers

We need to first sort the numbers and this takes O(NLogN) time. And then we can bruteforce the first two numbers and apply the two pointer (like Two Sum) algorithm on the right of the second numbers. This takes O(N^3) time.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution:
    def solve(self, nums, k):
        nums.sort()   # O(NLogN)
        n = len(nums)
        for i in range(n):
            for j in range(i + 1, n):
                a, b = j + 1, n - 1
                while a < b:
                    x = nums[i] + nums[j] + nums[a] + nums[b]
                    if x > k:
                        b -= 1
                    elif x == k:
                        return True
                    else:
                        a += 1
        return False c
class Solution:
    def solve(self, nums, k):
        nums.sort()   # O(NLogN)
        n = len(nums)
        for i in range(n):
            for j in range(i + 1, n):
                a, b = j + 1, n - 1
                while a < b:
                    x = nums[i] + nums[j] + nums[a] + nums[b]
                    if x > k:
                        b -= 1
                    elif x == k:
                        return True
                    else:
                        a += 1
        return False c

If the sum of current chosen four numbers is smaller than the target, we move the left pointer (third number) to the right, and if it is larger than the target, we need to move the right pointer (fourth number) to the left – until both pointers meet.

Generally speaking, K-sum problems (K is larger or than equal to two), we can bruteforce the K-2 numbers in tex_efba145a558cb74f713a225f3bea708f Teaching Kids Programming - Two Pointer Algorithm to Solve Four Sum Problem algorithms math python teaching kids programming Two Pointer youtube video time – and then apply the Two Pointer which takes O(N) time. Overall, the time complexity is tex_f7a346b7c41ef3fd86b56d0e9a1a39aa Teaching Kids Programming - Two Pointer Algorithm to Solve Four Sum Problem algorithms math python teaching kids programming Two Pointer youtube video time.

Two sum algorithm’s complexity is O(NLogN) because the sorting dominates.

Two Sum Variation Problems

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
606 words
Last Post: How to Mask Email Addresses using PHP Function?
Next Post: How to Add a ShortCode to Include a PHP File in WordPress?

The Permanent URL is: Teaching Kids Programming – Two Pointer Algorithm to Solve Four Sum Problem

Leave a Reply