Program to find the largest sum of contiguous integers in
the array. O(n)

Answer Posted / senthilkumar

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int lsum,sum,i,j,n,a[20],flag=0;
lsum=0;sum=0;
cout<<"Enter the total no. of elements:";
cin>>n;

for(i=0;i<n;i++)
cin>>a[i];

//Checking condition whether there are only negative numbers
for(i=0;i<n;i++)
{
if(a[i]>0)
flag++;
}
//for array including positive and negative numbers
if(flag>0)
{
for(i=0;i<n;i++)
{
sum+=a[i];
if(sum<0)
{
sum=0;
j++;
i=j;
}
else if(lsum<sum)
lsum=sum;
}
}
//for array having only negative numbers
else
{
lsum=a[0];
for(i=0;i<n;i++)
{
if(lsum<a[i])
lsum=a[i];
}
}
cout<<"The largest sum is:"<<lsum;
getch();
}

Is This Answer Correct ?    6 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

how to test pierrot divisor

2457


How to palindrom string in c language?

9779


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

3012


#include int main(void) { int a=4, b=2; a=b<>2 ; printf("%d",a); return 0; }

1293


can you use proc sql to manpulate a data set or would u prefer to use proc report ? if so why ? make up an example and explain in detail

2564






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

3073


could you please send the program code for multiplying sparse matrix in c????

3276


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

2627


How can I Create a C program in splitting set of characters to specific subsets. Example: INPUT SET OF CHARACTERS: Therefore, my dear brothers and sisters, stand firm. Let nothing move you. Always give yourselves fully to the work of the Lord, because you know that your labor in the Lord is not in vain. SPLIT INTO HOW MANY CHARACTERS PER SUBSETS: 10 OUTPUT: Therefore, my dear b rothers an d sisters, stand fir m. Let not hing move you. Alway s give you rselves fu lly to the work of t he Lord, b ecause you know that your labo r in the L ord is not in vain.

2160


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

2128


Write a routine to implement the polymarker function

4581


How can you relate the function with the structure? Explain with an appropriate example.

3126


Write a program to model an exploding firecracker in the xy plane using a particle system

3866


To Write a C program to remove the repeated characters in the entered expression or in entered characters(i.e) removing duplicates. String contains only lowercase characters ['a'-'z']

729


write a program for area of circumference of shapes

2225