
- 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++ code to find minimum moves with weapons to kill enemy
Suppose we have an array A with n elements, and another number H. H is health of an enemy. We have n weapons and damaging power of ith weapon is A[i]. Different weapons can be used to kill the enemy. We cannot use same weapon twice in a row. We have to count minimum how many times we can use weapons to kill the enemy.
So, if the input is like A = [2, 1, 7]; H = 11, then the output will be 3, because if we use weapon with damage power 7, then use 2 then again use 7 we can kill the enemy.
Steps
To solve this, we will follow these steps −
sort the array A n := size of A x := (A[n - 1] + A[n - 2]) return H / x * 2 + (H mod x + A[n - 1] - 1) / A[n-1]
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; int solve(vector<int> A, int H){ sort(A.begin(), A.end()); int n = A.size(); int x = (A[n - 1] + A[n - 2]); return H / x * 2 + (H % x + A[n - 1] - 1) / A[n - 1]; } int main(){ vector<int> A = { 2, 1, 7 }; int H = 11; cout << solve(A, H) << endl; }
Input
{ 2, 1, 7 }, 11
Output
3
- Related Articles
- C++ Program to find out the minimum number of operations required to defeat an enemy
- Program to find minimum moves to make array complementary in Python
- C++ Program to find minimum moves to get all books contiguous
- C++ program to find minimum number of locations to place bombs to kill all monsters
- C++ code to find minimum arithmetic mean deviation
- C++ code to find minimum different digits to represent n
- C++ code to find minimum difference between concerts durations
- C++ code to find minimum stones after all operations
- C++ code to find minimum jump to reach home by frog
- C++ code to find minimum time needed to do all tasks
- Find minimum moves to reach target on an infinite line in C++
- Minimum Moves to Equal Array Elements in C++
- C++ code to find minimum k to get more votes from students
- C++ code to find minimum operations to make numbers c and d
- C++ code to find minimum correct string from given binary string

Advertisements