- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to find starting index of the child who receives last balloon in Python?
Suppose we have n children standing in a circle, and they are waiting to get a balloon. The distribution is carried out starting with the kth child (first at index 0), and giving them a balloon they left the circle. Now every kth child gets a balloon going clockwise until there is only one child left that gets a balloon. So if we have n and k, we have to find the starting index of the child that receives the last balloon.
So, if the input is like n = 3 k = 2, then the output will be 1, in the first round, child 2 gets a balloon, and leave so the circle will be [0, 1]. In second round, child 0 gets a balloon, circle will be .
To solve this, we will follow these steps:
arr := a new list from range 0 to n
init := 0
while size of arr > 1, do
remove := (init + k) mod size of arr
init := remove
Let us see the following implementation to get better understanding:
class Solution: def solve(self, n, k): arr = list(range(0, n)) init = 0 while len(arr) > 1: remove = (init + k) % len(arr) del arr[remove] init = remove return arr ob = Solution() n = 3 k = 2 print(ob.solve(n, k))
- Python - Find starting index of all Nested Lists
- Program to find number of minimum steps to reach last index in Python
- Role of CSS :last-child Selector
- How to find the last index value of a string in Golang?
- jQuery :last-child Selector
- The :last-child Pseudo-class in CSS
- How to find index of last occurrence of a substring in a string in Python?
- Program to find number of only child in a binary tree in python
- Golang Program to update the ith index node value, when index is at the last index.
- Program to find id of candidate who have hot majority vote in Python
- Golang program to delete the ith index node, when the index is the last index in the linked list.
- jQuery :nth-last-child() Selector
- Program to find out the number of people who get a food packet using Python
- How to find the index of the last occurrence of repeated values in a vector in R?
- Program to find the K-th last node of a linked list in Python