# HackerRank Priyanka and Toys problem solution

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?

## Problem solution in Python.

```import sys

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}