<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-3302780462223800314</id><updated>2024-09-07T13:52:56.260-07:00</updated><category term="Recursion"/><category term="Trees"/><category term="Searching and Sorting"/><category term="Strings"/><category term="Brain Teaser"/><category term="Bits n Bytes"/><category term="Stacks and Queues"/><category term="Dynamic Programming"/><category term="Linked List"/><category term="System"/><title type='text'>Puddle of Riddles!</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://puddleofriddles.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3302780462223800314/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://puddleofriddles.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='http://www.blogger.com/feeds/3302780462223800314/posts/default?start-index=26&amp;max-results=25&amp;redirect=false'/><author><name>Suchit Maindola</name><uri>http://www.blogger.com/profile/18147182105271900742</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuYKcXai-jog9Be2PRlIuFJq9KFTWyqQnQFg79vXWquwehfIkmBArsWnO6g6qQsyF-R1_GN1cZRJRe7OhgYiuuVuSzuM2IR5sK5mANoHjN_QPO2Rr64IEZl2aQmBf8JA/s220/IMG_0380.JPG'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>92</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-3302780462223800314.post-8541045501778625080</id><published>2013-01-23T21:26:00.003-08:00</published><updated>2013-01-23T21:26:25.777-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Recursion"/><title type='text'>Sudoku Solver!</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Solution: Below is the code to solve a sudoku.
&lt;br /&gt;
&lt;pre class=&quot;prettyprint lang-cpp&quot;&gt;
/*
Input Matrix:
 7  0  0  1  0  0  0  0  0 
 0  2  0  0  0  0  0  1  5 
 0  0  0  0  0  6  3  9  0 
 2  0  0  0  1  8  0  0  0 
 0  4  0  0  9  0  0  7  0 
 0  0  0  7  5  0  0  0  3 
 0  7  8  5  0  0  0  0  0 
 5  6  0  0  0  0  0  4  0 
 0  0  0  0  0  1  0  0  2 
*/

#define TRUE 1
#define FALSE 0
#define N 9
int S[N+1][N+1] = {0,};

void printMat(int S[][N+1])
{
    int i = 0;
    int j = 0;


    for(i=1; i&lt;=N; i++) {
        for(j=1; j&lt;=N; j++) {
            printf(&quot;%2d &quot;, S[i][j]);
        }
        printf(&quot;\n&quot;);
    }
}

void initMat(int S[][N+1])
{
    S[1][1] = 7;
    S[1][4] = 1;

    S[2][2] = 2;
    S[2][8] = 1;
    S[2][9] = 5;

    S[3][6] = 6;
    S[3][7] = 3;
    S[3][8] = 9;

    S[4][1] = 2;
    S[4][5] = 1;
    S[4][6] = 8;
    
    S[5][2] = 4;
    S[5][5] = 9;
    S[5][8] = 7;

    S[6][4] = 7;
    S[6][5] = 5;
    S[6][9] = 3;

    S[7][2] = 7;
    S[7][3] = 8;
    S[7][4] = 5;

    S[8][1] = 5;
    S[8][2] = 6;
    S[8][8] = 4;

    S[9][6] = 1;
    S[9][9] = 2;
}

int permitted(int S[][N+1], int i, int j, int a)
{
    int x;

    for(x=1; x&lt;=N; x++) {
        if(S[i][x] == a) {
            return FALSE;
        }
    }

    for(x=1; x&lt;=N; x++) {
        if(S[x][j] == a) {
            return FALSE;
        }
    }

    return TRUE;
}

void solve(int S[][N+1], int i, int j)
{
    int a = 0;

    /*
     * this row is filled, jump to next row
     */
    if(j &gt; N) {
        j = 0;
        i++;
    }

    /*
     * all rows are filled, print sol. and exit
     */
    if(i &gt; N) {
        printMat(S);
        printf(&quot;\n&quot;);
        exit(0);
    }

    /*
     * each cell can take a value belonging to [1,9]
     */
    for(a=1; a&lt;=N; a++) {
        /*
         * cell is already occupied, move to next cell
         */
        if (S[i][j]) {
            solve(S, i, j+1);
            return;
        }
        else if (permitted(S, i, j, a)) {
            S[i][j] = a;
            solve(S, i, j+1);
            S[i][j] = 0;
        } else {
            continue;
        }
    }
}

int main()
{
    initMat(S);
    printMat(S);
    printf(&quot;\n&quot;);
    solve(S, 1, 1);
}

&lt;/pre&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://puddleofriddles.blogspot.com/feeds/8541045501778625080/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3302780462223800314/8541045501778625080' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3302780462223800314/posts/default/8541045501778625080'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3302780462223800314/posts/default/8541045501778625080'/><link rel='alternate' type='text/html' href='http://puddleofriddles.blogspot.com/2013/01/sudoku-solver.html' title='Sudoku Solver!'/><author><name>Suchit Maindola</name><uri>http://www.blogger.com/profile/18147182105271900742</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuYKcXai-jog9Be2PRlIuFJq9KFTWyqQnQFg79vXWquwehfIkmBArsWnO6g6qQsyF-R1_GN1cZRJRe7OhgYiuuVuSzuM2IR5sK5mANoHjN_QPO2Rr64IEZl2aQmBf8JA/s220/IMG_0380.JPG'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3302780462223800314.post-5225578630549472002</id><published>2013-01-21T16:25:00.001-08:00</published><updated>2013-01-24T23:30:27.337-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Dynamic Programming"/><title type='text'>Knapsack Problem</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Solution: Below is the code using dynamic programming,&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;prettyprint lang-cpp&quot;&gt;
#define MAX(a,b) (a)&gt;(b)?(a):(b)
/*
 * Number of objects = 4
 * Capacity ig Knapsack = 8
 */
#define N 4
#define W 8
typedef struct {
    int32_t v;
    int32_t w;
} Object;

Object obj[N+1] = {{0, 0}, {15, 1}, {10, 5}, {9, 3}, {5, 4}};
int32_t V[N+1][W+1] = {0, };

int main()
{
    int32_t n = 0;
    int32_t w = 0;
    int32_t value = 0;
    int32_t weight = 0;

    for(n=1; n&lt;=N; n++) {
        for(w=0; w&lt;=W; w++) {
            weight = obj[n].w;
            value = obj[n].v;

            /*
             * Cases:
             * - Last item is excluded from the final set: V[n-1][w]
             * - Last item is included in the final set: value + V[n-1][w-weight]
             *
             * Corner case:
             * - If weight &gt; w, choose V[n-1][w]
             */
            if(w &gt;= weight) {
                V[n][w] = MAX( V[n-1][w], value + V[n-1][w-weight] );
            }
            else {
                V[n][w] = V[n-1][w];
            }
        }
    }

    printf(&quot;Ans = %d\n&quot;, V[N][W]);
    return 0;
}

&lt;/pre&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://puddleofriddles.blogspot.com/feeds/5225578630549472002/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3302780462223800314/5225578630549472002' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3302780462223800314/posts/default/5225578630549472002'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3302780462223800314/posts/default/5225578630549472002'/><link rel='alternate' type='text/html' href='http://puddleofriddles.blogspot.com/2013/01/knapsack-problem.html' title='Knapsack Problem'/><author><name>Suchit Maindola</name><uri>http://www.blogger.com/profile/18147182105271900742</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuYKcXai-jog9Be2PRlIuFJq9KFTWyqQnQFg79vXWquwehfIkmBArsWnO6g6qQsyF-R1_GN1cZRJRe7OhgYiuuVuSzuM2IR5sK5mANoHjN_QPO2Rr64IEZl2aQmBf8JA/s220/IMG_0380.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3302780462223800314.post-4085525408778125917</id><published>2012-04-13T22:45:00.000-07:00</published><updated>2012-04-23T02:53:05.220-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="System"/><title type='text'>Determine direction of stack growth.</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Solution #1: The direction of stack growth depends on OS as well as on the architecture. Below is a program to detect the direction of stack growth,&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;prettyprint lang-cpp&quot;&gt;void b(int* x)
{
    int y;

    //if address of y is less than address of x
    //then the direction is downwards, upwards otherwise
    if(&amp;y &lt; x)
        printf(&quot;Stack growing downwards.\n&quot;);
    else
        printf(&quot;Stack growing upwards.\n&quot;);
}

void a()
{
    int x;
    b(&amp;x);
}

int main()
{
    a();
    return 0;
}&lt;/pre&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://puddleofriddles.blogspot.com/feeds/4085525408778125917/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3302780462223800314/4085525408778125917' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3302780462223800314/posts/default/4085525408778125917'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3302780462223800314/posts/default/4085525408778125917'/><link rel='alternate' type='text/html' href='http://puddleofriddles.blogspot.com/2012/04/determine-direction-of-stack-growth.html' title='Determine direction of stack growth.'/><author><name>Suchit Maindola</name><uri>http://www.blogger.com/profile/18147182105271900742</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuYKcXai-jog9Be2PRlIuFJq9KFTWyqQnQFg79vXWquwehfIkmBArsWnO6g6qQsyF-R1_GN1cZRJRe7OhgYiuuVuSzuM2IR5sK5mANoHjN_QPO2Rr64IEZl2aQmBf8JA/s220/IMG_0380.JPG'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3302780462223800314.post-2662776377807974691</id><published>2012-03-23T01:51:00.001-07:00</published><updated>2012-03-27T16:35:42.898-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Dynamic Programming"/><title type='text'>Subset sum problem.</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;b&gt;Problem Statement: Given a set of numbers A[1...n], find a subset which sums up to a given number S.&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Solution #1&lt;/b&gt;: Below is the code using Brute-force approach. The complexity is O(2^n).&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;prettyprint lang-cpp&quot;&gt;int subsetSum(int* A, int len, int sum)
{
    if(sum == 0)
        return 1;
    
    if((len &amp;lt; 0) || (sum &amp;lt; 0))
        return 0;

    return subsetSum(A+1, len-1, sum) || subsetSum(A+1, len-1, sum-A[0]);
}&lt;/pre&gt;&lt;br /&gt;
&lt;b&gt;Solution #2&lt;/b&gt;: A faster way to solve this problem is by using dynamic programming. Below is the equation and the code,&lt;br /&gt;
&lt;br /&gt;
RES[i][j] : is &#39;1&#39; if there is a subset in set A[1...i] which sums up to &#39;j&#39;, &#39;0&#39; otherwise.&lt;br /&gt;
&lt;br /&gt;
RES[i][j] = RES[i-1][j] | RES[i-1][j-A[i]]&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;prettyprint lang-cpp&quot;&gt;int RES[100][100];

int subsetSumDyn(int* A, int len, int sum)
{
    int i,j;

    for(j=0; j&amp;lt;=sum; j++)
        RES[0][j] = 0;
    
    for(i=0; i&amp;lt;=len; i++)
        RES[i][0] = 1;

    for(i=1; i&amp;lt;=len; i++)
    {
        for(j=1; j&amp;lt;=sum; j++)
        {
            int index = j - A[i-1];
            if(index &amp;gt;=0)
                RES[i][j] = RES[i-1][j] | RES[i-1][index];
            else
                RES[i][j] = RES[i-1][j];
        }
    }

    return RES[len][sum];
}

int main()
{
    int A[] = {13, 21, 11, 14, 4, 5};
    int len = sizeof(A)/sizeof(int);

    printf(&quot;Subset sub: %d\n&quot;, subsetSumDyn(A, len, 32));
    return 0;
}
&lt;/pre&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://puddleofriddles.blogspot.com/feeds/2662776377807974691/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3302780462223800314/2662776377807974691' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3302780462223800314/posts/default/2662776377807974691'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3302780462223800314/posts/default/2662776377807974691'/><link rel='alternate' type='text/html' href='http://puddleofriddles.blogspot.com/2012/03/subset-sum-problem.html' title='Subset sum problem.'/><author><name>Suchit Maindola</name><uri>http://www.blogger.com/profile/18147182105271900742</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuYKcXai-jog9Be2PRlIuFJq9KFTWyqQnQFg79vXWquwehfIkmBArsWnO6g6qQsyF-R1_GN1cZRJRe7OhgYiuuVuSzuM2IR5sK5mANoHjN_QPO2Rr64IEZl2aQmBf8JA/s220/IMG_0380.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3302780462223800314.post-2243818801832167730</id><published>2012-03-22T22:16:00.002-07:00</published><updated>2012-03-27T17:15:19.680-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Recursion"/><title type='text'>Maze Algorithm.</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;b&gt;Problem Statement: A mouse is at one corner of a maze and he has to reach the corner diagonally opposite to him. Write an algorithm to find the path through the maze. The maze is represented by a matrix of &#39;0&#39;s and &#39;1&#39;s where &#39;1&#39; denotes a path and &#39;0&#39; denotes a wall.&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
This problem can be solved using backtracking. Below is the code,&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;prettyprint lang-cpp&quot;&gt;#define N 4

void printMaze(int maze[N][N])
{
    int i, j;

    for(i=0; i &lt; N; i++)
    {
        for(j=0; j &lt; N; j++)
            printf(&quot;%d &quot;, maze[i][j]);
        printf(&quot;\n&quot;);
    }
}

void move(int maze[N][N], int sol[N][N], int x, int y)
{
    if((x == N-1) &amp;&amp; (y == N-1))
    {
        sol[x][y] = 1;
        printMaze(sol);
        return;
    }

    if(x != N-1)
    {
        if(maze[x+1][y])
        {
            sol[x][y] = 1;
            move(maze, sol, x+1, y);
            sol[x][y] = 0;
        }
    }

    if(y != N-1)
    {
        if(maze[x][y+1])
        {
            sol[x][y] = 1;
            move(maze, sol, x, y+1);
            sol[x][y] = 0;
        }
    }
}

int main()
{
    int maze[N][N] = 
    {
        {1, 0, 0, 0},
        {1, 1, 0, 1},
        {0, 1, 0, 0},
        {0, 1, 1, 1}
    };

    int sol[N][N] = {0,};

    move(maze, sol, 0, 0);
    return 0;
}
&lt;/pre&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://puddleofriddles.blogspot.com/feeds/2243818801832167730/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3302780462223800314/2243818801832167730' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3302780462223800314/posts/default/2243818801832167730'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3302780462223800314/posts/default/2243818801832167730'/><link rel='alternate' type='text/html' href='http://puddleofriddles.blogspot.com/2012/03/maze-algorithm.html' title='Maze Algorithm.'/><author><name>Suchit Maindola</name><uri>http://www.blogger.com/profile/18147182105271900742</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuYKcXai-jog9Be2PRlIuFJq9KFTWyqQnQFg79vXWquwehfIkmBArsWnO6g6qQsyF-R1_GN1cZRJRe7OhgYiuuVuSzuM2IR5sK5mANoHjN_QPO2Rr64IEZl2aQmBf8JA/s220/IMG_0380.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3302780462223800314.post-1528041757502258428</id><published>2012-03-21T16:38:00.007-07:00</published><updated>2012-03-27T17:10:19.540-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Dynamic Programming"/><title type='text'>Edit Distance.</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;b&gt;Problem Statement: The edit distance between two words is the minimum number of letter insertions, letter deletions and letter substitutions required to transform one word into another. For e.g. edit distance between &quot;food&quot;and &quot;money&quot; is 4. Below is the equation and the code for the problem,&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
min[i, j] = MIN(min[i-1, j] + 1, min[i, j-1] + 1, min[i-1, j-1] + (A[i] != B[j]))&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;prettyprint lang-cpp&quot;&gt;#define MIN(a, b) ((a)&amp;lt;(b))?(a):(b)

int min[100][100];

int editDistance(char* A, char* B)
{
    int n = strlen(A);
    int m = strlen(B);

    int i, j;

    for(i=0; i &amp;lt;= n; i++)
        min[i][0] = i;
    
    for(j=0; j &amp;lt;= m; j++)
        min[0][j] = j;

    for(i=1; i &amp;lt;= n; i++)
    {
        for(j=1; j &amp;lt;= m; j++)
        {
            min[i][j] = MIN(min[i-1][j] + 1, min[i][j-1] + 1);
            min[i][j] = MIN(min[i][j], min[i-1][j-1] + (A[i-1]!=B[j-1]));
        }
    }

    return min[n][m];
}

int main()
{
    char A[50] = {0, };
    char B[50] = {0, };

    sprintf(A, &quot;%s&quot;, &quot;food&quot;);
    sprintf(B, &quot;%s&quot;, &quot;money&quot;);

    printf(&quot;EDIT DISTANCE: %d\n&quot;, editDistance(A, B));
    return 0;
}
&lt;/pre&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://puddleofriddles.blogspot.com/feeds/1528041757502258428/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3302780462223800314/1528041757502258428' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3302780462223800314/posts/default/1528041757502258428'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3302780462223800314/posts/default/1528041757502258428'/><link rel='alternate' type='text/html' href='http://puddleofriddles.blogspot.com/2012/03/edit-distance.html' title='Edit Distance.'/><author><name>Suchit Maindola</name><uri>http://www.blogger.com/profile/18147182105271900742</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuYKcXai-jog9Be2PRlIuFJq9KFTWyqQnQFg79vXWquwehfIkmBArsWnO6g6qQsyF-R1_GN1cZRJRe7OhgYiuuVuSzuM2IR5sK5mANoHjN_QPO2Rr64IEZl2aQmBf8JA/s220/IMG_0380.JPG'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3302780462223800314.post-6366527120173050197</id><published>2012-03-20T22:58:00.002-07:00</published><updated>2012-08-03T11:55:23.838-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Recursion"/><category scheme="http://www.blogger.com/atom/ns#" term="Strings"/><title type='text'>Given two strings, print all interleaving strings.</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Solution #1: Note that interleaving of two strings does not change the actual order of the characters within the two strings. For e.g.&lt;br /&gt;
&lt;br /&gt;
str1 = &quot;AB&quot;&lt;br /&gt;
str2 = &quot;MN&quot;&lt;br /&gt;
&lt;br /&gt;
So, in the output string, &#39;A&#39; will always come before &#39;B&#39; and &#39;M&#39; will always come before &#39;N&#39; .&lt;br /&gt;
&lt;br /&gt;
Interleaving strings are,&lt;br /&gt;
&lt;br /&gt;
ABMN&lt;br /&gt;
AMBN&lt;br /&gt;
AMNB&lt;br /&gt;
MABN&lt;br /&gt;
MANB&lt;br /&gt;
MNAB&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;prettyprint lang-cpp&quot;&gt;void interleave(char* str1, char* str2, char* str, int len)
{
    int i=0;

    //if both input strings reach null, print final string and return
    if(str1[0] == &#39;\0&#39; &amp;amp;&amp;amp; str2[0] == &#39;\0&#39;)
    {
        printf(&quot;%s\n&quot;, str-len);
        return;
    }

    if(str1[0] != &#39;\0&#39;)
    {
        str[0] = str1[0];
        interleave(str1+1, str2, str+1, len);
    }
    if(str2[0] != &#39;\0&#39;)
    {
        str[0] = str2[0];
        interleave(str1, str2+1, str+1, len);
    }
}

int main()
{
    char* str1 = &quot;AB&quot;;
    char* str2 = &quot;MNO&quot;;

    //get length of input strings
    int len1 = strlen(str1);
    int len2 = strlen(str2);
    int len = len1+len2;

    //allocate memory for output string
    char* str = (char*)malloc(len+1);
    memset(str, 0, len+1);

    interleave(str1, str2, str, len);
    return 0;
}&lt;/pre&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://puddleofriddles.blogspot.com/feeds/6366527120173050197/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3302780462223800314/6366527120173050197' title='7 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3302780462223800314/posts/default/6366527120173050197'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3302780462223800314/posts/default/6366527120173050197'/><link rel='alternate' type='text/html' href='http://puddleofriddles.blogspot.com/2012/03/given-two-strings-print-all.html' title='Given two strings, print all interleaving strings.'/><author><name>Suchit Maindola</name><uri>http://www.blogger.com/profile/18147182105271900742</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuYKcXai-jog9Be2PRlIuFJq9KFTWyqQnQFg79vXWquwehfIkmBArsWnO6g6qQsyF-R1_GN1cZRJRe7OhgYiuuVuSzuM2IR5sK5mANoHjN_QPO2Rr64IEZl2aQmBf8JA/s220/IMG_0380.JPG'/></author><thr:total>7</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3302780462223800314.post-5425652072154712807</id><published>2012-03-18T23:52:00.003-07:00</published><updated>2012-03-27T17:10:19.541-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Dynamic Programming"/><title type='text'>Longest Increasing Subsequence problem.</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;b&gt;Problem Statement: Given an array of integers, determine the longest increasing subsequence (not necesarily contiguous).&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Solution #1: Below is the equation and the code for the problem,&lt;br /&gt;
&lt;br /&gt;
Equations:&lt;br /&gt;
LIS[i] = MAX( LIS[j] ) + 1; where 0 &amp;lt;= j &amp;lt; i and A[i] &amp;gt; A[j]&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;prettyprint lang-cpp&quot;&gt;#define MAX(a, b) ((a)&gt;(b))?(a):(b)

int LIS[1000];

int lis(int* A, int len)
{
    int i, j;
    int max;

    LIS[0] = 1;

    for(i=1; i &lt; len; i++)
    {
        max = 0;
        for(j=0; j &lt; i; j++)
        {
            if(A[i] &gt; A[j])
                max = MAX(max, LIS[j]);
        }

        printf(&quot;max: %d\n&quot;, max);

        LIS[i] = max + 1;
    }

    return LIS[len-1];
}

int main()
{
    int A[] = {4, 3, 9, 4 ,1 ,6};

    printf(&quot;LIS: %d\n&quot;, lis(A, 6));
    return 0;
}
&lt;/pre&gt;&lt;br /&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://puddleofriddles.blogspot.com/feeds/5425652072154712807/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3302780462223800314/5425652072154712807' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3302780462223800314/posts/default/5425652072154712807'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3302780462223800314/posts/default/5425652072154712807'/><link rel='alternate' type='text/html' href='http://puddleofriddles.blogspot.com/2012/03/longest-increasing-subsequence.html' title='Longest Increasing Subsequence problem.'/><author><name>Suchit Maindola</name><uri>http://www.blogger.com/profile/18147182105271900742</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuYKcXai-jog9Be2PRlIuFJq9KFTWyqQnQFg79vXWquwehfIkmBArsWnO6g6qQsyF-R1_GN1cZRJRe7OhgYiuuVuSzuM2IR5sK5mANoHjN_QPO2Rr64IEZl2aQmBf8JA/s220/IMG_0380.JPG'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3302780462223800314.post-7270382521749463994</id><published>2012-03-18T01:47:00.001-07:00</published><updated>2012-03-27T17:10:19.541-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Dynamic Programming"/><title type='text'>Longest common subsequence problem.</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;b&gt;Problem Statement:&amp;nbsp;Given two character arrays, arr1[m] and arr2[n], determine a longest common subsequence among these two arrays.&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Solution #1:&amp;nbsp;A Brute force approach will take exponential time to solve this problem. We will use dynamic programming to&amp;nbsp;solve it in O(mn) time and space. Below is the equation and the code for the problem,&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
LCS[i, j] = LCS[i-1][j-1] + 1 ; arr1[i] == arr2[j]&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;= MAX(LCS[i-1][j], LCS[i][j-1]) ; otherwise&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;prettyprint lang-cpp&quot;&gt;#define MAX(a,b) ((a)&amp;gt;(b))?(a):(b)

int LCS[100][100];

int lcs(char* arr1, int len1, char* arr2, int len2)
{
    int i = 0;
    int j = 0;

    //LCS is 0 when arr2 is NULL
    for(i=0; i &amp;lt;= len1; i++)
        LCS[i][0] = 0;

    //LCS is 0 when arr1 is NULL
    for(j=0; j &amp;lt;= len2; j++)
        LCS[0][j] = 0;

    for(i=1; i &amp;lt;= len1; i++)
    {
        for(j=1; j &amp;lt;= len2; j++)
        {
            if(arr1[i] == arr2[j])
                LCS[i][j] = LCS[i-1][j-1] + 1;
            else
                LCS[i][j] = MAX(LCS[i-1][j], LCS[i][j-1]);
        }
    }

    int temp = LCS[len1][len2];

    return temp;
}

int main()
{
    char* arr1 = &quot;adbcad&quot;;
    char* arr2 = &quot;dbacd&quot;;

    printf(&quot;LCS: %d\n&quot;, lcs(arr1, strlen(arr1), arr2, strlen(arr2)));
    return 0;
}
&lt;/pre&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://puddleofriddles.blogspot.com/feeds/7270382521749463994/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3302780462223800314/7270382521749463994' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3302780462223800314/posts/default/7270382521749463994'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3302780462223800314/posts/default/7270382521749463994'/><link rel='alternate' type='text/html' href='http://puddleofriddles.blogspot.com/2012/03/longest-common-subsequence-problem.html' title='Longest common subsequence problem.'/><author><name>Suchit Maindola</name><uri>http://www.blogger.com/profile/18147182105271900742</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuYKcXai-jog9Be2PRlIuFJq9KFTWyqQnQFg79vXWquwehfIkmBArsWnO6g6qQsyF-R1_GN1cZRJRe7OhgYiuuVuSzuM2IR5sK5mANoHjN_QPO2Rr64IEZl2aQmBf8JA/s220/IMG_0380.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3302780462223800314.post-7833564885483585255</id><published>2012-03-16T00:39:00.001-07:00</published><updated>2012-03-29T01:19:32.063-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Bits n Bytes"/><title type='text'>Count bits in an integer.</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Solution #1: The most straightforward approach is to check each bit one by one. Here is the code,&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;prettyprint lang-cpp&quot;&gt;int count1(int n)
{
    int count=0;

    while(n)
    {
        if(n&amp;amp;1)
            count++;

        n = n&amp;gt;&amp;gt;1;
    }

    return count;
}
&lt;/pre&gt;
&lt;br /&gt;
Solution #2: The below code is an efficient version which improves the average time,&lt;br /&gt;
&lt;pre class=&quot;prettyprint lang-cpp&quot;&gt;int count2(int n)
{
    int count=0;

    while(n)
    {
        count++;
        n = n&amp;amp;(n-1);
    }

    return count;
}&lt;/pre&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://puddleofriddles.blogspot.com/feeds/7833564885483585255/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3302780462223800314/7833564885483585255' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3302780462223800314/posts/default/7833564885483585255'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3302780462223800314/posts/default/7833564885483585255'/><link rel='alternate' type='text/html' href='http://puddleofriddles.blogspot.com/2012/03/count-bits-in-integer.html' title='Count bits in an integer.'/><author><name>Suchit Maindola</name><uri>http://www.blogger.com/profile/18147182105271900742</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuYKcXai-jog9Be2PRlIuFJq9KFTWyqQnQFg79vXWquwehfIkmBArsWnO6g6qQsyF-R1_GN1cZRJRe7OhgYiuuVuSzuM2IR5sK5mANoHjN_QPO2Rr64IEZl2aQmBf8JA/s220/IMG_0380.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3302780462223800314.post-1687435255746396071</id><published>2012-03-15T00:32:00.002-07:00</published><updated>2012-03-27T17:10:19.544-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Dynamic Programming"/><title type='text'>Coin denomination problem</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;b&gt;Problem Statement:&amp;nbsp;You are given n types of coin denominations of values d1 &amp;lt; d2 &amp;lt; ... &amp;lt; dn (all integers). Give an algorithm which makes change for an amount of money C with as few coins as possible.&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Solution #1: Let min[i] represents the minimum number of coins required to make the change &#39;i&#39;. Now, minimum number of coins required to make change &#39;C&#39; = min[C-dj] + 1, where d1 &amp;lt;= dj &amp;lt;= dn. Below is the code with comments,&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;prettyprint lang-cpp&quot;&gt;#define INFINITY 99999

int min[1000] = {0, };


int coins(int* arr, int len, int c)
{
    int i,j;

    //initialize all elements with INFINITY i.e.
    //infinite coins needed to make ith change
    for(i=0; i&amp;lt;=c; i++)
        min[i] = INFINITY;

    //0 coins are needed to get 0 change!
    min[0] = 0;

    //1 coin is needed to get the change equal to
    //any of the available coins
    for(i=0; i &amp;lt; len; i++)
        min[arr[i]] = 1;

    for(i=0; i&amp;lt;=c; i++)
    {
        for(j=0; j &amp;lt; len; j++)
        {
            int index = i-arr[j];
            if(index &amp;lt; 0)
                continue;

            if(min[index]+1 &amp;lt; min[i])
                min[i] = min[index]+1;
        }
    }

    return min[c];
}

int main()
{
    int arr[] = {4, 5, 7};
    printf(&quot;COINS: %d\n&quot;, coins(arr, 3, 166));

    return 0;
}&lt;/pre&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://puddleofriddles.blogspot.com/feeds/1687435255746396071/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3302780462223800314/1687435255746396071' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3302780462223800314/posts/default/1687435255746396071'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3302780462223800314/posts/default/1687435255746396071'/><link rel='alternate' type='text/html' href='http://puddleofriddles.blogspot.com/2012/03/you-are-given-n-types-of-coin.html' title='Coin denomination problem'/><author><name>Suchit Maindola</name><uri>http://www.blogger.com/profile/18147182105271900742</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuYKcXai-jog9Be2PRlIuFJq9KFTWyqQnQFg79vXWquwehfIkmBArsWnO6g6qQsyF-R1_GN1cZRJRe7OhgYiuuVuSzuM2IR5sK5mANoHjN_QPO2Rr64IEZl2aQmBf8JA/s220/IMG_0380.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3302780462223800314.post-3336387225999253917</id><published>2012-02-28T16:39:00.002-08:00</published><updated>2012-03-27T17:10:19.546-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Strings"/><category scheme="http://www.blogger.com/atom/ns#" term="System"/><title type='text'>Implement atoi().</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;Solution #1: Here is the code,&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;prettyprint lang-cpp&quot;&gt;int my_atoi(char* str)
{
    int len = strlen(str);
    int i;
    int isNegative = 0;
    int num = 0;

    if(str[0] == &#39;-&#39;)
    {
        isNegative = 1;
        str++;
    }

    for(i=0; i &lt; len; i++)
    {
        int curr = str[i] - &#39;0&#39;;

        //if it is a digit then put it at the units place
        if((curr &gt;=0) &amp;&amp; (curr &lt;= 9))
        {
            num = num*10 + curr;
        }
        else //if not a digit then break
            break;
    }

    if(isNegative)
        num = num*-1;

    return num;
}

int main()
{
    printf(&quot;%d\n&quot;, my_atoi(&quot;-123&quot;));
    return 0;
}&lt;/pre&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://puddleofriddles.blogspot.com/feeds/3336387225999253917/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3302780462223800314/3336387225999253917' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3302780462223800314/posts/default/3336387225999253917'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3302780462223800314/posts/default/3336387225999253917'/><link rel='alternate' type='text/html' href='http://puddleofriddles.blogspot.com/2012/02/implement-atoi.html' title='Implement atoi().'/><author><name>Suchit Maindola</name><uri>http://www.blogger.com/profile/18147182105271900742</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuYKcXai-jog9Be2PRlIuFJq9KFTWyqQnQFg79vXWquwehfIkmBArsWnO6g6qQsyF-R1_GN1cZRJRe7OhgYiuuVuSzuM2IR5sK5mANoHjN_QPO2Rr64IEZl2aQmBf8JA/s220/IMG_0380.JPG'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3302780462223800314.post-4274284855029836227</id><published>2012-02-28T16:02:00.002-08:00</published><updated>2012-03-27T17:10:19.549-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Searching and Sorting"/><title type='text'>Find second maximum value in an array efficiently.</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;Solution #1: Sorting is one obvious way, but, there is another efficient way. Here is the code,&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;prettyprint lang-cpp&quot;&gt;int secondMax(int* arr, int len)
{
    int max = 0;
    int sMax = 0;
    int i = 0;

    for(i=0; i &lt; len; i++)
    {
        int curr = arr[i];

        if(curr &gt; max)
        {
            sMax = max;
            max = curr;
        }
        else if(curr &gt; sMax)
            sMax = curr;
    }

    return sMax;
}

int main()
{
    int arr[5] = {3, 2, 5, 1, 4};

    printf(&quot;Second Max: %d\n&quot;, secondMax(arr, 5));

    return 0;
}
&lt;/pre&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://puddleofriddles.blogspot.com/feeds/4274284855029836227/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3302780462223800314/4274284855029836227' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3302780462223800314/posts/default/4274284855029836227'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3302780462223800314/posts/default/4274284855029836227'/><link rel='alternate' type='text/html' href='http://puddleofriddles.blogspot.com/2012/02/find-second-maximum-value-in-array.html' title='Find second maximum value in an array efficiently.'/><author><name>Suchit Maindola</name><uri>http://www.blogger.com/profile/18147182105271900742</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuYKcXai-jog9Be2PRlIuFJq9KFTWyqQnQFg79vXWquwehfIkmBArsWnO6g6qQsyF-R1_GN1cZRJRe7OhgYiuuVuSzuM2IR5sK5mANoHjN_QPO2Rr64IEZl2aQmBf8JA/s220/IMG_0380.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3302780462223800314.post-5214711244233732256</id><published>2012-01-30T17:01:00.001-08:00</published><updated>2012-03-27T17:10:19.549-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Bits n Bytes"/><title type='text'>Implement mask_and_set_bits()</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;Solution #1: Given an integer &#39;value&#39;, a position &#39;pos&#39;, an integer &#39;n&#39; and another integer &#39;bits&#39;. Replace &#39;n&#39; bits in &#39;value&#39; starting at position &#39;pos&#39; with &#39;bits&#39;. Here is the code written for 32-bit architectures,&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;prettyprint lang-cpp&quot;&gt;void mask_and_set_bits(unsigned int* val, unsigned int pos, unsigned int n, unsigned int bits)
{
    *val = (*val)&amp;((0xFFFFFFFF &lt;&lt; (pos+n))| ~(0xFFFFFFFF &lt;&lt; pos));
    *val = *val|(bits &lt;&lt; pos)&amp;~((0xFFFFFFFF &lt;&lt; (pos+n))|~(0xFFFFFFFF &lt;&lt; pos));
}
&lt;/pre&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://puddleofriddles.blogspot.com/feeds/5214711244233732256/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3302780462223800314/5214711244233732256' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3302780462223800314/posts/default/5214711244233732256'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3302780462223800314/posts/default/5214711244233732256'/><link rel='alternate' type='text/html' href='http://puddleofriddles.blogspot.com/2012/01/implement-maskandsetbits.html' title='Implement mask_and_set_bits()'/><author><name>Suchit Maindola</name><uri>http://www.blogger.com/profile/18147182105271900742</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuYKcXai-jog9Be2PRlIuFJq9KFTWyqQnQFg79vXWquwehfIkmBArsWnO6g6qQsyF-R1_GN1cZRJRe7OhgYiuuVuSzuM2IR5sK5mANoHjN_QPO2Rr64IEZl2aQmBf8JA/s220/IMG_0380.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3302780462223800314.post-7149143677582764720</id><published>2012-01-26T20:51:00.001-08:00</published><updated>2012-03-27T17:13:33.352-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Searching and Sorting"/><title type='text'>Find 2 smallest numbers in an array efficiently.</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;Solution #1: Here is the code,&lt;br /&gt;
&lt;pre class=&quot;prettyprint lang-cpp&quot;&gt;void findMinTwo(int* arr, int len)
{
    int i;
    int min = INT_MAX;
           int sMin = INT_MAX;

    for(i=0; i &lt; len; i++)
    {
        //if arr[i] is less them min, set min = arr[i]
        //and set sMin = min
        if(arr[i] &lt; min)
        {
            sMin = min;
            min = arr[i];
        }
        //else if arr[i] is less then sMin, update sMin
        else if(arr[i] &lt; sMin)
        {
            sMin = arr[i];
        }
    }

    printf(&quot;%d %d\n&quot;, min, sMin);
}&lt;/pre&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://puddleofriddles.blogspot.com/feeds/7149143677582764720/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3302780462223800314/7149143677582764720' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3302780462223800314/posts/default/7149143677582764720'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3302780462223800314/posts/default/7149143677582764720'/><link rel='alternate' type='text/html' href='http://puddleofriddles.blogspot.com/2012/01/find-2-smallest-numbers-in-array.html' title='Find 2 smallest numbers in an array efficiently.'/><author><name>Suchit Maindola</name><uri>http://www.blogger.com/profile/18147182105271900742</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuYKcXai-jog9Be2PRlIuFJq9KFTWyqQnQFg79vXWquwehfIkmBArsWnO6g6qQsyF-R1_GN1cZRJRe7OhgYiuuVuSzuM2IR5sK5mANoHjN_QPO2Rr64IEZl2aQmBf8JA/s220/IMG_0380.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3302780462223800314.post-6291869958409192693</id><published>2012-01-26T13:05:00.001-08:00</published><updated>2012-03-27T17:13:33.352-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Searching and Sorting"/><title type='text'>Given an array of 0s and 1s, segregate 0s on left and 1s on right side.</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;Solution #1: Here is the code,&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;prettyprint lang-cpp&quot;&gt;void segregate01(int* arr, int len)
{
    int start = 0;
    int end = len-1;

    while(start &lt; end)
    {
        while((arr[start] == 0) &amp;&amp; (start != end))
            start++;

        while((arr[end] == 1) &amp;&amp; (end != start))
            end--;

        if(start &lt; end)
        {
            swap(&amp;(arr[start]), &amp;(arr[end]));
        }
    }
}&lt;/pre&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://puddleofriddles.blogspot.com/feeds/6291869958409192693/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3302780462223800314/6291869958409192693' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3302780462223800314/posts/default/6291869958409192693'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3302780462223800314/posts/default/6291869958409192693'/><link rel='alternate' type='text/html' href='http://puddleofriddles.blogspot.com/2012/01/given-array-of-0s-and-1s-segregate-0s.html' title='Given an array of 0s and 1s, segregate 0s on left and 1s on right side.'/><author><name>Suchit Maindola</name><uri>http://www.blogger.com/profile/18147182105271900742</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuYKcXai-jog9Be2PRlIuFJq9KFTWyqQnQFg79vXWquwehfIkmBArsWnO6g6qQsyF-R1_GN1cZRJRe7OhgYiuuVuSzuM2IR5sK5mANoHjN_QPO2Rr64IEZl2aQmBf8JA/s220/IMG_0380.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3302780462223800314.post-8808734551408796400</id><published>2012-01-25T22:43:00.001-08:00</published><updated>2012-03-27T17:13:33.352-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Searching and Sorting"/><title type='text'>Given a sorted array with duplicate elements, find the number of occurrences of x.</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;Solution #1: We will use the modified binary search to solve this in O(log n) time. Here is the code,&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;prettyprint lang-cpp&quot;&gt;int count(int* arr, int x, int start, int end)
{
    if(start &gt; end)
        return;

    int mid = (start+end)/2;
    int cnt = 0;

    if(arr[mid] == x)
        cnt = 1 + count(arr, x, start, mid-1) + count(arr, x, mid+1, end);
    else if(arr[mid] &gt; x)
        cnt = count(arr, x, start, mid-1);
    else
        cnt = count(arr, x, mid+1, end);

    return cnt;
}&lt;/pre&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://puddleofriddles.blogspot.com/feeds/8808734551408796400/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3302780462223800314/8808734551408796400' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3302780462223800314/posts/default/8808734551408796400'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3302780462223800314/posts/default/8808734551408796400'/><link rel='alternate' type='text/html' href='http://puddleofriddles.blogspot.com/2012/01/given-sorted-array-with-duplicate.html' title='Given a sorted array with duplicate elements, find the number of occurrences of x.'/><author><name>Suchit Maindola</name><uri>http://www.blogger.com/profile/18147182105271900742</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuYKcXai-jog9Be2PRlIuFJq9KFTWyqQnQFg79vXWquwehfIkmBArsWnO6g6qQsyF-R1_GN1cZRJRe7OhgYiuuVuSzuM2IR5sK5mANoHjN_QPO2Rr64IEZl2aQmBf8JA/s220/IMG_0380.JPG'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3302780462223800314.post-2641270469816154522</id><published>2012-01-25T13:14:00.001-08:00</published><updated>2012-03-27T17:13:33.352-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Searching and Sorting"/><title type='text'>Given an array with duplicates, find the minimum distance between given x and y.</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;Solution #1: Below is the code with explanation in comments,&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;prettyprint lang-cpp&quot;&gt;int minDistance(int* arr, int len, int x, int y)
{
    int prevIndex = -1;
    int min = INT_MAX;
    int i=0;
    int first;
    int second;
    int distance;


    for(i=0; i &lt; len; i++)
    {
        if((arr[i] == x) || (arr[i] == y))
        {
            //first time encounter with x or y
            //&#39;first&#39; stores either x or y, depending on
            //which is encountered first
            if(prevIndex == -1)
            {
                first = (arr[i]==x)?x:y;
                second = (arr[i]==x)?y:x;
                prevIndex = i;
            }
            else
            {
                //if arr[i] is same as &#39;first&#39;, reset prevIndex
                if(arr[i] == first)
                    prevIndex = i;
                //if arr[i] is different from &#39;first&#39;, calculate distance
                else
                {
                    distance = i - prevIndex;
                    prevIndex = i;
                    min = MIN(min, distance);

                    //swap first and second
                    int temp = first;
                    first = second;
                    second = temp;
                }
            }
        }
    }
    return min;
}

int main()
{
    int arr[] ={2, 3, 4, 6, 6, 7, 5, 1, 4};

    int len = sizeof(arr)/sizeof(int);

    printf(&quot;MIN DISTANCE: %d\n&quot;, minDistance(arr, len, 4, 5));
    return 0;
}
&lt;/pre&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://puddleofriddles.blogspot.com/feeds/2641270469816154522/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3302780462223800314/2641270469816154522' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3302780462223800314/posts/default/2641270469816154522'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3302780462223800314/posts/default/2641270469816154522'/><link rel='alternate' type='text/html' href='http://puddleofriddles.blogspot.com/2012/01/given-array-with-duplicates-find.html' title='Given an array with duplicates, find the minimum distance between given x and y.'/><author><name>Suchit Maindola</name><uri>http://www.blogger.com/profile/18147182105271900742</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuYKcXai-jog9Be2PRlIuFJq9KFTWyqQnQFg79vXWquwehfIkmBArsWnO6g6qQsyF-R1_GN1cZRJRe7OhgYiuuVuSzuM2IR5sK5mANoHjN_QPO2Rr64IEZl2aQmBf8JA/s220/IMG_0380.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3302780462223800314.post-3338121757590051505</id><published>2012-01-24T22:55:00.001-08:00</published><updated>2012-03-27T17:13:33.353-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Bits n Bytes"/><title type='text'>Reverse bits of a byte.</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;Solution #1. E.g.&lt;br /&gt;
&lt;br /&gt;
00001001 ==&amp;gt; 10010000&lt;br /&gt;
&lt;br /&gt;
Here is the code,&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
#define GET(n, x) ((n&amp;amp;(1&amp;lt;&amp;lt;x))&amp;gt;&amp;gt;x)&lt;br /&gt;
#define SET(n, x) (n|(1&amp;lt;&amp;lt;x))&lt;br /&gt;
#define RESET(n, x) (n&amp;amp;(~(1&amp;lt;&amp;lt;x)))&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;prettyprint lang-cpp&quot;&gt;int reverseBits(unsigned char n)
{
    int temp = n;
    int start = 0;
    int end = 7;

    while(start &amp;lt; end)
    {
        int sbit = GET(n, start); //find start bit
        int ebit = GET(n, end); //find end bit
        
        //set end bit equal to start bit
        temp = sbit?SET(temp, end):RESET(temp, end);
        //set start bit equal to end bit
        temp = ebit?SET(temp, start):RESET(temp, start);

        start++;
        end--;
    }

    return temp;
}&lt;/pre&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://puddleofriddles.blogspot.com/feeds/3338121757590051505/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3302780462223800314/3338121757590051505' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3302780462223800314/posts/default/3338121757590051505'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3302780462223800314/posts/default/3338121757590051505'/><link rel='alternate' type='text/html' href='http://puddleofriddles.blogspot.com/2012/01/reverse-bits-of-byte.html' title='Reverse bits of a byte.'/><author><name>Suchit Maindola</name><uri>http://www.blogger.com/profile/18147182105271900742</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuYKcXai-jog9Be2PRlIuFJq9KFTWyqQnQFg79vXWquwehfIkmBArsWnO6g6qQsyF-R1_GN1cZRJRe7OhgYiuuVuSzuM2IR5sK5mANoHjN_QPO2Rr64IEZl2aQmBf8JA/s220/IMG_0380.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3302780462223800314.post-4728221756349614565</id><published>2012-01-22T18:07:00.002-08:00</published><updated>2012-04-03T23:55:35.620-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Linked List"/><category scheme="http://www.blogger.com/atom/ns#" term="Recursion"/><title type='text'>Reverse a linked list.</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Solution #1: Iterative approach to reverse a linked list,&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;prettyprint lang-cpp&quot;&gt;void itrReverse(struct node** head)
{
    if(*head == NULL)
        return;

    struct node* prev = NULL;
    struct node* curr = *head;
    struct node* next = curr-&amp;gt;next;

    while(1)
    {
        curr-&amp;gt;next = prev;

        if(next == NULL)
            break;

        prev = curr;
        curr = next;
        next = next-&amp;gt;next;
    }

    *head = curr;
}
&lt;/pre&gt;
&lt;br /&gt;
Solution #2: Recursive approach to reverse a linked list,&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;prettyprint lang-cpp&quot;&gt;void recReverse(struct node** head)
{
    struct node* first;
    struct node* rest;

    if(*head == NULL)
        return;

    first = *head;
    rest = first-&amp;gt;next;

    if(rest == NULL)
        return;

    recReverse(&amp;amp;rest);

    first-&amp;gt;next-&amp;gt;next = first;
    first-&amp;gt;next = NULL;

    *head = rest;
}
 &lt;/pre&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://puddleofriddles.blogspot.com/feeds/4728221756349614565/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3302780462223800314/4728221756349614565' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3302780462223800314/posts/default/4728221756349614565'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3302780462223800314/posts/default/4728221756349614565'/><link rel='alternate' type='text/html' href='http://puddleofriddles.blogspot.com/2012/01/reverse-linked-list.html' title='Reverse a linked list.'/><author><name>Suchit Maindola</name><uri>http://www.blogger.com/profile/18147182105271900742</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuYKcXai-jog9Be2PRlIuFJq9KFTWyqQnQFg79vXWquwehfIkmBArsWnO6g6qQsyF-R1_GN1cZRJRe7OhgYiuuVuSzuM2IR5sK5mANoHjN_QPO2Rr64IEZl2aQmBf8JA/s220/IMG_0380.JPG'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3302780462223800314.post-6501904345396042987</id><published>2012-01-22T13:03:00.001-08:00</published><updated>2012-03-27T17:13:33.353-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Recursion"/><category scheme="http://www.blogger.com/atom/ns#" term="Searching and Sorting"/><title type='text'>A sorted array is rotated unknown number of times. Find the minimum in the array.</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;Solution #1: A sorted array rotated unknown number of times will look something like this,&lt;br /&gt;
&lt;br /&gt;
6, 7, 8, 9, 1, 2, 3, 4, 5&lt;br /&gt;
&lt;br /&gt;
We will use modified binary search to find the minimum element in this array. Here is the code,&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;prettyprint lang-cpp&quot;&gt;int findMin(int* arr, int start, int end)
{
    if(start == end)
        return arr[start];

    int mid = (start+end)/2;

    if(arr[mid] &lt; arr[mid-1])
        return arr[mid];
    else if(arr[mid] &lt; arr[end])
        return findMin(arr, start, mid-1);
    else if(arr[mid] &gt; arr[end])
        return findMin(arr, mid+1, end);
}
&lt;/pre&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://puddleofriddles.blogspot.com/feeds/6501904345396042987/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3302780462223800314/6501904345396042987' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3302780462223800314/posts/default/6501904345396042987'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3302780462223800314/posts/default/6501904345396042987'/><link rel='alternate' type='text/html' href='http://puddleofriddles.blogspot.com/2012/01/sorted-array-is-rotated-unknown-number.html' title='A sorted array is rotated unknown number of times. Find the minimum in the array.'/><author><name>Suchit Maindola</name><uri>http://www.blogger.com/profile/18147182105271900742</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuYKcXai-jog9Be2PRlIuFJq9KFTWyqQnQFg79vXWquwehfIkmBArsWnO6g6qQsyF-R1_GN1cZRJRe7OhgYiuuVuSzuM2IR5sK5mANoHjN_QPO2Rr64IEZl2aQmBf8JA/s220/IMG_0380.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3302780462223800314.post-6653092361730445366</id><published>2012-01-21T16:46:00.001-08:00</published><updated>2012-03-27T17:13:33.353-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Recursion"/><category scheme="http://www.blogger.com/atom/ns#" term="Trees"/><title type='text'>Find diameter of a Binary Tree.</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;Solution #1: Diameter of a tree is the maximum distance between its any two nodes. For e.g. consider the below tree,&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 5&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/ &amp;nbsp; &amp;nbsp; &amp;nbsp;\&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 9&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;/ &amp;nbsp; &amp;nbsp;\ &amp;nbsp; &amp;nbsp; &amp;nbsp;/&lt;br /&gt;
&amp;nbsp; &amp;nbsp; 1 &amp;nbsp; &amp;nbsp;4 &amp;nbsp; &amp;nbsp;7&lt;br /&gt;
&lt;br /&gt;
Diameter: 5 (1-2-5-9-7 or 4-2-5-9-7)&lt;br /&gt;
&lt;br /&gt;
Here is the code,&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;prettyprint lang-cpp&quot;&gt;int height(struct node* root)
{
    if(root == NULL)
        return 0;

    int lh = height(root-&gt;left);
    int rh = height(root-&gt;right);

    return 1 + MAX(lh, rh);
}

int diameter(struct node* root)
{
    if(root == NULL)
        return 0;

    int lheight = height(root-&gt;left);
    int rheight = height(root-&gt;right);

    int ldia = diameter(root-&gt;left);
    int rdia = diameter(root-&gt;right);

    return MAX(1 + lheight + rheight, MAX(ldia, rdia));
}&lt;/pre&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://puddleofriddles.blogspot.com/feeds/6653092361730445366/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3302780462223800314/6653092361730445366' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3302780462223800314/posts/default/6653092361730445366'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3302780462223800314/posts/default/6653092361730445366'/><link rel='alternate' type='text/html' href='http://puddleofriddles.blogspot.com/2012/01/find-diameter-of-binary-tree.html' title='Find diameter of a Binary Tree.'/><author><name>Suchit Maindola</name><uri>http://www.blogger.com/profile/18147182105271900742</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuYKcXai-jog9Be2PRlIuFJq9KFTWyqQnQFg79vXWquwehfIkmBArsWnO6g6qQsyF-R1_GN1cZRJRe7OhgYiuuVuSzuM2IR5sK5mANoHjN_QPO2Rr64IEZl2aQmBf8JA/s220/IMG_0380.JPG'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3302780462223800314.post-9182690547732286459</id><published>2012-01-21T15:59:00.001-08:00</published><updated>2012-03-27T17:13:33.354-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Recursion"/><category scheme="http://www.blogger.com/atom/ns#" term="Stacks and Queues"/><category scheme="http://www.blogger.com/atom/ns#" term="Trees"/><title type='text'>Inorder traversal without recursion.</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;Solution #1: We can use a stack to implement Inorder traversal without recursion. Here is the code,&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;prettyprint lang-cpp&quot;&gt;void recInorder(struct node* root, stack&lt;struct node*&gt;&amp; s)
{
    struct node* current = root;

    while(1)
    {
        if(current != NULL)
        {
            s.push(current);
            current = current-&gt;left;
        }
        else
        {
            if(!s.empty())
            {
                struct node* temp = s.top();
                s.pop();

                printf(&quot;%d &quot;, temp-&gt;data);

                current = temp-&gt;right;
            }
            else
                break;
        }
    }

    printf(&quot;\n&quot;);
}
&lt;/pre&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://puddleofriddles.blogspot.com/feeds/9182690547732286459/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3302780462223800314/9182690547732286459' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3302780462223800314/posts/default/9182690547732286459'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3302780462223800314/posts/default/9182690547732286459'/><link rel='alternate' type='text/html' href='http://puddleofriddles.blogspot.com/2012/01/inorder-traversal-without-recursion.html' title='Inorder traversal without recursion.'/><author><name>Suchit Maindola</name><uri>http://www.blogger.com/profile/18147182105271900742</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuYKcXai-jog9Be2PRlIuFJq9KFTWyqQnQFg79vXWquwehfIkmBArsWnO6g6qQsyF-R1_GN1cZRJRe7OhgYiuuVuSzuM2IR5sK5mANoHjN_QPO2Rr64IEZl2aQmBf8JA/s220/IMG_0380.JPG'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3302780462223800314.post-8569843840722117208</id><published>2012-01-21T14:39:00.001-08:00</published><updated>2012-03-27T17:13:33.354-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Recursion"/><category scheme="http://www.blogger.com/atom/ns#" term="Trees"/><title type='text'>Construct a Binary Tree, given its Inorder and preorder.</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;Solution #1: Consider the following tree,&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;5&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/ &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;\&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;7&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; / &amp;nbsp; \ &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; /&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;1 &amp;nbsp; &amp;nbsp;4 &amp;nbsp; &amp;nbsp; &amp;nbsp;6&lt;br /&gt;
&lt;br /&gt;
Inorder: 1 2 4 5 6 7&lt;br /&gt;
Preorder: 5 2 1 4 7 6&lt;br /&gt;
&lt;br /&gt;
Algorithm:&lt;br /&gt;
1. Take first element from Preorder (in this case 5) and find it in Inorder.&lt;br /&gt;
2. Elements on the left side of &#39;5&#39; (1 2 4) form the left subtree of &#39;5&#39; and similarly elements on the right side (6 7) form right sub tree.&lt;br /&gt;
3. Recursively select each element from Preorder and create its left and right subtrees from Inorder.&lt;br /&gt;
&lt;br /&gt;
Here is the code,&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;prettyprint lang-cpp&quot;&gt;struct node* create(int* in, int* pre, int inStrt, int inEnd)
{
    if(inStrt &gt; inEnd)
        return NULL;

    int i;
    static int preIndex = 0;

    //create node from preOrder
    struct node* temp = (struct node*)malloc(sizeof(struct node));
    temp-&gt;data = pre[preIndex];

    //find node in inOrder
    for(i=inStrt; i&lt;=inEnd; i++)
        if(in[i] == pre[preIndex])
            break;

    preIndex++;
    
    //recursively call create for the left and right
    //half of inOrder created by preOrder
    temp-&gt;left = create(in, pre, inStrt, i-1);
    temp-&gt;right = create(in, pre, i+1, inEnd);

    return temp;
}&lt;/pre&gt;&lt;br /&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://puddleofriddles.blogspot.com/feeds/8569843840722117208/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3302780462223800314/8569843840722117208' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3302780462223800314/posts/default/8569843840722117208'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3302780462223800314/posts/default/8569843840722117208'/><link rel='alternate' type='text/html' href='http://puddleofriddles.blogspot.com/2012/01/construct-binary-tree-given-its-inorder.html' title='Construct a Binary Tree, given its Inorder and preorder.'/><author><name>Suchit Maindola</name><uri>http://www.blogger.com/profile/18147182105271900742</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuYKcXai-jog9Be2PRlIuFJq9KFTWyqQnQFg79vXWquwehfIkmBArsWnO6g6qQsyF-R1_GN1cZRJRe7OhgYiuuVuSzuM2IR5sK5mANoHjN_QPO2Rr64IEZl2aQmBf8JA/s220/IMG_0380.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3302780462223800314.post-1672075155656872388</id><published>2012-01-21T00:33:00.001-08:00</published><updated>2012-03-27T17:13:33.354-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Recursion"/><category scheme="http://www.blogger.com/atom/ns#" term="Trees"/><title type='text'>Find maximum width of a Binary Tree.</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;Solution #1: Maximum width of a tree is explained in the below e.g.&amp;nbsp;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/ &amp;nbsp; &amp;nbsp; &amp;nbsp; \&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 5 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 7&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/ &amp;nbsp; &amp;nbsp; \ &amp;nbsp; &amp;nbsp; &amp;nbsp; /&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;9 &amp;nbsp; &amp;nbsp; &amp;nbsp; 8 &amp;nbsp; &amp;nbsp;3&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;Width of level 1: 1&lt;/div&gt;&lt;div&gt;Width of level 2: 2&lt;/div&gt;&lt;div&gt;Width of level 3: 3&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;Therefore, maximum width: 3&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;We will first write a function to calculate the width at a given level and then will use this function to determine the maximum width of the tree. Here is the code,&lt;/div&gt;&lt;br /&gt;
&lt;pre class=&quot;prettyprint lang-cpp&quot;&gt;int width(struct node* root, int level)
{
    if(root == NULL)
        return 0;

    if(level == 1)
        return 1;

    return width(root-&gt;left, level-1) + width(root-&gt;right, level-1);
}

int maxWidth(struct node* root)
{
    if(root == NULL)
        return 0;

    int maxW = 0;
    int curW = 1;
    int level=2;

    while(curW)
    {
        curW = width(root, level++);

        maxW = MAX(curW, maxW);
    }

    return maxW;
}&lt;/pre&gt;&lt;br /&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://puddleofriddles.blogspot.com/feeds/1672075155656872388/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3302780462223800314/1672075155656872388' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3302780462223800314/posts/default/1672075155656872388'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3302780462223800314/posts/default/1672075155656872388'/><link rel='alternate' type='text/html' href='http://puddleofriddles.blogspot.com/2012/01/find-maximum-width-of-binary-tree.html' title='Find maximum width of a Binary Tree.'/><author><name>Suchit Maindola</name><uri>http://www.blogger.com/profile/18147182105271900742</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuYKcXai-jog9Be2PRlIuFJq9KFTWyqQnQFg79vXWquwehfIkmBArsWnO6g6qQsyF-R1_GN1cZRJRe7OhgYiuuVuSzuM2IR5sK5mANoHjN_QPO2Rr64IEZl2aQmBf8JA/s220/IMG_0380.JPG'/></author><thr:total>3</thr:total></entry></feed>