IN C++:
Please write a function called coin_row that takes in a vector (of type int) of coins in order and outputs a coin row solution.
( Find maximum possible sum of elements such that there are no 2 consecutive elements present in the sum.)
val set to the value of the optimal solution
coin_indices (0-indexed) set to the indices of the coins forming the optimal solution
MUST BE DONE WITH DYNAMIC PROGRAMMING. NO RECURSION
typedef struct coin_row_solution{
int val;
std::vector< int > coin_indices;
} CRS;
/******************************************/
CRS coin_row(std::vector< int> coin_row){
CRS soln;
/*code goes here*/
return soln;
}
Example:
Input: {5, 22, 26, 15, 4, 3,11}
Output: val = 48
(22 + 15 + 11)
Code:
Comments are provided inline
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
typedef struct coin_row_solution {
int val;
std::vector< int > coin_indices;
} CRS;
CRS coin_row(std::vector< int> coin_row) {
CRS soln;
//First sort the vector in decreasing order and map
their corresponding indices in the
//another vector named indices
vector<int> temp = coin_row;
std::sort(temp.begin(), temp.end(),
std::greater<int>());
vector<int> indices;
for (auto element : temp)
{
auto itr =
std::find(coin_row.begin(), coin_row.end(), element);
int index =
std::distance(coin_row.begin(), itr);
indices.push_back(index);
}
//Prepare the solnVector which contain a vector of
possible sum and array indices
vector<CRS> solnVector;
for (int i = 0; i < coin_row.size(); i++)
{
int sum = 0;
vector<int>
indicesconsidered;
indicesconsidered.push_back(indices[i]);
sum += temp[i];
for (int j = (i + 1); j <
coin_row.size(); j++)
{
int index =
indices[j];
//To check
whether the selected index is not adjacent
bool bresult =
std::all_of(indicesconsidered.begin(), indicesconsidered.end(),
[&](int anIndex) {
return abs(anIndex - index) > 1;
});
if
(bresult)
{
//If it is not adjacent add to the considered
index list and calculate the sum
indicesconsidered.push_back(index);
sum += temp[j];
}
}
solnVector.push_back({
sum,indicesconsidered });
}
//Finding the Maximum sum from the possible
sum
auto itr = std::max_element(solnVector.begin(),
solnVector.end(), [&](const CRS aSoln1,const CRS
aSoln2)->bool {
return aSoln1.val <
aSoln2.val;
});
soln = *itr;
return soln;
}
int main()
{
CRS sol = coin_row({ 5,22,26,15,4,3,11 });
cout << "\nMax Sum is " <<
sol.val;
cout << "\nIndices are :";
for (int index : sol.coin_indices)
{
cout << " " <<
index;
}
}
OUTPUT:

IN C++: Please write a function called coin_row that takes in a vector (of type int)...
Coding Language: C++
Function Header: vector<vector<int>>
printFromButtom(TreeNode* root) {}
Definition for a binary tree node:
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
};
The Problem Complete the printFromButtom function that accepts a BST TreeNode and returns the nodes' value from left to right, level by level from leaf to root. This function will return vector<vector int which similar to a 2-D array. Function std: reverse (myvector.begin myVector en might be helpful. Definition for a binary tree node: struct...
Hi, I have C++ programming problem here:
Problem:
Please modify your string type vector in for push_back()
function as below:
void push_back(string str)
{
// increase vector size by one
// initialize the new element with str
}
In addition, the standard library vector doesn't provide
push_front(). Implement
push_front() for your vector. Test your code with the main function
below.
int main()
{
vector v1(3);
cout<<"v1: ";
v1.print(); // this should display -, -, -
for...
C++ -- Vector of Bank Accounts (15 pts) Please help! :( Use csegrid or Clion for this part Add on to the lab12a solution(THIS IS PROVIDED BELOW). You will add an overload operator function to the operator << and a sort function (in functions.h and functions.cpp) Print out the customer’s name, address, account number and balance using whatever formatting you would like Sort on account balance (hint: you can overload the < operator and use sort from the...
Please write below code in C++ using Visual
Studio.
Write program that uses a class template to create a set of items. The program should: 1. add items to the set (there shouldn't be any duplicates) • Example: if your codes is adding three integers, 10, 5, 10, then your program will add only two values 10 and 5 • Hint: Use vectors and vector functions to store the set of items 2. Get the number of items in the...
In C++ I need the printRange function, and the main.cpp program. Thanks. (Binary search tree) Write a function printRange that takes as input a binary search tree t and two keys, k1 and k2, which are ordered so that k1 < k2, and print all elements x in the tree such that k1 <= x <= k2. You can add this function in BinarySearchTree.h (click the link) that we used in the lecture and lab 7. public: void printRange(int k1,...
JAVA 3 PLEASE ANSWER AS MANY QUESTIONS AS POSSIBLE! ONLY 2 QUESTIONS LEFT THIS MONTH!!! Question 12 pts Which is a valid constructor for Thread? Thread ( Runnable r, int priority ); Thread ( Runnable r, String name ); Thread ( int priority ); Thread ( Runnable r, ThreadGroup g ); Flag this Question Question 22 pts What method in the Thread class is responsible for pausing a thread for a specific amount of milliseconds? pause(). sleep(). hang(). kill(). Flag...
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
Part 1. [30 points] In this part, your program
loads a vending machine serving cold drinks. You start with many
foods, some are drinks. Your code loads a vending machine from
foods, or, it uses water as a default drink. Create class Drink,
make an array of drinks, load it and display it.
Part 1 steps:
[5 points] Create a class called
Drink that contains information about a single
drink. Provide...