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

What is long in c++?

731


Explain the differences between private, public and protected and give examples.

564


What is c++ stringstream?

597


what are the events occur in intr activated on interrupt vector table

1174


What data encapsulation is in c++?

589






What is the difference between the functions memmove() and memcpy()?

621


What do you mean by enumerated data type?

568


How do you invoke a base member function from a derived class in which you have not overridden that function?

578


write a program that withdrawals,deposits,balance check,shows mini statement. (using functions,pointers and arrays)

1802


What are the manipulators in c++?

545


What are the two main components of c++?

582


Write a program using shift_half( ) function to shift the elements of first half array to second half and vice versa.

781


Write about all the implicit member functions of a class?

590


Is string an object in c++?

652


What is the use of setfill in c++?

577