
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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 [1].
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
delete arr[remove]
init := remove
return arr[0]
Let us see the following implementation to get better understanding:
Example
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[0] ob = Solution() n = 3 k = 2 print(ob.solve(n, k))
Input
3,2
Output
1
- Related Articles
- Program to find number of minimum steps to reach last index in Python
- Python - Find starting index of all Nested Lists
- 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
- How to find the last index value of a string in Golang?
- Golang Program to update the ith index node value, when index is at the last index.
- Golang program to delete the ith index node, when the index is the last index in the linked list.
- Program to check whether we can reach last position from index 0 in Python
- C# program to remove characters starting at a particular index in StringBuilder
- The :last-child Pseudo-class in CSS
- Program to find id of candidate who have hot majority vote in Python
- Role of CSS :last-child Selector
- jQuery :last-child Selector
- Program to find the K-th last node of a linked list in Python
- Program to find last digit of the given sequence for given n in Python
