# Candies

Contributed by
Ayush Thakur
Last Updated: 23 Feb, 2023
Medium
0/80
Avg time to solve 10 mins
Success Rate 90 %
Share

## Problem Statement

#### Example :

``````Given students' ratings : [5, 8, 1, 5, 9, 4].
He gives the students candy in the following minimal amounts : [1, 2, 1, 2, 3, 1]. He must buy a minimum of 10 candies.
``````

#### Note :

``````1. If two students having the same grade are standing next to each other, they may receive the same number of candies.
2. Every student must get at least a candy.
``````
Detailed explanation ( Input/output format, Notes, Images )
##### Constraints :
``````1 <= T <= 10^2
1 <= N <= 10^4
1 <= STUDENTS[i] <= 10^5

Time Limit : 1 sec
``````
##### Sample Input 1 :
``````3
2
1 5
3
1 3 4
3
1 2 2
``````
##### Sample Output 1 :
``````3
6
4
``````
##### Explanation For Sample Input 1 :
``````(i) Optimal distribution will be 1 2
(ii) Optimal distribution will be 1 2 3
(iii) Optimal distribution will be 1 2 1 because for children with equal grades one child can have less candies
``````
##### Sample Input 2 :
``````3
1
100
5
1 5 3 4 6
6
1 9 1 3 2 4
``````
##### Sample Output 2 :
``````1
9
9
``````
##### Explanation For Sample Input 2 :
``````(i) Optimal distribution will be 1
(ii) Optimal distribution will be 1 2 1 2 3
(iii) Optimal distribution will be 1 2 1 2 1 2
``````
Auto
Console