HackerRank Çözümleri – Mark and Toys

HackerRank içerisinde bulunan “Mark and Toys” sorusunun açıklaması ve çözümü. Burada verilen oyuncakların fiyat listesine ve elinizdeki paraya göre, en fazla kaç adet oyuncak satın alabileceğinizin bulunması isteniyor.

► HackerRank – Mark and Toys: https://www.hackerrank.com/challenges/mark-and-toys/problem

► Problem açıklaması:

Mark and Jane are very happy after having their first child. Their son loves toys, so Mark wants to buy some. There are a number of different toys lying in front of him, tagged with their prices. Mark has only a certain amount to spend, and he wants to maximize the number of toys he buys with this money. Given a list of toy prices and an amount to spend, determine the maximum number of gifts he can buy.

Note Each toy can be purchased only once.


prices = [1, 2, 3, 4]

k = 7

The budget is 7 units of currency. He can buy items that cost [1, 2, 3] for 6, or [3, 4] for 7 units. The maximum is 3 items.

Function Description

Complete the function maximumToys in the editor below.

maximumToys has the following parameter(s):

int prices[n]: the toy prices

int k: Mark’s budget


int: the maximum number of toys

Sample Input

7 50

1 12 5 111 200 1000 10

Sample Output



He can buy only 4 toys at most. These toys have the following prices: 1, 12, 5, 10.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.