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
Is arr and &arr are same expression for an array?
what kind of projects are suitable for c and c++
Describe private, protected and public?
What are guid?
What is using namespace std in cpp?
Is java based off c++?
If you don’t declare a return value, what type of return value is assumed?
True or false, if you keep incrementing a variable, it will become negative a) True b) False c) It depends
What is do..while loops structure?
What is the standard template library (stl)?
Does c++ have arraylist?
What are c++ data types?
Discussion on error handling of C++ .
Where do I find the current c or c++ standard documents?
Difference between pass by value and pass by reference?