
- 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++ 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 i <= n, update (increase i by 1), do: a[i] := A[i - 1] sum := sum + a[i] if sum mod p is not equal to 0, then: return false k := sum / p for initialize i := 1, when i <= n, update (increase i by 1), do: b[i] := a[i] - a[(i mod n) + 1] sum := 0 for initialize i := 1, when i <= n, update (increase i by 1), do: a[i] := b[i] sum := sum + a[i] if sum is not equal to 0, then: return false for initialize i := 1, when i <= n, update (increase i by 1), do: if (a[i] + k) mod n is not equal to 0 or a[i] + k < 0, then: return false return true
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; bool solve(vector<int> A) { int n = A.size(); vector<int> a(n + 1); vector<int> b(n + 1); int sum = 0, p = n * (n + 1) / 2; for (int i = 1; i <= n; i++) { a[i] = A[i - 1]; sum += a[i]; } if (sum % p != 0) { return false; } int k = sum / p; for (int i = 1; i <= n; i++) { b[i] = a[i] - a[i % n + 1]; } sum = 0; for (int i = 1; i <= n; i++) { a[i] = b[i]; sum += a[i]; } if (sum != 0) { return false; } for (int i = 1; i <= n; i++) { if ((a[i] + k) % n != 0 || a[i] + k < 0) { return false; } } return true; } int main(){ vector<int> A = { 4, 5, 1, 2, 3 }; cout << solve(A) << endl; }
Input
{ 4, 5, 1, 2, 3 }
Output
1
- Related Articles
- Program to check we can cross river by stones or not in Python
- Program to find maximum number of boxes we can fit inside another boxes in python
- Remove Boxes in C++
- C++ code to find maximum stones we can pick from three heaps
- C++ program to check we can make two strings equal by swapping from third string
- Program to check whether we can take all courses or not in Python
- Program to check whether we can unlock all rooms or not in python
- Program to check whether we can form 24 by placing operators in python
- C++ code to find minimum stones after all operations
- C++ program to check we can buy product with given money or not
- Program to check we can reach end of list by starting from k in Python
- Program to check we can reach at position n by jumping or not in Python
- C++ program to create boxes and calculate volume and check using less than operator
- JavaScript Program to Check if all array elements can be converted to pronic numbers by rotating digits
- C# program to remove all the numbers after decimal places

Advertisements