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
What is a breakpoint?
What is c++ coding?
Define whitespace in C++.
Is c++ slower than c?
How do you print a string on the printer?
Which is the best c++ software?
Can you Mention some Application of C/C++?
Define the process of handling in case of destructor failure?
How to access a variable of the structure?
What is setbase c++?
How to declaring variables in c++?
Is c++ proprietary?
What programming language should I learn first?
What do you mean by ‘void’ return type?
Explain what are mutator methods in c++?