Program to find the largest sum of contiguous integers in
the array. O(n)
Answer Posted / raghuram.a
#include <iostream.h>
#include<conio.h>
main()
{
int s=-1,m=1,n,max=0,e=-1,sum=0,i,a[20];
cout<<"enter n:";
cin>>n;
cout<<"enter elements:";
for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n;i++)
{
if(m==1)
{
if(a[i]>0)
s=i;
m=0;
}
if(a[i]+sum>0)
sum+=a[i];
else
{
m=1;
sum=0;
}
if(sum>max)
{
max=sum;
e=i;
}
}
cout<<"max sum is:"<<max;
cout<<"\n\nmax sum producing sub array is:";
if(s!=-1&&e!=-1)
for(i=s;i<=e;i++)
cout<<a[i]<<"\t";
getch();
return 0;
}
Is This Answer Correct ? | 11 Yes | 13 No |
Post New Answer View All Answers
Write a Program in 'C' To Insert a Unique Number Only. (Hint: Just Like a Primary Key Numbers In Database.) Please Some One Suggest Me a Better Solution for This question ??
How to palindrom string in c language?
write a simple calculator c program to perform addition, subtraction, mul and div.
Write a routine to implement the polymarker function
why do you use macros? Explain a situation where you had to incorporate macros in your proc report? use a simple instream data example with code ?
What is the match merge ? compare data step match merge with proc sql merge - how many types are there ? data step vs proc sql
Set up procedure for generating a wire frame display of a polyhedron with the hidden edges of the object drawn with dashed lines
Given a spherical surface, write bump-mapping procedure to generate the bumpy surface of an orange
What is the difference between proc means and proc tabulate ? explain with a simple example when you have to use means or tabulate?
how to test pierrot divisor
why nlogn is the lower limit of any sort algorithm?
What is full form of PEPSI
How do you verify if the two sentences/phrases input is an anagram using predefined functions in string.h and by using arrays?
What is data _null_? ,Explain with code when u need to use it in data step programming ?
write a program for area of circumference of shapes