solution2

by ShaikWasef

JavaScript

/* Solution for problem */


function solution(arr){
let min = Number.MAX_SAFE_INTEGER ; //place holder value
let maxProfit = 0 ; //place holder for max

for(let i = 0 ; i < arr.length ; i++){
	//recording the min till pt of observation
	min = Math.min(min,arr[i]);
  maxProfit = Math.max(maxProfit,arr[i]-min);
}

return maxProfit;
}

/*test cases*/
console.log(solution([0,2,3,4,5]));
console.log(solution([10,2,3,4,5]));
console.log(solution([100,2,0,4,99]));