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
Program to find starting index of the child who receives last balloon in Python?
Suppose we have n children standing in a circle, waiting to receive balloons. The distribution starts with the kth child (indexing from 0), and after giving them a balloon, they leave the circle. The process continues clockwise, with every kth child receiving a balloon until only one child remains. We need to find the starting index of the child who receives the last balloon.
This is a variation of the famous Josephus problem, where we eliminate every kth person from a circle.
Problem Example
If the input is n = 3 and k = 2, then the output will be 1:
Initial circle: [0, 1, 2]
First round: Start at index 0, count 2 positions ? child at index 2 gets balloon and leaves. Circle becomes [0, 1]
Second round: Continue from index 2 (now 0), count 2 positions ? child at index 0 gets balloon and leaves. Circle becomes [1]
Child at index 1 receives the last balloon
Algorithm Steps
Create a list
childrencontaining indices from 0 to n−1Initialize
current_posto 0-
While more than one child remains in the circle:
Calculate
remove_index = (current_pos + k) % len(children)Remove the child at
remove_indexUpdate
current_pos = remove_index
Return the index of the remaining child
Implementation
class Solution:
def solve(self, n, k):
children = list(range(0, n))
current_pos = 0
while len(children) > 1:
remove_index = (current_pos + k) % len(children)
del children[remove_index]
current_pos = remove_index
return children[0]
# Test the solution
solution = Solution()
n = 3
k = 2
result = solution.solve(n, k)
print(f"Last child index: {result}")
Last child index: 1
Step-by-Step Trace
Let's trace through the algorithm with n = 5 and k = 3 ?
class Solution:
def solve_with_trace(self, n, k):
children = list(range(0, n))
current_pos = 0
round_num = 1
print(f"Initial circle: {children}")
while len(children) > 1:
remove_index = (current_pos + k) % len(children)
removed_child = children[remove_index]
del children[remove_index]
current_pos = remove_index
print(f"Round {round_num}: Remove child {removed_child}, circle: {children}")
round_num += 1
return children[0]
# Trace example
solution = Solution()
result = solution.solve_with_trace(5, 3)
print(f"Last child index: {result}")
Initial circle: [0, 1, 2, 3, 4] Round 1: Remove child 3, circle: [0, 1, 2, 4] Round 2: Remove child 2, circle: [0, 1, 4] Round 3: Remove child 0, circle: [1, 4] Round 4: Remove child 4, circle: [1] Last child index: 1
Optimized Mathematical Solution
The Josephus problem has a mathematical formula for direct calculation ?
def josephus_optimized(n, k):
"""
Mathematical solution using Josephus formula
J(n, k) = (J(n-1, k) + k) % n with J(1, k) = 0
"""
result = 0
for i in range(2, n + 1):
result = (result + k) % i
return result
# Test both approaches
n, k = 5, 3
result1 = josephus_optimized(n, k)
print(f"Optimized result: {result1}")
# Verify with simulation approach
solution = Solution()
result2 = solution.solve(n, k)
print(f"Simulation result: {result2}")
print(f"Results match: {result1 == result2}")
Optimized result: 1 Simulation result: 1 Results match: True
Comparison
| Method | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| Simulation | O(n²) | O(n) | Understanding the process |
| Mathematical | O(n) | O(1) | Large values of n |
Conclusion
The Josephus problem can be solved using simulation for better understanding or the mathematical formula for efficiency. The mathematical approach is preferred for large datasets due to its O(n) time complexity and O(1) space complexity.
