- 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 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.

https://www.tutorialspoint.com/data_structures_algorithms/greedy_algorithms.htm

https://www.tutorialspoint.com/data_structures_algorithms/dynamic_programming.htm

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.

## Steps

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

## Example

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; }

## Input

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

## Output

6

- Related Articles
- C++ Program to count operations to make filename valid
- C++ code to count number of operations to make two arrays same
- Minimum delete operations to make all elements of array same in C++.
- Program to count number of operations required to all cells into same color in Python
- Program to count number of operations required to convert all values into same in Python?
- C++ code to count operations to make array sorted
- C++ program to count minimum number of operations needed to make number n to 1
- Program to count number of operations needed to make string as concatenation of same string twice in Python
- C++ program to count expected number of operations needed for all node removal
- Program to count minimum number of operations to flip columns to make target in Python
- JavaScript program for Minimum move to end operations to make all strings equal
- Minimum move to end operations to make all strings equal in C++
- C# Program to perform all Basic Arithmetic Operations
- Count of operations to make a binary string “ab” free in C++
- Program to count minimum number of operations required to make numbers non coprime in Python?