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;
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:
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
add a comment |
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:
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
1
The conditionlist[i] + list[j] + list[k] === 0
is equivalent (more or less) tolist[k] === -(list[i] + list[j])
, which makes thek
loop of the brute-force solution a linear search, to be replaced by binary search.
– David Eisenstat
Feb 24 '16 at 22:59
add a comment |
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:
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
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:
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
javascript algorithm time-complexity binary-search
edited Feb 24 '16 at 23:29
cacoder
asked Feb 24 '16 at 22:50
cacodercacoder
376213
376213
1
The conditionlist[i] + list[j] + list[k] === 0
is equivalent (more or less) tolist[k] === -(list[i] + list[j])
, which makes thek
loop of the brute-force solution a linear search, to be replaced by binary search.
– David Eisenstat
Feb 24 '16 at 22:59
add a comment |
1
The conditionlist[i] + list[j] + list[k] === 0
is equivalent (more or less) tolist[k] === -(list[i] + list[j])
, which makes thek
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
add a comment |
4 Answers
4
active
oldest
votes
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:
- Sort the list in O(nlogn) time
- Find all pairs of numbers
(i,j)
, which is O(n^2) runtime. - Then, for each pair (i,j), it finds a number
k
wherek = sum - j - i
. This is constant time O(1) - The algorithm checks to see if each
k
exists, since the tuple(i,j,k)
would sum tosum
. 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
Is your hashtable solution still valid if the array contains duplications? IE:[-1,-1,0]
– Shlomi Schwartz
Mar 27 '17 at 19:55
add a comment |
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])
add a comment |
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 existk
, such that 0 = i + j + k.
kis simply
-(i + j), thus we can easily look it up by binary search in
O(log n). Cases where
i < k < j` doesn't hold are avoided to exclude duplicates.
Thus total time-complexity is O(n ^ 2 log n)
.
add a comment |
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)
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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:
- Sort the list in O(nlogn) time
- Find all pairs of numbers
(i,j)
, which is O(n^2) runtime. - Then, for each pair (i,j), it finds a number
k
wherek = sum - j - i
. This is constant time O(1) - The algorithm checks to see if each
k
exists, since the tuple(i,j,k)
would sum tosum
. 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
Is your hashtable solution still valid if the array contains duplications? IE:[-1,-1,0]
– Shlomi Schwartz
Mar 27 '17 at 19:55
add a comment |
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:
- Sort the list in O(nlogn) time
- Find all pairs of numbers
(i,j)
, which is O(n^2) runtime. - Then, for each pair (i,j), it finds a number
k
wherek = sum - j - i
. This is constant time O(1) - The algorithm checks to see if each
k
exists, since the tuple(i,j,k)
would sum tosum
. 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
Is your hashtable solution still valid if the array contains duplications? IE:[-1,-1,0]
– Shlomi Schwartz
Mar 27 '17 at 19:55
add a comment |
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:
- Sort the list in O(nlogn) time
- Find all pairs of numbers
(i,j)
, which is O(n^2) runtime. - Then, for each pair (i,j), it finds a number
k
wherek = sum - j - i
. This is constant time O(1) - The algorithm checks to see if each
k
exists, since the tuple(i,j,k)
would sum tosum
. 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
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:
- Sort the list in O(nlogn) time
- Find all pairs of numbers
(i,j)
, which is O(n^2) runtime. - Then, for each pair (i,j), it finds a number
k
wherek = sum - j - i
. This is constant time O(1) - The algorithm checks to see if each
k
exists, since the tuple(i,j,k)
would sum tosum
. 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
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
add a comment |
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
add a comment |
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])
add a comment |
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])
add a comment |
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])
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])
answered Mar 18 '16 at 0:59
Captain FogettiCaptain Fogetti
83421018
83421018
add a comment |
add a comment |
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 existk
, such that 0 = i + j + k.
kis simply
-(i + j), thus we can easily look it up by binary search in
O(log n). Cases where
i < k < j` doesn't hold are avoided to exclude duplicates.
Thus total time-complexity is O(n ^ 2 log n)
.
add a comment |
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 existk
, such that 0 = i + j + k.
kis simply
-(i + j), thus we can easily look it up by binary search in
O(log n). Cases where
i < k < j` doesn't hold are avoided to exclude duplicates.
Thus total time-complexity is O(n ^ 2 log n)
.
add a comment |
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 existk
, such that 0 = i + j + k.
kis simply
-(i + j), thus we can easily look it up by binary search in
O(log n). Cases where
i < k < j` doesn't hold are avoided to exclude duplicates.
Thus total time-complexity is O(n ^ 2 log n)
.
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 existk
, such that 0 = i + j + k.
kis simply
-(i + j), thus we can easily look it up by binary search in
O(log n). Cases where
i < k < j` doesn't hold are avoided to exclude duplicates.
Thus total time-complexity is O(n ^ 2 log n)
.
answered Mar 18 '16 at 1:12
PaulPaul
11.7k31330
11.7k31330
add a comment |
add a comment |
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)
add a comment |
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)
add a comment |
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));
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));
answered Mar 9 at 0:37
ASHISH RANJANASHISH RANJAN
78167
78167
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
1
The condition
list[i] + list[j] + list[k] === 0
is equivalent (more or less) tolist[k] === -(list[i] + list[j])
, which makes thek
loop of the brute-force solution a linear search, to be replaced by binary search.– David Eisenstat
Feb 24 '16 at 22:59