# Priyanka and Toys

### Problem Statement :

```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?

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.```

### Solution :

```                            ```Solution in C :

In  C :

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int cmpfunc (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}
int main() {

long int n,i,ct=0,f;
scanf("%ld",&n);
long int*a=(long int*)calloc(sizeof(long int),n+10);
for(i=0;i<n;i++)scanf("%ld",&a[i]);
qsort(a, n, sizeof(long int), cmpfunc);
ct=1;f=a[0]+4;
for(i=1;i<n;i++){
if(a[i]<=f)continue;
else{
f=a[i]+4;
ct++;
}
}
printf("%ld",ct);
return 0;
}```
```

```                        ```Solution in C++ :

In  C++  :

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cctype>
#include <cstring>
#include <string>
#define ll long long
#define PB push_back
#define Maxn 100010
using namespace std;
int N , a[Maxn];
int main(){
scanf("%d",&N);
for (int i=1;i<=N;i++)
scanf("%d",&a[i]);
sort(a+1,a+N+1);
int pre = -100 ,ans = 0;
for (int i=1;i<=N;i++){
if (a[i] <= pre + 4) continue;
ans ++;
pre = a[i];
}
cout << ans << endl;
}```
```

```                        ```Solution in Java :

In  Java :

import java.io.*;
import java.util.*;

public class Solution {
private static Reader in;
private static PrintWriter out;

public static void main(String[] args) throws IOException {
in = new Reader();
out = new PrintWriter(System.out, true);
int N = in.nextInt();
int[] arr = new int[N];
for (int i = 0; i < N; i++)arr[i] = in.nextInt();
Arrays.sort(arr);
int last = -10000;
int count = 0;
for (int i = 0; i < N; i++) {
if (last+4 < arr[i]) {
last = arr[i];
count++;
}
}
out.println(count);
out.close();
System.exit(0);
}

static class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;

public Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}

public Reader(String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}

public String readLine() throws IOException {
byte[] buf = new byte[1024];
int cnt = 0;
byte c = read();
while (c <= ' ')
c = read();
do {
buf[cnt++] = c;
} while ((c = read()) != '\n');
return new String(buf, 0, cnt);
}

public String next() throws IOException {
byte[] buf = new byte[1024];
int cnt = 0;
byte c = read();
while (c <= ' ')
c = read();
do {
buf[cnt++] = c;
} while ((c = read()) > ' ');
return new String(buf, 0, cnt);
}

public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}

public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}

public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.')
while ((c = read()) >= '0' && c <= '9')
ret += (c - '0') / (div *= 10);
if (neg)
return -ret;
return ret;
}

private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}

private byte read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}

public void close() throws IOException {
if (din == null)
return;
din.close();
}
}
}```
```

```                        ```Solution in Python :

In  Python3 :

#!/usr/bin/env python

import sys

if __name__ == '__main__':
N = int(sys.stdin.readline())
W = sorted(map(int, sys.stdin.readline().split()))

cost = 0
last_chosen = -5

for w in W:
if w > last_chosen + 4:
cost += 1
last_chosen = w

print(cost)```
```

## Sparse Arrays

There is a collection of input strings and a collection of query strings. For each query string, determine how many times it occurs in the list of input strings. Return an array of the results. Example: strings=['ab', 'ab', 'abc'] queries=['ab', 'abc', 'bc'] There are instances of 'ab', 1 of 'abc' and 0 of 'bc'. For each query, add an element to the return array, results=[2,1,0]. Fun

## Array Manipulation

Starting with a 1-indexed array of zeros and a list of operations, for each operation add a value to each of the array element between two given indices, inclusive. Once all operations have been performed, return the maximum value in the array. Example: n=10 queries=[[1,5,3], [4,8,7], [6,9,1]] Queries are interpreted as follows: a b k 1 5 3 4 8 7 6 9 1 Add the valu

## Print the Elements of a Linked List

This is an to practice traversing a linked list. Given a pointer to the head node of a linked list, print each node's data element, one per line. If the head pointer is null (indicating the list is empty), there is nothing to print. Function Description: Complete the printLinkedList function in the editor below. printLinkedList has the following parameter(s): 1.SinglyLinkedListNode

## Insert a Node at the Tail of a Linked List

You are given the pointer to the head node of a linked list and an integer to add to the list. Create a new node with the given integer. Insert this node at the tail of the linked list and return the head node of the linked list formed after inserting this new node. The given head pointer may be null, meaning that the initial list is empty. Input Format: You have to complete the SinglyLink

## Insert a Node at the head of a Linked List

Given a pointer to the head of a linked list, insert a new node before the head. The next value in the new node should point to head and the data value should be replaced with a given value. Return a reference to the new head of the list. The head pointer given may be null meaning that the initial list is empty. Function Description: Complete the function insertNodeAtHead in the editor below

## Insert a node at a specific position in a linked list

Given the pointer to the head node of a linked list and an integer to insert at a certain position, create a new node with the given integer as its data attribute, insert this node at the desired position and return the head node. A position of 0 indicates head, a position of 1 indicates one node away from the head and so on. The head pointer given may be null meaning that the initial list is e