
- 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 maximum possible median of elements whose sum is s
Suppose we have two numbers n and s. We have to find the maximum possible median of an array of n non-negative elements, such that the sum of elements is same as s.
So, if the input is like n = 3; s = 5, then the output will be 2, because for the array [1, 2, 2], the sum is 5 and median is 2.
Steps
To solve this, we will follow these steps −
m := floor of (n / 2) + 1 return floor of (s / m)
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; int solve(int n, int s) { int m = n / 2 + 1; return s / m; } int main() { int n = 3; int s = 5; cout << solve(n, s) << endl; }
Input
3, 5
Output
2
- Related Articles
- C++ program to find maximum possible value of XORed sum
- C++ program to find maximum possible value for which XORed sum is maximum
- Program to find sum of k non-overlapping sublists whose sum is maximum in C++
- Find maximum sum possible equal sum of three stacks in C++
- C++ Program to Find k Numbers Closest to Median of S, Where S is a Set of n Numbers
- C++ program to find range whose sum is same as n
- Program to find three unique elements from list whose sum is closest to k Python
- C++ program to find length of non empty substring whose sum is even
- C++ Program to find Median of Elements where Elements are stored in 2 different arrays
- Maximum Subarray Sum Excluding Certain Elements in C++ program
- Maximum Primes whose sum is equal to given N in C++
- Program to find maximum sum by flipping each row elements in Python
- Find the Number Whose Sum of XOR with Given Array Range is Maximum using C++
- C++ program to find maximum possible amount of allowance after playing the game
- Maximum sum of increasing order elements from n arrays in C++ program

Advertisements