C++ Program to count operations to make all gifts counts same

Suppose we have two arrays A and B of size n each. There are n gifts and we want to give them to some children. The ith gift has A[i] candies and B[i] oranges. During one move, we can choose some gift and do one of the following operations −

  • Take out exactly one candy from this gift (if available);

  • Take out exactly one orange from this gift (if available);

  • Take out exactly one candy and exactly one orange from this gift (if available).

All gifts should be equal. This means that after some sequence of moves the following two conditions should be satisfied: A[0] = A[1] = ... = A[n-1] and B[0] = B[1] = ... = B[n-1]. We have to find the minimum number of moves required to equalize all the given gifts.

Problem Category

The above-mentioned problem can be solved by applying Greedy problem-solving techniques. The greedy algorithm techniques are types of algorithms where the current best solution is chosen instead of going through all possible solutions. Greedy algorithm techniques are also used to solve optimization problems, like its bigger brother dynamic programming. In dynamic programming, it is necessary to go through all possible subproblems to find out the optimal solution, but there is a drawback of it; that it takes more time and space. So, in various scenarios greedy technique is used to find out an optimal solution to a problem. Though it does not gives an optimal solution in all cases, if designed carefully it can yield a solution faster than a dynamic programming problem. Greedy techniques provide a locally optimal solution to an optimization problem. Examples of this technique include Kruskal's and Prim's Minimal Spanning Tree (MST) algorithm, Huffman Tree coding, Dijkstra's Single Source Shortest Path problem, etc.



So, if the input of our problem is like A = [3, 5, 6]; B = [3, 2, 3], then the output will be 6, because initially take out from B, so now B[0] becomes [2, 2, 3], then take out from A[1] so A = [3, 4, 6], then again take out from A[1] so A = [3, 3, 6], then from both A[2] and B[2], so they become [3, 3, 5] and [2, 2, 2], then from A[2], so it becomes A = [3, 3, 4] and again from A[2] to make it [3, 3, 3]. So now A has same number of candies and B has same number of oranges.


To solve this, we will follow these steps −

minA := inf
minB := inf
kq := 0
n := size of A
for initialize i := 0, when i < n, update (increase i by 1), do:
   minA := minimum of minA and A[i]
for initialize i := 0, when i < n, update (increase i by 1), do:
   minB := minimum of minB and B[i]
for initialize i := 0, when i < n, update (increase i by 1), do:
   kq := kq + maximum of (A[i] - minA) and (B[i] - minB)
return kq


Let us see the following implementation to get better understanding −

#include <bits/stdc++.h>
using namespace std;
int solve(vector<int> A, vector<int> B){
   int minA = 999, minB = 999, kq = 0;
   int n = A.size();
   for (int i = 0; i < n; i++)
      minA = min(minA, A[i]);
   for (int i = 0; i < n; i++)
      minB = min(minB, B[i]);
   for (int i = 0; i < n; i++)
      kq += max(A[i] - minA, B[i] - minB);
   return kq;
int main(){
   vector<int> A = { 3, 5, 6 };
   vector<int> B = { 3, 2, 3 };
   cout << solve(A, B) << endl;


{ 3, 5, 6 }, { 3, 2, 3 }