answersLogoWhite

0

This is an all-time favorite software interview question. The best way to solve this puzzle is to use Kadane's algorithm which runs in O(n) time. The idea is to keep scanning through the array and calculating the maximum sub-array that ends at every position. The sub-array will either contain a range of numbers if the array has intermixed positive and negative values, or it will contain the least negative value if the array has only negative values. Here's some code to illustrate.

void maxSumSubArray( int *array, int len, int *start, int *end, int *maxSum )

{

int maxSumSoFar = -2147483648;

int curSum = 0;

int a = b = s = i = 0;

for( i = 0; i < len; i++ ) {

curSum += array[i];

if ( curSum > maxSumSoFar ) {

maxSumSoFar = curSum;

a = s;

b = i;

}

if( curSum < 0 ) {

curSum = 0;

s = i + 1;

}

}

*start = a;

*end = b;

*maxSum = maxSumSoFar;

}

User Avatar

Wiki User

13y ago

What else can I help you with?

Related Questions

Do whole numbers include all positive integers?

The answer to the question depends on what property of positive numbers the question is about.If your question is a case of adding random apostrophes, then the answer is yes, they do include oll positive integers. - No apostrophe required.


Which situation would be described with a negative integer?

Negative numbers are useful whenever there are two opposites. For example, going up might be described by a positive number, going down by a negative; gaining money might be described by a positive number, losing money by a negative number. Such numbers will typically NOT be integers; you can use integers whenever the unit used (and the precision required) does not require you to use decimals. For example, "altitude below sea level" will be a negative number; if you don't need numbers less than a meter, you can use whole meters.


What do you do when you subtract a positive from a negative?

Subtracting a positive from a negative. You would write (this is an example) : -7-(+9)=-16 You don't necessarily have to put the parentheses and the positive sign, though sometimes it may be required.


What is required to make permanent magnets?

Make sure they have got a negative and positive sides


Rectifier How do you change the 2 arrows direction in the circuit to make it positive or negative In Bridge?

A: Actually there is no changing of diodes required to get negative voltage out put the two cathode to ground if you have a load the voltage across will be negative. Electrons only flow in one direction where the measurement point are located across the load makes positive or negative.


Is E. coli positive or negative for starch agar test?

E.coli does not digest the starch on a starch agar plate, therefore it does not produce amylase making it negative.


How do you write integers?

First comes a negative sign, if required. That is followed by a string of one or more of the following: {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}.


How are integers important in your daily life?

Natural number are numbers used from 1 to infinite and Whole numbers are Natural numbers including zero at the start . Integers are whole numbers including additive inverses of natural numbers. Natural numbers are used for counting and Zero in it is required to show the absence of something. the integers have both of these included in them and except that it also have the negative numbers included in them such as -1,-2,-3 and this mixture of whole, natural and negative numbers is known as integers. Integers are used in many ways such as integers are used on thermometers to provide the temperature marking of the material we are measuring Integers are also used in any profit or loss for e.g. in banking or any other situation ( by - or + sign ). Not only that integers can be used in almost everything for e.g. games , shopping etc.


Can binding energy be negative?

No, binding energy cannot be negative. Binding energy is always a positive quantity that represents the energy required to hold a system together. If the binding energy were negative, it would imply that the system is in an unstable state.


What is the Greek meaning of plus?

More, required to be added; positive, as distinguished from negative; -- opposed to minus., Hence, in a literary sense, additional; real; actual.


In DC motors why you are connecting fuse to positive and negative terminal and in single phase AC motor why you are connecting fuse to phase line and neutral link to neutral line?

in ac supply both positive and negative cycles contain in single phase the fuse only required for supply side but in dc supply positive and negative seperated by two terminals so we provide two fuses.


Reasons why a smear from a gram-positive control showed a gram-negative result?

Possible reasons for a gram-positive control smear showing a gram-negative result could include errors in the staining process, contamination of the specimen with gram-negative bacteria, or misidentification of the control as a gram-positive organism when it is actually gram-negative. Additional troubleshooting and repeat testing may be needed to confirm the result.