Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

HackerRank Priyanka and Toys problem solution

YASH PAL, 31 July 2024

In this HackerRank Priyanka and Toys problem solution Priyanka works for an international toy company that ships by the container. Her task is to 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 that meet 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?

HackerRank Priyanka and Toys problem solution

Problem solution in Python.

import sys

N = int(sys.stdin.readline())

a = list(sys.stdin.readline().split())
for index, item in enumerate(a):
    a[index] = int(item)

a = sorted(a)

count = 0
i = 0
while i < N:
    temp = int(a[i]) + 4
    count+=1
    while i < N and int(a[i]) <= temp:
        i+=1

print (count)

{“mode”:”full”,”isActive”:false}

Problem solution in Java.

import java.util.Arrays;
import java.util.Scanner;

public class Solution {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int numberOfToys = scanner.nextInt();
		scanner.nextLine();
		int[] toysWeight = new int[numberOfToys];
		int toyIndex;
		for(toyIndex = 0; toyIndex < numberOfToys; toyIndex++){
			toysWeight[toyIndex] = scanner.nextInt();
		}
		Arrays.sort(toysWeight);
		int units = 0;
		int weight;
		for(toyIndex = 0; toyIndex < numberOfToys; toyIndex++){
			units++;
			weight = toysWeight[toyIndex] + 4;
			toyIndex++;
			while(toyIndex < numberOfToys){
				if(toysWeight[toyIndex] <= weight){
					toyIndex++;
				} else {
					toyIndex--;
					break;
				}
			}
		}
		System.out.println(units);
		scanner.close();
	}

}

{“mode”:”full”,”isActive”:false}

Problem solution in C++.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */  
    int n;
    cin>>n;
    vector<int> num(n);
    for(int i = 0; i < n; i++)
        cin>>num[i];
    sort(num.begin(), num.end());
    int result = 0;
    int i = 0;
    int w = -10000000;
    while(i < n)
    {
        if(w + 4 < num[i])
        {
            result++;
            w = num[i];
        }
        i++;
    }
    cout<<result;
    return 0;
}

{“mode”:”full”,”isActive”:false}

Problem solution in C.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int size;
int part(int *ar,int left,int right)
    {
    int pivot=ar[right-1],i=left,flag,index;
    for(int j=left;j<right-1;j++)
        {
        if(ar[j]<=pivot)
            {
            flag=ar[i];
            ar[i]=ar[j];
            ar[j]=flag;
            i++;
        	}
        
    }
    flag=ar[i];
    ar[i]=ar[right-1];
    ar[right-1]=flag;    
  return i; 
}
int quicksort(int *ar,int p,int r)
    {
    if(p<r-1)
        {
        int q;
        q=part(ar,p,r);
    
        quicksort(ar,p,q);
        quicksort(ar,q+1,r);
    }
    return 0;
}
int main() {

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 
    int ar[100000],n,count=0,j=0,temp;
    scanf("%d",&n);
    size=n;
    for(int i=0;i<n;i++)
        {
        scanf("%d",&ar[i]);
    }
    quicksort(ar,0,n);
    while(j<size)
    {
    	temp=ar[j++]+4;
    	count++;
    	while(ar[j]<=temp&&j<size)
    	{
    		j++;
    	}
    	
    }
    printf("%d",count);
    return 0;
}

{“mode”:”full”,”isActive”:false}

algorithm coding problems

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes