![makiasdillard0925](/avatars/41015.jpg)
makiasdillard0925
18.10.2019 •
Computers and Technology
Write an efficient c++ program to find the sum of contiguous subarray within a vector of numbers which has the largest sum.
function definition
int maxsum(vector a)
Solved
Show answers
More tips
- F Food and Cooking Kick Your Taste Buds Into Gear with This Authentic and Delicious Recipe for Solyanka...
- O Other What happens if you get scared half to death twice?...
- F Family and Home What s That Noise When a Kettle Boils? The Science of Water and Steam...
- L Leisure and Entertainment History of International Women s Day: When Did the Celebration of March 8th Begin?...
- W Work and Career How much does an honest traffic police officer earn in a day?...
- F Food and Cooking Red Caviar: How to Choose the Best?...
- S Style and Beauty How to Get Rid of a Bruise: Tips and Tricks...
- H Health and Medicine Is Massage Necessary? Facts and Opinions...
- L Leisure and Entertainment Should You Buy a Ceramic Knife?...
- C Computers and Internet Best Antivirus: How to Choose Protection for Your Computer?...
Answers on questions: Computers and Technology
- C Computers and Technology Write short notes about monitor printer and speaker...
- C Computers and Technology What is the total March Expenditure now? (cell Q2)...
- C Computers and Technology 1. In Fig. 2-2, three process states are shown. In theory, with three states, there could be six transitions, two out of each state. However, only four transitions are...
- B Biology How can air pollutants get into your blood...
- P Physics Circle the letter of each term for the difference in electrical potential energy between two places in a circuit a. current b. voltage c.voltage source d.potential difference...
- E English Ineed a topic for a research paper on the boy band one direction. should i do like the history of them? or their families? or type of music? or what? (don t send me hate....
- S Social Studies Which group of ppl do politicians pay the most attention to in elections?...
- C Chemistry Radioisotopes tell scientists that earth is how old? a. 65 million years b. 500 million years c. 4.5 billion years d. 13.4 billion years...
- A Arts The center of power for the western part of the roman empire was...
- H History Improved the lives of the russian serfs...
Ответ:
The program to this question can be given as:
Program:
#include<cstdio> //include header file.
#include<vector>
using namespace std;
int maxSum(vector <int>a); //function declaration
int maxSum(vector <int>a) //function definition
{
int i, n=a.size(),max_fast = 0,max_end= 0; //declare variable
for(i = 0; i < n; i++) //for loop
{
max_end = max_end+ a[i]; //add value in max_end
if(max_end < 0) //check value
{
max_end = 0;
}
if(max_fast < max_end)
{
max_fast = max_end; //assign value to max_fast
}
}
return max_fast; //return value.
}
int main() //main function.
{
vector<int> a;
//insert values.
a.push_back(-2);
a.push_back(-3);
a.push_back(4);
a.push_back(-1);
a.push_back(-2);
a.push_back(1);
a.push_back(5);
a.push_back(-3);
int max_sum; //define variable
max_sum = maxSum(a); //calling function.
printf("Maximum contiguous sum is %d\n", max_sum); //print value.
return 0;
}
Output:
Maximum contiguous sum is: 7
Explanation:
In the above program firstly we include header files. the we declare the function that name is already given in the question. In that function we declare variable and assign a value to variable. Then we declare the loop that is used for add the value of max_end. In this loop we use the multiple if statements in first if block we check that the value of max_end less then 0. When the condition is true max_end =0.In second if block we check that max_fast value is less then max_end. If it is true so we assign max_fast=max_end and return max_fast value. In the last we declare the main function .In this we assign the value in array and pass the array into the function and call the function.The ma_sum variable hold the return values of the function and in the last we print max_sum value.
Ответ: