+ Reply to Thread
Results 1 to 3 of 3

Thread: find triplet whose sum upto 0 in an array of integers

  1. #1
    Krazy Guest

    find triplet whose sum upto 0 in an array of integers

    Given a list of n integers?(negative and positive), not sorted and duplicates allowed, you have to output the triplets which sum upto 0.

  2. #2
    Surfer is offline Senior Member
    Join Date
    Mar 2010
    Posts
    321
    Some rules to base the algorithm on:
    * Zeroes dont directly contribute to the sum. So, only matter as the third number when needed in an output triplet.
    * For a number n, if there exists a 0 in the list, we need to find a -n and include the 0 to form a triplet. (Question: Not sure if I can count the same 0 multiple times - probably not.). We can use a hash table for this part. O(n) to form the table and O(1) lookups.
    * When there's no -n for a specific n, we need to find out -a and -b such that (a+b) = n. In other words, in context of a n, for every a, search for a -(n-b) number. We can use a different hash table for this part. Again, O(n) to form the table and O(1) lookups.
    * Overall algorithm is therefore, no worse than O(n) (strictly, O(2n)...), which is nearly the best possible case.

  3. #3
    puspendra Guest
    Your third point.. How will you decide a and b..

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Find three numbers in an array
    By TopGun in forum Algorithm/Data Structure Questions
    Replies: 1
    Last Post: 16th August 2010, 15:16
  2. Max of two integers
    By TopGun in forum Algorithm/Data Structure Questions
    Replies: 1
    Last Post: 7th July 2008, 12:14
  3. find 2 numbers in an array
    By TopGun in forum Amazon
    Replies: 1
    Last Post: 19th June 2008, 14:06

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts