- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures

- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces

# C++ code to count children who will get ball after each throw

Suppose we have a number n. Few kids are standing on a circle. They are numbered from 1 to n, they are in clockwise order and the child number 1 is holding the ball. First the child number 1 throws the ball to the next one clockwise, Then the child number 2 throws the ball to the next but one child, (to the child number 4), then the fourth child throws the ball to the child number 7 and so on. When a ball is thrown it may pass the beginning of the circle. Not all the children get the ball during the game. If a child doesn't get the ball, We have to find the numbers of the children who will get the ball after each throw.

So, if the input is like n = 10, then the output will be [2, 4, 7, 1, 6, 2, 9, 7, 6].

## Steps

To solve this, we will follow these steps −

p := 1 for initialize i := 1, when i < n, update (increase i by 1), do: p := p + i p := p mod n if not p is non-zero, then: p := n print p

## Example

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h> using namespace std; void solve(int n){ int p = 1; for (int i = 1; i < n; i++){ p += i; p %= n; if (!p) p = n; printf("%d, ", p); } } int main(){ int n = 10; solve(n); }

## Input

10

## Output

2, 4, 7, 1, 6, 2, 9, 7, 6,

- Related Articles
- C++ code to count who have declined invitation
- Can we write any code after throw statement in Java?
- C++ code to get minimum sum of cards after discarding
- C++ code to count number of dice rolls to get target x
- C++ code to find out if everyone will get ice cream
- C++ code to count numbers after division elements greater than half of array size
- C++ code to find who cannot give sufficient candies
- Program to find hoe many children will get candies while distributing them maintaining the rules in Python
- How to get the children of $(this) selector?
- C++ code to find minimal tiredness after meeting
- C++ code to get updated string with same 'a' and 'b' count
- Why does a moving ball stop after sometime?
- C++ program to count minimum how many minutes after there will be no new angry students
- C++ code to find out who won an n-round game
- Who has delivered the fastest ball in the history of cricket?