<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-6097953565153021725</id><updated>2024-12-18T19:20:41.722-08:00</updated><category term="maths"/><category term="class"/><category term="data structure"/><category term="dp"/><category term="dynamic programming"/><category term="gcd"/><category term="linked list"/><category term="reverse"/><category term="topcoder"/><title type='text'>DCODERS</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://dcoders01.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6097953565153021725/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://dcoders01.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Navneet</name><uri>http://www.blogger.com/profile/08782557583312965286</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>13</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-6097953565153021725.post-4527822857174790973</id><published>2013-09-14T04:30:00.001-07:00</published><updated>2013-09-14T04:30:22.992-07:00</updated><title type='text'>How do I reset a lost administrative password in UBUNTU?</title><content type='html'>
&lt;!DOCTYPE html&gt;
&lt;html style=&quot;bgcolor=white&quot;&gt;
&lt;head&gt;
    &lt;title&gt;How do I reset a lost administrative password in UBUNTU? &lt;/title&gt;
&lt;!--googleon: all--&gt;
&lt;div id=&quot;question-header&quot;&gt;
    &lt;h1&gt;How do I reset a lost administrative password?&lt;/h1&gt;
&lt;/div&gt;

&lt;br/&gt;

&lt;td style=&quot;font = 17px&quot; &gt;

&lt;br/&gt;    
&lt;div class=&quot;post-text&quot;&gt;&lt;p&gt;By default the first user&#39;s account is an administrative account, so if the UI is prompting you for a password it&#39;s probably that person&#39;s user password. If the  user doesn&#39;t remember their password you need to reset it. To do this you need to boot into recovery mode.&lt;/p&gt;

&lt;br/&gt;
&lt;p&gt;Boot up the machine, and &lt;em&gt;after&lt;/em&gt; the BIOS screen, hold down the left &lt;kbd&gt;Shift&lt;/kbd&gt; key. You will then be prompted by a menu that looks something like this: &lt;/p&gt;

&lt;br/&gt;
&lt;p&gt;&lt;img src=&quot;http://i.stack.imgur.com/76OWe.png&quot; alt=&quot;enter image description here&quot;&gt;&lt;/p&gt;

&lt;br/&gt;
&lt;br/&gt;
&lt;p&gt;I&#39;ve noticed on some systems that timing when to hit the left &lt;kbd&gt;Shift&lt;/kbd&gt; key can be tricky, sometimes I miss it and need to try it again.&lt;/p&gt;

&lt;br/&gt;
&lt;p&gt;Hit the down arrow until you select the 2nd entry from the top (the one with the recovery mode in the description) and then hit &lt;kbd&gt;Enter&lt;/kbd&gt;.&lt;/p&gt;

&lt;br/&gt;
&lt;p&gt;Now you should see this menu:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://i.stack.imgur.com/T9BOG.png&quot; alt=&quot;enter image description here&quot;&gt;&lt;/p&gt;

&lt;p&gt;Using the arrow keys scroll down to either root or netroot (doesn&#39;t matter in this case) and then hit &lt;kbd&gt;Enter&lt;/kbd&gt;.&lt;/p&gt;

&lt;br/&gt;
&lt;p&gt;You should now see a root prompt, something like this:&lt;/p&gt;

&lt;br/&gt;
&lt;pre&gt;&lt;code&gt;root@ubuntu:~#
&lt;/code&gt;&lt;/pre&gt;

&lt;br/&gt;
&lt;p&gt;At this stage it is &lt;em&gt;possible&lt;/em&gt; you have a read-only filesystem. You have to remount it with write permissions:&lt;/p&gt;

&lt;br/&gt;
&lt;pre&gt;&lt;code&gt;mount -rw -o remount /
&lt;/code&gt;&lt;/pre&gt;

&lt;br/&gt;
&lt;p&gt;Now we can set the user&#39;s password with the &lt;code&gt;passwd&lt;/code&gt; command. (In this example I will use jorge as the example, you need to substitute whatever the user&#39;s username is):&lt;/p&gt;

&lt;br/&gt;
&lt;pre&gt;&lt;code&gt;passwd navneet

Enter new UNIX password:

Retype new UNIX password:

passwd: password updated successfully

root@ubuntu:~#

&lt;/code&gt;&lt;/pre&gt;

&lt;br/&gt;
&lt;br/&gt;
&lt;p&gt;Type in what you want the new password to be at the prompt. After it&#39;s successful reboot the machine and the user will be able to log in with their new password.&lt;/p&gt;

&lt;ul&gt;

&lt;br/&gt;
&lt;br/&gt;
&lt;li&gt;&lt;a href=&quot;https://wiki.ubuntu.com/RecoveryMode&quot;&gt;Recovery Mode&lt;/a&gt; documentation&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://help.ubuntu.com/community/LostPassword&quot;&gt;Lost password&lt;/a&gt; documentation&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
    
&lt;/body&gt;
&lt;/html&gt;</content><link rel='replies' type='application/atom+xml' href='http://dcoders01.blogspot.com/feeds/4527822857174790973/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://dcoders01.blogspot.com/2013/09/how-do-i-reset-lost-administrative.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6097953565153021725/posts/default/4527822857174790973'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6097953565153021725/posts/default/4527822857174790973'/><link rel='alternate' type='text/html' href='http://dcoders01.blogspot.com/2013/09/how-do-i-reset-lost-administrative.html' title='How do I reset a lost administrative password in UBUNTU?'/><author><name>Navneet</name><uri>http://www.blogger.com/profile/08782557583312965286</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6097953565153021725.post-6373018365703526450</id><published>2013-09-01T17:46:00.000-07:00</published><updated>2013-09-01T17:46:20.216-07:00</updated><title type='text'>Input Taking Strategies</title><content type='html'>&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01//EN&quot; &quot;http://www.w3.org/TR/html4/strict.dtd&quot;&gt;
&lt;HTML&gt;
&lt;HEAD&gt;
&lt;META http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;
&lt;META http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=8&quot;&gt;
&lt;TITLE&gt;Input Taking Strategies&lt;/TITLE&gt;
&lt;STYLE type=&quot;text/css&quot;&gt;

body {margin-top: 0px;margin-left: 0px;}

#page_1 {position:relative; overflow: hidden;margin: 10px 0px 50px 50px;padding: 0px;border: none;width: 688px;}
#page_1 #id_1 {border:none;margin: 0px 0px 0px 0px;padding: 0px;border:none;width: 688px;overflow: hidden;}
#page_1 #id_2 {border:none;margin: 48px 0px 0px 0px;padding: 0px;border:none;width: 688px;overflow: hidden;}





#page_2 {position:relative; overflow: hidden;margin: 10px 0px 50px 50px;padding: 0px;border: none;width: 688px;}





#page_3 {position:relative; overflow: hidden;margin: 10px 0px 50px 50px;padding: 0px;border: none;width: 688px;}





#page_4 {position:relative; overflow: hidden;margin: 10px 0px 50px 50px;padding: 0px;border: none;width: 688px;}





#page_5 {position:relative; overflow: hidden;margin: 10px 0px 50px 50pxpx;padding: 0px;border: none;width: 688px;}





#page_6 {position:relative; overflow: hidden;margin: 10px 0px 50px -50px;padding: 0px;border: none;width: 688px;}





#page_7 {position:relative; overflow: hidden;margin: 10px 0px 50px 50px;padding: 0px;border: none;width: 688px;}





#page_8 {position:relative; overflow: hidden;margin: 10px 0px 50px 50px;padding: 0px;border: none;width: 688px;}





#page_9 {position:relative; overflow: hidden;margin: 10px 0px 20px 50px;padding: 0px;border: none;width: 688px;}





#page_10 {position:relative; overflow: hidden;margin: 10px 0px 20px 50px;padding: 0px;border: none;width: 688px;}





.ft0{font: 23px &#39;Times New Roman&#39;;line-height: 25px;}
.ft1{font: 12px &#39;Arial&#39;;line-height: 15px;}
.ft2{font: 15px &#39;Arial&#39;;line-height: 17px;}
.ft3{font: 12px &#39;Courier New&#39;;line-height: 15px;}
.ft4{font: 12px &#39;Courier New&#39;;line-height: 13px;}
.ft5{font: 1px &#39;Arial&#39;;line-height: 1px;}
.ft6{font: 12px &#39;Times New Roman&#39;;line-height: 15px;}
.ft7{font: 15px &#39;Times New Roman&#39;;line-height: 17px;}
.ft8{font: 15px &#39;Courier New&#39;;line-height: 17px;}
.ft9{font: 12px &#39;Courier New&#39;;line-height: 14px;}
.ft10{font: 1px &#39;Courier New&#39;;line-height: 1px;}
.ft11{font: 18px &#39;Times New Roman&#39;;line-height: 20px;}
.ft12{font: 15px &#39;Times New Roman&#39;;text-decoration: underline;color: #0000ff;line-height: 17px;}

.p0{text-align: left;padding-left: 168px;margin-top: 0px;margin-bottom: 0px;}
.p1{text-align: left;padding-right: 145px;margin-top: 67px;margin-bottom: 0px;}
.p2{text-align: left;margin-top: 23px;margin-bottom: 0px;}
.p3{text-align: left;margin-top: 17px;margin-bottom: 0px;}
.p4{text-align: left;padding-right: 147px;margin-top: 17px;margin-bottom: 0px;}
.p5{text-align: left;margin-top: 16px;margin-bottom: 0px;}
.p6{text-align: left;padding-left: 45px;margin-top: 17px;margin-bottom: 0px;}
.p7{text-align: left;padding-left: 45px;margin-top: 16px;margin-bottom: 0px;}
.p8{text-align: right;padding-right: 15px;margin-top: 0px;margin-bottom: 0px;white-space: nowrap;}
.p9{text-align: left;padding-left: 30px;margin-top: 0px;margin-bottom: 0px;white-space: nowrap;}
.p10{text-align: left;margin-top: 0px;margin-bottom: 0px;white-space: nowrap;}
.p11{text-align: right;padding-right: 38px;margin-top: 0px;margin-bottom: 0px;white-space: nowrap;}
.p12{text-align: left;margin-top: 0px;margin-bottom: 0px;}
.p13{text-align: left;padding-left: 45px;margin-top: 0px;margin-bottom: 0px;}
.p14{text-align: left;padding-left: 90px;margin-top: 17px;margin-bottom: 0px;}
.p15{text-align: left;margin-top: 49px;margin-bottom: 0px;}
.p16{text-align: left;padding-left: 90px;margin-top: 16px;margin-bottom: 0px;}
.p17{text-align: left;padding-left: 19px;margin-top: 17px;margin-bottom: 0px;}
.p18{text-align: left;padding-right: 153px;margin-top: 17px;margin-bottom: 0px;}
.p19{text-align: left;margin-top: 25px;margin-bottom: 0px;}
.p20{text-align: left;padding-right: 151px;margin-top: 17px;margin-bottom: 0px;}
.p21{text-align: left;padding-right: 144px;margin-top: 17px;margin-bottom: 0px;}
.p22{text-align: left;margin-top: 15px;margin-bottom: 0px;}
.p23{text-align: left;padding-left: 135px;margin-top: 17px;margin-bottom: 0px;}
.p24{text-align: left;padding-left: 90px;margin-top: 0px;margin-bottom: 0px;}
.p25{text-align: left;margin-top: 48px;margin-bottom: 0px;}
.p26{text-align: left;padding-left: 68px;margin-top: 17px;margin-bottom: 0px;}
.p27{text-align: left;padding-left: 135px;margin-top: 18px;margin-bottom: 0px;}
.p28{text-align: left;margin-top: 57px;margin-bottom: 0px;}
.p29{text-align: left;margin-top: 18px;margin-bottom: 0px;}
.p30{text-align: left;padding-left: 45px;margin-top: 18px;margin-bottom: 0px;}
.p31{text-align: left;padding-left: 173px;margin-top: 0px;margin-bottom: 0px;}
.p32{text-align: right;padding-right: 7px;margin-top: 0px;margin-bottom: 0px;white-space: nowrap;}
.p33{text-align: left;padding-left: 22px;margin-top: 0px;margin-bottom: 0px;white-space: nowrap;}
.p34{text-align: left;padding-left: 443px;margin-top: 16px;margin-bottom: 0px;}
.p35{text-align: left;padding-left: 128px;margin-top: 1px;margin-bottom: 0px;}
.p36{text-align: right;padding-right: 154px;margin-top: 16px;margin-bottom: 0px;}
.p37{text-align: left;padding-left: 128px;margin-top: 0px;margin-bottom: 0px;}
.p38{text-align: left;padding-left: 173px;margin-top: 16px;margin-bottom: 0px;}
.p39{text-align: left;padding-left: 218px;margin-top: 17px;margin-bottom: 0px;}
.p40{text-align: left;padding-left: 218px;margin-top: 16px;margin-bottom: 0px;}
.p41{text-align: left;padding-left: 173px;margin-top: 17px;margin-bottom: 0px;}
.p42{text-align: left;padding-left: 128px;margin-top: 48px;margin-bottom: 0px;}
.p43{text-align: left;padding-left: 173px;margin-top: 48px;margin-bottom: 0px;}
.p44{text-align: left;padding-right: 157px;margin-top: 16px;margin-bottom: 0px;}
.p45{text-align: left;padding-left: 45px;margin-top: 0px;margin-bottom: 0px;white-space: nowrap;}
.p46{text-align: right;padding-right: 4px;margin-top: 0px;margin-bottom: 0px;white-space: nowrap;}
.p47{text-align: left;padding-left: 4px;margin-top: 0px;margin-bottom: 0px;white-space: nowrap;}
.p48{text-align: left;padding-right: 153px;margin-top: 18px;margin-bottom: 0px;}
.p49{text-align: left;padding-right: 141px;margin-top: 17px;margin-bottom: 0px;}
.p50{text-align: left;margin-top: 29px;margin-bottom: 0px;}
.p51{text-align: right;padding-right: 34px;margin-top: 0px;margin-bottom: 0px;white-space: nowrap;}
.p52{text-align: left;padding-left: 34px;margin-top: 0px;margin-bottom: 0px;white-space: nowrap;}
.p53{text-align: left;padding-left: 49px;margin-top: 0px;margin-bottom: 0px;white-space: nowrap;}
.p54{text-align: left;padding-left: 94px;margin-top: 0px;margin-bottom: 0px;white-space: nowrap;}
.p55{text-align: left;padding-right: 158px;margin-top: 17px;margin-bottom: 0px;}
.p56{text-align: left;padding-left: 37px;margin-top: 17px;margin-bottom: 0px;}
.p57{text-align: left;padding-left: 135px;margin-top: 16px;margin-bottom: 0px;}
.p58{text-align: left;padding-right: 161px;margin-top: 16px;margin-bottom: 0px;}
.p59{text-align: left;padding-right: 148px;margin-top: 16px;margin-bottom: 0px;}
.p60{text-align: left;padding-left: 45px;margin-top: 48px;margin-bottom: 0px;}
.p61{text-align: left;padding-left: 218px;margin-top: 0px;margin-bottom: 0px;}
.p62{text-align: left;padding-left: 263px;margin-top: 17px;margin-bottom: 0px;}
.p63{text-align: left;padding-left: 128px;padding-right: 176px;margin-top: 49px;margin-bottom: 0px;}
.p64{text-align: right;padding-right: 144px;margin-top: 17px;margin-bottom: 0px;}
.p65{text-align: left;padding-left: 125px;margin-top: 2px;margin-bottom: 0px;}
.p66{text-align: left;padding-left: 125px;margin-top: 9px;margin-bottom: 0px;}

.td0{padding: 0px;margin: 0px;width: 53px;vertical-align: bottom;}
.td1{padding: 0px;margin: 0px;width: 90px;vertical-align: bottom;}
.td2{padding: 0px;margin: 0px;width: 150px;vertical-align: bottom;}
.td3{padding: 0px;margin: 0px;width: 143px;vertical-align: bottom;}
.td4{padding: 0px;margin: 0px;width: 233px;vertical-align: bottom;}
.td5{padding: 0px;margin: 0px;width: 203px;vertical-align: bottom;}
.td6{padding: 0px;margin: 0px;width: 41px;vertical-align: bottom;}
.td7{padding: 0px;margin: 0px;width: 26px;vertical-align: bottom;}
.td8{padding: 0px;margin: 0px;width: 30px;vertical-align: bottom;}
.td9{padding: 0px;margin: 0px;width: 31px;vertical-align: bottom;}
.td10{padding: 0px;margin: 0px;width: 45px;vertical-align: bottom;}
.td11{padding: 0px;margin: 0px;width: 22px;vertical-align: bottom;}
.td12{padding: 0px;margin: 0px;width: 71px;vertical-align: bottom;}
.td13{padding: 0px;margin: 0px;width: 380px;vertical-align: bottom;}

.tr0{height: 15px;}
.tr1{height: 32px;}
.tr2{height: 36px;}
.tr3{height: 19px;}
.tr4{height: 31px;}

.t0{width: 143px;margin-left: 37px;margin-top: 13px;font: 12px &#39;Courier New&#39;;}
.t1{width: 526px;margin-left: 128px;margin-top: 44px;font: 12px &#39;Courier New&#39;;}
.t2{width: 398px;margin-top: 13px;font: 12px &#39;Courier New&#39;;}
.t3{width: 451px;margin-left: 15px;margin-top: 12px;font: 12px &#39;Courier New&#39;;}

&lt;/STYLE&gt;
&lt;/HEAD&gt;

&lt;BODY&gt;
&lt;DIV id=&quot;page_1&quot;&gt;


&lt;DIV id=&quot;id_1&quot;&gt;
&lt;P class=&quot;p0 ft0&quot;&gt;Input Taking Strategies&lt;/P&gt;
&lt;P class=&quot;p1 ft1&quot;&gt;Judging by some of the submissions we receive most people seem to be getting the programming approach right on many of the problems, but fumble when it comes to handling the sometimes tricky input descriptions that the problems have. Here we shall see methods to handle some of the more common (and uncommon) input specifications of problems.&lt;/P&gt;
&lt;P class=&quot;p2 ft2&quot;&gt;I. Single test case, number of inputs specified&lt;/P&gt;
&lt;P class=&quot;p3 ft1&quot;&gt;This is as simple as it can get; a single test case with the number of inputs following specified.&lt;/P&gt;
&lt;P class=&quot;p3 ft1&quot;&gt;Example:&lt;/P&gt;
&lt;P class=&quot;p4 ft1&quot;&gt;Input consists of a single test case, beginning with a number N, specifying the number of inputs to follow.&lt;/P&gt;
&lt;P class=&quot;p3 ft1&quot;&gt;Sample Input:&lt;/P&gt;
&lt;P class=&quot;p3 ft1&quot;&gt;5&lt;/P&gt;
&lt;P class=&quot;p3 ft1&quot;&gt;1&lt;/P&gt;
&lt;P class=&quot;p3 ft1&quot;&gt;2&lt;/P&gt;
&lt;P class=&quot;p5 ft1&quot;&gt;3&lt;/P&gt;
&lt;P class=&quot;p3 ft1&quot;&gt;4&lt;/P&gt;
&lt;P class=&quot;p3 ft1&quot;&gt;5&lt;/P&gt;
&lt;P class=&quot;p3 ft3&quot;&gt;[C++ Code]&lt;/P&gt;
&lt;P class=&quot;p6 ft3&quot;&gt;int N = 0;&lt;/P&gt;
&lt;P class=&quot;p6 ft3&quot;&gt;cin &amp;gt;&amp;gt; N;&lt;/P&gt;
&lt;P class=&quot;p6 ft3&quot;&gt;for (int i = 0; i &amp;lt; N; i++)&lt;/P&gt;
&lt;P class=&quot;p7 ft3&quot;&gt;{&lt;/P&gt;
&lt;TABLE cellpadding=0 cellspacing=0 class=&quot;t0&quot;&gt;
&lt;TR&gt;
 &lt;TD class=&quot;tr0 td0&quot;&gt;&lt;P class=&quot;p8 ft4&quot;&gt;&amp;bsp;&lt;/P&gt;&lt;/TD&gt;
 &lt;TD class=&quot;tr0 td1&quot;&gt;&lt;P class=&quot;p9 ft4&quot;&gt;int val;&lt;/P&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
 &lt;TD class=&quot;tr1 td0&quot;&gt;&lt;P class=&quot;p10 ft5&quot;&gt;&amp;nbsp;&lt;/P&gt;&lt;/TD&gt;
 &lt;TD class=&quot;tr1 td1&quot;&gt;&lt;P class=&quot;p10 ft4&quot;&gt;cin &amp;gt;&amp;gt; val;&lt;/P&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
 &lt;TD class=&quot;tr2 td0&quot;&gt;&lt;P class=&quot;p11 ft3&quot;&gt;}&lt;/P&gt;&lt;/TD&gt;
 &lt;TD class=&quot;tr2 td1&quot;&gt;&lt;P class=&quot;p10 ft5&quot;&gt;&amp;nbsp;&lt;/P&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;DIV id=&quot;id_2&quot;&gt;
&lt;P class=&quot;p12 ft3&quot;&gt;[C++ Code]&lt;/P&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;DIV id=&quot;page_2&quot;&gt;


&lt;P class=&quot;p13 ft3&quot;&gt;int N = 0;&lt;/P&gt;
&lt;P class=&quot;p6 ft3&quot;&gt;cin &amp;gt;&amp;gt; N;&lt;/P&gt;
&lt;P class=&quot;p7 ft3&quot;&gt;while &lt;NOBR&gt;(N--)&lt;/NOBR&gt;&lt;/P&gt;
&lt;P class=&quot;p6 ft3&quot;&gt;{&lt;/P&gt;
&lt;P class=&quot;p14 ft3&quot;&gt;int val;&lt;/P&gt;
&lt;P class=&quot;p14 ft3&quot;&gt;cin &amp;gt;&amp;gt; val;&lt;/P&gt;
&lt;P class=&quot;p7 ft3&quot;&gt;}&lt;/P&gt;
&lt;P class=&quot;p15 ft3&quot;&gt;[C Code]&lt;/P&gt;
&lt;P class=&quot;p6 ft3&quot;&gt;int N = 0;&lt;/P&gt;
&lt;P class=&quot;p6 ft3&quot;&gt;scanf(&quot;%d&quot;, &amp;N);&lt;/P&gt;
&lt;P class=&quot;p7 ft3&quot;&gt;for (int i = 0; i &amp;lt; N; i++)&lt;/P&gt;
&lt;P class=&quot;p6 ft3&quot;&gt;{&lt;/P&gt;
&lt;P class=&quot;p14 ft3&quot;&gt;int val;&lt;/P&gt;
&lt;P class=&quot;p16 ft3&quot;&gt;scanf(&quot;%d&quot;, &amp;val);&lt;/P&gt;
&lt;P class=&quot;p6 ft3&quot;&gt;}&lt;/P&gt;
&lt;P class=&quot;p15 ft3&quot;&gt;[C Code]&lt;/P&gt;
&lt;P class=&quot;p7 ft3&quot;&gt;int N = 0;&lt;/P&gt;
&lt;P class=&quot;p17 ft6&quot;&gt;scanf(&quot;%d&quot;, &amp;N);&lt;/P&gt;
&lt;P class=&quot;p6 ft3&quot;&gt;while &lt;NOBR&gt;(N--)&lt;/NOBR&gt;&lt;/P&gt;
&lt;P class=&quot;p6 ft3&quot;&gt;{&lt;/P&gt;
&lt;P class=&quot;p14 ft3&quot;&gt;int val;&lt;/P&gt;
&lt;P class=&quot;p16 ft3&quot;&gt;scanf(&quot;%d&quot;, &amp;N);&lt;/P&gt;
&lt;P class=&quot;p6 ft3&quot;&gt;}&lt;/P&gt;
&lt;P class=&quot;p18 ft1&quot;&gt;Note that cin and scanf ignore whitespace. So the above code would work even if more than one input were on the same line, as follows:&lt;/P&gt;
&lt;/DIV&gt;
&lt;DIV id=&quot;page_3&quot;&gt;


&lt;P class=&quot;p12 ft1&quot;&gt;Sample Input&lt;/P&gt;
&lt;P class=&quot;p3 ft1&quot;&gt;5 1 2 3&lt;/P&gt;
&lt;P class=&quot;p3 ft1&quot;&gt;4 5&lt;/P&gt;
&lt;P class=&quot;p19 ft2&quot;&gt;II. Multiple test case, number of inputs for each test case specified&lt;/P&gt;
&lt;P class=&quot;p18 ft1&quot;&gt;In this case, each test case specified the number of inputs in it at the beginning, but there can be multiple test cases, the count of which is not specified.&lt;/P&gt;
&lt;P class=&quot;p5 ft1&quot;&gt;Example:&lt;/P&gt;
&lt;P class=&quot;p20 ft1&quot;&gt;Input consists of a multiple test cases, each beginning with a number N, specifying the number of inputs in that test case.&lt;/P&gt;
&lt;P class=&quot;p3 ft1&quot;&gt;Sample Input:&lt;/P&gt;
&lt;P class=&quot;p3 ft1&quot;&gt;3&lt;/P&gt;
&lt;P class=&quot;p3 ft1&quot;&gt;1&lt;/P&gt;
&lt;P class=&quot;p3 ft1&quot;&gt;2&lt;/P&gt;
&lt;P class=&quot;p3 ft1&quot;&gt;3&lt;/P&gt;
&lt;P class=&quot;p5 ft1&quot;&gt;2&lt;/P&gt;
&lt;P class=&quot;p3 ft1&quot;&gt;4&lt;/P&gt;
&lt;P class=&quot;p3 ft1&quot;&gt;5&lt;/P&gt;
&lt;P class=&quot;p21 ft1&quot;&gt;Here, a test case may be followed by either another test case, or EOF &lt;NOBR&gt;(End-Of-File),&lt;/NOBR&gt; which is a special character that signals the end of the input (on most implementations it is &lt;NOBR&gt;-1).&lt;/NOBR&gt; You can send EOF on the console with &lt;NOBR&gt;Ctrl-Z.&lt;/NOBR&gt; So what we have to do here is take input until EOF is encountered. Let’s see this in action:&lt;/P&gt;
&lt;P class=&quot;p22 ft3&quot;&gt;[C++ Code]&lt;/P&gt;
&lt;P class=&quot;p6 ft3&quot;&gt;int N = 0;&lt;/P&gt;
&lt;P class=&quot;p6 ft3&quot;&gt;while (cin &amp;gt;&amp;gt; N)&lt;/P&gt;
&lt;P class=&quot;p6 ft3&quot;&gt;{&lt;/P&gt;
&lt;P class=&quot;p16 ft3&quot;&gt;for (int i = 0; i &amp;lt; N; i++)&lt;/P&gt;
&lt;P class=&quot;p14 ft3&quot;&gt;{&lt;/P&gt;
&lt;P class=&quot;p23 ft3&quot;&gt;int val = 0;&lt;/P&gt;
&lt;P class=&quot;p23 ft3&quot;&gt;cin &amp;gt;&amp;gt; val;&lt;/P&gt;
&lt;/DIV&gt;
&lt;DIV id=&quot;page_4&quot;&gt;


&lt;P class=&quot;p24 ft3&quot;&gt;}&lt;/P&gt;
&lt;P class=&quot;p6 ft3&quot;&gt;}&lt;/P&gt;
&lt;P class=&quot;p25 ft3&quot;&gt;[C Code]&lt;/P&gt;
&lt;P class=&quot;p6 ft3&quot;&gt;int N = 0;&lt;/P&gt;
&lt;P class=&quot;p6 ft3&quot;&gt;while (scanf(&quot;%d&quot;, &amp;N) != EOF)&lt;/P&gt;
&lt;P class=&quot;p6 ft3&quot;&gt;{&lt;/P&gt;
&lt;P class=&quot;p16 ft3&quot;&gt;for (int i = 0; i &amp;lt; N; i++)&lt;/P&gt;
&lt;P class=&quot;p14 ft3&quot;&gt;{&lt;/P&gt;
&lt;P class=&quot;p26 ft7&quot;&gt;int val = 0;&lt;/P&gt;
&lt;P class=&quot;p27 ft3&quot;&gt;scanf(&quot;%d&quot;, &amp;val);&lt;/P&gt;
&lt;P class=&quot;p16 ft3&quot;&gt;}&lt;/P&gt;
&lt;P class=&quot;p6 ft3&quot;&gt;}&lt;/P&gt;
&lt;P class=&quot;p28 ft2&quot;&gt;III. Inputting strings - whitespace separated words&lt;/P&gt;
&lt;P class=&quot;p20 ft1&quot;&gt;Handling strings can often be tricky. Let’s consider the case where we simply want to input space separated strings (words).&lt;/P&gt;
&lt;P class=&quot;p5 ft1&quot;&gt;Example:&lt;/P&gt;
&lt;P class=&quot;p3 ft1&quot;&gt;Input consists of a number of space separated words.&lt;/P&gt;
&lt;P class=&quot;p3 ft1&quot;&gt;Sample Input:&lt;/P&gt;
&lt;P class=&quot;p3 ft1&quot;&gt;Association for Computing Machinery&lt;/P&gt;
&lt;P class=&quot;p3 ft3&quot;&gt;[C++ Code]&lt;/P&gt;
&lt;P class=&quot;p6 ft3&quot;&gt;char str[64] = { 0 };&lt;/P&gt;
&lt;P class=&quot;p6 ft3&quot;&gt;while (cin &amp;gt;&amp;gt; str)&lt;/P&gt;
&lt;P class=&quot;p16 ft3&quot;&gt;cout &amp;lt;&amp;lt; str &amp;lt;&amp;lt; endl;&lt;/P&gt;
&lt;P class=&quot;p15 ft3&quot;&gt;[C Code]&lt;/P&gt;
&lt;/DIV&gt;
&lt;DIV id=&quot;page_5&quot;&gt;


&lt;P class=&quot;p13 ft3&quot;&gt;char str[64] = { 0 };&lt;/P&gt;
&lt;P class=&quot;p6 ft3&quot;&gt;while (scanf(&quot;%s&quot;, str))&lt;/P&gt;
&lt;P class=&quot;p16 ft3&quot;&gt;printf(&quot;%s\n&quot;, str);&lt;/P&gt;
&lt;P class=&quot;p28 ft2&quot;&gt;IV. Inputting strings - lines of strings&lt;/P&gt;
&lt;P class=&quot;p29 ft1&quot;&gt;In this case you have to read each line of text, terminated by &#39;\n&#39;.&lt;/P&gt;
&lt;P class=&quot;p3 ft1&quot;&gt;Example:&lt;/P&gt;
&lt;P class=&quot;p5 ft1&quot;&gt;Input consists of multiple lines of text.&lt;/P&gt;
&lt;P class=&quot;p3 ft1&quot;&gt;Sample Input:&lt;/P&gt;
&lt;P class=&quot;p3 ft1&quot;&gt;Association for Computing Machinery&lt;/P&gt;
&lt;P class=&quot;p3 ft1&quot;&gt;a club dedicated&lt;/P&gt;
&lt;P class=&quot;p3 ft1&quot;&gt;to the promotion of computing&lt;/P&gt;
&lt;P class=&quot;p3 ft6&quot;&gt;as a profession and&lt;/P&gt;
&lt;P class=&quot;p3 ft1&quot;&gt;science&lt;/P&gt;
&lt;P class=&quot;p3 ft3&quot;&gt;[C++ Code]&lt;/P&gt;
&lt;P class=&quot;p6 ft3&quot;&gt;char str[64] = { 0 };&lt;/P&gt;
&lt;P class=&quot;p6 ft3&quot;&gt;while (cin.getline(str, 64))&lt;/P&gt;
&lt;P class=&quot;p16 ft3&quot;&gt;cout &amp;lt;&amp;lt; str &amp;lt;&amp;lt; endl;&lt;/P&gt;
&lt;P class=&quot;p15 ft3&quot;&gt;[C Code]&lt;/P&gt;
&lt;P class=&quot;p6 ft3&quot;&gt;char str[64] = { 0 };&lt;/P&gt;
&lt;P class=&quot;p7 ft3&quot;&gt;while (fgets(str, 64, stdin)) // fgets appends the &#39;\n&#39; to the&lt;/P&gt;
&lt;P class=&quot;p12 ft3&quot;&gt;string&lt;/P&gt;
&lt;P class=&quot;p16 ft3&quot;&gt;printf(&quot;%s&quot;, str);&lt;/P&gt;
&lt;P class=&quot;p3 ft1&quot;&gt;Sometimes you may find it useful to read a line of text one character at a time...&lt;/P&gt;
&lt;P class=&quot;p3 ft8&quot;&gt;[C++ Code]&lt;/P&gt;
&lt;P class=&quot;p30 ft3&quot;&gt;char str[64] = { 0 };&lt;/P&gt;
&lt;/DIV&gt;
&lt;DIV id=&quot;page_6&quot;&gt;


&lt;P class=&quot;p31 ft3&quot;&gt;int inp = 0;&lt;/P&gt;
&lt;TABLE cellpadding=0 cellspacing=0 class=&quot;t1&quot;&gt;
&lt;TR&gt;
 &lt;TD class=&quot;tr0 td2&quot;&gt;&lt;P class=&quot;p32 ft4&quot;&gt;while ((inp =&lt;/P&gt;&lt;/TD&gt;
 &lt;TD class=&quot;tr0 td3&quot;&gt;&lt;P class=&quot;p10 ft4&quot;&gt;cin.get()) != EOF)&lt;/P&gt;&lt;/TD&gt;
 &lt;TD class=&quot;tr0 td4&quot;&gt;&lt;P class=&quot;p33 ft4&quot;&gt;// There is a reason why int&lt;/P&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
 &lt;TD class=&quot;tr3 td2&quot;&gt;&lt;P class=&quot;p32 ft3&quot;&gt;is used where there&lt;/P&gt;&lt;/TD&gt;
 &lt;TD class=&quot;tr3 td3&quot;&gt;&lt;P class=&quot;p10 ft3&quot;&gt;can be a potential&lt;/P&gt;&lt;/TD&gt;
 &lt;TD class=&quot;tr3 td4&quot;&gt;&lt;P class=&quot;p10 ft3&quot;&gt;EOF&lt;/P&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TABLE&gt;

&lt;P class=&quot;p34 ft3&quot;&gt;// The reason is given in&lt;/P&gt;
&lt;P class=&quot;p37 ft3&quot;&gt;Kernighan &amp; Ritchie&lt;/P&gt;
&lt;P class=&quot;p38 ft3&quot;&gt;{&lt;/P&gt;
&lt;P class=&quot;p39 ft3&quot;&gt;int i = 0;&lt;/P&gt;
&lt;P class=&quot;p39 ft3&quot;&gt;str[i] = inp;&lt;/P&gt;
&lt;P class=&quot;p40 ft3&quot;&gt;while ((str[++i] = cin.get()) != &#39;\n&#39;);&lt;/P&gt;
&lt;P class=&quot;p39 ft3&quot;&gt;str[i] = 0;&lt;/P&gt;
&lt;P class=&quot;p39 ft3&quot;&gt;cout &amp;lt;&amp;lt; str &amp;lt;&amp;lt; endl;&lt;/P&gt;
&lt;P class=&quot;p41 ft3&quot;&gt;}&lt;/P&gt;
&lt;P class=&quot;p42 ft3&quot;&gt;[C Code]&lt;/P&gt;
&lt;P class=&quot;p41 ft3&quot;&gt;char str[64] = { 0 };&lt;/P&gt;
&lt;P class=&quot;p41 ft3&quot;&gt;int inp = 0;&lt;/P&gt;
&lt;P class=&quot;p43 ft3&quot;&gt;while ((inp = getchar()) != EOF)&lt;/P&gt;
&lt;P class=&quot;p41 ft3&quot;&gt;{&lt;/P&gt;
&lt;P class=&quot;p39 ft3&quot;&gt;int i = 0;&lt;/P&gt;
&lt;P class=&quot;p40 ft3&quot;&gt;str[i] = inp;&lt;/P&gt;
&lt;P class=&quot;p39 ft3&quot;&gt;while ((str[++i] = getchar()) != &#39;\n&#39;);&lt;/P&gt;
&lt;P class=&quot;p39 ft3&quot;&gt;str[i] = 0;&lt;/P&gt;
&lt;P class=&quot;p39 ft3&quot;&gt;cout &amp;lt;&amp;lt; str &amp;lt;&amp;lt; endl;&lt;/P&gt;
&lt;/DIV&gt;
&lt;DIV id=&quot;page_7&quot;&gt;


&lt;P class=&quot;p13 ft3&quot;&gt;}&lt;/P&gt;
&lt;P class=&quot;p28 ft2&quot;&gt;V. Reading formatted input&lt;/P&gt;
&lt;P class=&quot;p4 ft1&quot;&gt;Often you get input specifications where the input is given in a particular format. For example, you may have to read a date given in the following format: dd/mm/yyyy&lt;/P&gt;
&lt;P class=&quot;p44 ft1&quot;&gt;C comes in very handy for the above situation. Note that you can use the powerful formatted I/O functions of C in C++ by including &amp;lt;cstdio&amp;gt;.&lt;/P&gt;
&lt;TABLE cellpadding=0 cellspacing=0 class=&quot;t2&quot;&gt;
&lt;TR&gt;
 &lt;TD class=&quot;tr0 td5&quot;&gt;&lt;P class=&quot;p10 ft4&quot;&gt;[C/C++ Code]&lt;/P&gt;&lt;/TD&gt;
 &lt;TD class=&quot;tr0 td6&quot;&gt;&lt;P class=&quot;p10 ft5&quot;&gt;&amp;nbsp;&lt;/P&gt;&lt;/TD&gt;
 &lt;TD class=&quot;tr0 td7&quot;&gt;&lt;P class=&quot;p10 ft5&quot;&gt;&amp;nbsp;&lt;/P&gt;&lt;/TD&gt;
 &lt;TD class=&quot;tr0 td8&quot;&gt;&lt;P class=&quot;p10 ft5&quot;&gt;&amp;nbsp;&lt;/P&gt;&lt;/TD&gt;
 &lt;TD class=&quot;tr0 td9&quot;&gt;&lt;P class=&quot;p10 ft5&quot;&gt;&amp;nbsp;&lt;/P&gt;&lt;/TD&gt;
 &lt;TD class=&quot;tr0 td10&quot;&gt;&lt;P class=&quot;p10 ft5&quot;&gt;&amp;nbsp;&lt;/P&gt;&lt;/TD&gt;
 &lt;TD class=&quot;tr0 td11&quot;&gt;&lt;P class=&quot;p10 ft5&quot;&gt;&amp;nbsp;&lt;/P&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
 &lt;TD class=&quot;tr4 td5&quot;&gt;&lt;P class=&quot;p45 ft4&quot;&gt;#include &amp;lt;cstdio&amp;gt;&lt;/P&gt;&lt;/TD&gt;
 &lt;TD class=&quot;tr4 td6&quot;&gt;&lt;P class=&quot;p46 ft4&quot;&gt;//&lt;/P&gt;&lt;/TD&gt;
 &lt;TD class=&quot;tr4 td7&quot;&gt;&lt;P class=&quot;p47 ft4&quot;&gt;If&lt;/P&gt;&lt;/TD&gt;
 &lt;TD class=&quot;tr4 td8&quot;&gt;&lt;P class=&quot;p10 ft4&quot;&gt;you&lt;/P&gt;&lt;/TD&gt;
 &lt;TD class=&quot;tr4 td9&quot;&gt;&lt;P class=&quot;p10 ft4&quot;&gt;are&lt;/P&gt;&lt;/TD&gt;
 &lt;TD class=&quot;tr4 td10&quot;&gt;&lt;P class=&quot;p10 ft4&quot;&gt;using&lt;/P&gt;&lt;/TD&gt;
 &lt;TD class=&quot;tr4 td11&quot;&gt;&lt;P class=&quot;p10 ft4&quot;&gt;C++&lt;/P&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
 &lt;TD class=&quot;tr1 td5&quot;&gt;&lt;P class=&quot;p45 ft4&quot;&gt;#include &amp;lt;stdio.h&amp;gt;&lt;/P&gt;&lt;/TD&gt;
 &lt;TD class=&quot;tr1 td6&quot;&gt;&lt;P class=&quot;p46 ft4&quot;&gt;//&lt;/P&gt;&lt;/TD&gt;
 &lt;TD class=&quot;tr1 td7&quot;&gt;&lt;P class=&quot;p47 ft4&quot;&gt;If&lt;/P&gt;&lt;/TD&gt;
 &lt;TD class=&quot;tr1 td8&quot;&gt;&lt;P class=&quot;p10 ft4&quot;&gt;you&lt;/P&gt;&lt;/TD&gt;
 &lt;TD class=&quot;tr1 td9&quot;&gt;&lt;P class=&quot;p10 ft4&quot;&gt;are&lt;/P&gt;&lt;/TD&gt;
 &lt;TD class=&quot;tr1 td10&quot;&gt;&lt;P class=&quot;p10 ft4&quot;&gt;using&lt;/P&gt;&lt;/TD&gt;
 &lt;TD class=&quot;tr1 td11&quot;&gt;&lt;P class=&quot;p10 ft4&quot;&gt;C&lt;/P&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
 &lt;TD class=&quot;tr2 td5&quot;&gt;&lt;P class=&quot;p45 ft3&quot;&gt;...&lt;/P&gt;&lt;/TD&gt;
 &lt;TD class=&quot;tr2 td6&quot;&gt;&lt;P class=&quot;p10 ft5&quot;&gt;&amp;nbsp;&lt;/P&gt;&lt;/TD&gt;
 &lt;TD class=&quot;tr2 td7&quot;&gt;&lt;P class=&quot;p10 ft5&quot;&gt;&amp;nbsp;&lt;/P&gt;&lt;/TD&gt;
 &lt;TD class=&quot;tr2 td8&quot;&gt;&lt;P class=&quot;p10 ft5&quot;&gt;&amp;nbsp;&lt;/P&gt;&lt;/TD&gt;
 &lt;TD class=&quot;tr2 td9&quot;&gt;&lt;P class=&quot;p10 ft5&quot;&gt;&amp;nbsp;&lt;/P&gt;&lt;/TD&gt;
 &lt;TD class=&quot;tr2 td10&quot;&gt;&lt;P class=&quot;p10 ft5&quot;&gt;&amp;nbsp;&lt;/P&gt;&lt;/TD&gt;
 &lt;TD class=&quot;tr2 td11&quot;&gt;&lt;P class=&quot;p10 ft5&quot;&gt;&amp;nbsp;&lt;/P&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TABLE&gt;
&lt;P class=&quot;p6 ft3&quot;&gt;int d, m, y;&lt;/P&gt;
&lt;P class=&quot;p7 ft3&quot;&gt;scanf(&quot;%d/%d/%d&quot;, &amp;d, &amp;m, &amp;y);&lt;/P&gt;
&lt;P class=&quot;p28 ft2&quot;&gt;VI. Really Tricky Inputs&lt;/P&gt;
&lt;P class=&quot;p3 ft1&quot;&gt;Example:&lt;/P&gt;
&lt;P class=&quot;p48 ft1&quot;&gt;Input consists of multiple test cases. Each test case consists of a list of numbers on a single line. The count of numbers per line is not specified and may vary from line to line.&lt;/P&gt;
&lt;P class=&quot;p5 ft1&quot;&gt;Sample Input:&lt;/P&gt;
&lt;P class=&quot;p3 ft1&quot;&gt;1 2 3 4 5&lt;/P&gt;
&lt;P class=&quot;p3 ft1&quot;&gt;6 7 8&lt;/P&gt;
&lt;P class=&quot;p3 ft1&quot;&gt;9 10 11 23&lt;/P&gt;
&lt;P class=&quot;p3 ft1&quot;&gt;100 500 600&lt;/P&gt;
&lt;P class=&quot;p49 ft1&quot;&gt;This one is really tricky. If you are not familiar with some of the less known functions of &amp;t;iostream&amp;gt; or &amp;lt;stdio.h&amp;gt;, your first approach might be to read one character at a time, if it is a digit you have got the beginning of a new number and continue reading characters and appending to the number till you hit whitespace, skip all the whitespace, read next digit, and so on... until you read &#39;\n&#39; which indicates the end of a test case. This really dirty approach might look something like this:&lt;/P&gt;
&lt;P class=&quot;p50 ft3&quot;&gt;[C Code]&lt;/P&gt;
&lt;/DIV&gt;
&lt;DIV id=&quot;page_8&quot;&gt;


&lt;P class=&quot;p13 ft3&quot;&gt;int N = 0;&lt;/P&gt;
&lt;P class=&quot;p6 ft3&quot;&gt;int numbers[64] = { 0 };&lt;/P&gt;
&lt;P class=&quot;p7 ft3&quot;&gt;int current = 0;&lt;/P&gt;
&lt;P class=&quot;p6 ft3&quot;&gt;int temp = 0;&lt;/P&gt;
&lt;P class=&quot;p6 ft3&quot;&gt;while ((current = getchar()) != EOF)&lt;/P&gt;
&lt;P class=&quot;p6 ft3&quot;&gt;{&lt;/P&gt;
&lt;TABLE cellpadding=0 cellspacing=0 class=&quot;t3&quot;&gt;
&lt;TR&gt;
 &lt;TD class=&quot;tr0 td12&quot;&gt;&lt;P class=&quot;p10 ft10&quot;&gt;&amp;nbsp;&lt;/P&gt;&lt;/TD&gt;
 &lt;TD class=&quot;tr0 td13&quot;&gt;&lt;P class=&quot;p47 ft4&quot;&gt;N = 0;&lt;/P&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
 &lt;TD class=&quot;tr1 td12&quot;&gt;&lt;P class=&quot;p10 ft10&quot;&gt;&amp;nbsp;&lt;/P&gt;&lt;/TD&gt;
 &lt;TD class=&quot;tr1 td13&quot;&gt;&lt;P class=&quot;p47 ft4&quot;&gt;do&lt;/P&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
 &lt;TD class=&quot;tr1 td12&quot;&gt;&lt;P class=&quot;p51 ft4&quot;&gt;&amp;nbp;&lt;/P&gt;&lt;/TD&gt;
 &lt;TD class=&quot;tr1 td13&quot;&gt;&lt;P class=&quot;p52 ft4&quot;&gt;{&lt;/P&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
 &lt;TD class=&quot;tr1 td12&quot;&gt;&lt;P class=&quot;p10 ft10&quot;&gt;&amp;nbsp;&lt;/P&gt;&lt;/TD&gt;
 &lt;TD class=&quot;tr1 td13&quot;&gt;&lt;P class=&quot;p53 ft4&quot;&gt;while (!isdigit(current) &amp;&amp; current != &#39;\n&#39;)&lt;/P&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
 &lt;TD class=&quot;tr4 td12&quot;&gt;&lt;P class=&quot;p10 ft10&quot;&gt;&amp;nbsp;&lt;/P&gt;&lt;/TD&gt;
 &lt;TD class=&quot;tr4 td13&quot;&gt;&lt;P class=&quot;p54 ft4&quot;&gt;current = getchar();&lt;/P&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
 &lt;TD class=&quot;tr1 td12&quot;&gt;&lt;P class=&quot;p10 ft10&quot;&gt;&amp;nbsp;&lt;/P&gt;&lt;/TD&gt;
 &lt;TD class=&quot;tr1 td13&quot;&gt;&lt;P class=&quot;p53 ft4&quot;&gt;if (current == &#39;\n&#39;)&lt;/P&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
 &lt;TD class=&quot;tr1 td12&quot;&gt;&lt;P class=&quot;p10 ft10&quot;&gt;&amp;nbsp;&lt;/P&gt;&lt;/TD&gt;
 &lt;TD class=&quot;tr1 td13&quot;&gt;&lt;P class=&quot;p54 ft4&quot;&gt;break;&lt;/P&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
 &lt;TD class=&quot;tr4 td12&quot;&gt;&lt;P class=&quot;p10 ft10&quot;&gt;&amp;nbsp;&lt;/P&gt;&lt;/TD&gt;
 &lt;TD class=&quot;tr4 td13&quot;&gt;&lt;P class=&quot;p53 ft4&quot;&gt;current &lt;NOBR&gt;-=&lt;/NOBR&gt; &#39;0&#39;;&lt;/P&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
 &lt;TD class=&quot;tr1 td12&quot;&gt;&lt;P class=&quot;p10 ft10&quot;&gt;&amp;nbsp;&lt;/P&gt;&lt;/TD&gt;
 &lt;TD class=&quot;tr1 td13&quot;&gt;&lt;P class=&quot;p53 ft4&quot;&gt;while (isdigit(temp = getchar()))&lt;/P&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
 &lt;TD class=&quot;tr1 td12&quot;&gt;&lt;P class=&quot;p10 ft10&quot;&gt;&amp;nbsp;&lt;/P&gt;&lt;/TD&gt;
 &lt;TD class=&quot;tr1 td13&quot;&gt;&lt;P class=&quot;p54 ft4&quot;&gt;current = current * 10 + temp - &#39;0&#39;;&lt;/P&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
 &lt;TD class=&quot;tr1 td12&quot;&gt;&lt;P class=&quot;p10 ft10&quot;&gt;&amp;nbsp;&lt;/P&gt;&lt;/TD&gt;
 &lt;TD class=&quot;tr1 td13&quot;&gt;&lt;P class=&quot;p53 ft4&quot;&gt;numbers[N++] = current;&lt;/P&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
 &lt;TD class=&quot;tr4 td12&quot;&gt;&lt;P class=&quot;p10 ft10&quot;&gt;&amp;nbsp;&lt;/P&gt;&lt;/TD&gt;
 &lt;TD class=&quot;tr4 td13&quot;&gt;&lt;P class=&quot;p47 ft4&quot;&gt;} while (1);&lt;/P&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
 &lt;TD class=&quot;tr2 td12&quot;&gt;&lt;P class=&quot;p51 ft3&quot;&gt;}&lt;/P&gt;&lt;/TD&gt;
 &lt;TD class=&quot;tr2 td13&quot;&gt;&lt;P class=&quot;p10 ft10&quot;&gt;&amp;nbsp;&lt;/P&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TABLE&gt;
&lt;P class=&quot;p55 ft1&quot;&gt;Really ugly, right? Fortunately, C gives us a way to solve this problem very elegantly. Check out the neat code below:&lt;/P&gt;
&lt;P class=&quot;p5 ft3&quot;&gt;[C Code]&lt;/P&gt;
&lt;P class=&quot;p6 ft3&quot;&gt;int numbers[64] = { 0 };&lt;/P&gt;
&lt;P class=&quot;p6 ft3&quot;&gt;char str[128] = { 0 };&lt;/P&gt;
&lt;P class=&quot;p6 ft3&quot;&gt;int N = 0;&lt;/P&gt;
&lt;P class=&quot;p6 ft3&quot;&gt;int len = 0;&lt;/P&gt;
&lt;P class=&quot;p7 ft3&quot;&gt;int n = 0;&lt;/P&gt;
&lt;/DIV&gt;
&lt;DIV id=&quot;page_9&quot;&gt;


&lt;P class=&quot;p13 ft3&quot;&gt;while (fgets(str, 128, stdin))&lt;/P&gt;
&lt;P class=&quot;p7 ft3&quot;&gt;{&lt;/P&gt;
&lt;P class=&quot;p56 ft6&quot;&gt;len = 0;&lt;/P&gt;
&lt;P class=&quot;p14 ft3&quot;&gt;N = 0;&lt;/P&gt;
&lt;P class=&quot;p14 ft3&quot;&gt;while (sscanf(str + len, &quot;%d%n&quot;, &amp;numbers[N], &amp;n) != EOF)&lt;/P&gt;
&lt;P class=&quot;p14 ft3&quot;&gt;{&lt;/P&gt;
&lt;P class=&quot;p23 ft3&quot;&gt;len += n;&lt;/P&gt;
&lt;P class=&quot;p57 ft3&quot;&gt;N++;&lt;/P&gt;
&lt;P class=&quot;p14 ft3&quot;&gt;}&lt;/P&gt;
&lt;P class=&quot;p6 ft3&quot;&gt;}&lt;/P&gt;
&lt;P class=&quot;p58 ft1&quot;&gt;%n is a little known specifier in scanf that returns the number of bytes read as input till the point where it is specified. sscanf is a function that reads input from a specified string. Its output counterpart is sprintf. The rest should be easy to understand.&lt;/P&gt;
&lt;P class=&quot;p59 ft1&quot;&gt;C++ also gives us a way to do this using streams. It is much more elegant and intuitive but we will not be explaining it here as it is beyond the scope of this tutorial. Look up &quot;string&quot; and &quot;stringstream&quot; classes in the STL (Standard Template Library). The code is as follows:&lt;/P&gt;
&lt;P class=&quot;p5 ft3&quot;&gt;[C++ STL Code]&lt;/P&gt;
&lt;P class=&quot;p6 ft3&quot;&gt;#include &amp;lt;string&amp;gt;&lt;/P&gt;
&lt;P class=&quot;p7 ft3&quot;&gt;#include &amp;lt;sstream&amp;gt;&lt;/P&gt;
&lt;P class=&quot;p6 ft3&quot;&gt;...&lt;/P&gt;
&lt;P class=&quot;p6 ft3&quot;&gt;string str;&lt;/P&gt;
&lt;P class=&quot;p6 ft3&quot;&gt;int numbers[64] = { 0 };&lt;/P&gt;
&lt;P class=&quot;p6 ft3&quot;&gt;int N = 0;&lt;/P&gt;
&lt;P class=&quot;p60 ft3&quot;&gt;while (getline(cin, str))&lt;/P&gt;
&lt;P class=&quot;p6 ft3&quot;&gt;{&lt;/P&gt;
&lt;P class=&quot;p16 ft3&quot;&gt;N = 0;&lt;/P&gt;
&lt;P class=&quot;p14 ft3&quot;&gt;stringstream in(str);&lt;/P&gt;

&lt;P class=&quot;p14 ft3&quot;&gt;while (in &amp;gt;&amp;gt; numbers[N])&lt;/P&gt;
&lt;P class=&quot;p14 ft3&quot;&gt;N++;&lt;/P&gt;
&lt;P class=&quot;p6 ft3&quot;&gt;}&lt;/P&gt;
&lt;P class=&quot;p5 ft1&quot;&gt;Master these techniques and input taking will no longer be a problem for you. Good luck!&lt;/P&gt;

&lt;/DIV&gt;
&lt;DIV id=&quot;page_10&quot;&gt;


&lt;/DIV&gt;
&lt;/body&gt;
&lt;/HTML&gt;</content><link rel='replies' type='application/atom+xml' href='http://dcoders01.blogspot.com/feeds/6373018365703526450/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://dcoders01.blogspot.com/2013/09/input-taking-strategies.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6097953565153021725/posts/default/6373018365703526450'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6097953565153021725/posts/default/6373018365703526450'/><link rel='alternate' type='text/html' href='http://dcoders01.blogspot.com/2013/09/input-taking-strategies.html' title='Input Taking Strategies'/><author><name>Navneet</name><uri>http://www.blogger.com/profile/08782557583312965286</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6097953565153021725.post-8057351607415358772</id><published>2013-08-26T03:09:00.000-07:00</published><updated>2013-08-26T03:43:42.868-07:00</updated><title type='text'>Permutations of Binary Search tree</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div&gt;
&lt;div style=&quot;background-color: white; border: 0px; clear: both; font-family: Arial, &#39;Liberation Sans&#39;, &#39;DejaVu Sans&#39;, sans-serif; font-size: 14px; line-height: 18px; margin-bottom: 1em; padding: 0px; vertical-align: baseline; word-wrap: break-word;&quot;&gt;
Given an array of integers arr = [5,6,1].&lt;/div&gt;
&lt;div style=&quot;background-color: white; border: 0px; clear: both; font-family: Arial, &#39;Liberation Sans&#39;, &#39;DejaVu Sans&#39;, sans-serif; font-size: 14px; line-height: 18px; margin-bottom: 1em; padding: 0px; vertical-align: baseline; word-wrap: break-word;&quot;&gt;
When we construct a BST with this input in the same order, we will have &quot;5&quot; as root, &quot;6&quot; as the right child and &quot;1&quot; as left child.&lt;/div&gt;
&lt;div style=&quot;background-color: white; border: 0px; clear: both; font-family: Arial, &#39;Liberation Sans&#39;, &#39;DejaVu Sans&#39;, sans-serif; font-size: 14px; line-height: 18px; margin-bottom: 1em; padding: 0px; vertical-align: baseline; word-wrap: break-word;&quot;&gt;
Now if our input is changed to [5,1,6], still our BST structure will be identical.&lt;/div&gt;
&lt;div style=&quot;background-color: white; border: 0px; clear: both; font-family: Arial, &#39;Liberation Sans&#39;, &#39;DejaVu Sans&#39;, sans-serif; font-size: 14px; line-height: 18px; margin-bottom: 1em; padding: 0px; vertical-align: baseline; word-wrap: break-word;&quot;&gt;
So given an array of integers, how to find the number of different permutations of the input array that results in the identical BST as the BST formed on the original array order?&lt;/div&gt;
&lt;div style=&quot;background-color: white; border: 0px; clear: both; font-family: Arial, &#39;Liberation Sans&#39;, &#39;DejaVu Sans&#39;, sans-serif; font-size: 14px; line-height: 18px; margin-bottom: 1em; padding: 0px; vertical-align: baseline; word-wrap: break-word;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: white; border: 0px; clear: both; font-family: Arial, &#39;Liberation Sans&#39;, &#39;DejaVu Sans&#39;, sans-serif; font-size: 14px; line-height: 18px; margin-bottom: 1em; padding: 0px; vertical-align: baseline; word-wrap: break-word;&quot;&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: blue;&quot;&gt;Startegy:&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: white; border: 0px; clear: both; font-family: Arial, &#39;Liberation Sans&#39;, &#39;DejaVu Sans&#39;, sans-serif; font-size: 14px; line-height: 18px; margin-bottom: 1em; padding: 0px; vertical-align: baseline; word-wrap: break-word;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;border: 0px; clear: both; margin-bottom: 1em; padding: 0px; vertical-align: baseline; word-wrap: break-word;&quot;&gt;
Your question is equivalent to the question of counting the number of topological orderings for the given BST.&lt;/div&gt;
&lt;div style=&quot;border: 0px; clear: both; margin-bottom: 1em; padding: 0px; vertical-align: baseline; word-wrap: break-word;&quot;&gt;
For example, for the BST&lt;/div&gt;
&lt;pre style=&quot;background-color: #eeeeee; border: 0px; font-family: Consolas, Menlo, Monaco, &#39;Lucida Console&#39;, &#39;Liberation Mono&#39;, &#39;DejaVu Sans Mono&#39;, &#39;Bitstream Vera Sans Mono&#39;, &#39;Courier New&#39;, monospace, serif; margin-bottom: 10px; max-height: 600px; overflow: auto; padding: 5px; vertical-align: baseline; width: auto;&quot;&gt;&lt;code style=&quot;border: 0px; font-family: Consolas, Menlo, Monaco, &#39;Lucida Console&#39;, &#39;Liberation Mono&#39;, &#39;DejaVu Sans Mono&#39;, &#39;Bitstream Vera Sans Mono&#39;, &#39;Courier New&#39;, monospace, serif; margin: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;  10
 /  \
5   20
 \  | \
  7 15 30
&lt;/code&gt;&lt;/pre&gt;
&lt;div style=&quot;border: 0px; clear: both; margin-bottom: 1em; padding: 0px; vertical-align: baseline; word-wrap: break-word;&quot;&gt;
the set of topological orderings can be counted by hand like this: 10 starts every ordering. The number of topological orderings for the subtree starting with 20 is two: (20, 15, 30) and (20, 30, 15). The subtree starting with 5 has only one ordering: (5, 7). These two sequence can be interleaved in an arbitrary manner, leading to 2 x 10 interleavings, thus producing twenty inputs which produce the same BST. The first 10 are enumerated below for the case (20, 15, 30):&lt;/div&gt;
&lt;pre style=&quot;background-color: #eeeeee; border: 0px; font-family: Consolas, Menlo, Monaco, &#39;Lucida Console&#39;, &#39;Liberation Mono&#39;, &#39;DejaVu Sans Mono&#39;, &#39;Bitstream Vera Sans Mono&#39;, &#39;Courier New&#39;, monospace, serif; margin-bottom: 10px; max-height: 600px; overflow: auto; padding: 5px; vertical-align: baseline; width: auto;&quot;&gt;&lt;code style=&quot;border: 0px; font-family: Consolas, Menlo, Monaco, &#39;Lucida Console&#39;, &#39;Liberation Mono&#39;, &#39;DejaVu Sans Mono&#39;, &#39;Bitstream Vera Sans Mono&#39;, &#39;Courier New&#39;, monospace, serif; margin: 0px; padding: 0px; vertical-align: baseline;&quot;&gt; 10 5 7 20 15 30
 10 5 20 7 15 30
 10 5 20 15 7 30
 10 5 20 15 30 7
 10 20 5 7 15 30
 10 20 5 15 7 30
 10 20 5 15 30 7
 10 20 15 5 7 30
 10 20 15 5 30 7
 10 20 15 30 5 7
&lt;/code&gt;&lt;/pre&gt;
&lt;div style=&quot;border: 0px; clear: both; margin-bottom: 1em; padding: 0px; vertical-align: baseline; word-wrap: break-word;&quot;&gt;
The case (20, 30, 15) is analogous --- you can check that any one of the following inputs produces the same BST.&lt;/div&gt;
&lt;div style=&quot;border: 0px; clear: both; margin-bottom: 1em; padding: 0px; vertical-align: baseline; word-wrap: break-word;&quot;&gt;
This examples also provides a recursive rule to calculate the number of the orderings. For a leaf, the number is 1. For a non-leaf node with one child, the number equals to the number of topological orderings for the child. For a non-leaf node with two children with subtree sizes |L| and |R|, both having l and r orderings, resp., the number equals to&lt;/div&gt;
&lt;pre style=&quot;background-color: #eeeeee; border: 0px; font-family: Consolas, Menlo, Monaco, &#39;Lucida Console&#39;, &#39;Liberation Mono&#39;, &#39;DejaVu Sans Mono&#39;, &#39;Bitstream Vera Sans Mono&#39;, &#39;Courier New&#39;, monospace, serif; margin-bottom: 10px; max-height: 600px; overflow: auto; padding: 5px; vertical-align: baseline; width: auto;&quot;&gt;&lt;code style=&quot;border: 0px; font-family: Consolas, Menlo, Monaco, &#39;Lucida Console&#39;, &#39;Liberation Mono&#39;, &#39;DejaVu Sans Mono&#39;, &#39;Bitstream Vera Sans Mono&#39;, &#39;Courier New&#39;, monospace, serif; margin: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;  l x r x INT(|L|, |R|)
&lt;/code&gt;&lt;/pre&gt;
&lt;div style=&quot;border: 0px; clear: both; margin-bottom: 1em; padding: 0px; vertical-align: baseline; word-wrap: break-word;&quot;&gt;
Where INT is the number of possible interleavings of |L| and |R| elements. This can be calculated easily by (|L| + |R|)! / (|L|! x |R|!). For the example above, we get the following recursive computation:&lt;/div&gt;
&lt;pre style=&quot;background-color: #eeeeee; border: 0px; font-family: Consolas, Menlo, Monaco, &#39;Lucida Console&#39;, &#39;Liberation Mono&#39;, &#39;DejaVu Sans Mono&#39;, &#39;Bitstream Vera Sans Mono&#39;, &#39;Courier New&#39;, monospace, serif; margin-bottom: 10px; max-height: 600px; overflow: auto; padding: 5px; vertical-align: baseline; width: auto;&quot;&gt;&lt;code style=&quot;border: 0px; font-family: Consolas, Menlo, Monaco, &#39;Lucida Console&#39;, &#39;Liberation Mono&#39;, &#39;DejaVu Sans Mono&#39;, &#39;Bitstream Vera Sans Mono&#39;, &#39;Courier New&#39;, monospace, serif; margin: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;  Ord(15) = 1
  Ord(30) = 1
  Ord(20) = 1 x 1 x INT(1, 1) = 2  ; INT(1, 1) = 2! / 1 = 2
  Ord(7) = 1
  Ord(5) = 1
  Ord(10) = 1 x 2 x INT(2, 3) = 2 x 5! / (2! x 3!) = 2 x 120 / 12 = 2 x 10 = 20&lt;/code&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: #eeeeee; border: 0px; font-family: Consolas, Menlo, Monaco, &#39;Lucida Console&#39;, &#39;Liberation Mono&#39;, &#39;DejaVu Sans Mono&#39;, &#39;Bitstream Vera Sans Mono&#39;, &#39;Courier New&#39;, monospace, serif; margin-bottom: 10px; max-height: 600px; overflow: auto; padding: 5px; vertical-align: baseline; width: auto;&quot;&gt;&lt;code style=&quot;border: 0px; font-family: Consolas, Menlo, Monaco, &#39;Lucida Console&#39;, &#39;Liberation Mono&#39;, &#39;DejaVu Sans Mono&#39;, &#39;Bitstream Vera Sans Mono&#39;, &#39;Courier New&#39;, monospace, serif; margin: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b&gt;&lt;u&gt;Useful Link:&lt;/u&gt;&lt;/b&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: #eeeeee; border: 0px; font-family: Consolas, Menlo, Monaco, &#39;Lucida Console&#39;, &#39;Liberation Mono&#39;, &#39;DejaVu Sans Mono&#39;, &#39;Bitstream Vera Sans Mono&#39;, &#39;Courier New&#39;, monospace, serif; margin-bottom: 10px; max-height: 600px; overflow: auto; padding: 5px; vertical-align: baseline; width: auto;&quot;&gt;&lt;code style=&quot;border: 0px; font-family: Consolas, Menlo, Monaco, &#39;Lucida Console&#39;, &#39;Liberation Mono&#39;, &#39;DejaVu Sans Mono&#39;, &#39;Bitstream Vera Sans Mono&#39;, &#39;Courier New&#39;, monospace, serif; margin: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;a href=&quot;http://stackoverflow.com/questions/1701612/permutations-of-bst?rq=1&quot;&gt;http://stackoverflow.com/questions/1701612/permutations-of-bst?rq=1&lt;/a&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;/div&gt;
&lt;div style=&quot;border: 0px; clear: both; margin-bottom: 1em; padding: 0px; vertical-align: baseline; word-wrap: break-word;&quot;&gt;
l x r x INT(|L|, |R|) comes from (1) &quot;l&quot; different ways to order the sequence that produces the left subtree, (2), &quot;r&quot; different ways to order the sequence that produces the right subtree, and (3) INT(|L|, |R|) different ways to mix those two sequences, i.e. interleave them. INT(a,b) = (a+b)!/(a! b!) because among all interleavings of a+b elements, we are interested only in those where the sequence of elements a has a fixed ordering, ditto for sequence of elements b, hence divide by a! and b! separately.
&lt;/div&gt;
&lt;pre style=&quot;background-color: #eeeeee; border: 0px; font-family: Consolas, Menlo, Monaco, &#39;Lucida Console&#39;, &#39;Liberation Mono&#39;, &#39;DejaVu Sans Mono&#39;, &#39;Bitstream Vera Sans Mono&#39;, &#39;Courier New&#39;, monospace, serif; margin-bottom: 10px; max-height: 600px; overflow: auto; padding: 5px; vertical-align: baseline; width: auto;&quot;&gt;&lt;code style=&quot;border: 0px; font-family: Consolas, Menlo, Monaco, &#39;Lucida Console&#39;, &#39;Liberation Mono&#39;, &#39;DejaVu Sans Mono&#39;, &#39;Bitstream Vera Sans Mono&#39;, &#39;Courier New&#39;, monospace, serif; margin: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;
#include &amp;lt;vector&amp;gt;
#include &amp;lt;iostream&amp;gt;

using namespace std;

int factorial(int x) {
  return (x &amp;lt;= 1) ? 1 : x * factorial(x - 1);
}

int f(int a, int b) {
  return factorial(a + b) / (factorial(a) * factorial(b));
}

template &amp;lt;typename T&amp;gt;
int n(vector&amp;lt;T&amp;gt;&amp;amp; P) {
  if (P.size() &amp;lt;= 1) return 1;
  vector&amp;lt;T&amp;gt; L, R;
  for (int i = 1; i &amp;lt; P.size(); i++) {
    if (P[i] &amp;lt; P[0])
      L.push_back(P[i]);
    else
      R.push_back(P[i]);
  }
  return n(L) * n(R) * f(L.size(), R.size());
}

int main(int argc, char *argv[]) {
  vector&amp;lt;int&amp;gt; a = { 10, 5, 7, 20, 15, 30 };
  cout &amp;lt;&amp;lt; n(a) &amp;lt;&amp;lt; endl;
  return 0;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dcoders01.blogspot.com/feeds/8057351607415358772/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://dcoders01.blogspot.com/2013/08/permutations-of-binary-search-tree.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6097953565153021725/posts/default/8057351607415358772'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6097953565153021725/posts/default/8057351607415358772'/><link rel='alternate' type='text/html' href='http://dcoders01.blogspot.com/2013/08/permutations-of-binary-search-tree.html' title='Permutations of Binary Search tree'/><author><name>Navneet</name><uri>http://www.blogger.com/profile/08782557583312965286</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6097953565153021725.post-7703297963471255346</id><published>2013-08-26T01:44:00.000-07:00</published><updated>2013-08-26T01:44:51.555-07:00</updated><title type='text'>alerts of the coding contests via sms or email...</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;h3&gt;Add Programming contest calender to ur Google Calendar With these One Click Links :&lt;/h3&gt;&lt;/br&gt;

HackerRank : &lt;a href=&quot;http://www.google.com/calendar/render?cid=http://www.hackerrank.com/calendar/cal.ics&quot;&gt; Hackerrank &lt;/a&gt;

&lt;/br&gt;
Top Coder : &lt;a href=&quot;http://www.google.com/calendar/render?cid=http%3A//topcoder-calendar.appspot.com/ical%3Fsrm%2Ctco*%2Ctccc*%2Clong%2Cpredictive%2Camd&quot;&gt; Topcoder &lt;/a&gt;
&lt;/br&gt;
CodeForces : &lt;a href=&quot;https://www.google.com/calendar/render?cid=br1o1n70iqgrrbc875vcehacjg%40group.calendar.google.com&quot;&gt; CodeForces &lt;/a&gt;
&lt;/br&gt;
Code Chef :&lt;a href= &quot;https://www.google.com/calendar/render?cid=codechef.com_3ilksfmv45aqr3at9ckm95td5g%40group.calendar.google.com&quot;&gt; Codechef &lt;/a&gt;


&lt;/br&gt;
&lt;/br&gt;
&lt;/br&gt;
 &lt;h3&gt;Customize your notifications in google calender&lt;/h3&gt;    &lt;div class=&quot;main-section--answer main-section content-container&quot;&gt;&lt;p&gt;Google Calendar offers different options for receiving event reminders. You can choose to receive your event reminders via SMS messages, emails, or popups within Google Calendar itself.&lt;/p&gt;
&lt;p&gt;Here&#39;s how you can customize your reminder settings:&lt;/p&gt;
&lt;/br&gt;
  &lt;li&gt;In the calendar list on the left, click the down-arrow button next to the appropriate calendar, then select &lt;strong&gt;Reminders and notifications&lt;/strong&gt;.&amp;nbsp;&lt;/li&gt;
&lt;/br&gt;
  &lt;li&gt;In the &lt;strong&gt;Event reminders&lt;/strong&gt; section, select the reminder method from the drop-down menu and enter the corresponding reminder time (between five minutes and four weeks). If you&#39;d like to add additional default reminders, simply click &lt;strong&gt;Add a&amp;nbsp;reminder&lt;/strong&gt;.&lt;/li&gt;
&lt;/br&gt;  
&lt;li&gt;You can also choose to be notified of specific activities on the calendar by selecting the appropriate options in the &lt;strong&gt;Choose how you would like to be notified&lt;/strong&gt; section.&lt;/li&gt;
&lt;/br&gt;  
&lt;li&gt;Click &lt;strong&gt;Save&lt;/strong&gt;.&lt;/li&gt;

&lt;/br&gt;
&lt;/br&gt;
&lt;h3&gt; So that you do not miss any contest...&lt;/h3&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dcoders01.blogspot.com/feeds/7703297963471255346/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://dcoders01.blogspot.com/2013/08/alerts-of-coding-contests-via-sms-or.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6097953565153021725/posts/default/7703297963471255346'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6097953565153021725/posts/default/7703297963471255346'/><link rel='alternate' type='text/html' href='http://dcoders01.blogspot.com/2013/08/alerts-of-coding-contests-via-sms-or.html' title='alerts of the coding contests via sms or email...'/><author><name>Navneet</name><uri>http://www.blogger.com/profile/08782557583312965286</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6097953565153021725.post-2063486883617153019</id><published>2013-08-16T12:21:00.000-07:00</published><updated>2013-08-16T12:21:27.989-07:00</updated><title type='text'>So much with Binary search tree...</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div&gt;
   
   &lt;p&gt; &lt;cite&gt;&lt;span style=&quot;font-size:18px;&quot;&gt;&lt;em&gt;A &amp;quot;binary search tree&amp;quot; (BST) or &amp;quot;ordered binary tree&amp;quot; is a type of binary tree where the nodes are arranged in order:&lt;/em&gt;&lt;/span&gt;&lt;/cite&gt;&lt;/p&gt;
   &lt;p&gt;
    &lt;span style=&quot;font-size: 18px;&quot;&gt;&lt;em&gt;for each node, all elements in its left subtree are less-or-equal to the node (&amp;lt;=), and all the elements in its right&amp;nbsp;&lt;/em&gt;&lt;/span&gt;&lt;em style=&quot;font-size: 18px;&quot;&gt;subtree are greater than the node (&amp;gt;).&lt;/em&gt;&lt;/p&gt;
  &lt;/div&gt;
  &lt;div&gt;
   &amp;nbsp;&lt;/div&gt;
  &lt;div&gt;
   &amp;nbsp;&lt;/div&gt;
  &lt;div&gt;
   &lt;span style=&quot;font-size:18px;&quot;&gt;&lt;span style=&quot;color:#000080;&quot;&gt;&lt;em&gt;Basic structure of a node of BST :-&lt;/em&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
  &lt;div&gt;
   &amp;nbsp;&lt;/div&gt;
  &lt;div&gt;
   &lt;div&gt;
    &lt;span style=&quot;font-size:18px;&quot;&gt;&lt;span style=&quot;color:#000080;&quot;&gt;&lt;em&gt;struct node {&lt;/em&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
   &lt;div&gt;
    &lt;span style=&quot;font-size:18px;&quot;&gt;&lt;span style=&quot;color:#000080;&quot;&gt;&lt;em&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;int data;&lt;/em&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
   &lt;div&gt;
    &lt;span style=&quot;font-size:18px;&quot;&gt;&lt;span style=&quot;color:#000080;&quot;&gt;&lt;em&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;struct node* left;&lt;/em&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
   &lt;div&gt;
    &lt;span style=&quot;font-size:18px;&quot;&gt;&lt;span style=&quot;color:#000080;&quot;&gt;&lt;em&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;struct node* right;&lt;/em&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
   &lt;div&gt;
    &lt;span style=&quot;font-size:18px;&quot;&gt;&lt;span style=&quot;color:#000080;&quot;&gt;&lt;em&gt;}&lt;/em&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
  &lt;/div&gt;

&lt;/br&gt;
&lt;/br&gt;
&lt;/br&gt;
&lt;iframe src=&quot;https://docs.google.com/file/d/0B1Ee5jRs3kX6WDA1QVJlaE1qNGs/preview&quot; width=&quot;640&quot; height=&quot;480&quot;&gt;&lt;/iframe&gt;
&lt;/br&gt;
&lt;/br&gt;
&lt;a href=&quot;https://docs.google.com/file/d/0B1Ee5jRs3kX6WDA1QVJlaE1qNGs/edit?usp=sharing&quot;&gt; Download it here &lt;/a&gt;

&lt;/br&gt;
&lt;/br&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;http://ideone.com/api/embed.js/link/qGNUzg&quot;&gt;&lt;/script&gt;

&lt;/br&gt;
&lt;/br&gt;
&lt;br /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dcoders01.blogspot.com/feeds/2063486883617153019/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://dcoders01.blogspot.com/2013/08/so-much-with-binary-search-tree.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6097953565153021725/posts/default/2063486883617153019'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6097953565153021725/posts/default/2063486883617153019'/><link rel='alternate' type='text/html' href='http://dcoders01.blogspot.com/2013/08/so-much-with-binary-search-tree.html' title='So much with Binary search tree...'/><author><name>Navneet</name><uri>http://www.blogger.com/profile/08782557583312965286</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6097953565153021725.post-1588566028012224951</id><published>2013-08-15T05:52:00.000-07:00</published><updated>2013-08-15T05:52:07.776-07:00</updated><title type='text'>Is this how u allocate memory!!</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;

&lt;body&gt;
  &lt;h2&gt;
   &amp;nbsp;&lt;/h2&gt;
  &lt;div&gt;
   &lt;h2&gt;
    &lt;span style=&quot;font-size: 20px;&quot;&gt;Flexibility in allocation in C...&lt;/span&gt;&lt;/h2&gt;
  &lt;/div&gt;
  &lt;div&gt;
   &amp;nbsp;&lt;/div&gt;
  &lt;div style=&quot;text-align: justify;&quot;&gt;
   &lt;var&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;One of the reasons why the C programming language is so popular is the flexibility of its use for memory management. Programmers have opportunities to control how, when, and where to allocate and deallocate memory. Memory is allocated statically, automatically, or dynamically in the C programming language.&lt;/span&gt;&lt;/var&gt;&lt;/div&gt;
  &lt;div style=&quot;text-align: justify;&quot;&gt;
   &amp;nbsp;&lt;/div&gt;
  &lt;div style=&quot;text-align: justify;&quot;&gt;
   &amp;nbsp;&lt;/div&gt;
  &lt;div style=&quot;text-align: justify;&quot;&gt;
   &lt;var&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;Static variables and global variables are static-duration variables, which are allocated along with the&lt;/span&gt;&lt;/var&gt;&lt;/div&gt;
  &lt;div style=&quot;text-align: justify;&quot;&gt;
   &lt;var&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;executable code of the program and persist during the execution of the application. When a local variable is declared as static inside a function, it is initialized only once. Whatever values the function puts into its static local variables during one call will be present when the function is called again.&lt;/span&gt;&lt;/var&gt;&lt;/div&gt;
  &lt;div style=&quot;text-align: justify;&quot;&gt;
   &amp;nbsp;&lt;/div&gt;
  &lt;div style=&quot;text-align: justify;&quot;&gt;
   &amp;nbsp;&lt;/div&gt;
  &lt;div style=&quot;text-align: justify;&quot;&gt;
   &lt;var&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;The following is a typical interview question about static local variables: What is the output when the function test executes?&amp;nbsp;&lt;/span&gt;&lt;/var&gt;&lt;/div&gt;
  &lt;div style=&quot;text-align: justify;&quot;&gt;
   &amp;nbsp;&lt;/div&gt;
  &lt;div style=&quot;text-align: justify;&quot;&gt;
   &amp;nbsp;&lt;/div&gt;
  &lt;div style=&quot;text-align: justify;&quot;&gt;
   &lt;var&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;&lt;span style=&quot;color: rgb(75, 0, 130);&quot;&gt;C Code with Static Variables &amp;nbsp;:-&lt;/span&gt;&lt;/span&gt;&lt;/var&gt;&lt;/div&gt;
  &lt;div style=&quot;text-align: justify;&quot;&gt;
   &amp;nbsp;&lt;/div&gt;
  &lt;div style=&quot;text-align: justify;&quot;&gt;
   &lt;var&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;&lt;em&gt;&lt;span style=&quot;color: rgb(0, 0, 128);&quot;&gt;int hasStatic(int n) {&lt;/span&gt;&lt;/em&gt;&lt;/span&gt;&lt;/var&gt;&lt;/div&gt;
  &lt;div style=&quot;text-align: justify;&quot;&gt;
   &lt;var&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;&lt;em&gt;&lt;span style=&quot;color: rgb(0, 0, 128);&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; static int x = 0;&lt;/span&gt;&lt;/em&gt;&lt;/span&gt;&lt;/var&gt;&lt;/div&gt;
  &lt;div style=&quot;text-align: justify;&quot;&gt;
   &lt;var&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;&lt;em&gt;&lt;span style=&quot;color: rgb(0, 0, 128);&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; x += n;&lt;/span&gt;&lt;/em&gt;&lt;/span&gt;&lt;/var&gt;&lt;/div&gt;
  &lt;div style=&quot;text-align: justify;&quot;&gt;
   &lt;var&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;&lt;em&gt;&lt;span style=&quot;color: rgb(0, 0, 128);&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; return x;&lt;/span&gt;&lt;/em&gt;&lt;/span&gt;&lt;/var&gt;&lt;/div&gt;
  &lt;div&gt;
   &lt;div style=&quot;text-align: justify;&quot;&gt;
    &lt;var&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;&lt;em&gt;&lt;span style=&quot;color: rgb(0, 0, 128);&quot;&gt;}&lt;/span&gt;&lt;/em&gt;&lt;/span&gt;&lt;/var&gt;&lt;/div&gt;
   &lt;div style=&quot;text-align: justify;&quot;&gt;
    &amp;nbsp;&lt;/div&gt;
   &lt;div style=&quot;text-align: justify;&quot;&gt;
    &lt;var&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;&lt;em&gt;&lt;span style=&quot;color: rgb(0, 0, 128);&quot;&gt;void test(){&lt;/span&gt;&lt;/em&gt;&lt;/span&gt;&lt;/var&gt;&lt;/div&gt;
   &lt;div style=&quot;text-align: justify;&quot;&gt;
    &lt;var&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;&lt;em&gt;&lt;span style=&quot;color: rgb(0, 0, 128);&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; int sum;&lt;/span&gt;&lt;/em&gt;&lt;/span&gt;&lt;/var&gt;&lt;/div&gt;
   &lt;div style=&quot;text-align: justify;&quot;&gt;
    &lt;var&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;&lt;em&gt;&lt;span style=&quot;color: rgb(0, 0, 128);&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; sum = 0;&lt;/span&gt;&lt;/em&gt;&lt;/span&gt;&lt;/var&gt;&lt;/div&gt;
   &lt;div style=&quot;text-align: justify;&quot;&gt;
    &lt;var&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;&lt;em&gt;&lt;span style=&quot;color: rgb(0, 0, 128);&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; for(int i = 1; i &amp;lt;= 4; ++i)&lt;/span&gt;&lt;/em&gt;&lt;/span&gt;&lt;/var&gt;&lt;/div&gt;
   &lt;div style=&quot;text-align: justify;&quot;&gt;
    &lt;var&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;&lt;em&gt;&lt;span style=&quot;color: rgb(0, 0, 128);&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; sum += hasStatic(i);&lt;/span&gt;&lt;/em&gt;&lt;/span&gt;&lt;/var&gt;&lt;/div&gt;
   &lt;div style=&quot;text-align: justify;&quot;&gt;
    &lt;var&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;&lt;em&gt;&lt;span style=&quot;color: rgb(0, 0, 128);&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; printf(&amp;quot;Result of sum is %d.\n&amp;quot;, sum);&lt;/span&gt;&lt;/em&gt;&lt;/span&gt;&lt;/var&gt;&lt;/div&gt;
   &lt;div style=&quot;text-align: justify;&quot;&gt;
    &lt;var&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;&lt;em&gt;&lt;span style=&quot;color: rgb(0, 0, 128);&quot;&gt;}&lt;/span&gt;&lt;/em&gt;&lt;/span&gt;&lt;/var&gt;&lt;/div&gt;
   &lt;div style=&quot;text-align: justify;&quot;&gt;
    &amp;nbsp;&lt;/div&gt;
   &lt;div style=&quot;text-align: justify;&quot;&gt;
    &amp;nbsp;&lt;/div&gt;
   &lt;div style=&quot;text-align: justify;&quot;&gt;
    &lt;var&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;The variable x in the function hasStatic is declared as static. When the function is called the first&lt;/span&gt;&lt;/var&gt;&lt;/div&gt;
   &lt;div style=&quot;text-align: justify;&quot;&gt;
    &lt;var&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;time with a parameter 1, the static variable x is initialized as 0, and then set to 1. When the function is&lt;/span&gt;&lt;/var&gt;&lt;/div&gt;
   &lt;div style=&quot;text-align: justify;&quot;&gt;
    &lt;var&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;called the second time with a parameter 2, it is not initialized again and its value 1 persists. It becomes 3 after execution. Similarly, its value becomes 6 and 10 after execution with parameters 3 and 4. Therefore, the result is the sum 1+2+6+10=19.&lt;/span&gt;&lt;/var&gt;&lt;/div&gt;
   &lt;div style=&quot;text-align: justify;&quot;&gt;
    &amp;nbsp;&lt;/div&gt;
   &lt;div style=&quot;text-align: justify;&quot;&gt;
    &amp;nbsp;&lt;/div&gt;
   &lt;div style=&quot;text-align: justify;&quot;&gt;
    &lt;var&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;Automatic-duration variables, also called local variables, are allocated on the stack. They are allocated and deallocated automatically when the program execution flow enters and leaves their scopes. In the sample code below, there is a local array str in the function allocateMemory. What is the problem with it?&lt;/span&gt;&lt;/var&gt;&lt;/div&gt;
   &lt;div style=&quot;text-align: justify;&quot;&gt;
    &amp;nbsp;&lt;/div&gt;
   &lt;div style=&quot;text-align: justify;&quot;&gt;
    &amp;nbsp;&lt;/div&gt;
   &lt;div style=&quot;text-align: justify;&quot;&gt;
    &lt;var&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;&lt;span style=&quot;color: rgb(75, 0, 130);&quot;&gt;C Code to Allocate Memory :-&lt;/span&gt;&lt;/span&gt;&lt;/var&gt;&lt;/div&gt;
   &lt;div style=&quot;text-align: justify;&quot;&gt;
    &amp;nbsp;&lt;/div&gt;
   &lt;div style=&quot;text-align: justify;&quot;&gt;
    &lt;var&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;&lt;em&gt;&lt;span style=&quot;color: rgb(0, 0, 128);&quot;&gt;char* allocateMemory() {&lt;/span&gt;&lt;/em&gt;&lt;/span&gt;&lt;/var&gt;&lt;/div&gt;
   &lt;div style=&quot;margin-left: 40px; text-align: justify;&quot;&gt;
    &lt;var&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;&lt;em&gt;&lt;span style=&quot;color: rgb(0, 0, 128);&quot;&gt;char str[20] = &amp;quot;Hello world.&amp;quot;;&lt;/span&gt;&lt;/em&gt;&lt;/span&gt;&lt;/var&gt;&lt;/div&gt;
   &lt;div style=&quot;margin-left: 40px; text-align: justify;&quot;&gt;
    &lt;var&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;&lt;em&gt;&lt;span style=&quot;color: rgb(0, 0, 128);&quot;&gt;return str;&lt;/span&gt;&lt;/em&gt;&lt;/span&gt;&lt;/var&gt;&lt;/div&gt;
   &lt;div style=&quot;text-align: justify;&quot;&gt;
    &lt;var&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;&lt;em&gt;&lt;span style=&quot;color: rgb(0, 0, 128);&quot;&gt;}&lt;/span&gt;&lt;/em&gt;&lt;/span&gt;&lt;/var&gt;&lt;/div&gt;
   &lt;div style=&quot;text-align: justify;&quot;&gt;
    &amp;nbsp;&lt;/div&gt;
   &lt;div style=&quot;text-align: justify;&quot;&gt;
    &lt;var&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;&lt;em&gt;&lt;span style=&quot;color: rgb(0, 0, 128);&quot;&gt;void test() {&lt;/span&gt;&lt;/em&gt;&lt;/span&gt;&lt;/var&gt;&lt;/div&gt;
   &lt;div style=&quot;margin-left: 40px; text-align: justify;&quot;&gt;
    &lt;var&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;&lt;em&gt;&lt;span style=&quot;color: rgb(0, 0, 128);&quot;&gt;char* pString = allocateMemory();&lt;/span&gt;&lt;/em&gt;&lt;/span&gt;&lt;/var&gt;&lt;/div&gt;
   &lt;div style=&quot;margin-left: 40px; text-align: justify;&quot;&gt;
    &lt;var&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;&lt;em&gt;&lt;span style=&quot;color: rgb(0, 0, 128);&quot;&gt;printf(&amp;quot;pString is %s.\n&amp;quot;, pString);&lt;/span&gt;&lt;/em&gt;&lt;/span&gt;&lt;/var&gt;&lt;/div&gt;
   &lt;div style=&quot;text-align: justify;&quot;&gt;
    &lt;var&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;&lt;em&gt;&lt;span style=&quot;color: rgb(0, 0, 128);&quot;&gt;}&lt;/span&gt;&lt;/em&gt;&lt;/span&gt;&lt;/var&gt;&lt;/div&gt;
   &lt;div style=&quot;text-align: justify;&quot;&gt;
    &amp;nbsp;&lt;/div&gt;
   &lt;div style=&quot;text-align: justify;&quot;&gt;
    &amp;nbsp;&lt;/div&gt;
   &lt;div style=&quot;text-align: justify;&quot;&gt;
    &lt;var&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;Because the array str is a local variable, it is deallocated automatically when the execution flow leaves the function allocateMemory. Therefore, the returned memory to pString in the function test is actually invalid, and its content is somewhat random.&lt;/span&gt;&lt;/var&gt;&lt;/div&gt;
   &lt;div style=&quot;text-align: justify;&quot;&gt;
    &amp;nbsp;&lt;/div&gt;
   &lt;div style=&quot;text-align: justify;&quot;&gt;
    &lt;var&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;One limitation of static-duration and automatic-duration variables is that their size of allocation is&lt;/span&gt;&lt;/var&gt;&lt;/div&gt;
   &lt;div style=&quot;text-align: justify;&quot;&gt;
    &lt;var&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;required to be compile-time constant. This limitation can be avoided by dynamic memory allocation in which memory is more explicitly, as well as more flexibly, managed. In the C programming language, the library function malloc is used to allocate a block of memory dynamically on the heap, where size is specified at runtime. This block of memory is accessible via a pointer that malloc returns. When the memory is no longer needed, the pointer should be passed to another library function, free, which deallocates the memory in order to avoid memory leaks.&lt;/span&gt;&lt;/var&gt;&lt;/div&gt;
   &lt;div style=&quot;text-align: justify;&quot;&gt;
    &amp;nbsp;&lt;/div&gt;
   &lt;div style=&quot;text-align: justify;&quot;&gt;
    &lt;var&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;The function allocateMemory below utilizes the function malloc to allocate memory&lt;/span&gt;&lt;/var&gt;&lt;/div&gt;
   &lt;div style=&quot;text-align: justify;&quot;&gt;
    &lt;var&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;dynamically. What is the problem in it?&lt;/span&gt;&lt;/var&gt;&lt;/div&gt;
   &lt;div style=&quot;text-align: justify;&quot;&gt;
    &amp;nbsp;&lt;/div&gt;
   &lt;div style=&quot;text-align: justify;&quot;&gt;
    &amp;nbsp;&lt;/div&gt;
   &lt;div&gt;
    &lt;div style=&quot;text-align: justify;&quot;&gt;
     &lt;var&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;&lt;span style=&quot;color: rgb(75, 0, 130);&quot;&gt;C Code to Allocate Memory :-&lt;/span&gt;&lt;/span&gt;&lt;/var&gt;&lt;/div&gt;
    &lt;div style=&quot;text-align: justify;&quot;&gt;
     &amp;nbsp;&lt;/div&gt;
    &lt;div style=&quot;text-align: justify;&quot;&gt;
     &lt;var&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;&lt;em&gt;&lt;span style=&quot;color: rgb(0, 0, 128);&quot;&gt;void allocateMemory(char* pString, int length) {&lt;/span&gt;&lt;/em&gt;&lt;/span&gt;&lt;/var&gt;&lt;/div&gt;
    &lt;div style=&quot;margin-left: 40px; text-align: justify;&quot;&gt;
     &lt;var&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;&lt;em&gt;&lt;span style=&quot;color: rgb(0, 0, 128);&quot;&gt;pString = (char*)malloc(length);&lt;/span&gt;&lt;/em&gt;&lt;/span&gt;&lt;/var&gt;&lt;/div&gt;
    &lt;div style=&quot;text-align: justify;&quot;&gt;
     &lt;var&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;&lt;em&gt;&lt;span style=&quot;color: rgb(0, 0, 128);&quot;&gt;}&lt;/span&gt;&lt;/em&gt;&lt;/span&gt;&lt;/var&gt;&lt;/div&gt;
    &lt;div style=&quot;text-align: justify;&quot;&gt;
     &amp;nbsp;&lt;/div&gt;
    &lt;div style=&quot;text-align: justify;&quot;&gt;
     &lt;var&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;&lt;em&gt;&lt;span style=&quot;color: rgb(0, 0, 128);&quot;&gt;void test() {&lt;/span&gt;&lt;/em&gt;&lt;/span&gt;&lt;/var&gt;&lt;/div&gt;
    &lt;div style=&quot;margin-left: 40px; text-align: justify;&quot;&gt;
     &lt;var&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;&lt;em&gt;&lt;span style=&quot;color: rgb(0, 0, 128);&quot;&gt;char* pString = NULL;&lt;/span&gt;&lt;/em&gt;&lt;/span&gt;&lt;/var&gt;&lt;/div&gt;
    &lt;div style=&quot;margin-left: 40px; text-align: justify;&quot;&gt;
     &lt;var&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;&lt;em&gt;&lt;span style=&quot;color: rgb(0, 0, 128);&quot;&gt;allocateMemory(pString, 20);&lt;/span&gt;&lt;/em&gt;&lt;/span&gt;&lt;/var&gt;&lt;/div&gt;
    &lt;div style=&quot;margin-left: 40px; text-align: justify;&quot;&gt;
     &lt;var&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;&lt;em&gt;&lt;span style=&quot;color: rgb(0, 0, 128);&quot;&gt;strcpy(pString, &amp;quot;Hello world.&amp;quot;);&lt;/span&gt;&lt;/em&gt;&lt;/span&gt;&lt;/var&gt;&lt;/div&gt;
    &lt;div style=&quot;text-align: justify;&quot;&gt;
     &lt;var&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;&lt;em&gt;&lt;span style=&quot;color: rgb(0, 0, 128);&quot;&gt;}&lt;/span&gt;&lt;/em&gt;&lt;/span&gt;&lt;/var&gt;&lt;/div&gt;
    &lt;div style=&quot;text-align: justify;&quot;&gt;
     &amp;nbsp;&lt;/div&gt;
    &lt;div style=&quot;text-align: justify;&quot;&gt;
     &lt;var&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;The parameter pString in the function allocateMemory is a pointer, and the string content it points to&lt;/span&gt;&lt;/var&gt;&lt;/div&gt;
    &lt;div style=&quot;text-align: justify;&quot;&gt;
     &lt;var&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;can be modified. However, the memory address it owns cannot be changed when the function&lt;/span&gt;&lt;/var&gt;&lt;/div&gt;
    &lt;div style=&quot;text-align: justify;&quot;&gt;
     &lt;var&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;allocateMemory returns. Since the input parameter pString of allocateMemory in the function test is NULL, it remains NULL after the execution of allocateMemory, and it causes the program to crash when the NULL address is accessed in test. In order to fix this problem, the first parameter of allocateMemory should be modified as char** pString.&lt;/span&gt;&lt;/var&gt;&lt;/div&gt;
    &lt;div style=&quot;text-align: justify;&quot;&gt;
     &amp;nbsp;&lt;/div&gt;
    &lt;div style=&quot;text-align: justify;&quot;&gt;
     &lt;var&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;Enjoyy...&lt;/span&gt;&lt;/var&gt;&lt;/div&gt;
   &lt;/div&gt;
  &lt;/div&gt;
 &lt;/body&gt;

&lt;br /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dcoders01.blogspot.com/feeds/1588566028012224951/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://dcoders01.blogspot.com/2013/08/is-this-how-u-allocate-memory.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6097953565153021725/posts/default/1588566028012224951'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6097953565153021725/posts/default/1588566028012224951'/><link rel='alternate' type='text/html' href='http://dcoders01.blogspot.com/2013/08/is-this-how-u-allocate-memory.html' title='Is this how u allocate memory!!'/><author><name>Navneet</name><uri>http://www.blogger.com/profile/08782557583312965286</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6097953565153021725.post-7886983202073346016</id><published>2013-08-15T05:22:00.000-07:00</published><updated>2013-08-15T05:22:09.603-07:00</updated><title type='text'>Playing around with some macros...</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;

&lt;body&gt;
  &lt;div style=&quot;margin-left: 40px;&quot;&gt;
   &amp;nbsp;&lt;/div&gt;
  &lt;div style=&quot;margin-left: 40px;&quot;&gt;
   &amp;nbsp;&lt;/div&gt;
  &lt;div style=&quot;margin-left: 40px;&quot;&gt;
   &lt;span style=&quot;font-size:22px;&quot;&gt;C Code with Macros :-&lt;/span&gt;&lt;/div&gt;
  &lt;div style=&quot;margin-left: 40px;&quot;&gt;
   &amp;nbsp;&lt;/div&gt;
  &lt;div style=&quot;margin-left: 40px;&quot;&gt;
   &lt;span style=&quot;font-size:16px;&quot;&gt;&lt;span style=&quot;color:#000080;&quot;&gt;&lt;em&gt;#define SQUARE(x) (x*x)&lt;/em&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
  &lt;div style=&quot;margin-left: 40px;&quot;&gt;
   &lt;span style=&quot;font-size:16px;&quot;&gt;&lt;span style=&quot;color:#000080;&quot;&gt;&lt;em&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; void test() {&lt;/em&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
  &lt;div style=&quot;margin-left: 40px;&quot;&gt;
   &lt;span style=&quot;font-size:16px;&quot;&gt;&lt;span style=&quot;color:#000080;&quot;&gt;&lt;em&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; printf(&amp;quot;Square of (3+4) is %d.\n&amp;quot;, SQUARE(3+4));&lt;/em&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
  &lt;div style=&quot;margin-left: 40px;&quot;&gt;
   &lt;span style=&quot;font-size:16px;&quot;&gt;&lt;span style=&quot;color:#000080;&quot;&gt;&lt;em&gt;}&lt;/em&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
  &lt;div style=&quot;margin-left: 40px;&quot;&gt;
   &amp;nbsp;&lt;/div&gt;
  &lt;div style=&quot;margin-left: 40px;&quot;&gt;
   &lt;span style=&quot;font-size:16px;&quot;&gt;The result of 3+4 is 7, and the square of 7 is 49. However, 49 is not the result of SQUARE(3+4). Macros are substituted by the preprocessor, so SQUARE(3+4) becomes 3+4*3+4, which is actually 19. In order to make the result as expected, the macro should be modified to #define SQUARE(x) ((x)*(x)).&lt;/span&gt;&lt;/div&gt;
  &lt;div style=&quot;margin-left: 40px;&quot;&gt;
   &amp;nbsp;&lt;/div&gt;
  &lt;div style=&quot;margin-left: 40px;&quot;&gt;
   &lt;span style=&quot;font-size:16px;&quot;&gt;Even in the revised version of macro SQUARE, its result might look surprising if it is not analyzed carefully in some cases.&lt;/span&gt;&lt;/div&gt;
  &lt;div style=&quot;margin-left: 40px;&quot;&gt;
   &amp;nbsp;&lt;/div&gt;
  &lt;div style=&quot;margin-left: 40px;&quot;&gt;
   &lt;span style=&quot;font-size:16px;&quot;&gt;For instance, what are the results of x and y after the code below is executed?&lt;/span&gt;&lt;/div&gt;
  &lt;div style=&quot;margin-left: 40px;&quot;&gt;
   &amp;nbsp;&lt;/div&gt;
  &lt;span _fck_bookmark=&quot;1&quot; style=&quot;display: none;&quot;&gt;&amp;nbsp;&lt;/span&gt;
  &lt;div style=&quot;margin-left: 40px;&quot;&gt;
   &lt;span style=&quot;font-size:16px;&quot;&gt;&lt;span style=&quot;color:#000080;&quot;&gt;&lt;em&gt;&lt;span _fck_bookmark=&quot;1&quot; style=&quot;display: none;&quot;&gt;&amp;nbsp;&lt;/span&gt;#define SQUARE(x) ((x)*(x))&lt;/em&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
  &lt;div style=&quot;margin-left: 40px;&quot;&gt;
   &lt;span style=&quot;font-size:16px;&quot;&gt;&lt;span style=&quot;color:#000080;&quot;&gt;&lt;em&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; void test() {&lt;/em&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
  &lt;div style=&quot;margin-left: 40px;&quot;&gt;
   &lt;span style=&quot;font-size:16px;&quot;&gt;&lt;span style=&quot;color:#000080;&quot;&gt;&lt;em&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; int x = 5;&lt;/em&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
  &lt;div style=&quot;margin-left: 40px;&quot;&gt;
   &lt;span style=&quot;font-size:16px;&quot;&gt;&lt;span style=&quot;color:#000080;&quot;&gt;&lt;em&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; int y = SQUARE(x++);&lt;/em&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
  &lt;div style=&quot;margin-left: 40px;&quot;&gt;
   &lt;span style=&quot;font-size:16px;&quot;&gt;&lt;span style=&quot;color:#000080;&quot;&gt;&lt;em&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; printf(&amp;quot;Result of x is %d, y is %d.\n&amp;quot;, x, y);&lt;/em&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
  &lt;div style=&quot;margin-left: 40px;&quot;&gt;
   &lt;span style=&quot;font-size:16px;&quot;&gt;&lt;span style=&quot;color:#000080;&quot;&gt;&lt;em&gt;}&lt;/em&gt;&lt;/span&gt;&lt;/span&gt;&lt;em&gt;&lt;span _fck_bookmark=&quot;1&quot; style=&quot;display: none;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/em&gt;&lt;/div&gt;
  &lt;span _fck_bookmark=&quot;1&quot; style=&quot;display: none;&quot;&gt;&amp;nbsp;&lt;/span&gt;
  &lt;div style=&quot;margin-left: 40px;&quot;&gt;
   &amp;nbsp;&lt;/div&gt;
  &lt;div&gt;
   &lt;div style=&quot;margin-left: 40px;&quot;&gt;
    &lt;span style=&quot;font-size:16px;&quot;&gt;It looks like that the variable x increases only once in the code above. However, when the macro is&lt;/span&gt;&lt;/div&gt;
   &lt;div style=&quot;margin-left: 40px;&quot;&gt;
    &lt;span style=&quot;font-size:16px;&quot;&gt;substituted, the variable y is assigned as ((x++)*(x++)). The result of y is 25, and x becomes 7 because it increases twice.&lt;/span&gt;&lt;/div&gt;
   &lt;div style=&quot;margin-left: 40px;&quot;&gt;
    &amp;nbsp;&lt;/div&gt;
   &lt;div style=&quot;margin-left: 40px;&quot;&gt;
    &amp;nbsp;&lt;/div&gt;
   &lt;div style=&quot;margin-left: 40px;&quot;&gt;
    &lt;span style=&quot;font-size:16px;&quot;&gt;Enjoyyy...&lt;/span&gt;&lt;/div&gt;
  &lt;/div&gt;
 &lt;/body&gt;
&lt;br /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dcoders01.blogspot.com/feeds/7886983202073346016/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://dcoders01.blogspot.com/2013/08/playing-around-with-some-macros.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6097953565153021725/posts/default/7886983202073346016'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6097953565153021725/posts/default/7886983202073346016'/><link rel='alternate' type='text/html' href='http://dcoders01.blogspot.com/2013/08/playing-around-with-some-macros.html' title='Playing around with some macros...'/><author><name>Navneet</name><uri>http://www.blogger.com/profile/08782557583312965286</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6097953565153021725.post-6151055379263322497</id><published>2013-08-15T05:04:00.001-07:00</published><updated>2013-08-15T14:54:20.388-07:00</updated><title type='text'>What is the size of a EMPTY class!!!</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;

&lt;body&gt;
  &lt;div&gt;
   &lt;span style=&quot;font-size:20px;&quot;&gt;&lt;strong&gt;C++ Concepts&lt;/strong&gt;&lt;/span&gt;&lt;/div&gt;
  &lt;div&gt;
   &amp;nbsp;&lt;/div&gt;
  &lt;div&gt;
   The most popular questions in this category concern the keyword sizeof.&lt;/div&gt;
  &lt;div&gt;
   &amp;nbsp;&lt;/div&gt;
  &lt;div&gt;
   For instance, the following dialog is repeated again and again in many interviews:&lt;/div&gt;
  &lt;div&gt;
   &amp;nbsp;&lt;/div&gt;
  &lt;div&gt;
   &lt;strong&gt;Interviewer:&lt;/strong&gt;&amp;nbsp; &amp;nbsp;There is an empty class, without any member fields and functions inside its definition.&lt;/div&gt;
  &lt;div&gt;
   &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; What is the result of sizeof for this class?&lt;/div&gt;
  &lt;div&gt;
   &amp;nbsp;&lt;/div&gt;
  &lt;div&gt;
   &lt;cite&gt;&lt;strong&gt;Candidate:&lt;/strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;It is 1.&lt;/cite&gt;&lt;/div&gt;
  &lt;div&gt;
   &amp;nbsp;&lt;/div&gt;
  &lt;div&gt;
   &lt;cite&gt;&lt;strong&gt;Interviewer:&lt;/strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;Why not 0?&lt;/cite&gt;&lt;/div&gt;
  &lt;div&gt;
   &amp;nbsp;&lt;/div&gt;
  &lt;div&gt;
   &lt;cite&gt;&lt;strong&gt;Candidate:&lt;/strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;The size seems to be 0 since the class is empty without any data. However, it should&lt;/cite&gt;&lt;/div&gt;
  &lt;div&gt;
   &lt;cite&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;occupy some memory; otherwise, it cannot be accessed. The size of memory is decided&lt;/cite&gt;&lt;/div&gt;
  &lt;div&gt;
   &lt;cite&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;by compilers. It occupies 1 byte in Visual Studio.&lt;/cite&gt;&lt;/div&gt;
  &lt;div&gt;
   &amp;nbsp;&lt;/div&gt;
  &lt;div&gt;
   &lt;cite&gt;&lt;strong&gt;Interviewer:&lt;/strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; What is the result of sizeof if we add the constructor and destructor functions in the&lt;/cite&gt;&lt;/div&gt;
  &lt;div&gt;
   &lt;cite&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;class?&lt;/cite&gt;&lt;/div&gt;
  &lt;div&gt;
   &amp;nbsp;&lt;/div&gt;
  &lt;div&gt;
   &lt;cite&gt;&lt;strong&gt;Candidate: &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/strong&gt;It is also 1. The addresses for functions are irrelevant to instances, and compilers do not&lt;/cite&gt;&lt;/div&gt;
  &lt;div&gt;
   &lt;cite&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;add any data in instances of this class.&lt;/cite&gt;&lt;/div&gt;
  &lt;div&gt;
   &amp;nbsp;&lt;/div&gt;
  &lt;div&gt;
   &lt;cite&gt;&lt;strong&gt;Interviewer:&lt;/strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;How about declaring the destructor function as a virtual function?&lt;/cite&gt;&lt;/div&gt;
  &lt;div&gt;
   &amp;nbsp;&lt;/div&gt;
  &lt;div&gt;
   &lt;strong&gt;Candidate: &amp;nbsp; &amp;nbsp;&lt;/strong&gt;&amp;nbsp; &amp;nbsp; When a C++ compiler sees a virtual function inside a class, it creates a virtual function&lt;/div&gt;
  &lt;div&gt;
   &lt;cite&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; table for the class and adds a pointer to the table in each instance. A pointer in a 32-bit&lt;/cite&gt;&lt;/div&gt;
  &lt;div&gt;
   &lt;cite&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; machine occupies 4 bytes, so the result &lt;/cite&gt;of&amp;nbsp;sizeof is 4. &lt;cite&gt;The result on a 64-bit machine is 8&lt;/cite&gt;&lt;/div&gt;
  &lt;div&gt;
   &lt;cite&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; because a pointer occupies 8 bytes there.&lt;/cite&gt;&lt;/div&gt;
  &lt;div&gt;
   &amp;nbsp;&lt;/div&gt;

&lt;var&gt;&lt;span style=&quot;font-size:16px;&quot;&gt;&lt;em&gt;&lt;span style=&quot;color: rgb(51, 51, 51); font-family: arial, tahoma, &#39;times New Roman&#39;; line-height: 22.390625px;&quot;&gt;&lt;span style=&quot;background-color:#ffffff;&quot;&gt;The size of the empty class is always ONE and not ZERO. This is because of below reasons&lt;/span&gt;&lt;/span&gt;&lt;br style=&quot;color: rgb(51, 51, 51); font-family: arial, tahoma, &#39;times New Roman&#39;; font-size: 14px; line-height: 22.390625px; background-color: rgb(250, 247, 237);&quot; /&gt;
   &lt;br style=&quot;color: rgb(51, 51, 51); font-family: arial, tahoma, &#39;times New Roman&#39;; font-size: 14px; line-height: 22.390625px; background-color: rgb(250, 247, 237);&quot; /&gt;
   &lt;span style=&quot;background-color:#ffffff;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: rgb(51, 51, 51); font-family: arial, tahoma, &#39;times New Roman&#39;; line-height: 22.390625px;&quot;&gt;&lt;span style=&quot;background-color:#ffffff;&quot;&gt;1. Every object should have some address location to identify, independent of the no.of member variables in the class. Generally the size of the object is the sum of the member variables in the class. if no data is there, one byte of memory allocated to the object to identify. this will help to not conflicting with other empty objects.&lt;/span&gt;&lt;/span&gt;&lt;br style=&quot;color: rgb(51, 51, 51); font-family: arial, tahoma, &#39;times New Roman&#39;; font-size: 14px; line-height: 22.390625px; background-color: rgb(250, 247, 237);&quot; /&gt;
   &lt;br style=&quot;color: rgb(51, 51, 51); font-family: arial, tahoma, &#39;times New Roman&#39;; font-size: 14px; line-height: 22.390625px; background-color: rgb(250, 247, 237);&quot; /&gt;
   &lt;span style=&quot;background-color:#ffffff;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: rgb(51, 51, 51); font-family: arial, tahoma, &#39;times New Roman&#39;; line-height: 22.390625px;&quot;&gt;&lt;span style=&quot;background-color:#ffffff;&quot;&gt;2. One more&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;b style=&quot;color: rgb(51, 51, 51); font-family: arial, tahoma, &#39;times New Roman&#39;; font-size: 14px; line-height: 22.390625px; background-color: rgb(250, 247, 237);&quot;&gt;&lt;span style=&quot;background-color:#ffffff;&quot;&gt;logical&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;color: rgb(51, 51, 51); font-family: arial, tahoma, &#39;times New Roman&#39;; line-height: 22.390625px;&quot;&gt;&lt;span style=&quot;background-color:#ffffff;&quot;&gt;&amp;nbsp;reason is that Some times we will use the sizeof() operator in division operation like X%&lt;/span&gt;&lt;/span&gt;&lt;i style=&quot;color: rgb(51, 51, 51); font-family: arial, tahoma, &#39;times New Roman&#39;; font-size: 14px; line-height: 22.390625px; background-color: rgb(250, 247, 237);&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;background-color:#ffffff;&quot;&gt;sizeof&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;span style=&quot;color: rgb(51, 51, 51); font-family: arial, tahoma, &#39;times New Roman&#39;; line-height: 22.390625px;&quot;&gt;&lt;span style=&quot;background-color:#ffffff;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: arial, tahoma, &#39;times New Roman&#39;; line-height: 22.390625px; color: rgb(204, 0, 0);&quot;&gt;&lt;span style=&quot;background-color:#ffffff;&quot;&gt;emptyclass&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: rgb(51, 51, 51); font-family: arial, tahoma, &#39;times New Roman&#39;; line-height: 22.390625px;&quot;&gt;&lt;span style=&quot;background-color:#ffffff;&quot;&gt;) and if the value of empty class is zero , application/program will crash.&lt;/span&gt;&lt;/span&gt;&lt;/em&gt;&lt;/span&gt;&lt;/var&gt;
&lt;/br&gt;
&lt;/br&gt;
  &lt;div&gt;
   Hope U liked it...&lt;/div&gt;
 &lt;/body&gt;

&lt;br /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dcoders01.blogspot.com/feeds/6151055379263322497/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://dcoders01.blogspot.com/2013/08/what-is-size-of-empty-class.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6097953565153021725/posts/default/6151055379263322497'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6097953565153021725/posts/default/6151055379263322497'/><link rel='alternate' type='text/html' href='http://dcoders01.blogspot.com/2013/08/what-is-size-of-empty-class.html' title='What is the size of a EMPTY class!!!'/><author><name>Navneet</name><uri>http://www.blogger.com/profile/08782557583312965286</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6097953565153021725.post-5336837306524016876</id><published>2013-08-13T12:40:00.000-07:00</published><updated>2013-08-14T12:57:49.024-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="class"/><category scheme="http://www.blogger.com/atom/ns#" term="data structure"/><category scheme="http://www.blogger.com/atom/ns#" term="linked list"/><category scheme="http://www.blogger.com/atom/ns#" term="reverse"/><title type='text'>So much with linked list..</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;h3&gt; Linked list &lt;/h3&gt;
&lt;/br&gt;

General structure and functions :-
&lt;/br&gt;
&lt;/br&gt;

&lt;body&gt;
  &lt;pre class=&quot;de1&quot; style=&quot;margin-top: 0px; margin-bottom: 0px; padding-bottom: 5px; padding-top: 3px; padding-left: 10px; color: rgb(0, 0, 102); font-size: 13px; background-color: rgb(250, 250, 250);&quot;&gt;
  &lt;span class=&quot;kw4&quot; style=&quot;line-height: 13px !important; color: rgb(0, 0, 255);&quot;&gt;

struct&lt;/span&gt; node
&lt;span class=&quot;br0&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 0);&quot;&gt;{&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span class=&quot;kw4&quot; style=&quot;line-height: 13px !important; color: rgb(0, 0, 255);&quot;&gt;int&lt;/span&gt; x&lt;span class=&quot;sy4&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 128);&quot;&gt;;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; node&lt;span class=&quot;sy2&quot; style=&quot;line-height: 13px !important; color: rgb(0, 0, 64);&quot;&gt;*&lt;/span&gt; next&lt;span class=&quot;sy4&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 128);&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;br0&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 0);&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;sy4&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 128);&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
&lt;span class=&quot;kw2&quot; style=&quot;line-height: 13px !important; color: rgb(0, 0, 255);&quot;&gt;class&lt;/span&gt; llist
&lt;span class=&quot;br0&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 0);&quot;&gt;{&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; node &lt;span class=&quot;sy2&quot; style=&quot;line-height: 13px !important; color: rgb(0, 0, 64);&quot;&gt;*&lt;/span&gt;list&lt;span class=&quot;sy4&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 128);&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kw2&quot; style=&quot;line-height: 13px !important; color: rgb(0, 0, 255);&quot;&gt;public&lt;/span&gt;&lt;span class=&quot;sy4&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 128);&quot;&gt;:&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; llist&lt;span class=&quot;br0&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 0);&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;br0&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 0);&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;sy4&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 128);&quot;&gt;;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span class=&quot;kw4&quot; style=&quot;line-height: 13px !important; color: rgb(0, 0, 255);&quot;&gt;void&lt;/span&gt; addnode&lt;span class=&quot;br0&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 0);&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kw4&quot; style=&quot;line-height: 13px !important; color: rgb(0, 0, 255);&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;br0&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 0);&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;sy4&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 128);&quot;&gt;;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span class=&quot;kw4&quot; style=&quot;line-height: 13px !important; color: rgb(0, 0, 255);&quot;&gt;void&lt;/span&gt; print&lt;span class=&quot;br0&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 0);&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;br0&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 0);&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;sy4&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 128);&quot;&gt;;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span class=&quot;kw4&quot; style=&quot;line-height: 13px !important; color: rgb(0, 0, 255);&quot;&gt;void&lt;/span&gt; count&lt;span class=&quot;br0&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 0);&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kw4&quot; style=&quot;line-height: 13px !important; color: rgb(0, 0, 255);&quot;&gt;int&lt;/span&gt; x&lt;span class=&quot;br0&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 0);&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;sy4&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 128);&quot;&gt;;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span class=&quot;kw4&quot; style=&quot;line-height: 13px !important; color: rgb(0, 0, 255);&quot;&gt;void&lt;/span&gt; getnth&lt;span class=&quot;br0&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 0);&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kw4&quot; style=&quot;line-height: 13px !important; color: rgb(0, 0, 255);&quot;&gt;int&lt;/span&gt; x&lt;span class=&quot;br0&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 0);&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;sy4&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 128);&quot;&gt;;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span class=&quot;kw4&quot; style=&quot;line-height: 13px !important; color: rgb(0, 0, 255);&quot;&gt;void&lt;/span&gt; getnth1&lt;span class=&quot;br0&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 0);&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kw4&quot; style=&quot;line-height: 13px !important; color: rgb(0, 0, 255);&quot;&gt;int&lt;/span&gt; x&lt;span class=&quot;br0&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 0);&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;sy4&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 128);&quot;&gt;;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span class=&quot;kw4&quot; style=&quot;line-height: 13px !important; color: rgb(0, 0, 255);&quot;&gt;void&lt;/span&gt; dellist&lt;span class=&quot;br0&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 0);&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;br0&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 0);&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;sy4&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 128);&quot;&gt;;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span class=&quot;kw4&quot; style=&quot;line-height: 13px !important; color: rgb(0, 0, 255);&quot;&gt;int&lt;/span&gt; pop&lt;span class=&quot;br0&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 0);&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;br0&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 0);&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;sy4&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 128);&quot;&gt;;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span class=&quot;kw4&quot; style=&quot;line-height: 13px !important; color: rgb(0, 0, 255);&quot;&gt;void&lt;/span&gt; insertnth&lt;span class=&quot;br0&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 0);&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kw4&quot; style=&quot;line-height: 13px !important; color: rgb(0, 0, 255);&quot;&gt;int&lt;/span&gt;,&lt;span class=&quot;kw4&quot; style=&quot;line-height: 13px !important; color: rgb(0, 0, 255);&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;br0&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 0);&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;sy4&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 128);&quot;&gt;;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span class=&quot;kw4&quot; style=&quot;line-height: 13px !important; color: rgb(0, 0, 255);&quot;&gt;void&lt;/span&gt; sortedinsert&lt;span class=&quot;br0&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 0);&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kw4&quot; style=&quot;line-height: 13px !important; color: rgb(0, 0, 255);&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;br0&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 0);&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;sy4&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 128);&quot;&gt;;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span class=&quot;kw4&quot; style=&quot;line-height: 13px !important; color: rgb(0, 0, 255);&quot;&gt;void&lt;/span&gt; insertsort&lt;span class=&quot;br0&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 0);&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;br0&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 0);&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;sy4&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 128);&quot;&gt;;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span class=&quot;kw4&quot; style=&quot;line-height: 13px !important; color: rgb(0, 0, 255);&quot;&gt;void&lt;/span&gt; append&lt;span class=&quot;br0&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 0);&quot;&gt;(&lt;/span&gt;llist&lt;span class=&quot;sy3&quot; style=&quot;line-height: 13px !important; color: rgb(0, 0, 64);&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;br0&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 0);&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;sy4&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 128);&quot;&gt;;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span class=&quot;kw4&quot; style=&quot;line-height: 13px !important; color: rgb(0, 0, 255);&quot;&gt;void&lt;/span&gt; frontbacksplit&lt;span class=&quot;br0&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 0);&quot;&gt;(&lt;/span&gt;llist&lt;span class=&quot;sy3&quot; style=&quot;line-height: 13px !important; color: rgb(0, 0, 64);&quot;&gt;&amp;amp;&lt;/span&gt;,llist&lt;span class=&quot;sy3&quot; style=&quot;line-height: 13px !important; color: rgb(0, 0, 64);&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;br0&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 0);&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;sy4&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 128);&quot;&gt;;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span class=&quot;kw4&quot; style=&quot;line-height: 13px !important; color: rgb(0, 0, 255);&quot;&gt;void&lt;/span&gt; removeduplicates&lt;span class=&quot;br0&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 0);&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;br0&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 0);&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;sy4&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 128);&quot;&gt;;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span class=&quot;kw4&quot; style=&quot;line-height: 13px !important; color: rgb(0, 0, 255);&quot;&gt;void&lt;/span&gt; movenode&lt;span class=&quot;br0&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 0);&quot;&gt;(&lt;/span&gt;llist&lt;span class=&quot;sy3&quot; style=&quot;line-height: 13px !important; color: rgb(0, 0, 64);&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;br0&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 0);&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;sy4&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 128);&quot;&gt;;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span class=&quot;kw4&quot; style=&quot;line-height: 13px !important; color: rgb(0, 0, 255);&quot;&gt;void&lt;/span&gt; alternatingspilt&lt;span class=&quot;br0&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 0);&quot;&gt;(&lt;/span&gt;llist&lt;span class=&quot;sy3&quot; style=&quot;line-height: 13px !important; color: rgb(0, 0, 64);&quot;&gt;&amp;amp;&lt;/span&gt;,llist&lt;span class=&quot;sy3&quot; style=&quot;line-height: 13px !important; color: rgb(0, 0, 64);&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;br0&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 0);&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;sy4&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 128);&quot;&gt;;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span class=&quot;kw4&quot; style=&quot;line-height: 13px !important; color: rgb(0, 0, 255);&quot;&gt;void&lt;/span&gt; sufflemerge&lt;span class=&quot;br0&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 0);&quot;&gt;(&lt;/span&gt;llist&lt;span class=&quot;sy3&quot; style=&quot;line-height: 13px !important; color: rgb(0, 0, 64);&quot;&gt;&amp;amp;&lt;/span&gt;,llist&lt;span class=&quot;sy3&quot; style=&quot;line-height: 13px !important; color: rgb(0, 0, 64);&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;br0&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 0);&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;sy4&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 128);&quot;&gt;;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span class=&quot;kw4&quot; style=&quot;line-height: 13px !important; color: rgb(0, 0, 255);&quot;&gt;void&lt;/span&gt; sortedintersection&lt;span class=&quot;br0&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 0);&quot;&gt;(&lt;/span&gt;llist&lt;span class=&quot;sy3&quot; style=&quot;line-height: 13px !important; color: rgb(0, 0, 64);&quot;&gt;&amp;amp;&lt;/span&gt;,llist&lt;span class=&quot;sy3&quot; style=&quot;line-height: 13px !important; color: rgb(0, 0, 64);&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;br0&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 0);&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;sy4&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 128);&quot;&gt;;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span class=&quot;kw4&quot; style=&quot;line-height: 13px !important; color: rgb(0, 0, 255);&quot;&gt;void&lt;/span&gt; reverse2&lt;span class=&quot;br0&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 0);&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;br0&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 0);&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;sy4&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 128);&quot;&gt;;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span class=&quot;kw4&quot; style=&quot;line-height: 13px !important; color: rgb(0, 0, 255);&quot;&gt;void&lt;/span&gt; reverse&lt;span class=&quot;br0&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 0);&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;br0&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 0);&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;sy4&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 128);&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;br0&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 0);&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;sy4&quot; style=&quot;line-height: 13px !important; color: rgb(0, 128, 128);&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;
 &lt;/body&gt;
&lt;/br&gt;
&lt;iframe src=&quot;https://docs.google.com/file/d/0B1Ee5jRs3kX6cW1YaW41VFNyZzg/preview&quot; width=&quot;640&quot; height=&quot;480&quot;&gt;&lt;/iframe&gt;
&lt;/br&gt;
&lt;/br&gt;
&lt;a href=&quot;https://docs.google.com/file/d/0B1Ee5jRs3kX6cW1YaW41VFNyZzg/edit?usp=sharing&quot;&gt; Link to Download &lt;/a&gt;
&lt;/br&gt;
&lt;/br&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;http://ideone.com/api/embed.js/link/mSldoF&quot;&gt;&lt;/script&gt;
&lt;br /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dcoders01.blogspot.com/feeds/5336837306524016876/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://dcoders01.blogspot.com/2013/08/so-much-with-linked-list.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6097953565153021725/posts/default/5336837306524016876'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6097953565153021725/posts/default/5336837306524016876'/><link rel='alternate' type='text/html' href='http://dcoders01.blogspot.com/2013/08/so-much-with-linked-list.html' title='So much with linked list..'/><author><name>Navneet</name><uri>http://www.blogger.com/profile/08782557583312965286</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6097953565153021725.post-2318754178006111604</id><published>2013-08-12T12:16:00.000-07:00</published><updated>2013-08-13T17:46:03.956-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="topcoder"/><title type='text'>Topcoder-588 Div-2 Easy-250</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;h3&gt; Topcoder-588 Div-2 Easy-250 &lt;/h3&gt;
&lt;br/&gt;
Simple ad-hoc problem.. One should get logic just by reading the problem statement..
&lt;/br&gt;
&lt;br/&gt;
&lt;a href=&quot;http://community.topcoder.com/stat?c=problem_statement&amp;pm=12714&amp;rd=15700&quot;&gt; problem statement &lt;/a&gt;
&lt;br/&gt;
&lt;br/&gt;
My solution:-

&lt;/br&gt;

&lt;script type=&quot;text/javascript&quot; src=&quot;http://ideone.com/api/embed.js/link/3Jb3In&quot;&gt;&lt;/script&gt;
&lt;br/&gt;
&lt;br/&gt;
Another easier way :-
&lt;br/&gt;
&lt;br/&gt;

&lt;script type=&quot;text/javascript&quot; src=&quot;http://ideone.com/api/embed.js/link/j8DWr5&quot;&gt;&lt;/script&gt;
&lt;/br&gt;
&lt;br/&gt;
&lt;br/&gt;

Regarding any doubt.. Don&#39;t forget to comment..
&lt;br/&gt;&lt;br/&gt;&lt;br/&gt;
&lt;br /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dcoders01.blogspot.com/feeds/2318754178006111604/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://dcoders01.blogspot.com/2013/08/topcoder-588-div-2-easy-250-simple-ad.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6097953565153021725/posts/default/2318754178006111604'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6097953565153021725/posts/default/2318754178006111604'/><link rel='alternate' type='text/html' href='http://dcoders01.blogspot.com/2013/08/topcoder-588-div-2-easy-250-simple-ad.html' title='Topcoder-588 Div-2 Easy-250'/><author><name>Navneet</name><uri>http://www.blogger.com/profile/08782557583312965286</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6097953565153021725.post-4456184693034568298</id><published>2013-08-12T07:37:00.002-07:00</published><updated>2013-08-13T17:47:05.933-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="dp"/><category scheme="http://www.blogger.com/atom/ns#" term="dynamic programming"/><category scheme="http://www.blogger.com/atom/ns#" term="maths"/><title type='text'>nCr using DP.. </title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;h3&gt; Calculate &lt;sup&gt;n&lt;/sup&gt;C&lt;sub&gt;r&lt;/sub&gt; with out having overflow when it is guaranteed that the final result will not overflow:&lt;/h3&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;From pascal&#39;s triangular relation, we get,&lt;br /&gt;&lt;sup&gt;n&lt;/sup&gt;C&lt;sub&gt;r&lt;/sub&gt; = &lt;sup&gt;n-1&lt;/sup&gt;C&lt;sub&gt;r&lt;/sub&gt; + &lt;sup&gt;n-1&lt;/sup&gt;C&lt;sub&gt;r-1&lt;/sub&gt;&lt;br /&gt;&lt;br /&gt;Using this recursive formula directly will lead the program to exceed the time limit, as this may calculate the same value for many times which is un-necessary and we can remove this part by saving the states which means by using dynamic programming concepts.&lt;br /&gt;&lt;br /&gt;In this formulation, one thing is to be noted that n and r keep decreasing, and sometimes is is possible that n becomes smaller than r. So considering these cases we get our base conditions for the recursive formula.&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;We know,&lt;br /&gt;&lt;sup&gt;n&lt;/sup&gt;C&lt;sub&gt;n&lt;/sub&gt; = 1&lt;br /&gt;&lt;sup&gt;n&lt;/sup&gt;C&lt;sub&gt;1&lt;/sub&gt; = n&lt;br /&gt;&lt;sup&gt;n&lt;/sup&gt;C&lt;sub&gt;0&lt;/sub&gt; = 1&lt;br /&gt;and&lt;br /&gt;&lt;sup&gt;n&lt;/sup&gt;C&lt;sub&gt;r&lt;/sub&gt; = &lt;sup&gt;n-1&lt;/sup&gt;C&lt;sub&gt;r&lt;/sub&gt; + &lt;sup&gt;n-1&lt;/sup&gt;C&lt;sub&gt;r-1&lt;/sub&gt;&lt;br /&gt;&lt;br /&gt;So, we can build the recursive function as follows:&lt;pre class=&quot;brush: plain; wrap-lines: false;&quot;&gt;&lt;br /&gt;function nCr(n, r):&lt;br /&gt;    if n == r:&lt;br /&gt;        return 1&lt;br /&gt;    if r == 1:&lt;br /&gt;        return n&lt;br /&gt;    if r == 0:&lt;br /&gt;        return 1&lt;br /&gt;    return nCr(n-1, r) + nCr(n-1, r-1)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;Now, to reduce recursive steps, we maintain a table for saving the values of &lt;sup&gt;n&lt;/sup&gt;C&lt;sub&gt;r&lt;/sub&gt; of intermediate steps. So, when we face a sub-problem which is already solved, we can look up its value from the pre-calculation table.&lt;/div&gt;&lt;pre class=&quot;brush: plain; wrap-lines: false&quot;&gt;&lt;br /&gt;table dp[N][R]&lt;br /&gt;&lt;br /&gt;function nCr(n, r):&lt;br /&gt;    if n == r:&lt;br /&gt;        dp[n][r] = 1&lt;br /&gt;    if r == 1:&lt;br /&gt;        dp[n][r] = n&lt;br /&gt;    if r == 0:&lt;br /&gt;        dp[n][r] = 1&lt;br /&gt;    if dp[n][r] is not yet calculated:&lt;br /&gt;        dp[n][r] = nCr(n-1,r) + nCr(n-1,r-1)&lt;br /&gt;    return dp[n][r]&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;Here is a sample code written in C++ which demonstrates the idea. (It is assumed that MAX N is 65 and N &gt;= R).&lt;/div&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;http://ideone.com/api/embed.js/link/YAfbW8&quot;&gt;&lt;/script&gt;
&lt;div style=&#39;clear: both;&#39;&gt;&lt;/div&gt;
&lt;br /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dcoders01.blogspot.com/feeds/4456184693034568298/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://dcoders01.blogspot.com/2013/08/ncr-using-dp.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6097953565153021725/posts/default/4456184693034568298'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6097953565153021725/posts/default/4456184693034568298'/><link rel='alternate' type='text/html' href='http://dcoders01.blogspot.com/2013/08/ncr-using-dp.html' title='nCr using DP.. '/><author><name>Navneet</name><uri>http://www.blogger.com/profile/08782557583312965286</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6097953565153021725.post-4238756894354337684</id><published>2013-08-12T06:17:00.002-07:00</published><updated>2013-08-13T17:48:18.704-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="gcd"/><category scheme="http://www.blogger.com/atom/ns#" term="maths"/><title type='text'>GCD really!!!</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;http://ideone.com/api/embed.js/link/WZdRyE&quot;&gt;&lt;/script&gt;
&lt;br /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dcoders01.blogspot.com/feeds/4238756894354337684/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://dcoders01.blogspot.com/2013/08/gcd-really.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6097953565153021725/posts/default/4238756894354337684'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6097953565153021725/posts/default/4238756894354337684'/><link rel='alternate' type='text/html' href='http://dcoders01.blogspot.com/2013/08/gcd-really.html' title='GCD really!!!'/><author><name>Navneet</name><uri>http://www.blogger.com/profile/08782557583312965286</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6097953565153021725.post-5792232472646130402</id><published>2013-08-12T05:40:00.000-07:00</published><updated>2013-08-12T05:40:23.379-07:00</updated><title type='text'>Welcome Dcoders</title><content type='html'>&lt;script type=&quot;text/javascript&quot; src=&quot;http://ideone.com/api/embed.js/link/AuIpqn&quot;&gt;&lt;/script&gt;</content><link rel='replies' type='application/atom+xml' href='http://dcoders01.blogspot.com/feeds/5792232472646130402/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://dcoders01.blogspot.com/2013/08/blog-post.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6097953565153021725/posts/default/5792232472646130402'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6097953565153021725/posts/default/5792232472646130402'/><link rel='alternate' type='text/html' href='http://dcoders01.blogspot.com/2013/08/blog-post.html' title='Welcome Dcoders'/><author><name>Navneet</name><uri>http://www.blogger.com/profile/08782557583312965286</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>