<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0"><channel><atom:id>tag:blogger.com,1999:blog-6912797365202061321</atom:id><lastBuildDate>Mon, 28 Nov 2011 00:11:23 +0000</lastBuildDate><category>C++</category><category>tricks</category><category>techniques</category><category>patterns</category><category>concepts</category><category>C</category><category>.Net</category><category>C SC545</category><category>Memory</category><category>algorithms</category><category>Optimizations</category><category>general</category><category>ToDo</category><title>Engineering Code</title><description>My space where I write about how to improve programming skills and other skills related to programming.</description><link>http://letuscode.blogspot.com/</link><managingEditor>noreply@blogger.com (Amit Wadhwa)</managingEditor><generator>Blogger</generator><openSearch:totalResults>29</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/CodeLearner" /><feedburner:info uri="codelearner" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6912797365202061321.post-1299076557834059150</guid><pubDate>Mon, 30 Nov 2009 02:37:00 +0000</pubDate><atom:updated>2009-11-29T18:37:45.498-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">general</category><category domain="http://www.blogger.com/atom/ns#">concepts</category><category domain="http://www.blogger.com/atom/ns#">.Net</category><title>Introduction to .Net Framework</title><description>&lt;p align="left"&gt;In his latest blog post about &lt;a href="http://skwpspace.com/2009/09/29/five-rules-for-writing-good-code/" target="_blank"&gt;five rules for writing good code&lt;/a&gt;, Yan Pritzker states Rule 5 as &lt;strong&gt;Really learn the language and the framework. &lt;/strong&gt;He says &lt;strong&gt;“No need to be a walking encyclopedia, but remember to occasionally open up that encyclopedia and read through it so that you know at least what’s out there.” &lt;/strong&gt;So, I decided to revisit some .NET fundamentals&amp;#160; and jot down my notes here. &lt;/p&gt;  &lt;p align="left"&gt;The following is a short introduction to .Net framework and its components.&lt;/p&gt;  &lt;p align="left"&gt;The .Net architecture consists of the Common Language Runtime (CLR), .Net Framework Class library, and language support components. &lt;/p&gt;  &lt;p align="left"&gt;&lt;a href="http://lh5.ggpht.com/_zaEn_RqZHY4/SxMv6kqX9CI/AAAAAAAAB_o/sMnZpR84Nfo/s1600-h/Framework%5B3%5D.jpg"&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title=".Net Framework Components" border="0" alt=".Net Framework" src="http://lh5.ggpht.com/_zaEn_RqZHY4/SxMv7BtpEPI/AAAAAAAAB_s/Ag-j1GUlGFM/Framework_thumb%5B1%5D.jpg?imgmax=800" width="240" height="244" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;The Common Language Runtime (a.k.a. CLR) simplifies provides a robust and secure execution environment for .NET applications. It is also referred to as a managed environment, in which common services, such as garbage collection and security, are automatically provided.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/_zaEn_RqZHY4/SxMv7jCFTdI/AAAAAAAAB_w/dWhVgKuFxWo/s1600-h/CLR%5B2%5D.jpg"&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="Common Language Runtime Components" border="0" alt="Common Language Runtime Components" src="http://lh5.ggpht.com/_zaEn_RqZHY4/SxMv8FphvII/AAAAAAAAB_0/IyX05qkbZ64/CLR_thumb.jpg?imgmax=800" width="244" height="171" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;A short description of some of the components is below:&lt;/p&gt; &lt;center&gt;   &lt;table border="1" cellspacing="0" cellpadding="2" width="622"&gt;&lt;tbody&gt;       &lt;tr&gt;         &lt;td valign="top" width="200"&gt;Component&lt;/td&gt;          &lt;td valign="top" width="420"&gt;Description&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="200"&gt;Class Loader&lt;/td&gt;          &lt;td valign="top" width="420"&gt;Manages metadata, loading and layout of classes&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="200"&gt;MSIL to Native Compiler&lt;/td&gt;          &lt;td valign="top" width="420"&gt;JIT compiler for converting MSIL to Native code&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="200"&gt;Code Manager&lt;/td&gt;          &lt;td valign="top" width="420"&gt;Manages code execution&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="200"&gt;Garbage collection&lt;/td&gt;          &lt;td valign="top" width="420"&gt;--&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="200"&gt;Security Engine&lt;/td&gt;          &lt;td valign="top" width="420"&gt;Provides security based on user identity and code origin&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="200"&gt;Debugger &lt;/td&gt;          &lt;td valign="top" width="420"&gt;--&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="200"&gt;Type Checker&lt;/td&gt;          &lt;td valign="top" width="420"&gt;--&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="200"&gt;Exception Manager&lt;/td&gt;          &lt;td valign="top" width="420"&gt;Provides structured Exception handling, integrates with Windows Structured Error Handling (SEH) &lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="200"&gt;Thread Support &lt;/td&gt;          &lt;td valign="top" width="420"&gt;--&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="200"&gt;COM marshaler&lt;/td&gt;          &lt;td valign="top" width="420"&gt;--&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="200"&gt;.NET Class Library support&lt;/td&gt;          &lt;td valign="top" width="420"&gt;Integrates code with the runtime that supports the .NET Framework class library&lt;/td&gt;       &lt;/tr&gt;     &lt;/tbody&gt;&lt;/table&gt; &lt;/center&gt;  &lt;p&gt;The .NET Framework class library exposes features of the runtime and provides other high-level services. &lt;/p&gt;  &lt;p&gt;ADO.NET, the next generation of ADO technology provides improved support for disconnected programming. It also provides rich XML support in the System.Xml namespace.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh6.ggpht.com/_zaEn_RqZHY4/SxMv8gMwd3I/AAAAAAAAB_4/t3rW4bn-rlk/s1600-h/ADONET%5B2%5D.jpg"&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="ADO.NET" border="0" alt="ADO.NET" src="http://lh4.ggpht.com/_zaEn_RqZHY4/SxMv9FX47GI/AAAAAAAAB_8/2eats1BXnjs/ADONET_thumb.jpg?imgmax=800" width="244" height="171" /&gt;&lt;/a&gt;ASP.NET is a programming framework built on the common language runtime that can be used on a server to build powerful Web applications&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_zaEn_RqZHY4/SxMv9lCaa6I/AAAAAAAACAA/51zeYQH9c74/s1600-h/ASPNET%5B2%5D.jpg"&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="ASP.NET" border="0" alt="ASP.NET" src="http://lh6.ggpht.com/_zaEn_RqZHY4/SxMv-H1SRiI/AAAAAAAACAE/bbO22ZLaVTY/ASPNET_thumb.jpg?imgmax=800" width="244" height="238" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;References:&lt;/p&gt;  &lt;p&gt;1.&amp;#160; &lt;a title="http://skwpspace.com/2009/09/29/five-rules-for-writing-good-code/" href="http://skwpspace.com/2009/09/29/five-rules-for-writing-good-code/"&gt;http://skwpspace.com/2009/09/29/five-rules-for-writing-good-code/&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;2. &lt;a title="http://msdn.microsoft.com/en-us/netframework/default.aspx" href="http://msdn.microsoft.com/en-us/netframework/default.aspx"&gt;http://msdn.microsoft.com/en-us/netframework/default.aspx&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;3. &lt;a title="http://msdn.microsoft.com/en-us/library/aa139615.aspx" href="http://msdn.microsoft.com/en-us/library/aa139615.aspx"&gt;http://msdn.microsoft.com/en-us/library/aa139615.aspx&lt;/a&gt;&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6912797365202061321-1299076557834059150?l=letuscode.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/CTU5nOlYRSKwZ6nPXs2RKMVoQPU/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/CTU5nOlYRSKwZ6nPXs2RKMVoQPU/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/CTU5nOlYRSKwZ6nPXs2RKMVoQPU/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/CTU5nOlYRSKwZ6nPXs2RKMVoQPU/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeLearner/~4/x5iL5CHq1H4" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/CodeLearner/~3/x5iL5CHq1H4/introduction-to-net-framework.html</link><author>noreply@blogger.com (Amit Wadhwa)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh5.ggpht.com/_zaEn_RqZHY4/SxMv7BtpEPI/AAAAAAAAB_s/Ag-j1GUlGFM/s72-c/Framework_thumb%5B1%5D.jpg?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://letuscode.blogspot.com/2009/11/introduction-to-net-framework.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6912797365202061321.post-8368554481398311562</guid><pubDate>Wed, 30 Sep 2009 04:55:00 +0000</pubDate><atom:updated>2009-11-29T18:40:09.138-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">tricks</category><category domain="http://www.blogger.com/atom/ns#">general</category><category domain="http://www.blogger.com/atom/ns#">concepts</category><title>Why every other playoff system sucks</title><description>&lt;p align="justify"&gt;Many times we’ve seen a underdog team defeating a top-ranked team in the 1st round of a major tournament. Most people rejoice (except the fans of the losing team) since everybody loves a fairy tale&amp;#160; victory, The organizer’s are happy since it creates a lot of buzz and everybody’s happy that the sports is still not dead since anything can happen.&lt;/p&gt;&lt;p align="justify"&gt;Let’s take a deeper look. Team A wins 63 games out of 80 games in the regular season plays Team B which won around 40 games. Team A loses the match and bows out of the tournament. Everybody is surprised for a moment, Team B probably loses in the next round since they again play a better team and are already happy with the results they have in hand [there was a reason it only won 40 games out of 80].&lt;/p&gt;&lt;p align="justify"&gt;The above is a common format used across many competitions in the world (NBA, Cricket Tournaments, NCAA Tournament, Champions league). But, I think such upsets does more harm to the game than benefits it. Since the team who was definitely one of the better teams in the tournament is out due to having a bad day or couple of major players being out/suspended for the game.&lt;/p&gt;&lt;p&gt;&lt;a href="http://lh4.ggpht.com/_zaEn_RqZHY4/SsLku7oO4KI/AAAAAAAAB-c/QMng_dNp9CQ/s1600-h/ncaa_mens3.jpg"&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="ncaa_mens" border="0" alt="ncaa_mens" src="http://lh6.ggpht.com/_zaEn_RqZHY4/SsLkv9dMrUI/AAAAAAAAB-g/mChwzrkf8qY/ncaa_mens_thumb.jpg?imgmax=800" width="244" height="190" /&gt;&lt;/a&gt; &lt;/p&gt;&lt;p align="justify"&gt;Some organizers e.g. the NBA, try to avoid the above scenario by either keeping N (=3,5,7) no. of matches or sometimes&amp;#160; giving the top ranked team a home advantage or sometimes both. Playing multiple matches definitely lowers the odds of the better team losing, however, this usually leads to some boring matches in the initial part of the tournament.&lt;/p&gt;&lt;p&gt;&lt;a href="http://lh5.ggpht.com/_zaEn_RqZHY4/SsLkwbVlsPI/AAAAAAAAB-k/ZXhgReTg3W8/s1600-h/Nba_format4.png"&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="Nba_format" border="0" alt="Nba_format" src="http://lh3.ggpht.com/_zaEn_RqZHY4/SsLkwr3y27I/AAAAAAAAB-o/ngDYDSHe_HA/Nba_format_thumb1.png?imgmax=800" width="244" height="179" /&gt;&lt;/a&gt; Here comes the format used by the Australian Football league, which looks like this:&lt;/p&gt;&lt;p&gt;&lt;a href="http://lh6.ggpht.com/_zaEn_RqZHY4/SsLkxLQvvoI/AAAAAAAAB-s/D0dkJUruZ40/s1600-h/800px-AFL_finals_v2%5B2%5D.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px" title="800px-AFL_finals_v2" border="0" alt="800px-AFL_finals_v2" src="http://lh6.ggpht.com/_zaEn_RqZHY4/SsLkxmZd9hI/AAAAAAAAB-w/IDpAAAl7q2E/800px-AFL_finals_v2_thumb.png?imgmax=800" width="244" height="120" /&gt;&lt;/a&gt; Clearly, this system has numerous advantages than the knockout system usually followed. To quote Wikipedia “Under this finals system, the final eight teams are broken up into four groups of two. Each group of two earns one extra benefit over the teams beneath it. These benefits are home ground finals and the &lt;i&gt;double-chance&lt;/i&gt;, whereby a first-week loss will not eliminate the team from the finals.”&lt;/p&gt;&lt;p&gt;I think atleast some tournaments should explore this format like the NCAA basketball tournament and the Cricket world cup which have the worst structure since there is only a single match between the teams and there’s no clear home-court advantage.&lt;/p&gt;&lt;p&gt;References:&lt;/p&gt;&lt;p&gt;&lt;a title="http://en.wikipedia.org/wiki/AFL_finals_system" href="http://en.wikipedia.org/wiki/AFL_finals_system"&gt;http://en.wikipedia.org/wiki/AFL_finals_system&lt;/a&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6912797365202061321-8368554481398311562?l=letuscode.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/ILK80vOm_TEmktV26Pw1gJRPGyM/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/ILK80vOm_TEmktV26Pw1gJRPGyM/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/ILK80vOm_TEmktV26Pw1gJRPGyM/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/ILK80vOm_TEmktV26Pw1gJRPGyM/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeLearner/~4/hPq44astU4s" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/CodeLearner/~3/hPq44astU4s/why-every-other-playoff-system-sucks.html</link><author>noreply@blogger.com (Amit Wadhwa)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh6.ggpht.com/_zaEn_RqZHY4/SsLkv9dMrUI/AAAAAAAAB-g/mChwzrkf8qY/s72-c/ncaa_mens_thumb.jpg?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://letuscode.blogspot.com/2009/09/why-every-other-playoff-system-sucks.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6912797365202061321.post-6954768011738879343</guid><pubDate>Sat, 12 Sep 2009 22:24:00 +0000</pubDate><atom:updated>2009-09-12T15:40:20.714-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">patterns</category><title>The Decorator Pattern</title><description>Decorator pattern is a kind of structural design pattern which provides a way to attach new state and behavior to an object dynamically. The original object is left untouched which makes the pattern useful for changing systems. Decorators both inherit the original class and contain an instant of it. &lt;br /&gt;
The Decorator pattern is generally used when a object is needed with some extra functionality than an existing class but the existing class is not available for subclassing or one doesn't want to subclass for some reason (e.g. to avoid class explosion). &lt;br /&gt;
The following example illustrates the pattern: &lt;br /&gt;
&lt;pre class="c-sharp" name="code"&gt;using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DPPractice
{
    class DecoratorPattern
    {
        interface IWindow
        {
            string getDescription();
            void draw();
        }
        public class SimpleWindow : IWindow
        {
            public void draw()
            {
                //draw Simple Window
            }

            public string getDescription()
            {
                return "simple window";
            }

        }
        class VerticalScrollBarDecorator : IWindow
        {

            private IWindow decoratedWindow;

            public VerticalScrollBarDecorator(IWindow decoratedWindow)
            {
                this.decoratedWindow = decoratedWindow;
            }


            public void draw()
            {
                drawVerticalScrollBar();
                decoratedWindow.draw();
            }

            private void drawVerticalScrollBar()
            {
                // draw the vertical scrollbar
            }

            public String getDescription()
            {
                return decoratedWindow.getDescription() + ", including vertical scrollbars";
            }
        }

        static void Main(String[] args)
        {
            // create a decorated Window with horizontal and vertical scrollbars
            IWindow window = new VerticalScrollBarDecorator(new SimpleWindow());

            // print the Window's description
            Console.WriteLine(window.getDescription());
        }


    }
}

&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
As can be seen from the example the Decorator pattern is heavily utilized in displaying windows etc. adding and excluding scroll bars easily while rendering. &lt;br /&gt;
&lt;br /&gt;
Other uses of the Decorator Pattern: &lt;br /&gt;
&lt;br /&gt;
1. Decorators are used in I/O API's of C#. &lt;br /&gt;
System.IO.Stream is decorated by &lt;br /&gt;
System.IO.BufferedStream &lt;br /&gt;
System.IO.FileStream Etc. &lt;br /&gt;
&lt;br /&gt;
2. As illustrated by the example the Decorator pattern is heavily utilized in rendering windows/display objects&lt;br /&gt;
&lt;br /&gt;
References:&lt;br /&gt;
&lt;a href="http://en.wikipedia.org/wiki/Decorator_pattern"&gt;http://en.wikipedia.org/wiki/Decorator_pattern&lt;/a&gt;&lt;br /&gt;
&lt;a href="http://www.amazon.com/gp/product/0201633612?ie=UTF8&amp;amp;tag=engincode-20&amp;amp;linkCode=as2&amp;amp;camp=1789&amp;amp;creative=9325&amp;amp;creativeASIN=0201633612"&gt;Design Patterns: Elements of Reusable Object-Oriented Software&lt;/a&gt;&lt;img alt="" border="0" height="1" src="http://www.assoc-amazon.com/e/ir?t=engincode-20&amp;amp;l=as2&amp;amp;o=1&amp;amp;a=0201633612" style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; margin: 0px;" width="1" /&gt;&lt;br /&gt;
&lt;a href="http://www.amazon.com/gp/product/059652773X?ie=UTF8&amp;amp;tag=engincode-20&amp;amp;linkCode=as2&amp;amp;camp=1789&amp;amp;creative=9325&amp;amp;creativeASIN=059652773X"&gt;C# 3.0 Design Patterns&lt;/a&gt;&lt;img alt="" border="0" height="1" src="http://www.assoc-amazon.com/e/ir?t=engincode-20&amp;amp;l=as2&amp;amp;o=1&amp;amp;a=059652773X" style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; margin: 0px;" width="1" /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6912797365202061321-6954768011738879343?l=letuscode.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/ziIOBgOi9Yq4HExGSX3x-x8nDFU/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/ziIOBgOi9Yq4HExGSX3x-x8nDFU/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/ziIOBgOi9Yq4HExGSX3x-x8nDFU/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/ziIOBgOi9Yq4HExGSX3x-x8nDFU/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeLearner/~4/l4ivs4p8lxE" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/CodeLearner/~3/l4ivs4p8lxE/decorator-pattern.html</link><author>noreply@blogger.com (Amit Wadhwa)</author><thr:total>0</thr:total><feedburner:origLink>http://letuscode.blogspot.com/2009/09/decorator-pattern.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6912797365202061321.post-586845499635744579</guid><pubDate>Tue, 08 Sep 2009 03:29:00 +0000</pubDate><atom:updated>2009-10-03T23:22:05.635-07:00</atom:updated><title>Time Management</title><description>&lt;p&gt;This weekend I took some time out to watch the Time Management talk by Randy Pausch.Randy Pausch was an Computer Science professor at CMU. He died of cancer in July, 2008. He was a great speaker and some of his lectures has become very famous in the last few years. The most famous one being “&lt;i&gt;&lt;a href="http://en.wikipedia.org/wiki/Really_Achieving_Your_Childhood_Dreams"&gt;The Last Lecture: Really Achieving Your Childhood Dreams&lt;/a&gt;&lt;/i&gt;&amp;quot; which is one of the most inspiring talks I have ever heard. &lt;/p&gt;  &lt;p&gt;The talk on Time management, however, is a more pragmatic talk and talks about techniques to manage time better. Again, the underlying principle in Randy’s talk is to maximize life and fun. &lt;/p&gt;  &lt;p&gt;Here is the talk followed with my notes. The slides are available &lt;a href="http://www.cs.virginia.edu/~robins/Randy/RandyPauschTimeManagement2007.pdf"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:5737277B-5D6D-4f48-ABFC-DD9C333F4C5D:687ab9a5-a63a-4789-a490-78a9f4ded127" class="wlWriterEditableSmartContent"&gt;&lt;div id="3cf805c8-6a27-4b99-bba4-eb1d97719e9c" style="margin: 0px; padding: 0px; display: inline;"&gt;&lt;div&gt;&lt;a href="http://www.youtube.com/watch?v=oTugjssqOT0&amp;amp;hl=en&amp;amp;fs=1&amp;amp;" target="_new"&gt;&lt;img src="http://lh3.ggpht.com/_zaEn_RqZHY4/Ssg_DIG29RI/AAAAAAAAB-0/1pEMgFf_JnI/video0b8d5f45275c%5B2%5D.jpg?imgmax=800" style="border-style: none" galleryimg="no" onload="var downlevelDiv = document.getElementById('3cf805c8-6a27-4b99-bba4-eb1d97719e9c'); downlevelDiv.innerHTML = &amp;quot;&amp;lt;div&amp;gt;&amp;lt;object width=\&amp;quot;425\&amp;quot; height=\&amp;quot;355\&amp;quot;&amp;gt;&amp;lt;param name=\&amp;quot;movie\&amp;quot; value=\&amp;quot;http://www.youtube.com/v/oTugjssqOT0&amp;amp;hl=en&amp;amp;fs=1&amp;amp;&amp;amp;hl=en\&amp;quot;&amp;gt;&amp;lt;\/param&amp;gt;&amp;lt;embed src=\&amp;quot;http://www.youtube.com/v/oTugjssqOT0&amp;amp;hl=en&amp;amp;fs=1&amp;amp;&amp;amp;hl=en\&amp;quot; type=\&amp;quot;application/x-shockwave-flash\&amp;quot; width=\&amp;quot;425\&amp;quot; height=\&amp;quot;355\&amp;quot;&amp;gt;&amp;lt;\/embed&amp;gt;&amp;lt;\/object&amp;gt;&amp;lt;\/div&amp;gt;&amp;quot;;" alt=""&gt;&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;  &lt;p&gt;1. Pragmatic Talk    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; a. Time is only commodity that matters     &lt;br /&gt;2. Agenda:     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; a. Set goals     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; b. Avoid wasting time     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; c. Deal w/ boss     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; d. How to delegate to others     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; e. Tools     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; f. Stress and Procrastination     &lt;br /&gt;3. Slides on website, Red stars are important     &lt;br /&gt;4. Americans bad at dealing with time     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; a. Waste time     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; b. Time and money are equitable     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; c. How much is your time worth/hr     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; d. Costs company twice your salary     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; e. 50000/yr --&amp;gt; 100k cost to org     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; f. Decisions: Outsource vs. DIY     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; g. Manage time as money     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; i. Money is not well managed     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ii. Though better     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; h. Money can be earned later, Time doesn’t comes back     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; i. Boss == Academic Advisor/Parents     &lt;br /&gt;5. One good thief [11]     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; a. Talk is composed mainly from nuggets from the 2 books     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; i. Time Management for Teachers     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ii. Career Track Seminar: Taking control of Your Work Day     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; b. Time Famine:     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; i. Problem is systematic     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ii. Long term solution required     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; c. This is Life Advice     &lt;br /&gt;6. Overall goal is Fun     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; a. Maximize time is a means so that we maximize fun     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; 7. Typical Office worker wastes 2 hours/day[13]     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; a. Messy desk etc.     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; 8. Manage time well to be successful     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; a. Meta skills     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; b. If you want to run with ppl faster than you, optimize the skills you have     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; 9. Planning [16]     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; a. Great if you can cross stuff from your to-do list     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; b. More important to do the right things adequately than Doing (wrong) things right     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; c. 80/20 rule: Very small things on ur list is gonna matter most     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; d. Experience comes with time and no shortcuts.     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; e. When things don’t go well, you're learning a lot and it'll be better soon.     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; 10. Inspiration     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; a. If you can dream it, you can do it.     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; b. If you refuse to dream it, you won't do it.     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; c. Disneyland: 366 days     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; i. Used everyone of them     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; 11. Planning     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; a. Daily/Weekly/Semester     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; b. You can always change the plan     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; 12. To do lists     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; a. Break stuff into small steps     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; b. Boss: Grow your people     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; c. Eat that frog. Start with the big one first     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; d. *Covey's four quadrants     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; i. Go to Quad 2 after Q1 rather than Q3     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; e. *Paperwork     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; i. No clutter     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ii. Touch each piece of paper/email only once     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 1) Inbox is not to-do list     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; f. Filing system     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; i. All paper goes there in alpha order     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ii. Everything should have a place     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; 13. Desk [ime - 28:06]     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; a. Atleast 2 monitors     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; i. Left:     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 1) To do list     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ii. Mid:     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 1) Inbox     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; iii. Right:     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 1) Calendar     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; b. *Speaker Phone     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; i. Great for waiting     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ii. Timer: Great for making people guilty on other side&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; iii. Telephone Techniques     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 1) Group your calls     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 2) Keep calls short     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 3) Use a headset     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; c. *Thank you cards     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 1) Very important     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 2) Tangible way to tell someone how much you appreciated things     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; d. Recycle Bin:     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; i. Can dig stuff out     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ii. Post-its/notepad     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; iii. Find a system for yourself     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; e. Others     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; i. Address Stamper     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ii. Kleenex     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; 14. Scheduling     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; a. You don’t find time for important things, you make it     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 1) By not wasting time on other things     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 2) Learn to say no     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; a) “I’ll do it if nobody else steps forward”     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; b) “I’ll be your deep fall back,&amp;quot; but you have to keep searching.     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; b. Find your Most productive times     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; c. Find your dead times     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; d. *Interruptions     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; i. 6-9 minutes, 4-5 minute recovery     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ii. reduce frequency and length of interruptions     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; iii. No Email &amp;quot;ding&amp;quot;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; e. Cut things short     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; i. Stand up, stroll to the door, complement, thank, shake hands     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; f. Time journal     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; i. Record your time     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ii. Update not at EOD     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; g. Make up fake meeting b/w near by meetings     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; i. Go to lib.     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; 15. Work-life balance     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; i. Work fewer hours/get more done     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; 16. Focus on things that are important     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; i. Smoking women concerned about noise of jackhammers on her unborn child     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ii. Effective vs. Efficient     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; 17. Procrastination     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; a.&amp;#160;&amp;#160; Quotes     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; i. If I wait long enough it will go away     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ii. Parkinson's mail     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; b. Make fake deadlines     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; c. Get into a comfort zone     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; i. Identify why you aren’t enthusiastic     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ii. Don't be afraid to ask people     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; 18. Delegation     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; a. Authority with responsibility     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; i. Empower ppl to get it done     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ii. Always do the ugliest/dirtiest job yourself     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; b. Can't be vague     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; i. Specific thing     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ii. Specific time     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; iii. Specific reward/penalty for them     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; c. Challenge people     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; i. Delegate until people complain     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ii. Email: Get it in writing     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; iii. Give objectives not procedures     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; iv. Relative importance of tasks     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; d. Never too early to delegate     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; e. Reinforce behavior you want repeated     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; 19. Meetings     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; a. No black-berries     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; b. Agenda     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; c. 1 minute minutes: an efficient way to keep track of decisions made in a meeting: who is responsible for what by when?     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; 20. Technology     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; a. Computer's are faster but they take longer     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; b. End to end     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; c. It must make your life better     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; d. New way of doing things     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; 21. Email Tips     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; a. Save all of it/Searchable     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; b. If you want something done, only one recipient.     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; c. If you really want something done, CC their boss.     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; d. Follow up after 48 hours     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; 22. Communication with Boss     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; a. Write things down     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; b. When's next meeting     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; c. What's the goal     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; d. Who to ask for help     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; e. They want Results!     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; 23. General Advice     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; a. No vacation if you're connected to work     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; b. Kill TV     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; c. Convert Money to Time esp if you have children     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; d. Eat, sleep and exercise     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; e. Never break a promise, but re-negotiate them if need be     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; f. If you haven’t got time to do it right, you don’t have time to do it wrong     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; g. Recognize that most things are pass/fail.     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; h. Feedback loops: ask in confidence     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; 24. Reading     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; a. One minute Manager     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; b. 7 Habits     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; 25. Do it now     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; a. Get a day-timer/PDA     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; b. TODO list in priority order/Covey 4 quads     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; c. Time Journal/Count hours of TV     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; d. Revisit the talk in 30 days and ask &amp;quot;What have I changed?&amp;quot;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; 26. &amp;quot;Time is the only thing you have and one day you may find that you have less than you imagined&amp;quot;. &lt;/p&gt;  &lt;p&gt;Since the formatting above is not great, Here are the notes in a one note format:&lt;/p&gt;  &lt;p&gt;&lt;a title="http://cid-c10db90c8a3e3ef0.skydrive.live.com/self.aspx/.Public/Time%20Management.one" href="http://cid-c10db90c8a3e3ef0.skydrive.live.com/self.aspx/.Public/Time%20Management.one"&gt;http://cid-c10db90c8a3e3ef0.skydrive.live.com/self.aspx/.Public/Time%20Management.one&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;References:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://en.wikipedia.org/wiki/Randy_Pausch"&gt;http://en.wikipedia.org/wiki/Randy_Pausch&lt;/a&gt;    &lt;br /&gt;&lt;a href="http://www.cs.virginia.edu/~robins/Randy/RandyPauschTimeManagement2007.pdf"&gt;http://www.cs.virginia.edu/~robins/Randy/RandyPauschTimeManagement2007.pdf&lt;/a&gt;     &lt;br /&gt;&lt;a href="http://www.cs.virginia.edu/~robins/Randy/"&gt;http://www.cs.virginia.edu/~robins/Randy/&lt;/a&gt;&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6912797365202061321-586845499635744579?l=letuscode.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/SMwYLYPpra-nnWMETB32tcHho9k/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/SMwYLYPpra-nnWMETB32tcHho9k/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/SMwYLYPpra-nnWMETB32tcHho9k/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/SMwYLYPpra-nnWMETB32tcHho9k/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeLearner/~4/BBcqyPTz1WI" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/CodeLearner/~3/BBcqyPTz1WI/time-management.html</link><author>noreply@blogger.com (Amit Wadhwa)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh3.ggpht.com/_zaEn_RqZHY4/Ssg_DIG29RI/AAAAAAAAB-0/1pEMgFf_JnI/s72-c/video0b8d5f45275c%5B2%5D.jpg?imgmax=800" height="72" width="72" /><thr:total>3</thr:total><feedburner:origLink>http://letuscode.blogspot.com/2009/09/time-management.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6912797365202061321.post-4689988104970026346</guid><pubDate>Thu, 27 Aug 2009 00:26:00 +0000</pubDate><atom:updated>2009-08-26T17:31:48.816-07:00</atom:updated><title>Josephus Problem</title><description>&lt;p&gt;The Josephus problem&amp;#160; is a problem often discussed and seen in CS literature. I got introduced to the problem when I initially started programming and knowing nothing about linked lists and/or recursion had a really hard time solving it.&lt;/p&gt;  &lt;p&gt;The problem is stated as:    &lt;br /&gt;There are people standing in a circle waiting to be executed. After the first man is executed, certain number of people are skipped and one man is executed. Then again, people are skipped and a man is executed. The elimination proceeds around the circle (which is becoming smaller and smaller as the executed people are removed), until only the last man remains, who is given freedom. &lt;/p&gt;  &lt;p&gt;The task is to choose the place in the initial circle so that you survive (are the last one remaining).&lt;/p&gt;  &lt;p&gt;The easiest and most logical way to solve the problem is to simply simulate it using a circular link list.&lt;/p&gt;  &lt;p&gt;N is no. of people, M as the count.&lt;/p&gt;  &lt;pre class="c-sharp" name="code"&gt;struct node {  &lt;br /&gt;	int item; &lt;br /&gt;	node* next; &lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;int simple_simulation(int N, int M)&lt;br /&gt;{ &lt;br /&gt;  int i;&lt;br /&gt;  node *t=new node();&lt;br /&gt;  node *x=t;&lt;br /&gt;  t-&amp;gt;item = 1; t-&amp;gt;next = t;&lt;br /&gt;&lt;br /&gt;  //Construct the list &lt;br /&gt;  for (i = 2; i &amp;lt;= N; i++)&lt;br /&gt;  {&lt;br /&gt;    x = (x-&amp;gt;next = new node ());&lt;br /&gt;    x-&amp;gt;item = i; &lt;br /&gt;    x-&amp;gt;next = t;&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  //Find the survivor&lt;br /&gt;  while (x != x-&amp;gt;next)&lt;br /&gt;  {&lt;br /&gt;    for (i = 1; i &amp;lt; M; i++) &lt;br /&gt;      x = x-&amp;gt;next;&lt;br /&gt;    x-&amp;gt;next = x-&amp;gt;next-&amp;gt;next; N--;&lt;br /&gt;  }&lt;br /&gt;  return x-&amp;gt;item;&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;A better way to solve the problem is through recursion. Its easy to find who is killed first, (Hint: it’s the Mth guy from the start) after that we are left with N-1 total people and the count starts from the new guy who was at M+1th position earlier. Going on like this gives us a simple recursive function:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="c-sharp" name="code"&gt;int formula (int N, int M) &lt;br /&gt;{ &lt;br /&gt;if (N == 1)&lt;br /&gt;	return 0; &lt;br /&gt;return (formula (N-1, M) + M) % N; &lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;References: &lt;br /&gt;  &lt;br /&gt;&lt;a href="http://en.wikipedia.org/wiki/Josephus_problem"&gt;http://en.wikipedia.org/wiki/Josephus_problem&lt;/a&gt;&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6912797365202061321-4689988104970026346?l=letuscode.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/eYj7KNoiAoW7RT4hNjUmWYiSKxs/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/eYj7KNoiAoW7RT4hNjUmWYiSKxs/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/eYj7KNoiAoW7RT4hNjUmWYiSKxs/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/eYj7KNoiAoW7RT4hNjUmWYiSKxs/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeLearner/~4/4uCTmLgFcEg" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/CodeLearner/~3/4uCTmLgFcEg/josephus-problem.html</link><author>noreply@blogger.com (Amit Wadhwa)</author><thr:total>1</thr:total><feedburner:origLink>http://letuscode.blogspot.com/2009/08/josephus-problem.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6912797365202061321.post-6269236554628684401</guid><pubDate>Wed, 12 Aug 2009 19:34:00 +0000</pubDate><atom:updated>2009-08-12T12:34:41.316-07:00</atom:updated><title>Climb the Pyramid</title><description>&lt;p&gt;Cal Newport describes the pyramid strategy to evaluate one’s progress. &lt;/p&gt;  &lt;p&gt;&lt;a href="http://calnewport.com/blog/2009/06/03/the-pyramid-method-a-simple-strategy-for-becoming-exceptionally-good/"&gt;http://calnewport.com/blog/2009/06/03/the-pyramid-method-a-simple-strategy-for-becoming-exceptionally-good/&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;From the blog:&lt;/p&gt;  &lt;p&gt;[quote]&lt;/p&gt;  &lt;p&gt;I call this general technique the Pyramid Method. &lt;strong&gt;I claim that it’s a powerful approach for anyone looking to transform an interest or natural talent into an expertise that cannot be ignored.&lt;/strong&gt; Regardless of the pursuit in question, if you want to take it someplace serious, follow Chris’s example. This means:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Pick a single relevant venue to join at the entry level and work to increase your standing. &lt;/li&gt;    &lt;li&gt;Make sure the venue offers clear metrics on your progress; use these metrics to guide your efforts to get better. &lt;/li&gt;    &lt;li&gt;Forget all the other bullshit advice and mini-strategies people offer for getting ahead in your pursuit. If you can’t master this one venue, then you don’t yet deserve the world’s respect. &lt;/li&gt;    &lt;li&gt;Put your head down, and get it done. &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;[/quote]&lt;/p&gt;  &lt;p&gt;The strategy can work for many skills music, sports and coding. But choosing the proper venue is critical. IMO, apart from the above properties mentioned by Chris, The venue should have some other properties.&amp;#160; I would talk about them from a coder’s perspective.&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;1. Have a low entry barrier: So that you can start early from a entry-level. So, when you only know the syntax and basic constructs of a high level language (C++, Java, C#)&lt;/p&gt;    &lt;p&gt;2. A high competition/quality: So, you don’t “win” the venue too soon. So, you are actively engaged when you can solve non-trivial problems in your arena also. This also ensures that good coders will keep coming to your venue rather than taking off someplace else.&lt;/p&gt;    &lt;p&gt;3. Can cater to more than 1 skill: I find this important to be a successful programmer you have to do more than code! This can involve making system architecture, understanding existing codebases, soft skills like communication.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;One of the venues I know is Topcoder.com as it offers all what Chris has outlined in his article and what I have outlined here in this post.&lt;/p&gt;  &lt;p&gt;Now, I just have to go and follow the all important Step 4 from Chris’s post.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6912797365202061321-6269236554628684401?l=letuscode.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/IUqUKC-v9-NKjTuupLMZMKhans0/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/IUqUKC-v9-NKjTuupLMZMKhans0/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/IUqUKC-v9-NKjTuupLMZMKhans0/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/IUqUKC-v9-NKjTuupLMZMKhans0/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeLearner/~4/bBkoyabzwE4" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/CodeLearner/~3/bBkoyabzwE4/climb-pyramid.html</link><author>noreply@blogger.com (Amit Wadhwa)</author><thr:total>0</thr:total><feedburner:origLink>http://letuscode.blogspot.com/2009/08/climb-pyramid.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6912797365202061321.post-3870170397602831928</guid><pubDate>Mon, 18 May 2009 09:18:00 +0000</pubDate><atom:updated>2009-05-18T02:24:34.192-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">tricks</category><category domain="http://www.blogger.com/atom/ns#">techniques</category><title>Largest Element in the Stack</title><description>Problem: Given a stack you have to insert a stream of numbers in it and you have to keep track of the highest element at any point in the stack&lt;br /&gt;&lt;br /&gt;In general, the solution will require traversing through the entire stack hence taking O(n) time. However, given the option of using another data-structure this can be done using minimal extra memory using another "Stack" !. &lt;br /&gt;&lt;br /&gt;The basic idea is when pushing an element in the original, check if the the "highest_elem_stack" is empty or if its top element is less than or equal to the element being pushed, if yes push this element on both stacks. &lt;br /&gt;&lt;br /&gt;While popping check if the popped element is equal to the top element of the "highest_elem_stack". If yes pop both stacks.&lt;br /&gt;&lt;br /&gt;The highest element in the original stack would always be equal to the top element in the "highest_elem_stack"&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6912797365202061321-3870170397602831928?l=letuscode.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/jl3Kl5zCp5YLZshBBVyq6FtIzfM/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/jl3Kl5zCp5YLZshBBVyq6FtIzfM/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/jl3Kl5zCp5YLZshBBVyq6FtIzfM/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/jl3Kl5zCp5YLZshBBVyq6FtIzfM/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeLearner/~4/O4ND2s1YV5s" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/CodeLearner/~3/O4ND2s1YV5s/largest-element-in-stack.html</link><author>noreply@blogger.com (Amit Wadhwa)</author><thr:total>0</thr:total><feedburner:origLink>http://letuscode.blogspot.com/2009/05/largest-element-in-stack.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6912797365202061321.post-1317277066579361363</guid><pubDate>Tue, 09 Dec 2008 18:36:00 +0000</pubDate><atom:updated>2008-12-09T10:48:19.339-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">algorithms</category><title>3 sum problem</title><description>The problem is defined as given a array find a set of elements such that c=a+b.&lt;br /&gt;&lt;br /&gt;Approach&lt;br /&gt;1. Sort the array&lt;br /&gt;2. for each element c=arr[i] in the array &lt;br /&gt;3.        left=0; right=i; &lt;br /&gt;4.        while left&amp;lt;right&lt;br /&gt;5.        check if c==arr[left]+arr[right] &lt;br /&gt;6.              then add c to output&lt;br /&gt;7.              else if c&amp;lt;arr[left]+arr[right] right--&lt;br /&gt;8.              else left++&lt;br /&gt;9.        end&lt;br /&gt;10.end&lt;br /&gt;&lt;br /&gt;&lt;pre name=code class='c-sharp'&gt;&lt;br /&gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;&lt;br /&gt;using namespace std;&lt;br /&gt;&lt;br /&gt;int compare (const void * a, const void * b);&lt;br /&gt;void sum3(int arr[], int n);&lt;br /&gt;void main(){&lt;br /&gt; int arr[]={2,3,5,1,4,9};&lt;br /&gt; &lt;br /&gt; qsort(arr,6,sizeof(int),compare);&lt;br /&gt;&lt;br /&gt; sum3(arr,6);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;void sum3(int arr[], int n){&lt;br /&gt; int l,r;&lt;br /&gt; int count=0; &lt;br /&gt; int index[6];&lt;br /&gt; int curr=0;&lt;br /&gt; for(int i=n-1;i&amp;gt;=0;i--)&lt;br /&gt; { &lt;br /&gt;  l=0; r=i;&lt;br /&gt;  while(l&amp;lt;r){&lt;br /&gt;   curr=arr[l] +arr[r];  &lt;br /&gt;   if(arr[i] == curr){&lt;br /&gt;    index[count++]=i; &lt;br /&gt;    cout&amp;lt;&amp;lt;arr[i]&amp;lt;&amp;lt;&amp;quot; &amp;quot;;&lt;br /&gt;    break;&lt;br /&gt;   }&lt;br /&gt;   else if(arr[i] &amp;lt; curr)&lt;br /&gt;    r--;&lt;br /&gt;   else l++;&lt;br /&gt;  }&lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;int compare (const void * a, const void * b)&lt;br /&gt;{&lt;br /&gt;  return ( *(int*)a - *(int*)b );&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Complexity of the above procedure is O(n^2). Apparently a better solution for the problem doesn't exist. Also, a number of problems can be reduced to the 3 sum problem and are called 3sum hard.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6912797365202061321-1317277066579361363?l=letuscode.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/XETsikI0-WQqw2t05E3wea50zvE/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/XETsikI0-WQqw2t05E3wea50zvE/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/XETsikI0-WQqw2t05E3wea50zvE/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/XETsikI0-WQqw2t05E3wea50zvE/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeLearner/~4/md_ft3VYpKY" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/CodeLearner/~3/md_ft3VYpKY/3-sum-problem.html</link><author>noreply@blogger.com (Amit Wadhwa)</author><thr:total>0</thr:total><feedburner:origLink>http://letuscode.blogspot.com/2008/12/3-sum-problem.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6912797365202061321.post-4962224593012919287</guid><pubDate>Tue, 09 Dec 2008 10:59:00 +0000</pubDate><atom:updated>2008-12-09T03:00:13.889-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">C SC545</category><title>Implement a queue using 2 stacks</title><description>Use 2 stacks, S and T&lt;br /&gt;&lt;br /&gt;1. Enqueue&lt;br /&gt;S.push()&lt;br /&gt;&lt;br /&gt;2. Dequeue&lt;br /&gt;if T.count==0&lt;br /&gt;  while (S.count&gt;0) t.push(s.pop());&lt;br /&gt;&lt;br /&gt;T.pop();&lt;br /&gt;&lt;br /&gt;The operations take O(1) amortized time.&lt;br /&gt;&lt;br /&gt;Reference&lt;br /&gt;C SC 545 Exam Fall 08&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6912797365202061321-4962224593012919287?l=letuscode.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/RkEdu7GwX-0k0tXOnRm5tF5Xf6E/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/RkEdu7GwX-0k0tXOnRm5tF5Xf6E/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/RkEdu7GwX-0k0tXOnRm5tF5Xf6E/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/RkEdu7GwX-0k0tXOnRm5tF5Xf6E/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeLearner/~4/Mgr6GpR4DrQ" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/CodeLearner/~3/Mgr6GpR4DrQ/implement-queue-using-2-stacks.html</link><author>noreply@blogger.com (Amit Wadhwa)</author><thr:total>0</thr:total><feedburner:origLink>http://letuscode.blogspot.com/2008/12/implement-queue-using-2-stacks.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6912797365202061321.post-3189834553464991950</guid><pubDate>Tue, 09 Dec 2008 10:33:00 +0000</pubDate><atom:updated>2008-12-09T02:34:54.096-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">algorithms</category><title>Kth smallest in merge of 2 sorted arrays</title><description>Idea:&lt;br /&gt;The solution goes like this &lt;br /&gt;&lt;br /&gt;A[1...m] B[1...n]&lt;br /&gt;&lt;br /&gt;1. Compare A(k/2) and B(k/2) &lt;br /&gt;if A(k/2) &amp;lt; B(k/2) &lt;br /&gt;&lt;br /&gt;So the kth smallest can&amp;#039;t be in A[1..k/2] and B[k/2+1 .. n]. A[1...k/2] &amp;lt; Kth smallest &amp;lt; B[k/2+1 ... n]&lt;br /&gt;&lt;br /&gt;So we discard these elements. And now search for the k/2th smallest number in the 2 remaining arrays&lt;br /&gt;&lt;br /&gt;if the reverse is true we do the same operation but with B and A swapped&lt;br /&gt;&lt;br /&gt;2. if m&amp;lt;k &lt;br /&gt;&lt;br /&gt;we discard B[1...k-m-1]&lt;br /&gt;&lt;br /&gt;and look for (m+1)th (= k-(k-m-1)) smallest element in remaining arrays&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6912797365202061321-3189834553464991950?l=letuscode.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/AWrHS1bf2BkNDDqxRIF8Nll8rEk/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/AWrHS1bf2BkNDDqxRIF8Nll8rEk/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/AWrHS1bf2BkNDDqxRIF8Nll8rEk/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/AWrHS1bf2BkNDDqxRIF8Nll8rEk/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeLearner/~4/Frj5QNYLnxc" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/CodeLearner/~3/Frj5QNYLnxc/kth-smallest-in-merge-of-2-sorted.html</link><author>noreply@blogger.com (Amit Wadhwa)</author><thr:total>0</thr:total><feedburner:origLink>http://letuscode.blogspot.com/2008/12/kth-smallest-in-merge-of-2-sorted.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6912797365202061321.post-237198109033311633</guid><pubDate>Tue, 09 Dec 2008 10:11:00 +0000</pubDate><atom:updated>2008-12-09T02:17:57.588-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">C</category><category domain="http://www.blogger.com/atom/ns#">C++</category><category domain="http://www.blogger.com/atom/ns#">concepts</category><title>const trick</title><description>1. const int* x=5;&lt;br /&gt;&lt;br /&gt;Declares a pointer to a constant. &lt;br /&gt;So now a assignment *x=10; will fail&lt;br /&gt;&lt;br /&gt;2. int* const x=5;&lt;br /&gt;Declares a constant pointer to a variable.  &lt;br /&gt;so *x=10 will PASS while x=10 will fail.&lt;br /&gt;&lt;br /&gt;3. const int * ==  int const *&lt;br /&gt;The above 2 mean the same thing&lt;br /&gt;&lt;br /&gt;4. const int const *x=5;&lt;br /&gt;&lt;br /&gt;Using 3. statement 4 Declares a pointer to a constant. (same as 1)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6912797365202061321-237198109033311633?l=letuscode.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/drs5oABNvdjeltlmlVF7BEWiBOU/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/drs5oABNvdjeltlmlVF7BEWiBOU/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/drs5oABNvdjeltlmlVF7BEWiBOU/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/drs5oABNvdjeltlmlVF7BEWiBOU/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeLearner/~4/ID4cHvLNEsM" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/CodeLearner/~3/ID4cHvLNEsM/const-trick.html</link><author>noreply@blogger.com (Amit Wadhwa)</author><thr:total>0</thr:total><feedburner:origLink>http://letuscode.blogspot.com/2008/12/const-trick.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6912797365202061321.post-4230935368060050549</guid><pubDate>Wed, 03 Dec 2008 23:47:00 +0000</pubDate><atom:updated>2008-12-03T15:48:35.148-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">C</category><category domain="http://www.blogger.com/atom/ns#">C++</category><category domain="http://www.blogger.com/atom/ns#">concepts</category><title>New vs. Malloc</title><description>New calls the "operator new(...)" method to allocate memory (like what malloc does), but memory allocation is followed by a call to the appropriate constructor. Also new is type-safe, yet malloc is not.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6912797365202061321-4230935368060050549?l=letuscode.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/MCp77YBoLM385jpYCSgh4SLX0h4/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/MCp77YBoLM385jpYCSgh4SLX0h4/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/MCp77YBoLM385jpYCSgh4SLX0h4/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/MCp77YBoLM385jpYCSgh4SLX0h4/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeLearner/~4/1uPsbPJH6Pk" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/CodeLearner/~3/1uPsbPJH6Pk/new-vs-malloc.html</link><author>noreply@blogger.com (Amit Wadhwa)</author><thr:total>0</thr:total><feedburner:origLink>http://letuscode.blogspot.com/2008/12/new-vs-malloc.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6912797365202061321.post-11757697779498848</guid><pubDate>Wed, 03 Dec 2008 23:39:00 +0000</pubDate><atom:updated>2008-12-11T09:30:23.040-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">C</category><category domain="http://www.blogger.com/atom/ns#">concepts</category><title>Static function in C</title><description>A static function is a function with the static qualifier applied:&lt;br /&gt;&lt;br /&gt;static void foo ( void )&lt;br /&gt;{&lt;br /&gt;  /* Blah blah */&lt;br /&gt;}&lt;br /&gt;What it does is restrict visibility of the function to the translation unit in which it's declared. Functions are implicitly declared as extern by default, which means they're visible across translation units.&lt;br /&gt;&lt;br /&gt;Reference&lt;br /&gt;&lt;a href="http://www.daniweb.com/forums/thread82504.html"&gt;http://www.daniweb.com/forums/thread82504.html&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6912797365202061321-11757697779498848?l=letuscode.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/-A71koffDYAuc1n5fuiR6j12u0g/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/-A71koffDYAuc1n5fuiR6j12u0g/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/-A71koffDYAuc1n5fuiR6j12u0g/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/-A71koffDYAuc1n5fuiR6j12u0g/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeLearner/~4/M4hu1UJNSgw" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/CodeLearner/~3/M4hu1UJNSgw/static-function-in-c.html</link><author>noreply@blogger.com (Amit Wadhwa)</author><thr:total>0</thr:total><feedburner:origLink>http://letuscode.blogspot.com/2008/12/static-function-in-c.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6912797365202061321.post-6150716597764678690</guid><pubDate>Tue, 02 Dec 2008 03:28:00 +0000</pubDate><atom:updated>2008-12-09T01:33:05.027-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">algorithms</category><category domain="http://www.blogger.com/atom/ns#">techniques</category><title>Maximum Sum Sub array</title><description>This is a interesting problem which can be solved in a variety of ways. This problem has a chunk of a chapter devoted in the Programming Pearls book by Jon Bentley.&lt;br /&gt;&lt;br /&gt;Method 1. Brute Force O(n^2)&lt;br /&gt;&lt;br /&gt;This involves taking each index i between 0 to len and for every other index j&gt;i calculate the sum while keeping track of the maximum. &lt;br /&gt;&lt;br /&gt;&lt;pre name=code class='c-sharp'&gt;&lt;br /&gt;private static int sumbruteforce(int [] arr){&lt;br /&gt;  int sum=0;&lt;br /&gt;  int max=Integer.MIN_VALUE;&lt;br /&gt;  int maxi=0;&lt;br /&gt;  int maxj=0;&lt;br /&gt;  for(int i=0;i&amp;lt;arr.length;i++){&lt;br /&gt;   sum=0;&lt;br /&gt;   for(int j=i;j&amp;lt;arr.length;j++){&lt;br /&gt;    sum+=arr[j];&lt;br /&gt;    if(sum&amp;gt;max){&lt;br /&gt;     max=sum;&lt;br /&gt;     maxi=i;&lt;br /&gt;     maxj=j;&lt;br /&gt;    }&lt;br /&gt;   }  &lt;br /&gt;  }&lt;br /&gt;  System.out.println(maxi+&amp;quot; &amp;quot;+maxj);&lt;br /&gt;  return max;&lt;br /&gt; }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;2. Method 2: Divide and Conquer: O(n log n)&lt;br /&gt;This involves dividing the array recursively into 3 parts left, right and mid. And comparing max sum of the three. &lt;br /&gt;&lt;br /&gt;Calculating max(left) and max(right) is trivial using recursion. For the mid element, we know the left bound (l) lies in left half and right  bound (r) lies in right half. So we calculate l s.t sum of elements between l and mid is max and r s.t the sum is maximum for elements between mid and right.&lt;br /&gt;&lt;pre name=code class='c-sharp'&gt;&lt;br /&gt;private static int sumDivConquer(int [] arr, int l, int h){&lt;br /&gt;  if(l&amp;gt;h)&lt;br /&gt;   return 0;&lt;br /&gt;  if(l==h)&lt;br /&gt;   return arr[l];&lt;br /&gt;&lt;br /&gt;  &lt;br /&gt;  int m=(int) Math.floor((double)((l+h)/2));&lt;br /&gt;  int max=0;&lt;br /&gt;  &lt;br /&gt;  max=sumDivConquer(arr, l, m);&lt;br /&gt;  max=Math.max(max,sumDivConquer(arr, m+1, h));&lt;br /&gt;  &lt;br /&gt;  &lt;br /&gt;  int L=0;&lt;br /&gt;  int R=0;&lt;br /&gt;  int S=0;&lt;br /&gt;  for(int i=m;i&amp;gt;=l;i--){&lt;br /&gt;   S+=arr[i];&lt;br /&gt;   L=S&amp;gt;L? S:L;&lt;br /&gt;  }&lt;br /&gt;  R=S=0;&lt;br /&gt;  for(int i=m+1;i&amp;lt;=h;i++){&lt;br /&gt;   S+=arr[i];&lt;br /&gt;   R=S&amp;gt;R? S:R;&lt;br /&gt;  }&lt;br /&gt;  max=Math.max(max, L+R);&lt;br /&gt;  &lt;br /&gt;  return max;     &lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;3. Dynamic programming O(n)&lt;br /&gt;&lt;br /&gt;An optimized DP solution takes O(n). &lt;br /&gt;&lt;br /&gt;The naive DP solution acts exactly like method 1 and generates a matrix A[i,j] containing sum of elements with left bound i and right bound j.&lt;br /&gt;&lt;br /&gt;The optimized DP constructs a single dimension table of array size, with &lt;br /&gt;table[i]: Represents the sum of subarray with maximum sum terminating at i;&lt;br /&gt;&lt;br /&gt;for i=0, table[0]=arr[0]&lt;br /&gt;for i&gt;0, table[i]= Max{ arr[i], arr[i]+table[i-1]}&lt;br /&gt;&lt;br /&gt;Once we have the table finding max sum takes only a single pass through the above table, we can fasten the procedure up by keeping track of max side by side. &lt;br /&gt;&lt;br /&gt;&lt;pre name=code class='c-sharp'&gt;&lt;br /&gt;private static int sumDP(int [] arr){&lt;br /&gt; int table[]=new int[arr.length];&lt;br /&gt; table[0]=arr[0];&lt;br /&gt; for(int i=1;i&amp;lt;arr.length;i++){&lt;br /&gt;  table[i]=Math.max(arr[i], arr[i]+table[i-1]);&lt;br /&gt; }&lt;br /&gt; int max=Integer.MIN_VALUE;&lt;br /&gt; for(int i=0;i&amp;lt;arr.length;i++){&lt;br /&gt;  if(table[i]&amp;gt;max)&lt;br /&gt;   max=table[i];&lt;br /&gt; }&lt;br /&gt; return max;&lt;br /&gt;}&lt;br /&gt; &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;A simpler algorithm based on the same idea as the DP method is called Kadane's algorithm and it works without the need of maintaining the auxiliary table. &lt;br /&gt;&lt;br /&gt;The code is derived from &lt;a href="http://www.algorithmist.com/index.php/Kadane%27s_Algorithm"&gt;here&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre name=code class='c-sharp'&gt;&lt;br /&gt;private static int sumKadane(int arr[]){&lt;br /&gt;    int max=Integer.MIN_VALUE;&lt;br /&gt;    int a,b; a=b=0;&lt;br /&gt;    int curr = 0;&lt;br /&gt;    int aa = 1;&lt;br /&gt;    for (int bb= 1;bb&amp;lt;arr.length;bb++){&lt;br /&gt;        curr = curr + arr[bb];&lt;br /&gt;        if (curr &amp;gt; max) {&lt;br /&gt;            max=curr;&lt;br /&gt;            a=aa;&lt;br /&gt;            b=bb;             &lt;br /&gt;        }&lt;br /&gt;        &lt;br /&gt;        if (curr &amp;lt; 0) {&lt;br /&gt;            curr = 0;&lt;br /&gt;            aa = bb + 1;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;    System.out.println(a+&amp;quot; &amp;quot;+b);&lt;br /&gt;    return max;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6912797365202061321-6150716597764678690?l=letuscode.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/uUAjEEWvuYN-sFST_Nqhc8Opo5o/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/uUAjEEWvuYN-sFST_Nqhc8Opo5o/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/uUAjEEWvuYN-sFST_Nqhc8Opo5o/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/uUAjEEWvuYN-sFST_Nqhc8Opo5o/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeLearner/~4/Yd_TrvYEfYA" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/CodeLearner/~3/Yd_TrvYEfYA/maximum-sum-sub-array.html</link><author>noreply@blogger.com (Amit Wadhwa)</author><thr:total>0</thr:total><feedburner:origLink>http://letuscode.blogspot.com/2008/12/maximum-sum-sub-array.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6912797365202061321.post-2558927746921814365</guid><pubDate>Mon, 01 Dec 2008 12:12:00 +0000</pubDate><atom:updated>2008-12-01T04:25:58.686-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">algorithms</category><title>Counting Number of set bits in a number</title><description>1. Method 1&lt;br /&gt;&lt;br /&gt;Complexity= O(n)&lt;br /&gt;&lt;br /&gt;&lt;pre name=code class='c-sharp'&gt;&lt;br /&gt;int numOfOnes(int n){&lt;br /&gt;  int count=0;&lt;br /&gt;  while(n!=0){&lt;br /&gt;   if((n &amp; 1) == 1)&lt;br /&gt;    count++;&lt;br /&gt;   n=n&gt;&gt;&gt;1;&lt;br /&gt;  }&lt;br /&gt;  return count;   &lt;br /&gt; }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Method 2:&lt;br /&gt;The method works as follows:&lt;br /&gt;&lt;br /&gt;The operation:&lt;br /&gt;n=n&amp;(n-1) &lt;br /&gt;resets the last set bit of n. &lt;br /&gt;&lt;br /&gt;So the number of time the above operation can be done before n is equal to 0 are the number of set bits in n originally.&lt;br /&gt;&lt;br /&gt;Complexity= O(m) where m is the number of set bits in n.&lt;br /&gt;&lt;br /&gt;&lt;pre name=code class='c-sharp'&gt;&lt;br /&gt;int numOfOnes2(int n){&lt;br /&gt; int count=0;&lt;br /&gt; while(n!=0){&lt;br /&gt;  n=n&amp;(n-1);   &lt;br /&gt;  count++;&lt;br /&gt; }&lt;br /&gt; return count;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Also note that if count above is 1, then number n is a power of 2.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6912797365202061321-2558927746921814365?l=letuscode.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/W8cNTEbw54s8bSqEtcAB5ll0uic/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/W8cNTEbw54s8bSqEtcAB5ll0uic/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/W8cNTEbw54s8bSqEtcAB5ll0uic/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/W8cNTEbw54s8bSqEtcAB5ll0uic/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeLearner/~4/wtcLzFH3-hQ" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/CodeLearner/~3/wtcLzFH3-hQ/counting-number-of-set-bits-in-number.html</link><author>noreply@blogger.com (Amit Wadhwa)</author><thr:total>0</thr:total><feedburner:origLink>http://letuscode.blogspot.com/2008/12/counting-number-of-set-bits-in-number.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6912797365202061321.post-7812379120858848520</guid><pubDate>Mon, 01 Dec 2008 09:29:00 +0000</pubDate><atom:updated>2008-12-01T01:34:29.048-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">algorithms</category><title>Generating Combinations</title><description>The algorithm for this is very similar to the algorithm for generating permutations.&lt;br /&gt;&lt;br /&gt;Algorithm:&lt;br /&gt;1. Remove Item i from elements and put in output array&lt;br /&gt;2. Display Output&lt;br /&gt;3. Generate combinations for rest of the array&lt;br /&gt;&lt;br /&gt;&lt;pre name=code class='c-sharp'&gt;&lt;br /&gt;public void combine(String str){&lt;br /&gt;  int l=str.length();  &lt;br /&gt;  StringBuilder out=new StringBuilder();&lt;br /&gt;  char []in=str.toCharArray();&lt;br /&gt;  doCombine(in,out,l,0);&lt;br /&gt; }&lt;br /&gt; private void doCombine(char[] in, StringBuilder out, int n, int start){&lt;br /&gt;  for(int i=start;i&amp;lt;n;i++){   &lt;br /&gt;   out.append(in[i]);&lt;br /&gt;   System.out.println(out);&lt;br /&gt;   if(i&amp;lt;n)&lt;br /&gt;    doCombine(in, out, n, i+1);&lt;br /&gt;   &lt;br /&gt;   out.setLength(out.length()-1);&lt;br /&gt;  }&lt;br /&gt; }&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6912797365202061321-7812379120858848520?l=letuscode.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/UDLg1rGK4oAkeAOQJVv3vCMZhoI/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/UDLg1rGK4oAkeAOQJVv3vCMZhoI/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/UDLg1rGK4oAkeAOQJVv3vCMZhoI/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/UDLg1rGK4oAkeAOQJVv3vCMZhoI/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeLearner/~4/uqvxB7rNjz4" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/CodeLearner/~3/uqvxB7rNjz4/generating-combinations.html</link><author>noreply@blogger.com (Amit Wadhwa)</author><thr:total>0</thr:total><feedburner:origLink>http://letuscode.blogspot.com/2008/12/generating-combinations.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6912797365202061321.post-3115373741075758789</guid><pubDate>Mon, 01 Dec 2008 08:17:00 +0000</pubDate><atom:updated>2008-12-01T01:05:54.290-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">algorithms</category><title>Generating Permutations</title><description>This has been a problem I always had difficulty with. The only resort I had till now was to memorize the solution and spit it out when needed. But now, with a algorithms class behind me and a little bit more insight into the problem, I can atlast cook up the logic here.&lt;br /&gt;&lt;br /&gt;SO, the Question is: &lt;br /&gt;Given n items (numbers, chars) generate/print all possible permutation of them&lt;br /&gt;&lt;br /&gt;Algorithm:&lt;br /&gt;1. Take a item i and put it in the perm array&lt;br /&gt;2. Generate Permutation for rest of the n-1 elements (Print perm array when n==0)&lt;br /&gt;3. Put back Item i in elements&lt;br /&gt;&lt;br /&gt;&lt;pre name=code class='c-sharp'&gt;&lt;br /&gt;public class Permute {&lt;br /&gt; public void permute(String str){&lt;br /&gt;  int l=str.length();&lt;br /&gt;  boolean [] used=new boolean[l];&lt;br /&gt;  StringBuilder out=new StringBuilder();&lt;br /&gt;  char []in=str.toCharArray();&lt;br /&gt;  doPermute(in,out,used, l, 0);&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; private void doPermute(char[] in, StringBuilder out, boolean[] used, int n,&lt;br /&gt;   int level) {&lt;br /&gt;  if (level==n){&lt;br /&gt;   System.out.println(out.toString());&lt;br /&gt;   return;&lt;br /&gt;  }&lt;br /&gt;  &lt;br /&gt;  for(int i=0;i&amp;lt;n;i++){&lt;br /&gt;   if(used[i]) continue;&lt;br /&gt;   out.append(in[i]);&lt;br /&gt;   used[i]=true;&lt;br /&gt;   doPermute(in, out, used, n, level+1);&lt;br /&gt;   used[i]=false;&lt;br /&gt;   out.setLength(out.length()-1);&lt;br /&gt;  }&lt;br /&gt;  &lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;References:&lt;br /&gt;&lt;br /&gt;1. Programming Interviews Exposed&lt;br /&gt;2. &lt;a href="http://www.paulgriffiths.net/program/c/perm.php"&gt;http://www.paulgriffiths.net/program/c/perm.php&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6912797365202061321-3115373741075758789?l=letuscode.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/y1CmchCDTMAJ-KFsbAGLhRjjSPI/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/y1CmchCDTMAJ-KFsbAGLhRjjSPI/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/y1CmchCDTMAJ-KFsbAGLhRjjSPI/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/y1CmchCDTMAJ-KFsbAGLhRjjSPI/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeLearner/~4/N8hYvnXUBoM" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/CodeLearner/~3/N8hYvnXUBoM/generating-permutations.html</link><author>noreply@blogger.com (Amit Wadhwa)</author><thr:total>0</thr:total><feedburner:origLink>http://letuscode.blogspot.com/2008/12/generating-permutations.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6912797365202061321.post-657404193385117264</guid><pubDate>Sun, 16 Nov 2008 11:13:00 +0000</pubDate><atom:updated>2008-11-16T04:17:36.518-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">tricks</category><category domain="http://www.blogger.com/atom/ns#">algorithms</category><title>Shuffling</title><description>Shuffling is a very interesting programming problem, Almost everybody can come up with a good algorithm using a simple rand() function, but it gets a little tricky when one has to perform a in place shuffle (i.e. w/o using any extra memory).&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Knuth's Algorithm&lt;/b&gt; described in the Art of Computer programming, Vol 2 (which is based on Fisher Yates algorithm) is regarded as one the best known algorithm for the problem.&lt;br /&gt;&lt;br /&gt;The description on wikipedia is a little clearer and goes like this:&lt;br /&gt;1. Let A1 := 1, A2 := 2 and so on up to AN := N, and let n := N.&lt;br /&gt;2. Pick a random number k between 1 and n inclusive.&lt;br /&gt;3. If k ≠ n, swap the values of Ak and An.&lt;br /&gt;4. Decrease n by one.&lt;br /&gt;5. Repeat from step 2 until n is less than 2.&lt;br /&gt;&lt;br /&gt;&lt;pre name=code class='c-sharp'&gt;&lt;br /&gt;  public static void shuffle (int[] array) &lt;br /&gt;    {        &lt;br /&gt;        Random rng = new Random();   // i.e., java.util.Random.&lt;br /&gt;       &lt;br /&gt;       // The number of items left to shuffle (loop invariant).&lt;br /&gt;        int n = array.length;                &lt;br /&gt;        &lt;br /&gt;        while (n &amp;gt; 1) &lt;br /&gt;        {&lt;br /&gt;            int k = rng.nextInt(n);  // 0 &lt;= k &lt; n.&lt;br /&gt;            n--;      // n is now the last pertinent index;&lt;br /&gt;            // swap array[n] with array[k] (does nothing if k == n).&lt;br /&gt;            int temp = array[n];                 &lt;br /&gt;            array[n] = array[k];&lt;br /&gt;            array[k] = temp;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Alternately, instead of going n to 1 we can do a forward pass like here:&lt;br /&gt;&lt;br /&gt;&lt;pre name=code class='c-sharp'&gt;&lt;br /&gt;int N = array.length; &lt;br /&gt;for (int i = 0; i &lt; N; i++) &lt;br /&gt;{ &lt;br /&gt;   int r = i + (int) (Math.random() * (N-i)); &lt;br /&gt;   int t = array[r]; &lt;br /&gt;   array[r] = array[i]; &lt;br /&gt;   array[i] = t; &lt;br /&gt;} &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;An &lt;b&gt;alternative method&lt;/b&gt; is we can assign each element of the set to be shuffled a random number and the set is then sorted according to these numbers. This may be faster in practice, despite having worse asymptotic time complexity (O(n log n) vs. O(n)). &lt;br /&gt;&lt;br /&gt;Like the Fisher-Yates shuffle, this method will also produce unbiased results if correctly implemented, and may be more tolerant of certain kinds of bias in the random numbers. However, care must be taken to ensure that the assigned random numbers are never duplicated, since sorting algorithms in general won't order elements randomly in case of a tie.&lt;br /&gt;&lt;br /&gt;Wikipedia also defines a interesting algorithm very similar to the Knuth's algorithm called the &lt;b&gt;Sattolo algorithm for generating uniformly distributed cyclic permutations&lt;/b&gt;. The only difference between the 2 algorithms is that in Sattolo's algorithm in step 2, the random number k is chosen from the range between 1 and n−1 (rather than between 1 and n) inclusive. &lt;br /&gt;&lt;br /&gt;To turn the Java example above into an example of Sattolo's algorithm, simply replace rng.nextInt(n) with rng.nextInt(n-1) in the code. This simple change modifies the algorithm so that the resulting permutation always consists of a single cycle.&lt;br /&gt;&lt;br /&gt;Reference:&lt;br /&gt;1. TAOCP Vol 2 Section 3.4.5 by Donald Knuth&lt;br /&gt;2. &lt;a href="http://en.wikipedia.org/wiki/Knuth_shuffle"&gt;http://en.wikipedia.org/wiki/Knuth_shuffle&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6912797365202061321-657404193385117264?l=letuscode.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/UJTJE3AbEpZHX-qkSL52ok3yxoE/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/UJTJE3AbEpZHX-qkSL52ok3yxoE/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/UJTJE3AbEpZHX-qkSL52ok3yxoE/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/UJTJE3AbEpZHX-qkSL52ok3yxoE/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeLearner/~4/_wLoAOADq6w" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/CodeLearner/~3/_wLoAOADq6w/shuffling.html</link><author>noreply@blogger.com (Amit Wadhwa)</author><thr:total>0</thr:total><feedburner:origLink>http://letuscode.blogspot.com/2008/11/shuffling.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6912797365202061321.post-8902221133748558875</guid><pubDate>Fri, 14 Nov 2008 07:59:00 +0000</pubDate><atom:updated>2008-11-14T01:09:29.973-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">tricks</category><category domain="http://www.blogger.com/atom/ns#">algorithms</category><title>Finding a loop in a singly linked list</title><description>&lt;style&gt; &lt;br /&gt;pre.code {&lt;br /&gt;  background-color:#ccffcc;&lt;br /&gt;  border:thin black ridge;&lt;br /&gt;  padding:1cm;&lt;br /&gt;}&lt;br /&gt;&lt;/style&gt; &lt;br /&gt;There are three "decent" solutions for it&lt;br /&gt;&lt;br /&gt;1. Pass through the list, use a hashtable to store the addresses of the visited nodes, if 2 nodes hash to same key, there's a loop.&lt;br /&gt; &lt;br /&gt;&lt;pre name=code class='c-sharp'&gt;&lt;br /&gt;function boolean hasLoop(Node startNode){&lt;br /&gt;  HashSet nodesSeen = new HashSet();&lt;br /&gt;  Node currentNode = startNode;&lt;br /&gt;  do {&lt;br /&gt;    if (nodesSeen.contains(currentNode)) return true;&lt;br /&gt;    nodesSeen.add(currentNode);&lt;br /&gt;  } while (currentNode = currentNode.next());&lt;br /&gt;  return false;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;2. Floyd's Cycle-Finding Algorithm&lt;br /&gt;&lt;br /&gt;Simultaneously go through the list by ones (slow iterator) and by twos (fast iterator). If there is a loop the fast iterator will go around that loop twice as fast as the slow iterator. The fast iterator will lap the slow iterator within a single pass through the cycle. Detecting a loop is then just detecting that the slow iterator has been lapped by the fast iterator.&lt;br /&gt;&lt;br /&gt;&lt;pre name=code class='c-sharp'&gt;&lt;br /&gt;function boolean hasLoop(Node startNode){&lt;br /&gt;  Node slowNode = Node fastNode1 = Node fastNode2 = startNode;&lt;br /&gt;  while (slowNode &amp;&amp; fastNode1 = fastNode2.next() &amp;&amp; fastNode2 = fastNode1.next()){&lt;br /&gt;    if (slowNode == fastNode1 || slowNode == fastNode2) return true;&lt;br /&gt;    slowNode = slowNode.next();&lt;br /&gt;  }&lt;br /&gt;  return false;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Alternately, in C&lt;br /&gt;&lt;br /&gt;&lt;pre name=code class='c-sharp'&gt;&lt;br /&gt;p=q=head;&lt;br /&gt;do{&lt;br /&gt;p=p-&gt;next-&gt;next;&lt;br /&gt;q=q-&gt;next;&lt;br /&gt;}while p!=q or p!=null;&lt;br /&gt;if p==q&lt;br /&gt;loop exists&lt;br /&gt;else&lt;br /&gt;no loop&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;To find the smallest index where the cycle starts:&lt;br /&gt;&lt;pre name=code class='c-sharp'&gt;&lt;br /&gt;// To find the smallest index where the cycle starts&lt;br /&gt;// P is set from last algorithm&lt;br /&gt;int index=0;&lt;br /&gt;q=0;&lt;br /&gt;while p!=q{&lt;br /&gt;     p=p-&gt;next;&lt;br /&gt;     q=q-&gt;next;&lt;br /&gt;     index ++;&lt;br /&gt;}&lt;br /&gt;return index;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;To find cycle length&lt;br /&gt;&lt;pre name=code class='c-sharp'&gt;&lt;br /&gt;// P &amp; Q  is set from last algorithm&lt;br /&gt;int len=0;&lt;br /&gt;while p!=q{&lt;br /&gt;     p=p-&gt;next;     &lt;br /&gt;     len ++;&lt;br /&gt;}&lt;br /&gt;return len;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;See reasoning for len, index functions at &lt;a href="http://en.wikipedia.org/wiki/Cycle_detection"&gt;wiki&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;3. Brent' Algorithm&lt;br /&gt;&lt;pre name=code class='c-sharp'&gt;&lt;br /&gt;power=len=1;&lt;br /&gt;q=head;&lt;br /&gt;p=head-&gt;next;&lt;br /&gt;&lt;br /&gt;while p!=q {&lt;br /&gt; if (power==len){&lt;br /&gt;  p=q;&lt;br /&gt;  power*=2 , len=0;&lt;br /&gt; }&lt;br /&gt; p=p-&gt;next; len++;&lt;br /&gt; if(p==NULL) return (no cycle);&lt;br /&gt;}&lt;br /&gt;return (cycle), len;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;For finding Index&lt;br /&gt;&lt;pre name=code class='c-sharp'&gt;&lt;br /&gt;i=index=0;&lt;br /&gt;p=q=head;&lt;br /&gt;while(i++&amp;lt;len)&lt;br /&gt; p=p-&amp;gt;next;&lt;br /&gt;&lt;br /&gt;while p!=q {&lt;br /&gt;  q=q-&amp;gt;next;&lt;br /&gt;  p=p-&amp;gt;next;&lt;br /&gt;  index++;&lt;br /&gt;}&lt;br /&gt;return index;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Reference:&lt;br /&gt;&lt;a href="http://ostermiller.org/find_loop_singly_linked_list.html"&gt;http://ostermiller.org/find_loop_singly_linked_list.html&lt;/a&gt;&lt;br /&gt;&lt;a href="http://en.wikipedia.org/wiki/Cycle_detection"&gt;wiki&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6912797365202061321-8902221133748558875?l=letuscode.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/KQ35sFocUE3kxvBK91lZY46iFLg/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/KQ35sFocUE3kxvBK91lZY46iFLg/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/KQ35sFocUE3kxvBK91lZY46iFLg/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/KQ35sFocUE3kxvBK91lZY46iFLg/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeLearner/~4/g0i7rYFY6vI" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/CodeLearner/~3/g0i7rYFY6vI/finding-loop-in-singly-linked-list.html</link><author>noreply@blogger.com (Amit Wadhwa)</author><thr:total>0</thr:total><feedburner:origLink>http://letuscode.blogspot.com/2008/11/finding-loop-in-singly-linked-list.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6912797365202061321.post-888403876437539361</guid><pubDate>Thu, 13 Nov 2008 23:51:00 +0000</pubDate><atom:updated>2008-11-13T15:52:59.978-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">tricks</category><category domain="http://www.blogger.com/atom/ns#">algorithms</category><title>Delete a node in single linked list</title><description>The solution to this is to copy the data from the next node into this node and delete the next node!. Ofcourse this wont work if the node to be deleted is the last node. Mark it as dummy in that case. If you have a Circular linked list, then this might be all the more interesting.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6912797365202061321-888403876437539361?l=letuscode.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/qA3_KYYXbgMEJsCjnHg-C2nxBpY/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/qA3_KYYXbgMEJsCjnHg-C2nxBpY/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/qA3_KYYXbgMEJsCjnHg-C2nxBpY/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/qA3_KYYXbgMEJsCjnHg-C2nxBpY/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeLearner/~4/q2S3NwL4Vqs" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/CodeLearner/~3/q2S3NwL4Vqs/delete-node-in-single-linked-list.html</link><author>noreply@blogger.com (Amit Wadhwa)</author><thr:total>0</thr:total><feedburner:origLink>http://letuscode.blogspot.com/2008/11/delete-node-in-single-linked-list.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6912797365202061321.post-1800370978872305696</guid><pubDate>Tue, 11 Nov 2008 22:16:00 +0000</pubDate><atom:updated>2008-11-11T14:19:37.715-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">algorithms</category><title>Two problems for the day</title><description>Robotic Arm movement: http://icpcres.ecs.baylor.edu/onlinejudge/index.php?option=com_onlinejudge&amp;Itemid=8&amp;category=3&amp;page=show_problem&amp;problem=37&lt;br /&gt;&lt;br /&gt;Binary Search: http://codekata.pragprog.com/2007/01/kata_two_karate.html&lt;br /&gt;&lt;br /&gt;To be updated...&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6912797365202061321-1800370978872305696?l=letuscode.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/STP1uMlnXvp7tBmtzuxkrsgdGMI/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/STP1uMlnXvp7tBmtzuxkrsgdGMI/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/STP1uMlnXvp7tBmtzuxkrsgdGMI/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/STP1uMlnXvp7tBmtzuxkrsgdGMI/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeLearner/~4/2cfd4UTvags" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/CodeLearner/~3/2cfd4UTvags/two-problems-for-day.html</link><author>noreply@blogger.com (Amit Wadhwa)</author><thr:total>0</thr:total><feedburner:origLink>http://letuscode.blogspot.com/2008/11/two-problems-for-day.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6912797365202061321.post-4792672797231441831</guid><pubDate>Mon, 10 Nov 2008 11:30:00 +0000</pubDate><atom:updated>2008-11-11T00:43:06.282-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">algorithms</category><category domain="http://www.blogger.com/atom/ns#">Optimizations</category><title>Optimizing Loops</title><description>A few ways to optimize loops&lt;br /&gt;&lt;br /&gt;Iterating through loops has the following structure in many cases:&lt;br /&gt;&lt;pre name="code" class="c-sharp"&gt;&lt;br /&gt;for(int k=0; k&amp;lt;someVar.length(); k++)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Check it out, you execute the k=0 command, then you execute the comparison as well as the length method as many times as you have n variables to loop through. Finally you increment k n+1 times. This all leads to a lot of executions. To make it go faster:&lt;br /&gt;&lt;pre name="code" class="c-sharp"&gt;&lt;br /&gt;int length = someVar.length();&lt;br /&gt;for(int k=0; k&amp;lt;length; k++)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Taking the method call to length() out will increase the loop&amp;#039;s speed slightly. However, you&amp;#039;re still calling length from the variable table n times. So try it this way instead:&lt;br /&gt;&lt;pre name="code" class="c-sharp"&gt;&lt;br /&gt;int length = someVar.length();&lt;br /&gt;for(int k=length; k&amp;gt;0; --k)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;This will iterate backwards. What that means, is that now you won&amp;#039;t be pulling up 2 variables in the comparison. You&amp;#039;ll only compare one variable to 0. Although, it isn&amp;#039;t necessarily that much more beneficial to call length outside of the loop statement now since your call to length is only executed once.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6912797365202061321-4792672797231441831?l=letuscode.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/b1GOcDYzl9xQZrMcqltxsdcrceE/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/b1GOcDYzl9xQZrMcqltxsdcrceE/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/b1GOcDYzl9xQZrMcqltxsdcrceE/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/b1GOcDYzl9xQZrMcqltxsdcrceE/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeLearner/~4/OiXjdLG8MRk" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/CodeLearner/~3/OiXjdLG8MRk/optimizing-loops.html</link><author>noreply@blogger.com (Amit Wadhwa)</author><thr:total>0</thr:total><feedburner:origLink>http://letuscode.blogspot.com/2008/11/optimizing-loops.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6912797365202061321.post-5896113210908872806</guid><pubDate>Mon, 03 Nov 2008 20:50:00 +0000</pubDate><atom:updated>2008-11-03T12:52:55.649-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Memory</category><category domain="http://www.blogger.com/atom/ns#">ToDo</category><category domain="http://www.blogger.com/atom/ns#">C++</category><title>Virtual Destructors</title><description>If an object (with a non-virtual destructor) is destroyed explicitly by applying the delete operator to a base-class pointer to the object, the base-class destructor function (matching the pointer type) is called on the object.&lt;br /&gt;&lt;br /&gt;There is a simple solution to this problem: declare a virtual base-class destructor. This makes all derived-class destructors virtual even though they don't have the same name as the base-class destructor. Now, if the object in the hierarchy is destroyed explicitly by applying the delete operator to a base-class pointer to a derived-class object, the destructor for the appropriate class is called.&lt;br /&gt;&lt;br /&gt;Virtual constructor: Constructors cannot be virtual. Declaring a constructor as a virtual function is a syntax error. &lt;br /&gt;&lt;br /&gt;To be completed .. (Example)&lt;br /&gt;&lt;br /&gt;References&lt;br /&gt;&lt;a href="http://wiki.answers.com/Q/What_are_virtual_destructors"&gt;WikiAnswers&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6912797365202061321-5896113210908872806?l=letuscode.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/UzYPgn1xpocUK-5aMUP0vAgiJuU/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/UzYPgn1xpocUK-5aMUP0vAgiJuU/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/UzYPgn1xpocUK-5aMUP0vAgiJuU/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/UzYPgn1xpocUK-5aMUP0vAgiJuU/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeLearner/~4/6mpp0G8laE0" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/CodeLearner/~3/6mpp0G8laE0/virtual-destructors.html</link><author>noreply@blogger.com (Amit Wadhwa)</author><thr:total>0</thr:total><feedburner:origLink>http://letuscode.blogspot.com/2008/11/virtual-destructors.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6912797365202061321.post-3448782287374097889</guid><pubDate>Thu, 23 Oct 2008 08:07:00 +0000</pubDate><atom:updated>2008-10-23T01:13:46.559-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">algorithms</category><category domain="http://www.blogger.com/atom/ns#">C SC545</category><title>Amortized Analysis</title><description>Amortized analysis refers to finding the average running time per operation over a worst-case sequence of operations. &lt;br /&gt;&lt;br /&gt;The method requires knowledge of which series of operations are possible. This is most commonly the case with data structures, which have state that persists between operations. The basic idea is that a worst case operation can alter the state in such a way that the worst case cannot occur again for a long time, thus "amortizing" its cost.&lt;br /&gt;&lt;br /&gt;As a simple example, in a specific implementation of the dynamic array, we double the size of the array each time it fills up. Because of this, array reallocation may be required, and in the worst case an insertion may require O(n). However, a sequence of n insertions can always be done in O(n) time, so the amortized time per operation is O(n) / n = O(1).&lt;br /&gt;&lt;br /&gt;The approach is going to be to somehow assign an artificial cost to each operation in the sequence.  This artificial cost is called the _amortized cost_ of an operation.  The key property required of amortized cost is that the total real cost of the sequence should be bounded by the total of the amortized costs of all the operations.&lt;br /&gt;&lt;br /&gt;There are several techniques used in amortized analysis:&lt;br /&gt;&lt;br /&gt;    * Aggregate analysis determines the upper bound T(n) on the total cost of a sequence of n operations, then calculates the average cost to be T(n) / n.&lt;br /&gt;&lt;br /&gt;    * Accounting method determines the individual cost of each operation, combining its immediate execution time and its influence on the running time of future operations. Usually, many short-running operations accumulate a "debt" of unfavorable state in small increments, while rare long-running operations decrease it drastically.&lt;br /&gt;&lt;br /&gt;    * Potential method is like the accounting method, but overcharges operations early to compensate for undercharges later.&lt;br /&gt;&lt;br /&gt;References:&lt;br /&gt;http://en.wikipedia.org/wiki/Amortized&lt;br /&gt;http://www.eli.sdsu.edu/courses/fall96/cs660/notes/amortizedAnalysis/amortizedAnalysis.html&lt;br /&gt;http://www.cs.cmu.edu/afs/cs.cmu.edu/academic/class/15750-s03/www/amortized-analysis.txt&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6912797365202061321-3448782287374097889?l=letuscode.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/EHVzPcaolD9tcCK2Ju8y6-dQZyg/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/EHVzPcaolD9tcCK2Ju8y6-dQZyg/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/EHVzPcaolD9tcCK2Ju8y6-dQZyg/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/EHVzPcaolD9tcCK2Ju8y6-dQZyg/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeLearner/~4/VWG5nbDmPgE" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/CodeLearner/~3/VWG5nbDmPgE/amortized-analysis.html</link><author>noreply@blogger.com (Amit Wadhwa)</author><thr:total>0</thr:total><feedburner:origLink>http://letuscode.blogspot.com/2008/10/amortized-analysis.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6912797365202061321.post-2193195601524734836</guid><pubDate>Mon, 15 Sep 2008 01:14:00 +0000</pubDate><atom:updated>2008-09-14T18:24:26.548-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">algorithms</category><title>GCD of 2 numbers</title><description>&lt;p&gt;The algorithm reduces the problem of finding the GCD by repeatedly applying these identities:&lt;/p&gt;&lt;ol&gt;&lt;li&gt;gcd(0, &lt;i&gt;v&lt;/i&gt;) = &lt;i&gt;v&lt;/i&gt;, because everything divides zero, and &lt;i&gt;v&lt;/i&gt; is the largest number that divides &lt;i&gt;v&lt;/i&gt;. Similarly, gcd(&lt;i&gt;u&lt;/i&gt;, 0) = &lt;i&gt;u&lt;/i&gt;. gcd(0, 0) is not defined.&lt;/li&gt;&lt;li&gt;If &lt;i&gt;u&lt;/i&gt; and &lt;i&gt;v&lt;/i&gt; are both even, then gcd(&lt;i&gt;u&lt;/i&gt;, &lt;i&gt;v&lt;/i&gt;) = 2·gcd(&lt;i&gt;u&lt;/i&gt;/2, &lt;i&gt;v&lt;/i&gt;/2), because 2 is a common divisor.&lt;/li&gt;&lt;li&gt;If &lt;i&gt;u&lt;/i&gt; is even and &lt;i&gt;v&lt;/i&gt; is odd, then gcd(&lt;i&gt;u&lt;/i&gt;, &lt;i&gt;v&lt;/i&gt;) = gcd(&lt;i&gt;u&lt;/i&gt;/2, &lt;i&gt;v&lt;/i&gt;), because 2 is not a common divisor. Similarly, if &lt;i&gt;u&lt;/i&gt; is odd and &lt;i&gt;v&lt;/i&gt; is even, then gcd(&lt;i&gt;u&lt;/i&gt;, &lt;i&gt;v&lt;/i&gt;) = gcd(&lt;i&gt;u&lt;/i&gt;, &lt;i&gt;v&lt;/i&gt;/2).&lt;/li&gt;&lt;li&gt;If &lt;i&gt;u&lt;/i&gt; and &lt;i&gt;v&lt;/i&gt; are both odd, and &lt;i&gt;u&lt;/i&gt; ≥ &lt;i&gt;v&lt;/i&gt;, then gcd(&lt;i&gt;u&lt;/i&gt;, &lt;i&gt;v&lt;/i&gt;) = gcd((&lt;i&gt;u&lt;/i&gt;−&lt;i&gt;v&lt;/i&gt;)/2, &lt;i&gt;v&lt;/i&gt;). If both are odd and &lt;i&gt;u&lt;/i&gt; &lt;&lt;i&gt;v&lt;/i&gt;, then gcd(&lt;i&gt;u&lt;/i&gt;, &lt;i&gt;v&lt;/i&gt;) = gcd((&lt;i&gt;v&lt;/i&gt;-&lt;i&gt;u&lt;/i&gt;)/2, &lt;i&gt;u&lt;/i&gt;). These are combinations of one step of the simple Euclidean algorithm, which uses subtraction at each step, and an application of step 3 above. The division by 2 results in an integer because the difference of two odd numbers is even.&lt;/li&gt;&lt;li&gt;Repeat steps 3–4 until &lt;i&gt;u&lt;/i&gt; = &lt;i&gt;v&lt;/i&gt;, or (one more step) until &lt;i&gt;u&lt;/i&gt; = 0. In either case, the result is 2&lt;sup&gt;&lt;i&gt;k&lt;/i&gt;&lt;/sup&gt;&lt;i&gt;v&lt;/i&gt;, where &lt;i&gt;k&lt;/i&gt; is the number of common factors of 2 found in step 2.&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;Since this definition is &lt;a href="http://en.wikipedia.org/wiki/Tail_recursion" title="Tail recursion"&gt;tail-recursive&lt;/a&gt;, a loop can be used to replace the recursion.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre name="code" class="c-sharp"&gt;&lt;br /&gt;unsigned int gcd(unsigned int u, unsigned int v)&lt;br /&gt;{&lt;br /&gt;int shift;&lt;br /&gt;&lt;br /&gt;/* GCD(0,x) := x */&lt;br /&gt;if (u == 0 || v == 0)&lt;br /&gt;  return u | v;&lt;br /&gt;&lt;br /&gt;/* Let shift := lg K, where K is the greatest power of 2&lt;br /&gt;   dividing both u and v. */&lt;br /&gt;for (shift = 0; ((u | v) &amp;amp; 1) == 0; ++shift) {&lt;br /&gt;    u &gt;&gt;= 1;&lt;br /&gt;    v &gt;&gt;= 1;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;while ((u &amp;amp; 1) == 0)&lt;br /&gt;  u &gt;&gt;= 1;&lt;br /&gt;&lt;br /&gt;/* From here on, u is always odd. */&lt;br /&gt;do {&lt;br /&gt;    while ((v &amp;amp; 1) == 0)  /* Loop X */&lt;br /&gt;      v &gt;&gt;= 1;&lt;br /&gt;&lt;br /&gt;    /* Now u and v are both odd, so diff(u, v) is even.&lt;br /&gt;       Let u = min(u, v), v = diff(u, v)/2. */&lt;br /&gt;    if (u &lt;= v) {&lt;br /&gt;             v -= u;        &lt;br /&gt;    } else {                      &lt;br /&gt;             int diff = u - v;&lt;br /&gt;             u = v; &lt;br /&gt;             v = diff;      &lt;br /&gt;    } &lt;br /&gt;    v &gt;&gt;= 1;&lt;br /&gt;    } while (v != 0);&lt;br /&gt;    return u;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6912797365202061321-2193195601524734836?l=letuscode.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/RQfBzL7Iwveso1EsFIOI0jAtAL4/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/RQfBzL7Iwveso1EsFIOI0jAtAL4/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/RQfBzL7Iwveso1EsFIOI0jAtAL4/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/RQfBzL7Iwveso1EsFIOI0jAtAL4/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeLearner/~4/MyHYCubfV38" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/CodeLearner/~3/MyHYCubfV38/gcd-of-2-numbers.html</link><author>noreply@blogger.com (Amit Wadhwa)</author><thr:total>0</thr:total><feedburner:origLink>http://letuscode.blogspot.com/2008/09/gcd-of-2-numbers.html</feedburner:origLink></item></channel></rss>

