HelloKoding

Practical coding guides

3Sum Problem

In this article, you will learn to analyze and resolve the 3Sum problem by using two pointers algorithm

Problem

  • Given an array of integers A[N] and an integer number targetSum
  • Find if existing in A three integers A[i], A[j] and A[k] such that A[i] + A[j] + A[k] = targetSum

Example

  • Input A[8] = {4, -9, 0, 11, 6, -20, 1, 7}, targetSum = 10
  • Expected output true

Two pointers approach

TwoPointers_3Sum.java

package com.hellokoding.algorithm;

import java.util.Arrays;

public class TwoPointers_3Sum {
    boolean contains3(int[] a, int targetSum) {
        Arrays.sort(a);

        for (int i = 0; i < a.length - 2; i++) {
            int j = i + 1;
            int k = a.length - 1;

            while (j < k) {
                int sum = a[i] + a[j] + a[k];

                if (sum == targetSum) {
                    return true;
                } else if (sum < targetSum) {
                    j++;
                } else {
                    k--;
                }
            }
        }

        return false;
    }

    public static void main(String[] args) {
        int[] a = {4, -9, 0, 11, 6, -20, 1, 7};
        System.out.println(new TwoPointers_3Sum().contains3(a, 10));
    }
}
Follow HelloKoding