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
Why is standard template library used?
What are keywords in c++?
What is isdigit c++?
What is c++ & why it is used?
What is the error in the code below and how should it be corrected?
What are the three forms of cin.get() and what are their differences?
What is the extraction operator and what does it do?
What is stoi in c++?
What is an inline function in c++?
How can an improvement in the quality of software be done by try/catch/throw?
What is the meaning of c++?
What is a modifier in c++?
What is pointer to member?
Explain the difference between realloc() and free() in c++?
Explain the register storage classes in c++.