Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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

Why is standard template library used?

1035


What are keywords in c++?

1085


What is isdigit c++?

1102


What is c++ & why it is used?

1077


What is the error in the code below and how should it be corrected?

749


What are the three forms of cin.get() and what are their differences?

1216


What is the extraction operator and what does it do?

1076


What is stoi in c++?

1162


What is an inline function in c++?

1101


How can an improvement in the quality of software be done by try/catch/throw?

1047


What is the meaning of c++?

1013


What is a modifier in c++?

1130


What is pointer to member?

1071


Explain the difference between realloc() and free() in c++?

1001


Explain the register storage classes in c++.

1184