You're given an array containing both positive and negative
integers and required to find the sub-array with the largest
sum (O(N) a la KBL). Write a routine in C for the above.
Answer Posted / gopika
main()
{
int arr[100],sz,i,max,j,k,sum;
int start,end;
printf("\nEnter size :");
scanf("%d",&sz);
printf("\nEnter elements : ");
for(i=0;i<sz;i++)
scanf("%d",&arr[i]);
max=arr[0];
for(j=1;j<=sz;j++)
for(i=0;i<sz-j+1;i++)
{ sum=0;
for(k=0;k<j;k++)
{
sum+=arr[i+k];
if(sum>max)
{
max=sum;
start=i;
end=i+k;
}
}
}
printf("\nMax is %d\nStart %d\nend %d",max,start,end);
getch();
}
Is This Answer Correct ? | 0 Yes | 1 No |
Post New Answer View All Answers
What is flag in computer?
Which bit wise operator is suitable for checking whether a particular bit is on or off?
What are the advantages of using const reference arguments in a function?
Is the declaration of a class its interface or its implementation?
What is a namespace in c++?
What are the uses of typedef in a program?
What are the uses of static class data?
Write a program to add three numbers in C++ utilizing classes.
What happens when the extern "c" char func (char*,waste) executes?
Write a program using merge () function to combine the elements of array x[ ] and y[ ] into array z[ ].
What can I safely assume about the initial values of variables which are not explicitly initialized?
Why do you use the namespace feature?
What is the output of the following program? Why?
What is data abstraction? How is it different from data encapsulation?
Define stacks. Provide an example where they are useful.