Largest contiguous sum

by Vikram Deshmukh

HTML

<div id='output'>

</div>

CSS

#output {
  width: 100%;
  height: 100%;
  overflow: auto;
  box-sizing: border-box;
  padding: 20px;
  background-color: #333;
  border: 1px solid #666;
	color: white;
}

JavaScript

const arr = [1,3,5,-36,7,8,-3,3,6,-7,7,34,2];


// Using Kadane's Algorithm
function getLCS(target) {
	let max = 0; //15
  let curr = 0; //45
  for(const item of target) {
		curr += item;
		if(curr < 0) curr = 0;
		if(max < curr) {
			max = curr;
		}
  }
	// This logic won't handle an array of negative numbers.
	// In that case, we can just return the largest number in the array.
	return max > curr ? max : curr;
}

function print(whatever) {
	document.getElementById("output").innerHTML += "<br/>"+whatever;
}
print(getLCS(arr));