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

How does c++ structure differ from c++ class?

577


How is modularity introduced in C++?

763


Explain about vectors in c ++?

591


What is a memory leak c++?

583


Is c++ a good beginners programming language?

578






Which one is better- macro or function?

644


How do you generate a random number in c++?

601


What is the meaning of string in c++?

566


Can you please explain the difference between overloading and overriding?

588


What is operators in c++?

561


Describe private, protected and public?

592


Explain the auto storage classes in c++.

593


What is const pointer and const reference?

585


Write about the retrieval of n number of objects during the process of delete[]p?

567


Which command properly allocates memory a) char *a=new char[20]; b) char a=new char[20]; c) char a=new char(20.0);

612