Question

In: Computer Science

implement Max Subarray Problem via Divide-and-conquer off this code #include <iostream> #define MAX_INT 2147483647 using namespace...

implement Max Subarray Problem via Divide-and-conquer off this code

#include <iostream>
#define MAX_INT 2147483647
using namespace std;

int main(int argc,char **argv) {

int* array;
int arraySize = 1;


// Get the size of the sequence
cin >> arraySize;
array = new int[arraySize];

// Read the sequence
for(int i=0; i<arraySize; i++){

cin >> array[i];

}

Solutions

Expert Solution

//C++ program

#include <iostream>
#define MAX_INT 2147483647
using namespace std;

int max(int,int);
int CrossingSum(int * , int ,int,int);
int SubArraySum(int* , int,int);

int main(int argc,char **argv) {

int* array;
int arraySize = 1;


// Get the size of the sequence
cin >> arraySize;
array = new int[arraySize];

// Read the sequence
for(int i=0; i<arraySize; i++){

cin >> array[i];

}

cout<<"Maximum subArray sum : "<<SubArraySum(array,0,arraySize-1)<<"\n";
return 0;

}

int max(int a, int b) {
if(a>b)return a;
return b;
}
  
int SubArraySum(int array[], int low, int high)
{
if (low == high)
return array[low];
  
int mid = (low + high)/2;
  
return max(max(SubArraySum(array, low, mid), SubArraySum(array, mid+1, high)), CrossingSum(array, low, mid, high));
}

  
int CrossingSum(int array[], int l, int m, int h)
{
int sum = 0;
int left_half_sum = INT_MIN;
for (int i = m; i >= l; i--)
{
sum = sum + array[i];
if (sum > left_half_sum)
left_half_sum = sum;
}
  
sum = 0;
int right_half_sum = INT_MIN;
for (int i = m+1; i <= h; i++)
{
sum = sum + array[i];
if (sum > right_half_sum)
right_half_sum = sum;
}
  
return left_half_sum + right_half_sum;
}
  


Related Solutions

Do an insertion sort based off this code #include <iostream> #define MAX_INT 2147483647 using namespace std;...
Do an insertion sort based off this code #include <iostream> #define MAX_INT 2147483647 using namespace std;    int main(int argc,char **argv) { int* Sequence; int arraySize = 1; // Get the size of the sequence cin >> arraySize; Sequence = new int[arraySize];    // Read the sequence for(int i=0; i<arraySize; i++) cin >> Sequence[i];
implement heap sort algorithm continuing off this #include <iostream> #define MAX_INT 2147483647 using namespace std; int...
implement heap sort algorithm continuing off this #include <iostream> #define MAX_INT 2147483647 using namespace std; int main(int argc,char **argv) { int* array; int arraySize = 1; // Get the size of the sequence cin >> arraySize; array = new int[arraySize]; // Read the sequence for(int i=0; i<arraySize; i++){ cin >> array[i]; }
--- TURN this Code into Java Language --- #include <iostream> #include <string> using namespace std; //...
--- TURN this Code into Java Language --- #include <iostream> #include <string> using namespace std; // constants const int FINAL_POSITION = 43; const int INITIAL_POSITION = -1; const int NUM_PLAYERS = 2; const string BLUE = "BLUE"; const string GREEN = "GREEN"; const string ORANGE = "ORANGE"; const string PURPLE = "PURPLE"; const string RED = "RED"; const string YELLOW = "YELLOW"; const string COLORS [] = {BLUE, GREEN, ORANGE, PURPLE, RED, YELLOW}; const int NUM_COLORS = 6; // names...
Implement the following functions for an array based stack. #include <stdexcept> #include <iostream> using namespace std;...
Implement the following functions for an array based stack. #include <stdexcept> #include <iostream> using namespace std; #include "Stack.h" template <typename DataType> class StackArray : public Stack<DataType> { public: StackArray(int maxNumber = Stack<DataType>::MAX_STACK_SIZE); StackArray(const StackArray& other); StackArray& operator=(const StackArray& other); ~StackArray(); void push(const DataType& newDataItem) throw (logic_error); DataType pop() throw (logic_error); void clear(); bool isEmpty() const; bool isFull() const; void showStructure() const; private: int maxSize; int top; DataType* dataItems; }; //-------------------------------------------------------------------- // // // ** Array implementation of the Stack ADT...
Complete the C++ code #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; struct Cell {...
Complete the C++ code #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; struct Cell { int val; Cell *next; }; int main() { int MAX = 10; Cell *c = NULL; Cell *HEAD = NULL; srand (time(NULL)); for (int i=0; i<MAX; i++) { // Use dynamic memory allocation to create a new Cell then initialize the // cell value (val) to rand(). Set the next pointer to the HEAD and // then update HEAD. } print_cells(HEAD); }
I need the following code completed: #include <iostream> #include <time.h> #include "CSVparser.hpp" using namespace std; //============================================================================...
I need the following code completed: #include <iostream> #include <time.h> #include "CSVparser.hpp" using namespace std; //============================================================================ // Global definitions visible to all methods and classes //============================================================================ // forward declarations double strToDouble(string str, char ch); // define a structure to hold bid information struct Bid { string bidId; // unique identifier string title; string fund; double amount; Bid() { amount = 0.0; } }; // FIXME (1): Internal structure for tree node struct Node {    Bid data; Node *left; Node...
LargestTwo problem: Based upon the pseudocode code below, implement in C++ a divide-and-conquer algorithm that finds...
LargestTwo problem: Based upon the pseudocode code below, implement in C++ a divide-and-conquer algorithm that finds the largest and second largest value from a vector of ints. void LargestTwo (vector l, int left, int right, int & largest, int & secondLargest) • Please write comment for your functions, similar to the one in the pseudocode, to include both pre- and post-conditions. • Comment on the logic of your code, e.g., what is true after the two recursive calls? • Answer...
write the algorithm for this the code?!. #include<iostream> using namespace std; #include<string.h> int main() { char...
write the algorithm for this the code?!. #include<iostream> using namespace std; #include<string.h> int main() { char plain[50], cipher[50]="", decrypt[50]=""; int subkeys[50], len;       cout<<"Enter the plain text:"<<endl; cin>>plain;    cout<<"Enter the first subkey:"<<endl; cin>>subkeys[0];    _strupr(plain);    len = strlen(plain);    /**********Find the subkeys**************/    for(int i=1; i<len; i++) { if ((plain[i-1]>='A') && (plain[i-1]<='Z')) { subkeys[i] = plain[i-1]-65; } }    /****************ENCRYPTION***************/       for(int i=0; i<len; i++) { if ((plain[i]>='A') && (plain[i]<='Z')) {    cipher[i] = (((plain[i]-65)+subkeys[i])%26)+65; }...
C++ finish the AVL Tree code: #include "AVLNode.h" #include "AVLTree.h" #include <iostream> #include <string> using namespace...
C++ finish the AVL Tree code: #include "AVLNode.h" #include "AVLTree.h" #include <iostream> #include <string> using namespace std; AVLTree::AVLTree() { root = NULL; } AVLTree::~AVLTree() { delete root; root = NULL; } // insert finds a position for x in the tree and places it there, rebalancing // as necessary. void AVLTree::insert(const string& x) { // YOUR IMPLEMENTATION GOES HERE } // remove finds x's position in the tree and removes it, rebalancing as // necessary. void AVLTree::remove(const string& x) {...
What is the flowchart for this code. Thank You! #include<iostream> #include<iomanip> #include<string> #include<cmath> using namespace std;...
What is the flowchart for this code. Thank You! #include<iostream> #include<iomanip> #include<string> #include<cmath> using namespace std; float series(float r[], int n) {    float sum = 0;    int i;    for (i = 0; i < n; i++)        sum += r[i];    return sum; } float parallel(float r[], int n) {    float sum = 0;    int i;    for (i = 0; i < n; i++)        sum = sum + (1 / r[i]);...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT