Thursday, October 22, 2015

Function to find median from an array of integers

#include <iostream>
#include <sstream>
#include <vector>
#include <queue>
#include <stdlib.h>
#include <iomanip>

using namespace std;

void GetMedian(int arr[], int len)
{
    priority_queue <int, vector <int>, greater <int>> minheap;
   
    priority_queue <int, vector <int>, less <int>> maxheap;
   
    int i, x;
   
    for (i=0; i<len; i++)
    {
        x = arr[i];
        if (!maxheap.empty() && x > maxheap.top()) {
            minheap.emplace(x);
        } else {
            maxheap.emplace(x);
        }
        if (minheap.size() > maxheap.size() + 1) {
            maxheap.emplace(minheap.top());
            minheap.pop();
        } else if (maxheap.size() > minheap.size() + 1) {
            minheap.emplace(maxheap.top());
            maxheap.pop();
        }
       
        cout << fixed;
        cout << setprecision(1);
       
        if (minheap.size() == maxheap.size()) {
            cout << (double) (0.5 * (minheap.top() + maxheap.top())) << endl;
        } else {
            cout << (double) ((minheap.size() > maxheap.size() ? minheap.top() : maxheap.top())) << endl;
        }
    }
}

int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */  
    int N, i;
   
    cin >> N;
   
    int A[N];
   
    for (i = 0; i < N; i++)
    {
        cin >> A[i];
    }
   
    GetMedian(A, N);
   
    return 0;
}

Monday, June 3, 2013

Previous blog post regarding P2P chat application referenced on Frozen Mountain's website/blog

http://www.frozenmountain.com/Blog/post/IceLink-WebSync-and-Unity.aspx

Creating a P2P chat application using IceLink and WebSync in Unity3D


Here's a P2P chat application I recently created re-using the basics in the text chat sample provided by Frozen Mountain (http://www.frozenmountain.com/). Frozen Mountain offers software libraries for developing real-time applications. In this sample, I have enabled usage of Frozen Mountain's IceLink and WebSync libraries in a Unity3D application demonstrating a simple text chat among peers.

Key Technologies/Software used -

  • IceLink
  • WebSync
  • Unity3D
  • MonoDevelop/Mono

The sample requires download and execution of IceLink and WebSync servers, which are available via the Frozen Mountain link above. This particular sample was developed using the Community Edition of the IceLink and WebSync servers running on the local machine (a Windows 7 system); however, the server URLs/IP addresses may be changed as appropriate using the source code provided.


Here's a snapshot of the sample in action -




Sunday, May 26, 2013

HP Pavilion dv7-6c95dx integrated webcam (HP TrueVision HD)

The integrated webcam of my HP Pavilion laptop stopped working one day and it turns out to be a common problem due to a short connector as reported by many users. The following recommended steps fixed the problem -