Maximum value in an array after m range increment operations - GeeksforGeeks
Consider an array of size n with all initial values as 0, we need to perform following m range increment operations.
http://www.geeksforgeeks.org/maximum-occurred-integer-n-ranges/
Given n ranges of the form L and R, the task is to find the maximum occurred integer in all the ranges. If more than one such integer exits, print the smallest one.
Read full article from Maximum value in an array after m range increment operations - GeeksforGeeks
Consider an array of size n with all initial values as 0, we need to perform following m range increment operations.
increment(a, b, k) : Increment values from 'a' to 'b' by 'k'.After m operations, we need to calculate the maximum of the values in the array.
Perform two things in a single operation:
1- Add k-value to only lower_bound of a range.
2- Reduce upper_bound + 1 index by k-value.
1- Add k-value to only lower_bound of a range.
2- Reduce upper_bound + 1 index by k-value.
After all operations, add all values, check the maximum sum and print maximum sum.
int
findMax(
int
n,
int
m,
int
a[],
int
b[],
int
k[])
{
int
arr[n+1];
memset
(arr, 0,
sizeof
(arr));
// Start performing 'm' operations
for
(
int
i=0; i<m; i++)
{
// Store lower and upper index i.e. range
int
lowerbound = a[i];
int
upperbound = b[i];
// Add k to the lower_bound
arr[lowerbound] += k[i];
// Reduce upper_bound+1 indexed value by k
arr[upperbound+1] -= k[i];
}
// Find maximum sum possible from all values
long
long
sum = 0, res = INT_MIN;
for
(
int
i=0; i < n; ++i)
{
sum += arr[i];
res = max(res, sum);
}
// return maximum value
return
res;
}
A naive method is to perform each operation on given range and then at last find the maximum number.
Time Complexity: O(m * max(range)). Here max(range) means maximum elements to which k is added in a single operation.
int
findMax(
int
n,
int
a[],
int
b[],
int
k[],
int
m)
{
int
arr[n];
memset
(arr, 0,
sizeof
(arr));
// start performing m operations
for
(
int
i = 0; i< m; i++)
{
// Store lower and upper index i.e. range
int
lowerbound = a[i];
int
upperbound = b[i];
// Add 'k[i]' value at this operation to
// whole range
for
(
int
j=lowerbound; j<=upperbound; j++)
arr[j] += k[i];
}
// Find maximum value after all operations and
// return
int
res = INT_MIN;
for
(
int
i=0; i<n; i++)
res = max(res, arr[i]);
return
res;
}
http://www.geeksforgeeks.org/maximum-occurred-integer-n-ranges/
Given n ranges of the form L and R, the task is to find the maximum occurred integer in all the ranges. If more than one such integer exits, print the smallest one.
A simple solution is to use hash table to store counts of all numbers. We traverse every interval from Li to Ri and increment count of all numbers present in every interval. Finally we traverse the hash table to find the number with maximum count. Time complexity of this solution is O(n * MAX_INTERVAL) where MAX_INTERVAL is maximum number of elements in an interval.
An efficient solution requires linear time. We create an array arr[] of size 1000000 (limit given on maximum value of an interval). The idea is to add +1 to each Li index and -1 to corresponding Riindex in arr[]. After this, find the prefix sum of the array. Adding +1 at Li shows the starting point of ith Range and adding -1 shows the ending point of ith range. Finally we return the array index that has maximum prefix sum
Algorithm to solve the problem:
- Initialize an array arr[] to 0.
- For each range i, add +1 at Li index and -1 at Ri of the array.
- Find the prefix sum of the array and find the smallest index having maximum prefix sum.
Time Complexity: O(n + MAX)
Exercise: Try for 0 <= Li, Ri <= 1000000000. (Hint: Use stl map).
int
maximumOccuredElement(
int
L[],
int
R[],
int
n)
{
// Initalising all element of array to 0.
int
arr[MAX];
memset
(arr, 0,
sizeof
arr);
// Adding +1 at Li index and substracting 1
// at Ri index.
for
(
int
i = 0; i < n; i++)
{
arr[L[i]] += 1;
arr[R[i] + 1] -= 1;
}
// Finding prefix sum and index having maximum
// prefix sum.
int
msum = arr[0], ind;
for
(
int
i = 1; i < MAX; i++)
{
arr[i] += arr[i-1];
if
(msum < arr[i])
{
msum = arr[i];
ind = i;
}
}
return
ind;
}