What if all the elements are less than zero?

if all elemnets are less than 0 . ans=0 which is incorrect

yes @hssharma2212000, this is the only case when the kadane algorithm will fail , for that you can either initialize max_so_far with INT_MIN, or you can have a boolean variable ,which is true if all value are negative ,and you return maximum value of the array as answer