HackerRank Priyanka and Toys Problem Solution
In this post, we will solve HackerRank Priyanka and Toys Problem 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 w= [1, 2, 3, 4, 5, 10, 11, 12, 13]. This can be broken into two containers: [1, 2, 3, 4, 5] and [10, 11, 12, 13]. Each container will contain items weighing within 4 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 n, the number of orders to ship.
The next line contains n space-separated integers, w[1], w[2],….w[n], 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
8
1 2 3 21 7 12 14 21
Sample Output
4
Explanation
The first container holds items weighing 1, 2 and 3. (weights in range 1…5)
The second container holds the items weighing 21 units. (21…25)
The third container holds the item weighing 7 units. (7… 11)
The fourth container holds the items weighing 12 and 14 units. (12…14)
4 containers are required.
Priyanka and Toys C Solution
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
void quicksort(long long ar_size, long long * ar) {
long long piv;
piv = ar[ar_size-1];
long long i, temp, c=0;
long long prevmax, prevind=-1; //a/c ques
for (i=0; i<ar_size-1; i++){
if (ar[i] < piv){
if ((prevind>-1) && (ar[i] < prevmax)){
temp = ar[i];
ar[i] = prevmax;
ar[ prevind ] = temp;
prevind = prevind+1;
prevmax = ar[prevind];
}
c++;
}
else if (prevind<0) {
prevmax = ar[i];
prevind = i;
}
}
if (prevind>-1){
ar[ar_size-1] = ar[c];
ar[c] = piv;
}
if (ar_size>2){
if (c!=0)
quicksort(c, &ar[0]);
if (ar_size-c-1 != 0)
quicksort(ar_size-c-1, &ar[c+1]);
}
}
int main() {
unsigned long long k,i,w,c=0,j;
scanf("%llu", &k);
unsigned long long arr[k];
for (i=0; i<k; i++)
scanf("%llu", arr+i);
quicksort(k, &arr[0]);
for (i=0; i<k; i++){
w = arr[i];
for (j=i+1; j<k; j++){
if (arr[j] > w+4)
break;
}
i = j-1;
c++;
}
printf("%llu", c);
return 0;
}
Priyanka and Toys C++ Solution
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int n;
cin>>n;
int w[n];
for(int i=0; i<n; i++){
cin>>w[i];
}
sort(w,w+n);
int c = 0;
int pivot = w[0],i=0;
while(true){
c++;
pivot = w[i];
while(w[i] <= pivot +4 && i<n){
i++;
}
if(i>n-1)
{
break;
}
}
cout<<c<<endl;
return 0;
}
Priyanka and Toys C Sharp Solution
using System;
using System.Collections.Generic;
using System.IO;
class Solution {
static void Main(String[] args) {
Console.ReadLine();
string[] s = Console.ReadLine().Split(' ');
int[] W = new int[s.Length];
for (int i = 0; i < s.Length; ++i)
W[i] = int.Parse(s[i]);
Array.Sort(W);
int res = 1;
int WL = W[0] + 4;
for (int i =0; i < W.Length; ++i)
{
if (W[i] > WL)
{
WL = W[i] + 4;
res++;
}
}
Console.WriteLine(res);
}
}
Priyanka and Toys Java Solution
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();
}
}
Priyanka and Toys JavaScript Solution
function processData(input) {
var lines = input.split(/[\r\n]+/);
var count = +lines[0];
var arr = lines[1].split(" ").map(Number);
arr.sort((a,b)=>a-b);
var last = -1000;
var sum = 0;
while(arr.length > 0)
{
if(arr[0] <= last + 4)
arr.shift();
else
{
last = arr.shift();
sum++;
}
}
console.log(sum);
}
process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
processData(_input);
});
Priyanka and Toys Python Solution
input()
toys = [ int(i) for i in input().split()]
toys.sort()
last_buy = -10000
sum_buy = 0
for i in toys:
if i > last_buy + 4:
sum_buy+=1
last_buy = i
print(sum_buy)