Question

C++ max_subarray(Arr): return a contiguous subarray within Arr which has the largest sum.

C++
max_subarray(Arr): return a contiguous subarray within Arr which has the largest sum.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution

#include<iostream>
#include<climits>
using namespace std;
  
int max_subarray(int Arr[], int size)
{
int max_so_far = INT_MIN, max_ending_here = 0;
  
for (int i = 0; i < size; i++)
{
max_ending_here = max_ending_here + Arr[i];
if (max_so_far < max_ending_here)
max_so_far = max_ending_here;
  
if (max_ending_here < 0)
max_ending_here = 0;
}
return max_so_far;
}
  
/* to test max_subarray*/
int main()
{
int Arr[] = {-2, -3, 4, -1, -2, 1, 5, -3};
int n = sizeof(Arr)/sizeof(Arr[0]);
int max_sum = max_subarray(Arr, n);
cout << "Maximum contiguous sum is " << max_sum;
return 0;
}

Screenshot

---

If there is anything missing or anything you want to add, please mention it

love to help

all the best

Add a comment
Know the answer?
Add Answer to:
C++ max_subarray(Arr): return a contiguous subarray within Arr which has the largest sum.
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write out C++ code for the following: Declare a templated function that has a return type...

    Write out C++ code for the following: Declare a templated function that has a return type of T". It has two parameters: An array of T items named arr, and an integer named size. This function is assumed to work with numerical data types. Within the function, create a new T variable named sum and initialize it to 0. Use a for-loop to go from 0 (inclusive) to size (exclusive), adding each element from arr to the sum. Finally, return...

  • Consider a non-empty int array ints. A contiguous subarray ints[ start .. start + len -1...

    Consider a non-empty int array ints. A contiguous subarray ints[ start .. start + len -1 ] (with starting index start and length len) is called a flat if all elements of that subarray are equal. Furthermore, such a subarray is called a plateau if it is flat and each of the elements ints[start -1] and ints[start + len] that immediately proceed/succeed the subarray are either nonexistent (i.e., out of array’s index range) or are strictly smaller than the elements...

  • Assume that arr[] is an array of 5 values. Write code within the FOR loop below...

    Assume that arr[] is an array of 5 values. Write code within the FOR loop below so that it finds the min value in the array. You MUST use the x pointer within the FOR loop. It is the only pointer you can use. You are not allowed to made additional function calls or modify anything else within the function. Do not modify the FOR loop conditions or anything outside of the FOR loop. You must use complete C++ code...

  • Description An array in C++ is a collection of items stored at contiguous memory locations and...

    Description An array in C++ is a collection of items stored at contiguous memory locations and elements can be accessed randomly using indices of an array. They are used to store similar type of elements as in the data type must be the same for all elements. One advantage of arrays is easy data manipulation and accessibility of elements stored in consecutive locations. The Problem: One teaching assistant of a computer science department in Engineering University got a simple question...

  • I need help fixing my code: In C++ *************** 1) I want to sum the digits...

    I need help fixing my code: In C++ *************** 1) I want to sum the digits of an n*n matrix 2) find the average I have completed the rest ****Do not use C++ standard library. You must use pointers and pointer arithmetic to represent the matrix and to navigate through it. MY CODE: (I have indicated at which point I need help) #include <iostream> using namespace std; void swap(int *xp, int *yp) { int temp = *xp; *xp = *yp;...

  • Step 4: Write a Sum Function Since we can write, compile and run simple c files,...

    Step 4: Write a Sum Function Since we can write, compile and run simple c files, lets add a bit to make a program that will sum an entire array of integers. To do this we are going to simply overwrite the main.c file to include the new function. The sum function below is not complete and must be finished before the program will execute properly. %%file main.c #include <stdio.h> int sum(int array[], int arrayLength) {     int i =...

  • C. What is the running time for the following approaches to solving the max sum of...

    C. What is the running time for the following approaches to solving the max sum of a consecutive subset of the array? Approach 1. initialize the max sum to be the first element in the array for a subset of size 1 for each consecutive subset of that size, calc the sum of the subset if the subset is larger than the current max, update the current max repeat for subset sizes 2 through n Approach 2. Divide the array...

  • Decrease-by-Half Algorithm We can solve the same problem using a decrease-by-half algorithm. This...

    Convert the pseudocode into a C++ function Decrease-by-Half Algorithm We can solve the same problem using a decrease-by-half algorithm. This algorithm is based on the following ideas: In the base case, n 1 and the only possible solution is b 0, e 1 In the general case, divide V into a left and rnight half; then the maximum subarray can be in one of three places: o entirely in the left half; o entirely in the right half; or o...

  • 10 points 2. Find the largest possible open rectangular box within tyy'-space in which the existe...

    Please give the solution (how you get an answer) not only an answer. 10 points 2. Find the largest possible open rectangular box within tyy'-space in which the existence and uniqueness theorem applies to the nonlinear initial value problem and thereby infer that it has a unique local solution. ; y(0)-1 , y'(0) 2 2 10 points 2. Find the largest possible open rectangular box within tyy'-space in which the existence and uniqueness theorem applies to the nonlinear initial value...

  • Consider the following C program: int sub(int *sum) { *sum =*sum +*sum; return 10; } void...

    Consider the following C program: int sub(int *sum) { *sum =*sum +*sum; return 10; } void main() { int num= 3; num = sub(&num)+ num; } What is the value of num after the assignment statement in main, assuming operands are evaluated left to right. operands are evaluated right to left.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT