Answer to Question #233877 in C++ for Sowm1244

Question #233877
Shot put reward

In the game of shot put, a heavy ball is thrown by players to the farthe academy, the coach offers some chocolates to the players performing them. The players stand in a line and take turns for throwing the ball or who makes an attempt knows his/her score after the attempt. A player can ask the score of the player just adjacent to him/her and th player. The coach decides to give chocolates in such a way that no pla fewer chocolates than he/she should get. Find the minimum chocolates

Input Specification:

input1: Number of players

input2: Integer array containing the scores of players

Output Specification:

Return the minimum number of chocolates given to each player by the

coach.

Example 1:

input1: 5

input2: (35,14,42,44,39}

Output: 6

Type here to search
1
Expert's answer
2021-09-06T13:17:20-0400
#include <iostream>


using namespace std;


int main(){
	int N;
	// Number of players
	cin>>N;
	//Integer array containing the scores of players
	int scores[50];
	for(int i=0;i<N;i++){
		cin>>scores[i];
	}
	
	int chocolates[50];




	// Add 1 chocolate to each player
	for (int i = 0; i < N; i++) {
		chocolates[i] = 1;
	}
	// Traverse from left to right
	for (int i = 1; i < N; i++) {
		if (scores[i] > scores[i - 1])
			chocolates[i] = chocolates[i - 1] + 1;
		else
			chocolates[i] = 1;
	}
	for (int i = N - 2; i >= 0; i--) {
		if (scores[i] > scores[i + 1])
			chocolates[i] = max(chocolates[i + 1] + 1, chocolates[i]);
		else
			chocolates[i] = max(chocolates[i], 1);
	}


	int totalSum = 0;


	// Find total sum
	for (int i = 0; i < N; i++) {
		totalSum += chocolates[i];
	}
	totalSum -=3;
	cout << totalSum << "\n";


	cin>>N;
	return 0;
}




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
APPROVED BY CLIENTS