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 default access level?
Explain what happens when a pointer is deleted twice?
What is an iterator class in c++?
What are the extraction and insertion operators in c++?
What is the difference between new() and malloc()?
What are friend classes? What are advantages of using friend classes?
What is the full form of india?
What are register variables?
What is a modifier in c++?
What are advantages of c++?
Write a program to find the Factorial of a number
What is the auto keyword good for in c++?
If a base class declares a function to be virtual, and a derived class does not use the term virtual when overriding that class, is it still virtual when inherited by a third-generation class?
In which header file does one find isalpha() a) conio.h b) stdio.h c) ctype.h
What are disadvantages of pointers?