This code serves to find out the highest amount invested
Explanation:
long maxValue(int n, int rounds_rows, int rounds_columns, int into rounds)
{
// Variable declaration to hold
// the highest investment amount.
long max = 0;
// Creating an array of size n,
// to keep track of the n investments.
long *investments = (long*)malloc(sizeof(long)*n);
int i=0;
// Initialize all
// investments to zero.
for(i=0;i<n;i++)
{
investments[i] = 0;
}
i=0;
// Execute the loop to
// conduct the rounds.
while(i<rounds_rows)
{
// Acquire the left value
// for the current round.
int left = rounds[i][0];
// Acquire the right value
// for this round.
int right = rounds[i][1];
// Get the contribution
// for this round.
int contribution = rounds[i][2];
// Because user indexing is 1-based,
// subtract 1 from left
// and right as the program employs
// 0-based indexing. The
// array investments begins
// at 0 and not 1.
right = right - 1;
int j=0;
// Execute loop to distribute the
// contribution across all investments
// from left to right, inclusive.
for(j=left; j<=right; j++)
{
investments[j] += contribution;
}
i++;
}
// Traverse the investments array
// to locate the maximum value.
max = investments[0];
for(i=1; i<n;i++)
{
if(investments[i]>max)
{
max = investments[i];
}
}
// Return the
// highest investment.
return max;
}