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
how to test pierrot divisor
How to palindrom string in c language?
What is data _null_? ,Explain with code when u need to use it in data step programming ?
#include
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
Given a spherical surface, write bump-mapping procedure to generate the bumpy surface of an orange
could you please send the program code for multiplying sparse matrix in c????
What is the match merge ? compare data step match merge with proc sql merge - how many types are there ? data step vs proc sql
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.
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 ??
Write a routine to implement the polymarker function
How can you relate the function with the structure? Explain with an appropriate example.
Write a program to model an exploding firecracker in the xy plane using a particle system
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']
write a program for area of circumference of shapes