The following code will be used to determine the maximum amount invested
<u>Explanation:</u>
long maxValue(int n, int rounds_rows, int rounds_columns, int into rounds)
{
// Define the variable to store
// the maximum amount invested.
long max = 0;
// Define an array of size n,
// to store the n investments.
long *investments = (long*)malloc(sizeof(long)*n);
int i=0;
// Initially set all
// the investments to 0.
for(i=0;i<n;i++)
{
investments[i] = 0;
}
i=0;
// Run the loop to
// perform the rounds.
while(i<rounds_rows)
{
// Get the left value
// of the current round.
int left = rounds[i][0];
// Get the right value
// of the current round.
int right = rounds[i][1];
// Get the contribution
// for the current round.
int contribution = rounds[i][2];
// Since the user uses 1-based
// indexing, subtract 1 from left
// and right as the program uses
// 0-based indexing. That is, the
// investments in the array start
// from 0 and not 1.
right = right - 1;
int j=0;
// Run the loop to add the
// contribution to all the investments
// between left and right, both inclusive.
for(j=left; j<=right; j++)
{
investments[j] += contribution;
}
i++;
}
// Traverse the investments array
// to find the maximum element.
max = investments[0];
for(i=1; i<n;i++)
{
if(investments[i]>max)
{
max = investments[i];
}
}
// Return the
// maximum investment.
return max;
}