Program to find out the number of people who get a food packet using Python

Suppose in a conference, there are two types of people. The first type prefers vegetarian lunch (denoted by 0), and the other type prefers non-vegetarian lunch (denoted by 1). There are limited food packets, and if vegetarians receive a non-vegetarian packet or vice versa, they will not take that packet and wait until they get their preferred one.

We are given two arrays: one containing food packets and another containing the queue of people with their preferences. If a person does not receive their preferred packet, they re-enter the queue at the end. We need to find the number of people who remain without food packets.

Problem Example

If the input is people = [0,1,1,0], packets = [0, 1, 0, 0], then the output will be 1.

Two people prefer non-vegetarian food, but there is only one non-vegetarian packet. The first person preferring non-vegetarian gets that packet, while the other person keeps waiting because there is no other non-vegetarian packet available.

Algorithm

To solve this problem, we follow these steps:

  • Count the number of vegetarian (0) and non-vegetarian (1) people

  • Iterate through the packets in order

  • For each packet, check if there are people waiting for that type

  • If yes, serve one person; if no, stop (remaining people cannot be served)

  • Return the count of people who cannot be served

Solution

def solve(people, packets):
    # Count vegetarian (0) and non-vegetarian (1) people
    temp_arr = [0, 0]
    for person in people:
        temp_arr[person] += 1
    
    k = 0
    while k < len(packets):
        if temp_arr[packets[k]] > 0:
            temp_arr[packets[k]] -= 1
        else:
            break
        k += 1
    
    return len(packets) - k

# Test the function
people = [0, 1, 1, 0]
packets = [0, 1, 0, 0]
result = solve(people, packets)
print(f"Number of people without food packets: {result}")
Number of people without food packets: 1

How It Works

The algorithm works as follows:

  1. Count preferences: temp_arr[0] stores vegetarian count, temp_arr[1] stores non-vegetarian count
  2. Process packets: For each packet in order, check if there are people waiting for that type
  3. Serve or stop: If people are waiting, serve one person; otherwise, stop processing
  4. Calculate result: Remaining packets indicate people who couldn't be served

Example Walkthrough

def solve_with_steps(people, packets):
    print(f"People preferences: {people}")
    print(f"Available packets: {packets}")
    
    temp_arr = [0, 0]
    for person in people:
        temp_arr[person] += 1
    
    print(f"Vegetarian people: {temp_arr[0]}, Non-vegetarian people: {temp_arr[1]}")
    
    k = 0
    while k < len(packets):
        packet_type = packets[k]
        print(f"Packet {k+1}: Type {packet_type}")
        
        if temp_arr[packet_type] > 0:
            temp_arr[packet_type] -= 1
            print(f"  Served! Remaining: Veg={temp_arr[0]}, Non-veg={temp_arr[1]}")
        else:
            print(f"  No one wants this packet type. Stopping.")
            break
        k += 1
    
    return len(packets) - k

result = solve_with_steps([0, 1, 1, 0], [0, 1, 0, 0])
print(f"\nFinal result: {result} people without food")
People preferences: [0, 1, 1, 0]
Available packets: [0, 1, 0, 0]
Vegetarian people: 2, Non-vegetarian people: 2
Packet 1: Type 0
  Served! Remaining: Veg=1, Non-veg=2
Packet 2: Type 1
  Served! Remaining: Veg=1, Non-veg=1
Packet 3: Type 0
  Served! Remaining: Veg=0, Non-veg=1

Final result: 1 people without food

Conclusion

This solution efficiently counts food preferences and processes packets in order. When no one wants the current packet type, the remaining people cannot be served, giving us the final count of people without food packets.

---
Updated on: 2026-03-26T13:54:30+05:30

520 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements