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
C++ Program to check we can remove all stones by selecting boxes
Suppose we have an array A with N elements. Consider there are N boxes and they are arranged in a circle. The ith box contains A[i] stones. We have to check whether we can remove all stones from the boxes by repeatedly performing the operation: Select a box say ith box. For each j in range 1 to N, remove exactly j stones from the (i+j)th box. Here (N+k)th box is termed as kth box. This operation cannot be performed if a box does not contain sufficient number of stones.
So, if the input is like A = [4, 5, 1, 2, 3], then the output will be True, because we can remove all stones by starting from second box.
To solve this, we will follow these steps −
n := size of A Define an array a of size (n + 1) Define an array b of size (n + 1) sum := 0, p := n * (n + 1) for initialize i := 1, when iExample
Let us see the following implementation to get better understanding −
#includeusing namespace std; bool solve(vector A) { int n = A.size(); vector a(n + 1); vector b(n + 1); int sum = 0, p = n * (n + 1) / 2; for (int i = 1; i A = { 4, 5, 1, 2, 3 }; cout Input
{ 4, 5, 1, 2, 3 }Output
1
