Hello **Programmers/Coders,** Today we are going to share ** solutions of Programming problems of HackerRank, Algorithm Solutions of Problem Solving Section in Java**. At Each Problem with Successful submission with

**all Test Cases Passed,**you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.

In this post, you will find the solution for **Mark and Toys** **in Java-HackerRank Problem**. We are providing the **correct and tested solutions** of coding problems present on **HackerRank**. If you are not able to solve any problem, then you can take help from our Blog/website.

Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.

**Introduction To Algorithm**

The word **Algorithm** means “a process or set of rules to be followed in calculations or other problem-solving operations”. Therefore Algorithm refers to a set of rules/instructions that step-by-step define how a work is to be executed upon in order to get the expected results.

**Advantages of Algorithms:**

- It is easy to understand.
- Algorithm is a step-wise representation of a solution to a given problem.
- In Algorithm the problem is broken down into smaller pieces or steps hence, it is easier for the programmer to convert it into an actual program.

** Link for the Problem** – Mark and Toys – Hacker Rank Solution

Mark and Toys – Hacker Rank Solution

**Problem:**

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.

**Example**

The budget is units of currency. He can buy items that cost for , or for units. The maximum is 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

**Returns**

*int:*the maximum number of toys

**Input Format**

The first line contains two integers, and , the number of priced toys and the amount Mark has to spend.

The next line contains space-separated integers

**Constraints**

A toy can’t be bought multiple times.

**Sample Input**

7 50 1 12 5 111 200 1000 10

**Sample Output**

4

**Explanation**

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

Mark and Toys – Hacker Rank Solution

import java.util.Arrays; import java.util.Scanner; /** * @author Techno-RJ * */ public class MarkAndToys { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int K = in.nextInt(); int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = in.nextInt(); } Arrays.sort(a); int toyCount = 0, sum = 0; for (int i = 0; i < a.length; i++) { sum += a[i]; if (sum <= K) { toyCount++; } else { break; } } System.out.println(toyCount); in.close(); } }