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


Please Help Members By Posting Answers For Below Questions

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 ??

1770


How to palindrom string in c language?

8836


write a simple calculator c program to perform addition, subtraction, mul and div.

3143


Write a routine to implement the polymarker function

4375


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 ?

2258






What is the match merge ? compare data step match merge with proc sql merge - how many types are there ? data step vs proc sql

2405


Set up procedure for generating a wire frame display of a polyhedron with the hidden edges of the object drawn with dashed lines

3026


Given a spherical surface, write bump-mapping procedure to generate the bumpy surface of an orange

2857


What is the difference between proc means and proc tabulate ? explain with a simple example when you have to use means or tabulate?

3708


how to test pierrot divisor

2256


why nlogn is the lower limit of any sort algorithm?

2373


What is full form of PEPSI

1865


How do you verify if the two sentences/phrases input is an anagram using predefined functions in string.h and by using arrays?

2019


What is data _null_? ,Explain with code when u need to use it in data step programming ?

2820


write a program for area of circumference of shapes

2028