- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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:

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))

3,2

1

- Related Questions & Answers
- 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
- The :last-child Pseudo-class in CSS
- How to find index of last occurrence of a substring in a string in Python?
- jQuery :last-child Selector
- Golang Program to update the ith index node value, when index is at the last index.
- Program to find number of only child in a binary tree in python
- 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.
- Program to find out the number of people who get a food packet using Python
- jQuery :nth-last-child() Selector
- 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
- Program to find last digit of the given sequence for given n in Python

Advertisements