Maximum number of candies that can be bought
Last Updated :
09 Jun, 2022
Given an array arr[] of size n where arr[i] is the number of candies of type i. You have an 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 a greedy approach and start from the end of the array. If we have taken x candies of 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++
#include <bits/stdc++.h>
using namespace std;
int maxCandies( int arr[], int n)
{
int prevBought = arr[n - 1];
int candies = prevBought;
for ( int i = n - 2; i >= 0; i--) {
int x = min(prevBought - 1, arr[i]);
if (x >= 0) {
candies += x;
prevBought = x;
}
}
return candies;
}
int main()
{
int arr[] = { 1, 2, 1, 3, 6 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << maxCandies(arr, n);
return 0;
}
|
Java
class GFG
{
static int maxCandies( int arr[], int n)
{
int prevBought = arr[n - 1 ];
int candies = prevBought;
for ( int i = n - 2 ; i >= 0 ; i--)
{
int x = Math.min(prevBought - 1 , arr[i]);
if (x >= 0 )
{
candies += x;
prevBought = x;
}
}
return candies;
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 1 , 3 , 6 };
int n = arr.length;
System.out.println(maxCandies(arr, n));
}
}
|
Python3
def maxCandies(arr, n) :
prevBought = arr[n - 1 ];
candies = prevBought;
for i in range (n - 2 , - 1 , - 1 ) :
x = min (prevBought - 1 , arr[i]);
if (x > = 0 ) :
candies + = x;
prevBought = x;
return candies;
if __name__ = = "__main__" :
arr = [ 1 , 2 , 1 , 3 , 6 ];
n = len (arr)
print (maxCandies(arr, n));
|
C#
using System;
class GFG
{
static int maxCandies( int [] arr, int n)
{
int prevBought = arr[n - 1];
int candies = prevBought;
for ( int i = n - 2; i >= 0; i--)
{
int x = Math.Min(prevBought - 1, arr[i]);
if (x >= 0)
{
candies += x;
prevBought = x;
}
}
return candies;
}
public static void Main()
{
int [] arr= { 1, 2, 1, 3, 6 };
int n = arr.Length;
Console.WriteLine(maxCandies(arr, n));
}
}
|
PHP
<?php
function maxCandies( $arr , $n )
{
$prevBought = $arr [ $n - 1];
$candies = $prevBought ;
for ( $i = $n - 2; $i >= 0; $i --)
{
$x = min( $prevBought - 1, $arr [ $i ]);
if ( $x >= 0)
{
$candies += $x ;
$prevBought = $x ;
}
}
return $candies ;
}
$arr = array (1, 2, 1, 3, 6 );
$n = sizeof( $arr );
echo (maxCandies( $arr , $n ));
?>
|
Javascript
<script>
function maxCandies(arr, n)
{
let prevBought = arr[n - 1];
let candies = prevBought;
for (let i = n - 2; i >= 0; i--)
{
let x = Math.min(prevBought - 1, arr[i]);
if (x >= 0)
{
candies += x;
prevBought = x;
}
}
return candies;
}
let arr = [ 1, 2, 1, 3, 6 ];
let n = arr.length;
document.write(maxCandies(arr, n));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...