Program to find the largest sum of contiguous integers in
the array. O(n)
Answers were Sorted based on User's Feedback
Answer / 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 |
#include <iostream.h>
#include<conio.h>
/*prints subarray producing maximum sum..Efficiency is O(n)*/
main()
{
int flag=0,n,i,c=0,sum=0,max=0,a[25],s=-1,e=-1;
cout<<"enter n:";
cin>>n;
cout<<"enter elements:";
for(i=1;i<=n;i++)
cin>>a[i];
for(i=1;i<=n;i++)
{
if(a[i]+sum>0)
sum+=a[i];
else
{
sum=0;
if(c!=0)
c=1;
}
if(sum>max)
{
max=sum;
e=i;
s=e-c;
c++;
}
}
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<<"\t"<<a[i];
getch();
return 0;
}
Is This Answer Correct ? | 11 Yes | 9 No |
Answer / saurabh
#include<stdio.h>
#include<stdlib.h>
int main()
{
int arr[50];
int i,sum1,sum2,n;
int flag;
//int front1,front2,rear1,rear2;
printf("How many elements do you want to enter: ");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
sum1=arr[0];
sum2=arr[0];
if(arr[0]<0)
flag=1;
else
flag=0;
for(i=1;i<n;i++)
{
if(flag==1)
{
if((arr[i]>0&&arr[i-1]<0)||(arr[i]<0&&arr[i-1]<0&&arr[i]>arr[i-1]))
sum2=0;
if(arr[i]<0&&arr[i-1]<0&&arr[i]<arr[i-1])
{
int temp=arr[i];
arr[i]=arr[i-1];
arr[i-1]=arr[i];
sum2=0;
}
if(arr[i]<0&&arr[i-1]>0&&(arr[i-1]+arr[i])<0)
sum1=arr[i-1];
}
sum2=sum2+arr[i];
if(sum2>sum1)
{
sum1=sum2;
flag=0;
}
if(sum2<0)
{
sum2=0;
sum2=sum2+arr[i];
flag=1;
}
}
//printf("%d",rear);
if(sum1>=sum2)
printf("Largest subarray sum= %d\n",sum1);
else
printf("Largest subarray sum= %d\n",sum2);
return 0;
}
Is This Answer Correct ? | 2 Yes | 0 No |
Answer / jjaspirin
I came to this website looking for the solution and finally
ended up writing it myself. Hope this is useful for someone
else.
Here is a working solution. Set the array "a" and N_ELEMENTS
accordingly. Some cases are not covered but should be an
easy fix.
#include <stdio.h>
#define N_ELEMENTS 7
int main() {
int a[N_ELEMENTS] = {-1, 2, -3, 2, 0, 5, -11 }; // if
you change the array, make sure you change N_ELEMENTS
int i = 0;
while(a[i] < 0 && i<N_ELEMENTS) {
i++;
}
if (a[i] < 0) {
printf ("DEBUG: array with only negative numbers.
Print the smallest negative number as the sum and we are
done.\n");
}
int sum_p=0, sum_n = 0;
int largest_sum = 0;
while (i<N_ELEMENTS) {
if (a[i] > 0) {
sum_p += a[i];
}
else {
sum_n += a[i];
}
if (sum_p+sum_n > largest_sum) {
largest_sum = sum_p + sum_n;
}
if (sum_p+sum_n <= 0) {
// find the next positive number
while(a[i] < 0 && i<N_ELEMENTS) {
i++;
}
if (a[i] < 0 || i == N_ELEMENTS) {
break;
}
sum_p = 0;
sum_n = 0;
} else {
i++;
}
}
printf ("DEBUG: The largest consecutive sum = %d\n",
largest_sum);
}
Is This Answer Correct ? | 3 Yes | 2 No |
Answer / somebody
Guys..Answer 3 is correct..When there are -ve
numbers..maximum sum is 0.U should not consider any elements
at all..Coz null set which has maximum sum.i.e 0.So the 4th
ans is right.
Is This Answer Correct ? | 2 Yes | 2 No |
#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 |
Answer / dilip
i think ths program doesnt not work when the sum is
negative.. cant we not work around it by setting the max as
somelarge negative value and comparing with that??
Is This Answer Correct ? | 3 Yes | 5 No |
Answer / deepika jadav
the cod is written in c#...similar to java and c.....so interpret it accordingly..
static void Main(string[] args)
{
//int[] arr={2,3,-5,-2,-2,5,6,7,8,-2,3,4};
int[] arr = {-2,-3,-4,-5,-6,-7 };
int max=0;
int sum=0;
int min = 0;
int flag = 0;
for (int i = 0; i < arr.Length;i++)
{
if (arr[i] < 0)
{
sum = 0;
flag++;
if(flag==1)
min = arr[i];
if (flag == arr.Length)
Console.WriteLine("The smallest no amogst negative nos is {0} hence the largest aum is {1}",min,min);
if (min < arr[i])
min = arr[i];
continue;
}
else
{
sum = sum + arr[i];
}
if (max < sum)
max = sum;
}
if(flag!=arr.Length)
Console.WriteLine("the Largest Sum is {0}",max);
Console.ReadLine();
}
Is This Answer Correct ? | 0 Yes | 2 No |
Answer / raghuram.a
Actually I have posted 3 answers..Bcoz after posting twice I
found there are some logical errors.Last one is fully
correct..If not please let me know..
Is This Answer Correct ? | 2 Yes | 7 No |
Answer / raghuram.a
/*prints subarray producing maximum sum.Efficiency is O(n)*/
#include <iostream.h>
#include<conio.h>
main()
{
int n,i,c=0,sum=0,max=0,a[25],s=-1,e=-1;
cout<<"enter n:";
cin>>n;
cout<<"enter elements:";
for(i=1;i<=n;i++)
cin>>a[i];
for(i=1;i<=n;i++)
{
if(a[i]+sum>0)
{
sum+=a[i];
if(sum>=max)
s=i-c;
c++;
}
else
{
sum=0;
c=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<<"\t"<<a[i];
getch();
return 0;
}
Is This Answer Correct ? | 4 Yes | 10 No |
What is the difference between proc means and proc tabulate ? explain with a simple example when you have to use means or tabulate?
write a c-program to find gcd using recursive functions
#include<stdio.h> int main() { int a=3,post,pre; post= a++ * a++ * a++; a=3; pre= ++a * ++a * ++a; printf("post=%d pre=%d",post,pre); return 0; }
Write a program to check whether the number is prime and also check if it there i n fibonacci series, then return true otherwise return false
main() { int x=5; clrscr(); for(;x<= 0;x--) { printf("x=%d ", x--); } } a. 5, 3, 1 b. 5, 2, 1, c. 5, 3, 1, -1, 3 d. –3, -1, 1, 3, 5
Program to Delete an element from a doubly linked list.
4 Answers College School Exams Tests, Infosys,
main() { int *j; { int i=10; j=&i; } printf("%d",*j); }
write a simple calculator c program to perform addition, subtraction, mul and div.
0 Answers United Healthcare, Virtusa,
#include<stdio.h> main() { const int i=4; float j; j = ++i; printf("%d %f", i,++j); }
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 ?
Cluster head selection in Wireless Sensor Network using C programming language.
What is the problem with the following code segment? while ((fgets(receiving array,50,file_ptr)) != EOF) ;