Priyanka and Toys in Algorithm | HackerRank Programming Solutions | HackerRank Problem Solving Solutions in Java [💯Correct]

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 Priyanka 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 ProblemPriyanka and Toys – Hacker Rank Solution

Priyanka and Toys – Hacker Rank Solution


Priyanka works for an international toy company that ships by container. Her task is to the determine the lowest cost way to combine her orders for shipping. She has a list of item weights. The shipping company has a requirement that all items loaded in a container must weigh less than or equal to 4 units plus the weight of the minimum weight item. All items meeting that requirement will be shipped in one container.

What is the smallest number of containers that can be contracted to ship the items based on the given list of weights?

For example, there are items with weights . This can be broken into two containers:  and . Each container will contain items weighing within  units of the minimum weight item.

Function Description

Complete the toys function in the editor below. It should return the minimum number of containers required to ship.

toys has the following parameter(s):

  • w: an array of integers that represent the weights of each order to ship

Input Format

The first line contains an integer , the number of orders to ship.
The next line contains  space-separated integers, , representing the orders in a weight array.


Output Format

Return the integer value of the number of containers Priyanka must contract to ship all of the toys.

Sample Input

1 2 3 21 7 12 14 21

Sample Output



The first container holds items weighing ,  and . (weights in range )
The second container holds the items weighing  units. ()
The third container holds the item weighing  units. ()
The fourth container holds the items weighing  and  units. ()

 containers are required.

Priyanka and Toys – Hacker Rank Solution
import java.util.Arrays;
import java.util.Scanner;

 * @author Techno-RJ
public class PriyankaAndToys {
	static int toys(int[] w) {
		int containerCouter = 1, weightLimit = w[0];
		for (int i = 0; i < w.length; i++) {
			if (w[i] > weightLimit + 4) {
				weightLimit = w[i];
		return containerCouter;

	public static void main(String[] args) {
		Scanner in = new Scanner(;
		int n = in.nextInt();
		int[] w = new int[n];
		for (int w_i = 0; w_i < n; w_i++) {
			w[w_i] = in.nextInt();
		int result = toys(w);

Leave a Comment

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker🙏.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock