- 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

# 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 Questions & Answers
- Program to find sum of k non-overlapping sublists whose sum is maximum in C++
- C++ program to find maximum possible value of XORed sum
- C++ program to find maximum possible value for which XORed sum is maximum
- Find maximum sum possible equal sum of three stacks in C++
- Program to find three unique elements from list whose sum is closest to k Python
- C++ Program to Find k Numbers Closest to Median of S, Where S is a Set of n Numbers
- Program to find product of few numbers whose sum is given in Python
- Program to find number of sublists whose sum is given target in python
- Program to find length of longest sublist whose sum is 0 in Python
- C++ program to find length of non empty substring whose sum is even
- C++ program to find range whose sum is same as n
- Adjacent elements of array whose sum is closest to 0 - JavaScript
- Maximum Possible Sum of Products in JavaScript
- Program to find sum of rectangle whose sum at most k in Python
- Find the Number Whose Sum of XOR with Given Array Range is Maximum using C++

Advertisements