Print the largest sub-array with the least sum

Find the largest sub-array with the least sum if

var str = “1, 2, -3, 1, 1, -3, 4, -4, 0, 1”

The desired output is: -3,1,1,-3

That’s a string, not an array, it has no sub arrays, or actual numbers for that matter.

You can use .split() to convert it to array.

While we are primarily here to help people with their Free Code Camp progress, we are open to people on other paths, too. Some of what you are asking is pretty trivial in the Free Code Camp context, so you might find that if you’re not getting the instruction and material you need in your current studies, the FCC curriculum will really help you get started.

With your current questions, we don’t have enough context to know what you already know or don’t know, so it is impossible to guide you without just telling you the answer (which we won’t do).

It is pretty typical on here for people to share a codepen / jsfiddle example of what they have tried so that anyone helping has more of an idea of what help is actually helpful.

Please provide some example of what you’ve tried and I’m sure you’ll get more help.

Happy coding :slight_smile:

IS this answer correct or is there a better way to do it? Plz help, tnx :slight_smile:

function findsubarray(str){

str = str.split(',').map(function(item) {
    return parseInt(item, 10);
});

var cur_sum,least_sum=str[0],startIndex = 0;endIndex = 0;
var subarr = [];
for(let i=0;i<str.length;i++){
      subarr.push(str[i]); 
	  cur_sum = subarr.reduce(function(a,b){return a+b;});
     
	  if(cur_sum < least_sum){
	     startIndex = endIndex;
		 endIndex = i;
		  var result = str.slice(startIndex,endIndex+1);
	    least_sum  = cur_sum;

	  }
}
	return result;
}
console.log(findsubarray("1,2,-3,1,1,-3,4,-4,0,1"));