Maximum number of people that can be killed with strength P | C ++ implementation of the approach ; Function to return the maximum number of people that can be killed ; Loop will break when the ith person cannot be killed ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxPeople ( int p ) { int tmp = 0 , count = 0 ; for ( int i = 1 ; i * i <= p ; i ++ ) { tmp = tmp + ( i * i ) ; if ( tmp <= p ) count ++ ; else break ; } return count ; } int main ( ) { int p = 14 ; cout << maxPeople ( p ) ; return 0 ; }