Selling Donuts
Source: codechef
Difficulty: 754

Selling Donuts
Chef owns a donut shop, which sells$N$different types of donuts, numbered from$1$to$N$.
Today, he baked$A_i$donuts of the$i$-th type.$M$customers will visit the shop. The$i$-th customer wants to buy
exactly one
donut of type$B_i$.
If Chef has no remaining donuts of type$i$, the$i$-th customer will become sad.
How many customers will become sad?
Input Format
The first line of input will contain a single integer$T$, denoting the number of test cases.
Each test case consists of three lines of input.
The first line of each test case contains two space-separated integers$N$and$M$— the number of donut types and the number of customers.
The second line contains$N$space-separated integers$A_1, A_2, \ldots, A_N$, the number of each type of donut that Chef has.
The third line contains$M$space-separated integers$B_1, B_2, \ldots, B_M$, denoting the types of donuts the$M$customers want.
Output Format
For each test case, output on a new line a single integer: the number of customers who will become sad.
Constraints$1 \leq T \leq 100$$1 \leq N \leq 100$$1 \leq M \leq 100$$0 \leq A_i \leq 100$$1 \leq B_i \leq N$Sample 1:
Explanation:
Test case$1$:
The number of donuts are initially$[1, 2, 0, 2]$. The customers behave as follows:$B_1 = 1$, so customer 1 wants a type-$1$donut. That's available, so he will be satisfied.
With one type-$1$donut gone, there are now$0$type-$1$donuts available.
The number of donuts are$[0, 2, 0, 2]$.$B_2 = 3$, so customer 2 wants a type-$3$donut. That's not available ($A_3 = 0$), so he will be sad.
The number of donuts are$[0, 2, 0, 2]$.$B_3 = 1$, so customer 1 wants a type-$1$donut. That's no longer available since the first customer bought the last type-$1$donut. So, he will be sad.
In the end, two customers are sad.
Test case$2$:
All the customers will receive the donuts they want.
Test case$3$:
The first three customers will receive the donuts they want, but the last three customers will be sad.

Sample Input:
3
4 3
1 2 0 2
1 3 1
4 5
5 2 3 1
2 2 1 4 3
2 6
2 1
1 2 1 2 1 2

Sample Output:
2
0
3

C++ solution:
#include <bits/stdc++.h>
#include<cmath>
using namespace std;
typedef long long ll;
int main() {
ll t;
cin>>t;
while(t--){
    ll n,m,count=0,x,y;
    cin>>n>>m;
    int arr[n],brr[m];
    for(int i=0;i<n;i++){
        cin>>arr[i];
    }
    for(int i=0;i<m;i++){
        cin>>brr[i];
    }
    for(int i=0;i<m;i++){
        if(arr[brr[i] - 1]>=1){
           
        arr[brr[i] - 1]--;
        }
        else{count++;}
    }
    cout<<count<<endl;
}
}

Go solution:
package main

import (
    "bufio"
    "fmt"
    "os"
    "strconv"
    "strings"
)

func main() {
    reader := bufio.NewReader(os.Stdin)

    // Read the number of test cases
    x, _ := reader.ReadString('\n')
    y, _ := strconv.Atoi(strings.TrimSpace(x))

    for z := 0; z < y; z++ {
        // Read N and M
        u, _ := reader.ReadString('\n')
        v := strings.Fields(u)
        w, _ := strconv.Atoi(v[0])
        r, _ := strconv.Atoi(v[1])

        // Read available donuts
        a, _ := reader.ReadString('\n')
        b := make([]int, w)
        c := strings.Fields(a)
        for d := 0; d < w; d++ {
            b[d], _ = strconv.Atoi(c[d])
        }

        // Read customer requests
        e, _ := reader.ReadString('\n')
        f := strings.Fields(e)

        // Track sad customers
        g := 0
        for h := 0; h < r; h++ {
            i, _ := strconv.Atoi(f[h])
            i-- // Convert 1-based to 0-based
            if b[i] > 0 {
                b[i]-- // Serve a donut
            } else {
                g++ // Sad customer
            }
        }

        // Print sad customer count
        fmt.Println(g)
    }
}

Java solution:
import java.util.*;
import java.lang.*;
import java.io.*;

class Codechef
{
    public static void main (String[] args)
    {
        // your code goes here
        Scanner read=new Scanner(System.in);
        int t=read.nextInt();
        while(t-->0){
            int n=read.nextInt();
            int m=read.nextInt();
            int[] a=new int[n];
            int[] b=new int[m];
            for(int i=0;i<n;i++){
                a[i]=read.nextInt();
            }
            for(int i=0;i<m;i++){
                b[i]=read.nextInt();
            }
            int result=0;
            for(int i=0;i<m;i++){
                if(a[b[i]-1]>0){
                    a[b[i]-1]--;
                }
                else{
                    result++;
                }
            }
            System.out.println(result);
        }
        read.close();
    }
}

JavaScript solution:
process.stdin.setEncoding("utf8");
let input = "";
process.stdin.on("data", function(user){
    input += user;
});
process.stdin.on('end',function(){
    const data = input.trim().split("\n");
    let T = parseInt(data[0]);
    let i = 1;
    while (i <= T*3){
        const [N, M] = data[i].split(" ").map(Number);
        const A = data[i+1].split(' ').map(Number);
        const B = data[i+2].split(' ').map(Number);
        let sad = 0;
        for(let j = 0; j < B.length; j++){
            let type = B[j];
          if(A[type-1]> 0){
              A[type-1]= A[type-1]-1;
          }
        else{
            sad++;
        }
        }    
        i += 3;
        console.log(sad);
    }
    
});

Python solution:
# cook your dish here
def solve():
    n, m = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))

    donut_counts = list(a)
    sad_customers = 0

    for donut_type_wanted in b:
        index_wanted = donut_type_wanted - 1
        if 0 <= index_wanted < n and donut_counts[index_wanted] > 0:
            donut_counts[index_wanted] -= 1
        else:
            sad_customers += 1

    print(sad_customers)

t = int(input())
for _ in range(t):
    solve()

Ruby solution:
def solve
  # Read N and M
  input = gets
  while input && input.strip.empty?
    input = gets
  end
  n, m = input.strip.split.map(&:to_i)

  # Read donut counts
  a = gets.strip.split.map(&:to_i)

  # Read customer requests
  b = gets.strip.split.map(&:to_i)

  # Make a copy of donut counts to modify
  donut_counts = a.dup
  sad_customers = 0

  b.each do |request|
    type = request - 1  # Convert to 0-based index
    if type.between?(0, n - 1) && donut_counts[type] > 0
      donut_counts[type] -= 1
    else
      sad_customers += 1
    end
  end

  puts sad_customers
end

# Read number of test cases
t = gets.to_i

# Process each test case
t.times { solve }