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
How does c++ structure differ from c++ class?
How is modularity introduced in C++?
Explain about vectors in c ++?
What is a memory leak c++?
Is c++ a good beginners programming language?
Which one is better- macro or function?
How do you generate a random number in c++?
What is the meaning of string in c++?
Can you please explain the difference between overloading and overriding?
What is operators in c++?
Describe private, protected and public?
Explain the auto storage classes in c++.
What is const pointer and const reference?
Write about the retrieval of n number of objects during the process of delete[]p?
Which command properly allocates memory a) char *a=new char[20]; b) char a=new char[20]; c) char a=new char(20.0);