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


Please Help Members By Posting Answers For Below Questions

What is flag in computer?

685


Which bit wise operator is suitable for checking whether a particular bit is on or off?

695


What are the advantages of using const reference arguments in a function?

706


Is the declaration of a class its interface or its implementation?

799


What is a namespace in c++?

707






What are the uses of typedef in a program?

694


What are the uses of static class data?

737


Write a program to add three numbers in C++ utilizing classes.

721


What happens when the extern "c" char func (char*,waste) executes?

724


Write a program using merge () function to combine the elements of array x[ ] and y[ ] into array z[ ].

690


What can I safely assume about the initial values of variables which are not explicitly initialized?

714


Why do you use the namespace feature?

747


What is the output of the following program? Why?

720


What is data abstraction? How is it different from data encapsulation?

617


Define stacks. Provide an example where they are useful.

679