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

Answers were Sorted based on User's Feedback



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

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

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

Answer / raghuram.a

#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

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

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

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

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

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

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

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

Answer / 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

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

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

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

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

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

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

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

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

Post New Answer

More C Code Interview Questions

main() { int c[ ]={2.8,3.4,4,6.7,5}; int j,*p=c,*q=c; for(j=0;j<5;j++) { printf(" %d ",*c); ++q; } for(j=0;j<5;j++){ printf(" %d ",*p); ++p; } }

1 Answers  


what is variable length argument list?

2 Answers  


C program to print magic square of order n where n > 3 and n is odd

2 Answers   Accenture,


Write a single line c expression to delete a,b,c from aabbcc

2 Answers   Microsoft,


#include <stdio.h> #define a 10 main() { #define a 50 printf("%d",a); }

2 Answers  






void main() { if(~0 == (unsigned int)-1) printf(“You can answer this if you know how values are represented in memory”); }

1 Answers  


How we print the table of 3 using for loop in c programing?

7 Answers  


Write a routine to implement the polymarker function

0 Answers   TCS,


char *someFun1() { char temp[ ] = “string"; return temp; } char *someFun2() { char temp[ ] = {‘s’, ‘t’,’r’,’i’,’n’,’g’}; return temp; } int main() { puts(someFun1()); puts(someFun2()); }

2 Answers  


main() { main(); }

1 Answers  


func(a,b) int a,b; { return( a= (a==b) ); } main() { int process(),func(); printf("The value of process is %d !\n ",process(func,3,6)); } process(pf,val1,val2) int (*pf) (); int val1,val2; { return((*pf) (val1,val2)); }

1 Answers   Satyam,


What is the main difference between STRUCTURE and UNION?

13 Answers   HCL,


Categories