- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- 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

Given the task is to find the maximum number of people that can be killed with strength P. Consider a row with infinite people and each of them have an index number starting from 1.

The strength of the s^{th} person is represented by s^{2}. After killing a person with s strength your
strength also decreases by s.

Let’s now understand what we have to do using an example −

P = 20

3

Strength of 1st person = 1 * 1 = 1 < 20, therefore 1st person can be killed. Remaining strength = P – 1 = 20 – 1 = 19 Strength of 2nd person = 2 * 2 = 4 < 19, therefore 2nd person can be killed. Remaining strength = P – 4 = 19 – 4 = 15 Strength of 3rd person = 3 * 3 = 9 < 15, therefore 3rd person can be killed. Remaining strength = P – 9 = 15 – 9 = 6 Strength of 4th person = 4 * 4 = 16 > 6, therefore 4th person cannot be killed. Output = 3

30

4

In main() function initialize P = 30 of type int as it will store the strength and pass it into Max() function.

In Max() function initialize s = 0 and P = 0 both of type int.

Loop from j = 1 till j * j <= P

Put s = s + (j * j) and if s <= P add 1 to ans, else break;

Return ans.

#include <bits/stdc++.h> using namespace std; int Max(int P){ int s = 0, ans = 0; for (int j = 1; j * j <= P; j++){ s = s + (j * j); if (s <= P) ans++; else break; } return ans; } //main function int main(){ //Strength int P = 30; cout << “Maximum number of people that can be killed with strength P are: ”<<Max(P); return 0; }

Maximum number of people that can be killed with strength P are: 4

- Related Questions & Answers
- Maximum Number of Events That Can Be Attended in C++
- Maximum number of candies that can be bought in C
- Maximum number of threads that can be created within a process in C
- Find maximum number that can be formed using digits of a given number in C++
- Maximum number of partitions that can be sorted individually to make sorted in C++
- Maximum litres of water that can be bought with N Rupees in C++
- Program to find maximum number enemies will be killed to place a bomb in C++?
- Program to find maximum number of package that can be bought by buyers in C++
- Maximum elements that can be made equal with k updates in C++
- Maximum number of parallelograms that can be made using the given length of line segments in C++
- Maximise the number of toys that can be purchased with amount K in C++
- Maximum money that can be withdrawn in two steps in C
- Program to find maximum number of people we can make happy in Python
- Maximum number that can be display on Seven Segment Display using N segments in C++
- Problem to Find Out the Maximum Number of Coins that Can be Collected in Python

Advertisements