 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP 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
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 i < n, update (increase i by 1), do: ans := ans + (k - A[i]) return ans
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; int solve(vector<int> A, int k){    int ans = 0;    int n = A.size();    sort(A.begin(), A.end());    for (int i = 1; i < n; i++){       ans += (k - A[i]) / A[0];    }    return ans; } int main(){    vector<int> A = { 1, 2, 3 };    int k = 5;    cout << solve(A, k) << endl; } Input
{ 1, 2, 3 }, 5  Output
5
