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++ code to count copy operations without exceeding k
Suppose we have an array A with n elements and another number k. There are n piles of candies. The ith pile has A[i] number of candies. We can perform the operation on two indices i and j (i != j), then add another A[i] number of candies to A[i] (A[i] will not be reduced). We can perform this operation any number of times, but unfortunately if some pile contains strictly more than k candies we cannot perform the operation anymore. We have to find the maximum number of times we can perform this operation.
So, if the input is like A = [1, 2, 3]; k = 5, then the output will be 5, because we can take i = 0 and for j = 1 we can perform three times and for j = 2 we can perform two times. So in total 5 times.
Steps
To solve this, we will follow these steps −
ans := 0 n := size of A sort the array A for initialize i := 1, when iExample
Let us see the following implementation to get better understanding −
#includeusing namespace std; int solve(vector A, int k){ int ans = 0; int n = A.size(); sort(A.begin(), A.end()); for (int i = 1; i A = { 1, 2, 3 }; int k = 5; cout Input
{ 1, 2, 3 }, 5Output
5
