Friday, December 17, 2010

Find Missing Number

Complexity o(n)
Input : arr[] = {2,4,1,5,6};
Output : 3
Code :
int arr[]={2,4,1,5,6};
int findMissingNumber(){
    int arrSize = sizeof(arr)/sizeof(int);
    int num = arr[0];
    int num2 = 1;
    int i;
    for( i= 1;i
            num ^=arr[i]; // xor all element of array
            num2 ^= (i+1); // xor all indexes of array from 2 to n+1
        }
    num2 ^= (i+1);
    int res = num^num2; // xor two results will give us the missing number from array
    return res;
    }


DS and Algorithms

How to find the power of a number with log(n) complexity?
Problem : Compute a^n where n belongs to N
Native approach : multiple the base exponent times. Complexity will be O(n).
How can we improve this.
Divide and Conquer method can be used.
a^n =
a^n/2 * a^n/2 if n is even;
a^(n–1)/2 * a^(n–1)/2 ⋅ a if n is odd.
T(n) = T(n/2) + Θ(1) ⇒ T(n) = O(lg n).
C++ Implementation :
double power(int base,int exp){
    if(exp == 0 ){
            return 1;
        }
    if(base == 0){
            return 0;
        }
    if(exp == 1){
            return base;
        }
    if(exp %2 == 0){
            return power(base,exp/2)* power(base,exp/2);
        }
    else{
            return (power(base,(exp-1)/2)*power(base,(exp-1)/2)*base);
        }
    }

Limitations : Negative exponent and base cases are not handled.








Tuesday, December 1, 2009

Biological Comparion of MVC

I have been thinking since long time that what could be the best source of doing good software architecture.
In this process a thought came to my mind that why not ask to nature who has done so any great creation and hence there must
some good architectural examples which i can get from it to develop my skill.But what is the best and very near to me example to start
with ????? hhhmmm .... what can be better then human body architecture??

In this time i was learning some web application development using ASP.net MVC.So I started comparing MVC with human body and hence i came up with
this title "Biological Comparison of MVC" , sounds interesting to me.

Following are my views.

1. Model : Any sort of idea,thought,past/current knowledge
2. View     : Any expression
3. Controller : Central Controller,Brain

When any request comes from outside world it will be first translated by Brain.
Brain uses any past/current knowledge and decided who can handle/respond to this request and sends it to that part.
Say i touched to any hot thing (which is a request) so brain will interpret and sees that what all body part have to
react on this and will sends signal to them.
Now body parts will take appropriate actions and that will get converted to expression.

Isn't it MVC?????????

I am still trying to find out still more good examples from this best creation.
s