More than one sub-array can have a maximum sum, in that case, output any.
The first line contains an integer 'T' which denotes the number of test cases or queries to be run.
The first line of each test case contains a single integer ‘N’ denoting the size of the array.
The second line of each test case contains ‘N’ space-separated integers denoting the elements of the array.
For each case, If the returned subarray is correct then print 1, else print 0.
The output of each test case will be printed in a separate line.
You do not need to input or print anything, and it has already been taken care of. Just implement the given function.
1 <= T <= 5
1 <= N <= 1000
-99 <= |arr| <= 99
Time limit: 1 sec.
Our Brute force approach will figure out all the possible combinations possible. To find a subarray, we need to know both the starting point and the ending out of the array. We can make a loop that would iterate through the array. During each iteration, we will make another nested loop inside which will take all possible combinations where the starting index will be the pointer of the parent loop and the endpoint will be the pointer of the current loop. We can take a few variables which would be updated whenever we get a new subarray with a larger sum.
As we know that we cannot have a negative-sum, we can simply maintain two variables as maximum and local sum where we would add every element to local sum. If our local sum exceeds our maximum sum, we shall update our maximum sum and even note down the index. If in any case, our local sum becomes negative, then it’s better to not consider any elements only and we would again start from the next index with local sum as 0.
Missing Number
Longest Subarray With Zero Sum
Merge Two Sorted Arrays Without Extra Space
Ninja And The Strictly Increasing Array
Negative To The End