ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
tip   SiteMap shows list of All Categories in this site.
Google
 
Categories  >>  Code Snippets  >>  Programming Code  >>  C Code
 
 


 

 
 C Code interview questions  C Code Interview Questions
 C++ Code interview questions  C++ Code Interview Questions
 VC++ Code interview questions  VC++ Code Interview Questions
 Java Code interview questions  Java Code Interview Questions
 Dot Net Code interview questions  Dot Net Code Interview Questions
 Visual Basic Code interview questions  Visual Basic Code Interview Questions
 Programming Code AllOther interview questions  Programming Code AllOther Interview Questions
Question
Program to find the largest sum of contiguous integers in 
the array. O(n)
 Question Submitted By :: Sirc
I also faced this Question!!     Rank Answer Posted By  
 
  Re: Program to find the largest sum of contiguous integers in the array. O(n)
Answer
# 1
#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 ?    6 Yes 7 No
Raghuram.A
 
  Re: Program to find the largest sum of contiguous integers in the array. O(n)
Answer
# 2
#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 ?    5 Yes 4 No
Raghuram.A
 
 
 
  Re: Program to find the largest sum of contiguous integers in the array. O(n)
Answer
# 3
/*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 ?    2 Yes 8 No
Raghuram.A
 
  Re: Program to find the largest sum of contiguous integers in the array. O(n)
Answer
# 4
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 ?    0 Yes 6 No
Raghuram.A
 
  Re: Program to find the largest sum of contiguous integers in the array. O(n)
Answer
# 5
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 ?    1 Yes 4 No
Dilip
 
  Re: Program to find the largest sum of contiguous integers in the array. O(n)
Answer
# 6
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 ?    0 Yes 2 No
Jjaspirin
 
  Re: Program to find the largest sum of contiguous integers in the array. O(n)
Answer
# 7
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 ?    1 Yes 2 No
Somebody
 

 
 
 
Other C Code Interview Questions
 
  Question Asked @ Answers
 
main() { int i = 100; clrscr(); printf("%d", sizeof(sizeof(i))); } a. 2 b. 100 c. 4 d. none of the above HCL2
main( ) { static int a[ ] = {0,1,2,3,4}; int *p[ ] = {a,a+1,a+2,a+3,a+4}; int **ptr = p; ptr++; printf(“\n %d %d %d”, ptr-p, *ptr-a, **ptr); *ptr++; printf(“\n %d %d %d”, ptr-p, *ptr-a, **ptr); *++ptr; printf(“\n %d %d %d”, ptr-p, *ptr-a, **ptr); ++*ptr; printf(“\n %d %d %d”, ptr-p, *ptr-a, **ptr); }  1
main() { char *a = "Hello "; char *b = "World"; clrscr(); printf("%s", strcpy(a,b)); } a. “Hello” b. “Hello World” c. “HelloWorld” d. None of the above HCL1
char *someFun() { char *temp = “string constant"; return temp; } int main() { puts(someFun()); }  1
union u { union u { int i; int j; }a[10]; int b[10]; }u; main() { printf("\n%d", sizeof(u)); printf(" %d", sizeof(u.a)); // printf("%d", sizeof(u.a[4].i)); } a. 4, 4, 4 b. 40, 4, 4 c. 1, 100, 1 d. 40 400 4 HCL1
main() { float f=5,g=10; enum{i=10,j=20,k=50}; printf("%d\n",++k); printf("%f\n",f<<2); printf("%lf\n",f%g); printf("%lf\n",fmod(f,g)); }  1
Is the following code legal? typedef struct a { int x; aType *b; }aType  1
main() { int i=-1; -i; printf("i = %d, -i = %d \n",i,-i); }  1
union u { struct st { int i : 4; int j : 4; int k : 4; int l; }st; int i; }u; main() { u.i = 100; printf("%d, %d, %d",u.i, u.st.i, u.st.l); } a. 4, 4, 0 b. 0, 0, 0 c. 100, 4, 0 d. 40, 4, 0 HCL1
how to return a multiple value from a function? Wipro5
main() { extern i; printf("%d\n",i); { int i=20; printf("%d\n",i); } }  1
Sorting entire link list using selection sort and insertion sort and calculating their time complexity NetApp1
main() { int i=5; printf(“%d”,i=++i ==6); }  1
main() { char i=0; for(;i>=0;i++) ; printf("%d\n",i); }  1
What is "far" and "near" pointers in "c"...?  3
int DIM(int array[]) { return sizeof(array)/sizeof(int ); } main() { int arr[10]; printf(“The dimension of the array is %d”, DIM(arr)); }  1
struct point { int x; int y; }; struct point origin,*pp; main() { pp=&origin; printf("origin is(%d%d)\n",(*pp).x,(*pp).y); printf("origin is (%d%d)\n",pp->x,pp->y); }  1
Write a function to find the depth of a binary tree. Adobe8
Given an array of characters which form a sentence of words, give an efficient algorithm to reverse the order of the words (not characters) in it. Wipro2
write a program to Insert in a sorted list Microsoft4
 
For more C Code Interview Questions Click Here 
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us interview questions urls   External Links 
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com