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                      
Do you have a collection of Interview Questions and interested to share with us!!
Please send that collection to along with your userid / name. ThanQ
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 ?    4 Yes 2 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 ?    2 Yes 1 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 ?    1 Yes 4 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 2 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 1 No
Dilip
 

 
 
 
Other C Code Interview Questions
 
  Question Asked @ Answers
 
To Write a C program to remove the repeated characters in the entered expression or in entered characters(i.e) removing duplicates. Synergy2
How do you write a program which produces its own source code as its output?  4
How can u say that a given point is in a triangle? 1. with the co-ordinates of the 3 vertices specified. 2. with only the co-ordinates of the top vertex given.  1
Link list in reverse order. NetApp7
how can u draw a rectangle in C Wipro24
How we will connect multiple client ? (without using fork,thread) TelDNA2
Sorting entire link list using selection sort and insertion sort and calculating their time complexity NetApp1
How to reverse a String without using C functions ? Wipro8
how to return a multiple value from a function? Wipro4
Program to Delete an element from a doubly linked list. Infosys4
How to swap two variables, without using third variable ? HCL28
Program to find the largest sum of contiguous integers in the array. O(n)  5
Write a routine that prints out a 2-D array in spiral order Microsoft2
Find the largest number in a binary tree Infosys4
Finding a number multiplication of 8 with out using arithmetic operator NetApp7
What is "far" and "near" pointers in "c"...?  2
How do you sort a Linked List (singly connected) in O(n) please mail to pawan.10k@gmail.com if u can find an anser...i m desperate to knw... Oracle3
Give a very good method to count the number of ones in a 32 bit number. (caution: looping through testing each bit is not a solution) Microsoft4
Write a program that find and print how many odd numbers in a binary tree  1
Finding a number which was log of base 2 NetApp1
 
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