Time Complexity: 3Sum algorithm under cubic time?What is the best algorithm for an overridden System.Object.GetHashCode?Finding three elements in an array whose sum is closest to a given numberUkkonen's suffix tree algorithm in plain EnglishImage Processing: Algorithm Improvement for 'Coca-Cola Can' RecognitionHow to find time complexity of an algorithmWhat is the optimal algorithm for the game 2048?Could this recursive binary search algorithm be more efficient?Are there any cases where you would prefer a higher big-O time complexity algorithm over the lower one?3Sum Algorithm Issue O(N^2 logn)Why does assignment in a binary search algorithm not add to the time complexity?

How does quantile regression compare to logistic regression with the variable split at the quantile?

Do infinite dimensional systems make sense?

Today is the Center

How can I make my BBEG immortal short of making them a Lich or Vampire?

Languages that we cannot (dis)prove to be Context-Free

How can I prevent hyper evolved versions of regular creatures from wiping out their cousins?

How much of data wrangling is a data scientist's job?

A newer friend of my brother's gave him a load of baseball cards that are supposedly extremely valuable. Is this a scam?

What is a clear way to write a bar that has an extra beat?

Did Shadowfax go to Valinor?

How to format long polynomial?

NMaximize is not converging to a solution

tikz convert color string to hex value

Can you really stack all of this on an Opportunity Attack?

DC-DC converter from low voltage at high current, to high voltage at low current

How do I deal with an unproductive colleague in a small company?

What does the "remote control" for a QF-4 look like?

Why does Kotter return in Welcome Back Kotter?

Is it legal for company to use my work email to pretend I still work there?

Why is Minecraft giving an OpenGL error?

How old can references or sources in a thesis be?

What is the word for reserving something for yourself before others do?

Does detail obscure or enhance action?

"You are your self first supporter", a more proper way to say it



Time Complexity: 3Sum algorithm under cubic time?


What is the best algorithm for an overridden System.Object.GetHashCode?Finding three elements in an array whose sum is closest to a given numberUkkonen's suffix tree algorithm in plain EnglishImage Processing: Algorithm Improvement for 'Coca-Cola Can' RecognitionHow to find time complexity of an algorithmWhat is the optimal algorithm for the game 2048?Could this recursive binary search algorithm be more efficient?Are there any cases where you would prefer a higher big-O time complexity algorithm over the lower one?3Sum Algorithm Issue O(N^2 logn)Why does assignment in a binary search algorithm not add to the time complexity?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















How can I make use of binary search for improving my algorithms time complexity?



I'm reviewing time complexity for some interviews & I'm having trouble making my algorithm more time efficient. This is my brute force solution for the 3-Sum problem: how many triples sum to exactly 0? Background: I don't have a CS degree.



//BRUTE FORCE SOLUTION: N^3
var threeSum = function(list)
var count = 0;

//checking each triple
for(var i = 0; i < list.length; i++)
for(var j = i+1; j < list.length; j++)
for(var k = j+1; k < list.length; k++)

if(list[i] + list[j] + list[k] === 0)count++;



return count;
;

//binary search code
var binarySearch = function(target, array)
var lo = 0;
var hi = array.length - 1;
//base case
while(lo <= hi)
var mid = Math.floor( lo + (hi - lo) / 2 );
if(target === array[mid]) return mid;
if(target < array[mid])
hi = mid - 1;

if(target > array[mid])
lo = mid + 1;


// value not found
return -1;



I was reviewing an algorithms course online from Princeton & the professor noted that this algorithm could be made more efficient with use of a binary search algorithm.



According to the professor we would:



  • sort the list

  • for each pair of numbers array[ i ] & array[ j ] binary search for -(array[ i ] + array[ j ])

However, I'm having trouble understanding how binary search comes in to solve the problem. Here is a slide from the lecture, which I'm still trying to understand, but maybe useful to others:



enter image description here



I'm sure there a several efficient solutions out there: feel free to chime in with your implementation as it may help me and other future readers. Thanks










share|improve this question



















  • 1





    The condition list[i] + list[j] + list[k] === 0 is equivalent (more or less) to list[k] === -(list[i] + list[j]), which makes the k loop of the brute-force solution a linear search, to be replaced by binary search.

    – David Eisenstat
    Feb 24 '16 at 22:59

















1















How can I make use of binary search for improving my algorithms time complexity?



I'm reviewing time complexity for some interviews & I'm having trouble making my algorithm more time efficient. This is my brute force solution for the 3-Sum problem: how many triples sum to exactly 0? Background: I don't have a CS degree.



//BRUTE FORCE SOLUTION: N^3
var threeSum = function(list)
var count = 0;

//checking each triple
for(var i = 0; i < list.length; i++)
for(var j = i+1; j < list.length; j++)
for(var k = j+1; k < list.length; k++)

if(list[i] + list[j] + list[k] === 0)count++;



return count;
;

//binary search code
var binarySearch = function(target, array)
var lo = 0;
var hi = array.length - 1;
//base case
while(lo <= hi)
var mid = Math.floor( lo + (hi - lo) / 2 );
if(target === array[mid]) return mid;
if(target < array[mid])
hi = mid - 1;

if(target > array[mid])
lo = mid + 1;


// value not found
return -1;



I was reviewing an algorithms course online from Princeton & the professor noted that this algorithm could be made more efficient with use of a binary search algorithm.



According to the professor we would:



  • sort the list

  • for each pair of numbers array[ i ] & array[ j ] binary search for -(array[ i ] + array[ j ])

However, I'm having trouble understanding how binary search comes in to solve the problem. Here is a slide from the lecture, which I'm still trying to understand, but maybe useful to others:



enter image description here



I'm sure there a several efficient solutions out there: feel free to chime in with your implementation as it may help me and other future readers. Thanks










share|improve this question



















  • 1





    The condition list[i] + list[j] + list[k] === 0 is equivalent (more or less) to list[k] === -(list[i] + list[j]), which makes the k loop of the brute-force solution a linear search, to be replaced by binary search.

    – David Eisenstat
    Feb 24 '16 at 22:59













1












1








1


1






How can I make use of binary search for improving my algorithms time complexity?



I'm reviewing time complexity for some interviews & I'm having trouble making my algorithm more time efficient. This is my brute force solution for the 3-Sum problem: how many triples sum to exactly 0? Background: I don't have a CS degree.



//BRUTE FORCE SOLUTION: N^3
var threeSum = function(list)
var count = 0;

//checking each triple
for(var i = 0; i < list.length; i++)
for(var j = i+1; j < list.length; j++)
for(var k = j+1; k < list.length; k++)

if(list[i] + list[j] + list[k] === 0)count++;



return count;
;

//binary search code
var binarySearch = function(target, array)
var lo = 0;
var hi = array.length - 1;
//base case
while(lo <= hi)
var mid = Math.floor( lo + (hi - lo) / 2 );
if(target === array[mid]) return mid;
if(target < array[mid])
hi = mid - 1;

if(target > array[mid])
lo = mid + 1;


// value not found
return -1;



I was reviewing an algorithms course online from Princeton & the professor noted that this algorithm could be made more efficient with use of a binary search algorithm.



According to the professor we would:



  • sort the list

  • for each pair of numbers array[ i ] & array[ j ] binary search for -(array[ i ] + array[ j ])

However, I'm having trouble understanding how binary search comes in to solve the problem. Here is a slide from the lecture, which I'm still trying to understand, but maybe useful to others:



enter image description here



I'm sure there a several efficient solutions out there: feel free to chime in with your implementation as it may help me and other future readers. Thanks










share|improve this question
















How can I make use of binary search for improving my algorithms time complexity?



I'm reviewing time complexity for some interviews & I'm having trouble making my algorithm more time efficient. This is my brute force solution for the 3-Sum problem: how many triples sum to exactly 0? Background: I don't have a CS degree.



//BRUTE FORCE SOLUTION: N^3
var threeSum = function(list)
var count = 0;

//checking each triple
for(var i = 0; i < list.length; i++)
for(var j = i+1; j < list.length; j++)
for(var k = j+1; k < list.length; k++)

if(list[i] + list[j] + list[k] === 0)count++;



return count;
;

//binary search code
var binarySearch = function(target, array)
var lo = 0;
var hi = array.length - 1;
//base case
while(lo <= hi)
var mid = Math.floor( lo + (hi - lo) / 2 );
if(target === array[mid]) return mid;
if(target < array[mid])
hi = mid - 1;

if(target > array[mid])
lo = mid + 1;


// value not found
return -1;



I was reviewing an algorithms course online from Princeton & the professor noted that this algorithm could be made more efficient with use of a binary search algorithm.



According to the professor we would:



  • sort the list

  • for each pair of numbers array[ i ] & array[ j ] binary search for -(array[ i ] + array[ j ])

However, I'm having trouble understanding how binary search comes in to solve the problem. Here is a slide from the lecture, which I'm still trying to understand, but maybe useful to others:



enter image description here



I'm sure there a several efficient solutions out there: feel free to chime in with your implementation as it may help me and other future readers. Thanks







javascript algorithm time-complexity binary-search






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 24 '16 at 23:29







cacoder

















asked Feb 24 '16 at 22:50









cacodercacoder

376213




376213







  • 1





    The condition list[i] + list[j] + list[k] === 0 is equivalent (more or less) to list[k] === -(list[i] + list[j]), which makes the k loop of the brute-force solution a linear search, to be replaced by binary search.

    – David Eisenstat
    Feb 24 '16 at 22:59












  • 1





    The condition list[i] + list[j] + list[k] === 0 is equivalent (more or less) to list[k] === -(list[i] + list[j]), which makes the k loop of the brute-force solution a linear search, to be replaced by binary search.

    – David Eisenstat
    Feb 24 '16 at 22:59







1




1





The condition list[i] + list[j] + list[k] === 0 is equivalent (more or less) to list[k] === -(list[i] + list[j]), which makes the k loop of the brute-force solution a linear search, to be replaced by binary search.

– David Eisenstat
Feb 24 '16 at 22:59





The condition list[i] + list[j] + list[k] === 0 is equivalent (more or less) to list[k] === -(list[i] + list[j]), which makes the k loop of the brute-force solution a linear search, to be replaced by binary search.

– David Eisenstat
Feb 24 '16 at 22:59












4 Answers
4






active

oldest

votes


















4















However, I'm having trouble understanding how binary search comes in to solve the problem.




This is how the n^2 log(n) algorithm works:



  1. Sort the list in O(nlogn) time

  2. Find all pairs of numbers (i,j), which is O(n^2) runtime.

  3. Then, for each pair (i,j), it finds a number k where k = sum - j - i. This is constant time O(1)

  4. The algorithm checks to see if each k exists, since the tuple (i,j,k) would sum to sum. To do this, do a binary search which takes log(n) time.

The final runtime would be O(nlogn) + O(logn * n^2) = O(n^2logn)



An alternative (and faster) solution would be to replace the sorting portion with a hashtable. Then, lookup of value k would take O(1) time instead of logn






share|improve this answer























  • Is your hashtable solution still valid if the array contains duplications? IE:[-1,-1,0]

    – Shlomi Schwartz
    Mar 27 '17 at 19:55


















1














The problem that the binary search approach is trying to solve is reducing the complexity of a cubic algorithm (this is your brute force algorithm) into a ~ N^2 log N algorithm.



As other commenters pointed out we know that when the following statement: list[i] + list[j] + list[k] == 0 is true than we found a 3SUM result. This is the same as saying that -(list[i] + list[j]) == list[k]. So the goal of the algorithm is to check for each i index and j index pair that there is a corresponding k index which satisfies the previous equation. Binary search can find those k indices in ~log N time. Hence the overall order of growth being ~N^2 log N (the outer for loops correspond to the N^2 part).



As for the implementation in javascript I would do it like this:






var threesum = function(list) 
list.sort(function(a,b)return a - b);
console.log(list);
var cnt = 0;
for(var i=0; i<list.length; i++)
for(var j=i+1; j<list.length; j++)
var k = bsearch(-(list[i]+list[j]), list);
if (k!= null && k > j)
console.log("[i=%d], [j=%d], [k=%d]", i,j,k);
cnt++;



return cnt;
;

var bsearch = function(key, a)
var lo = 0;
var hi = a.length-1;
while (lo <= hi)
var mid = lo + Math.floor((hi - lo) / 2);
if (a[mid] < key)
lo = mid + 1;
else if (a[mid] > key)
hi = mid - 1;
else
return mid;


return null;
;

threesum([41, 48, 31, 32, 34, 38, 1, -9, 12, 13, 99, 5, -65, 8, 3, -3])








share|improve this answer






























    0














    The algorithm basically works in the following way:



    • Sort the array (worst-case O(n ^ 2), depending upon sorting algorithm)

    • Generate all pairs of numbers - takes O(n ^ 2)

    • for each pair (i , j), there might exist k, such that 0 = i + j + k.kis simply-(i + j), thus we can easily look it up by binary search inO(log n). Cases wherei < k < j` doesn't hold are avoided to exclude duplicates.

    Thus total time-complexity is O(n ^ 2 log n).






    share|improve this answer






























      0

















      const threeSum =(array,target)=>

      let result =[]

      array = array.sort((a,b)=> a-b)

      for(let i=0; i < array.length-2; i++)

      let left = i+1;
      let right = array.length -1;

      while(left < right)

      let sum = array[i]+ array[left]+ array[right];

      if(sum === target)
      result.push([array[i],array[left], array[right]]);

      left++;
      right--
      else if(sum < target)

      //sum is lower than target so increment left pointer
      left++;
      else if(sum > target)
      //sum is greater than target so increment right pointer
      right--;





      //return the list
      return result;


      let a = [12, 3, 1, 2, -6, 5, -8, 6];
      console.log(threeSum(a, 0));





       Time Complexity: O(n^2) 
      Space Complexity: O(1)





      share|improve this answer























        Your Answer






        StackExchange.ifUsing("editor", function ()
        StackExchange.using("externalEditor", function ()
        StackExchange.using("snippets", function ()
        StackExchange.snippets.init();
        );
        );
        , "code-snippets");

        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "1"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader:
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        ,
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













        draft saved

        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f35614845%2ftime-complexity-3sum-algorithm-under-cubic-time%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        4















        However, I'm having trouble understanding how binary search comes in to solve the problem.




        This is how the n^2 log(n) algorithm works:



        1. Sort the list in O(nlogn) time

        2. Find all pairs of numbers (i,j), which is O(n^2) runtime.

        3. Then, for each pair (i,j), it finds a number k where k = sum - j - i. This is constant time O(1)

        4. The algorithm checks to see if each k exists, since the tuple (i,j,k) would sum to sum. To do this, do a binary search which takes log(n) time.

        The final runtime would be O(nlogn) + O(logn * n^2) = O(n^2logn)



        An alternative (and faster) solution would be to replace the sorting portion with a hashtable. Then, lookup of value k would take O(1) time instead of logn






        share|improve this answer























        • Is your hashtable solution still valid if the array contains duplications? IE:[-1,-1,0]

          – Shlomi Schwartz
          Mar 27 '17 at 19:55















        4















        However, I'm having trouble understanding how binary search comes in to solve the problem.




        This is how the n^2 log(n) algorithm works:



        1. Sort the list in O(nlogn) time

        2. Find all pairs of numbers (i,j), which is O(n^2) runtime.

        3. Then, for each pair (i,j), it finds a number k where k = sum - j - i. This is constant time O(1)

        4. The algorithm checks to see if each k exists, since the tuple (i,j,k) would sum to sum. To do this, do a binary search which takes log(n) time.

        The final runtime would be O(nlogn) + O(logn * n^2) = O(n^2logn)



        An alternative (and faster) solution would be to replace the sorting portion with a hashtable. Then, lookup of value k would take O(1) time instead of logn






        share|improve this answer























        • Is your hashtable solution still valid if the array contains duplications? IE:[-1,-1,0]

          – Shlomi Schwartz
          Mar 27 '17 at 19:55













        4












        4








        4








        However, I'm having trouble understanding how binary search comes in to solve the problem.




        This is how the n^2 log(n) algorithm works:



        1. Sort the list in O(nlogn) time

        2. Find all pairs of numbers (i,j), which is O(n^2) runtime.

        3. Then, for each pair (i,j), it finds a number k where k = sum - j - i. This is constant time O(1)

        4. The algorithm checks to see if each k exists, since the tuple (i,j,k) would sum to sum. To do this, do a binary search which takes log(n) time.

        The final runtime would be O(nlogn) + O(logn * n^2) = O(n^2logn)



        An alternative (and faster) solution would be to replace the sorting portion with a hashtable. Then, lookup of value k would take O(1) time instead of logn






        share|improve this answer














        However, I'm having trouble understanding how binary search comes in to solve the problem.




        This is how the n^2 log(n) algorithm works:



        1. Sort the list in O(nlogn) time

        2. Find all pairs of numbers (i,j), which is O(n^2) runtime.

        3. Then, for each pair (i,j), it finds a number k where k = sum - j - i. This is constant time O(1)

        4. The algorithm checks to see if each k exists, since the tuple (i,j,k) would sum to sum. To do this, do a binary search which takes log(n) time.

        The final runtime would be O(nlogn) + O(logn * n^2) = O(n^2logn)



        An alternative (and faster) solution would be to replace the sorting portion with a hashtable. Then, lookup of value k would take O(1) time instead of logn







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Feb 24 '16 at 23:40









        user4998087user4998087

        997




        997












        • Is your hashtable solution still valid if the array contains duplications? IE:[-1,-1,0]

          – Shlomi Schwartz
          Mar 27 '17 at 19:55

















        • Is your hashtable solution still valid if the array contains duplications? IE:[-1,-1,0]

          – Shlomi Schwartz
          Mar 27 '17 at 19:55
















        Is your hashtable solution still valid if the array contains duplications? IE:[-1,-1,0]

        – Shlomi Schwartz
        Mar 27 '17 at 19:55





        Is your hashtable solution still valid if the array contains duplications? IE:[-1,-1,0]

        – Shlomi Schwartz
        Mar 27 '17 at 19:55













        1














        The problem that the binary search approach is trying to solve is reducing the complexity of a cubic algorithm (this is your brute force algorithm) into a ~ N^2 log N algorithm.



        As other commenters pointed out we know that when the following statement: list[i] + list[j] + list[k] == 0 is true than we found a 3SUM result. This is the same as saying that -(list[i] + list[j]) == list[k]. So the goal of the algorithm is to check for each i index and j index pair that there is a corresponding k index which satisfies the previous equation. Binary search can find those k indices in ~log N time. Hence the overall order of growth being ~N^2 log N (the outer for loops correspond to the N^2 part).



        As for the implementation in javascript I would do it like this:






        var threesum = function(list) 
        list.sort(function(a,b)return a - b);
        console.log(list);
        var cnt = 0;
        for(var i=0; i<list.length; i++)
        for(var j=i+1; j<list.length; j++)
        var k = bsearch(-(list[i]+list[j]), list);
        if (k!= null && k > j)
        console.log("[i=%d], [j=%d], [k=%d]", i,j,k);
        cnt++;



        return cnt;
        ;

        var bsearch = function(key, a)
        var lo = 0;
        var hi = a.length-1;
        while (lo <= hi)
        var mid = lo + Math.floor((hi - lo) / 2);
        if (a[mid] < key)
        lo = mid + 1;
        else if (a[mid] > key)
        hi = mid - 1;
        else
        return mid;


        return null;
        ;

        threesum([41, 48, 31, 32, 34, 38, 1, -9, 12, 13, 99, 5, -65, 8, 3, -3])








        share|improve this answer



























          1














          The problem that the binary search approach is trying to solve is reducing the complexity of a cubic algorithm (this is your brute force algorithm) into a ~ N^2 log N algorithm.



          As other commenters pointed out we know that when the following statement: list[i] + list[j] + list[k] == 0 is true than we found a 3SUM result. This is the same as saying that -(list[i] + list[j]) == list[k]. So the goal of the algorithm is to check for each i index and j index pair that there is a corresponding k index which satisfies the previous equation. Binary search can find those k indices in ~log N time. Hence the overall order of growth being ~N^2 log N (the outer for loops correspond to the N^2 part).



          As for the implementation in javascript I would do it like this:






          var threesum = function(list) 
          list.sort(function(a,b)return a - b);
          console.log(list);
          var cnt = 0;
          for(var i=0; i<list.length; i++)
          for(var j=i+1; j<list.length; j++)
          var k = bsearch(-(list[i]+list[j]), list);
          if (k!= null && k > j)
          console.log("[i=%d], [j=%d], [k=%d]", i,j,k);
          cnt++;



          return cnt;
          ;

          var bsearch = function(key, a)
          var lo = 0;
          var hi = a.length-1;
          while (lo <= hi)
          var mid = lo + Math.floor((hi - lo) / 2);
          if (a[mid] < key)
          lo = mid + 1;
          else if (a[mid] > key)
          hi = mid - 1;
          else
          return mid;


          return null;
          ;

          threesum([41, 48, 31, 32, 34, 38, 1, -9, 12, 13, 99, 5, -65, 8, 3, -3])








          share|improve this answer

























            1












            1








            1







            The problem that the binary search approach is trying to solve is reducing the complexity of a cubic algorithm (this is your brute force algorithm) into a ~ N^2 log N algorithm.



            As other commenters pointed out we know that when the following statement: list[i] + list[j] + list[k] == 0 is true than we found a 3SUM result. This is the same as saying that -(list[i] + list[j]) == list[k]. So the goal of the algorithm is to check for each i index and j index pair that there is a corresponding k index which satisfies the previous equation. Binary search can find those k indices in ~log N time. Hence the overall order of growth being ~N^2 log N (the outer for loops correspond to the N^2 part).



            As for the implementation in javascript I would do it like this:






            var threesum = function(list) 
            list.sort(function(a,b)return a - b);
            console.log(list);
            var cnt = 0;
            for(var i=0; i<list.length; i++)
            for(var j=i+1; j<list.length; j++)
            var k = bsearch(-(list[i]+list[j]), list);
            if (k!= null && k > j)
            console.log("[i=%d], [j=%d], [k=%d]", i,j,k);
            cnt++;



            return cnt;
            ;

            var bsearch = function(key, a)
            var lo = 0;
            var hi = a.length-1;
            while (lo <= hi)
            var mid = lo + Math.floor((hi - lo) / 2);
            if (a[mid] < key)
            lo = mid + 1;
            else if (a[mid] > key)
            hi = mid - 1;
            else
            return mid;


            return null;
            ;

            threesum([41, 48, 31, 32, 34, 38, 1, -9, 12, 13, 99, 5, -65, 8, 3, -3])








            share|improve this answer













            The problem that the binary search approach is trying to solve is reducing the complexity of a cubic algorithm (this is your brute force algorithm) into a ~ N^2 log N algorithm.



            As other commenters pointed out we know that when the following statement: list[i] + list[j] + list[k] == 0 is true than we found a 3SUM result. This is the same as saying that -(list[i] + list[j]) == list[k]. So the goal of the algorithm is to check for each i index and j index pair that there is a corresponding k index which satisfies the previous equation. Binary search can find those k indices in ~log N time. Hence the overall order of growth being ~N^2 log N (the outer for loops correspond to the N^2 part).



            As for the implementation in javascript I would do it like this:






            var threesum = function(list) 
            list.sort(function(a,b)return a - b);
            console.log(list);
            var cnt = 0;
            for(var i=0; i<list.length; i++)
            for(var j=i+1; j<list.length; j++)
            var k = bsearch(-(list[i]+list[j]), list);
            if (k!= null && k > j)
            console.log("[i=%d], [j=%d], [k=%d]", i,j,k);
            cnt++;



            return cnt;
            ;

            var bsearch = function(key, a)
            var lo = 0;
            var hi = a.length-1;
            while (lo <= hi)
            var mid = lo + Math.floor((hi - lo) / 2);
            if (a[mid] < key)
            lo = mid + 1;
            else if (a[mid] > key)
            hi = mid - 1;
            else
            return mid;


            return null;
            ;

            threesum([41, 48, 31, 32, 34, 38, 1, -9, 12, 13, 99, 5, -65, 8, 3, -3])








            var threesum = function(list) 
            list.sort(function(a,b)return a - b);
            console.log(list);
            var cnt = 0;
            for(var i=0; i<list.length; i++)
            for(var j=i+1; j<list.length; j++)
            var k = bsearch(-(list[i]+list[j]), list);
            if (k!= null && k > j)
            console.log("[i=%d], [j=%d], [k=%d]", i,j,k);
            cnt++;



            return cnt;
            ;

            var bsearch = function(key, a)
            var lo = 0;
            var hi = a.length-1;
            while (lo <= hi)
            var mid = lo + Math.floor((hi - lo) / 2);
            if (a[mid] < key)
            lo = mid + 1;
            else if (a[mid] > key)
            hi = mid - 1;
            else
            return mid;


            return null;
            ;

            threesum([41, 48, 31, 32, 34, 38, 1, -9, 12, 13, 99, 5, -65, 8, 3, -3])





            var threesum = function(list) 
            list.sort(function(a,b)return a - b);
            console.log(list);
            var cnt = 0;
            for(var i=0; i<list.length; i++)
            for(var j=i+1; j<list.length; j++)
            var k = bsearch(-(list[i]+list[j]), list);
            if (k!= null && k > j)
            console.log("[i=%d], [j=%d], [k=%d]", i,j,k);
            cnt++;



            return cnt;
            ;

            var bsearch = function(key, a)
            var lo = 0;
            var hi = a.length-1;
            while (lo <= hi)
            var mid = lo + Math.floor((hi - lo) / 2);
            if (a[mid] < key)
            lo = mid + 1;
            else if (a[mid] > key)
            hi = mid - 1;
            else
            return mid;


            return null;
            ;

            threesum([41, 48, 31, 32, 34, 38, 1, -9, 12, 13, 99, 5, -65, 8, 3, -3])






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 18 '16 at 0:59









            Captain FogettiCaptain Fogetti

            83421018




            83421018





















                0














                The algorithm basically works in the following way:



                • Sort the array (worst-case O(n ^ 2), depending upon sorting algorithm)

                • Generate all pairs of numbers - takes O(n ^ 2)

                • for each pair (i , j), there might exist k, such that 0 = i + j + k.kis simply-(i + j), thus we can easily look it up by binary search inO(log n). Cases wherei < k < j` doesn't hold are avoided to exclude duplicates.

                Thus total time-complexity is O(n ^ 2 log n).






                share|improve this answer



























                  0














                  The algorithm basically works in the following way:



                  • Sort the array (worst-case O(n ^ 2), depending upon sorting algorithm)

                  • Generate all pairs of numbers - takes O(n ^ 2)

                  • for each pair (i , j), there might exist k, such that 0 = i + j + k.kis simply-(i + j), thus we can easily look it up by binary search inO(log n). Cases wherei < k < j` doesn't hold are avoided to exclude duplicates.

                  Thus total time-complexity is O(n ^ 2 log n).






                  share|improve this answer

























                    0












                    0








                    0







                    The algorithm basically works in the following way:



                    • Sort the array (worst-case O(n ^ 2), depending upon sorting algorithm)

                    • Generate all pairs of numbers - takes O(n ^ 2)

                    • for each pair (i , j), there might exist k, such that 0 = i + j + k.kis simply-(i + j), thus we can easily look it up by binary search inO(log n). Cases wherei < k < j` doesn't hold are avoided to exclude duplicates.

                    Thus total time-complexity is O(n ^ 2 log n).






                    share|improve this answer













                    The algorithm basically works in the following way:



                    • Sort the array (worst-case O(n ^ 2), depending upon sorting algorithm)

                    • Generate all pairs of numbers - takes O(n ^ 2)

                    • for each pair (i , j), there might exist k, such that 0 = i + j + k.kis simply-(i + j), thus we can easily look it up by binary search inO(log n). Cases wherei < k < j` doesn't hold are avoided to exclude duplicates.

                    Thus total time-complexity is O(n ^ 2 log n).







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Mar 18 '16 at 1:12









                    PaulPaul

                    11.7k31330




                    11.7k31330





















                        0

















                        const threeSum =(array,target)=>

                        let result =[]

                        array = array.sort((a,b)=> a-b)

                        for(let i=0; i < array.length-2; i++)

                        let left = i+1;
                        let right = array.length -1;

                        while(left < right)

                        let sum = array[i]+ array[left]+ array[right];

                        if(sum === target)
                        result.push([array[i],array[left], array[right]]);

                        left++;
                        right--
                        else if(sum < target)

                        //sum is lower than target so increment left pointer
                        left++;
                        else if(sum > target)
                        //sum is greater than target so increment right pointer
                        right--;





                        //return the list
                        return result;


                        let a = [12, 3, 1, 2, -6, 5, -8, 6];
                        console.log(threeSum(a, 0));





                         Time Complexity: O(n^2) 
                        Space Complexity: O(1)





                        share|improve this answer



























                          0

















                          const threeSum =(array,target)=>

                          let result =[]

                          array = array.sort((a,b)=> a-b)

                          for(let i=0; i < array.length-2; i++)

                          let left = i+1;
                          let right = array.length -1;

                          while(left < right)

                          let sum = array[i]+ array[left]+ array[right];

                          if(sum === target)
                          result.push([array[i],array[left], array[right]]);

                          left++;
                          right--
                          else if(sum < target)

                          //sum is lower than target so increment left pointer
                          left++;
                          else if(sum > target)
                          //sum is greater than target so increment right pointer
                          right--;





                          //return the list
                          return result;


                          let a = [12, 3, 1, 2, -6, 5, -8, 6];
                          console.log(threeSum(a, 0));





                           Time Complexity: O(n^2) 
                          Space Complexity: O(1)





                          share|improve this answer

























                            0












                            0








                            0










                            const threeSum =(array,target)=>

                            let result =[]

                            array = array.sort((a,b)=> a-b)

                            for(let i=0; i < array.length-2; i++)

                            let left = i+1;
                            let right = array.length -1;

                            while(left < right)

                            let sum = array[i]+ array[left]+ array[right];

                            if(sum === target)
                            result.push([array[i],array[left], array[right]]);

                            left++;
                            right--
                            else if(sum < target)

                            //sum is lower than target so increment left pointer
                            left++;
                            else if(sum > target)
                            //sum is greater than target so increment right pointer
                            right--;





                            //return the list
                            return result;


                            let a = [12, 3, 1, 2, -6, 5, -8, 6];
                            console.log(threeSum(a, 0));





                             Time Complexity: O(n^2) 
                            Space Complexity: O(1)





                            share|improve this answer
















                            const threeSum =(array,target)=>

                            let result =[]

                            array = array.sort((a,b)=> a-b)

                            for(let i=0; i < array.length-2; i++)

                            let left = i+1;
                            let right = array.length -1;

                            while(left < right)

                            let sum = array[i]+ array[left]+ array[right];

                            if(sum === target)
                            result.push([array[i],array[left], array[right]]);

                            left++;
                            right--
                            else if(sum < target)

                            //sum is lower than target so increment left pointer
                            left++;
                            else if(sum > target)
                            //sum is greater than target so increment right pointer
                            right--;





                            //return the list
                            return result;


                            let a = [12, 3, 1, 2, -6, 5, -8, 6];
                            console.log(threeSum(a, 0));





                             Time Complexity: O(n^2) 
                            Space Complexity: O(1)





                            const threeSum =(array,target)=>

                            let result =[]

                            array = array.sort((a,b)=> a-b)

                            for(let i=0; i < array.length-2; i++)

                            let left = i+1;
                            let right = array.length -1;

                            while(left < right)

                            let sum = array[i]+ array[left]+ array[right];

                            if(sum === target)
                            result.push([array[i],array[left], array[right]]);

                            left++;
                            right--
                            else if(sum < target)

                            //sum is lower than target so increment left pointer
                            left++;
                            else if(sum > target)
                            //sum is greater than target so increment right pointer
                            right--;





                            //return the list
                            return result;


                            let a = [12, 3, 1, 2, -6, 5, -8, 6];
                            console.log(threeSum(a, 0));





                            const threeSum =(array,target)=>

                            let result =[]

                            array = array.sort((a,b)=> a-b)

                            for(let i=0; i < array.length-2; i++)

                            let left = i+1;
                            let right = array.length -1;

                            while(left < right)

                            let sum = array[i]+ array[left]+ array[right];

                            if(sum === target)
                            result.push([array[i],array[left], array[right]]);

                            left++;
                            right--
                            else if(sum < target)

                            //sum is lower than target so increment left pointer
                            left++;
                            else if(sum > target)
                            //sum is greater than target so increment right pointer
                            right--;





                            //return the list
                            return result;


                            let a = [12, 3, 1, 2, -6, 5, -8, 6];
                            console.log(threeSum(a, 0));






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Mar 9 at 0:37









                            ASHISH RANJANASHISH RANJAN

                            78167




                            78167



























                                draft saved

                                draft discarded
















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid …


                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.

                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f35614845%2ftime-complexity-3sum-algorithm-under-cubic-time%23new-answer', 'question_page');

                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Thal And Out Agency railway station See also References External links Navigation menuOfficial Web Site of Pakistan RailwaysArchivedOfficial Web Site of Pakistan Railwayseeexpanding ite

                                Posting a File and Associated Data to a RESTful WebService preferably as JSON2019 Community Moderator ElectionREST URI convention - Singular or plural name of resource while creating itUploading both data and files in one form using Ajax?How do I upload a file with metadata using a REST web service?REST API - file (ie images) processing - best practicesSpring: JSON data and file in the same requestREST API Put large dataPosting a file and JSON data to Spring rest serviceRESTful Web Service Upload/Download Large Data With JSONREST API Design sending JSON data and a file to the api in same requestPost JSONArray to REST servicePUT vs. POST in RESTParsing values from a JSON file?JavaScript/jQuery to download file via POST with JSON dataHow do I upload a file with metadata using a REST web service?How to POST JSON data with Curl from Terminal/Commandline to Test Spring REST?Separate REST JSON API server and client?what's the correct way to send a file from REST web service to client?How do I write JSON data to a file?What is “406-Not Acceptable Response” in HTTP?REST API - file (ie images) processing - best practices

                                How can I change the color of pagination dots of UIPageControl?How to change UIPageControl dotsIs there a way to change page indicator dots colorCustomize dot with image of UIPageControl at index 0 of UIPageControlNo visible @interface for 'NSObject<PageControlDelegate>' declares the selector 'pageControlPageDidChange:'How to change the color of pagination dots in UIPageControl with a different color per pagepagecontrol indicator custom image instead of DefaultChanging the colour of UIPageControl dots in MonoTouchpagecontrol selectable page visibility color?Alternative way to load ViewControllers on a UIPageControlHow to set only layer.border-color for UIpage control dots in swiftHow can I develop for iPhone using a Windows development machine?How to change the name of an iOS app?UITableView - change section header coloruipagecontrol indicator(dot)issueCustom UIPageControl dots color not changingchange the interspace between UIPageControl dotsHow to change Status Bar text color in iOSHow can I change image tintColor in iOS and WatchKitUIPageControl dots with larger space in between each dotsHow to change the color of pagination dots in UIPageControl with a different color per page