Given an array **arr[]** of size **n** where **arr[i]** is the amount of candies of type **i**. You have unlimited amount of money. The task is to buy as many candies as possible satisfying the following conditions:

If you buy **x(i)** candies of type **i** (clearly, 0 ≤ x(i) ≤ arr[i]), then for all **j** (1 ≤ j ≤ i) at least one of the following must hold:

**x(j) < x(i)**(you bought less candies of type j than of type i)**x(j) = 0**(you bought 0 candies of type j)

**Examples:**

Input:arr[] = {1, 2, 1, 3, 6}

Output:10

x[] = {0, 0, 1, 3, 6} where x[i] is the number of candies bought of type i

Input:arr[] = {3, 2, 5, 4, 10}

Output:20

Input:arr[] = {1, 1, 1, 1}

Output:1

**Approach:** We can use greedy approach and start from the end of the array. If we have taken **x** candies of the type **i + 1** then we can only take **min(arr[i], x – 1)** candies of type **i**. If this value is negative, we cannot buy candies of the current type.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the maximum candies ` `// that can be bought ` `int` `maxCandies(` `int` `arr[], ` `int` `n) ` `{ ` ` ` ` ` `// Buy all the candies of the last type ` ` ` `int` `prevBought = arr[n - 1]; ` ` ` `int` `candies = prevBought; ` ` ` ` ` `// Starting from second last ` ` ` `for` `(` `int` `i = n - 2; i >= 0; i--) { ` ` ` ` ` `// Amount of candies of the current ` ` ` `// type that can be bought ` ` ` `int` `x = min(prevBought - 1, arr[i]); ` ` ` ` ` `if` `(x >= 0) { ` ` ` ` ` `// Add candies of current type ` ` ` `// that can be bought ` ` ` `candies += x; ` ` ` ` ` `// Update the previous bought amount ` ` ` `prevBought = x; ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `candies; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 1, 2, 1, 3, 6 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` `cout << maxCandies(arr, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` `// Function to return the maximum candies ` `// that can be bought ` `static` `int` `maxCandies(` `int` `arr[], ` `int` `n) ` `{ ` ` ` ` ` `// Buy all the candies of the last type ` ` ` `int` `prevBought = arr[n - ` `1` `]; ` ` ` `int` `candies = prevBought; ` ` ` ` ` `// Starting from second last ` ` ` `for` `(` `int` `i = n - ` `2` `; i >= ` `0` `; i--) ` ` ` `{ ` ` ` ` ` `// Amount of candies of the current ` ` ` `// type that can be bought ` ` ` `int` `x = Math.min(prevBought - ` `1` `, arr[i]); ` ` ` ` ` `if` `(x >= ` `0` `) ` ` ` `{ ` ` ` ` ` `// Add candies of current type ` ` ` `// that can be bought ` ` ` `candies += x; ` ` ` ` ` `// Update the previous bought amount ` ` ` `prevBought = x; ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `candies; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `arr[] = { ` `1` `, ` `2` `, ` `1` `, ` `3` `, ` `6` `}; ` ` ` `int` `n = arr.length; ` ` ` `System.out.println(maxCandies(arr, n)); ` `} ` `} ` ` ` `// This code is contributed by Code_Mech. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the maximum candies ` `# that can be bought ` `def` `maxCandies(arr, n) : ` ` ` ` ` `# Buy all the candies of the last type ` ` ` `prevBought ` `=` `arr[n ` `-` `1` `]; ` ` ` `candies ` `=` `prevBought; ` ` ` ` ` `# Starting from second last ` ` ` `for` `i ` `in` `range` `(n ` `-` `2` `, ` `-` `1` `, ` `-` `1` `) : ` ` ` ` ` `# Amount of candies of the current ` ` ` `# type that can be bought ` ` ` `x ` `=` `min` `(prevBought ` `-` `1` `, arr[i]); ` ` ` `if` `(x >` `=` `0` `) : ` ` ` ` ` `# Add candies of current type ` ` ` `# that can be bought ` ` ` `candies ` `+` `=` `x; ` ` ` ` ` `# Update the previous bought amount ` ` ` `prevBought ` `=` `x; ` ` ` ` ` `return` `candies; ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `arr ` `=` `[ ` `1` `, ` `2` `, ` `1` `, ` `3` `, ` `6` `]; ` ` ` `n ` `=` `len` `(arr) ` ` ` `print` `(maxCandies(arr, n)); ` ` ` `# This code is contributed by Ryuga ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the maximum candies ` `// that can be bought ` `static` `int` `maxCandies(` `int` `[] arr, ` `int` `n) ` `{ ` ` ` ` ` `// Buy all the candies of the last type ` ` ` `int` `prevBought = arr[n - 1]; ` ` ` `int` `candies = prevBought; ` ` ` ` ` `// Starting from second last ` ` ` `for` `(` `int` `i = n - 2; i >= 0; i--) ` ` ` `{ ` ` ` ` ` `// Amount of candies of the current ` ` ` `// type that can be bought ` ` ` `int` `x = Math.Min(prevBought - 1, arr[i]); ` ` ` ` ` `if` `(x >= 0) ` ` ` `{ ` ` ` ` ` `// Add candies of current type ` ` ` `// that can be bought ` ` ` `candies += x; ` ` ` ` ` `// Update the previous bought amount ` ` ` `prevBought = x; ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `candies; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `[] arr= { 1, 2, 1, 3, 6 }; ` ` ` `int` `n = arr.Length; ` ` ` `Console.WriteLine(maxCandies(arr, n)); ` `} ` `} ` ` ` `// This code is contributed by Code_Mech. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the approach ` ` ` `// Function to return the maximum candies ` `// that can be bought ` `function` `maxCandies(` `$arr` `, ` `$n` `) ` `{ ` ` ` ` ` `// Buy all the candies of the last type ` ` ` `$prevBought` `= ` `$arr` `[` `$n` `- 1]; ` ` ` `$candies` `= ` `$prevBought` `; ` ` ` ` ` `// Starting from second last ` ` ` `for` `(` `$i` `= ` `$n` `- 2; ` `$i` `>= 0; ` `$i` `--) ` ` ` `{ ` ` ` ` ` `// Amount of candies of the current ` ` ` `// type that can be bought ` ` ` `$x` `= min(` `$prevBought` `- 1, ` `$arr` `[` `$i` `]); ` ` ` ` ` `if` `(` `$x` `>= 0) ` ` ` `{ ` ` ` ` ` `// Add candies of current type ` ` ` `// that can be bought ` ` ` `$candies` `+= ` `$x` `; ` ` ` ` ` `// Update the previous bought amount ` ` ` `$prevBought` `= ` `$x` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `$candies` `; ` `} ` ` ` `// Driver code ` `$arr` `= ` `array` `(1, 2, 1, 3, 6 ); ` `$n` `= sizeof(` `$arr` `); ` `echo` `(maxCandies(` `$arr` `, ` `$n` `)); ` ` ` `// This code is contributed by Code_Mech. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

10

## Recommended Posts:

- Buy Maximum Stocks if i stocks can be bought on i-th day
- Maximum litres of water that can be bought with N Rupees
- Maximum items that can be bought with the given type of coins
- Maximum items that can be bought from the cost Array based on given conditions
- Find the minimum and maximum amount to buy all N candies
- Distribution of candies according to ages of students
- Distribute N candies among K people
- Distribute candies in a Binary Tree
- Find maximum number that can be formed using digits of a given number
- Maximum number of prime factors a number can have with exactly x factors
- Maximum number of customers that can be satisfied with given quantity
- Maximum number of partitions that can be sorted individually to make sorted
- Maximum number of parallelograms that can be made using the given length of line segments
- Maximum number of teams that can be formed with given persons
- Maximum number of segments that can contain the given points
- Maximum number that can be display on Seven Segment Display using N segments
- Maximum number of people that can be killed with strength P
- Count the maximum number of elements that can be selected from the array
- Maximum number of region in which N non-parallel lines can divide a plane
- Maximum number of distinct positive integers that can be used to represent N

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.