In today’s Programming Praxis exercise, our goal is to find all groups of three numbers in a given list that sum up to 0, and to do so in O(n^{2}). Let’s get started, shall we?
import Data.List import qualified Data.IntSet as I
The naive O(n^{3}) version can be modified fairly easily to be more efficient. The first two loops can remain unchanged. In the final loop, we already know the number we’re looking for (the complement of the other two and all we need to know is whether it exists in the list. This can be done in O(1) using an IntSet. Unfortunately, this returns every triple thrice, so we sort the triples (O(1)) and remove the duplicates (I used the rather inefficient nub function here for the sake of brevity; in practive you’ll probably want to use a Set to reduce this part from O(k^{2}) to O(k log k)).
sum3 :: [Int] > [[Int]] sum3 xs = nub [sort [a,b,ab]  (a:bs) < tails xs, b < bs, I.member (ab) s] where s = I.fromList xs
A test to see if everything is working properly:
main :: IO () main = print $ sum3 [8,25,4,10,10,7,2,3] == [[10,2,8],[7,3,10]]
To check whether the function is indeed O(n^{2}) I ran some timings by using list of consecutive numbers:
Input list

Time taken 
1 to 8000  0.4s 
1 to 16000  1.4s 
1 to 32000  5.2s 
1 to 64000  20.6s 
As you can see, doubling the input size leads to a quadrupling of execution time, give or take a few tenths of a second, which means the algorithm is indeed O(n^{2}).
Tags: 3sum, bonsai, code, Haskell, kata, numbers, praxis, programming, sum, zero
Leave a Reply