Minimum number of candies required to distribute among children based on given conditions
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: 5
Explanation:
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], 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 less than or equal to ans[i + 1], then update the value of ans[i] as ans[i + 1] + 1.
- 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] if (arr[i + 1] > arr[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; } // 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; } |
Java
// Java program for the above approach import java.io.*; class GFG{ // Function to count the minimum number // of candies that is to be distributed static 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 = new int [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] if (arr[i + 1 ] > arr[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 ; } // Increment the sum by ans[i] sum += ans[i]; } sum += ans[n - 1 ]; // Return the resultant sum return sum; } // Driver Code public static void main(String[] args) { int arr[] = { 1 , 0 , 2 }; int N = arr.length; System.out.println(countCandies(arr, N)); } } // This code is contributed by patel2127 |
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] if (arr[i + 1 ] > arr[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 # 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 |
C#
using System; public class GFG{ // Function to count the minimum number // of candies that is to be distributed static 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 = new int [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] if (arr[i + 1] > arr[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; } // Increment the sum by ans[i] sum += ans[i]; } sum += ans[n - 1]; // Return the resultant sum return sum; } // Driver Code static public void Main (){ int [] arr = { 1, 0, 2 }; int N = arr.Length; Console.WriteLine(countCandies(arr, N)); } } // This code is contributed by rag2127 |
Javascript
<script> // Javascript program for the above approach // Function to count the minimum number // of candies that is to be distributed function countCandies(arr, n) { // Stores total count of candies let sum = 0; // Stores the amount of candies // allocated to a student let ans = new Array(n); // If the value of N is 1 if (n == 1) { return 1; } // Initialize with 1 all array // element for (let i = 0; i < n; i++) ans[i] = 1; // Traverse the array arr[] for (let i = 0; i < n - 1; i++) { // If arr[i+1] is greater than // arr[i] if (arr[i + 1] > arr[i]) { // Assign ans[i]+1 to // ans[i+1] ans[i + 1] = ans[i] + 1; } } // Iterate until i is atleast 0 for (let 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; } // Increment the sum by ans[i] sum += ans[i]; } sum += ans[n - 1]; // Return the resultant sum return sum; } // Driver Code let arr = [ 1, 0, 2 ]; let N = arr.length; document.write(countCandies(arr, N)) // This code is contributed by unknown2108 </script> |
5
Time Complexity: O(N)
Auxiliary Space: O(N)