Given an array **arr[]** consisting of **N** positive integers representing the ratings of **N** children, the task is to find the minimum number of candies required for distributing to **N** children such that every child gets at least one candy and the children having the higher rating get more candies than its neighbours.

**Examples:**

Input:arr[] = {1, 0, 2}Output:5Explanation:

Consider the distribution of candies as {2, 1, 2} that satisfy the given conditions. Therefore, the sum of candies is 2 + 1 + 2 = 5, which is the minimum required candies.

Input:arr[] = {1, 2, 2}Output:4

**Approach:** The given problem can be solved by using the Greedy Approach. Follow the steps below to solve the problem:

- Initialize an array, say
**ans[]**to store the amount of candies assigned to every child, and initialize it with**1**to every array element**ans[]**. - Traverse the given array
**arr[]**and if the value of**arr[i + 1]**is greater than**arr[i]**and the value of**ans[i + 1]**is at least**ans[i]**, then update the value of**ans[i + 1]**as**ans[i] + 1**. - Traverse the given array from the back and perform the following steps:
- If the value of
**arr[i]**is greater than**arr[i + 1]**and the value of**ans[i]**is at least**ans[i + 1]**, then update the value of**ans[i + 1]**as**ans[i] + 1**. - If the value of
**arr[i]**is equal to**arr[i + 1]**and the value of**ans[i]**is less than**ans[i + 1]**, then update the value of**ans[i + 1]**as**ans[i] + 1**.

- If the value of
- After completing the above steps, print the sum of array
**ans[]**as the resultant sum of candies.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to count the minimum number` `// of candies that is to be distributed` `int` `countCandies(` `int` `arr[], ` `int` `n)` `{` ` ` `// Stores total count of candies` ` ` `int` `sum = 0;` ` ` `// Stores the amount of candies` ` ` `// allocated to a student` ` ` `int` `ans[n];` ` ` `// If the value of N is 1` ` ` `if` `(n == 1) {` ` ` `return` `1;` ` ` `}` ` ` `// Initialize with 1 all array` ` ` `// element` ` ` `for` `(` `int` `i = 0; i < n; i++)` ` ` `ans[i] = 1;` ` ` `// Traverse the array arr[]` ` ` `for` `(` `int` `i = 0; i < n - 1; i++) {` ` ` `// If arr[i+1] is greater than` ` ` `// arr[i] and ans[i+1] is` ` ` `// at most ans[i]` ` ` `if` `(arr[i + 1] > arr[i]` ` ` `&& ans[i + 1] <= ans[i]) {` ` ` `// Assign ans[i]+1 to` ` ` `// ans[i+1]` ` ` `ans[i + 1] = ans[i] + 1;` ` ` `}` ` ` `}` ` ` `// Iterate until i is atleast 0` ` ` `for` `(` `int` `i = n - 2; i >= 0; i--) {` ` ` `// If arr[i] is greater than` ` ` `// arr[i+1] and ans[i] is` ` ` `// at most ans[i+1]` ` ` `if` `(arr[i] > arr[i + 1]` ` ` `&& ans[i] <= ans[i + 1]) {` ` ` `// Assign ans[i+1]+1 to` ` ` `// ans[i]` ` ` `ans[i] = ans[i + 1] + 1;` ` ` `}` ` ` `// If arr[i] is equals arr[i+1]` ` ` `// and ans[i] is less than` ` ` `// ans[i+1]` ` ` `else` `if` `(arr[i] == arr[i + 1]` ` ` `&& ans[i] < ans[i + 1]) {` ` ` `// Assign ans[i+1]+1 to` ` ` `// ans[i]` ` ` `ans[i] = ans[i + 1] + 1;` ` ` `}` ` ` `// Increment the sum by ans[i]` ` ` `sum += ans[i];` ` ` `}` ` ` `sum += ans[n - 1];` ` ` `// Return the resultant sum` ` ` `return` `sum;` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `arr[] = { 1, 0, 2 };` ` ` `int` `N = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]);` ` ` `cout << countCandies(arr, N);` ` ` `return` `0;` `}` |

## Python3

`# Python3 program for the above approach` `# Function to count the minimum number` `# of candies that is to be distributed` `def` `countCandies(arr, n):` ` ` ` ` `# Stores total count of candies` ` ` `sum` `=` `0` ` ` `# Stores the amount of candies` ` ` `# allocated to a student` ` ` `ans ` `=` `[` `1` `] ` `*` `n` ` ` `# If the value of N is 1` ` ` `if` `(n ` `=` `=` `1` `):` ` ` `return` `1` ` ` `# Initialize with 1 all array` ` ` `# element` ` ` `# for (i = 0 i < n i++)` ` ` `# ans[i] = 1` ` ` `# Traverse the array arr[]` ` ` `for` `i ` `in` `range` `(n ` `-` `1` `):` ` ` `# If arr[i+1] is greater than` ` ` `# arr[i] and ans[i+1] is` ` ` `# at most ans[i]` ` ` `if` `(arr[i ` `+` `1` `] > arr[i] ` `and` ` ` `ans[i ` `+` `1` `] <` `=` `ans[i]):` ` ` ` ` `# Assign ans[i]+1 to` ` ` `# ans[i+1]` ` ` `ans[i ` `+` `1` `] ` `=` `ans[i] ` `+` `1` ` ` `# Iterate until i is atleast 0` ` ` `for` `i ` `in` `range` `(n ` `-` `2` `, ` `-` `1` `, ` `-` `1` `):` ` ` ` ` `# If arr[i] is greater than` ` ` `# arr[i+1] and ans[i] is` ` ` `# at most ans[i+1]` ` ` `if` `(arr[i] > arr[i ` `+` `1` `] ` `and` ` ` `ans[i] <` `=` `ans[i ` `+` `1` `]):` ` ` `# Assign ans[i+1]+1 to` ` ` `# ans[i]` ` ` `ans[i] ` `=` `ans[i ` `+` `1` `] ` `+` `1` ` ` `# If arr[i] is equals arr[i+1]` ` ` `# and ans[i] is less than` ` ` `# ans[i+1]` ` ` `elif` `(arr[i] ` `=` `=` `arr[i ` `+` `1` `] ` `and` ` ` `ans[i] < ans[i ` `+` `1` `]):` ` ` `# Assign ans[i+1]+1 to` ` ` `# ans[i]` ` ` `ans[i] ` `=` `ans[i ` `+` `1` `] ` `+` `1` ` ` `# Increment the sum by ans[i]` ` ` `sum` `+` `=` `ans[i]` ` ` `sum` `+` `=` `ans[n ` `-` `1` `]` ` ` `# Return the resultant sum` ` ` `return` `sum` `# Driver Code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` ` ` `arr ` `=` `[ ` `1` `, ` `0` `, ` `2` `]` ` ` `N ` `=` `len` `(arr)` ` ` ` ` `print` `(countCandies(arr, N))` `# This code is contributed by mohit kumar 29` |

**Output:**

5

**Time Complexity:** O(N)**Auxiliary Space:** O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**

In case you wish to attend live classes with industry experts, please refer **Geeks Classes Live**