# [Solved] Maximum Sum Subarray | Microsoft Interview Question

Problem Statement / Task: You are given an array of integers (includes both positive and negative numbers). You have to find the sum of that subarray that has the highest sum among all the sub-arrays that can be formed including the array itself.

Example:

```Input : -2  -1  -3  1  1  1  -4
Output : 3```

Explanation: The subarray [1,  1,  1] has the highest sum (which is 3) in all the sub-arrays.

This coding question was asked in Microsoft coding interview.

### MAXIMUM SUM SUBARRAY

Note: In this program, we are using Kaden’s Algorithm you can find more here.

#### Python Program

Prerequisite:

Code:

```def sub_Array_Sum(lst):
max_now = 0
max_end = 0
for i in range(len(lst)):
max_now = max_now+lst[i]
if max_now < 0:
max_now = 0
elif max_end < max_now:
max_end = max_now
return max_end

print("Enter values for array:")
l = list(map(int, input().split()))
print("Maximum Sum Subarray: ", sub_Array_Sum(l))```

Output:

```Enter values for array:
-2 -1 -3 1 1 1 -4
Maximum Sum Subarray: 3```

#### Java Program

Prerequisite:

Code:

```class Main {
public static void main(String[] args) {
int[] array = {-2, -1, -3, 1, 1, 1, -4};
System.out.println("Max sum subarray:" + subArraySum(array));
}

public static int subArraySum(int[] arr) {
int max_now = 0, max_end = 0;
for (int i = 0; i < arr.length; i++) {
max_now = max_now + arr[i];
if (max_now < 0)
max_now = 0;
else if (max_end < max_now)
max_end = max_now;
}
return max_end;
}
}```

Output:

`Max sum subarray: 3`

#### Complexity

In an interview, sometimes you will be asked to find the complexity of your written code.

The complexity of the above program is `O(n)` as we are traversing each element in the list at once.

Similar Competitive Coding Challenges:

This is all about the maximum sum subarray problem asked in the Microsoft interview. You can implement this logic in any other programming language of your choice.