![]() |
VOOZH | about |
Given an array A[] consisting of N integers, where each value represents the marks of the ith student, the task is to find the minimum number of chocolates required to be distributed such that:
Examples:
Input: A[] = {10, 30, 20}
Output: 4
Explanation : Since, 30 is larger than its adjacent, so the second student must get more chocolates. Therefore, the minimum chocolates can be distributed as {1, 2, 1} = 1 + 2 + 1 = 4Input: A[] = {23, 14, 15, 14, 56, 29, 14}
Output: 12
Method 1:
Approach: The problem can be solved using Greedy approach. Follow the steps below to solve the problem:
Below is the implementation of the above approach:
12
Time Complexity: O(N) where N is the length of the given array.
Auxiliary Space: O(N)
Method 2 : Efficient approach
On careful observation, the space complexity can be reduced to O(1).
I. Observation:
** An exception of second observation is mentioned below
II. Distributing chocolates
Case 1: subarray is strictly increasing
If the values are strictly increasing, number of chocolates given to ith student will be one more than the number of chocolates given to (i-1)th student (for any i > 0)
One chocolate will be given to the left most person in subarray, two the next and so on incrementally up to the person with highest marks.
For a strictly increasing subarray of length k, the chocolate distribution will be [1, 2, ... , k].
Case 2 : subarray is strictly decreasing
Number of chocolates given to ith student will be one more than the chocolates given to (i+1)th student ( for any i < n-1) with one chocolate to the rightmost person and max number to the leftmost person of the subarray.
For a strictly decreasing subarray of length k, the chocolate distribution will be [k, k-1, ... ,1].
Case 3 : flat sequence
Given that a student with highest marks will be awarded more number of chocolates than neighbors. So there is no dependency if the values are equal. Minimum value will be assigned for optimal result.
One chocolate will be given to person at position i if both the adjacent values are equal to a[i] i.e, a[i-1] == a[i] == a[i+1]
For a flat subarray of length k, the chocolate distribution will be [1, 1, ... ,1].
**The difference for an assigned value with both the neighbors may be greater than 1, if there is a single element in flat sequence and it lies exactly between an increasing & decreasing sequence
Transition points: The points where the trend(increasing/ decreasing/ flat nature) of subarray changes.
then value assigned will be max(k1, k2)
where k1 - value obtained from increasing sequence,
k2 - value obtained from decreasing sequence.
This point will be considered as part of either increasing or decreasing sequence only
III. Result :
As the values in an increasing/decreasing sequence differ by 1, the number of chocolates distributed to students in a specific subarray of k elements will be sum of k natural numbers. And the count will be k for a flat sequence as all the values are 1. The required value will be the total sum of the results of subarrays.
IV. Implementation:
Consider variables i, j initially pointing to first element, val = 1, res = 0.
After traversing through the array res gives the total number of chocolates distributed.
val while iterating index j (in increasing/flat subarray) represents the number of chocolates received by the person at j
If the subarray is increasing or a flat sequence, val is added to res; i, j are moved forward and val is updated according to next value (a[j + 1]).
If the subarray is decreasing, i is pointed to the starting point of subarray and j is moved forward till next transition point. val, res are not updated till the end of the subarray. In this case val holds the value of the peak element obtained from previous subarray. At the end of the decreasing sequence res is updated using get_sum function & val is updated to point to the number of chocolates held by the next person.
V. Example:
👁 ImageInput: A[ ] = {1, 2, 10, 7, 6, 4, 5, 5, 5, 6}
Output : 19
Explanation:
subarray -- sequence type -- count of chocolates
A[0-1] -- increasing sequence -- [1, 2]
A[2-5] -- decreasing sequence -- [4, 3, 2, 1]
A[5-6] -- increasing sequence -- [1, 2]
A[7-7] -- flat sequence -- [1]
A[8-9] -- increasing sequence -- [1, 2]
A[2], A[9] are peak points
Chocolates distribution will be
[1, 2, 4, 3, 2, 1, 2, 1, 1, 2]
Sum of all values = 19
Below is the code for above approach
Minimum number of chocolates = 16
Time Complexity : O(N), N is the length of the array
Space Complexity : O(1)