Answer to Question #285204 in C++ for rocky

Question #285204

You’re given n boxes of chocolates arranged sequentially. Each box contains ai chocolates. You have to pick the maximum number of chocolates from the boxes and you must follow the given rule: ● You cannot pick chocolates from two consecutive boxes Let me explain the rule. If you pick chocolates from the ith box, you are not allowed to pick chocolates from the i+1th box or i-1 th box. The first line of the input represents n (2<=n<=1000), the number of boxes, and the following line represents the number of chocolate of those n boxes. Print the maximum number of chocolates you can collect from the boxes with satisfying the condition. 

Sample Input :

8

1 2 3 4 5 6 7 8

Sample Output: 20

Sample Input 3:

5

2 50 100 75 1

Sample Output:

125


1
Expert's answer
2022-01-06T05:38:22-0500
#include <iostream> 

using namespace std;

int main()
{
	int N;
	cout<<"Please, enter a number of boxes: ";
	cin>>N;
	if(N<2||N>1000)
	{
		cout<<"Incorrect number!";
		return 0;
	}
	int* boxes=new int[N];
	cout<<"Please, enter a number of chocolates in every box: ";
	for(int i=0;i<N;i++)
	{
		cin>>boxes[i];
	}	
	double summEven=0;
	for(int i=0;i<N;i+=2)
	{
		summEven+=boxes[i];
	}
	double summOdd=0;
	for(int i=1;i<N;i+=2)
	{
		summOdd+=boxes[i];
	}
	if(summEven>=summOdd)
		cout<<"The maximum number of chocolates is "<<summEven;
	else
		cout<<"The maximum number of chocolates is "<<summOdd;
		
	delete[] boxes;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog