Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program to find out the size of the bus that can contain all the friends in the group
Suppose there are n number of student groups waiting to get back from their college to their home via the college bus. In each student group, there are m number of students. The student groups want to travel by bus without getting separated. They board the bus if and only if all the members of their group can get on the bus. Also, a group does not board the bus if their previous group has not boarded the bus or has already reached their destination.
Given the number of groups and the number of students in each group, we need to find the bus size so that the bus can transport all groups with no empty space each trip.
Problem Understanding
If the input is groups = [3, 4, 2, 2, 1, 4, 3, 5], then the output will be [12, 24].
- If the bus size is 12, it can contain groups 1-5 on the first trip, and the rest on the second trip
- If the bus size is 24, it can contain all groups in one trip
Algorithm Steps
To solve this problem, we follow these steps:
- Calculate cumulative sum of all group sizes
- Find all factors of the total number of students
- For each factor, check if it can divide groups perfectly (no empty seats)
- Return valid bus sizes
Implementation
from functools import reduce
def solve(groups):
# Calculate cumulative sum of group sizes
total = [groups[0]]
for i in range(1, len(groups)):
total.append(total[i - 1] + groups[i])
valid_sizes = []
# Check each possible bus size (factors of total students)
for size in get_factors(sum(groups)):
# Filter cumulative sums that are multiples of bus size
trip_endpoints = list(filter(lambda x: x % size == 0, total))
index = 1
is_valid = True
# Check if trips fill the bus completely
for endpoint in trip_endpoints:
if endpoint != size * index:
is_valid = False
break
index += 1
if is_valid:
valid_sizes.append(size)
return valid_sizes
def get_factors(n):
"""Return all factors of n in sorted order"""
factors = []
for i in range(1, int(n**0.5) + 1):
if n % i == 0:
factors.extend([i, n // i])
return sorted(set(factors))
# Test the function
groups = [3, 4, 2, 2, 1, 4, 3, 5]
result = solve(groups)
print(f"Valid bus sizes: {result}")
Valid bus sizes: [12, 24]
How It Works
Let's trace through the example with groups = [3, 4, 2, 2, 1, 4, 3, 5]:
groups = [3, 4, 2, 2, 1, 4, 3, 5]
# Step 1: Calculate cumulative sums
cumulative = [3, 7, 9, 11, 12, 16, 19, 24]
# Step 2: Total students = 24, factors = [1, 2, 3, 4, 6, 8, 12, 24]
# Step 3: Check bus size 12
# Trip endpoints where cumulative % 12 == 0: [12, 24]
# Check: 12 == 12*1 ?, 24 == 12*2 ? ? Valid
# Step 4: Check bus size 24
# Trip endpoints where cumulative % 24 == 0: [24]
# Check: 24 == 24*1 ? ? Valid
print("Cumulative sums:", [3, 7, 9, 11, 12, 16, 19, 24])
print("Factors of 24:", [1, 2, 3, 4, 6, 8, 12, 24])
print("Valid sizes:", [12, 24])
Cumulative sums: [3, 7, 9, 11, 12, 16, 19, 24] Factors of 24: [1, 2, 3, 4, 6, 8, 12, 24] Valid sizes: [12, 24]
Key Points
- Bus size must be a factor of the total number of students
- Each trip must completely fill the bus (no empty seats)
- Groups cannot be separated across trips
- Cumulative sums help identify valid trip endpoints
Conclusion
This solution finds all possible bus sizes that can transport student groups efficiently without wasting seats. It uses cumulative sums and factor analysis to determine valid configurations.
