Uva 10107 Solution

#include<bits/stdc++.h>
#define ll long long int
using namespace std;
ll a[1001];
int main()
{
    ll i, j, n, m, b;
    i = 0;
    vector<ll>a;

    while(cin >> b)
    {
        a.push_back(b);
        sort(a.begin(), a.end());

        if(i%2 == 1)
        {
            n = i/2;
            m = (a[n] + a[n+1])/2;
            cout << m << endl;
        }

        if(i%2 == 0)
        {
            n = i/2;
            cout << a[n] << endl;
        }
        i++;
    }
    return 0;
}

Comments

  1. pleease explain this solution

    ReplyDelete
  2. Dear Sir,
    for solve this problem first we will be know what is Median?

    median of this array [1, 2, 3, 4, 5, 6, 7, 8, 9] is -------> 5. because array length is odd.

    On the other hand.........

    median of this array [1, 2, 3, 4, 5, 6, 7, 8] is ----------> (4+5)/2 = 4. because array length is even.

    In this problem, first given 1. so median is 1.

    second given 3. so the array is [1, 3] and median is -----> (1+3)/2 = 2. because array length is even

    third given 4. so the array is [1, 3, 4] and median is ------> 3. because array length is odd

    fourth given 60. so the array is [1, 3, 4, 60] and median is --------> (3+4)/2 = 3.

    fifth given 70. so the array is [1, 3, 4, 60, 70] and median is --------> 4.

    .................. so on.......

    I hope you understand this problem........... Happy Coding....

    ReplyDelete

Post a Comment