Table of Contents
The method used in the following program is as follows
Example
Output
Home Backend Development C++ Maximum number of candies that can be purchased

Maximum number of candies that can be purchased

Aug 29, 2023 pm 11:33 PM
quantity Buy candy

Maximum number of candies that can be purchased

We get a candy[] array with the length stored in "size". Each element of candies[i] has a number for a candy of type i. The goal is to buy as many candies as possible with any amount of money. The conditions are as follows -

If you buy X[i] of type i (0

  • X(j)

  • X(j)=0, no candy of type j was purchased

We understand through examples.

Input - Arr[] = { 1,3,5,2,6,7}.

Output - Maximum number of candies that can be purchased - 16

Description - Purchase type i { 0,3,5,2,6, 0 }

's Candy>Input - Arr[] = { 5,7,7,3,4 }.

Output - OK Maximum candies purchased - 10

Explanation - Purchase candies of type i { 0,0,7,3,0 }

The method used in the following program is as follows

  • The integer array candies[] is used to store the number of candies of type i.

  • The variable 'size' stores the length of the array candies.

  • The function maxCandies(int arr[], int n) is used to return the total number of candies that can be purchased.

  • First suppose we buy the last candy. buy=arr[n-1]

  • Start from the penultimate element, for(i=n-2;i>=0;i--)

  • Variable x stores the number of candies that can be purchased of the current type. x=arr[i] or buy-1, whichever is smaller.

  • If x is not zeo, add it to the total.

  • If the sum is greater than the amount of the previous purchase, purchase = x.

  • Return purchase results.

  • Example

    Live Demonstration

    #include <stdio.h>
    int maxCandies(int arr[], int n){
       int bought = arr[n - 1];
       int total = bought;
       // 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 = arr[i]<bought-1?arr[i]:bought-1;
          if (x >= 0) {
             total += x;
             bought = x;
          }
       }
       return total;
    }
    int main(){
       int candies[] = { 1,2,4,3,7 };
       int size = 5;
       printf("Total Candies that can be bought: %d", maxCandies(candies, size));
       return 0;
    }
    Copy after login

    Output

    If we run the above code, it will generate the following output-

    Total Candies that can be bought: 13
    Copy after login

    The above is the detailed content of Maximum number of candies that can be purchased. For more information, please follow other related articles on the PHP Chinese website!

    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    1 months ago By 尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. Best Graphic Settings
    1 months ago By 尊渡假赌尊渡假赌尊渡假赌
    Will R.E.P.O. Have Crossplay?
    1 months ago By 尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    How to buy two items from one store on Pinduoduo How to buy two items from the same store together How to buy two items from one store on Pinduoduo How to buy two items from the same store together Apr 01, 2024 pm 10:19 PM

    Pinduoduo app official download free latest version is a very good mobile shopping software. The platform provides a wide range of products. You can buy them anytime and anywhere. The shopping method is very simple. Open the search bar and enter the product name to find it accurately. Swipe up or down to choose the next one-click payment. You can enjoy many great discounts every day. You can get red envelope coupons of different amounts for free. You can also invite friends and family to join together for shopping, creating a comfortable and refreshing shopping method. If you want to buy There are all of them. Next, the editor will provide Pinduoduo partners with details on how to buy two products from the same store together. 1. Go to the [Pinduoduo] homepage with your mobile phone and select the product you want to buy. 2. After entering the product homepage, click the [Collect] button below. 3. Collection

    How to buy different models of the same product on Pinduoduo_ How to place an order with multiple models at once on Pinduoduo How to buy different models of the same product on Pinduoduo_ How to place an order with multiple models at once on Pinduoduo Mar 20, 2024 pm 10:51 PM

    Pinduoduo has launched the “select multiple models at once” function, which allows you to select different models of the same product and place an order at once. The editor has compiled some relevant content to share with you. Friends in need can come and take a look. How to buy different models of the same product on Pinduoduo 1. First open the page of the product you want to buy, click [Collection] in the lower left corner. 2. Then return to the personal center and use [Collection] to view the product you just collected. 3. Click [Select multiple models at once] below the product specifications to add products of different specifications. 4. After the purchase is completed, select the order and click the payment in the lower right corner.

    How to purchase Tencent Video advance on-demand How to purchase Tencent Video advance on-demand Mar 08, 2024 am 08:00 AM

    Tencent Video software brings together a large amount of film and television content, many of which have advanced screening functions for some well-known film and television works. This function allows many viewers to enjoy exciting episodes in advance. However, especially for users with Tencent Video SVIP membership, they do not yet know whether they can enjoy the privilege of advanced on-demand viewing, so this tutorial guide will Let’s introduce in detail how to use the super on-demand function in Tencent Video. Hope it helps users who don’t know yet! Can Tencent Video svip watch advance on-demand? Answer: No. 1. Advanced on-demand requires users to first activate Tencent Video’s membership service, and then pay extra to purchase unbroadcast episodes; 2. Member rights: super theater, growth acceleration, super coupons, and privilege additions

    Where can I buy Optimism coins? Which platform is Optimism Coin traded on? Where can I buy Optimism coins? Which platform is Optimism Coin traded on? Feb 28, 2024 pm 04:40 PM

    Where can I buy Optimism coins? Optimism Coin can be purchased on a number of cryptocurrency exchanges, including: Binance Huobi OKX Matcha Gate.io Kraken Bittrex Poloniex Coinbase Pro Gemini You will need to register an account with these exchanges and complete identity verification before you can purchase Optimism Coin. The steps to purchase Optimism coins are as follows: Choose a safe and reliable exchange. Register an account and complete identity verification. Fund your account. On the trading interface of the trading platform, find the trading pair of Optimism currency, such as OP/USDT. Enter the amount of Optimism coins you want to purchase and click

    Where can I buy EC coins? On which platform are EC coins traded? Where can I buy EC coins? On which platform are EC coins traded? Feb 27, 2024 pm 04:49 PM

    Where can I buy EC coins? On which platform are EC coins traded? Text: EC coin (Ethereum Classic, abbreviation: ETC) is an open source, decentralized blockchain platform and the first hard fork currency of Ethereum. EC Coin has a similar technical foundation to Ethereum, but there are also some key differences. For example, EC Coin has a shorter block time and does not use the Proof of Stake (PoS) consensus mechanism. EC coins can be purchased on many cryptocurrency trading platforms, including: Centralized exchanges: Binance, Huobi, OKX, Matcha, Gate.io, Kraken, Bittrex, Poloniex, etc. Decentralized exchanges: PancakeSwap, Uniswap, Su

    Where to buy grapefruit coins? Which platform is used to trade Yuzi Coin? Where to buy grapefruit coins? Which platform is used to trade Yuzi Coin? Feb 27, 2024 pm 07:50 PM

    YuziCoin (YZC for short) is a digital currency based on blockchain technology and was created by the YuziCoin Foundation in August 2017. The logo of Yuzi Coin mainly features the image of grapefruit. This iconic design quickly attracted attention in the online world. Yuzu Coin trading platform centralized exchanges: Binance, Huobi, OKX, Matcha, Gate.io, Kraken, Bittrex, Poloniex, etc. Decentralized exchanges: PancakeSwap, Uniswap, Sushiswap, etc. Steps to purchase grapefruit coins: Select a trading platform, register an account, recharge funds, purchase grapefruit coins, withdraw grapefruit coins, grapefruit coins wallet software wallet: grapefruit coin official wallet, MetaMask, T

    How to buy 99Bitcoins (99BTC) tokens? Step-by-step guide to buying 99Bitcoins (99BTC) tokens How to buy 99Bitcoins (99BTC) tokens? Step-by-step guide to buying 99Bitcoins (99BTC) tokens Jun 03, 2024 pm 01:20 PM

    99Bitcoins is a well-known educational platform that provides tutorials and guides related to cryptocurrency. Its developers recently started a pre-sale of its native token 99BTC to expand the community and attract investors. Thanks to its innovative learn-to-earn model, the project has raised over $932,000 to date. With this in mind, we have created a guide that will show you step by step how to buy 99Bitcoins. We'll also explain what makes this project profitable for investors so you can make an informed decision. What is 99 Bitcoins? Before we show you how to buy Bitcoin for $99, let’s first explain what makes this platform unique. As mentioned above, 99Bitcoins is a company founded in 2

    Which one is more worth buying, vivox100 or vivox100pro? Which one is more worth buying, vivox100 or vivox100pro? Mar 18, 2024 pm 03:51 PM

    With the continuous development of technology, the smartphone market has gradually become an indispensable part of people's lives. Among many mobile phone brands, vivox100 and vivox100pro have attracted much attention from users. So, among these two mobile phones, which one is more worth buying? Next we will conduct a detailed comparative analysis of these two mobile phones. First of all, from the appearance design point of view, there is not much difference in appearance between vivox100 and vivox100pro. They all adopt a streamlined design, with rounded body frames and hand-held

    See all articles