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 the difference between while and do while loop? Explain with examples.
What is a c++ class?
Am pass the 10000 records to target in target I will take commit interval 15000 when I was stop the work flow what will happened
Write about the role of c++ in the tradeoff of safety vs. Usability?
How do I use arrays in c++?
What are the different types of polymorphism in c++?
Can static member variables be private?
What is bubble sort c++?
If you push the numbers (in order) 1, 3, and 5 onto a stack, which pops out first a) 1 b) 5 c) 3
Describe new operator?
What is #include cmath?
What is the purpose of the "delete" operator?
Declare a class vehicle and make it an abstract data type.
What are the advantages of pointers?
How java is different from c and c++?