<?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-7748170484639853544</id><updated>2024-10-05T09:21:04.077+05:30</updated><category term="Algorithm"/><category term="Questions"/><category term="Data Structure"/><category term="Interview"/><category term="CLRS"/><category term="array"/><category term="tree"/><category term="Linked List"/><category term="binary tree"/><category term="problem"/><category term="Recursion"/><category term="random"/><category term="BIT"/><category term="Binary indexed tree"/><category term="Puzzle"/><category term="binary search"/><category term="java"/><category term="merge sort"/><category term="scjp"/><category term="thread"/><category term="3d"/><category term="3g"/><category term="Diameter"/><category term="Endianness"/><category term="Fenwick Tree"/><category term="Gold"/><category term="Kth Largest"/><category term="LW272"/><category term="MCSS"/><category term="Maximal Contiguous Subsequent Sum Problem"/><category term="Merge two linked list"/><category term="Minimum and maximum"/><category term="Nth fibonacci number"/><category term="Permutation of string"/><category term="PwerSet"/><category term="Robot"/><category term="Young tableau"/><category term="abc123"/><category term="add two list"/><category term="big endian"/><category term="binary search tree"/><category term="bsnl"/><category term="closest to zero"/><category term="cut"/><category term="dutch flag"/><category term="fibonacci"/><category term="find merge node"/><category term="find missing number"/><category term="insertion sort"/><category term="join()"/><category term="little endian"/><category term="malloc 3d array"/><category term="matrix"/><category term="minimumAlgorithm"/><category term="n-pairs"/><category term="parenthesis"/><category term="pick two numbers"/><category term="product"/><category term="rand5()"/><category term="rand7()"/><category term="remove duplicates"/><category term="repeating element"/><category term="reverse"/><category term="reverse dutch flag"/><category term="subtree"/><category term="sum of two numbers"/><category term="synchronized"/><category term="temple puzzle"/><category term="unlock 3g"/><category term="|a-b| + |b-c| + |c-a|"/><title type='text'>Data Structures And Algorithms</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://prpds.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7748170484639853544/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://prpds.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='http://www.blogger.com/feeds/7748170484639853544/posts/default?start-index=26&amp;max-results=25&amp;redirect=false'/><author><name>anantha</name><uri>http://www.blogger.com/profile/03120527085848815308</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>54</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-7748170484639853544.post-5144008849355999161</id><published>2012-03-02T17:56:00.001+05:30</published><updated>2012-03-02T17:57:56.659+05:30</updated><title type='text'></title><content type='html'>&lt;B&gt;Problem:&lt;/B&gt;&lt;/BR&gt;
Find the rectangle with maximum area under a histogram in linear time.

&lt;/BR&gt;&lt;B&gt;Solution:&lt;/B&gt;&lt;/BR&gt;

&lt;/BR&gt;&lt;B&gt;Complexity:&lt;/B&gt;&lt;/BR&gt;
O(n)

&lt;/BR&gt;&lt;B&gt;Code:&lt;/B&gt;&lt;/BR&gt;
&lt;script class=&quot;brush:c&quot; type=&quot;syntaxhighlighter&quot;&gt;
&lt;![CDATA[
void findLargestRectangleUnderHistogram() {
    
    
    int top=-1,i=0,sum = 0,n=11;
    
    int l2r[11],r2l[11],S[11];
    
    int H[11]={5,10,7,15,17,20,14,4,2,12,6};
    
    for(i=0;i&lt;n;i++)
    {
        l2r[i] = 1;
        sum = 0;
        while(top&gt;=0 &amp;&amp; H[S[top]] &gt;= H[i])
        {
            sum += l2r[S[top]];
            top--;
        }
        l2r[i] += sum;
        S[++top] = i;
    }
    
    top = -1;
 
    for(i=n-1;i&gt;=0;i--)
    {
        r2l[i] = 1;
        sum = 0;
        while(top&gt;=0 &amp;&amp; H[S[top]] &gt;= H[i])
        {
            sum += r2l[S[top]];
            top--;
        }
        r2l[i] += sum;
        S[++top] = i;
    }
    
    int max = 0;
    for(i=0;i&lt;n;i++)
    {
        int width = (l2r[i]+r2l[i]-1);
        int area = width * H[i];
        if(area &gt; max)
            max = area;
    }
    
    printf(&quot;Maximum Area: %d&quot;,max);
}
 
int main()
{
        findLargestRectangleUnderHistogram();
        
        return 0;
}
]]&gt;
&lt;/script&gt;&lt;br /&gt;
&lt;!-- Start CBClicks.com code --&gt;
&lt;script&gt;
cb_layout = 1;
cb_num = 6;
cb_width = 160;
cb_height = 600;
cb_border_color = &quot;#CCCCCC&quot;;
cb_back_color = &quot;#FFFFFF&quot;;
cb_title_color = &quot;#0000FF&quot;;
cb_ad_color = &quot;#000000&quot;;
&lt;/script&gt;
&lt;script src=&quot;http://www.cbclicks.com/getclickedjava.php?top=earn%20money%20Clickbank&amp;cat=&amp;cid=anantha89&quot;&gt;&lt;/script&gt;
&lt;!-- End CBClicks.com code --&gt;</content><link rel='replies' type='application/atom+xml' href='http://prpds.blogspot.com/feeds/5144008849355999161/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prpds.blogspot.com/2012/03/problem-find-rectangle-with-maximum.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7748170484639853544/posts/default/5144008849355999161'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7748170484639853544/posts/default/5144008849355999161'/><link rel='alternate' type='text/html' href='http://prpds.blogspot.com/2012/03/problem-find-rectangle-with-maximum.html' title=''/><author><name>anantha</name><uri>http://www.blogger.com/profile/03120527085848815308</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7748170484639853544.post-4229725389979423877</id><published>2012-02-21T11:37:00.000+05:30</published><updated>2012-02-21T11:37:58.248+05:30</updated><title type='text'>kth smallest element in the union of the two sorted arrays</title><content type='html'>&lt;B&gt;Problem:&lt;/B&gt;&lt;/BR&gt;
Given two sorted arrays A and B of length n, find the kth smallest element in the union of the two sorted arrays. Do so in Θ(lgn).

&lt;/BR&gt;
&lt;B&gt;Complexity:&lt;/B&gt;&lt;/BR&gt;
O(log N)
&lt;/BR&gt;
&lt;B&gt;Code:&lt;/B&gt;&lt;/BR&gt;
&lt;script class=&quot;brush:python&quot; type=&quot;syntaxhighlighter&quot;&gt;
&lt;![CDATA[
import math
def findkthsmallest():
        
    A=[1,5,10,22,30,35,75,125,150,175,200]
    B=[15,16,20,22,25,30,100,155,160,170]
    lM=0
    lN=0
    hM=len(A)-1
    hN=len(B)-1
    k=17

    while True:
        if k==1:
            return min(A[lM],B[lN])
        

        cM=hM-lM+1
        cN=hN-lN+1
        tmp = cM/float(cM+cN)
        iM=int(math.ceil(tmp*k))
        iN=k-iM
        iM=lM+iM-1
        iN=lN+iN-1
        if A[iM] &gt;= B[iN]:
            if iN == hN or A[iM] &lt; B[iN+1]:
                return A[iM]
            else:
                k = k - (iN-lN+1)
                lN=iN+1
                hM=iM-1
        if B[iN] &gt;= A[iM]:
            if iM == hM or B[iN] &lt; A[iM+1]:
                return B[iN]
            else:
                k = k - (iM-lM+1)
                lM=iM+1
                hN=iN-1
        if hM &lt; lM:
            return B[lN+k-1]
        if hN &lt; lN:
            return A[lM+k-1]

if __name__ == &#39;__main__&#39;:
    print findkthsmallest();


]]&gt;
&lt;/script&gt;&lt;br /&gt;
&lt;!-- Start CBClicks.com code --&gt;
&lt;script&gt;
cb_layout = 1;
cb_num = 6;
cb_width = 160;
cb_height = 600;
cb_border_color = &quot;#CCCCCC&quot;;
cb_back_color = &quot;#FFFFFF&quot;;
cb_title_color = &quot;#0000FF&quot;;
cb_ad_color = &quot;#000000&quot;;
&lt;/script&gt;
&lt;script src=&quot;http://www.cbclicks.com/getclickedjava.php?top=earn%20money%20Clickbank&amp;cat=&amp;cid=anantha89&quot;&gt;&lt;/script&gt;
&lt;!-- End CBClicks.com code --&gt;</content><link rel='replies' type='application/atom+xml' href='http://prpds.blogspot.com/feeds/4229725389979423877/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prpds.blogspot.com/2012/02/kth-smallest-element-in-union-of-two.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7748170484639853544/posts/default/4229725389979423877'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7748170484639853544/posts/default/4229725389979423877'/><link rel='alternate' type='text/html' href='http://prpds.blogspot.com/2012/02/kth-smallest-element-in-union-of-two.html' title='kth smallest element in the union of the two sorted arrays'/><author><name>anantha</name><uri>http://www.blogger.com/profile/03120527085848815308</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7748170484639853544.post-360500613431568408</id><published>2012-01-25T14:48:00.000+05:30</published><updated>2012-01-25T14:48:15.407+05:30</updated><title type='text'>Lucene Example</title><content type='html'>&lt;script class=&quot;brush:java&quot; type=&quot;syntaxhighlighter&quot;&gt;
&lt;![CDATA[
        import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;

import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;

/**
 *
 * @author srkrishnan
 */
public class Searchtest1
{

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        RAMDirectory ramdir = new RAMDirectory();

        IndexWriterConfig writerconfig = new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35));
        IndexWriter writer = null;
        try
        {
            writer = new IndexWriter(ramdir, writerconfig);
        } catch (CorruptIndexException ex)
        {
            Logger.getLogger(Searchtest1.class.getName()).log(Level.SEVERE, null, ex);
        } catch (LockObtainFailedException ex)
        {
            Logger.getLogger(Searchtest1.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex)
        {
            Logger.getLogger(Searchtest1.class.getName()).log(Level.SEVERE, null, ex);
        }
        try
        {
            for(int i=0;i&lt;Integer.MAX_VALUE-10;i++)
            {
            // Add some Document objects containing quotes
            writer.addDocument(createDocument(&quot;Theodore Roosevelt&quot;+i,
                    &quot;It behooves every man to remember that the work of the &quot;
                    + &quot;critic, is of altogether secondary importance, and that, &quot;
                    + &quot;in the end, progress is accomplished by the man who does &quot;
                    + &quot;things.&quot;));
            writer.addDocument(createDocument(&quot;Friedrich Hayek&quot;+i,
                    &quot;The case for individual freedom rests largely on the &quot;
                    + &quot;recognition of the inevitable and universal ignorance &quot;
                    + &quot;of all of us concerning a great many of the factors on &quot;
                    + &quot;which the achievements of our ends and welfare depend.&quot;));
            writer.addDocument(createDocument(&quot;Ayn Rand&quot;+i,
                    &quot;There is nothing to take a man&#39;s freedom away from &quot;
                    + &quot;him, save other men. To be free, a man must be free &quot;
                    + &quot;of his brothers.&quot;));
            writer.addDocument(createDocument(&quot;Mohandas Gandhi&quot;+i,
                    &quot;Freedom is not worth having if it does not connote &quot;
                    + &quot;freedom to err.&quot;));
            }
            // Optimize and close the writer to finish building the index
            writer.optimize();
            writer.close();

        } catch (CorruptIndexException ex)
        {
            Logger.getLogger(Searchtest1.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex)
        {
            Logger.getLogger(Searchtest1.class.getName()).log(Level.SEVERE, null, ex);
        }
        try
        {
            IndexSearcher searcher = new IndexSearcher(ramdir);
            try
            {
                // Run some queries
                search(searcher, &quot;freedom to err&quot;);
                search(searcher, &quot;free&quot;);
                search(searcher, &quot;progress or achievements&quot;);
            } catch (ParseException ex)
            {
                Logger.getLogger(Searchtest1.class.getName()).log(Level.SEVERE, null, ex);
            }

        } catch (CorruptIndexException ex)
        {
            Logger.getLogger(Searchtest1.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex)
        {
            Logger.getLogger(Searchtest1.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    /**
     * Make a Document object with an un-indexed title field and an indexed
     * content field.
     */
    private static Document createDocument(String title, String content)
    {
        Document doc = new Document();

        // Add the title as an unindexed field...
        doc.add(new Field(&quot;title&quot;, title, Field.Store.YES, Field.Index.ANALYZED));

        // ...and the content as an indexed field. Note that indexed
        // Text fields are constructed using a Reader. Lucene can read
        // and index very large chunks of text, without storing the
        // entire content verbatim in the index. In this example we
        // can just wrap the content string in a StringReader.
        doc.add(new Field(&quot;content&quot;, content, Field.Store.NO, Field.Index.ANALYZED));

        return doc;
    }

    /**
     * Searches for the given string in the &quot;content&quot; field
     */
    private static void search(IndexSearcher searcher, String queryString)
            throws ParseException, IOException
    {

        // Build a Query object
        QueryParser queryParser = new QueryParser(Version.LUCENE_35, &quot;content&quot;, new StandardAnalyzer(Version.LUCENE_35));
        Query query = queryParser.parse(queryString);

        // Search for the query
        TopDocs hits = searcher.search(query, 10);

        // Examine the Hits object to see if there were any matches
        int hitCount = hits.totalHits;
        if (hitCount == 0)
        {
            System.out.println(
                    &quot;No matches were found for \&quot;&quot; + queryString + &quot;\&quot;&quot;);
        } else
        {
            System.out.println(&quot;Hits for \&quot;&quot;
                    + queryString + &quot;\&quot; were found in quotes by:&quot;);

            ScoreDoc[] scoredocs = hits.scoreDocs;
            // Iterate over the Documents in the Hits object
            for (int i = 0; i &lt; scoredocs.length; i++)
            {
                ScoreDoc scoredoc = scoredocs[i];// .doc(i);

                Document doc = searcher.doc(scoredoc.doc);
                // Print the value that we stored in the &quot;title&quot; field. Note
                // that this Field was not indexed, but (unlike the
                // &quot;contents&quot; field) was stored verbatim and can be
                // retrieved.
                System.out.println(&quot;  &quot; + (i + 1) + &quot;. &quot; + doc.get(&quot;title&quot;));
            }
        }
        System.out.println();
    }
}
]]&gt;
&lt;/script&gt;</content><link rel='replies' type='application/atom+xml' href='http://prpds.blogspot.com/feeds/360500613431568408/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prpds.blogspot.com/2012/01/lucene-example.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7748170484639853544/posts/default/360500613431568408'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7748170484639853544/posts/default/360500613431568408'/><link rel='alternate' type='text/html' href='http://prpds.blogspot.com/2012/01/lucene-example.html' title='Lucene Example'/><author><name>anantha</name><uri>http://www.blogger.com/profile/03120527085848815308</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7748170484639853544.post-3969119420024782153</id><published>2012-01-20T11:45:00.001+05:30</published><updated>2012-01-20T11:47:12.868+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="3g"/><category scheme="http://www.blogger.com/atom/ns#" term="bsnl"/><category scheme="http://www.blogger.com/atom/ns#" term="LW272"/><category scheme="http://www.blogger.com/atom/ns#" term="unlock 3g"/><title type='text'>Unlock BSNL 3G data card LW272</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;/div&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;Install BSNL 3G software.&lt;/li&gt;
&lt;li&gt;Simply copy the following content to text file&lt;/li&gt;
&lt;/ul&gt;
Windows XP&lt;br /&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;background-color: white; color: #222222; font-family: &#39;Courier New&#39;, Courier, monospace; font-size: 13px; line-height: 18px;&quot;&gt;Windows Registry Editor Version 5.00&lt;/span&gt;&lt;/div&gt;
&lt;span style=&quot;background-color: white; color: #222222; font-family: &#39;Courier New&#39;, Courier, monospace; font-size: 13px; line-height: 18px;&quot;&gt;[HKEY_LOCAL_MACHINE\SOFTWARE\Linktop3G]&lt;br /&gt;&quot;Version&quot;=&quot;V1&quot;&lt;br /&gt;@=&quot;&quot;&lt;br /&gt;&quot;Engineer&quot;=dword:00000000&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
Windows 7&lt;/div&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;Windows Registry Editor Version 5.00&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Linktop3G]&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&quot;Version&quot;=&quot;V1&quot;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;@=&quot;&quot;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&quot;Engineer&quot;=dword:00000000&lt;/span&gt;&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;Save it as unlock.reg&lt;/li&gt;
&lt;li&gt;run it.&lt;/li&gt;
&lt;li&gt;In Windows 7 right click bsnl.exe and click &quot;Troubleshoot Compatibility&quot;.&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;script&gt;
cb_layout = 1;
cb_num = 6;
cb_width = 160;
cb_height = 600;
cb_border_color = &quot;#CCCCCC&quot;;
cb_back_color = &quot;#FFFFFF&quot;;
cb_title_color = &quot;#0000FF&quot;;
cb_ad_color = &quot;#000000&quot;;
&lt;/script&gt;
&lt;script src=&quot;http://www.cbclicks.com/getclickedjava.php?top=earn%20money%20Clickbank&amp;amp;cat=&amp;amp;cid=anantha89&quot;&gt;
&lt;/script&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prpds.blogspot.com/feeds/3969119420024782153/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prpds.blogspot.com/2012/01/unlock-bsnl-3g-data-card-lw272.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7748170484639853544/posts/default/3969119420024782153'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7748170484639853544/posts/default/3969119420024782153'/><link rel='alternate' type='text/html' href='http://prpds.blogspot.com/2012/01/unlock-bsnl-3g-data-card-lw272.html' title='Unlock BSNL 3G data card LW272'/><author><name>anantha</name><uri>http://www.blogger.com/profile/03120527085848815308</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7748170484639853544.post-3259026791916545631</id><published>2012-01-09T20:20:00.001+05:30</published><updated>2012-01-09T20:20:36.880+05:30</updated><title type='text'>Ask Questions,Get Answers</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Check out&amp;nbsp;&lt;a href=&quot;http://chatbot.in/&quot;&gt;http://chatbot.in/&lt;/a&gt;.&lt;br /&gt;
&lt;script&gt;
cb_layout = 1;
cb_num = 6;
cb_width = 160;
cb_height = 600;
cb_border_color = &quot;#CCCCCC&quot;;
cb_back_color = &quot;#FFFFFF&quot;;
cb_title_color = &quot;#0000FF&quot;;
cb_ad_color = &quot;#000000&quot;;
&lt;/script&gt;
&lt;script src=&quot;http://www.cbclicks.com/getclickedjava.php?top=earn%20money%20Clickbank&amp;amp;cat=&amp;amp;cid=anantha89&quot;&gt;
&lt;/script&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prpds.blogspot.com/feeds/3259026791916545631/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prpds.blogspot.com/2012/01/ask-questionsget-answer.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7748170484639853544/posts/default/3259026791916545631'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7748170484639853544/posts/default/3259026791916545631'/><link rel='alternate' type='text/html' href='http://prpds.blogspot.com/2012/01/ask-questionsget-answer.html' title='Ask Questions,Get Answers'/><author><name>anantha</name><uri>http://www.blogger.com/profile/03120527085848815308</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7748170484639853544.post-5886276407314179195</id><published>2011-12-22T13:02:00.000+05:30</published><updated>2011-12-22T13:02:25.263+05:30</updated><title type='text'>Converting Text file to wav file</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;b&gt;Below Code can be used to convert any text file to wav file&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;br /&gt;
&lt;script class=&quot;brush:java&quot; type=&quot;syntaxhighlighter&quot;&gt;
&lt;![CDATA[
        try {
            VoiceManager voicemanager = VoiceManager.getInstance();
            Voice voice = voicemanager.getVoice(&quot;kevin16&quot;);
            voice.allocate();
            SingleFileAudioPlayer sfap = new SingleFileAudioPlayer(&quot;D:\\a&quot;, Type.WAVE);
            voice.setAudioPlayer(sfap);
            voice.speak(new FileInputStream(&quot;D:\\mysample.txt&quot;));
            sfap.close();
            voice.deallocate();
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        }
]]&gt;
&lt;/script&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;br /&gt;
&lt;script&gt;
cb_layout = 1;
cb_num = 6;
cb_width = 160;
cb_height = 600;
cb_border_color = &quot;#CCCCCC&quot;;
cb_back_color = &quot;#FFFFFF&quot;;
cb_title_color = &quot;#0000FF&quot;;
cb_ad_color = &quot;#000000&quot;;
&lt;/script&gt;
&lt;script src=&quot;http://www.cbclicks.com/getclickedjava.php?top=earn%20money%20Clickbank&amp;amp;cat=&amp;amp;cid=anantha89&quot;&gt;
&lt;/script&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prpds.blogspot.com/feeds/5886276407314179195/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prpds.blogspot.com/2011/12/below-code-can-be-used-to-convert-any.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7748170484639853544/posts/default/5886276407314179195'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7748170484639853544/posts/default/5886276407314179195'/><link rel='alternate' type='text/html' href='http://prpds.blogspot.com/2011/12/below-code-can-be-used-to-convert-any.html' title='Converting Text file to wav file'/><author><name>anantha</name><uri>http://www.blogger.com/profile/03120527085848815308</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7748170484639853544.post-6988644473066296521</id><published>2011-12-22T12:57:00.000+05:30</published><updated>2011-12-22T12:57:37.287+05:30</updated><title type='text'>Converting Text to wav File</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;b&gt;Below code can be used to convert text to wav file:&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;br /&gt;
&lt;script class=&quot;brush:java&quot; type=&quot;syntaxhighlighter&quot;&gt;
&lt;![CDATA[
        VoiceManager voicemanager = VoiceManager.getInstance();
        Voice voice = voicemanager.getVoice(&quot;kevin16&quot;);
        voice.allocate();
        SingleFileAudioPlayer sfap = new SingleFileAudioPlayer(&quot;D:\\a&quot;, Type.WAVE);
        voice.setAudioPlayer(sfap);
        voice.speak(&quot;Hello!! System property x is undefined&quot;);
        sfap.close();
        voice.deallocate();
]]&gt;
&lt;/script&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;br /&gt;
&lt;script&gt;
cb_layout = 1;
cb_num = 6;
cb_width = 160;
cb_height = 600;
cb_border_color = &quot;#CCCCCC&quot;;
cb_back_color = &quot;#FFFFFF&quot;;
cb_title_color = &quot;#0000FF&quot;;
cb_ad_color = &quot;#000000&quot;;
&lt;/script&gt;
&lt;script src=&quot;http://www.cbclicks.com/getclickedjava.php?top=earn%20money%20Clickbank&amp;amp;cat=&amp;amp;cid=anantha89&quot;&gt;
&lt;/script&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prpds.blogspot.com/feeds/6988644473066296521/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prpds.blogspot.com/2011/12/converting-text-to-wav-file.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7748170484639853544/posts/default/6988644473066296521'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7748170484639853544/posts/default/6988644473066296521'/><link rel='alternate' type='text/html' href='http://prpds.blogspot.com/2011/12/converting-text-to-wav-file.html' title='Converting Text to wav File'/><author><name>anantha</name><uri>http://www.blogger.com/profile/03120527085848815308</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7748170484639853544.post-6656816469157646938</id><published>2011-09-12T17:38:00.000+05:30</published><updated>2011-09-12T17:38:58.589+05:30</updated><title type='text'>Generate all the strings of n bits</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:&lt;/b&gt;&lt;br /&gt;
Generate all the strings of n bits.&lt;br /&gt;
&lt;b&gt;Solution:&lt;/b&gt;&lt;br /&gt;
Subtract and Conquer.&lt;br /&gt;
&lt;b&gt;Complexity: O(2^n)&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;Code:&lt;/b&gt;&lt;br /&gt;
&lt;script class=&quot;brush:python&quot; type=&quot;syntaxhighlighter&quot;&gt;
&lt;![CDATA[
def printbinary(n,A,N):
    if n==N:
        print A
        return
    A[n]=0
    printbinary(n+1,A,N)
    A[n]=1
    printbinary(n+1,A,N)
 
n=10
A=[0]*n
printbinary(0,A,n)
]]&gt;
&lt;/script&gt;

&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://prpds.blogspot.com/feeds/6656816469157646938/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prpds.blogspot.com/2011/09/generate-all-strings-of-n-bits.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7748170484639853544/posts/default/6656816469157646938'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7748170484639853544/posts/default/6656816469157646938'/><link rel='alternate' type='text/html' href='http://prpds.blogspot.com/2011/09/generate-all-strings-of-n-bits.html' title='Generate all the strings of n bits'/><author><name>anantha</name><uri>http://www.blogger.com/profile/03120527085848815308</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7748170484639853544.post-5062523583496954089</id><published>2011-08-30T15:52:00.000+05:30</published><updated>2011-09-12T17:15:59.511+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="array"/><category scheme="http://www.blogger.com/atom/ns#" term="closest to zero"/><category scheme="http://www.blogger.com/atom/ns#" term="pick two numbers"/><title type='text'></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:&lt;/b&gt;&lt;br /&gt;
Given an array of integers. How do we pick two numbers such that their sum is closest to zero.&lt;br /&gt;
&lt;b&gt;Solution:&amp;nbsp;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Sort the array by comparing absolute value.&lt;/li&gt;
&lt;li&gt;traverse the array once to find the two consecutive numbers with minimum sum.&lt;/li&gt;
&lt;/ul&gt;&lt;br /&gt;
&lt;b&gt;Complexity:&amp;nbsp;&lt;/b&gt;&lt;b&gt;O(n log n)&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;Code:&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;
&lt;/b&gt;&lt;br /&gt;
&lt;script class=&quot;brush:python&quot; type=&quot;syntaxhighlighter&quot;&gt;
&lt;![CDATA[
import math
def solve():
    a=[10,20,30,40,50,160,-15,-25,-60,-100,0,-11]
    n=len(a)
    qsort(a,0,n-1)
    #print a
    findClosestToZero(a)


def qsort(a,begin,end):
	if begin&gt;=end:
		return
	p=partition(a,begin,end)
	qsort(a,begin,p-1)
	qsort(a,p+1,end)

def partition(a,begin,end):
	t=math.fabs(a[end])
	j=begin
	for i in range(begin,end):
		if math.fabs(a[i])&lt;math.fabs(t):
			temp=a[i]
			a[i]=a[j]
			a[j]=temp
			j=j+1
	temp=a[end]
	a[end]=a[j]
	a[j]=temp
	return j

def findClosestToZero(a):
    n=len(a)
    min=99999
    for i in range(0,n-1):
        if math.fabs(a[i]+a[i+1]) &lt; min:
            min=math.fabs(a[i]+a[i+1])
            A=a[i]
            B=a[i+1]
    print A,B
    
solve()

]]&gt;
&lt;/script&gt;&lt;br /&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prpds.blogspot.com/feeds/5062523583496954089/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prpds.blogspot.com/2011/08/problem-given-array-of-integers.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7748170484639853544/posts/default/5062523583496954089'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7748170484639853544/posts/default/5062523583496954089'/><link rel='alternate' type='text/html' href='http://prpds.blogspot.com/2011/08/problem-given-array-of-integers.html' title=''/><author><name>anantha</name><uri>http://www.blogger.com/profile/03120527085848815308</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7748170484639853544.post-492862150203706239</id><published>2011-08-28T16:13:00.000+05:30</published><updated>2011-08-28T16:13:32.883+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="abc123"/><category scheme="http://www.blogger.com/atom/ns#" term="dutch flag"/><category scheme="http://www.blogger.com/atom/ns#" term="reverse dutch flag"/><title type='text'>a1a2a3....anb1b2b3....bn to a1b1a2b2a3b3....anbn</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:&lt;/b&gt;&lt;br /&gt;
Convert a1a2a3....anb1b2b3.....bn to&lt;br /&gt;
a1b1a2b2a3b3.....anbn&lt;br /&gt;
in O(n) time and O(1) space&lt;br /&gt;
&lt;b&gt;Solution:&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;background-color: white; font-family: arial, sans-serif; font-size: 13px; font-weight: normal;&quot;&gt;&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;div&gt;&lt;b&gt;[b] &amp;nbsp; &amp;nbsp; &amp;nbsp; a&amp;nbsp;&lt;span style=&quot;background-color: #ffff99;&quot;&gt;b&amp;nbsp;&lt;/span&gt;c d e 1 2 3 4 5 &amp;nbsp; &amp;nbsp; &amp;nbsp;[2&amp;lt;=5, index=2*2-1=3]&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;[c] &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;a&amp;nbsp;b&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: #ffff99;&quot;&gt;b&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;d e 1 2 3&lt;/span&gt;&amp;nbsp;4 5 &amp;nbsp; &amp;nbsp; &amp;nbsp;[3&amp;lt;=5, index=3*2-1=5]&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;[e] &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;a&amp;nbsp;b b&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;d&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: #ffff99;&quot;&gt;c&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;1 2 3&lt;/span&gt;&amp;nbsp;4 5 &amp;nbsp; &amp;nbsp; &amp;nbsp;[5&amp;lt;=5, index=5*2-1=9]&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;[4] &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;a&amp;nbsp;b b&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;d&amp;nbsp;c&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;1 2 3&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;background-color: #ffff99;&quot;&gt;e&lt;/span&gt;&amp;nbsp;5 &amp;nbsp; &amp;nbsp; &amp;nbsp;[9&amp;gt;5, index=(9-5)*2=8]&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;[3] &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;a&amp;nbsp;b b&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;d&amp;nbsp;c&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;1 2&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: #ffff99;&quot;&gt;4&lt;/span&gt;&amp;nbsp;e 5 &amp;nbsp; &amp;nbsp; &amp;nbsp;[8&amp;gt;5, index=(8-5)*2=6]&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;[1] &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;a&amp;nbsp;b b&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;d&amp;nbsp;c&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: #ffff99;&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;2 4&lt;/span&gt;&amp;nbsp;e 5 &amp;nbsp; &amp;nbsp; &amp;nbsp;[6&amp;gt;5, index=(6-5)*2=2]&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;[b] &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;a&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: #ffff99;&quot;&gt;1&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;b&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;d&amp;nbsp;c&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;3&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;2 4&lt;/span&gt;&amp;nbsp;e 5 &amp;nbsp; &amp;nbsp; &amp;nbsp;[startindex=index=2]&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;[d] &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;a 1 b&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: #ffff99;&quot;&gt;d&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;c&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;3&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;2 4&lt;/span&gt;&amp;nbsp;e 5 &amp;nbsp; &amp;nbsp; &amp;nbsp;[4&amp;lt;=5,index=4*2-1=7]&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;[2] &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;a 1 b&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;d&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;c&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;3&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: #ffff99;&quot;&gt;d&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;4&lt;/span&gt;&amp;nbsp;e 5 &amp;nbsp; &amp;nbsp; &amp;nbsp;[7&amp;gt;5,index=(7-5)*2=4]&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;[d] &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;a 1 b&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: #ffff99;&quot;&gt;2&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;c&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;3&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;d&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;4&lt;/span&gt;&amp;nbsp;e 5 &amp;nbsp; &amp;nbsp; &amp;nbsp;[startindex=index=4]&lt;/b&gt;&lt;/div&gt;&lt;br /&gt;
&lt;b&gt;Complexity: O(n) time O(1) space&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;Code:&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;
&lt;/b&gt;&lt;br /&gt;
&lt;script class=&quot;brush:python&quot; type=&quot;syntaxhighlighter&quot;&gt;
&lt;![CDATA[
a=[&#39;-&#39;,&#39;a&#39;,&#39;b&#39;,&#39;c&#39;,&#39;d&#39;,&#39;e&#39;,&#39;1&#39;,&#39;2&#39;,&#39;3&#39;,&#39;4&#39;,&#39;5&#39;]
n=len(a)-1
i=2
itr=0
while(itr&lt;n-2):
    starti=i
    b=a[i]
    if i&lt;=n/2:
        i=i*2-1
    else:
        i=(i-n/2)*2
    while(1):
        tmp=a[i]
        a[i]=b
        b=tmp
        itr=itr+1
        if i==starti:
            break;
        if i&lt;=n/2:
            i=i*2-1
        else:
            i=(i-n/2)*2
    i=i+2
    print a
    print &#39;itr&#39;,itr

]]&gt;
&lt;/script&gt;&lt;br /&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prpds.blogspot.com/feeds/492862150203706239/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prpds.blogspot.com/2011/08/a1a2a3anb1b2b3bn-to-a1b1a2b2a3b3anbn.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7748170484639853544/posts/default/492862150203706239'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7748170484639853544/posts/default/492862150203706239'/><link rel='alternate' type='text/html' href='http://prpds.blogspot.com/2011/08/a1a2a3anb1b2b3bn-to-a1b1a2b2a3b3anbn.html' title='a1a2a3....anb1b2b3....bn to a1b1a2b2a3b3....anbn'/><author><name>anantha</name><uri>http://www.blogger.com/profile/03120527085848815308</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7748170484639853544.post-2077903898116561971</id><published>2011-08-18T16:56:00.000+05:30</published><updated>2011-08-18T16:56:20.970+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Algorithm"/><category scheme="http://www.blogger.com/atom/ns#" term="array"/><category scheme="http://www.blogger.com/atom/ns#" term="|a-b| + |b-c| + |c-a|"/><title type='text'>|a-b| + |b-c| + |c-a|</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:&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;background-color: white; color: #565e67; font-family: Verdana, Helvetica, sans-serif; font-size: 12px; font-weight: normal;&quot;&gt;&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;div&gt;&lt;b&gt;&lt;span style=&quot;background-color: white; color: #565e67; font-family: Verdana, Helvetica, sans-serif; font-size: 12px;&quot;&gt;Given n arrays, find n number such that sum of their differences is minimum. For e.g. if there are three arrays&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;&lt;span style=&quot;background-color: white; color: #565e67; font-family: Verdana, Helvetica, sans-serif; font-size: 12px;&quot;&gt;&lt;div style=&quot;padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;A = {4, 10, 15, 20}&lt;br /&gt;
B = {1, 13, 29}&lt;br /&gt;
C = {5, 14, 28}&lt;/div&gt;&lt;div style=&quot;padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;find three numbers a, b, c such that |a-b| + |b-c| + |c-a| is minimum&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;/span&gt;&lt;span style=&quot;background-color: white; color: #565e67; font-family: Verdana, Helvetica, sans-serif; font-size: 12px;&quot;&gt;where a E A , bEB , cEC&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;&lt;span style=&quot;color: #565e67; font-family: Verdana, Helvetica, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 12px;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;background-color: white; color: #565e67; font-family: Verdana, Helvetica, sans-serif; font-size: 12px;&quot;&gt;&lt;div style=&quot;padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;. Here the answer is a = 15, b = 13, and c = 14&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;br /&gt;
&lt;b&gt;Solution:&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;background-color: white; font-family: arial, sans-serif; font-size: 13px; font-weight: normal;&quot;&gt;&lt;span style=&quot;background-color: white; color: #565e67; font-family: Verdana, Helvetica, sans-serif; font-size: 12px;&quot;&gt;Let,&lt;/span&gt;&lt;span style=&quot;background-color: white; color: #565e67; font-family: Verdana, Helvetica, sans-serif; font-size: 12px;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;min_dif=INT_MAX&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;1.Sort the N arrays A,B,C.....&lt;br /&gt;
2.Find the minimum and maximum of A[0],B[0],C[0].....&lt;br /&gt;
3.Take the difference between MAX-MIN values.&lt;br /&gt;
5.If the difference is less than min_dif then update min_dif and save all n values.&lt;br /&gt;
6.Now increment the index of the array which contains minimum element.&lt;br /&gt;
7.repeat these steps till end of array is reached for atleast one array.&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Complexity: O(total no of elements)&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;Code:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;script class=&quot;brush:python&quot; type=&quot;syntaxhighlighter&quot;&gt;
&lt;![CDATA[
A=[10,25,35]
B=[10,25,30]
C=[5,6,25,30]
A.sort(),B.sort(),C.sort()
i=0
j=0
k=0
t=-1
An=len(A)
Bn=len(B)
Cn=len(C)
Max=0
Min=min_dif=99999
while i&lt;An and j&lt;Bn and k&lt;Cn:
    if A[i]&gt;Max:
        Max=A[i]
    if B[j]&gt;Max:
        Max=B[j]
    if C[k]&gt;Max:
        Max=C[k]
    if A[i]&lt;Min:
        Min=A[i]
        t=1
    if B[j]&lt;Min:
        Min=B[j]
        t=2
    if C[k]&lt;Min:
        Min=C[k]
        t=3
    
 
    if Max-Min &lt; min_dif:
        min_dif=Max-Min
        min_i=i
        min_j=j
        min_k=k
        
    if t==1:
        i=i+1
    if t==2:
        j=j+1
    if t==3:
        k=k+1
    Max=0
    Min=99999
print &quot;A=&quot;,A[min_i],&quot; B=&quot;,B[min_j],&quot; C=&quot;,C[min_k],&quot; min diff=&quot;,min_dif
]]&gt;
&lt;/script&gt;&lt;br /&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prpds.blogspot.com/feeds/2077903898116561971/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prpds.blogspot.com/2011/08/b-b-c-c.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7748170484639853544/posts/default/2077903898116561971'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7748170484639853544/posts/default/2077903898116561971'/><link rel='alternate' type='text/html' href='http://prpds.blogspot.com/2011/08/b-b-c-c.html' title='|a-b| + |b-c| + |c-a|'/><author><name>anantha</name><uri>http://www.blogger.com/profile/03120527085848815308</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7748170484639853544.post-8737156344623926540</id><published>2011-08-10T15:24:00.000+05:30</published><updated>2011-08-10T15:24:17.870+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Algorithm"/><category scheme="http://www.blogger.com/atom/ns#" term="binary tree"/><category scheme="http://www.blogger.com/atom/ns#" term="Diameter"/><title type='text'>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;&lt;b&gt;Problem:&lt;/b&gt;&lt;br /&gt;
Find the diameter of a binary tree.&lt;br /&gt;
&lt;b&gt;Solution:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Do Post order traversal&lt;/li&gt;
&lt;li&gt;if Left Max path + Right Max path &amp;gt; Max then&lt;/li&gt;
&lt;li&gt;Max = Left Max&amp;nbsp;path + Right Max path&amp;nbsp;&lt;/li&gt;
&lt;li&gt;return 1 + MAX(Left Max path length + Right Max path length)&lt;/li&gt;
&lt;/ol&gt;&lt;br /&gt;
&lt;b&gt;Complexity: O(n), where n = no of nodes&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;Code:&lt;/b&gt;&lt;br /&gt;
&lt;script class=&quot;brush:c&quot; type=&quot;syntaxhighlighter&quot;&gt;
&lt;![CDATA[
int _treeDiameter(Tnode *root, int *Max) {
    if (root == NULL)
        return 0;
    //assign S to Current Max
    int S = *Max;
    int left = _treeDiameter(root-&gt;left, &amp;S);
    int right = _treeDiameter(root-&gt;right, &amp;S);
    
    //check for new longest path
    if (left + right &gt; S) {
        *Max = left + right;
    } else {
        *Max = S;
    }
    if (left &gt;= right) {
        return left + 1;
    }
    return right + 1;
}
]]&gt;
&lt;/script&gt;&lt;br /&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prpds.blogspot.com/feeds/8737156344623926540/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prpds.blogspot.com/2011/08/diameter-of-binary-tree.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7748170484639853544/posts/default/8737156344623926540'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7748170484639853544/posts/default/8737156344623926540'/><link rel='alternate' type='text/html' href='http://prpds.blogspot.com/2011/08/diameter-of-binary-tree.html' title='Diameter of a binary tree'/><author><name>anantha</name><uri>http://www.blogger.com/profile/03120527085848815308</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7748170484639853544.post-1013105406896112420</id><published>2011-08-09T22:32:00.000+05:30</published><updated>2011-08-09T22:32:40.913+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="CLRS"/><category scheme="http://www.blogger.com/atom/ns#" term="find missing number"/><title type='text'>CLRS (Page 85): Problems 4-2 : Finding the missing integer</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:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;font-weight: bold;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: normal;&quot;&gt;An array A[1...n] contains all the integers from 0 to n except one. It would be easy to determine the missing&lt;/span&gt;&lt;/div&gt;integer in O(n) time by using an auxiliary array B[0...n] to record which numbers appear in A. In this&lt;br /&gt;
problem, however, we cannot access an entire integer in A with a single operation. The elements of A are&lt;br /&gt;
represented in binary, and the only operation we can use to access them is ”fetch the jth bit of A[i],” which&lt;br /&gt;
takes constant time.&lt;br /&gt;
Show that if we use only this operation, we can still determine the missing integer in O(n) time.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Solution:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Our convention is to call the least signiﬁcant bit (LSB) as 0-th bit. Number of digits required to represent&lt;br /&gt;
n is at least dlg(n + 1)e. Most signiﬁcant bit (MSB) of n is the (dlg(n + 1)e − 1)-th bit of it.&lt;br /&gt;
Among the integers between 0 to n, exactly 2&lt;br /&gt;
(dlg(n+1)e−1)&lt;br /&gt;
numbers are &amp;lt; 2&lt;br /&gt;
(dlg(n+1)e−1)&lt;br /&gt;
. If we look at&lt;br /&gt;
the (dlg(n + 1)e − 1)-th bit of any non-negative number ≤ n we can determine whether the number is&lt;br /&gt;
&amp;lt; 2&lt;br /&gt;
(dlg(n+1)e−1)&lt;br /&gt;
. We now look at the (dlg(n + 1)e − 1)-th bit of all the numbers in A[1...n]. We populate two&lt;br /&gt;
lists of references (e.g. pointer, index, handle etc) during the ﬁrst n lookup. Reference to the numbers having&lt;br /&gt;
0 in their (dlg(n + 1)e − 1)-th bit are kept in one and the rest in the other. The size of the ﬁrst list is the&lt;br /&gt;
count of numbers &amp;lt; 2&lt;br /&gt;
(dlg(n+1)e−1)&lt;br /&gt;
. The missing integer is &amp;lt; 2&lt;br /&gt;
(dlg(n+1)e−1)&lt;br /&gt;
iﬀ this count is 2&lt;br /&gt;
(dlg(n+1)e−1) − 1.&lt;br /&gt;
We look for the missing integer in the ﬁrst list in that case. Otherwise, we look in the second list. In this&lt;br /&gt;
process, we ﬁgure out the (dlg(n + 1)e − 1)-th bit of the missing integer in n bit lookup.&lt;br /&gt;
We want to use recursion to search the appropriate list. The fact that now we search in a list of references&lt;br /&gt;
to numbers in stead of a list of numbers can be handled as follows. The initial problem comes with the list&lt;br /&gt;
all the references. The fact that the search range could be shifted in the subproblem is handled by replacing&lt;br /&gt;
all the (dlg(n + 1)e − 1)-th bits as 0. However, as we never read the (dlg(n + 1)e − 1)-th bits again we don’t&lt;br /&gt;
have to do this modiﬁcation explicitly.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
find_missing_integer ( list_of_references, n )&lt;br /&gt;
if ( n is 1 )&lt;br /&gt;
print 0-th bit of missing number is (1 - the number in the list)&lt;br /&gt;
return&lt;br /&gt;
left_list . initialize ()&lt;br /&gt;
right_list . initialize ()&lt;br /&gt;
msb_position &amp;lt;- ceiling( lg(n+1) ) - 1&lt;br /&gt;
forall number in list_of_references&lt;br /&gt;
if ( number [ msb_position ] is 0 )&lt;br /&gt;
left_list . insert ( number )&lt;br /&gt;
else&lt;br /&gt;
right_list . insert ( number )&lt;br /&gt;
if ( left_list . size () &amp;lt; power_of_two ( msb_position ) )&lt;br /&gt;
print msb_position-th bit of missing number is 0&lt;br /&gt;
find_missing_integer ( left_list, power_of_two ( msb_position ) - 1 )&lt;br /&gt;
else&lt;br /&gt;
print msb_position-th bit of missing number is 1&lt;br /&gt;
find_missing_integer ( right_list, n - power_of_two ( msb_position ) )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Complexity: O(log n)&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;Code:&lt;/b&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prpds.blogspot.com/feeds/1013105406896112420/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prpds.blogspot.com/2011/08/clrs-page-85-problems-4-2-finding.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7748170484639853544/posts/default/1013105406896112420'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7748170484639853544/posts/default/1013105406896112420'/><link rel='alternate' type='text/html' href='http://prpds.blogspot.com/2011/08/clrs-page-85-problems-4-2-finding.html' title='CLRS (Page 85): Problems 4-2 : Finding the missing integer'/><author><name>anantha</name><uri>http://www.blogger.com/profile/03120527085848815308</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7748170484639853544.post-8182191735798013747</id><published>2011-08-09T00:05:00.001+05:30</published><updated>2011-08-09T00:06:53.320+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="CLRS"/><category scheme="http://www.blogger.com/atom/ns#" term="random"/><title type='text'>CLRS Exercises 5.1-2</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:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Describe an implementation of the procedure RANDOM(a, b) that only makes calls to&lt;br /&gt;
RANDOM(0, 1). What is the expected running time of your procedure, as a function of a and&lt;br /&gt;
b?&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Solution:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;&lt;li&gt;n = ceil(log(b-a+1))&lt;/li&gt;
&lt;li&gt;decimal_number=0;&lt;/li&gt;
&lt;li&gt;do&lt;/li&gt;
&lt;li&gt;for i = 1 to n: get all binary&lt;/li&gt;
&lt;li&gt;decimal_number = generate decimal from binary digits&lt;/li&gt;
&lt;li&gt;while decimal_number &amp;gt; b-a+1&lt;/li&gt;
&lt;li&gt;return decimal_number&lt;/li&gt;
&lt;/ol&gt;&lt;br /&gt;
&lt;b&gt;Complexity:&amp;nbsp;&lt;/b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;background-color: white; font-family: &#39;trebuchet ms&#39;, sans-serif; font-size: 13px;&quot;&gt;&amp;nbsp;&lt;span style=&quot;background-color: #fff9ee; color: #222222; line-height: 21px;&quot;&gt;Θ(log (b-a+1))&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;
&lt;/b&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prpds.blogspot.com/feeds/8182191735798013747/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prpds.blogspot.com/2011/08/clsr-exercises-51-2.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7748170484639853544/posts/default/8182191735798013747'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7748170484639853544/posts/default/8182191735798013747'/><link rel='alternate' type='text/html' href='http://prpds.blogspot.com/2011/08/clsr-exercises-51-2.html' title='CLRS Exercises 5.1-2'/><author><name>anantha</name><uri>http://www.blogger.com/profile/03120527085848815308</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7748170484639853544.post-4303879137061556179</id><published>2011-08-08T23:41:00.000+05:30</published><updated>2011-08-08T23:41:25.204+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="CLRS"/><category scheme="http://www.blogger.com/atom/ns#" term="random"/><title type='text'>CLRS Exercises 5.1-3:</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:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Suppose that you want to output 0 with probability 1/2 and 1 with probability 1/2. At your&lt;br /&gt;
disposal is a procedure BIASED-RANDOM, that outputs either 0 or 1. It outputs 1 with some&lt;br /&gt;
probability p and 0 with probability 1 - p, where 0 &amp;lt; p &amp;lt; 1, but you do not know what p is.&lt;br /&gt;
Give an algorithm that uses BIASED-RANDOM as a subroutine, and returns an unbiased&lt;br /&gt;
answer, returning 0 with probability 1/2 and 1 with probability 1/2. What is the expected&lt;br /&gt;
running time of your algorithm as a function of p?&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Solution:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Start while loop&lt;/li&gt;
&lt;li&gt;a=rand(); b=rand()&lt;/li&gt;
&lt;li&gt;if(a==0 &amp;amp;&amp;amp; b==1) return 0;&lt;/li&gt;
&lt;li&gt;else if(a==1 &amp;amp;&amp;amp; b==0) return 1;&lt;/li&gt;
&lt;li&gt;else continue;&lt;/li&gt;
&lt;/ol&gt;&lt;br /&gt;
&lt;b&gt;Complexity:&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;
&lt;/b&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prpds.blogspot.com/feeds/4303879137061556179/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prpds.blogspot.com/2011/08/clrs-exercises-51-3.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7748170484639853544/posts/default/4303879137061556179'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7748170484639853544/posts/default/4303879137061556179'/><link rel='alternate' type='text/html' href='http://prpds.blogspot.com/2011/08/clrs-exercises-51-3.html' title='CLRS Exercises 5.1-3:'/><author><name>anantha</name><uri>http://www.blogger.com/profile/03120527085848815308</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7748170484639853544.post-5006440991134839482</id><published>2011-08-08T23:34:00.000+05:30</published><updated>2011-08-08T23:34:35.452+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="rand5()"/><category scheme="http://www.blogger.com/atom/ns#" term="rand7()"/><category scheme="http://www.blogger.com/atom/ns#" term="random"/><title type='text'>Expand a random range from 1-5 to 1-7</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:&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;background-color: white; font-family: Arial, &#39;Liberation Sans&#39;, &#39;DejaVu Sans&#39;, sans-serif; font-size: 14px; font-weight: normal; line-height: 18px;&quot;&gt;Given a function which produces a random integer in the range 1 to 5, write a function which produces a random integer in the range 1 to 7.&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;Solution:&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;background-color: white; font-family: Arial, &#39;Liberation Sans&#39;, &#39;DejaVu Sans&#39;, sans-serif; font-size: 14px; font-weight: normal; line-height: 18px;&quot;&gt;&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;pre style=&quot;background-attachment: initial; background-clip: initial; background-color: #eeeeee; background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; font-family: Consolas, Menlo, Monaco, &#39;Lucida Console&#39;, &#39;Liberation Mono&#39;, &#39;DejaVu Sans Mono&#39;, &#39;Bitstream Vera Sans Mono&#39;, &#39;Courier New&#39;, monospace, serif; font-size: 14px; margin-bottom: 10px; max-height: 600px; overflow-x: auto; overflow-y: auto; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; vertical-align: baseline; width: auto;&quot;&gt;&lt;b&gt;&lt;code style=&quot;background-attachment: initial; background-clip: initial; background-color: #eeeeee; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; border-bottom-width: 0px; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; font-family: Consolas, Menlo, Monaco, &#39;Lucida Console&#39;, &#39;Liberation Mono&#39;, &#39;DejaVu Sans Mono&#39;, &#39;Bitstream Vera Sans Mono&#39;, &#39;Courier New&#39;, monospace, serif; font-size: 14px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; vertical-align: baseline;&quot;&gt;int i;
do
{
  i = 5 * (rand5() - 1) + rand5();  // i is now uniformly random between 1 and 25
} while(i &amp;gt; 21);
// i is now uniformly random between 1 and 21
return i % 7 + 1;  // result is now uniformly random between 1 and 7&lt;/code&gt;&lt;/b&gt;&lt;/pre&gt;&lt;br /&gt;
&lt;b&gt;Complexity:&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;
&lt;/b&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prpds.blogspot.com/feeds/5006440991134839482/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prpds.blogspot.com/2011/08/expand-random-range-from-1-5-to-1-7.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7748170484639853544/posts/default/5006440991134839482'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7748170484639853544/posts/default/5006440991134839482'/><link rel='alternate' type='text/html' href='http://prpds.blogspot.com/2011/08/expand-random-range-from-1-5-to-1-7.html' title='Expand a random range from 1-5 to 1-7'/><author><name>anantha</name><uri>http://www.blogger.com/profile/03120527085848815308</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7748170484639853544.post-2320316813684466933</id><published>2011-08-07T11:50:00.001+05:30</published><updated>2011-08-09T00:07:41.161+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Algorithm"/><category scheme="http://www.blogger.com/atom/ns#" term="CLRS"/><category scheme="http://www.blogger.com/atom/ns#" term="merge sort"/><title type='text'>CLRS - Problem 2.1</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:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Although merge sort runs in Θ(n lg n) worst-case time and insertion sort runs in Θ(n2) worstcase&lt;br /&gt;
time, the constant factors in insertion sort make it faster for small n. Thus, it makes sense&lt;br /&gt;
to use insertion sort within merge sort when subproblems become sufficiently small. Consider&lt;br /&gt;
a modification to merge sort in which n/k sublists of length k are sorted using insertion sort&lt;br /&gt;
and then merged using the standard merging mechanism, where k is a value to be determined.&lt;br /&gt;
&lt;b&gt;a. Show that the n/k sublists, each of length k, can be sorted by insertion sort in Θ(nk)&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;worst-case time.&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/b&gt;Time to sort each list of length k using insertion sort= T(k^2)&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/b&gt;Time to sort n/k lists each of length k = T(n/k *&lt;b&gt;&amp;nbsp; &lt;/b&gt;k^2) =&amp;nbsp;Θ(nk)&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;
&lt;b&gt;b. Show that the sublists can be merged in Θ(n lg (n/k) worst-case time.&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/b&gt;Height of the tree with n elements and with k = log n - log k = log(n/k)&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; so for merging it will take&amp;nbsp;Θ(n * log(n/k))&lt;br /&gt;
&lt;div style=&quot;font-weight: bold;&quot;&gt;c. Given that the modified algorithm runs in Θ(nk + n lg (n/k)) worst-case time, what is&lt;/div&gt;&lt;div style=&quot;font-weight: bold;&quot;&gt;the largest asymptotic (Θnotation) value of k as a function of n for which the modified&lt;/div&gt;&lt;div style=&quot;font-weight: bold;&quot;&gt;algorithm has the same asymptotic running time as standard merge sort?&lt;/div&gt;Let k=n,&lt;br /&gt;
it will become&amp;nbsp;Θ(n^2 + n lg(1)) =&amp;nbsp;Θ(n^2)&lt;br /&gt;
&lt;br /&gt;
Lets take k=1,&lt;br /&gt;
it will become&amp;nbsp;Θ(n + n lg n)&lt;br /&gt;
&lt;br /&gt;
Lets take k= lg n&lt;br /&gt;
it will become&amp;nbsp;Θ(n lg n + n lg(n/(lg n)) =&amp;gt;&amp;nbsp;Θ(n lg n + n lg n - n lg(lg n)) =&amp;gt;&amp;nbsp;Θ(2 lg n - n lg(lg n)) =&amp;gt;&amp;nbsp;Θ(n lg n)&lt;br /&gt;
&lt;br /&gt;
So, k=&amp;nbsp;Θ(lg n)&lt;br /&gt;
&lt;div style=&quot;font-weight: bold;&quot;&gt;d. How should k be chosen in practice?&lt;/div&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;
&lt;/b&gt;&lt;br /&gt;
In practise k should be taken greater the lg n depending upon the constant factors.&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;
&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;
&lt;/b&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prpds.blogspot.com/feeds/2320316813684466933/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prpds.blogspot.com/2011/08/clsr-problem-21.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7748170484639853544/posts/default/2320316813684466933'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7748170484639853544/posts/default/2320316813684466933'/><link rel='alternate' type='text/html' href='http://prpds.blogspot.com/2011/08/clsr-problem-21.html' title='CLRS - Problem 2.1'/><author><name>anantha</name><uri>http://www.blogger.com/profile/03120527085848815308</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7748170484639853544.post-1276772091307435871</id><published>2011-08-04T17:12:00.001+05:30</published><updated>2011-08-09T00:07:59.120+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="binary search"/><category scheme="http://www.blogger.com/atom/ns#" term="CLRS"/><category scheme="http://www.blogger.com/atom/ns#" term="merge sort"/><category scheme="http://www.blogger.com/atom/ns#" term="sum of two numbers"/><title type='text'>CLRS Exercises 2.3-7</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:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Describe a Θ(n lg n)-time algorithm that, given a set S of n integers and another integer x,&lt;br /&gt;
determines whether or not there exist two elements in S whose sum is exactly x.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Solution:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Merge sort the elements which will take O(n log n)&lt;/li&gt;
&lt;li&gt;then have two pointers one from beginning and another from end&lt;/li&gt;
&lt;li&gt;if sum of both the elements a[i]+a[j] &amp;lt; sum then i++&lt;/li&gt;
&lt;li&gt;else if sum of both the elements a[i]+a[j] &amp;gt; sum then j--&lt;/li&gt;
&lt;li&gt;else we found print those elements a[i] and a[j].&lt;/li&gt;
&lt;li&gt;complexity is O(n) in worst case.&lt;/li&gt;
&lt;li&gt;Alternatively, after merge sort, do binary search for tmp=Sum - a[i] in the array a[i+1 ... n]&lt;/li&gt;
&lt;li&gt;here for binary search complexity is O(log n), so still total worst case complexity is O(n log n)&lt;/li&gt;
&lt;/ol&gt;&lt;br /&gt;
&lt;b&gt;Complexity: O(n log n)&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;Code:&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;
&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;script class=&quot;brush:c&quot; type=&quot;syntaxhighlighter&quot;&gt;
&lt;![CDATA[

void sum(int a[],int n,int S)
{
    mergesort(a,0,n-1);
   
    int i=0,j=n-1;

    while(i&lt;j)
    {
        
        if(a[i]+a[j]&gt;S)
            j--;
        else if(a[i]+a[j]&lt;S)
            i++;
        else
        {
            printf(&quot;YES&quot;);
            break;
        }
    }
    if(i==j)
        printf(&quot;NO&quot;);
}
 
 
int bisearch(int a[],int start,int end,int key)
{
    int middle=0;
    while(start&lt;end)
    {
        middle=(start+end)/2;
        if(key&lt;a[middle])
            end=middle-1;
        else if(key&gt;a[middle])
            start=middle+1;
        else
            return 1;
    }
    return 0;
}
 
void sumbsearch(int a[],int n,int S)
{
    mergesort(a,0,n-1);
    int i=0,tmp=0;
    for(i=0;i&lt;n-1;i++)
    {
        tmp=S-a[i];
        if(bisearch(a,i+1,n-1,tmp)==1)
        {
            printf(&quot;YES&quot;);
            break;
        }
    }
    if(i==n-1)
        printf(&quot;NO&quot;);
}
]]&gt;
&lt;/script&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prpds.blogspot.com/feeds/1276772091307435871/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prpds.blogspot.com/2011/08/clrs-exercises-23-7.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7748170484639853544/posts/default/1276772091307435871'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7748170484639853544/posts/default/1276772091307435871'/><link rel='alternate' type='text/html' href='http://prpds.blogspot.com/2011/08/clrs-exercises-23-7.html' title='CLRS Exercises 2.3-7'/><author><name>anantha</name><uri>http://www.blogger.com/profile/03120527085848815308</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7748170484639853544.post-5355936408415064796</id><published>2011-08-03T18:03:00.001+05:30</published><updated>2011-08-09T00:08:29.250+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="CLRS"/><title type='text'>CLRS - Exercises 2.3-6</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:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Observe that the while loop of lines 5 - 7 of the INSERTION-SORT procedure in Section 2.1&lt;br /&gt;
uses a linear search to scan (backward) through the sorted subarray A[1   j - 1]. Can we use a&lt;br /&gt;
binary search (see Exercise 2.3-5) instead to improve the overall worst-case running time of&lt;br /&gt;
insertion sort to Θ(n lg n)?&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Solution:&lt;/b&gt;&lt;br /&gt;
No, for finding correct place lets say O(log n) then for moving all the elements will take O(n) time. So, its running time will be O(n^2)&lt;br /&gt;
&lt;b&gt;Complexity:&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;Code:&lt;/b&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prpds.blogspot.com/feeds/5355936408415064796/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prpds.blogspot.com/2011/08/clsr-exercises-23-6.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7748170484639853544/posts/default/5355936408415064796'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7748170484639853544/posts/default/5355936408415064796'/><link rel='alternate' type='text/html' href='http://prpds.blogspot.com/2011/08/clsr-exercises-23-6.html' title='CLRS - Exercises 2.3-6'/><author><name>anantha</name><uri>http://www.blogger.com/profile/03120527085848815308</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7748170484639853544.post-327157976821366428</id><published>2011-08-03T17:49:00.001+05:30</published><updated>2011-08-09T00:08:57.821+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Algorithm"/><category scheme="http://www.blogger.com/atom/ns#" term="binary search"/><category scheme="http://www.blogger.com/atom/ns#" term="CLRS"/><title type='text'>CLRS Exercises 2.3-5 Binary search</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:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Binary search is an algorithm that repeats this&lt;br /&gt;
procedure, halving the size of the remaining portion of the sequence each time. Write&lt;br /&gt;
pseudocode, either iterative or recursive, for binary search. Argue that the worst-case running&lt;br /&gt;
time of binary search is Θ(lg n).&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Solution:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;b&gt;If there are 2^i elements then the height of the tree will be i+1&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;&lt;b&gt;At each level we will just do&amp;nbsp;comparison [key with middle element] which is a constant time O(1)&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;&lt;b&gt;So for any n, height of the tree will be log n + 1&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Thus, worst case running complexity will be&amp;nbsp;Θ(lg n).&lt;/b&gt;&lt;/li&gt;
&lt;/ol&gt;&lt;br /&gt;
&lt;b&gt;Complexity:&amp;nbsp;Θ(lg n).&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;Code:&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;
&lt;/b&gt;&lt;br /&gt;
&lt;script class=&quot;brush:python&quot; type=&quot;syntaxhighlighter&quot;&gt;
&lt;![CDATA[
def bsearch(A,start,end,key):
    if start&gt;end:
        return -1
    i=(start+end)/2
    if A[i]==key:
        return i
    if key&lt;A[i]:
        return bsearch(A,start,i-1,key)
    return bsearch(A,i+1,end,key)

def binarysearch(A,key):
    return bsearch(A,0,len(A)-1,key)

A=[1,10,34,56,90,100,325,625,900,1000]
key=10001
print binarysearch(A,key)
]]&gt;
&lt;/script&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prpds.blogspot.com/feeds/327157976821366428/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prpds.blogspot.com/2011/08/exercises-23-5-binary-search.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7748170484639853544/posts/default/327157976821366428'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7748170484639853544/posts/default/327157976821366428'/><link rel='alternate' type='text/html' href='http://prpds.blogspot.com/2011/08/exercises-23-5-binary-search.html' title='CLRS Exercises 2.3-5 Binary search'/><author><name>anantha</name><uri>http://www.blogger.com/profile/03120527085848815308</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7748170484639853544.post-3112100518174822725</id><published>2011-08-03T17:16:00.003+05:30</published><updated>2011-08-09T00:09:17.553+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Algorithm"/><category scheme="http://www.blogger.com/atom/ns#" term="CLRS"/><category scheme="http://www.blogger.com/atom/ns#" term="insertion sort"/><category scheme="http://www.blogger.com/atom/ns#" term="Recursion"/><title type='text'>CLRS Exercises 2.3-4 Recursive insertion sort</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:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Insertion sort can be expressed as a recursive procedure as follows. In order to sort A[1   n],&lt;br /&gt;
we recursively sort A[1   n -1] and then insert A[n] into the sorted array A[1   n - 1]. Write a&lt;br /&gt;
recurrence for the running time of this recursive version of insertion sort.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Solution:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;b&gt;Initially the array size will be n&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;&lt;b&gt;call insertionsort function until the size becomes 1&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;&lt;b&gt;then start the normal insertion approach as shown in code&lt;/b&gt;&lt;/li&gt;
&lt;/ol&gt;T(n) = T(n-1) + 1 + &amp;nbsp;O(n) = O(n^2)&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Complexity: O(n^2) TC&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;Code:&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;
&lt;/b&gt;&lt;br /&gt;
&lt;script class=&quot;brush:python&quot; type=&quot;syntaxhighlighter&quot;&gt;
&lt;![CDATA[
def insertionsort(A,n):
    if n&lt;=1:
        return;
    insertionsort(A,n-1)
    j=n-1;
    i=j-1;
    key=A[j];
    while i&gt;=0 and A[i]&gt;key:
        A[i+1]=A[i];
        i=i-1
    A[i+1]=key;
    return;

A=[10,340,2,445,34,12,54]
print A
insertionsort(A,len(A))
print A
]]&gt;
&lt;/script&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prpds.blogspot.com/feeds/3112100518174822725/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prpds.blogspot.com/2011/08/exercises-23-4-recursive-insertion-sort.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7748170484639853544/posts/default/3112100518174822725'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7748170484639853544/posts/default/3112100518174822725'/><link rel='alternate' type='text/html' href='http://prpds.blogspot.com/2011/08/exercises-23-4-recursive-insertion-sort.html' title='CLRS Exercises 2.3-4 Recursive insertion sort'/><author><name>anantha</name><uri>http://www.blogger.com/profile/03120527085848815308</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7748170484639853544.post-8883360445918766074</id><published>2011-08-01T18:23:00.000+05:30</published><updated>2011-08-01T18:23:54.299+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="add two list"/><category scheme="http://www.blogger.com/atom/ns#" term="Algorithm"/><category scheme="http://www.blogger.com/atom/ns#" term="Linked List"/><title type='text'></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:&lt;/b&gt;&lt;br /&gt;
You have two numbers represented by a linked list, where each node contains a single digit&amp;nbsp;&amp;nbsp; The digits are stored in reverse order, such that the 1’s digit is at the head of &lt;br /&gt;
the list&amp;nbsp;&amp;nbsp; Write a function that adds the two numbers and returns the sum as a linked &lt;br /&gt;
list &lt;br /&gt;
EXAMPLE &lt;br /&gt;
Input: (3 -&amp;gt; 1 -&amp;gt; 5) + (5 -&amp;gt; 9 -&amp;gt; 2)&lt;br /&gt;
Output: 8 -&amp;gt; 0 -&amp;gt; 8&lt;b&gt; &lt;/b&gt;&lt;br /&gt;
&lt;b&gt;Solution:&lt;/b&gt;&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;b&gt;Have pointer on each head of the two list&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;&lt;b&gt; Add the data of two nodes along with carry.&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;&lt;b&gt;create new result node and put the value, SUM % 10&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;&lt;b&gt;assign SUM / 10 to carry&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Repeat until any one list exhausts.&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Then add the carry to the node, repeat till end of list&lt;/b&gt;&lt;/li&gt;
&lt;/ol&gt;&lt;b&gt;Complexity: O(n) where n&amp;gt;=m, m and n are length of two lists&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;Code:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;script class=&quot;brush:c&quot; type=&quot;syntaxhighlighter&quot;&gt;
&lt;![CDATA[
List *addtwoList(List *head1,List *head2)
{
    List *newhead=NULL,*lastnode=NULL,*head=NULL;
    int carry=0,S=0;
    
    while(head1!=NULL &amp;&amp; head2!=NULL)
    {
        S=head1-&gt;data+head2-&gt;data+carry;
        if(newhead==NULL)
        {
            newhead=createnode(S%10);
            lastnode=newhead;
        }
        else
        {
            lastnode-&gt;link=createnode(S%10);
            lastnode=lastnode-&gt;link;
        }
        carry=S/10;
        head1=head1-&gt;link;
        head2=head2-&gt;link;
    }
    head=NULL;
    if(head1!=NULL)
        head=head1;
    if(head2!=NULL)
        head=head2;
    while(head!=NULL)
    {
        S=head-&gt;data+carry;
        lastnode-&gt;link=createnode(S%10);
        lastnode=lastnode-&gt;link;
        carry=S/10;
        head=head-&gt;link;
    }
    if(carry!=0)
        lastnode-&gt;link=createnode(carry);
    return newhead;
}
]]&gt;
&lt;/script&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prpds.blogspot.com/feeds/8883360445918766074/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prpds.blogspot.com/2011/08/problem-you-have-two-numbers.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7748170484639853544/posts/default/8883360445918766074'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7748170484639853544/posts/default/8883360445918766074'/><link rel='alternate' type='text/html' href='http://prpds.blogspot.com/2011/08/problem-you-have-two-numbers.html' title=''/><author><name>anantha</name><uri>http://www.blogger.com/profile/03120527085848815308</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7748170484639853544.post-9197264885904886840</id><published>2011-08-01T16:42:00.000+05:30</published><updated>2011-08-01T16:42:03.786+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Algorithm"/><category scheme="http://www.blogger.com/atom/ns#" term="Linked List"/><category scheme="http://www.blogger.com/atom/ns#" term="remove duplicates"/><title type='text'></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:&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &lt;/b&gt;Write code to remove duplicates from an unsorted linked list&lt;br /&gt;
&lt;b&gt;Solution:&lt;/b&gt;&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;b&gt;Have two pointers ptr1 and ptr2.&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;&lt;b&gt;For each node ptr1 visits check from head [head&amp;nbsp; to node before ptr1] whether that data is already present.&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;&lt;b&gt;If so, remove the ptr1 node.&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;&lt;b&gt;return the new list without any duplicates&lt;/b&gt;&lt;/li&gt;
&lt;/ol&gt;&lt;b&gt;Complexity: O(n^2)&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;Code:&lt;/b&gt;&lt;br /&gt;
&lt;script class=&quot;brush:c&quot; type=&quot;syntaxhighlighter&quot;&gt;
&lt;![CDATA[
void removedup(List *head)
{
    if(head==NULL)
        return;
    List *ptr1=NULL,*ptr2=NULL,*prev=NULL,*dupnode=NULL;
    
    ptr2=head-&gt;link;
    prev=head;
    
    while(ptr2!=NULL)
    {
        ptr1=head;
        while(ptr1!=ptr2)
        {
            if(ptr1-&gt;data==ptr2-&gt;data)
            {
                dupnode=ptr2;
                prev-&gt;link=ptr2-&gt;link;
                ptr2=prev-&gt;link;
                free(dupnode);
                break;
            }
            ptr1=ptr1-&gt;link;
        }
        if(ptr1==ptr2)
        {
            prev=prev-&gt;link;
            ptr2=ptr2-&gt;link;
        }
        
    }
    
}]]&gt;
&lt;/script&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prpds.blogspot.com/feeds/9197264885904886840/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prpds.blogspot.com/2011/08/problem-write-code-to-remove-duplicates.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7748170484639853544/posts/default/9197264885904886840'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7748170484639853544/posts/default/9197264885904886840'/><link rel='alternate' type='text/html' href='http://prpds.blogspot.com/2011/08/problem-write-code-to-remove-duplicates.html' title=''/><author><name>anantha</name><uri>http://www.blogger.com/profile/03120527085848815308</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7748170484639853544.post-2387844252103746831</id><published>2011-08-01T15:02:00.000+05:30</published><updated>2011-08-01T15:02:37.337+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Algorithm"/><category scheme="http://www.blogger.com/atom/ns#" term="subtree"/><category scheme="http://www.blogger.com/atom/ns#" term="tree"/><title type='text'></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:&lt;/b&gt;&lt;br /&gt;
&amp;nbsp;You have two very large binary trees: T1, with millions of nodes, and T2, with hundreds of nodes&amp;nbsp;&amp;nbsp; Create an algorithm to decide if T2 is a subtree of T1&lt;br /&gt;
&lt;b&gt;Solution:&lt;/b&gt;&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;b&gt;Traverse the Tree T1.&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;&lt;b&gt;For each node, call a method to do the following (Step 3),&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;&lt;b&gt;traverse both the Tree T1 and T2, until tree T2 exhausts or until data of T1 and T2 does not match.&amp;nbsp;&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Return true if T2 is a subtree of T1 else return false.&lt;/b&gt;&lt;/li&gt;
&lt;/ol&gt;&lt;b&gt;&amp;nbsp;&lt;/b&gt;&lt;div&gt;&lt;b&gt;&amp;nbsp;&lt;/b&gt;&lt;b&gt;Complexity: O(m*n) in the worst case&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;Code:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;script class=&quot;brush:c&quot; type=&quot;syntaxhighlighter&quot;&gt;
&lt;![CDATA[
int check(Tnode *root1,Tnode *root2)
{
    if(root2==NULL)
        return 1;
    if(root1==NULL &amp;&amp; root2!=NULL)
        return 0;
    if(root1==NULL &amp;&amp; root2==NULL)
        return 1;
    if(root1-&gt;data!=root2-&gt;data)
        return 0;
    int l=0,r=0;
    l=check(root1-&gt;left,root2-&gt;left);
    if(l!=0)
        r=check(root1-&gt;right,root2-&gt;right);
    return l&amp;&amp;r;
}
 
int traverseT1(Tnode *root1,Tnode *root2)
{
    if(root1==NULL &amp;&amp; root2==NULL)
        return 1;
    if(root1==NULL)
        return 0;
    int ans=check(root1,root2);
    int l=0,r=0;
    if(ans==0)
    {
        l=traverseT1(root1-&gt;left,root2);
        r=traverseT1(root1-&gt;right,root2);
    }
    return ans||l||r;
    
}
]]&gt;
&lt;/script&gt;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prpds.blogspot.com/feeds/2387844252103746831/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prpds.blogspot.com/2011/08/problem-have-two-very-large-binary.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7748170484639853544/posts/default/2387844252103746831'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7748170484639853544/posts/default/2387844252103746831'/><link rel='alternate' type='text/html' href='http://prpds.blogspot.com/2011/08/problem-have-two-very-large-binary.html' title=''/><author><name>anantha</name><uri>http://www.blogger.com/profile/03120527085848815308</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7748170484639853544.post-1699245994711160948</id><published>2011-07-31T21:03:00.001+05:30</published><updated>2011-08-01T15:02:04.430+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Algorithm"/><category scheme="http://www.blogger.com/atom/ns#" term="find merge node"/><category scheme="http://www.blogger.com/atom/ns#" term="Linked List"/><category scheme="http://www.blogger.com/atom/ns#" term="Merge two linked list"/><title type='text'></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:&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 15px; font-weight: normal; line-height: 20px;&quot;&gt;There are 2 link lists merging at some node. Find the node, where these 2 lists merge.&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh8bWmhwCM7dz3CbDfCH0cLMYZGS1MO5xlxSzdvZzad-vkanLlNX5E_q8eR6zZY54423Ij6pok1XJbBpLSDIhZ8FGG6IvmV6CynN-Oub0FlMcdFcEVp8JH1MJSYhiQWO-doEe7XuYFU79c/s320/merging+lists.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh8bWmhwCM7dz3CbDfCH0cLMYZGS1MO5xlxSzdvZzad-vkanLlNX5E_q8eR6zZY54423Ij6pok1XJbBpLSDIhZ8FGG6IvmV6CynN-Oub0FlMcdFcEVp8JH1MJSYhiQWO-doEe7XuYFU79c/s320/merging+lists.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 15px; font-weight: normal; line-height: 20px;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;Solution:&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;b&gt;Point both the heads with temp1 and temp2 pointers.&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Move both pointers one step at a time.&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Repeat step 2 until temp1 or temp2 reaches the last node.&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Make a pointer P1 to point the head of longest list and pointer P2 to point the head of another list.&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: normal;&quot;&gt;&lt;b&gt;Move the temp pointer of longest list and another pointer from its list head one position at a time until temp reaches the last node.&lt;/b&gt;&lt;/span&gt;&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: normal;&quot;&gt;&lt;b&gt;Now we have two pointers with equal numbers of nodes.&lt;/b&gt;&lt;/span&gt;&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: normal;&quot;&gt;&lt;b&gt;move two pointers one at a time until they both meet.&lt;/b&gt;&lt;/span&gt;&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: normal;&quot;&gt;&lt;b&gt;return the merge node.&lt;/b&gt;&lt;/span&gt;&lt;/b&gt;&lt;/li&gt;
&lt;/ol&gt;&lt;br /&gt;
&lt;b&gt;Complexity: O(m+n)&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;Code:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;script class=&quot;brush:c&quot; type=&quot;syntaxhighlighter&quot;&gt;
&lt;![CDATA[
List *findmergepoint(List *head1,List *head2)
{
    if(head1==NULL || head2==NULL)
        return NULL;
    List *temp1=head1;
    List *temp2=head2;
    List *p1=NULL,*p2=NULL,*temp=NULL;
    while(temp1-&gt;link!=NULL &amp;&amp; temp2-&gt;link!=NULL)
    {
        temp1=temp1-&gt;link;
        temp2=temp2-&gt;link;
    }
    if(temp1-&gt;link==NULL)
    {
        p1=head2;
        p2=head1;
        temp=temp2;
    }
    else if(temp2-&gt;link==NULL)
    {
        p1=head1;
        p2=head2;
        temp=temp1;
    }
    while(temp-&gt;link!=NULL)
    {
        p1=p1-&gt;link;
        temp=temp-&gt;link;
    }
    while((p1!=NULL &amp;&amp; p2!=NULL)&amp;&amp;p1!=p2)
    {
        p1=p1-&gt;link;
        p2=p2-&gt;link;
    }
    return p1;
}
]]&gt;
&lt;/script&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prpds.blogspot.com/feeds/1699245994711160948/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prpds.blogspot.com/2011/07/problem-there-are-2-link-lists-merging.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7748170484639853544/posts/default/1699245994711160948'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7748170484639853544/posts/default/1699245994711160948'/><link rel='alternate' type='text/html' href='http://prpds.blogspot.com/2011/07/problem-there-are-2-link-lists-merging.html' title=''/><author><name>anantha</name><uri>http://www.blogger.com/profile/03120527085848815308</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh8bWmhwCM7dz3CbDfCH0cLMYZGS1MO5xlxSzdvZzad-vkanLlNX5E_q8eR6zZY54423Ij6pok1XJbBpLSDIhZ8FGG6IvmV6CynN-Oub0FlMcdFcEVp8JH1MJSYhiQWO-doEe7XuYFU79c/s72-c/merging+lists.png" height="72" width="72"/><thr:total>0</thr:total></entry></feed>