Find triplets in an array such that sum of two numbers is also a number in the given array

Nandan

Given an array, such as [1, 0, -1, 2, 3, 4, 5], find all subsets of length three where one element is equal to the sum of the other two elements in the subset. For example:

1 + -1 == 0 (from [1, 0, -1])
2 +  3 == 5 (from [2, 3, 5])

I came up with an O(n²) solution, but my interviewer insisted that there is an O(n log n) solution.

What is the best way to approach this problem?

DinoOcch

At first glance, I can't see an algorithm that is less than O(n²). (Unless it is an extra-ordinarily clever one)

This problem is very similar to the infamous 3SUM question: Basically, the idea is that given a set of n elements, are there any triples that sum to zero?

An algorithm solving 3SUM faster than O(n²) is not known - This is an open problem in computer science... (See here: http://en.wikipedia.org/wiki/3SUM)

However, since your question is not exactly 3SUM (A+B+C=0), rather it involves (A+B=C), we cannot immediately preclude clever tricks (but we do make them very unlikely).

That being said, your problem can be transformed into A+B-C=0, which seems to me that such a solution would solve 3SUM in less than O(n²) as well...


Consider this solution for such a problem:

Consider a solution to the 2SUM problem (that is, finding 2 element lists that sum to a certain value). We can Hash every element in the array (or some other data type with constant time lookup). Inserting every element takes O(n) time. This is the linear solution to a 2SUM problem. (One then loops over every element in the array and checks the hash for T-element)

Taking this idea, we can hash every sum in the array. However, getting every possible combination takes n*(n-1) or O(n²) time at best.


If anyone does have a solution faster than O(n²) I will be forever in awe of your number-fu (and if I find one, I will edit this and eat my words).

Good Luck in your search!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Find numbers of subarray of an array whose sum is divided by given number

From Dev

Given a sorted array and a parameter k, find the count of sum of two numbers greater than or equal to k

From Dev

Given an array of integers, find two numbers such that they add up to a specific target number

From Dev

Given an array of integers, find two numbers such that they add up to a specific target number

From Dev

Find Pair Of Integers in Array whose Sum is Given Number in PHP

From Dev

find numbers in array that add up to a given number c++

From Java

Given an array, you have to find the max possible two equal sum

From Dev

find even numbers in given array

From Dev

Check if the sum of two different numbers in an array equal a variable number?

From Dev

Check if the sum of two different numbers in an array equal a variable number?

From Dev

Finding a number that is sum of two other numbers in a sorted array

From Dev

Sum of difference of a number to an array of numbers

From Java

How to find the sum of an array of numbers

From Dev

Given an XOR and SUM of two numbers, how to find the number of pairs that satisfy them?

From Dev

Array of random numbers with sum in given range?

From Dev

Given 2d array of integers, find a path that sum up to a given number recursively

From Dev

Given XOR & SUM of two numbers. How to find the numbers?

From Dev

Generating a numpy array with all combinations of numbers that sum to less than a given number

From Dev

Rounding to the closest number in a given array of numbers

From Dev

Sum of any two elements in a given array

From Dev

Find widest subarray with given sum (array slicing)

From Dev

Given 4 numbers of array of 1 to 10 elements. Find 3 numbers whose sum can generate all the four numbers?

From Dev

Find the largest sequence in a given array of numbers

From Dev

find optimal sum of elements in list of numbers that are > a given number

From Dev

numpy - Given a number, find numbers that sum to it, with fuzzy weights

From Dev

Find permutations of a given set of numbers whose sum APPROACH a target number

From Dev

Given sum of two numbers A and B find maximum product of A and B

From Dev

How to find the sum of all numbers in an array?

From Dev

How to find the sum of all numbers in an array?

Related Related

  1. 1

    Find numbers of subarray of an array whose sum is divided by given number

  2. 2

    Given a sorted array and a parameter k, find the count of sum of two numbers greater than or equal to k

  3. 3

    Given an array of integers, find two numbers such that they add up to a specific target number

  4. 4

    Given an array of integers, find two numbers such that they add up to a specific target number

  5. 5

    Find Pair Of Integers in Array whose Sum is Given Number in PHP

  6. 6

    find numbers in array that add up to a given number c++

  7. 7

    Given an array, you have to find the max possible two equal sum

  8. 8

    find even numbers in given array

  9. 9

    Check if the sum of two different numbers in an array equal a variable number?

  10. 10

    Check if the sum of two different numbers in an array equal a variable number?

  11. 11

    Finding a number that is sum of two other numbers in a sorted array

  12. 12

    Sum of difference of a number to an array of numbers

  13. 13

    How to find the sum of an array of numbers

  14. 14

    Given an XOR and SUM of two numbers, how to find the number of pairs that satisfy them?

  15. 15

    Array of random numbers with sum in given range?

  16. 16

    Given 2d array of integers, find a path that sum up to a given number recursively

  17. 17

    Given XOR & SUM of two numbers. How to find the numbers?

  18. 18

    Generating a numpy array with all combinations of numbers that sum to less than a given number

  19. 19

    Rounding to the closest number in a given array of numbers

  20. 20

    Sum of any two elements in a given array

  21. 21

    Find widest subarray with given sum (array slicing)

  22. 22

    Given 4 numbers of array of 1 to 10 elements. Find 3 numbers whose sum can generate all the four numbers?

  23. 23

    Find the largest sequence in a given array of numbers

  24. 24

    find optimal sum of elements in list of numbers that are > a given number

  25. 25

    numpy - Given a number, find numbers that sum to it, with fuzzy weights

  26. 26

    Find permutations of a given set of numbers whose sum APPROACH a target number

  27. 27

    Given sum of two numbers A and B find maximum product of A and B

  28. 28

    How to find the sum of all numbers in an array?

  29. 29

    How to find the sum of all numbers in an array?

HotTag

Archive