You're given an array containing both positive and negative
integers and required to find the sub-array with the largest
sum (O(N) a la KBL). Write a routine in C for the above.

Answer Posted / sujan

#include<iostream>
#define SIZE 16
using namespace std;
int main()
{
int a[SIZE] = {-3, 5, -9, 4, -6, -24, -13, -14, -3, -20,
-45, -11, -2, -8, 1,10};
int temp[SIZE];
int j=0,sum=0;
for(int i=0;i<=SIZE;i++)
{
if(a[i]>0)
{
temp[j]=a[i];

j++;
}

}
cout<<"Sub-array:";
for(int k=0;k<j-1;k++)
{
sum+=temp[k];
cout<<temp[k]<<"\t";
}


cout<<"\n"<<"Sum:"<<sum<<endl;


system("pause");
}

Is This Answer Correct ?    3 Yes 31 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Is arr and &arr are same expression for an array?

684


what kind of projects are suitable for c and c++

712


Describe private, protected and public?

687


What are guid?

796


What is using namespace std in cpp?

641






Is java based off c++?

615


If you don’t declare a return value, what type of return value is assumed?

633


True or false, if you keep incrementing a variable, it will become negative a) True b) False c) It depends

1998


What is do..while loops structure?

716


What is the standard template library (stl)?

729


Does c++ have arraylist?

638


What are c++ data types?

734


Discussion on error handling of C++ .

734


Where do I find the current c or c++ standard documents?

689


Difference between pass by value and pass by reference?

697