
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ program to find maximum how many chocolates we can buy with at most k rupees
Suppose we have an array A with n elements, and other values l, r and k are there. Amal wants to buy chocolates and he will not buy too expensive chocolates, and not also too cheap chocolates. In the store, there are n different chocolate bars and the prices are represented in A. A chocolate bar is too expensive if its price is larger than r and too cheap if its price is less than l. He wants to spend at most k rupees. We have to find the maximum amount of chocolates he can buy.
So, if the input is like A = [1, 2, 3, 4, 5, 6]; l = 3; r = 5; k = 10, then the output will be 2, because he can buy chocolates worth rupees 3 and 4 by 7 rupees.
Steps
To solve this, we will follow these steps −
n := size of A ans := 0 sort the array A for initialize i := 0, when i < n, update (increase i by 1), do: if A[i] > k, then: Come out from the loop if A[i] >= l and A[i] <= r, then: k := k - A[i] (increase ans by 1) return ans
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; int solve(vector<int> A, int l, int r, int k) { int n = A.size(); int ans = 0; sort(A.begin(), A.end()); for (int i = 0; i < n; ++i) { if (A[i] > k) break; if (A[i] >= l && A[i] <= r) { k -= A[i]; ++ans; } } return ans; } int main() { vector<int> A = { 1, 2, 3, 4, 5, 6 }; int l = 3; int r = 5; int k = 10; cout << solve(A, l, r, k) << endl; }
Input
{ 1, 2, 3, 4, 5, 6 }, 3, 5, 10
Output
2
- Related Articles
- Program to find how many ways we can climb stairs (maximum steps at most k times) in Python
- Program to find maximum profit we can make after k Buy and Sell in python
- C++ program to find minimum how much rupees we have to pay to buy exactly n liters of water
- Program to find maximum how many water bottles we can drink in Python
- Program to find maximum sum by performing at most k negate operations in Python
- C++ program to find string with palindrome substring whose length is at most k
- Find Maximum number possible by doing at-most K swaps in C++
- C++ program to check we can buy product with given money or not
- C++ program to find minimum how many coins needed to buy binary string
- Maximum number of chocolates to be distributed equally among k students in C
- Maximum sum subsequence with at-least k distant elements in C++ program
- C++ program to calculate how many years needed to get X rupees with 1% interest
- Program to find minimum cost to reach final index with at most k steps in python
- Program to find maximum value of k for which we can maintain safe distance in Python
- Find the maximum cost of an array of pairs choosing at most K pairs in C++
