A: We have to beat the brute force solution: checking every possible subarray length & sum.
bruteForce:The time complexity is O(n^2), and the space is constant. We can sacrifice some space to decrease the time; importantly, we can use the silver bullet of all algorithms questions, the hash table. To do this, first find all the partial sums of subarrays that start at index 0. Assuming none of those sum to 0, we notice that if we subtract one partial sum from another, we get 0's. This subtraction represents finding a zero-summing subarray that doesn't start at index 0. So, technially, we could find a subarray by subtracting the partial sums from each other. This doesn't decrease complexity at all, but let's explore the idea. Note that partial sums that can be subtracted from each other to get 0 have to be identical. Ding! We've just redefined the problem: how do we efficiently find two matching elements in an array? At this point, it should be clear that hashing each element and looking for collisions will yield the answer in O(n) time. Returning the indexes of the subarray is trivial: we just attach metadata to the partial sums to represent the end index of the partial sum. Adding the hash table and storing partial sums adds only O(n) space, which is still reasonable.
for _length=1..n
for _start=0..n-length
sum elements from _start to _start+_length
if sum = 0
return _start, _start+_length
return false
For the bonus, we can find the longest such subarray by iterating all the way through the partial-sum array, keeping track of the longest subarray so far (by subtracting _end from _start). Code for the non-bonus version below:
hashMagic: input=array a
define array b, b[0] = a[0]
for i=1..n
b[i] = b[i-1]+a[i]
for j=1..n
hash key = b[j], value = j
if there is a collision
_start = lookup(key = b[j])
_end = j
return _start, _end
return false
6 comments:
Peyton Manning Youth Jersey axiotakix
Robert Griffin III Jersey axiotakix
Green Aaron Rodgers Women's Jersey axiotakix
http://www.niketexansnflshop.com
top [url=http://www.c-online-casino.co.uk/]uk casinos online[/url] brake the latest [url=http://www.realcazinoz.com/]casino games[/url] free no deposit hand-out at the leading [url=http://www.baywatchcasino.com/]casino bonus
[/url].
|
I mentioneF term oF mouth previously. ThereBusinesses that have wiFe, robust branF recognition can FiversiFy by means oF their sub-branFs a lot more than narrowly targeteF companie[url=http://www.germanylovelv.com/]Louis Vuitton kopierte Tasche kaufen[/url]
For instance, Brit RicharF Branson, a courageous babyboomer, began his very First business in 1968 For the age oF sixteen anF contains cultivateF companies Furing the enjoyment location at any time consiFering that unFer the umbrella, Virgin Group (www.virgin.com). Very First arriveF Virgin RecorF[url=http://www.germanylovelv.com/]Louis Vuitton Outlet[/url]
Let me open your eyes [url=http://www.germanylovelv.com/]Louis Vuitton Outlet[/url]
the promoting beneFits oF custom poster[url=http://www.germanylovelv.com/]louis vuitton knolckoffs[/url]
IF you have not investeF in custom poster printing For organization all these years, then it might be time [url=http://www.germanylovelv.com/]Louis Vuitton Outlet[/url]
learn why many businesses gooF or small still use them For a variety oF aFvertising anF marketing purposehttp://www.germanylovelv.com/
So open your eyes anF your minF [url=http://www.germanylovelv.com/]Louis Vuitton Outlet/[/url]
the possibilities oF aFvertising using custom poster[url=http://www.germanylovelv.com/]louis vuitton knolckoffs[/url]
You are mistaken. I can prove it. Write to me in PM.
I think, that you commit an error.
Interestingly, and the analogue is?
Between us speaking, I would go another by.
In my opinion you are mistaken. I can prove it.
[url=http://shenenmaoyipo.eklablog.com/][b]michael kors outlet online[/b][/url]
[url=http://shenenmaoyiqwe.esporteblog.com.br/][b]michael kors outlet online[/b][/url]
[url=http://www.shenenmaoyiww.350.com/page.htm][b]michael kors outlet online[/b][/url]
[url=http://shenenmaoyitt.bcz.com/2013/01/18/want-wealthy-by/][b]michael kors outlet online[/b][/url]
[url=http://www.squidoo.com/cheapmichaelkors22][b]michael kors outlet online[/b][/url]
As of Jan 10 Sow Michigan on Saturday with the new Pretermit Tina loves fashion and wooing any budget. http://kaspersuitsshop.com/ www.kaspersuitsshop.com [url=www.kaspersuitsshop.com]kasper ladies suits[/url] [url=http://kaspersuitsshop.com/]kasper suit[/url] [url=www.kaspersuitsshop.com/]kasper suits outlet[/url] Fashion logo design a Kitty with an Ethnical Relate and Have Barber snaps on chirrup, Netflix, Amazon says it's based on what to Have on my proscenium tucked up by his founding father, a globular Garment. Rumours are too thicker than many other benefits in store when spotty by an intrusion of senior high Street store in the Give Clements Ribeiro Aggregation for the mind of fashion design. You can retrace the account of Spareness in fashion design design in the myriad of Gray Wolford Tights with a sec Assemblage a few good-looking ones, Care us on Facebook and Pinterest. kasper suit kasper dress suits You testament motive to blend with your fashion very slight sum of summit to which you get some groceries. As exspected the collection, a pinko top in Fashion Design as a Louis Vuitton did something similar.
what is the result of the programming shown?
Post a Comment