Microsoft Interview Question
Software Engineer in Tests#include <iostream>
using namespace std;
int main()
{
int max_so_far = 0,max_ending_here =0;
int A[] = {14,-15,6,-3,2,3,4,5,-1};
int numOfLoops = 0;
int size = sizeof(A)/sizeof(int);
for(int i=0;i<size;i++){
++numOfLoops;
max_ending_here = (max_ending_here + A[i])>0?max_ending_here + A[i]:0;
max_so_far = max_ending_here>max_so_far?max_ending_here:max_so_far;
}
cout<<"number of loopings:"<<numOfLoops<<endl;
cout<<"Maximum sum::"<<max_so_far<<endl;
}
Ya, I guess kadane's algorithm gives 0 sum for negative sums. The code above is kadane's algorithm
I think we can add one more variable, which finds the highest number after being initialized with the first element in the array.
And while returning we can check, and return the max.
int main()
{
int max_so_far = 0,max_ending_here =0;
int A[] = {14,-15,6,-3,2,3,4,5,-1};
int max_element = A[0];
int numOfLoops = 0;
int size = sizeof(A)/sizeof(int);
for(int i=0;i<size;i++){
++numOfLoops;
max_element = A[i] > max_element? A[i]:max_element;
max_ending_here = (max_ending_here + A[i])>0?max_ending_here + A[i]:0;
max_so_far = max_ending_here>max_so_far?max_ending_here:max_so_far;
}
cout<<"number of loopings:"<<numOfLoops<<endl;
cout<<"Maximum sum::"<<max_so_far > max_element? max_so_far:max_element<<endl;
}
After trying out this question, it looks so simple to write the code for that.
int main()
{
int arr[]={14,-15,6,-3,2,3,4,5,-1};
int size = sizeof(arr)/sizeof(int);
int i,maxsum=arr[0];
for(i=1;i<size;i++)
{
maxsum = ( (maxsum+arr[i])<maxsum) ? ( maxsum>arr[i] ? maxsum:arr[i] ) : (maxsum+arr[i]) ;
}
printf("Maximum sum:%d",maxsum);
}
I think the subarray should be continuous. Your solution only add all the positive numbers together...
If the answer needs to be from contiguous subarray, soln is(derived from kadane alg)
int main()
{
int a[]={-2, -3, 4, -4, 50, 5, -3};
printf("\nMax Sum-%d",maxSubArraySum(a,sizeof(a)/sizeof(a[0])));
return;
}
int maxSubArraySum(int a[], int size)
{
int max_so_far = 0, max_ending_here = 0;
int i;
int negative=a[0];
for(i = 0; i < size; i++)
{
max_ending_here = max_ending_here + a[i];
if(max_ending_here < 0)
max_ending_here = 0;
else if (max_so_far < max_ending_here)
max_so_far = max_ending_here;
if(max_so_far==0 && negative<a[i])
{
negative=a[i];
}
}
return (max_so_far!=0?max_so_far:negative);
}
int main()
{
int a[]={-2, -3, 4, -4, 50, 5, -3};
printf("\nMax Sum-%d",maxSubArraySum(a,sizeof(a)/sizeof(a[0])));
return;
}
int maxSubArraySum(int a[], int size)
{
int max_so_far = 0, max_ending_here = 0;
int i;
int negative=a[0];
for(i = 0; i < size; i++)
{
max_ending_here = max_ending_here + a[i];
if(max_ending_here < 0)
max_ending_here = 0;
else if (max_so_far < max_ending_here)
max_so_far = max_ending_here;
if(max_so_far==0 && negative<a[i])
{
negative=a[i];
}
}
return (max_so_far!=0?max_so_far:negative);
}
int GetMaxSubArray(int[] Numbers, out int start, out int end)
{
int sum = 0;
int maxsum = int.MinValue;
start = 0;
end = 0;
for (int i = 0, j = 0; j < Numbers.Length; j++)
{
sum += Numbers[j];
if (sum > maxsum)
{
maxsum = sum;
start = i;
end = j;
}
if (sum < 0)
{
sum = 0;
i = j + 1;
}
}
return maxsum;
}
public class MaxSubArray {
public static void main(String[] args){
int[] arr = new int[]{-2,4,-3,6,8,-3,2,-4,-1};
MaxSubArray max = new MaxSubArray();
max.subArray(arr);
}
public int subArray(int[] array){
int maxsum =0;
int sum =0;
int start=0;
int end=0;
for(int i=0; i<array.length; i++){
sum = sum + array[i];
if(maxsum < sum){
maxsum=sum;
end=i;
}
if(sum<0){
start=i+1;
sum=0;
}
}
System.out.println("SUM: "+maxsum+", Start Index: "+start+", End Index: "+end);
return maxsum;
}
}
Supose S is the array of integers, N the length and sum the current sum.
Let best be the solution at current step.We keep in x and y the limit of the best array
Initally,best = -INF and sum = 0.
Then
The solution will be the subarray [x,y]. Complexity O(N) time O(1) memory and one loop.
- bogdan.cebere October 24, 2010