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 general form of pure virtual function? Explain?
Explain some examples of operator overloading?
Is vector a class in c++?
Is java a c++?
What is a virtual destructor? Explain the use of it?
can any one help to find a specific string between html tags
which is changed to a sting..
weather.html looks (for location) is
Write about the scope resolution operator? Can I have a reference as a data member of a class? If yes, then how do I initialise it? What is the best ide for c++? What is c++ runtime? How can you quickly find the number of elements stored in a a) static array b) dynamic array ? Why is it difficult to store linked list in an array?how can you find the nodes with repetetive data in a linked list? Define 'std'. How is computer programming useful in real life? How do you clear a set in c++? What does the nocreate and noreplace flag ensure when they are used for opening a file?