Javatpoint Logo
Javatpoint Logo

Maximize Total Score

Introduction

One of the most important ideas in programming is optimization. Whether you are creating effective systems or deriving solutions for intricate algorithms, the objective is frequently to maximize or minimize a given value. The goal is to maximize the overall score, and in order to do this, the scoring system's regulations must be established. Let us examine a fictitious situation in which we have a collection of items, all of which have a score. The task at hand involves choosing a subset of these components so that, within a set of restrictions, the total of their scores is maximized.

Strategies for Maximization

Greedy Algorithm

The greedy algorithm is a popular method for resolving optimization issues. In order to find a global optimum, the greedy method entails making decisions at each stage that are locally optimal. This could entail prioritizing the elements with the greatest individual ratings in order to maximize the overall score.

Code

Output:

Maximize Total Score

Code Explanation

Function for Sorting (compare)

  • When sorting numbers in decreasing order, the qsort function uses the comparison function as a callback function.
  • It accepts two pointers to integers (a and b) and outputs the outcome of dividing b by a. This leads to sorting in descending order.

Greedy Algorithm Function (maximizeScoreGreedy)

  • An array of scores, the number of elements in the array (n), and a constraint value are required for this function to work.
  • The scores are sorted using the comparison function in descending order by utilizing the qsort function.
  • After that, iteratively chooses each element from the sorted scores until the specified constraint is satisfied.
  • The remaining limitation is updated in accordance with the totalScore variable's accumulation of the chosen scores.
  • The final total score is returned.

Main Function (main)

  • Sets the constraint value (15) and an array of sample scores ({10, 8, 5, 3, 2}) to their initial values.
  • Uses the sample scores, the number of items, and the constraint to call the maximizeScoreGreedy function.
  • Shows the greatest score obtained by the greedy method, the constraint, and the input scores.
  • The output consists of the greedy algorithm's outcome, the supplied constraint, and each score.

Sample Output

  • The program produces the greatest total score attained by the greedy algorithm, the supplied constraint, and the sorted array of scores.
  • Depending on the input numbers, the output will change, but it usually shows the chosen scores that maximize the total score within the specified bound.

Usage of qsort

  • The array of scores is sorted in descending order using the qsort function.
  • It requires an array's base address, its number of elements, each element's size, and a comparison function (compare).

Dynamic Programming

This is an additional effective method for addressing optimization issues. Minimizing duplicate calculations entails segmenting a problem into smaller subproblems and solving each one only once. The solutions to the subproblems are then stored in a table. By taking into account the scores of subsets of elements, dynamic programming may be used to identify the best solution in the context of maximizing the total score.

Code

Output:

Maximize Total Score

Code Explanation

Maximum Function (max)

  • This function uses the ternary operator (? :) to return the maximum of two numbers, a and b.

Dynamic programming Function (maximizeScoreDP)

  • The function requires an array of scores (scores), a constraint value, and the number of elements in the array (n).
  • It sets up a 2D array called dp to hold subproblem answers. (n + 1) x (constraint + 1) is the size of dp.
  • The nested loops iterate over all conceivable combinations of the constraint and the number of elements.
  • The innermost if-else loop computes the maximum score using the recurrence relation and handles the base cases, which occur when either i or j is zero.
  • The dp table has the outcome.

Main Function (main)

  • Sets the constraint value (15) and an array of sample scores ({10, 8, 5, 3, 2}) to their initial values.
  • Uses the sample scores, the number of items, and the constraint to call the maximizeScoreDP function.
  • Uses dynamic programming to display the input scores, the constraint, and the highest possible score.
  • The output consists of the dynamic programming approach's outcome, the provided constraint, and each score.

Usage of Dynamic Programming

  • By storing answers to subproblems in the dp table, dynamic programming is utilized to solve the optimization problem effectively.
  • By taking into account the remaining constraint and the scores of subsets of elements, the function constructs the table.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA