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
Differentiate between the manipulator and setf( ) function?
What is the difference between global variables and local variable
Why cstdlib is used in c++?
What is data structure in c++?
Which sort does c++ use?
Explain what is polymorphism in c++?
Write about c++ storage classes?
What are guid?
What is prototype in c++ with example?
What are multiple inheritances (virtual inheritance)?
What does std mean in c++?
Which programming language should I learn first?
How would you find out if a linked-list is a cycle or not?
What is prototype for that c string function?
What is c strings syntax?