- 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 find out the minimum number of operations required to defeat an enemy

Suppose, we are playing a video game where the protagonist uses knives to defeat his enemies. The protagonist can use the knife for slashing the enemy or he can throw it towards the enemy. If the protagonist throws a knife, that cannot be retrieved again. The damage dealt by knife i is given in the array 'knives' where each element is of the form {slash, throw}. 'Slash' means the damage done to an enemy by slashing them with that knife and 'throw' means damage done to them by throwing that particular knife. Slashing can be done unlimited times, but a knife can only once be thrown. Now, an enemy appears who has health h. We have to find out the minimum number of operations (slashing or throwing) that are necessary to defeat the enemy. The enemy gets defeated when they have 0 health.

So, if the input is like n = 2, h = 11, knives = {{4, 5}, {3, 6}}, then the output will be 2.

If the protagonist throws both knives, the damage dealt is 5 + 6 = 11. The enemy's health becomes 0, so they get defeated.

## Steps

To solve this, we will follow these steps −

val := 0 for initialize i := 0, when i < n, update (increase i by 1), do: val := maximum of (val and first value of knives[i]) sort the array knives res := 0 for initialize i := 0, when i < n, update (increase i by 1), do: if second value of knives[i] > val, then: h := h - second value of knives[i] (increase res by 1) if h <= 0, then: print(res) exit Otherwise Come out from the loop print((res + ceiling value of (h / (double))))

## Example

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h> using namespace std; void solve(int n, int h, vector<pair<int, int>> knives){ int val = 0; for(int i = 0; i < n; i++){ val = max(val, knives[i].first); } sort(knives.begin(), knives.end()); int res = 0; for(int i = 0; i < n; i++){ if(knives[i].second > val){ h -= knives[i].second; res++; if(h <= 0){ cout << res << endl; return; } } else break; } cout << (res + ceil(h / (double)val)) << endl; } int main() { int n = 2, h = 11; vector<pair<int, int>> knives = {{4, 5}, {3, 6}}; solve(n, h, knives); return 0; }

## Input

2, 11, {{4, 5}, {3, 6}}

## Output

2

- Related Articles
- Program to find minimum number of operations required to make one number to another in Python
- Program to find minimum number of operations required to make lists strictly Increasing in python
- Minimum operations required to remove an array in C++
- Find out the minimum number of coins required to pay total amount in C++
- Program to find minimum number of operations required to make one string substring of other in Python
- Program to find number of operations required to remove palindromic sublists in C++
- Minimum number of operations required to sum to binary string S using C++.
- Minimum number of operations required to delete all elements of the array using C++.
- Program to find minimum number of steps required to catch the opponent in C++
- Program to count minimum number of operations required to make numbers non coprime in Python?
- Minimum number of given operations required to make two strings equal using C++.
- Find minimum number of merge operations to make an array palindrome in C++
- C++ program to find minimum how many operations needed to make number 0
- Program to find minimum number of pins required to hang all banners in C++
- Program to find number of given operations required to reach Target in Python