Kadane algorithm

while using kadane alfo,if we input an array wherein all elements are negative then it the algo will fail.
so are we required to first check if all numbers are negative and then return smallest of them?

@himanshuep32 Yes. In case all array elements are negative the Kadane algo will fail. So in that case return the least negative number.

that means i will have to traverse twice in an array :

first to check if all elements are negative and second to find smallest negative integer??

You can do it in one traversal also. Maintain a flag to check elements are negative, and max variable to store the least negative number so far.At the end of the traversal if flag suggests all elements negative then answer will be in max variable.

hey!
the constraint given for N in the question is==> " 1 <= N <= 100000 ".
Q1) how should i declare my array in the main function??
Q2) is it right to declare the array as int arr[100000] in the main function ?

Yes you can do that. There is no problem.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.