<?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-1124334728283106452</id><updated>2024-11-05T19:03:24.859-08:00</updated><category term="Uva Problems &amp; Solution"/><category term="Algorithm Design and Analysis"/><category term="Data Structure"/><category term="Uva - Ad Hoc"/><category term="Discrete Mathematics"/><category term="Bitwise Operator"/><category term="Dynamic Programming"/><category term="Uva - Recurrence Relation"/><category term="Number System"/><category term="Programming in C"/><category term="Uva - Dynamic Programming"/><category term="Uva - LIS"/><category term="Uva - Math"/><category term="Uva - Queue"/><category term="Counting"/><category term="Data Communication"/><category term="Digital Signal Processing"/><category term="Recurrence Relation"/><category term="Uva - Big Number"/><category term="Uva - Sorting"/><category term="Computer Network"/><category term="LightOJ"/><category term="LightOJ-Math"/><category term="Uva - Binary Tree"/><category term="Uva - Bitwise Operation"/><category term="Uva - Convex Hull"/><category term="Uva - Counting"/><category term="Uva - Euler Circuits &amp; Shortest Path"/><category term="Uva - Geometric Sequences and Sums"/><category term="Uva - Kadane&#39;s Algorithm"/><category term="Uva - LCS"/><category term="Uva - LDS"/><category term="Uva - Math (Counting)"/><category term="Uva - Prime Numbers"/><category term="Uva - Shortest Path"/><category term="Uva - Simulation Problem"/><category term="Uva - String Processing"/><category term="Uva- Primitive Pythagorean Triples Uva Problems &amp; Solution"/><title type='text'>Rizoan Toufiq</title><subtitle type='html'>&quot;They plan, and Allah plans. Surely, Allah is the Best of planners.&quot; #Quran 8:30</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://rizoantoufiq.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1124334728283106452/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://rizoantoufiq.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='http://www.blogger.com/feeds/1124334728283106452/posts/default?start-index=26&amp;max-results=25&amp;redirect=false'/><author><name>Unknown</name><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>74</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-1124334728283106452.post-1001321080350395830</id><published>2016-08-18T10:50:00.003-07:00</published><updated>2016-08-18T11:01:15.323-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Data Structure"/><title type='text'>Data Structure: Linked List - Part 8</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;

&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;b&gt;Circular header linked list হতে একটি node বাদ দেওয়ার পদ্ধতি&lt;/b&gt;&lt;/div&gt;
&lt;br /&gt;

&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;b&gt;সমস্যাঃ &lt;/b&gt;একটি node (যার data মান x) header linked list হতে delete (বাদ) দিতে হবে।&lt;/div&gt;

&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;b&gt;সমাধান:&lt;/b&gt;প্রথমে আমাকে linked list এর x মান বিশিষ্ট node টি খুঁজে বের করতে হবে। ধরি, LOC pointer টি নির্দিষ্ট node কে point করে থাকবে আর LOCP নির্দিষ্ট node এর পূরবর্তী node কে point করে থাকবে। x মান বিশিষ্ট যদি linked list এ না থাকে তাহলে “Not Found” দেখাবে।&lt;/div&gt;

&lt;div style=&quot;text-align: justify;&quot;&gt;
কিছু শর্ততে ভাগ করে এখন Deletion পদ্ধতিটি আলোচনা করব।&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;
&lt;/div&gt;

&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;b&gt;শর্ত-১&lt;/b&gt; linked list এ কোন node না থাকে, অর্থাৎ head←∧, তখন “Underflow/Empty” দেখাবে।&lt;/div&gt;



&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;b&gt;শর্ত-২&lt;/b&gt; যদি head কে delete করতে হলে অর্থাৎ LOC←head or head↑.INFO = x&lt;/div&gt;

&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEicjwDaaNMER0zvNxFCS_31cybXsd5wHsdBPm0iASPzmzSAChQukrggTnUIOGbqr6N4a69J0Sg9jtKCYB191dP7ca8cID8A6GgORHeKqGDXyBcrE-PYtQiwT6scdhpAYbo5mKefjlY9OU9I/s1600/CLL_IN_7.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;66&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEicjwDaaNMER0zvNxFCS_31cybXsd5wHsdBPm0iASPzmzSAChQukrggTnUIOGbqr6N4a69J0Sg9jtKCYB191dP7ca8cID8A6GgORHeKqGDXyBcrE-PYtQiwT6scdhpAYbo5mKefjlY9OU9I/s400/CLL_IN_7.jpg&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;


&lt;br /&gt;
তাহলে instruction হবে, &lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;b&gt;head← head↑.LINK&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;tail↑.LINK←head&lt;/b&gt;&lt;/blockquote&gt;
&lt;br /&gt;
&lt;b&gt;শর্ত-৩&lt;/b&gt; নির্দিষ্ট node (যার data মান x) খুঁজে পাওয়া যায় তাহলে  LOC≠head&lt;br /&gt;


&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgrnp8VaMPhVngAsBxJOogH0TDhcJtrQ5Q3OysZxfCbYv79Afo2k_56u5rQA14TFbuUS-aZo66-CgbzGCBflgcmePMpeLrMui_NKUOx9Ml06vgiH_LYi2Wnt4uq0f1sYpYYRdfJ-Tiyk2NN/s1600/CLL_DEL_1.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;85&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgrnp8VaMPhVngAsBxJOogH0TDhcJtrQ5Q3OysZxfCbYv79Afo2k_56u5rQA14TFbuUS-aZo66-CgbzGCBflgcmePMpeLrMui_NKUOx9Ml06vgiH_LYi2Wnt4uq0f1sYpYYRdfJ-Tiyk2NN/s400/CLL_DEL_1.jpg&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;

LOC pointer টি নির্দিষ্ট node কে point করে থাকবে আর LOCP নির্দিষ্ট node এর পূরবর্তী node কে point করে থাকবে। তাহলে instruction হবে, &lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;b&gt;LOCP↑.LINK ← LOC↑.LINK&lt;/b&gt;&lt;/blockquote&gt;
যদি LOC tail node কে point করে থাকে তাহলে &lt;br /&gt;

&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgDJlh3hUcW4hRwnNWaB0a8uDMXHqpXMbcEI1FZWdsdtNTvtMoT_bUeZJQsC2a3nRg7-3DSABf2hZcj8vNuChee-wcDbdDsJbDxB4qtO4vu3GwnqgiDmfBrw65SSUuJLOhQWm75YEyO6xsY/s1600/CLL_DEL_2.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;85&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgDJlh3hUcW4hRwnNWaB0a8uDMXHqpXMbcEI1FZWdsdtNTvtMoT_bUeZJQsC2a3nRg7-3DSABf2hZcj8vNuChee-wcDbdDsJbDxB4qtO4vu3GwnqgiDmfBrw65SSUuJLOhQWm75YEyO6xsY/s400/CLL_DEL_2.jpg&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;


&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;b&gt;tail←LOCP&lt;/b&gt;&lt;/blockquote&gt;
&lt;br /&gt;
&lt;b&gt;শর্ত-৪&lt;/b&gt; যদি x মান বিশিষ্ট কোন node পাওয়া না যায় অর্থাৎ LOC=head, তাহলে “Not Found” দেখাবে।&lt;br /&gt;
&lt;b&gt;Algorithm:&lt;/b&gt;
&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
This algorithm delete a node from a linked list (head pointer) which data field is x.&lt;br /&gt;
1.	If head = ∧, then: [CASE-I]&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Write: Empty and exit.&lt;br /&gt;
2.	if head↑.INFO = x, then: [CASE-II]&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; head← head↑.LINK, tail↑.LINK←head and exit.&lt;br /&gt;
3.	LOC←head↑.LINK and LOCP ← head&lt;br /&gt;
4.	Repeat step 5 while LOC≠head and LOC↑.INFO≠x&lt;br /&gt;
5.	&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;                     LOCP←LOC and LOC←LOC↑.LINK&lt;br /&gt;
6.	IF LOC≠head then: [CASE-III]&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; LOCP↑.LINK←LOC↑.LINK&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; If LOC=tail then: tail←LOCP   &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; ELSE: [CASE-IV]&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Write: Not Found&lt;br /&gt;
7.	Exit.&lt;/blockquote&gt;

&lt;b&gt;Flow Chart:&lt;/b&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;b&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhxsJAzGDM2DFVuIKKg3hxLzFtAooSymUdOuqh9-JwzHyTTYNLmybkkvSR2KsD6fumIy6kPL4o8nbMMgIcuZsPJ6gWPcmhehfNzeRALnNmnEIQusCsloqyxQNhskFzVM0CZABOKfAyfQoKU/s1600/CLL_DE_FC.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;400&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhxsJAzGDM2DFVuIKKg3hxLzFtAooSymUdOuqh9-JwzHyTTYNLmybkkvSR2KsD6fumIy6kPL4o8nbMMgIcuZsPJ6gWPcmhehfNzeRALnNmnEIQusCsloqyxQNhskFzVM0CZABOKfAyfQoKU/s400/CLL_DE_FC.jpg&quot; width=&quot;350&quot; /&gt;&lt;/a&gt;&lt;/b&gt;&lt;/div&gt;

&lt;br /&gt;
&lt;b&gt;
Program:&lt;/b&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;

&lt;div style=&quot;background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;
&lt;table&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt; 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62&lt;/pre&gt;
&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;&lt;span style=&quot;color: #557799;&quot;&gt;#include&amp;lt;stdio.h&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #557799;&quot;&gt;#include&amp;lt;stdlib.h&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;struct&lt;/span&gt; node{
    &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; INFO;
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;struct&lt;/span&gt; node &lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;LINK;
};
&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;main&lt;/span&gt;(){
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;struct&lt;/span&gt; node &lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;q&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;,&lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;p&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;,&lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;head&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;,&lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;tail&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;,&lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;LOC&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;,&lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;LOCP&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;;
  &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; i,n&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;5&lt;/span&gt;,x;

  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt;(i&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;i&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;=&lt;/span&gt;n;i&lt;span style=&quot;color: #333333;&quot;&gt;++&lt;/span&gt;){
    q &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; (&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;struct&lt;/span&gt; node &lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;)malloc(&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;sizeof&lt;/span&gt;(&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;struct&lt;/span&gt; node));
    q&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;;
    q&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;INFO &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; i;

    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;(p&lt;span style=&quot;color: #333333;&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;)  head &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; q;
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt;
      p&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; q;
    p&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;q;
  }
  p&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;head;
  tail&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;p;

  &lt;span style=&quot;color: #888888;&quot;&gt;/*INSERTION (Successor)*/&lt;/span&gt;
  printf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;Insert the value of target node:&quot;&lt;/span&gt;);
  scanf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;%d&quot;&lt;/span&gt;,&lt;span style=&quot;color: #333333;&quot;&gt;&amp;amp;&lt;/span&gt;x);

  q &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; (&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;struct&lt;/span&gt; node &lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;)malloc(&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;sizeof&lt;/span&gt;(&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;struct&lt;/span&gt; node));
  q&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;;
  q&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;INFO &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;10&lt;/span&gt;;

  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;(head&lt;span style=&quot;color: #333333;&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;){
      printf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;Empty&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #666666; font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;&lt;/span&gt;);
  }
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; (head&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;INFO&lt;span style=&quot;color: #333333;&quot;&gt;==&lt;/span&gt;x){
      head &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; head&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK;
      tail&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;head;
  }
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt;{
    LOCP&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;head;LOC&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;head&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK;
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt;(LOC&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK&lt;span style=&quot;color: #333333;&quot;&gt;!=&lt;/span&gt;head &lt;span style=&quot;color: #333333;&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; LOC&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;INFO&lt;span style=&quot;color: #333333;&quot;&gt;!=&lt;/span&gt;x){
          LOCP&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;LOC;
          LOC&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;LOC&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK;
    }
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;(LOC&lt;span style=&quot;color: #333333;&quot;&gt;!=&lt;/span&gt;head){
        LOCP&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;LOC&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK;
        &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;(LOC&lt;span style=&quot;color: #333333;&quot;&gt;==&lt;/span&gt;tail) tail&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;LOCP;
    }
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt;
      printf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;Not Found&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #666666; font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;&lt;/span&gt;);

  }

  &lt;span style=&quot;color: #888888;&quot;&gt;/*Traverse the CHLL*/&lt;/span&gt;
  p&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;head;
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;do&lt;/span&gt;{
    printf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;%d &quot;&lt;/span&gt;,p&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;INFO);
    p&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;p&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK;
  }&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt;(p&lt;span style=&quot;color: #333333;&quot;&gt;!=&lt;/span&gt;head);

  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
}
&lt;/pre&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;

</content><link rel='replies' type='application/atom+xml' href='http://rizoantoufiq.blogspot.com/feeds/1001321080350395830/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://rizoantoufiq.blogspot.com/2016/08/data-structure-linked-list-part-8.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1124334728283106452/posts/default/1001321080350395830'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1124334728283106452/posts/default/1001321080350395830'/><link rel='alternate' type='text/html' href='http://rizoantoufiq.blogspot.com/2016/08/data-structure-linked-list-part-8.html' title='Data Structure: Linked List - Part 8'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEicjwDaaNMER0zvNxFCS_31cybXsd5wHsdBPm0iASPzmzSAChQukrggTnUIOGbqr6N4a69J0Sg9jtKCYB191dP7ca8cID8A6GgORHeKqGDXyBcrE-PYtQiwT6scdhpAYbo5mKefjlY9OU9I/s72-c/CLL_IN_7.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1124334728283106452.post-5453135398589489352</id><published>2016-08-18T09:41:00.001-07:00</published><updated>2016-08-18T09:42:25.032-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Data Structure"/><title type='text'>Data Structure: Linked List : Part 7</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;b&gt;একটি নতুন node কে Circular header linked list এ সংযুক্ত করার পদ্ধতি&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;b&gt;সমস্যাঃ&lt;/b&gt; একটি নতুন node linked list এর একটি node (যার data মান x) ও তার পরবর্তি (successor) node এর মাঝে insert (যুক্ত) করতে হবে। &lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;b&gt;সমাধান:&lt;/b&gt;প্রথমে আমাকে linked list এর x মান বিশিষ্ট node টি খুঁজে বের করতে হবে। ধরি, LOC pointer টি নির্দিষ্ট node কে point করে আছে। x মান বিশিষ্ট node যদি linked list এ না থাকে তাহলে তা linked list এর শেষে যুক্ত হবে অর্থাৎ LOC pointer শেষ node কে point করে থাকবে।&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
কিছু শর্ত নিয়ে এখন আলোচনা করব।&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;b&gt;শর্ত-১&lt;/b&gt; linked list এ কোন node না থাকে, অর্থাৎ head←∧&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg9G_WIY25wMiA6SMgfit1IjAxVN4xjNg8L-5FDJckV9JbVIXwl8_HuS3ufdIDRQ1nvcTgxpI5_KxeM8_CdS3AZAj4YVCFGN-nG-cH8KQN7f_Qrnnl3K2ubfvdDitEnwqcBErIlxgjc5o0h/s1600/CLL_IN_1.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg9G_WIY25wMiA6SMgfit1IjAxVN4xjNg8L-5FDJckV9JbVIXwl8_HuS3ufdIDRQ1nvcTgxpI5_KxeM8_CdS3AZAj4YVCFGN-nG-cH8KQN7f_Qrnnl3K2ubfvdDitEnwqcBErIlxgjc5o0h/s1600/CLL_IN_1.jpg&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
q pointer যদি নতুন node কে point করে থাকে তাহলে নতুন node টি head node হবে। তাহলে instruction হবে,&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;b&gt;head←q&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;tail←q&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;head↑.LINK←q&lt;/b&gt;&lt;/blockquote&gt;
&lt;b&gt;শর্ত-২&lt;/b&gt; linked list এর x মান বিশিষ্ট node টি খুঁজে বের করতে হবে (LOC point করে থাকবে)। যদি node টি tail node না হলে, &lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhCry6HHcye-ZE0gB-YcF_BYMEQKRVlz215UvFp309icj1gylsbiRFKSuxlnZ9WVfjyngzHc9a-13TjL8KnFYLZ-W3Wxph6jwmNzySl-neSbH2CaNQP1kljIRBnsL8ZJvdJ5xSFQLw0W0Z4/s1600/CLL_IN_5.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;96&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhCry6HHcye-ZE0gB-YcF_BYMEQKRVlz215UvFp309icj1gylsbiRFKSuxlnZ9WVfjyngzHc9a-13TjL8KnFYLZ-W3Wxph6jwmNzySl-neSbH2CaNQP1kljIRBnsL8ZJvdJ5xSFQLw0W0Z4/s400/CLL_IN_5.jpg&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
q pointer যদি নতুন node কে point করে থাকে তাহলে instruction হবে&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;b&gt;q↑.LINK←LOC↑.LINK&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;LOC↑.LINK←q&lt;/b&gt;&lt;/blockquote&gt;
শর্ত-৩ linked list এর x মান বিশিষ্ট node টি খুঁজে বের করতে হবে (LOC point করে থাকবে)। যদি node টি tail node হলে,&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgPNd21fuhMcJxPM8T3czTzv0x2NuzUn0O9dccorW4rPxKiv_bIiDZQbf1OMAOcfgRtU1rv6_BKPLA5jdjOvNv4qMIfhreGUNjMug-3kXDbQz7meTq0NN58-AQ0QsVvRpuO_7yKms5VZ7Hk/s1600/CLL_IN_6.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;96&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgPNd21fuhMcJxPM8T3czTzv0x2NuzUn0O9dccorW4rPxKiv_bIiDZQbf1OMAOcfgRtU1rv6_BKPLA5jdjOvNv4qMIfhreGUNjMug-3kXDbQz7meTq0NN58-AQ0QsVvRpuO_7yKms5VZ7Hk/s400/CLL_IN_6.jpg&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;b&gt;q↑.LINK←head&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;LOC↑.LINK←q&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;tail←q&lt;/b&gt;&lt;/blockquote&gt;
&lt;b&gt;Algorithm&lt;/b&gt;&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
This algorithm insert a new node between the node which data field is x and its successor&lt;br /&gt;
1. Call New_Node(q)&lt;br /&gt;
2. If head = ∧, then: [CASE-I]&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; head←q, tail←q, head↑.LINK←q and exit.&lt;br /&gt;
3. LOC←head&lt;br /&gt;
4. Repeat step 5 while LOC↑.LINK≠head and LOC↑.INFO≠x&lt;br /&gt;
5.&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;                      LOC←LOC↑.LINK&lt;br /&gt;
6. q↑.LINK←LOC↑.LINK and LOC↑.LINK←q &lt;br /&gt;
7. if LOC=tail then: tail←q;&lt;br /&gt;
8. Exit.&lt;/blockquote&gt;
&lt;b&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;FLOW CHART:&lt;/b&gt;&lt;b&gt;&lt;/b&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;b&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgg1mMb6K-2ElaZ1B4inzsF_cw3HIOM-B0VW5f_RyiTMjwI6B-P9f-qfPJTnPRHUCu5UAzEgn8JmlvJPKy7yUjCubPD43rakxK-25O7MQo4YK-3YO06CbFFDIPhWrGyjFN4gRG8Nil0Kupq/s1600/CLL_IN_FC_1.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;400&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgg1mMb6K-2ElaZ1B4inzsF_cw3HIOM-B0VW5f_RyiTMjwI6B-P9f-qfPJTnPRHUCu5UAzEgn8JmlvJPKy7yUjCubPD43rakxK-25O7MQo4YK-3YO06CbFFDIPhWrGyjFN4gRG8Nil0Kupq/s400/CLL_IN_FC_1.jpg&quot; width=&quot;330&quot; /&gt;&lt;/a&gt;&lt;/b&gt;&lt;/div&gt;
&lt;b&gt;&lt;b&gt;
Program:&lt;/b&gt;
&lt;/b&gt;&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style=&quot;background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;
&lt;table&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt; 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57&lt;/pre&gt;
&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;&lt;span style=&quot;color: #557799;&quot;&gt;#include&amp;lt;stdio.h&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #557799;&quot;&gt;#include&amp;lt;stdlib.h&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;struct&lt;/span&gt; node{
    &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; INFO;
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;struct&lt;/span&gt; node &lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;LINK;
};
&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;main&lt;/span&gt;(){
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;struct&lt;/span&gt; node &lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;q&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;,&lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;p&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;,&lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;head&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;,&lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;tail&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;,&lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;LOC&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;;
  &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; i,n&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;5&lt;/span&gt;,x;

  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt;(i&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;i&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;=&lt;/span&gt;n;i&lt;span style=&quot;color: #333333;&quot;&gt;++&lt;/span&gt;){
    q &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; (&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;struct&lt;/span&gt; node &lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;)malloc(&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;sizeof&lt;/span&gt;(&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;struct&lt;/span&gt; node));
    q&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;;
    q&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;INFO &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; i;

    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;(p&lt;span style=&quot;color: #333333;&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;)  head &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; q;
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt;
      p&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; q;
    p&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;q;
  }
  p&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;head;
  tail&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;p;

  &lt;span style=&quot;color: #888888;&quot;&gt;/*INSERTION (Successor)*/&lt;/span&gt;
  printf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;Insert the value of target node:&quot;&lt;/span&gt;);
  scanf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;%d&quot;&lt;/span&gt;,&lt;span style=&quot;color: #333333;&quot;&gt;&amp;amp;&lt;/span&gt;x);

  q &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; (&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;struct&lt;/span&gt; node &lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;)malloc(&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;sizeof&lt;/span&gt;(&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;struct&lt;/span&gt; node));
  q&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;;
  q&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;INFO &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;10&lt;/span&gt;;

  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;(head&lt;span style=&quot;color: #333333;&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;){
      head&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;q;
      tail&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;q;
      head&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;q;
  }
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt;{
    LOC&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;head;
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt;(LOC&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK&lt;span style=&quot;color: #333333;&quot;&gt;!=&lt;/span&gt;head &lt;span style=&quot;color: #333333;&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; LOC&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;INFO&lt;span style=&quot;color: #333333;&quot;&gt;!=&lt;/span&gt;x){
          LOC&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;LOC&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK;
    }
    q&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;LOC&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK;
    LOC&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;q;
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; (LOC&lt;span style=&quot;color: #333333;&quot;&gt;==&lt;/span&gt;tail){
         tail&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;q;
    }
  }

  &lt;span style=&quot;color: #888888;&quot;&gt;/*Traverse the CHLL*/&lt;/span&gt;
  p&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;head;
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;do&lt;/span&gt;{
    printf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;%d &quot;&lt;/span&gt;,p&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;INFO);
    p&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;p&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK;
  }&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt;(p&lt;span style=&quot;color: #333333;&quot;&gt;!=&lt;/span&gt;head);

  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
}
&lt;/pre&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://rizoantoufiq.blogspot.com/feeds/5453135398589489352/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://rizoantoufiq.blogspot.com/2016/08/data-structure-linked-list-part-7.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1124334728283106452/posts/default/5453135398589489352'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1124334728283106452/posts/default/5453135398589489352'/><link rel='alternate' type='text/html' href='http://rizoantoufiq.blogspot.com/2016/08/data-structure-linked-list-part-7.html' title='Data Structure: Linked List : Part 7'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg9G_WIY25wMiA6SMgfit1IjAxVN4xjNg8L-5FDJckV9JbVIXwl8_HuS3ufdIDRQ1nvcTgxpI5_KxeM8_CdS3AZAj4YVCFGN-nG-cH8KQN7f_Qrnnl3K2ubfvdDitEnwqcBErIlxgjc5o0h/s72-c/CLL_IN_1.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1124334728283106452.post-5069006527878210050</id><published>2016-08-18T06:11:00.001-07:00</published><updated>2016-08-18T06:13:41.044-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Data Structure"/><title type='text'>Data Structure: Linked List - Part 6</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;&lt;b&gt;একটি নতুন node কে Circular header linked list এ সংযুক্ত করার পদ্ধতি&amp;nbsp;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;b&gt;সমস্যাঃ &lt;/b&gt;একটি নতুন node circular linked list এর একটি node (যার data মান x) ও তার পূর্ববর্তী(predecesor) node এর মাঝে insert (যুক্ত) করতে হবে। যদি x মান বিশিষ্ট কোন node পাওয়া না যায় তাহলে নতুন node টি linked list এর শেষে সংযুক্ত করে দিতে হবে। &lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;b&gt;সমাধান:&lt;/b&gt;
প্রথমে আমাকে linked list এর x মান বিশিষ্ট node টি খুঁজে বের করতে হবে। ধরি, LOC pointer টি নির্দিষ্ট node কে point করে থাকবে আর LOCP নির্দিষ্ট node এর পূরবর্তী node কে point করে থাকবে। x মান বিশিষ্ট যদি linked list এ না থাকে তাহলে তা linked list এর শেষে যুক্ত হবে অর্থাৎ LOCP pointer শেষ node কে point করে থাকবে আর LOC←∧।&lt;/div&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
কিছু শর্ততে ভাগ করে এখন Insertion পদ্ধতিটি আলোচনা করব।&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;শর্ত-১&lt;/b&gt; circular linked list এ কোন node না থাকে, অর্থাৎ head←∧&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhxjYdIG8XUIwPzwqJaIzjRp4l2a1BxB-p1Ysi5A8lo1sOhLg6cx8cvwt5ZZzYwpUEtxIrGwYk8c-AxbqD0sJ3qoyoRy_KD7oHTRSsz0Y_1SFZETv3TPAmbAxHyPZVt49Gwx1wBIWVU3fC_/s1600/CLL_IN_1.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhxjYdIG8XUIwPzwqJaIzjRp4l2a1BxB-p1Ysi5A8lo1sOhLg6cx8cvwt5ZZzYwpUEtxIrGwYk8c-AxbqD0sJ3qoyoRy_KD7oHTRSsz0Y_1SFZETv3TPAmbAxHyPZVt49Gwx1wBIWVU3fC_/s1600/CLL_IN_1.jpg&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
q pointer যদি নতুন node কে point করে থাকে তাহলে নতুন node টি head node হবে। তাহলে instruction হবে,&lt;/div&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;b&gt;head←q&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;tail←q&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;head↑.LINK←q&lt;/b&gt;&lt;/blockquote&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;b&gt;শর্ত-২ &lt;/b&gt;নতুন node যদি head হিসেবে সংযুক্ত করা লাগে অর্থাৎ LOC←head or head↑.INFO = x&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgd185VyrPzoL1KdWcVeJF0LcUOvaujhUbD6PCvkcz17Fbg1eAxC_TLEFiM8hwju64bNQW6Du6rKMX8QjXPgAu_WgolQGFOq6AtxRIWN7rydfsWEZmB5VWMWnJkgbu_lxF4ORb7utkW0HLE/s1600/CLL_IN_2.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;80&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgd185VyrPzoL1KdWcVeJF0LcUOvaujhUbD6PCvkcz17Fbg1eAxC_TLEFiM8hwju64bNQW6Du6rKMX8QjXPgAu_WgolQGFOq6AtxRIWN7rydfsWEZmB5VWMWnJkgbu_lxF4ORb7utkW0HLE/s400/CLL_IN_2.jpg&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
q pointer যদি নতুন node কে point করে থাকে তাহলে instruction হবে, &lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;b&gt;q↑.LINK ←head&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;head←q&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;tail↑.LINK←head&lt;/b&gt;&lt;/blockquote&gt;
&lt;br /&gt;
&lt;b&gt;শর্ত-৩&lt;/b&gt; নির্দিষ্ট node (যার data মান x) ও তার পূর্ববর্তী(predecesor) node এর মাঝে insert (যুক্ত) করতে হলে, অর্থাৎ i.e. LOC≠head&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi_EPiDw72OhvJTPlpdcPB1sBp_Xgp2lX-tJV6XYmCqKwDwPF0ch4HENti3k4qsHX7mxRYV0qGy6X2jt1s55itsHZlzst7w9_7tV6sAZOQ0FtVz_U9dqGkeHZXnnjQ9M-OLxm2aEqxwNkFX/s1600/CLL_IN_3.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;132&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi_EPiDw72OhvJTPlpdcPB1sBp_Xgp2lX-tJV6XYmCqKwDwPF0ch4HENti3k4qsHX7mxRYV0qGy6X2jt1s55itsHZlzst7w9_7tV6sAZOQ0FtVz_U9dqGkeHZXnnjQ9M-OLxm2aEqxwNkFX/s400/CLL_IN_3.jpg&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
q pointer যদি নতুন node কে point করে থাকে তাহলে instruction হবে,&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;b&gt;LOCP↑.LINK ←q&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;q↑.LINK ←LOC&lt;/b&gt;&lt;/blockquote&gt;
&lt;br /&gt;
&lt;b&gt;শর্ত-৪&lt;/b&gt; যদি x মান বিশিষ্ট কোন node পাওয়া না যায় অর্থাৎ LOC=head, তাহলে tail node হিসেবে insert হবে। &lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjcM2ixN3dR6MG-wh_WBTxRdSF_JBCop2ygbky97EYD_ZnqG5yrAk9nStykva0CMb9Q33abaoJGmAtqRlFabSB0ns0j8HBx_7H-CnXlWdhp0yCtP8_plroruOHLFyABZQNUSQs_pnM4ATlh/s1600/CLL_IN_4.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;96&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjcM2ixN3dR6MG-wh_WBTxRdSF_JBCop2ygbky97EYD_ZnqG5yrAk9nStykva0CMb9Q33abaoJGmAtqRlFabSB0ns0j8HBx_7H-CnXlWdhp0yCtP8_plroruOHLFyABZQNUSQs_pnM4ATlh/s400/CLL_IN_4.jpg&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
q pointer যদি নতুন node কে point করে থাকে তাহলে instruction হবে, &lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;b&gt;tail↑.LINK←q&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;q↑.LINK←head&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;tail←q&lt;/b&gt;&lt;/blockquote&gt;
&lt;b&gt;
Algorithm:&lt;/b&gt;&lt;br /&gt;
This algorithm insert a new node between the node which data field is x and its predecessor&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
1. Call New_Node(q)&lt;br /&gt;
2. If head = ∧, then: [CASE-I]&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; head←q, head↑.LINK←q, tail←q, and exit.&lt;br /&gt;
3. if head↑.INFO = x, then: [CASE-II]&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; q↑.LINK←head, head←q, tail←q and exit.&lt;br /&gt;
4. LOC←head↑.LINK and LOCP ← head&lt;br /&gt;
5. Repeat step 6 while LOC≠head and LOC↑.INFO≠x&lt;br /&gt;
6. &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;                     LOCP←LOC and LOC←LOC↑.LINK&lt;br /&gt;
7. IF LOC≠head then: [CASE-III]&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; LOCP↑.LINK←q and q↑.LINK←LOC&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; ELSE: [CASE-IV]&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; tail↑.LINK←q, q↑.LINK←head and tail←q.&lt;br /&gt;
8. Exit.&lt;/blockquote&gt;
&lt;b&gt;FLOW CHART:
&lt;/b&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;b&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhO721TPfQqvGGZZifPeWzEEdLONV4GEbBCZcDEK-3ra7XbMHb_9GSUYmPbKUbT0bNboHbsS7MCbqgz3A4AKB9TITdsDeLDwEqvPzrObC278fa7xkTR3lbbiri4XJmkeN0JToMC_DqufTca/s1600/CLL_IN_FC.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;400&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhO721TPfQqvGGZZifPeWzEEdLONV4GEbBCZcDEK-3ra7XbMHb_9GSUYmPbKUbT0bNboHbsS7MCbqgz3A4AKB9TITdsDeLDwEqvPzrObC278fa7xkTR3lbbiri4XJmkeN0JToMC_DqufTca/s400/CLL_IN_FC.jpg&quot; width=&quot;386&quot; /&gt;&lt;/a&gt;&lt;/b&gt;&lt;/div&gt;
&lt;b&gt;Program:&lt;/b&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style=&quot;background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;
&lt;table&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt; 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68&lt;/pre&gt;
&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;&lt;span style=&quot;color: #557799;&quot;&gt;#include&amp;lt;stdio.h&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #557799;&quot;&gt;#include&amp;lt;stdlib.h&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;struct&lt;/span&gt; node{
    &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; INFO;
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;struct&lt;/span&gt; node &lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;LINK;
};
&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;main&lt;/span&gt;(){
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;struct&lt;/span&gt; node &lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;q&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;,&lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;p&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;,&lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;head&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;,&lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;tail&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;,&lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;LOC&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;,&lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;LOCP&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;;
  &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; i,n&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;5&lt;/span&gt;,x;

  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt;(i&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;i&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;=&lt;/span&gt;n;i&lt;span style=&quot;color: #333333;&quot;&gt;++&lt;/span&gt;){
    q &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; (&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;struct&lt;/span&gt; node &lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;)malloc(&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;sizeof&lt;/span&gt;(&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;struct&lt;/span&gt; node));
    q&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;;
    q&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;INFO &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; i;

    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;(p&lt;span style=&quot;color: #333333;&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;)  head &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; q;
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt;
      p&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; q;
    p&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;q;
  }
  p&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;head;
  tail&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;p;

  &lt;span style=&quot;color: #888888;&quot;&gt;/*INSERTION (Predecessor)*/&lt;/span&gt;
  printf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;Insert the value of target node:&quot;&lt;/span&gt;);
  scanf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;%d&quot;&lt;/span&gt;,&lt;span style=&quot;color: #333333;&quot;&gt;&amp;amp;&lt;/span&gt;x);

  q &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; (&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;struct&lt;/span&gt; node &lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;)malloc(&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;sizeof&lt;/span&gt;(&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;struct&lt;/span&gt; node));
  q&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;;
  q&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;INFO &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;10&lt;/span&gt;;

  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;(head&lt;span style=&quot;color: #333333;&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;){
      head&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;q;
      tail&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;q;
      head&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;q;
  }
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;(head&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;INFO &lt;span style=&quot;color: #333333;&quot;&gt;==&lt;/span&gt;x){
      q&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;head;
      head&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;q;
      tail&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;q;
  }
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt;{
    LOC&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;head&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK;
    LOCP&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;head;
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt;(LOC&lt;span style=&quot;color: #333333;&quot;&gt;!=&lt;/span&gt;head &lt;span style=&quot;color: #333333;&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; LOC&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;INFO&lt;span style=&quot;color: #333333;&quot;&gt;!=&lt;/span&gt;x){
          LOCP&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;LOC;
          LOC&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;LOC&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK;
    }
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; (LOC&lt;span style=&quot;color: #333333;&quot;&gt;!=&lt;/span&gt;head){
       LOCP&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;q;
       q&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; LOC;
    }
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt;{
      tail&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;q;
      q&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;head;
      tail&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;q;
    }
  }

  &lt;span style=&quot;color: #888888;&quot;&gt;/*Traverse the CHLL*/&lt;/span&gt;
  p&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;head;
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;do&lt;/span&gt;{
    printf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;%d &quot;&lt;/span&gt;,p&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;INFO);
    p&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;p&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK;
  }&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt;(p&lt;span style=&quot;color: #333333;&quot;&gt;!=&lt;/span&gt;head);

  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
}
&lt;/pre&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://rizoantoufiq.blogspot.com/feeds/5069006527878210050/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://rizoantoufiq.blogspot.com/2016/08/data-structure-linked-list-part-6.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1124334728283106452/posts/default/5069006527878210050'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1124334728283106452/posts/default/5069006527878210050'/><link rel='alternate' type='text/html' href='http://rizoantoufiq.blogspot.com/2016/08/data-structure-linked-list-part-6.html' title='Data Structure: Linked List - Part 6'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhxjYdIG8XUIwPzwqJaIzjRp4l2a1BxB-p1Ysi5A8lo1sOhLg6cx8cvwt5ZZzYwpUEtxIrGwYk8c-AxbqD0sJ3qoyoRy_KD7oHTRSsz0Y_1SFZETv3TPAmbAxHyPZVt49Gwx1wBIWVU3fC_/s72-c/CLL_IN_1.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1124334728283106452.post-1310218542799490606</id><published>2016-08-18T05:02:00.001-07:00</published><updated>2016-08-23T11:26:48.738-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Data Structure"/><title type='text'>Data Structure: Linked List-Part 5</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;&lt;b&gt;Circular Header linked list&amp;nbsp;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Header linked list এর শেষ node এর link এ null value থাকে, এখন সেই শেষ node এর link এ প্রথম node এর address থাকলে সেই header linked list কে বলে circular header linked list. একটি tail pointer শেষের node কে point করে থাকবে।&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjzs-QenomVXcFIyf0O2C2akeaFv3ld4KIkyRmPY_ZovD5QMfehbSVuHX11F2_0Cpp5Ra02lGmJowuG_jzigLyZ6yY6VrmGH0fJlu3sC0EBMSwiyyqV2IpBeruSwFIk24tAgtMw3uznkaAw/s1600/C_1.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;80&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjzs-QenomVXcFIyf0O2C2akeaFv3ld4KIkyRmPY_ZovD5QMfehbSVuHX11F2_0Cpp5Ra02lGmJowuG_jzigLyZ6yY6VrmGH0fJlu3sC0EBMSwiyyqV2IpBeruSwFIk24tAgtMw3uznkaAw/s400/C_1.jpg&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;b&gt;সমস্যাঃ&lt;/b&gt; আমরা একটা circular header linked list তৈরি করতে চাই যেখানে ব্যবহারকারি (user) বলে দিবে কতটা Node থাকবে আর তাদের INFO কি কি হবে।&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;b&gt;সমাধান:&lt;/b&gt;  আমরা এর আগে যেভাবে header linked list তৈরি করেছি সেইভাবেই করব, শুধু algorithm শেষ হওয়ার আগে কিছু instruction বেশি লেখব, তা হচ্ছেঃ &lt;/div&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;b&gt;p↑.link←head&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;tail←p&lt;/b&gt;&lt;/blockquote&gt;
এখানে p pointer টা শেষ node কে point করে আছে।&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;
&lt;b&gt;Algorithm:&lt;/b&gt;&lt;br /&gt;
This algorithm Create a circular link list where head pointer point the first node of the header linked list.&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
1. Read n&lt;br /&gt;
2. p←∧ and i ← 1&lt;br /&gt;
3. Repeat step 4 to 8 while i≤n&lt;br /&gt;
4. &amp;nbsp; &amp;nbsp;&amp;nbsp;           New_Node(q);&lt;br /&gt;
5.&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;            Read: x;&lt;br /&gt;
6.&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;            q↑.INFO←x;&lt;br /&gt;
7.&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;            If p=∧ then: head←q&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Else: p↑.LINK←q&lt;br /&gt;
8.&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;            p←q;&lt;br /&gt;
9. p↑.LINK←head&lt;br /&gt;
10. tail←p.&lt;br /&gt;
11. Exit&lt;/blockquote&gt;
&lt;b&gt;
FLOW CHART (Create a Circular Header Linked List):&lt;/b&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;b&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiyLaj5LDkFS4w7j15gBHxTH22TP3GcgMZmkws3C-iDfiRUDuBjfZh88AimwVpwdht3CSg7GGEyuqlusAsfIor62rY0Ces_j2g02VdwSFHuDXFojq_w9eBCDyX7yjDBEZFLObrKU6TysVzt/s1600/C_2.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;400&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiyLaj5LDkFS4w7j15gBHxTH22TP3GcgMZmkws3C-iDfiRUDuBjfZh88AimwVpwdht3CSg7GGEyuqlusAsfIor62rY0Ces_j2g02VdwSFHuDXFojq_w9eBCDyX7yjDBEZFLObrKU6TysVzt/s400/C_2.jpg&quot; width=&quot;313&quot; /&gt;&lt;/a&gt;&lt;/b&gt;&lt;/div&gt;
&lt;b&gt;সমস্যাঃ&lt;/b&gt; আমরা এবার circular header linked list traverse করব, আর node এর data print করব।
&lt;b&gt;&amp;nbsp;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;Algorithm:&lt;/b&gt;&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;b&gt;
&lt;/b&gt;This algorithm traverses a circular header link list.&lt;br /&gt;
1. p←head &lt;br /&gt;
2. Write: p↑.INFO&lt;br /&gt;
3. p←p↑.LINK&lt;br /&gt;
4. Repeat step 2 and 3 while p≠head&lt;br /&gt;
5. Exit    &lt;br /&gt;
&lt;b&gt;

&lt;/b&gt;&lt;/blockquote&gt;
&lt;b&gt;Flow Chart (Traverse A Circular Linked List)&lt;/b&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;b&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEidFEYssJOGoPUmjTP-wk6sEpAKWnYMjZmwNOJE8elufarPONoRnhb1TsO_uFSdOhgE5xGso4VCY8lQskUH6f6jKY8j9Gw-6wKXL-Yo5XoMHb6fFfUPZnG2BfBqyD9-UKz_G-BOsT8eLn-N/s1600/Untitled.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;320&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEidFEYssJOGoPUmjTP-wk6sEpAKWnYMjZmwNOJE8elufarPONoRnhb1TsO_uFSdOhgE5xGso4VCY8lQskUH6f6jKY8j9Gw-6wKXL-Yo5XoMHb6fFfUPZnG2BfBqyD9-UKz_G-BOsT8eLn-N/s320/Untitled.jpg&quot; width=&quot;286&quot; /&gt;&lt;/a&gt;&lt;/b&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;b&gt;&lt;/b&gt;&lt;/div&gt;
&lt;b&gt;Program:&lt;/b&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;div style=&quot;background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;margin: 0; line-height: 125%&quot;&gt; 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;margin: 0; line-height: 125%&quot;&gt;&lt;span style=&quot;color: #557799&quot;&gt;#include&amp;lt;stdio.h&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #557799&quot;&gt;#include&amp;lt;stdlib.h&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;struct&lt;/span&gt; node{
    &lt;span style=&quot;color: #333399; font-weight: bold&quot;&gt;int&lt;/span&gt; INFO;
    &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;struct&lt;/span&gt; node &lt;span style=&quot;color: #333333&quot;&gt;*&lt;/span&gt;LINK;
};
&lt;span style=&quot;color: #333399; font-weight: bold&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #0066BB; font-weight: bold&quot;&gt;main&lt;/span&gt;(){
  &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;struct&lt;/span&gt; node &lt;span style=&quot;color: #333333&quot;&gt;*&lt;/span&gt;q&lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #007020&quot;&gt;NULL&lt;/span&gt;,&lt;span style=&quot;color: #333333&quot;&gt;*&lt;/span&gt;p&lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #007020&quot;&gt;NULL&lt;/span&gt;,&lt;span style=&quot;color: #333333&quot;&gt;*&lt;/span&gt;head&lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #007020&quot;&gt;NULL&lt;/span&gt;,&lt;span style=&quot;color: #333333&quot;&gt;*&lt;/span&gt;tail&lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #007020&quot;&gt;NULL&lt;/span&gt;;
  &lt;span style=&quot;color: #333399; font-weight: bold&quot;&gt;int&lt;/span&gt; i,n,x;

  printf(&lt;span style=&quot;background-color: #fff0f0&quot;&gt;&amp;quot;Enter Number of Node: &amp;quot;&lt;/span&gt;);
  scanf(&lt;span style=&quot;background-color: #fff0f0&quot;&gt;&amp;quot;%d&amp;quot;&lt;/span&gt;,&lt;span style=&quot;color: #333333&quot;&gt;&amp;amp;&lt;/span&gt;n);

  &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;for&lt;/span&gt;(i&lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;1&lt;/span&gt;;i&lt;span style=&quot;color: #333333&quot;&gt;&amp;lt;=&lt;/span&gt;n;i&lt;span style=&quot;color: #333333&quot;&gt;++&lt;/span&gt;){
    printf(&lt;span style=&quot;background-color: #fff0f0&quot;&gt;&amp;quot;Enter a value:&amp;quot;&lt;/span&gt;);
    scanf(&lt;span style=&quot;background-color: #fff0f0&quot;&gt;&amp;quot;%d&amp;quot;&lt;/span&gt;,&lt;span style=&quot;color: #333333&quot;&gt;&amp;amp;&lt;/span&gt;x);

    q &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; (&lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;struct&lt;/span&gt; node &lt;span style=&quot;color: #333333&quot;&gt;*&lt;/span&gt;)malloc(&lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;sizeof&lt;/span&gt;(&lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;struct&lt;/span&gt; node));
    q&lt;span style=&quot;color: #333333&quot;&gt;-&amp;gt;&lt;/span&gt;LINK &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #007020&quot;&gt;NULL&lt;/span&gt;;
    q&lt;span style=&quot;color: #333333&quot;&gt;-&amp;gt;&lt;/span&gt;INFO &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; x;

    &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;if&lt;/span&gt;(p&lt;span style=&quot;color: #333333&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color: #007020&quot;&gt;NULL&lt;/span&gt;){
        head &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; q;
        printf(&lt;span style=&quot;background-color: #fff0f0&quot;&gt;&amp;quot;Head: %x&lt;/span&gt;&lt;span style=&quot;color: #666666; font-weight: bold; background-color: #fff0f0&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0&quot;&gt;&amp;quot;&lt;/span&gt;,head);
    }
    &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;else&lt;/span&gt;{
      p&lt;span style=&quot;color: #333333&quot;&gt;-&amp;gt;&lt;/span&gt;LINK &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; q;
      printf(&lt;span style=&quot;background-color: #fff0f0&quot;&gt;&amp;quot;Current: %x&lt;/span&gt;&lt;span style=&quot;color: #666666; font-weight: bold; background-color: #fff0f0&quot;&gt;\t&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0&quot;&gt; INFO:%d Next: %x&lt;/span&gt;&lt;span style=&quot;color: #666666; font-weight: bold; background-color: #fff0f0&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0&quot;&gt;&amp;quot;&lt;/span&gt;,p,p&lt;span style=&quot;color: #333333&quot;&gt;-&amp;gt;&lt;/span&gt;INFO,p&lt;span style=&quot;color: #333333&quot;&gt;-&amp;gt;&lt;/span&gt;LINK);
    }
    p&lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt;q;
    printf(&lt;span style=&quot;background-color: #fff0f0&quot;&gt;&amp;quot;Next: %x&lt;/span&gt;&lt;span style=&quot;color: #666666; font-weight: bold; background-color: #fff0f0&quot;&gt;\t&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0&quot;&gt; INFO:%d &lt;/span&gt;&lt;span style=&quot;color: #666666; font-weight: bold; background-color: #fff0f0&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0&quot;&gt;&amp;quot;&lt;/span&gt;,p,p&lt;span style=&quot;color: #333333&quot;&gt;-&amp;gt;&lt;/span&gt;INFO);
  }
  p&lt;span style=&quot;color: #333333&quot;&gt;-&amp;gt;&lt;/span&gt;LINK&lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt;head;
  tail&lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt;p;
  &lt;span style=&quot;color: #888888&quot;&gt;/*Traverse*/&lt;/span&gt;
  p&lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt;head;
  &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;if&lt;/span&gt;(p&lt;span style=&quot;color: #333333&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color: #007020&quot;&gt;NULL&lt;/span&gt;) printf(&lt;span style=&quot;background-color: #fff0f0&quot;&gt;&amp;quot;Empty&amp;quot;&lt;/span&gt;);
  &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;else&lt;/span&gt; 
   &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;do&lt;/span&gt;{
     printf(&lt;span style=&quot;background-color: #fff0f0&quot;&gt;&amp;quot;%d &amp;quot;&lt;/span&gt;,p&lt;span style=&quot;color: #333333&quot;&gt;-&amp;gt;&lt;/span&gt;INFO);
     p&lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt;p&lt;span style=&quot;color: #333333&quot;&gt;-&amp;gt;&lt;/span&gt;LINK;
   }&lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;while&lt;/span&gt;(p&lt;span style=&quot;color: #333333&quot;&gt;!=&lt;/span&gt;head); 
  &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;0&lt;/span&gt;;
}
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;

&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://rizoantoufiq.blogspot.com/feeds/1310218542799490606/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://rizoantoufiq.blogspot.com/2016/08/data-structure-linked-list-part5.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1124334728283106452/posts/default/1310218542799490606'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1124334728283106452/posts/default/1310218542799490606'/><link rel='alternate' type='text/html' href='http://rizoantoufiq.blogspot.com/2016/08/data-structure-linked-list-part5.html' title='Data Structure: Linked List-Part 5'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjzs-QenomVXcFIyf0O2C2akeaFv3ld4KIkyRmPY_ZovD5QMfehbSVuHX11F2_0Cpp5Ra02lGmJowuG_jzigLyZ6yY6VrmGH0fJlu3sC0EBMSwiyyqV2IpBeruSwFIk24tAgtMw3uznkaAw/s72-c/C_1.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1124334728283106452.post-3310121978545097300</id><published>2016-08-17T11:34:00.001-07:00</published><updated>2016-08-17T11:34:32.487-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Data Structure"/><title type='text'>Data Structure: Linked List - Part 4</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;b&gt;Header linked list হতে একটি node বাদ দেওয়ার পদ্ধতি&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;b&gt;সমস্যাঃ&lt;/b&gt; একটি node (যার data মান x) header linked list হতে delete (বাদ) দিতে হবে।&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;সমাধান:&lt;/b&gt;প্রথমে আমাকে linked list এর x মান বিশিষ্ট node টি খুঁজে বের করতে হবে। ধরি, LOC pointer টি নির্দিষ্ট node কে point করে থাকবে আর LOCP নির্দিষ্ট node এর পূরবর্তী node কে point করে থাকবে। x মান বিশিষ্ট যদি linked list এ না থাকে তাহলে “Not Found” দেখাবে।
কিছু শর্ততে ভাগ করে এখন Deletion পদ্ধতিটি আলোচনা করব।&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;
&lt;b&gt;শর্ত-১&lt;/b&gt; linked list এ কোন node না থাকে, অর্থাৎ head←∧, তখন “Underflow/Empty” দেখাবে।&lt;br /&gt;
&lt;b&gt;শর্ত-২&lt;/b&gt; যদি head কে delete করতে হলে অর্থাৎ LOC←head or head↑.INFO = x&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhjNmTfD4TB1ucbarWENYIQtOZLT67SWPNQsumj0FGtYYWJilZj26GUY07QJld2GSah7fJkXA0h-nIj6_JKi7iZICisRSPxn8mXzT13ggwEp0oec1eEyC3StjObCPHpsW5HW2U60VsOlP4U/s1600/D_2.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;96&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhjNmTfD4TB1ucbarWENYIQtOZLT67SWPNQsumj0FGtYYWJilZj26GUY07QJld2GSah7fJkXA0h-nIj6_JKi7iZICisRSPxn8mXzT13ggwEp0oec1eEyC3StjObCPHpsW5HW2U60VsOlP4U/s400/D_2.jpg&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;
তাহলে instruction হবে,&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;b&gt;head← head↑.LINK&lt;/b&gt;&lt;/blockquote&gt;
&lt;br /&gt;
&lt;b&gt;শর্ত-৩&lt;/b&gt; নির্দিষ্ট node (যার data মান x) খুঁজে পাওয়া যাবে অর্থাৎ i.e. LOC≠∧&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjOy7Kg3gyx-MFFEP50jKkiPt-jnPxqkJkp95g4Vx5EUgUUbYoksi5h5I88B8OoqOwYdghiZwsw8GxnSAUMA9DSOvQptgnseqmmepAMD1a0iubWgK835WZThSHGe2wWblcrXSaGXWn6eQgI/s1600/D_3.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;96&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjOy7Kg3gyx-MFFEP50jKkiPt-jnPxqkJkp95g4Vx5EUgUUbYoksi5h5I88B8OoqOwYdghiZwsw8GxnSAUMA9DSOvQptgnseqmmepAMD1a0iubWgK835WZThSHGe2wWblcrXSaGXWn6eQgI/s400/D_3.jpg&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
LOC pointer টি নির্দিষ্ট node কে point করে থাকবে আর LOCP নির্দিষ্ট node এর পূরবর্তী node কে point করে থাকবে। তাহলে instruction হবে,&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;b&gt;LOCP↑.LINK ← LOC↑.LINK&amp;nbsp;&lt;/b&gt;&lt;/blockquote&gt;
&lt;br /&gt;
&lt;b&gt;শর্ত-৪&lt;/b&gt; যদি x মান বিশিষ্ট কোন node পাওয়া না যায় অর্থাৎ LOC=∧, তাহলে “Not Found” দেখাবে।&lt;br /&gt;
&lt;b&gt;ALgorithm:&lt;/b&gt; 
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style=&quot;background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;This algorithm delete a node from a linked list (head pointer) which data&lt;/pre&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;field is x.
&lt;span style=&quot;color: #6600ee; font-weight: bold;&quot;&gt;1.&lt;/span&gt; If head &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;background-color: #ffaaaa; color: red;&quot;&gt;∧&lt;/span&gt;, then&lt;span style=&quot;color: #333333;&quot;&gt;:&lt;/span&gt; [CASE&lt;span style=&quot;color: #333333;&quot;&gt;-&lt;/span&gt;I]
        &lt;span style=&quot;color: #997700; font-weight: bold;&quot;&gt;    Write:&lt;/span&gt; Empty and exit.
&lt;span style=&quot;color: #6600ee; font-weight: bold;&quot;&gt;2.&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; head&lt;span style=&quot;background-color: #ffaaaa; color: red;&quot;&gt;↑&lt;/span&gt;.INFO &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; x, then&lt;span style=&quot;color: #333333;&quot;&gt;:&lt;/span&gt; [CASE&lt;span style=&quot;color: #333333;&quot;&gt;-&lt;/span&gt;II]
                head &lt;span style=&quot;background-color: #ffaaaa; color: red;&quot;&gt;←&lt;/span&gt;head&lt;span style=&quot;background-color: #ffaaaa; color: red;&quot;&gt;↑&lt;/span&gt;.LINK and exit.
&lt;span style=&quot;color: #6600ee; font-weight: bold;&quot;&gt;3.&lt;/span&gt; LOC&lt;span style=&quot;background-color: #ffaaaa; color: red;&quot;&gt;←&lt;/span&gt;head&lt;span style=&quot;background-color: #ffaaaa; color: red;&quot;&gt;↑&lt;/span&gt;.LINK and LOCP &lt;span style=&quot;background-color: #ffaaaa; color: red;&quot;&gt;←&lt;/span&gt; head
&lt;span style=&quot;color: #6600ee; font-weight: bold;&quot;&gt;4.&lt;/span&gt; Repeat step &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;5&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt; LOC&lt;span style=&quot;background-color: #ffaaaa; color: red;&quot;&gt;≠∧&lt;/span&gt; and LOC&lt;span style=&quot;background-color: #ffaaaa; color: red;&quot;&gt;↑&lt;/span&gt;.INFO&lt;span style=&quot;background-color: #ffaaaa; color: red;&quot;&gt;≠&lt;/span&gt;x
&lt;span style=&quot;color: #6600ee; font-weight: bold;&quot;&gt;5.&lt;/span&gt;                      LOCP&lt;span style=&quot;background-color: #ffaaaa; color: red;&quot;&gt;←&lt;/span&gt;LOC and LOC&lt;span style=&quot;background-color: #ffaaaa; color: red;&quot;&gt;←&lt;/span&gt;LOC&lt;span style=&quot;background-color: #ffaaaa; color: red;&quot;&gt;↑&lt;/span&gt;.LINK
&lt;span style=&quot;color: #6600ee; font-weight: bold;&quot;&gt;6.&lt;/span&gt; If LOC&lt;span style=&quot;background-color: #ffaaaa; color: red;&quot;&gt;≠∧&lt;/span&gt; then&lt;span style=&quot;color: #333333;&quot;&gt;:&lt;/span&gt; [CASE&lt;span style=&quot;color: #333333;&quot;&gt;-&lt;/span&gt;III]
            LOCP&lt;span style=&quot;background-color: #ffaaaa; color: red;&quot;&gt;↑&lt;/span&gt;.LINK&lt;span style=&quot;background-color: #ffaaaa; color: red;&quot;&gt;←&lt;/span&gt;LOC&lt;span style=&quot;background-color: #ffaaaa; color: red;&quot;&gt;↑&lt;/span&gt;.LINK
&lt;span style=&quot;color: #997700; font-weight: bold;&quot;&gt;        Else:&lt;/span&gt; [CASE&lt;span style=&quot;color: #333333;&quot;&gt;-&lt;/span&gt;IV]
         &lt;span style=&quot;color: #997700; font-weight: bold;&quot;&gt;Write:&lt;/span&gt; Not Found
&lt;span style=&quot;color: #6600ee; font-weight: bold;&quot;&gt;7.&lt;/span&gt; Exit.
&lt;/pre&gt;
&lt;/div&gt;
&lt;b&gt;Flow Chart&lt;/b&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;b&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgiXjjqjw0nckONI2nowNrAeUO_30zSzln54kNUqSBYXWSU65HbM2lMK-kI2PmzjDveZtJgQZmjksfbcoXXSfNiDR2nJFhvAJyMZz4URJRl7pC00UmUGC3GYAt6QTi3dFwXDWFEn_8CxY6u/s1600/D_Flow.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;376&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgiXjjqjw0nckONI2nowNrAeUO_30zSzln54kNUqSBYXWSU65HbM2lMK-kI2PmzjDveZtJgQZmjksfbcoXXSfNiDR2nJFhvAJyMZz4URJRl7pC00UmUGC3GYAt6QTi3dFwXDWFEn_8CxY6u/s400/D_Flow.jpg&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/b&gt;&lt;/div&gt;
&lt;b&gt;
&lt;/b&gt;&lt;b&gt;
Program:&lt;/b&gt; &lt;br /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style=&quot;background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;
&lt;table&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt; 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49&lt;/pre&gt;
&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;&lt;span style=&quot;color: #557799;&quot;&gt;#include&amp;lt;stdio.h&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #557799;&quot;&gt;#include&amp;lt;stdlib.h&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;struct&lt;/span&gt; node{
    &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; INFO;
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;struct&lt;/span&gt; node &lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;LINK;
};
&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;main&lt;/span&gt;(){
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;struct&lt;/span&gt; node &lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;q&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;,&lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;LOC&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;,&lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;LOCP&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;,&lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;head&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;,&lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;p&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;;
  &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; i,n&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;5&lt;/span&gt;,x;
  &lt;span style=&quot;color: #888888;&quot;&gt;/*Create A list*/&lt;/span&gt;
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt;(i&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;i&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;=&lt;/span&gt;n;i&lt;span style=&quot;color: #333333;&quot;&gt;++&lt;/span&gt;){
    q &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; (&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;struct&lt;/span&gt; node &lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;)malloc(&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;sizeof&lt;/span&gt;(&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;struct&lt;/span&gt; node));
    q&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;;
    q&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;INFO &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; i;
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;(p&lt;span style=&quot;color: #333333;&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;)
        head &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; q;
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt;
      p&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; q;
    p&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;q;
  }
  &lt;span style=&quot;color: #888888;&quot;&gt;/*DELETION*/&lt;/span&gt;
  printf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;Enter a Value of a node:&quot;&lt;/span&gt;);
  scanf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;%d&quot;&lt;/span&gt;,&lt;span style=&quot;color: #333333;&quot;&gt;&amp;amp;&lt;/span&gt;x);
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;(head&lt;span style=&quot;color: #333333;&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;){
     printf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;Empty&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #666666; font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;&lt;/span&gt;);
  }
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;(head&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;INFO &lt;span style=&quot;color: #333333;&quot;&gt;==&lt;/span&gt; x)
       head&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;head&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK;
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt;{
    LOC&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;head&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK;LOCP&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;head;
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt;(LOC&lt;span style=&quot;color: #333333;&quot;&gt;!=&lt;/span&gt;&lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt; &lt;span style=&quot;color: #333333;&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; LOC&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;INFO&lt;span style=&quot;color: #333333;&quot;&gt;!=&lt;/span&gt;x){
          LOCP&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;LOC;
          LOC&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;LOC&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK;
    }
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;(LOC&lt;span style=&quot;color: #333333;&quot;&gt;!=&lt;/span&gt;&lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;)
      LOCP&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;LOC&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK;
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt;
      printf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;Not Found&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #666666; font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;&lt;/span&gt;);
  }
  &lt;span style=&quot;color: #888888;&quot;&gt;/*Traverse*/&lt;/span&gt;
  printf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #666666; font-weight: bold;&quot;&gt;\n\n&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;&lt;/span&gt;);
  p&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;head;
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt;(p&lt;span style=&quot;color: #333333;&quot;&gt;!=&lt;/span&gt;&lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;){
   printf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;%d &quot;&lt;/span&gt;,p&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;INFO);
   p&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;p&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK;
  }

  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
}
&lt;/pre&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://rizoantoufiq.blogspot.com/feeds/3310121978545097300/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://rizoantoufiq.blogspot.com/2016/08/data-structure-linked-list-part-4.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1124334728283106452/posts/default/3310121978545097300'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1124334728283106452/posts/default/3310121978545097300'/><link rel='alternate' type='text/html' href='http://rizoantoufiq.blogspot.com/2016/08/data-structure-linked-list-part-4.html' title='Data Structure: Linked List - Part 4'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhjNmTfD4TB1ucbarWENYIQtOZLT67SWPNQsumj0FGtYYWJilZj26GUY07QJld2GSah7fJkXA0h-nIj6_JKi7iZICisRSPxn8mXzT13ggwEp0oec1eEyC3StjObCPHpsW5HW2U60VsOlP4U/s72-c/D_2.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1124334728283106452.post-8505668180872896185</id><published>2016-08-17T08:46:00.002-07:00</published><updated>2016-08-17T10:11:56.133-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Data Structure"/><title type='text'>Data Structure: Linked List- Part 3</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;b&gt;INSERTION A NEW NODE INTO A HEADER LINKED LIST&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;b&gt;সমস্যাঃ&lt;/b&gt; একটি নতুন node linked list এর একটি node (যার data মান x) ও তার পরবর্তি (successor) node এর মাঝে insert (যুক্ত) করতে হবে।&amp;nbsp;&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;b&gt;সমাধান:&lt;/b&gt;
প্রথমে আমাকে linked list এর x মান বিশিষ্ট node টি খুঁজে বের করতে হবে। ধরি, LOC pointer টি নির্দিষ্ট node কে point করে আছে। x মান বিশিষ্ট node যদি linked list এ না থাকে তাহলে তা linked list এর শেষে যুক্ত হবে অর্থাৎ LOC pointer শেষ node কে point করে থাকবে।

কিছু শর্ত নিয়ে এখন আলোচনা করব।&amp;nbsp;&lt;/div&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
&lt;b&gt;শর্ত-১ linked list এ কোন node না থাকে, অর্থাৎ head←∧&amp;nbsp;&lt;/b&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEia5v0tHxH3qIWqpRHiZeoq4fUMihtR5Q7m2cJyrk0afi_wS8yFziahOG6W2NptPj-GkzCPVU8CaKX-FWMhAGnPyWCTLwfNgQIezP8TvpPvWsxfGcvoDt63_0c0kVSKsUyIbVhUdsyCQzup/s1600/23.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEia5v0tHxH3qIWqpRHiZeoq4fUMihtR5Q7m2cJyrk0afi_wS8yFziahOG6W2NptPj-GkzCPVU8CaKX-FWMhAGnPyWCTLwfNgQIezP8TvpPvWsxfGcvoDt63_0c0kVSKsUyIbVhUdsyCQzup/s1600/23.jpg&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;b&gt;&amp;nbsp;&lt;/b&gt;q pointer যদি নতুন node কে point করে থাকে তাহলে নতুন node টি head node হবে। তাহলে instruction হবে,&amp;nbsp;&lt;/div&gt;
&lt;/div&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;b&gt;head←q

&lt;/b&gt;&lt;/blockquote&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;b&gt;শর্ত-২ linked list এর x মান বিশিষ্ট node টি খুঁজে বের করতে হবে (LOC point করে থাকবে)  অথবা LOC pointer শেষ node কে point করে থাকবে।&amp;nbsp;&lt;/b&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhd51DM_vyX7y_xeNFf9F8nHbZ9Na3wQGes6VQ1x1FDNBZw2R8utORdA8IRc5jaQdQgoIx9lRZWwl1JAHxbqLHDD7HI18vxXztpS1dD6rVMcFZC0URGqVgYIsENjfe1gTNLCwEPJhnnHIY9/s1600/27.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;96&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhd51DM_vyX7y_xeNFf9F8nHbZ9Na3wQGes6VQ1x1FDNBZw2R8utORdA8IRc5jaQdQgoIx9lRZWwl1JAHxbqLHDD7HI18vxXztpS1dD6rVMcFZC0URGqVgYIsENjfe1gTNLCwEPJhnnHIY9/s400/27.jpg&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
q pointer যদি নতুন node কে point করে থাকে তাহলে নতুন node টির link এ LOC point করে থাকা node এর link এর মান চলে আসবে, আর LOC point করে থাকা node এর link এ q চলে আসবে। তাহলে instruction হবে&amp;nbsp;&lt;/div&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;b&gt;q↑.LINK←LOC↑.LINK&amp;nbsp;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;LOC↑.LINK←q

&lt;/b&gt;&lt;/blockquote&gt;
&lt;b&gt;Algorithm-1.1: This algorithm insert a new node between the node which data field is x and its successor&amp;nbsp;&lt;/b&gt;&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
1. Call New_Node(q)&lt;br /&gt;
2. If head = ∧, then: [CASE-I]
 &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; head←q and exit.&lt;br /&gt;
3. LOC←head
4. Repeat step 5 while LOC↑.LINK≠∧ and LOC↑.INFO≠x&lt;br /&gt;
5.&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;                      LOC←LOC↑.LINK&lt;br /&gt;
6. q↑.LINK←LOC↑.LINK and LOC↑.LINK←q&lt;br /&gt;
7. Exit.

&lt;/blockquote&gt;
&lt;b&gt;Flow Chart:&lt;/b&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhhpjnWv1661_NjkKvNgVfoaJ_KrLfU2k5VBFHqLM5Cj1wWLaat7p18JwFyyP_3b0fQgRBujMGGsw6uCXNO9WMf1-ybwOGyn03vFXugPsNfPSgg1hg8VmshfxdfcIZx5vzH7tsXuhyphenhyphenPv4N7/s1600/28.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;376&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhhpjnWv1661_NjkKvNgVfoaJ_KrLfU2k5VBFHqLM5Cj1wWLaat7p18JwFyyP_3b0fQgRBujMGGsw6uCXNO9WMf1-ybwOGyn03vFXugPsNfPSgg1hg8VmshfxdfcIZx5vzH7tsXuhyphenhyphenPv4N7/s400/28.jpg&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;b&gt;Program:&lt;/b&gt; &lt;br /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style=&quot;background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;
&lt;table&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt; 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52&lt;/pre&gt;
&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;&lt;span style=&quot;color: #557799;&quot;&gt;#include&amp;lt;stdio.h&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #557799;&quot;&gt;#include&amp;lt;stdlib.h&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;struct&lt;/span&gt; node{
    &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; INFO;
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;struct&lt;/span&gt; node &lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;LINK;
};
&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;main&lt;/span&gt;(){
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;struct&lt;/span&gt; node &lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;q&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;,&lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;LOC&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;,&lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;LOCP&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;,&lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;head&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;,&lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;p&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;;
  &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; i,n&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;5&lt;/span&gt;,x;
  &lt;span style=&quot;color: #888888;&quot;&gt;/*Create A list*/&lt;/span&gt;
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt;(i&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;i&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;=&lt;/span&gt;n;i&lt;span style=&quot;color: #333333;&quot;&gt;++&lt;/span&gt;){
    q &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; (&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;struct&lt;/span&gt; node &lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;)malloc(&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;sizeof&lt;/span&gt;(&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;struct&lt;/span&gt; node));
    q&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;;
    q&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;INFO &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; i;
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;(p&lt;span style=&quot;color: #333333;&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;)
        head &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; q;
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt;
      p&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; q;
    p&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;q;
  }

  &lt;span style=&quot;color: #888888;&quot;&gt;/*INSERTION*/&lt;/span&gt;
  printf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;Enter a Value of a node:&quot;&lt;/span&gt;);
  scanf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;%d&quot;&lt;/span&gt;,&lt;span style=&quot;color: #333333;&quot;&gt;&amp;amp;&lt;/span&gt;x);

  q &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; (&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;struct&lt;/span&gt; node &lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;)malloc(&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;sizeof&lt;/span&gt;(&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;struct&lt;/span&gt; node));
  q&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;;
  q&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;INFO &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;10&lt;/span&gt;; &lt;span style=&quot;color: #888888;&quot;&gt;/*Manual Value*/&lt;/span&gt;


  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;(head&lt;span style=&quot;color: #333333;&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;){
     head &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; q;
  }
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt;{
    LOC&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;head;
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt;(LOC&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK&lt;span style=&quot;color: #333333;&quot;&gt;!=&lt;/span&gt;&lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt; &lt;span style=&quot;color: #333333;&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; LOC&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;INFO&lt;span style=&quot;color: #333333;&quot;&gt;!=&lt;/span&gt;x){
          LOC&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;LOC&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK;
    }
    q&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;LOC&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK;
    LOC&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;q;
  }

  &lt;span style=&quot;color: #888888;&quot;&gt;/*Traverse*/&lt;/span&gt;
  printf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #666666; font-weight: bold;&quot;&gt;\n\n&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;&lt;/span&gt;);
  p&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;head;
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt;(p&lt;span style=&quot;color: #333333;&quot;&gt;!=&lt;/span&gt;&lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;){
   printf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;%d &quot;&lt;/span&gt;,p&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;INFO);
   p&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;p&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK;
  }

  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
}
&lt;/pre&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://rizoantoufiq.blogspot.com/feeds/8505668180872896185/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://rizoantoufiq.blogspot.com/2016/08/data-structure-linked-list-part-3.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1124334728283106452/posts/default/8505668180872896185'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1124334728283106452/posts/default/8505668180872896185'/><link rel='alternate' type='text/html' href='http://rizoantoufiq.blogspot.com/2016/08/data-structure-linked-list-part-3.html' title='Data Structure: Linked List- Part 3'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEia5v0tHxH3qIWqpRHiZeoq4fUMihtR5Q7m2cJyrk0afi_wS8yFziahOG6W2NptPj-GkzCPVU8CaKX-FWMhAGnPyWCTLwfNgQIezP8TvpPvWsxfGcvoDt63_0c0kVSKsUyIbVhUdsyCQzup/s72-c/23.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1124334728283106452.post-3530454158968127844</id><published>2016-08-16T14:23:00.002-07:00</published><updated>2016-08-17T10:11:50.461-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Data Structure"/><title type='text'>Data Structure: Linked List- Part 2</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;br /&gt;
&lt;b&gt;INSERT A NEW NODE INTO A HEADER LINKED LIST
&lt;/b&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;b&gt;Problem-I:&lt;/b&gt; Insert a new node between a node which data field is x and its predecessor. If x is not found in header linked list then, the new node insert at the end of the list.&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;b&gt;Solution:&lt;/b&gt;
The first objective is to find the location of the node which data field is x. Let LOC be the pointer to point the target node and LOCP be the pointer that point the predecessor of the target node. If there is not exist any node which data field is x, then we set LOC←NULL and LOCP point to the last node of the linked list . If the target node is header node then we set LOCP←NULL and LOC←head.&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;/div&gt;
&lt;b&gt;CASE-I: Insert Node, if there is no node in header linked list i.e. head←∧&lt;/b&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi8dShVaNRPKVdG6mRmAwP5NbbbyRcCC5Sr2AJ2RO-KR5cwIsh9Yj20rqTIdCsjQ8SEBNht03gxxP1O5U-HeMZfu6VwZSLfvpK1_QNaESy2PiKX2kLBga3PngxJyVBzeHEjXRFlkMey1444/s1600/23.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi8dShVaNRPKVdG6mRmAwP5NbbbyRcCC5Sr2AJ2RO-KR5cwIsh9Yj20rqTIdCsjQ8SEBNht03gxxP1O5U-HeMZfu6VwZSLfvpK1_QNaESy2PiKX2kLBga3PngxJyVBzeHEjXRFlkMey1444/s1600/23.jpg&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
q is a pointer that point to a new node. The instruction to insert new node as header node will be&amp;nbsp;&lt;/div&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;b&gt;head←q

&lt;/b&gt;&lt;/blockquote&gt;
&lt;b&gt;CASE-II: Insert Node as Header node i.e. head↑.INFO = x&amp;nbsp;&lt;/b&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEga8wL6j5yNI5bHfBF8OFKHgSyI180Opn_Qq-u-4sxDU1MBdlwNtJZD8ZtJ4yTydbEt2iz5cqDrW2Nb3Oo5fZmpzapuqyfOYzfj6FsOWb5CBPs3_eH1fiDOyzSz2xRC0dLPichzL5J56q02/s1600/22.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;80&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEga8wL6j5yNI5bHfBF8OFKHgSyI180Opn_Qq-u-4sxDU1MBdlwNtJZD8ZtJ4yTydbEt2iz5cqDrW2Nb3Oo5fZmpzapuqyfOYzfj6FsOWb5CBPs3_eH1fiDOyzSz2xRC0dLPichzL5J56q02/s400/22.jpg&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;b&gt;&amp;nbsp;&lt;/b&gt;q is a pointer that point to a new node. The instruction to insert new node as header node will be&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
q↑.LINK ←LOC&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
head←q

&lt;/div&gt;
&lt;/blockquote&gt;
&lt;b&gt;CASE-III: Insert Node between the target node and its predecessor i.e. LOC≠∧&amp;nbsp;&lt;/b&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjxVK__xhuqGTYqNkwnnVBdpKand3BIIVYzmGZ7dHgbvyVY8jUnDEllyKmJbaB8bPEDHyG0-2JgnCyTRS7pcZ5Z9pzTbMN7Z1lmewRF2OYXBUoaRgRc5Id91TUkLFXNDcjnrWoTTVK9oFmX/s1600/21.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;132&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjxVK__xhuqGTYqNkwnnVBdpKand3BIIVYzmGZ7dHgbvyVY8jUnDEllyKmJbaB8bPEDHyG0-2JgnCyTRS7pcZ5Z9pzTbMN7Z1lmewRF2OYXBUoaRgRc5Id91TUkLFXNDcjnrWoTTVK9oFmX/s400/21.jpg&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
q is a pointer that point to a new node. The instruction to insert new node as header node will be&amp;nbsp;&lt;/div&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
LOCP↑.LINK ←q&lt;br /&gt;
q↑.LINK ←LOC

&lt;/blockquote&gt;
&lt;b&gt;CASE IV: Insert node at the end of the list i.e. LOC=∧&amp;nbsp;&lt;/b&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgxAWj131Yzg7pHpW9Ccw5NG8QI6ISSrbdAtZLbaQgBo5t0ikiWGi5ywasUiW7DPJqH4hZzFo6UuKufXxanOA9jynDzxeC0xFsSiydwBVPc1OqZqtjA2dja2Aro-ZL8kvYaOZqfXCqoMKpD/s1600/24.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;97&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgxAWj131Yzg7pHpW9Ccw5NG8QI6ISSrbdAtZLbaQgBo5t0ikiWGi5ywasUiW7DPJqH4hZzFo6UuKufXxanOA9jynDzxeC0xFsSiydwBVPc1OqZqtjA2dja2Aro-ZL8kvYaOZqfXCqoMKpD/s400/24.jpg&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
q is a pointer that point to a new node. The instruction to insert new node as header node will be&amp;nbsp;&lt;/div&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&amp;nbsp;LOCP↑.LINK←q

&lt;/blockquote&gt;
&lt;b&gt;ALGORITHM:&lt;/b&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style=&quot;background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;Algorithm&lt;span style=&quot;color: #333333;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #6600ee; font-weight: bold;&quot;&gt;1.1&lt;/span&gt;: This algorithm insert a &lt;span style=&quot;color: #007020;&quot;&gt;new&lt;/span&gt; node between&amp;nbsp;&lt;/pre&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;the node which data field &lt;span style=&quot;color: #007020;&quot;&gt;is&lt;/span&gt; x and its predecessor
&lt;span style=&quot;color: #6600ee; font-weight: bold;&quot;&gt;1.&lt;/span&gt; Call New_Node(q)
&lt;span style=&quot;color: #6600ee; font-weight: bold;&quot;&gt;2.&lt;/span&gt; If head &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;background-color: #ffaaaa; color: red;&quot;&gt;∧&lt;/span&gt;, then: [CASE&lt;span style=&quot;color: #333333;&quot;&gt;-&lt;/span&gt;I]
            head&lt;span style=&quot;background-color: #ffaaaa; color: red;&quot;&gt;←&lt;/span&gt;q, head&lt;span style=&quot;background-color: #ffaaaa; color: red;&quot;&gt;↑&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;.&lt;/span&gt;INFO&lt;span style=&quot;background-color: #ffaaaa; color: red;&quot;&gt;←&lt;/span&gt;y and &lt;span style=&quot;color: #007020;&quot;&gt;exit&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;.&lt;/span&gt;
&lt;span style=&quot;color: #6600ee; font-weight: bold;&quot;&gt;3.&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;If&lt;/span&gt; head&lt;span style=&quot;background-color: #ffaaaa; color: red;&quot;&gt;↑&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;.&lt;/span&gt;INFO &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; x, then: [CASE&lt;span style=&quot;color: #333333;&quot;&gt;-&lt;/span&gt;II]
            q&lt;span style=&quot;background-color: #ffaaaa; color: red;&quot;&gt;↑&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;.&lt;/span&gt;LINK&lt;span style=&quot;background-color: #ffaaaa; color: red;&quot;&gt;←&lt;/span&gt;head, head&lt;span style=&quot;background-color: #ffaaaa; color: red;&quot;&gt;←&lt;/span&gt;q, and &lt;span style=&quot;color: #007020;&quot;&gt;exit&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;.&lt;/span&gt;
&lt;span style=&quot;color: #6600ee; font-weight: bold;&quot;&gt;4.&lt;/span&gt; LOC&lt;span style=&quot;background-color: #ffaaaa; color: red;&quot;&gt;←&lt;/span&gt;head&lt;span style=&quot;background-color: #ffaaaa; color: red;&quot;&gt;↑&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;.&lt;/span&gt;LINK and LOCP &lt;span style=&quot;background-color: #ffaaaa; color: red;&quot;&gt;←&lt;/span&gt; head
&lt;span style=&quot;color: #6600ee; font-weight: bold;&quot;&gt;5.&lt;/span&gt; Repeat step &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;6&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt; LOC&lt;span style=&quot;background-color: #ffaaaa; color: red;&quot;&gt;≠∧&lt;/span&gt; and LOC&lt;span style=&quot;background-color: #ffaaaa; color: red;&quot;&gt;↑&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;.&lt;/span&gt;INFO&lt;span style=&quot;background-color: #ffaaaa; color: red;&quot;&gt;≠&lt;/span&gt;x
&lt;span style=&quot;color: #6600ee; font-weight: bold;&quot;&gt;6.&lt;/span&gt;                      LOCP&lt;span style=&quot;background-color: #ffaaaa; color: red;&quot;&gt;←&lt;/span&gt;LOC and LOC&lt;span style=&quot;background-color: #ffaaaa; color: red;&quot;&gt;←&lt;/span&gt;LOC&lt;span style=&quot;background-color: #ffaaaa; color: red;&quot;&gt;↑&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;.&lt;/span&gt;LINK
&lt;span style=&quot;color: #6600ee; font-weight: bold;&quot;&gt;7.&lt;/span&gt; IF LOC&lt;span style=&quot;background-color: #ffaaaa; color: red;&quot;&gt;≠∧&lt;/span&gt; then: [CASE&lt;span style=&quot;color: #333333;&quot;&gt;-&lt;/span&gt;III]
            LOCP&lt;span style=&quot;background-color: #ffaaaa; color: red;&quot;&gt;↑&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;.&lt;/span&gt;LINK&lt;span style=&quot;background-color: #ffaaaa; color: red;&quot;&gt;←&lt;/span&gt;q and q&lt;span style=&quot;background-color: #ffaaaa; color: red;&quot;&gt;↑&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;.&lt;/span&gt;LINK&lt;span style=&quot;background-color: #ffaaaa; color: red;&quot;&gt;←&lt;/span&gt;LOC
        ELSE: [CASE&lt;span style=&quot;color: #333333;&quot;&gt;-&lt;/span&gt;IV]
             LOCP&lt;span style=&quot;background-color: #ffaaaa; color: red;&quot;&gt;↑&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;.&lt;/span&gt;LINK&lt;span style=&quot;background-color: #ffaaaa; color: red;&quot;&gt;←&lt;/span&gt;q
&lt;span style=&quot;color: #6600ee; font-weight: bold;&quot;&gt;8.&lt;/span&gt; Exit&lt;span style=&quot;color: #333333;&quot;&gt;.&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;b&gt;FLOW CHART:&lt;/b&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhahod8z2akKdl84duW16mvB6yjvJFKSxmnByl0SRwJ72-RldcFcUypcR0wYeUfZA5Ufw8zD9a2a_r2KvxsSFyqIcbwDwKK0a7jsMW07n5yYO3r6JgAcLDgzKZMqcYrlPOqlgtiheBmOGoh/s1600/26.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;400&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhahod8z2akKdl84duW16mvB6yjvJFKSxmnByl0SRwJ72-RldcFcUypcR0wYeUfZA5Ufw8zD9a2a_r2KvxsSFyqIcbwDwKK0a7jsMW07n5yYO3r6JgAcLDgzKZMqcYrlPOqlgtiheBmOGoh/s400/26.jpg&quot; width=&quot;386&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;b&gt;
Program:&lt;/b&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style=&quot;background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;
&lt;table&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt; 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62&lt;/pre&gt;
&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;&lt;span style=&quot;color: #557799;&quot;&gt;#include&amp;lt;stdio.h&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #557799;&quot;&gt;#include&amp;lt;stdlib.h&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;struct&lt;/span&gt; node{
    &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; INFO;
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;struct&lt;/span&gt; node &lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;LINK;
};
&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;main&lt;/span&gt;(){
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;struct&lt;/span&gt; node &lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;q&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;,&lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;LOC&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;,&lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;LOCP&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;,&lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;head&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;,&lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;p&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;;
  &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; i,n&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;5&lt;/span&gt;,x;
  &lt;span style=&quot;color: #888888;&quot;&gt;/*Create A list*/&lt;/span&gt;
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt;(i&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;i&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;=&lt;/span&gt;n;i&lt;span style=&quot;color: #333333;&quot;&gt;++&lt;/span&gt;){
    q &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; (&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;struct&lt;/span&gt; node &lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;)malloc(&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;sizeof&lt;/span&gt;(&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;struct&lt;/span&gt; node));
    q&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;;
    q&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;INFO &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; i;
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;(p&lt;span style=&quot;color: #333333;&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;)
        head &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; q;
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt;
      p&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; q;
    p&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;q;
  }

  &lt;span style=&quot;color: #888888;&quot;&gt;/*INSERTION*/&lt;/span&gt;
  printf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;Enter a Value of a node:&quot;&lt;/span&gt;);
  scanf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;%d&quot;&lt;/span&gt;,&lt;span style=&quot;color: #333333;&quot;&gt;&amp;amp;&lt;/span&gt;x);

  q &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; (&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;struct&lt;/span&gt; node &lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;)malloc(&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;sizeof&lt;/span&gt;(&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;struct&lt;/span&gt; node));
  q&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;;
  q&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;INFO &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;10&lt;/span&gt;; &lt;span style=&quot;color: #888888;&quot;&gt;/*Manual Value*/&lt;/span&gt;


  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;(head&lt;span style=&quot;color: #333333;&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;){
     head &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; q;
  }
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;(head&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;INFO&lt;span style=&quot;color: #333333;&quot;&gt;==&lt;/span&gt;x){
      q&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;head;
      head&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;q;
  }
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt;{
    LOC&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;head&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK;
    LOCP&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;head;
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt;(LOC&lt;span style=&quot;color: #333333;&quot;&gt;!=&lt;/span&gt;&lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt; &lt;span style=&quot;color: #333333;&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; LOC&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;INFO&lt;span style=&quot;color: #333333;&quot;&gt;!=&lt;/span&gt;x){
          LOCP&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;LOC;
          LOC&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;LOC&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK;
    }
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; (LOC&lt;span style=&quot;color: #333333;&quot;&gt;!=&lt;/span&gt;&lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;){
       LOCP&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;q;
       q&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; LOC;
    }
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt;
      LOCP&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; q;
  }

  &lt;span style=&quot;color: #888888;&quot;&gt;/*Traverse*/&lt;/span&gt;
  printf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #666666; font-weight: bold;&quot;&gt;\n\n&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;&lt;/span&gt;);
  p&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;head;
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt;(p&lt;span style=&quot;color: #333333;&quot;&gt;!=&lt;/span&gt;&lt;span style=&quot;color: #007020;&quot;&gt;NULL&lt;/span&gt;){
   printf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;%d &quot;&lt;/span&gt;,p&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;INFO);
   p&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;p&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;LINK;
  }

  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
}
&lt;/pre&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://rizoantoufiq.blogspot.com/feeds/3530454158968127844/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://rizoantoufiq.blogspot.com/2016/08/data-structure-linked-list-part-2.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1124334728283106452/posts/default/3530454158968127844'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1124334728283106452/posts/default/3530454158968127844'/><link rel='alternate' type='text/html' href='http://rizoantoufiq.blogspot.com/2016/08/data-structure-linked-list-part-2.html' title='Data Structure: Linked List- Part 2'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi8dShVaNRPKVdG6mRmAwP5NbbbyRcCC5Sr2AJ2RO-KR5cwIsh9Yj20rqTIdCsjQ8SEBNht03gxxP1O5U-HeMZfu6VwZSLfvpK1_QNaESy2PiKX2kLBga3PngxJyVBzeHEjXRFlkMey1444/s72-c/23.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1124334728283106452.post-8310832981127441251</id><published>2016-08-15T12:02:00.002-07:00</published><updated>2016-08-23T11:34:28.604-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Data Structure"/><title type='text'>Data Structure: Linked List - Part 1</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;b&gt;Basic&lt;/b&gt; &lt;/div&gt;
Data Structure এর সবচেয়ে গুরুত্বপূর্ন বিষয় হচ্ছে linked list. এটা C প্রোগ্রাম শেখার পর নতুন একটা অধ্যায়। এটা খুব ভালোভাবে রপ্ত করতে পারা, প্রথামিক অবস্থায় সামান্য কঠিন আছে। যেহেতু আমাদের C প্রোগ্রামের structure সম্পর্কে ধারনা আছে তাই, একটা Node কি তা structure দিয়ে বললে অনেকের কাছে সহজ হবে। আমরা parallel কিছু array দিয়েও Linked list ব্যাখা করতে পারি, তবে তা পরে আলোচনা করা যাবে। C তে integer type variable কে point করার জন্য ওই ধরনের একটা pointer লাগে। একইভাবে structure type variable কে point করার জন্য ওই ধরনের একটা pointer লাগবে।&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjEvgQt62VO5dLSfW5FQoilMDqb9CclUT34OSctgWfxrItecjI0ojCt_uA35wEgJLALhwCT2s1K89VRqQQTLp5Gr2Ry0dBEO6qjrjTGJYujSQH8NtcSqqKyWXyksH8OePR2dIbEWH3NjSwv/s1600/1.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;120&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjEvgQt62VO5dLSfW5FQoilMDqb9CclUT34OSctgWfxrItecjI0ojCt_uA35wEgJLALhwCT2s1K89VRqQQTLp5Gr2Ry0dBEO6qjrjTGJYujSQH8NtcSqqKyWXyksH8OePR2dIbEWH3NjSwv/s400/1.jpg&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&amp;nbsp;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: justify;&quot;&gt;
&lt;/div&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;এবার তুমি প্রোগ্রাম টা আরো পরিবর্তন করে, তোমার structure এবং pointer সম্পর্কে ধারনা পরিষ্কার করে নিতে পারো। কোডে যদি নিচের মত করে পরিবর্তন করা হয় তাহলে কি হবে তা তোমরা নিজেরা দেখবে।)&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh0g-AlHfravp5jZityIGgJb3RtCJpqg3egQ4K6GJfOq61cpGlzR0JIxySxAz3N9CkqwUvyiciaSeNm0rWH1TQsRet1KJcUn7aB37lGWWNAiVnfesNoPF5wBxrMZY-eDRSKP0maGGGq92Ag/s1600/2.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh0g-AlHfravp5jZityIGgJb3RtCJpqg3egQ4K6GJfOq61cpGlzR0JIxySxAz3N9CkqwUvyiciaSeNm0rWH1TQsRet1KJcUn7aB37lGWWNAiVnfesNoPF5wBxrMZY-eDRSKP0maGGGq92Ag/s1600/2.jpg&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
এবার আরো এক ধাপ এগিয়ে যায়। এবার আমরা structure এর মাঝেই একই structure 
type pointer declare করব। এটা করব কেন? এটা করার কারন হচ্ছে আমরা চাচ্ছি 
structure  এর ভেতরের pointer টা দিয়ে অন্য একটা একই ধরনে structure type 
variable কে point করতে। এই ধরনের pointer কে বলে self reference pointer.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;siyam rupali&amp;quot;; font-size: 12.0pt; line-height: 115%;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjrjGVuWouiPvHVofRHqdz-B6cqhxcFCAy62FNVXazDvZDJFjFfQ3WShWuwZM56Y0X9yzm0D0XuR6VvCSGU-vMKC5L8N2kYs2lU8Tyn1QfChHQCCmK3571N6Bl7jCslghdECYSdzHSZiJMu/s1600/3.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;155&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjrjGVuWouiPvHVofRHqdz-B6cqhxcFCAy62FNVXazDvZDJFjFfQ3WShWuwZM56Y0X9yzm0D0XuR6VvCSGU-vMKC5L8N2kYs2lU8Tyn1QfChHQCCmK3571N6Bl7jCslghdECYSdzHSZiJMu/s400/3.jpg&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;
&lt;span style=&quot;font-family: &amp;quot;siyam rupali&amp;quot;; font-size: 12.0pt; line-height: 115%;&quot;&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
উপরের উদাহরণে struct node type একটা variable a যার LINK pointer struct node type একটা variable b কে  point করে আছে। আর b এর LINK pointer কাউকেই point করে নাই। অর্থাৎ আমরা একটা ছোট linked list  তৈরি করে ফেলেছি। এবার একটা নতুন term নিয়ে আসি। সেটা হচ্ছে header linked list, যার প্রথম node-টি নাম হচ্ছে header. আর একটা head নামের struct node type এর pointer, point করে থাকবে প্রথম node কে অর্থাৎ a node কে। 
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgN3GWrCRWdZYLz_Wg9dQ6t2M9J6NGXjpgIDOW2p_hjpn8K83YPVs1cV5vKumoiUvLsmT3vy9DWGUfmXx-1_wBoxxtxNPTb9x8P1-R-Q8un2Cv_RRmWUgO8Y4W9LukODB9r2Zh1aHqc7nOJ/s1600/4.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;185&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgN3GWrCRWdZYLz_Wg9dQ6t2M9J6NGXjpgIDOW2p_hjpn8K83YPVs1cV5vKumoiUvLsmT3vy9DWGUfmXx-1_wBoxxtxNPTb9x8P1-R-Q8un2Cv_RRmWUgO8Y4W9LukODB9r2Zh1aHqc7nOJ/s400/4.jpg&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
এখানে linked list বলতে head pointer কেই বোঝাবে। head pointer এর মান হারিয়ে ফেললেই linked list ধবংস হয়ে যাবে। এই ছোট linked list কে traverse করতে চাইলে আমরা head pointer কখনই সামনের node নিয়ে যাব না। তাই traverse/visit করার জন্য আমরা আরো একটা struct node type এর pointer v নিবো যা প্রথম node কে point করবে, তারপর ওই node এর INFO কে process করে, সামনের node কে point করবে। ধরি, v:=head, তাহলে সামনের node কে point করতে চাইলে আমাকে instruction দিতে হবেঃ v:=v↑.LINK. তাহলে এখন v pointer head এর পরের node কে point করে আছে। তাহলে v কতক্ষণ পর্যন্ত next node এ যেতে থাকবে? উত্তর হচ্ছে, v এর মান NULL না হওয়া পর্যন্ত, কারন শেষ node টি NULL কে point করে আছে। 
তাহলে এই পর্যায়ে traverse/visit এর algorithm হবেঃ


&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiwz5ICpFHrN6a4hg84qd-9SPJ51bawuLQMEY4WmocHZoi3zBUbQDeafUrrCvU1NdpQoausdmfFVmFQJRfeS5GhXUUfFU2SQXTPx9Iy5vuEhl8lf_I8N3qmAuiWmssiNkF61tZW8wtAefZT/s1600/A1.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;375&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiwz5ICpFHrN6a4hg84qd-9SPJ51bawuLQMEY4WmocHZoi3zBUbQDeafUrrCvU1NdpQoausdmfFVmFQJRfeS5GhXUUfFU2SQXTPx9Iy5vuEhl8lf_I8N3qmAuiWmssiNkF61tZW8wtAefZT/s400/A1.jpg&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
এই algorithm এর flow chart হবেঃ
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjJbgMzzice36RRppMPowb628XLXE4ezYu43TM5y8A-Ee1lbEkT6tevoayk0MNWOjxyZVUd1HAdYkPDcP5wBXPRaEfbvujQxxAmMyAkF4FwJnHwR_6-NaBfPyrGwnfXYf6hWzipyRFbRrcT/s1600/5.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;400&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjJbgMzzice36RRppMPowb628XLXE4ezYu43TM5y8A-Ee1lbEkT6tevoayk0MNWOjxyZVUd1HAdYkPDcP5wBXPRaEfbvujQxxAmMyAkF4FwJnHwR_6-NaBfPyrGwnfXYf6hWzipyRFbRrcT/s400/5.jpg&quot; width=&quot;378&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
Fig. Traverse a header linked list
&lt;/div&gt;
নিচে code সহ ব্যাখা তুলে ধরা হলঃ 
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiWI9PtN0ULtec1IMfrSt5wC_KtbEL7-YOqiK9vDtIi1iULMj5LWqpP8VGsWxpbnFXejDV3BQHhoXUrR_f4vs7ZZ8lVYW9wtiRtPIIYetITVm9rmA2TtdLRUBgckXFhtVAIVxcmRAXD3EQx/s1600/6.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;318&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiWI9PtN0ULtec1IMfrSt5wC_KtbEL7-YOqiK9vDtIi1iULMj5LWqpP8VGsWxpbnFXejDV3BQHhoXUrR_f4vs7ZZ8lVYW9wtiRtPIIYetITVm9rmA2TtdLRUBgckXFhtVAIVxcmRAXD3EQx/s400/6.jpg&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
এবার আসি, নতুন একটি node নিতে হলে কি করতে হবে? আমাদেরকে memory allocate করতে হবে। এই memory আমরা system হতে নিব। যদি memory না থাকে তাহলে আমরা memory allocate করতে পারব না। ধরি, আমাদের কাছে যে memory আছে সেগুলো নিয়ে একটা linked list তৈরি করা আছে, যার প্রথম node টি struct node type AV ponter variable দ্বারা point করা আছে।&amp;nbsp;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhxMqRKVfH_H7vnwnyS5knXMCbwZc0XtvV1AFsO2sr9ojRaZ5UGo4CQVvUQSgG2UDsZRfH69Qom46LD_8INHEr-R1LQSvTlvyIC1z0BjSb9zSq-dJPWsz45AG551xKXCncVKFQXA0d-E20R/s1600/7.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhxMqRKVfH_H7vnwnyS5knXMCbwZc0XtvV1AFsO2sr9ojRaZ5UGo4CQVvUQSgG2UDsZRfH69Qom46LD_8INHEr-R1LQSvTlvyIC1z0BjSb9zSq-dJPWsz45AG551xKXCncVKFQXA0d-E20R/s1600/7.jpg&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
এখানে AV:=NULL হলে আমরা নতুন node এর জন্য memory আর অবশিষ্ট নেই। আর যদি NULL না হয় তাহলে নিচের চিত্রের মত করে একটা নতুন node নিতে পারব। 
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiHL8qjKv6bfkqytF6TTwR2JSuM5HzXAdjR8fh40dQsmeh-L_FFjY-7ZxD5MdYfHNhsLx-XGGUGWb7pOdsgjAduuoZRzIE6goaIRz26eesFRNE91uGXiUJz1TfFsqUZEfipSOjzA-l_vM5L/s1600/8.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiHL8qjKv6bfkqytF6TTwR2JSuM5HzXAdjR8fh40dQsmeh-L_FFjY-7ZxD5MdYfHNhsLx-XGGUGWb7pOdsgjAduuoZRzIE6goaIRz26eesFRNE91uGXiUJz1TfFsqUZEfipSOjzA-l_vM5L/s1600/8.jpg&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
নতুন node allocate করার algorithm হবেঃ
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhm5a-zLqst8hpEsQs8IXrK40RYnQ6u-0yipSQdo1KO5La4i6JdgLjf5S8Rn4SCtnmYkYQRl83ep1q2NJQ-DH5DZevLU-2kC8PGVffOhXENRL09R2S-P18hGtR_bEaptBkQYUUKnWmd9YmY/s1600/A2.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;400&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhm5a-zLqst8hpEsQs8IXrK40RYnQ6u-0yipSQdo1KO5La4i6JdgLjf5S8Rn4SCtnmYkYQRl83ep1q2NJQ-DH5DZevLU-2kC8PGVffOhXENRL09R2S-P18hGtR_bEaptBkQYUUKnWmd9YmY/s400/A2.jpg&quot; width=&quot;380&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
নতুন node allocate করার flow chart হবেঃ
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEialZGFBeVKG4kDjRVSn5fpCKPp9MEo3WGmcqAJznQQQD62aaoQgAOyzM-SruT02ZERKTB8mlhOW1mll6MhAQ3WxgmjnSf2H9E9oV4iSiq58QLKaXv01ZVftVqiisGzHNp3jU5dcmN1GqmB/s1600/9.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEialZGFBeVKG4kDjRVSn5fpCKPp9MEo3WGmcqAJznQQQD62aaoQgAOyzM-SruT02ZERKTB8mlhOW1mll6MhAQ3WxgmjnSf2H9E9oV4iSiq58QLKaXv01ZVftVqiisGzHNp3jU5dcmN1GqmB/s1600/9.jpg&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
আমরা C program এ malloc function এর মাধ্যমে memory allocation করে থাকি, নিচে code এর মাধ্যমে দেখানো হল।&amp;nbsp;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgwqZV8-vqpRK8TWaKZCNJlzogHEqPA-zn0oSlt3AVCmm0iik4J7xtlpnmO1yadRNP3OYlnlGthvMFfGlMLNFF-65LFJTN2CSzCey6EwzHw7aynVpkLhgXbP7eyM6RLqPahoY8KrG2e1-Fp/s1600/10.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;400&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgwqZV8-vqpRK8TWaKZCNJlzogHEqPA-zn0oSlt3AVCmm0iik4J7xtlpnmO1yadRNP3OYlnlGthvMFfGlMLNFF-65LFJTN2CSzCey6EwzHw7aynVpkLhgXbP7eyM6RLqPahoY8KrG2e1-Fp/s400/10.jpg&quot; width=&quot;312&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;b&gt;Header Linked List তৈরি করা (Create A Header Linked List)&amp;nbsp;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
এবার একটা সমস্যা উল্লেখ করব, তার জন্য algorithm তৈরি করব, তারপর flow chart আঁকব, এবং সর্বশেষে code এর মাধ্যমে implement করব।&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;b&gt;সমস্যাঃ &lt;/b&gt;আমরা একটা header linked list তৈরি করতে চাই যেখানে ব্যবহারকারি (user) বলে দিবে কতটা Node থাকবে আর তাদের INFO কি কি হবে।
Algorithm:

&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjkq9lnQUTM5L5bR-u1HUyoFJVrvXbQEsuSspzSMI89B6bnX_1sf_igOr6WWjIzmdJX9qnR6M_WkwE_g-JXa7Hj0lE_FToMzKWRnUFiK_RUzCIh8OL2yKjtSb33sRYuJqbf-LjQn59fW_TI/s1600/A3.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;308&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjkq9lnQUTM5L5bR-u1HUyoFJVrvXbQEsuSspzSMI89B6bnX_1sf_igOr6WWjIzmdJX9qnR6M_WkwE_g-JXa7Hj0lE_FToMzKWRnUFiK_RUzCIh8OL2yKjtSb33sRYuJqbf-LjQn59fW_TI/s320/A3.jpg&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;b&gt;Flow Chart:&amp;nbsp;&lt;/b&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi-Lhg1CwC7mgIQA8CXdNXLH06oQHWBzJdB1iZWFqd18GMY7qTyzNTXPhNu91evfQPzWAoIFakBjRU_Yqh4s0mN1MkwPD_tWA0f5BHGKqPegbxnbHYBzs7JeqI-dYyVDxt0_b_0PeqIBIlf/s1600/11.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;400&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi-Lhg1CwC7mgIQA8CXdNXLH06oQHWBzJdB1iZWFqd18GMY7qTyzNTXPhNu91evfQPzWAoIFakBjRU_Yqh4s0mN1MkwPD_tWA0f5BHGKqPegbxnbHYBzs7JeqI-dYyVDxt0_b_0PeqIBIlf/s400/11.jpg&quot; width=&quot;352&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;b&gt;CODE IN C:
&lt;/b&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhu0Rd8Rhyphenhyphen-Yje4UatPR4ONRRBxRk7eeZ7S7lmFv-jJJTkp5MUYhZCMflD5ry7jeumw2Y_FEe4_LomWFwMsC6z3DOF8SxclPIcmBa0MAP6R-_DFpnNK8D2FPK6pD_BU72cGNu2oEPM5DkOg/s1600/12.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhu0Rd8Rhyphenhyphen-Yje4UatPR4ONRRBxRk7eeZ7S7lmFv-jJJTkp5MUYhZCMflD5ry7jeumw2Y_FEe4_LomWFwMsC6z3DOF8SxclPIcmBa0MAP6R-_DFpnNK8D2FPK6pD_BU72cGNu2oEPM5DkOg/s1600/12.jpg&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;b&gt;সমস্যাঃ&lt;/b&gt; একটা মান x বিশিষ্ট node, header linked list হতে খুঁজে বের করতে চাই।&lt;br /&gt;
&lt;b&gt;Algorithm:&lt;/b&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiXF6niDfrM1PV6tcHhY1FS3z6XToJGGnddiwhQUGnY0e0jCXxASNu_hkcUuKBp_UV23hyXx9ismckVrY2Wdleqs2hM6nsMig3oGwKZP6XsYiQM9alpBykKq0lL5b8s9ixKg72w8VRvn0e5/s1600/A4.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiXF6niDfrM1PV6tcHhY1FS3z6XToJGGnddiwhQUGnY0e0jCXxASNu_hkcUuKBp_UV23hyXx9ismckVrY2Wdleqs2hM6nsMig3oGwKZP6XsYiQM9alpBykKq0lL5b8s9ixKg72w8VRvn0e5/s1600/A4.jpg&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;b&gt;Flow Chart:&lt;/b&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEheQUmFopoWa_rqIUK1t_O1lHX0BS0sov8b9ZHxSsUyoAteulYDT_jRNHcO7idAkk03796pIvwAR72GUy3AdIW6R2S32NxRT15JmM_Tae0itc9iVmwpXsOYdSLVxINa-59u8vrsY7hXGzd4/s1600/13.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;371&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEheQUmFopoWa_rqIUK1t_O1lHX0BS0sov8b9ZHxSsUyoAteulYDT_jRNHcO7idAkk03796pIvwAR72GUy3AdIW6R2S32NxRT15JmM_Tae0itc9iVmwpXsOYdSLVxINa-59u8vrsY7hXGzd4/s400/13.jpg&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;b&gt;&amp;nbsp;Code:&lt;/b&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjvTyNFAALPxHds-JgTAzRHA19wI8jHSm_0rwSJpVlbPgnJXwiEewninFEAffk9drKnRdVGOPzAM3Nk0uUxUTYhEnJ0aOjVbFhRrrfifRC2r8qIBj5IxcOTz9SjMrFtUsuw1nxjP7rg8lN0/s1600/14.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;640&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjvTyNFAALPxHds-JgTAzRHA19wI8jHSm_0rwSJpVlbPgnJXwiEewninFEAffk9drKnRdVGOPzAM3Nk0uUxUTYhEnJ0aOjVbFhRrrfifRC2r8qIBj5IxcOTz9SjMrFtUsuw1nxjP7rg8lN0/s640/14.jpg&quot; width=&quot;331&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;b&gt;&amp;nbsp;&lt;/b&gt; &lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://rizoantoufiq.blogspot.com/feeds/8310832981127441251/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://rizoantoufiq.blogspot.com/2016/08/data-structure-linked-list-part-1.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1124334728283106452/posts/default/8310832981127441251'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1124334728283106452/posts/default/8310832981127441251'/><link rel='alternate' type='text/html' href='http://rizoantoufiq.blogspot.com/2016/08/data-structure-linked-list-part-1.html' title='Data Structure: Linked List - Part 1'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjEvgQt62VO5dLSfW5FQoilMDqb9CclUT34OSctgWfxrItecjI0ojCt_uA35wEgJLALhwCT2s1K89VRqQQTLp5Gr2Ry0dBEO6qjrjTGJYujSQH8NtcSqqKyWXyksH8OePR2dIbEWH3NjSwv/s72-c/1.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1124334728283106452.post-4477431857397039196</id><published>2015-12-03T02:41:00.002-08:00</published><updated>2015-12-03T08:25:38.375-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="LightOJ"/><category scheme="http://www.blogger.com/atom/ns#" term="LightOJ-Math"/><title type='text'>LightOJ 1098 - A New Function</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Problems: &lt;a href=&quot;http://lightoj.com:81/volume/problem/1098&quot; target=&quot;_blank&quot;&gt;LightOJ 1098 - A New Function&lt;/a&gt;&lt;br /&gt;
Explain:&lt;br /&gt;
The function&amp;nbsp; SOD(n)&amp;nbsp; (sum of divisors) is defined as the summation of all the actual divisors of an integer number n.The function CSOD(n) (cumulative SOD) of an integer n, is defined as below:&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhao1ZdESvwDd736wyIyu1deMp412Ra6l5xNF8t_Zf8kAkEP63qji0xUrazji2cZWiAcMhbRgrHEi35DUbSB6A4GyH0sfe5pqAuvrHjgJCU7KS5XTuOhaoDkwKim0ydS4lzu8ckHTn2yEoP/s1600/Untitledd.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhao1ZdESvwDd736wyIyu1deMp412Ra6l5xNF8t_Zf8kAkEP63qji0xUrazji2cZWiAcMhbRgrHEi35DUbSB6A4GyH0sfe5pqAuvrHjgJCU7KS5XTuOhaoDkwKim0ydS4lzu8ckHTn2yEoP/s1600/Untitledd.jpg&quot; /&gt; &lt;/a&gt;&amp;nbsp;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;/div&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;Let P =&amp;nbsp; 1 - 25 &lt;br /&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiBgGcSsqSGpdQbfsTwHrnX-37lDQulXji6AKzpov-CCXVVEYxbINJZfkJK3UkBJuau2mULeVW7SrrWJ-amoPjW9H9Wz0pyk4WLegAFay3S5yRFbob-tSSOxNpgBXChCB1OzAKv2Z7HToCc/s1600/Untitledd.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;320&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiBgGcSsqSGpdQbfsTwHrnX-37lDQulXji6AKzpov-CCXVVEYxbINJZfkJK3UkBJuau2mULeVW7SrrWJ-amoPjW9H9Wz0pyk4WLegAFay3S5yRFbob-tSSOxNpgBXChCB1OzAKv2Z7HToCc/s320/Untitledd.jpg&quot; width=&quot;227&quot; /&gt;&lt;/a&gt;&lt;br /&gt;
Total: 173&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Given Number P = 25, i = 2 , j= P/i = 12&lt;br /&gt;
From above calculation, we can find out two relations&lt;br /&gt;
First Relation&lt;br /&gt;
2+3+4+5+6+7+8+9+10+11+12 &lt;br /&gt;
= 1+2+3+4+5+6+7+8+9+10+11+12-1&amp;nbsp; &lt;br /&gt;
= j*(j+1)/2 - (i-1)*(i-1+1)/2&amp;nbsp;&amp;nbsp; &lt;br /&gt;
= j*(j+1)/2 - (i-1)*(i)/2&lt;br /&gt;
= (j^2+j - i^2 + i)/2&lt;br /&gt;
= ((j-i)(j+i)+(j+i))/2&lt;br /&gt;
= ((j+i)(j-i+1))/2&amp;nbsp; (General Form)&lt;br /&gt;
= (14 * 11 )/2 = 77&lt;br /&gt;
&lt;br /&gt;
Second Relation&lt;br /&gt;
2x10 = i*(j-i)&amp;nbsp; (General Form)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; = 20&lt;br /&gt;
&lt;br /&gt;
Again&lt;br /&gt;
&amp;nbsp;i&amp;nbsp; = 3 ,&amp;nbsp; j = P/i = 8&lt;br /&gt;
&lt;br /&gt;
First Relation&lt;br /&gt;
3+4+5+6+7+8 &lt;br /&gt;
= (1+2+3+4+5+6+7+8) - (1+2) &lt;br /&gt;
= (j*(j+1)/2 - (i-1)*(i-1+1)/2&lt;br /&gt;
= ((j+i)(j-i+1))/2&amp;nbsp; (General Form)&lt;br /&gt;
= (11 * 6 )/2 = 33&lt;br /&gt;
&lt;br /&gt;
Second Relation&lt;br /&gt;
3x5 = i*(j-i)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; = 15&lt;br /&gt;
&lt;br /&gt;
Again&lt;br /&gt;
&amp;nbsp;i&amp;nbsp; = 4 ,&amp;nbsp; j = P/i = 6&lt;br /&gt;
&lt;br /&gt;
First Relation&lt;br /&gt;
4+5+6 &lt;br /&gt;
= (1+2+3+4+5+6) - (1+2+3) &lt;br /&gt;
= (j*(j+1)/2 - (i-1)*(i-1+1)/2&lt;br /&gt;
= ((j+i)(j-i+1))/2&amp;nbsp; (General Form)&lt;br /&gt;
= (10 * 3 )/2 = 15&lt;br /&gt;
&lt;br /&gt;
Second Relation&lt;br /&gt;
4x2 = i*(j-i)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; = 8&lt;br /&gt;
&lt;br /&gt;
Again&lt;br /&gt;
&amp;nbsp;i&amp;nbsp; = 5 ,&amp;nbsp; j = P/i = 5&lt;br /&gt;
&lt;br /&gt;
First Relation&lt;br /&gt;
5 &lt;br /&gt;
= (1+2+3+4+5) - (1+2+3+4) &lt;br /&gt;
= (j*(j+1)/2 - (i-1)*(i-1+1)/2&lt;br /&gt;
= ((j+i)(j-i+1))/2&amp;nbsp; (General Form)&lt;br /&gt;
= (10 * 1 )/2 = 5&lt;br /&gt;
&lt;br /&gt;
Second Relation&lt;br /&gt;
5x0 = i*(j-i)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; = 0&lt;br /&gt;
&lt;br /&gt;
SUM = 77+20+33+15+15+8+5+0 = 173&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
-------&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style=&quot;background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;&lt;span style=&quot;color: #557799;&quot;&gt;#include &amp;lt;stdio.h&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;typedef&lt;/span&gt; &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;long&lt;/span&gt; &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;long&lt;/span&gt; LL;

&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;solve&lt;/span&gt;() {
  LL n,ans &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
  LL i,j;

  scanf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;%lld&quot;&lt;/span&gt;,&lt;span style=&quot;color: #333333;&quot;&gt;&amp;amp;&lt;/span&gt;n);
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt; (i &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;2&lt;/span&gt;; i &lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt; i &lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;=&lt;/span&gt; n; &lt;span style=&quot;color: #333333;&quot;&gt;++&lt;/span&gt;i) {
    j &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; n &lt;span style=&quot;color: #333333;&quot;&gt;/&lt;/span&gt; i;
    ans &lt;span style=&quot;color: #333333;&quot;&gt;+=&lt;/span&gt; (i &lt;span style=&quot;color: #333333;&quot;&gt;+&lt;/span&gt; j) &lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt; (j &lt;span style=&quot;color: #333333;&quot;&gt;-&lt;/span&gt; i &lt;span style=&quot;color: #333333;&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;) &lt;span style=&quot;color: #333333;&quot;&gt;/&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;2&lt;/span&gt;;
    ans &lt;span style=&quot;color: #333333;&quot;&gt;+=&lt;/span&gt; i &lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt; (j &lt;span style=&quot;color: #333333;&quot;&gt;-&lt;/span&gt; i);
  }
  printf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;%lld&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #666666; font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;&lt;/span&gt;, ans);
}

&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;main&lt;/span&gt;() {
  &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; tc;
  scanf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;%d&quot;&lt;/span&gt;,&lt;span style=&quot;color: #333333;&quot;&gt;&amp;amp;&lt;/span&gt;tc);
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; i &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;; i &lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt; tc; &lt;span style=&quot;color: #333333;&quot;&gt;++&lt;/span&gt;i) {
    printf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;Case %d: &quot;&lt;/span&gt;, i &lt;span style=&quot;color: #333333;&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;);
    solve();
  }
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://rizoantoufiq.blogspot.com/feeds/4477431857397039196/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://rizoantoufiq.blogspot.com/2015/12/lightoj-1098-new-function.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1124334728283106452/posts/default/4477431857397039196'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1124334728283106452/posts/default/4477431857397039196'/><link rel='alternate' type='text/html' href='http://rizoantoufiq.blogspot.com/2015/12/lightoj-1098-new-function.html' title='LightOJ 1098 - A New Function'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhao1ZdESvwDd736wyIyu1deMp412Ra6l5xNF8t_Zf8kAkEP63qji0xUrazji2cZWiAcMhbRgrHEi35DUbSB6A4GyH0sfe5pqAuvrHjgJCU7KS5XTuOhaoDkwKim0ydS4lzu8ckHTn2yEoP/s72-c/Untitledd.jpg" height="72" width="72"/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1124334728283106452.post-1783694756971309626</id><published>2015-10-23T01:59:00.003-07:00</published><updated>2015-10-23T02:03:19.002-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Bitwise Operator"/><category scheme="http://www.blogger.com/atom/ns#" term="Number System"/><title type='text'>Number System - Covert a Decimal Number to Octal and Hex Number</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
It is&amp;nbsp; very interesting topic. If we simple think, the process of conversion from decimal number to Octal number, then we can follow the below steps:&lt;br /&gt;
1. Let x = 15&lt;br /&gt;
2. r[0] = x%8 = 7&lt;br /&gt;
3. x = x/8 = 1&lt;br /&gt;
4. r[1] = x%8 = 1;&lt;br /&gt;
5. x = x/8 = 0&lt;br /&gt;
Terminate the algorithm, we find (15)&lt;span style=&quot;font-size: x-small;&quot;&gt;10&lt;/span&gt; = (17)&lt;span style=&quot;font-size: x-small;&quot;&gt;8 [Note: reverse r]&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;But if we use bit-wise operator, then it makes more interesting and efficient. &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;1. Let x = 15 = 1111&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;2. r[0] = last three bits = x&amp;amp;7 = 7&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Explain:&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;1111&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;0111&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;-------&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;0111&amp;nbsp; (AND)&lt;br /&gt;&lt;br /&gt;3. x = right shift by 3 = x&amp;gt;&amp;gt;3 &lt;br /&gt;x = (1)&lt;br /&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;4. r[1] = x&amp;amp;7 = 1&lt;br /&gt;5. x = x&amp;gt;&amp;gt;3 = 0&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Terminate the algorithm, we find (15)&lt;span style=&quot;font-size: x-small;&quot;&gt;10&lt;/span&gt; = (17)&lt;span style=&quot;font-size: x-small;&quot;&gt;8 [Note: reverse r]&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Code:&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&amp;nbsp; &lt;/span&gt; &lt;/span&gt;

&lt;br /&gt;
&lt;hr /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style=&quot;background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;&lt;span style=&quot;color: #557799;&quot;&gt;#include&amp;lt;stdio.h&amp;gt;&lt;/span&gt;

&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;main&lt;/span&gt;(){
  &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; x&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;,tx;
  &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;char&lt;/span&gt; rx[&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;12&lt;/span&gt;];
  &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; cb&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;,i;
  printf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;Enter a Number: &quot;&lt;/span&gt;);
  scanf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;%d&quot;&lt;/span&gt;,&lt;span style=&quot;color: #333333;&quot;&gt;&amp;amp;&lt;/span&gt;x);

  tx &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; x;
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt;(tx){
      rx[&lt;span style=&quot;color: #333333;&quot;&gt;++&lt;/span&gt;cb] &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; (tx&lt;span style=&quot;color: #333333;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;7&lt;/span&gt;);
      tx &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; tx&lt;span style=&quot;color: #333333;&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;3&lt;/span&gt;;
  }

  printf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;Result:&quot;&lt;/span&gt;);
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt; (i&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;cb;i;&lt;span style=&quot;color: #333333;&quot;&gt;--&lt;/span&gt;i)
       printf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;%d&quot;&lt;/span&gt;,rx[i]);

  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;hr /&gt;
&lt;br /&gt;
Similarly we can convert a decimal number into Hexadecimal number.&lt;br /&gt;
1. Let x = 17&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;2. r[0] = last four bits = x&amp;amp;15 =1&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Explain:&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;10001&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;01111&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;-------&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;00001&amp;nbsp; (AND)&lt;br /&gt;&lt;br /&gt;3. x = right shift by 4 = x&amp;gt;&amp;gt;4 &lt;br /&gt;x = (1)&lt;br /&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;4. r[1] = x&amp;amp;15 = 1&lt;br /&gt;5. x = x&amp;gt;&amp;gt;4 = 0&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Terminate the algorithm, we find (17)&lt;span style=&quot;font-size: x-small;&quot;&gt;10&lt;/span&gt; = (11)&lt;span style=&quot;font-size: x-small;&quot;&gt;16 [Note: reverse r]&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
code:&lt;br /&gt;
&lt;hr /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style=&quot;background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;&lt;span style=&quot;color: #557799;&quot;&gt;#include&amp;lt;stdio.h&amp;gt;&lt;/span&gt;

&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;main&lt;/span&gt;(){
  &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; x&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;,tx;
  &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;char&lt;/span&gt; rx[&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;12&lt;/span&gt;];
  &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; cb&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;,i;
  printf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;Enter a Number: &quot;&lt;/span&gt;);
  scanf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;%d&quot;&lt;/span&gt;,&lt;span style=&quot;color: #333333;&quot;&gt;&amp;amp;&lt;/span&gt;x);

  tx &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; x;
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt;(tx){
      rx[&lt;span style=&quot;color: #333333;&quot;&gt;++&lt;/span&gt;cb] &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; tx&lt;span style=&quot;color: #333333;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;15&lt;/span&gt;;
      tx &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; tx&lt;span style=&quot;color: #333333;&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;4&lt;/span&gt;;
  }

  printf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;Result:&quot;&lt;/span&gt;);
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt; (i&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;cb;i;&lt;span style=&quot;color: #333333;&quot;&gt;--&lt;/span&gt;i){
      &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; (rx[i]&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;9&lt;/span&gt;)
        printf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;%d&quot;&lt;/span&gt;,rx[i]);
      &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt;
        printf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;%c&quot;&lt;/span&gt;,rx[i]&lt;span style=&quot;color: #333333;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #0044dd;&quot;&gt;&#39;A&#39;&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;10&lt;/span&gt;);
  }
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;hr /&gt;
&lt;b&gt;Analysis:&lt;/b&gt;if r[i] =10 &lt;br /&gt;
rx[i] +&#39;A&#39; -10 = &#39;A&#39; &lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://rizoantoufiq.blogspot.com/feeds/1783694756971309626/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://rizoantoufiq.blogspot.com/2015/10/number-system-covert-decimal-number-to.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1124334728283106452/posts/default/1783694756971309626'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1124334728283106452/posts/default/1783694756971309626'/><link rel='alternate' type='text/html' href='http://rizoantoufiq.blogspot.com/2015/10/number-system-covert-decimal-number-to.html' title='Number System - Covert a Decimal Number to Octal and Hex Number'/><author><name>Unknown</name><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-1124334728283106452.post-2738637666660105220</id><published>2015-10-21T10:50:00.002-07:00</published><updated>2015-10-22T14:32:16.788-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Bitwise Operator"/><category scheme="http://www.blogger.com/atom/ns#" term="Number System"/><title type='text'>Number Conversion -1 : Decimal to Binary</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
At first glance, we think about the below algorithm&lt;br /&gt;
1. take input x&lt;br /&gt;
2. r[i] = x%2;&lt;br /&gt;
3. x = x/2&lt;br /&gt;
4. do above steps until x =0&lt;br /&gt;
&lt;br /&gt;
the complexity of this algorithm is O(lgn) , where n = decimal number.&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Code&lt;/b&gt;
&lt;br /&gt;
&lt;hr /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style=&quot;background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;&lt;span style=&quot;color: #cc0000; font-weight: bold;&quot;&gt;#include&amp;lt;cstdio&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;namespace&lt;/span&gt; std;

&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;main&lt;/span&gt;(){
  &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; x,i;
  &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;bool&lt;/span&gt; r[&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;32&lt;/span&gt;]={&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;}; &lt;span style=&quot;color: #888888;&quot;&gt;// think about 32 bits number&lt;/span&gt;

  printf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;Enter a decimal Number: &quot;&lt;/span&gt;);
  scanf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;%d&quot;&lt;/span&gt;,&amp;amp;x);
  i = &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt;(x){
     r[++i] = x%&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;2&lt;/span&gt;;
     x =x/&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;2&lt;/span&gt;;
  }

  printf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;Result: &quot;&lt;/span&gt;);
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt;(i){
     printf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;%d&quot;&lt;/span&gt;,r[i--]);
  }

  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;hr /&gt;
Now, we can replace the % and / sign by bitwise operator. How? &lt;br /&gt;
x = 1011&lt;br /&gt;
Objective: take last one bit into r[i]&lt;br /&gt;
x = 1011&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0001&lt;br /&gt;
-------------&lt;br /&gt;
AND&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1 --- &amp;gt; r[i]&lt;br /&gt;
x = 1011&lt;br /&gt;
just make a right shift, we get&lt;br /&gt;
x = (101)&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;code&lt;/b&gt; &lt;br /&gt;
&lt;hr /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style=&quot;background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;&lt;span style=&quot;color: #cc0000; font-weight: bold;&quot;&gt;#include&amp;lt;cstdio&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;namespace&lt;/span&gt; std;

&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;main&lt;/span&gt;(){
  &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; x,i;
  &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;bool&lt;/span&gt; r[&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;32&lt;/span&gt;]={&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;}; &lt;span style=&quot;color: #888888;&quot;&gt;// think about 32 bits number&lt;/span&gt;

  printf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;Enter a decimal Number: &quot;&lt;/span&gt;);
  scanf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;%d&quot;&lt;/span&gt;,&amp;amp;x);
  i = &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt;(x){
     r[++i] = x&amp;amp;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;
     x =x&amp;gt;&amp;gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;
  }

  printf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;Result: &quot;&lt;/span&gt;);
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt;(i){
     printf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;%d&quot;&lt;/span&gt;,r[i--]);
  }

  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;hr /&gt;
Although the complexity of this algorithm is O(lgn).&lt;br /&gt;
&lt;br /&gt;
Would we reduce this complexity into O(1)!!!!!! Yes. We can reverse the bits of a number in O(1) if we know the size of the number.   We can implement it using look up table.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Code:&lt;/b&gt; &lt;br /&gt;
&lt;hr /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style=&quot;background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;&lt;span style=&quot;color: #cc0000; font-weight: bold;&quot;&gt;#include&amp;lt;stdio.h&amp;gt;&lt;/span&gt;

&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;main&lt;/span&gt;(){
  &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; x=&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;,tx,c,flag;

  printf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;Enter a number : &quot;&lt;/span&gt;);
  scanf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;%d&quot;&lt;/span&gt;,&amp;amp;x);

  x = ((x&amp;amp;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0xAAAAAAAA&lt;/span&gt;) &amp;gt;&amp;gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;)| ((x&amp;amp; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0x55555555&lt;/span&gt;)&amp;lt;&amp;lt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;);
  x = ((x&amp;amp;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0xCCCCCCCC&lt;/span&gt;) &amp;gt;&amp;gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;2&lt;/span&gt;)| ((x&amp;amp; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0x33333333&lt;/span&gt;)&amp;lt;&amp;lt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;2&lt;/span&gt;);
  x = ((x&amp;amp;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0xF0F0F0F0&lt;/span&gt;) &amp;gt;&amp;gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;4&lt;/span&gt;) | ((x&amp;amp; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0x0F0F0F0F&lt;/span&gt;)&amp;lt;&amp;lt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;4&lt;/span&gt;);
  x = ((x&amp;amp;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0xFF00FF00&lt;/span&gt;) &amp;gt;&amp;gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;8&lt;/span&gt;) | ((x&amp;amp; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0x00FF00FF&lt;/span&gt;)&amp;lt;&amp;lt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;8&lt;/span&gt;);
  x = x &amp;gt;&amp;gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;16&lt;/span&gt; | x&amp;lt;&amp;lt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;16&lt;/span&gt;;

  printf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;Result: &quot;&lt;/span&gt;);
  tx = x;
  c = &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;32&lt;/span&gt;;
  flag = &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt;(c--){
     &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; (flag ==&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;) {
        &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; (tx&amp;amp;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;) printf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;1&quot;&lt;/span&gt;);
        &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt; printf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;0&quot;&lt;/span&gt;);
     }
     &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt;{
         &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; (tx&amp;amp;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;){
             flag = &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
             printf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;1&quot;&lt;/span&gt;);
         }
     }
     tx = tx&amp;gt;&amp;gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;
  }

  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;hr /&gt;
Now your task is to use paper and pencil to analysis this code. &lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://rizoantoufiq.blogspot.com/feeds/2738637666660105220/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://rizoantoufiq.blogspot.com/2015/10/number-conversion-1-decimal-to-binary.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1124334728283106452/posts/default/2738637666660105220'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1124334728283106452/posts/default/2738637666660105220'/><link rel='alternate' type='text/html' href='http://rizoantoufiq.blogspot.com/2015/10/number-conversion-1-decimal-to-binary.html' title='Number Conversion -1 : Decimal to Binary'/><author><name>Unknown</name><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-1124334728283106452.post-7105439174334042430</id><published>2015-10-20T14:57:00.000-07:00</published><updated>2015-10-20T14:57:04.679-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Uva - Geometric Sequences and Sums"/><category scheme="http://www.blogger.com/atom/ns#" term="Uva Problems &amp; Solution"/><title type='text'>UVa 107－The Cat in the Hat</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Problem: &lt;a href=&quot;https://uva.onlinejudge.org/external/1/107.pdf&quot;&gt;107 - The Cat in the Hat&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Explain:&lt;/b&gt;&lt;br /&gt;
Given, Initial Height of a Cat, IH&lt;br /&gt;
The number of worker Cats of height 1, WC1&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Objective:&lt;/b&gt;&lt;br /&gt;
Find, sum of all the cats&#39; heights, SCH =?&lt;br /&gt;
Number of Not Working Cat, NWC =?&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
To understand this problem, Let IH = 216, WC1 = 125&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;“The smallest Cats are of height one;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;These are the cats that get the work done;” [Copied from Uva 107]&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
If, IH = 1 and WC1 = 1,&lt;br /&gt;
This Indicate, TC = 1, NWC = 0&amp;nbsp;&amp;nbsp;&lt;br /&gt;
This also indicates the termination condition.&lt;br /&gt;
&lt;b&gt;“The number of cats inside each (non-smallest) cat&#39;s hat is a constant, &lt;i&gt;N&lt;/i&gt;. The height of these cats-in-a-hat is 1/(N+1) times the height of the cat whose hat they are in.&lt;/b&gt;&lt;b&gt;” [Copied from Uva 107]&lt;/b&gt;&lt;br /&gt;
Let Constant N = 5,&lt;br /&gt;
&lt;br /&gt;
&lt;table border=&quot;1&quot;&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td width=&quot;181&quot;&gt;&lt;b&gt;Not Working Cat ,NWC&lt;/b&gt;&lt;/td&gt;
&lt;td width=&quot;240&quot;&gt;&lt;b&gt;Height of Each Cat, Given, IH = 216&lt;/b&gt;&lt;/td&gt;
&lt;td width=&quot;217&quot;&gt;&lt;b&gt;sum of all the cats&#39; heights, SCH &lt;/b&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width=&quot;181&quot;&gt;&lt;/td&gt;
&lt;td width=&quot;240&quot;&gt;&lt;/td&gt;
&lt;td width=&quot;217&quot;&gt;∑&lt;sub&gt;height&lt;/sub&gt; = 216&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width=&quot;181&quot;&gt;1&lt;/td&gt;
&lt;td width=&quot;240&quot;&gt;The number of cats inside each (non-smallest) cat&#39;s (height = 216) hat is 5, Height = (216/(5+1) = 36&lt;/td&gt;
&lt;td width=&quot;217&quot;&gt;∑&lt;sub&gt;height&lt;/sub&gt; = (216/6)*5 + 216=396&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width=&quot;181&quot;&gt;1+5&lt;/td&gt;
&lt;td width=&quot;240&quot;&gt;The number of cats inside each (non-smallest) cat&#39;s (height = 36) hat is 5,&lt;br /&gt;
Height = 36/6 = 6&lt;/td&gt;
&lt;td width=&quot;217&quot;&gt;∑&lt;sub&gt;height&lt;/sub&gt; = (216/6&lt;sup&gt;2&lt;/sup&gt;)*5&lt;sup&gt;2&lt;/sup&gt; + 396 = 546&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width=&quot;181&quot;&gt;1+5+5&lt;sup&gt;2&lt;/sup&gt; = 31&lt;/td&gt;
&lt;td width=&quot;240&quot;&gt;The number of cats inside each (non-smallest) cat&#39;s (height = 36) hat is 5,&lt;br /&gt;
Height = 36/6 = 1&lt;/td&gt;
&lt;td width=&quot;217&quot;&gt;∑&lt;sub&gt;height&lt;/sub&gt; = (216/6&lt;sup&gt;3&lt;/sup&gt;)*5&lt;sup&gt;3&lt;/sup&gt; + 546 =671&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width=&quot;181&quot;&gt;&lt;/td&gt;
&lt;td width=&quot;240&quot;&gt;Break…..&lt;/td&gt;
&lt;td width=&quot;217&quot;&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Challenge:&lt;/b&gt;&lt;br /&gt;
Find N , i.e.&lt;br /&gt;
N&lt;sup&gt;m&lt;/sup&gt; = WC1 and&lt;br /&gt;
(N+1)&lt;sup&gt;m&lt;/sup&gt; = IH&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEir68w7szgdy-YTdZBnzHdW62IJsxptHA9gnqdC9IxBsUb7jdexm2qkiBdzqO3oJGB5SGCWmyTiqsE7wT0V7EQ3sQ9lKLtGXTgUPJlR_tGYQ6lH_bNgskpkGrVp5oL6dPPdH6wZEUUgJyWH/s1600/Untitled.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEir68w7szgdy-YTdZBnzHdW62IJsxptHA9gnqdC9IxBsUb7jdexm2qkiBdzqO3oJGB5SGCWmyTiqsE7wT0V7EQ3sQ9lKLtGXTgUPJlR_tGYQ6lH_bNgskpkGrVp5oL6dPPdH6wZEUUgJyWH/s1600/Untitled.jpg&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&amp;nbsp;Code:&lt;br /&gt;
======&lt;br /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style=&quot;background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;&lt;span style=&quot;color: #cc0000; font-weight: bold;&quot;&gt;#include&amp;lt;iostream&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #cc0000; font-weight: bold;&quot;&gt;#include&amp;lt;cstdio&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #cc0000; font-weight: bold;&quot;&gt;#include&amp;lt;cmath&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #cc0000; font-weight: bold;&quot;&gt;#define ERROR 1e-8&lt;/span&gt;
&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;namespace&lt;/span&gt; std;

&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;main&lt;/span&gt;(){
  &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; H, num, m, N;
   &lt;span style=&quot;background-color: #e3d2d2; color: #a61717;&quot;&gt;#&lt;/span&gt;ifndef ONLINE_JUDGE
    freopen(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;a.in&quot;&lt;/span&gt;,&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;r&quot;&lt;/span&gt;,stdin);
   &lt;span style=&quot;background-color: #e3d2d2; color: #a61717;&quot;&gt;#&lt;/span&gt;endif


  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt;( scanf( &lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;%d%d&quot;&lt;/span&gt;, &amp;amp;H, &amp;amp;num ) != EOF &amp;amp;&amp;amp; !(H == &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt; &amp;amp;&amp;amp; num == &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;)){
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;( H == &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt; &amp;amp;&amp;amp; num == &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt; ){
      printf( &lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;0 1&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #0044dd;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;&lt;/span&gt;);
      &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;continue&lt;/span&gt;;
    }

    m = &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt;( H != (&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt;)(pow(pow(num,&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1.0&lt;/span&gt;/m)+&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1.0&lt;/span&gt;, m)+ERROR) ) m++;
    N = (&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt;)(pow(num,&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1.0&lt;/span&gt;/m)+ERROR);
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;( N != &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt; ) printf( &lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;%d %d&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #0044dd;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;&lt;/span&gt;, (&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;-num)/(&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;-N), (H-num)*N+H );
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt; printf( &lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;%d %d&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #0044dd;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;&lt;/span&gt;, m, (H-num)*N+H );
  }
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://rizoantoufiq.blogspot.com/feeds/7105439174334042430/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://rizoantoufiq.blogspot.com/2015/10/uva-107the-cat-in-hat.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1124334728283106452/posts/default/7105439174334042430'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1124334728283106452/posts/default/7105439174334042430'/><link rel='alternate' type='text/html' href='http://rizoantoufiq.blogspot.com/2015/10/uva-107the-cat-in-hat.html' title='UVa 107－The Cat in the Hat'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEir68w7szgdy-YTdZBnzHdW62IJsxptHA9gnqdC9IxBsUb7jdexm2qkiBdzqO3oJGB5SGCWmyTiqsE7wT0V7EQ3sQ9lKLtGXTgUPJlR_tGYQ6lH_bNgskpkGrVp5oL6dPPdH6wZEUUgJyWH/s72-c/Untitled.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1124334728283106452.post-6690977960339830274</id><published>2015-10-19T14:19:00.001-07:00</published><updated>2015-10-19T14:38:37.831-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Uva - Math"/><category scheme="http://www.blogger.com/atom/ns#" term="Uva Problems &amp; Solution"/><title type='text'>Uva 113 - Power of Cryptography</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Problem: &lt;a href=&quot;https://uva.onlinejudge.org/external/1/113.pdf&quot;&gt;113 - Power of Cryptography&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
Explain:&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj3hGKgkfvmCVuV6LyYOHIu0nRc7VRgymBTWiR39mNtUiSoqygcqNohOJywwWkkFYtGPq_z5h2zr2gJpTUr7AtIUciqJNUq7DqF8x8_iKOTD0GbRcBcmHdU1HQA1_f9RDoNGya5mWmx4xkO/s1600/render.png&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj3hGKgkfvmCVuV6LyYOHIu0nRc7VRgymBTWiR39mNtUiSoqygcqNohOJywwWkkFYtGPq_z5h2zr2gJpTUr7AtIUciqJNUq7DqF8x8_iKOTD0GbRcBcmHdU1HQA1_f9RDoNGya5mWmx4xkO/s1600/render.png&quot; /&gt;&lt;/a&gt; &lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now just use pow() function.&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style=&quot;background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;&lt;span style=&quot;color: #cc0000; font-weight: bold;&quot;&gt;#include&amp;lt;stdio.h&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #cc0000; font-weight: bold;&quot;&gt;#include&amp;lt;math.h&amp;gt;&lt;/span&gt;

&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;main&lt;/span&gt;(){
    &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;double&lt;/span&gt; n,p;
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt; (scanf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;%lf%lf&quot;&lt;/span&gt;, &amp;amp;n, &amp;amp;p) == &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;2&lt;/span&gt;)
        printf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;%.0lf&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #0044dd;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;&lt;/span&gt;, pow(p, &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt; / n));
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://rizoantoufiq.blogspot.com/feeds/6690977960339830274/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://rizoantoufiq.blogspot.com/2015/10/uva-113-power-of-cryptography.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1124334728283106452/posts/default/6690977960339830274'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1124334728283106452/posts/default/6690977960339830274'/><link rel='alternate' type='text/html' href='http://rizoantoufiq.blogspot.com/2015/10/uva-113-power-of-cryptography.html' title='Uva 113 - Power of Cryptography'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj3hGKgkfvmCVuV6LyYOHIu0nRc7VRgymBTWiR39mNtUiSoqygcqNohOJywwWkkFYtGPq_z5h2zr2gJpTUr7AtIUciqJNUq7DqF8x8_iKOTD0GbRcBcmHdU1HQA1_f9RDoNGya5mWmx4xkO/s72-c/render.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1124334728283106452.post-6519072251380139235</id><published>2015-10-19T13:44:00.001-07:00</published><updated>2015-10-19T13:44:57.339-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Uva - Binary Tree"/><category scheme="http://www.blogger.com/atom/ns#" term="Uva Problems &amp; Solution"/><title type='text'>Uva 112 - Tree Summing</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Problem:&amp;nbsp; &lt;a href=&quot;https://uva.onlinejudge.org/external/1/112.pdf&quot;&gt;112 - Tree Summing&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
Explain:&lt;br /&gt;
1. Parse Tree * &lt;br /&gt;
2. Binary Tree Traversal Algorithm (Recursive algorithm)&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style=&quot;background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;&lt;span style=&quot;color: #cc0000; font-weight: bold;&quot;&gt;#include &amp;lt;iostream&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #cc0000; font-weight: bold;&quot;&gt;#include &amp;lt;cstdio&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #cc0000; font-weight: bold;&quot;&gt;#include &amp;lt;cctype&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;namespace&lt;/span&gt; std;

&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;enum&lt;/span&gt; Token {
  LEFT_BRACKET, RIGHT_BRACKET,
  NUMBER
};

&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;enum&lt;/span&gt; Tree {
  NULLNODE, NODE
};

Token &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;getToken&lt;/span&gt;(&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; &amp;amp;token){
  &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; c = getchar();
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt;( c == &lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&#39; &#39;&lt;/span&gt; || c == &lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&#39;\n&#39;&lt;/span&gt; ){
    c = getchar();
  }

  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;( c == &lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&#39;(&#39;&lt;/span&gt; ) &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; LEFT_BRACKET;
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;( c == &lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&#39;)&#39;&lt;/span&gt; ) &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; RIGHT_BRACKET;

  &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; sign = &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;( c == &lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&#39;-&#39;&lt;/span&gt; ){
    sign = -&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;
    c = getchar();
  }

  token = &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt;( isdigit(c) ){
    token = token * &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;10&lt;/span&gt; + (c - &lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&#39;0&#39;&lt;/span&gt;);
    c = getchar();
  }

  token *= sign;
  ungetc(c, stdin);&lt;span style=&quot;color: #888888;&quot;&gt;/*pushes the char into stdin. now char is available in stdin.*/&lt;/span&gt;

  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; NUMBER;
}

Tree &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;parseTree&lt;/span&gt;(&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;bool&lt;/span&gt; &amp;amp;isMatched, &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; rootSum, &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; target){
  &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; token;

  getToken(token); &lt;span style=&quot;color: #888888;&quot;&gt;// &#39;(&#39;&lt;/span&gt;

  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;( getToken(token) == RIGHT_BRACKET ){
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; NULLNODE;
  }

  rootSum += token;
  Tree leftSubTree = parseTree( isMatched, rootSum, target );
  Tree rightSubTree = parseTree( isMatched, rootSum, target );

  getToken(token);

  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; (isMatched) &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; NODE;

  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;( leftSubTree == NULLNODE &amp;amp;&amp;amp; rightSubTree == NULLNODE ){
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;( rootSum == target ){
      isMatched = &lt;span style=&quot;color: #003388;&quot;&gt;true&lt;/span&gt;;
    }
  }

  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; NODE;

}

&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;main&lt;/span&gt;(){
  &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; target,c;
  &lt;span style=&quot;color: #888888;&quot;&gt;//freopen(&quot;sample.txt&quot;,&quot;r&quot;,stdin);&lt;/span&gt;
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt;( scanf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;%d&quot;&lt;/span&gt;, &amp;amp;target) != EOF ){
    &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;bool&lt;/span&gt; isMatched = &lt;span style=&quot;color: #003388;&quot;&gt;false&lt;/span&gt;;
    parseTree(isMatched, &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;, target);
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;( isMatched ){
      printf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;yes&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #0044dd;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;&lt;/span&gt;);
    }
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt; {
      printf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;no&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #0044dd;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;&lt;/span&gt;);
    }
  }
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://rizoantoufiq.blogspot.com/feeds/6519072251380139235/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://rizoantoufiq.blogspot.com/2015/10/uva-112-tree-summing.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1124334728283106452/posts/default/6519072251380139235'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1124334728283106452/posts/default/6519072251380139235'/><link rel='alternate' type='text/html' href='http://rizoantoufiq.blogspot.com/2015/10/uva-112-tree-summing.html' title='Uva 112 - Tree Summing'/><author><name>Unknown</name><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-1124334728283106452.post-462985381468964809</id><published>2015-10-19T10:15:00.001-07:00</published><updated>2015-10-19T10:15:46.624-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Algorithm Design and Analysis"/><category scheme="http://www.blogger.com/atom/ns#" term="Dynamic Programming"/><title type='text'>Longest Common Sub-Sequence</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
The &lt;b&gt;longest common subsequence&lt;/b&gt; (&lt;b&gt;LCS&lt;/b&gt;) &lt;b&gt;problem&lt;/b&gt; is very popular in computer science. This problem can be solved by dynamic algorithm. We can define this problem to find the longest subsequence common to all sequences in a set of sequences (often just two sequences).&lt;br /&gt;
&lt;br /&gt;
Let A&amp;nbsp; = {1,3,4,6,8}&lt;br /&gt;
B = {7, 3,2,4,6,10,8}&lt;br /&gt;
&lt;br /&gt;
LCS (A,B) = {3,4,6,8} , Length = 4;&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
Application:&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt; bioinformatics&amp;nbsp;&lt;/li&gt;
&lt;li&gt;revision control systems (the management of changes to documents, computer programs, large web sites, and other collections of information)&lt;/li&gt;
&lt;/ul&gt;
How can we solve this problem?&lt;br /&gt;
if we have two sequence of length n and m, the time complexity of the dynamic Program approach is O(nm).&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhCTvd7lb9eVGvDcu8rvsQ9eQnUzOiDgRZ-B9RPUVYg87_bLfa5Jyv9DeAQWxOaWGsOTsrOO_DUIKaygxI1kvdZ6oLnZ4PTDjfNgJLyHbQEZ_P0g4avubdEc0OLdwGlMyX5d7xfdoyEp9gW/s1600/1.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;182&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhCTvd7lb9eVGvDcu8rvsQ9eQnUzOiDgRZ-B9RPUVYg87_bLfa5Jyv9DeAQWxOaWGsOTsrOO_DUIKaygxI1kvdZ6oLnZ4PTDjfNgJLyHbQEZ_P0g4avubdEc0OLdwGlMyX5d7xfdoyEp9gW/s320/1.jpg&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
Here, A = {4,7,6,5,9}&lt;br /&gt;
B = {2,3,4,6,5,9}&lt;br /&gt;
2 not belongs to A&lt;br /&gt;
3 also not belongs to A&lt;br /&gt;
4 belong to A&amp;nbsp; 0+1 = 1&lt;br /&gt;
4!=7 = &amp;gt;&amp;nbsp; max (0,1) = 1,&lt;br /&gt;
.&lt;br /&gt;
.&lt;br /&gt;
.&lt;br /&gt;
finally we get length of LCS i.e. 4&lt;br /&gt;
&lt;br /&gt;
we can print it as following:&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjrTKxGkPf5sswApzzEPbk4Pspxe-_5Qr6fliZgNxy2R2GF6bj08tE7U4Um4Ale-rNh3JULYqFdavy_c3-_1u8a6UkPnwmS8m02xS4NIS_io9VSLADH1UZkCbh4q1Qsrn9Hh_qBwdfkBqrR/s1600/2.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;181&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjrTKxGkPf5sswApzzEPbk4Pspxe-_5Qr6fliZgNxy2R2GF6bj08tE7U4Um4Ale-rNh3JULYqFdavy_c3-_1u8a6UkPnwmS8m02xS4NIS_io9VSLADH1UZkCbh4q1Qsrn9Hh_qBwdfkBqrR/s320/2.jpg&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style=&quot;background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;&lt;span style=&quot;color: #cc0000; font-weight: bold;&quot;&gt;#include&amp;lt;stdio.h&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #cc0000; font-weight: bold;&quot;&gt;#define max 100&lt;/span&gt;
&lt;span style=&quot;color: #cc0000; font-weight: bold;&quot;&gt;#define MAX(a,b) a&amp;lt;b?b:a&lt;/span&gt;
&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; c[max][max];&lt;span style=&quot;color: #888888;&quot;&gt;//table&lt;/span&gt;
&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; R[max],r=&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;&lt;span style=&quot;color: #888888;&quot;&gt;//R=result,r=lenth&lt;/span&gt;

&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;LCS&lt;/span&gt;(&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; *X,&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; *Y,&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; Lx,&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; Ly){
    &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; i,j;
 &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt;(i=&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;i&amp;lt;Lx;i++)
  c[i][&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;]=&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
 &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt;(j=&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;j&amp;lt;Ly;j++){
  c[&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;][j]=&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
  printf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;%d&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #0044dd;&quot;&gt;\t&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;&lt;/span&gt;,c[&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;][j]);
 }
    printf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #0044dd;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;&lt;/span&gt;);
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt;(i=&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;i&amp;lt;Lx;i++){
        printf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;%d&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #0044dd;&quot;&gt;\t&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;&lt;/span&gt;,c[i][&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;]);
        &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt;(j=&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;j&amp;lt;Ly;j++){
     &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;(X[i]==Y[j]){
      c[i][j] = c[i-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;][j-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;]+&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;
      printf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;%d&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #0044dd;&quot;&gt;\t&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;&lt;/span&gt;,c[i][j]);
     }
     &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt;{
      c[i][j]= MAX(c[i][j-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;],c[i-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;][j]);
      printf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;%d&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #0044dd;&quot;&gt;\t&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;&lt;/span&gt;,c[i][j]);
     }
  }
  printf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #0044dd;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;&lt;/span&gt;);
 }
}

&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;printLCS&lt;/span&gt;(&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; *X,&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; *Y,&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; i,&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; j){
   &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;(i==&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;||j==&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;)
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
   &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;(X[i]==Y[j]){
    &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; m= printLCS(X,Y,i-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;,j-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;);
    printf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;%d &quot;&lt;/span&gt;,X[i]);
    R[r]=X[i];
    r++;
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; m;
   }
   &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt;{
     &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;(c[i][j-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;]&amp;gt;c[i-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;][j])
   &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; printLCS(X,Y,i,j-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;);
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt;
   &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; printLCS(X,Y,i-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;,j);
   }
   &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
}

&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;main&lt;/span&gt;(){
 &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; X[]={&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;,&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;2&lt;/span&gt;,&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;3&lt;/span&gt;,&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;4&lt;/span&gt;,&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;6&lt;/span&gt;,&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;5&lt;/span&gt;,&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;9&lt;/span&gt;};
 &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; Y[] = {&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;,&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;4&lt;/span&gt;,&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;7&lt;/span&gt;,&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;6&lt;/span&gt;,&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;5&lt;/span&gt;,&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;9&lt;/span&gt;};
    &lt;span style=&quot;color: #888888;&quot;&gt;/*freopen(&quot;LCSout.txt&quot;,&quot;w&quot;,stdout);*/&lt;/span&gt;

 LCS(X,Y,&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;7&lt;/span&gt;,&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;6&lt;/span&gt;);

    printf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #0044dd;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;Print LCS: &quot;&lt;/span&gt;);
 printLCS(X,Y,&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;7&lt;/span&gt;,&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;6&lt;/span&gt;);

 printf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #0044dd;&quot;&gt;\n\n&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;Result: &lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #0044dd;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;&lt;/span&gt;);
 printf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #0044dd;&quot;&gt;\t&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;Lenth: %d&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #0044dd;&quot;&gt;\n\t&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;&lt;/span&gt;,r);
 &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt;(&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; i=&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;i&amp;lt;r;i++)
  printf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;%d &quot;&lt;/span&gt;,R[i]);
    printf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #0044dd;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;&lt;/span&gt;);
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://rizoantoufiq.blogspot.com/feeds/462985381468964809/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://rizoantoufiq.blogspot.com/2015/10/longest-common-sub-sequence.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1124334728283106452/posts/default/462985381468964809'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1124334728283106452/posts/default/462985381468964809'/><link rel='alternate' type='text/html' href='http://rizoantoufiq.blogspot.com/2015/10/longest-common-sub-sequence.html' title='Longest Common Sub-Sequence'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhCTvd7lb9eVGvDcu8rvsQ9eQnUzOiDgRZ-B9RPUVYg87_bLfa5Jyv9DeAQWxOaWGsOTsrOO_DUIKaygxI1kvdZ6oLnZ4PTDjfNgJLyHbQEZ_P0g4avubdEc0OLdwGlMyX5d7xfdoyEp9gW/s72-c/1.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1124334728283106452.post-6814405182498088569</id><published>2015-10-19T09:17:00.000-07:00</published><updated>2015-10-19T09:17:50.125-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Uva - Dynamic Programming"/><category scheme="http://www.blogger.com/atom/ns#" term="Uva - LCS"/><category scheme="http://www.blogger.com/atom/ns#" term="Uva Problems &amp; Solution"/><title type='text'>Uva 111 - History Grading</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Problem: &lt;a href=&quot;https://uva.onlinejudge.org/external/1/111.pdf&quot;&gt;Uva 111 History Grading&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&amp;nbsp;Explain: LCS Problem , Complexity O(n^2)&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuNb_P6Ipp8vgbIOp7A5r8BYMdoTNMw2s23R5t0dBFEg757XQyQiKPWDgMXU_yhFXTrtEWHu6ccHfe6BRLVj2s8v8bh2AmBHKwkdkA3VVX72ceOCv8y_ZvA85R42Mc167v3c5IliHQ8ITE/s1600/1.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;182&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuNb_P6Ipp8vgbIOp7A5r8BYMdoTNMw2s23R5t0dBFEg757XQyQiKPWDgMXU_yhFXTrtEWHu6ccHfe6BRLVj2s8v8bh2AmBHKwkdkA3VVX72ceOCv8y_ZvA85R42Mc167v3c5IliHQ8ITE/s320/1.jpg&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj6q5OnfgNpAV_olZrdtkYpo_Yxkjs4F4OrlyjoGrGjryUzrTg4Zroh8W0fkjAgW_3GQ3OajQP2JPjw5Xg8Grp0NgSNSgp6pbYgHUV5cpfkCQU5A5EJ4Z3p7_nYliSzWY89xZ2pNxPXH8cj/s1600/2.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;181&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj6q5OnfgNpAV_olZrdtkYpo_Yxkjs4F4OrlyjoGrGjryUzrTg4Zroh8W0fkjAgW_3GQ3OajQP2JPjw5Xg8Grp0NgSNSgp6pbYgHUV5cpfkCQU5A5EJ4Z3p7_nYliSzWY89xZ2pNxPXH8cj/s320/2.jpg&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgsJM5BlOLr-KXXeFfveyp1XZzOw_-jovy4iFhyphenhyphenrsPjspUISQr3LykuPdgHjMENjf7ohsiEf5iWe8F4pvPWr3M3nGP-nY7wqYaoqOtEB5nFO-GrbChKkECrkakPKOT5uoyt8zmqLFl-gp7-/s1600/Untitled.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;255&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgsJM5BlOLr-KXXeFfveyp1XZzOw_-jovy4iFhyphenhyphenrsPjspUISQr3LykuPdgHjMENjf7ohsiEf5iWe8F4pvPWr3M3nGP-nY7wqYaoqOtEB5nFO-GrbChKkECrkakPKOT5uoyt8zmqLFl-gp7-/s320/Untitled.jpg&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
code:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style=&quot;background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;&lt;span style=&quot;color: #cc0000; font-weight: bold;&quot;&gt;#include&amp;lt;stdio.h&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #cc0000; font-weight: bold;&quot;&gt;#define max 25&lt;/span&gt;
&lt;span style=&quot;color: #cc0000; font-weight: bold;&quot;&gt;#define MAX(a,b) a&amp;lt;b?b:a&lt;/span&gt;

&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;main&lt;/span&gt;(){
 freopen(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;a.in&quot;&lt;/span&gt;,&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;r&quot;&lt;/span&gt;,stdin);
 &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; c[max][max]={&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;};&lt;span style=&quot;color: #888888;&quot;&gt;/*table*/&lt;/span&gt;
 &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; X[max];
 &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; Y[max];
 &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; n,m,i,j,a;

 scanf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;%d&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #0044dd;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;&lt;/span&gt;,&amp;amp;n);
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt;(i=&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;i&amp;lt;=n;i++){
     scanf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;%d&quot;&lt;/span&gt;,&amp;amp;m);
        X[m]=i;
 }
 &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt;(&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;){
     a= scanf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;%d&quot;&lt;/span&gt;,&amp;amp;m);
     &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; (a!=&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;) &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;break&lt;/span&gt;;
     Y[m] = &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt;(i=&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;2&lt;/span&gt;;i&amp;lt;=n;i++){
     scanf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;%d&quot;&lt;/span&gt;,&amp;amp;m);
     Y[m]=i;
  }
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt;(i=&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;i&amp;lt;=n;i++){
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt;(j=&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;j&amp;lt;=n;j++){
      &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;(X[i]==Y[j])
      c[i][j] = c[i-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;][j-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;]+&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;
      &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt;
     c[i][j]= MAX(c[i][j-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;],c[i-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;][j]);
    }
     }
  printf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;%d&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #0044dd;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;&lt;/span&gt;,c[n][n]);
 }
 &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://rizoantoufiq.blogspot.com/feeds/6814405182498088569/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://rizoantoufiq.blogspot.com/2015/10/uva-111-history-grading.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1124334728283106452/posts/default/6814405182498088569'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1124334728283106452/posts/default/6814405182498088569'/><link rel='alternate' type='text/html' href='http://rizoantoufiq.blogspot.com/2015/10/uva-111-history-grading.html' title='Uva 111 - History Grading'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuNb_P6Ipp8vgbIOp7A5r8BYMdoTNMw2s23R5t0dBFEg757XQyQiKPWDgMXU_yhFXTrtEWHu6ccHfe6BRLVj2s8v8bh2AmBHKwkdkA3VVX72ceOCv8y_ZvA85R42Mc167v3c5IliHQ8ITE/s72-c/1.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1124334728283106452.post-1241401131674068796</id><published>2015-10-12T15:31:00.003-07:00</published><updated>2015-10-12T15:31:31.812-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Discrete Mathematics"/><title type='text'>Linear Congruences</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
A congruence of the&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjFbn1Q2aMUf15Y84RIGuVFGiOzc9f9hOYYwFzR65iqna7YF25bOMXiGHVgHqRTPVFyHy3qACFrqIc0uxZ7Izzaz1eXu36BzfIGg0-RZWQ0y7rE0gePvLgDKnESQ1WbA9f13_j4N4u0CYLL/s1600/render.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjFbn1Q2aMUf15Y84RIGuVFGiOzc9f9hOYYwFzR65iqna7YF25bOMXiGHVgHqRTPVFyHy3qACFrqIc0uxZ7Izzaz1eXu36BzfIGg0-RZWQ0y7rE0gePvLgDKnESQ1WbA9f13_j4N4u0CYLL/s1600/render.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
where m is a positive integer, a and b are integer and x is a variable, is called a linear congruence.&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;
কেমন করে এটার সমাধান করব? অর্থাৎ কেমন করে আমরা x এর মান বের করব?&lt;br /&gt;
একটা equation হচ্ছে,&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&amp;nbsp;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjxPclbi5RheEPz6CF23xEnjfCzSi9-Uo1FBQfo_XGaoJLwjN4lnXIVeK2eLUzOVLn6b1iTrpJQzuCJUaq9leCjb60AvI23gZRe0-v1Sh36A78ZUYh29dBzQNUQMTl96bfam33OAjGb1R76/s1600/render.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjxPclbi5RheEPz6CF23xEnjfCzSi9-Uo1FBQfo_XGaoJLwjN4lnXIVeK2eLUzOVLn6b1iTrpJQzuCJUaq9leCjb60AvI23gZRe0-v1Sh36A78ZUYh29dBzQNUQMTl96bfam33OAjGb1R76/s1600/render.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiP60Xfr87Q-BRm_jD8akCsNWzh96rkWpt1SAlovmU5BwR082WYXc_YEyMSePdm1gjUBKKOZOnt-KN79V-hp4YjrmC5-93Yf0Cat05dV0MjD2gElxEf6yNv9Pju-t1-E7lz9vCJxN4KxGqS/s1600/Untitled.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;br /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgmHxjkT3Z7BkTgKSogokktYUz7RLWeml6_CTfbj8NVixFsBcwP7bSo0i3mNOtBMHbqhU0x8W2C0XU7z0MEoWUQvyPi1Y97mOkgwQLyBB8VVpr64M44nHllEgPmiX43iBtIx-cTTtQ6gJR9/s1600/1.jpg&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;536&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgmHxjkT3Z7BkTgKSogokktYUz7RLWeml6_CTfbj8NVixFsBcwP7bSo0i3mNOtBMHbqhU0x8W2C0XU7z0MEoWUQvyPi1Y97mOkgwQLyBB8VVpr64M44nHllEgPmiX43iBtIx-cTTtQ6gJR9/s640/1.jpg&quot; width=&quot;500&quot; /&gt;&lt;/a&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiKG72boxi7FcHhCL3Tueb-6Hqo-nrZRKdW4AfPzwSp8POzqWHyXJdokANbJXgao3aDDq1hdAUYyaaVpOmJabUHWYv-MMhhcbU6cxP5Oan9Vz-XEHqiQv2YdmCzLZaUSPN_gxDYW5viUm_y/s1600/Untitled.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;br /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://rizoantoufiq.blogspot.com/feeds/1241401131674068796/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://rizoantoufiq.blogspot.com/2015/10/linear-congruences.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1124334728283106452/posts/default/1241401131674068796'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1124334728283106452/posts/default/1241401131674068796'/><link rel='alternate' type='text/html' href='http://rizoantoufiq.blogspot.com/2015/10/linear-congruences.html' title='Linear Congruences'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjFbn1Q2aMUf15Y84RIGuVFGiOzc9f9hOYYwFzR65iqna7YF25bOMXiGHVgHqRTPVFyHy3qACFrqIc0uxZ7Izzaz1eXu36BzfIGg0-RZWQ0y7rE0gePvLgDKnESQ1WbA9f13_j4N4u0CYLL/s72-c/render.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1124334728283106452.post-2861613712217581975</id><published>2015-10-11T10:26:00.002-07:00</published><updated>2015-10-11T10:36:19.917-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Uva - LIS"/><category scheme="http://www.blogger.com/atom/ns#" term="Uva Problems &amp; Solution"/><title type='text'>Uva 497 - Strategic Defense Initiative</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Problem: &lt;a href=&quot;https://uva.onlinejudge.org/external/4/497.pdf&quot; target=&quot;_blank&quot;&gt;497 - Strategic Defense Initiative&lt;/a&gt;&lt;br /&gt;
Description:&amp;nbsp; &lt;a href=&quot;http://rizoantoufiq.blogspot.com/2015/10/longest-increasing-subsequence.html&quot; target=&quot;_blank&quot;&gt;Longest Increasing Subsequence&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style=&quot;background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;&lt;span style=&quot;color: #888888;&quot;&gt;/*&lt;/span&gt;
&lt;span style=&quot;color: #888888;&quot;&gt;Algorithm: Longest Increasing Seq.&lt;/span&gt;
&lt;span style=&quot;color: #888888;&quot;&gt;Complexity: nlgn&lt;/span&gt;
&lt;span style=&quot;color: #888888;&quot;&gt;*/&lt;/span&gt;
&lt;span style=&quot;color: #cc0000; font-weight: bold;&quot;&gt;#include&amp;lt;stdio.h&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #cc0000; font-weight: bold;&quot;&gt;#include&amp;lt;limits.h&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #cc0000; font-weight: bold;&quot;&gt;#include&amp;lt;string.h&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #cc0000; font-weight: bold;&quot;&gt;#include&amp;lt;stdlib.h&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #cc0000; font-weight: bold;&quot;&gt;#define S 100000&lt;/span&gt;

&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; R[S]={&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;};&lt;span style=&quot;color: #888888;&quot;&gt;/*Generated Table*/&lt;/span&gt;
&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; A[S] = {&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;};&lt;span style=&quot;color: #888888;&quot;&gt;/*Input data*/&lt;/span&gt;
&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; Indx[S]= {&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;}; &lt;span style=&quot;color: #888888;&quot;&gt;/*Index of the Table Element*/&lt;/span&gt;
&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; Pre[S]={&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;}; &lt;span style=&quot;color: #888888;&quot;&gt;/*Previous Element*/&lt;/span&gt;

&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;Print&lt;/span&gt;(&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; i){
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; ( i == &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;) &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt;;
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt; {
        Print(Pre[i]);
        printf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;%d&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #0044dd;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;&lt;/span&gt;,A[i]);
    }
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt;;
}
&lt;span style=&quot;color: #888888;&quot;&gt;/*Complexity lg2^N*/&lt;/span&gt;
&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;BS&lt;/span&gt;(&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; v,&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; s,&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; e){
    &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; mid=&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt;(s&amp;lt;=e){
         mid = s+((e-s)/&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;2&lt;/span&gt;);
         &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; (v&amp;lt;=R[mid]){
              &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;(R[mid-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;]&amp;lt;v) &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;break&lt;/span&gt;;
              e = mid-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;
         }
         &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; (v&amp;gt;R[mid]){
            &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;(R[mid+&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;]&amp;gt;=v){mid= mid+&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;break&lt;/span&gt;;}
            s = mid+&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;
         }
    }
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; mid;
}

&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;main&lt;/span&gt;(){
    &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; i,v=&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
    &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; rc;
    &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; s,e,mid;&lt;span style=&quot;color: #888888;&quot;&gt;/*Binary Search*/&lt;/span&gt;
    &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; N,C=&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
    &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;char&lt;/span&gt; ch[&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;10&lt;/span&gt;];

    &lt;span style=&quot;color: #888888;&quot;&gt;/*freopen(&quot;LIS.in&quot;,&quot;r&quot;,stdin);*/&lt;/span&gt;

    A[&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;]=R[&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;] = INT_MIN;
    Indx[&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;] = &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;

    scanf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;%d&quot;&lt;/span&gt;,&amp;amp;N);
    getchar();
    gets (ch);
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt;(&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;){
       rc=&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;
       i=&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;
       &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt;(gets(ch) &amp;amp;&amp;amp; strlen(ch)){
           A[i] = atoi(ch);
           v = A[i];
           &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;(v&amp;gt;R[rc-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;]){
              R[rc] = v;
              Indx[rc] = i;
              Pre[i] = Indx[rc-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;];
              rc++;
           }
           &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;(v&amp;lt;R[rc-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;]){&lt;span style=&quot;color: #888888;&quot;&gt;/*Binary Search*/&lt;/span&gt;
              mid = BS(v,&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;,rc-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;);
              R[mid] = v;
              Indx[mid]= i;
              Pre[i] = Indx[mid-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;];
            }
            i++;
        }
        printf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;Max hits: %d&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #0044dd;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;&lt;/span&gt;,rc-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;);
        i = Indx[rc-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;];
        Print(i);
        C++;
        &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;(C==N) &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;break&lt;/span&gt;;
        printf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #0044dd;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;&lt;/span&gt;);

        &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt;(i=&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;i&amp;lt;=N;i++) Pre[i]=&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;,Indx[i]=&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;,R[i]=&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
    }
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://rizoantoufiq.blogspot.com/feeds/2861613712217581975/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://rizoantoufiq.blogspot.com/2015/10/uva-497-strategic-defense-initiative.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1124334728283106452/posts/default/2861613712217581975'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1124334728283106452/posts/default/2861613712217581975'/><link rel='alternate' type='text/html' href='http://rizoantoufiq.blogspot.com/2015/10/uva-497-strategic-defense-initiative.html' title='Uva 497 - Strategic Defense Initiative'/><author><name>Unknown</name><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-1124334728283106452.post-5103070439115620514</id><published>2015-10-11T09:12:00.001-07:00</published><updated>2015-10-11T10:36:27.840-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Uva - LDS"/><category scheme="http://www.blogger.com/atom/ns#" term="Uva Problems &amp; Solution"/><title type='text'>Uva - 231 - Testing the CATCHER</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Problem: &lt;a href=&quot;https://uva.onlinejudge.org/external/2/231.pdf&quot; target=&quot;_blank&quot;&gt;231 - Testing the CATCHER&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
Explain: &lt;a href=&quot;http://rizoantoufiq.blogspot.com/2015/10/longest-decreasing-subsequence.html&quot; target=&quot;_blank&quot;&gt;Longest Decreasing Subsequence&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;Code:&lt;br /&gt;
-------&lt;br /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style=&quot;background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;&lt;span style=&quot;color: #888888;&quot;&gt;/*&lt;/span&gt;
&lt;span style=&quot;color: #888888;&quot;&gt;Algorithm: Longest Decreasing Seq.&lt;/span&gt;
&lt;span style=&quot;color: #888888;&quot;&gt;Complexity: nlgn&lt;/span&gt;
&lt;span style=&quot;color: #888888;&quot;&gt;*/&lt;/span&gt;
&lt;span style=&quot;color: #cc0000; font-weight: bold;&quot;&gt;#include&amp;lt;stdio.h&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #cc0000; font-weight: bold;&quot;&gt;#include&amp;lt;limits.h&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #cc0000; font-weight: bold;&quot;&gt;#define S 1000000&lt;/span&gt;

&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; R[S]={&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;};&lt;span style=&quot;color: #888888;&quot;&gt;/*Generated Table*/&lt;/span&gt;
&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; A[S] = {&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;};&lt;span style=&quot;color: #888888;&quot;&gt;/*Input data*/&lt;/span&gt;
&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; Indx[S]= {&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;}; &lt;span style=&quot;color: #888888;&quot;&gt;/*Index of the Table Element*/&lt;/span&gt;
&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; Pre[S]={&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;}; &lt;span style=&quot;color: #888888;&quot;&gt;/*Previous Element*/&lt;/span&gt;

&lt;span style=&quot;color: #888888;&quot;&gt;/*Complexity lg2^N*/&lt;/span&gt;
&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;BS&lt;/span&gt;(&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; v,&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; s,&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; e){
    &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; mid=&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt;(s&amp;lt;=e){
         mid = s+((e-s)/&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;2&lt;/span&gt;);
         &lt;span style=&quot;color: #888888;&quot;&gt;/*Remove small*/&lt;/span&gt;
         &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; (R[mid]&amp;lt;=v){
              &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; (v&amp;lt;R[mid-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;]|| v == R[mid-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;])&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;break&lt;/span&gt;;
              e = mid-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;
         }
         &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; (v&amp;lt;R[mid]){
            &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;(R[mid+&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;]&amp;lt;v){mid=mid+&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;break&lt;/span&gt;;}
            s = mid+&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;
         }
    }
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; mid;
}


&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;main&lt;/span&gt;(){
    &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; i,v=&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;,j;
    &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; rc;
    &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; s,e,mid;&lt;span style=&quot;color: #888888;&quot;&gt;/*Binary Search*/&lt;/span&gt;
    &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; N=&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;,flag;

    &lt;span style=&quot;color: #888888;&quot;&gt;/*freopen(&quot;a.txt&quot;,&quot;r&quot;,stdin);*/&lt;/span&gt;

    A[&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;]=R[&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;] = INT_MAX;
    Indx[&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;] = &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
    flag = &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt;(&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;){
       rc=&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;
       &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt;(i=&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;;i++){
           scanf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;%d&quot;&lt;/span&gt;,&amp;amp;A[i]);
           &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; ( A[i] == -&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;){ &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; ( i == &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;) flag = &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;break&lt;/span&gt;;}
           v = A[i];
           &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;(v&amp;lt;=R[rc-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;]){
              R[rc] = v;
              Indx[rc] = i;
              Pre[i] = Indx[rc-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;];
              rc++;
           }
           &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;(v&amp;gt;R[rc-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;]){&lt;span style=&quot;color: #888888;&quot;&gt;/*Binary Search*/&lt;/span&gt;
              mid = BS(v,&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;,rc-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;);
              R[mid] = v;
              Indx[mid]= i;
              Pre[i] = Indx[mid-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;];
            }
        }
        &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;(flag==&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;) &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;break&lt;/span&gt;;
        &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; (N &amp;gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;) printf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #0044dd;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;&lt;/span&gt;);
        printf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;Test #%d:&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #0044dd;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;  maximum possible interceptions: %d&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #0044dd;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;&lt;/span&gt;,N++,rc-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;);
        &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt;(i=&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;i&amp;lt;=N;i++) Pre[i]=&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;,Indx[i]=&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;,R[i]=&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
    }
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://rizoantoufiq.blogspot.com/feeds/5103070439115620514/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://rizoantoufiq.blogspot.com/2015/10/uva-231-testing-catcher.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1124334728283106452/posts/default/5103070439115620514'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1124334728283106452/posts/default/5103070439115620514'/><link rel='alternate' type='text/html' href='http://rizoantoufiq.blogspot.com/2015/10/uva-231-testing-catcher.html' title='Uva - 231 - Testing the CATCHER'/><author><name>Unknown</name><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-1124334728283106452.post-3154355944261342209</id><published>2015-10-09T12:38:00.000-07:00</published><updated>2015-10-09T12:45:55.798-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Uva - LIS"/><category scheme="http://www.blogger.com/atom/ns#" term="Uva Problems &amp; Solution"/><title type='text'>Uva 481 - What Goes Up</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;b&gt;Problem :&lt;/b&gt;&amp;nbsp; 481 - &lt;a href=&quot;https://uva.onlinejudge.org/external/4/481.pdf&quot; target=&quot;_blank&quot;&gt;What Goes Up&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Description:&lt;/b&gt;&lt;br /&gt;
Find out the &lt;a href=&quot;http://rizoantoufiq.blogspot.com/2015/10/longest-increasing-subsequence.html&quot; target=&quot;_blank&quot;&gt;Longest Increasing Sequences&lt;/a&gt;. Complexity: O(nlgn)&lt;br /&gt;
&lt;pre&gt;Gievn &lt;/pre&gt;
&lt;pre&gt;-7
10
9
2
3
8
8
1&lt;/pre&gt;
&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre&gt;-7 : -i -7&amp;nbsp;&lt;/pre&gt;
&lt;pre&gt;10: -i -7 10&lt;/pre&gt;
&lt;pre&gt;9 : -i -7 9&lt;/pre&gt;
&lt;pre&gt;2: -i -7 2&lt;/pre&gt;
&lt;pre&gt;3: -i -7 2 3&lt;/pre&gt;
&lt;pre&gt;8: -i -7 2 3 8&lt;/pre&gt;
&lt;pre&gt;8: -i -7 2 3 8&lt;/pre&gt;
&lt;pre&gt;1: -i -7 1 3 8&lt;/pre&gt;
&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre&gt;Length: 4&amp;nbsp;&lt;/pre&gt;
&lt;pre&gt;and Seq: -7 2 3 8&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;hr /&gt;
&lt;/pre&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style=&quot;background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;&lt;span style=&quot;color: #cc0000; font-weight: bold;&quot;&gt;#include&amp;lt;stdio.h&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #cc0000; font-weight: bold;&quot;&gt;#include&amp;lt;limits.h&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #cc0000; font-weight: bold;&quot;&gt;#define S 100000&lt;/span&gt;

&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; R[S]={&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;};&lt;span style=&quot;color: #888888;&quot;&gt;/*Generated Table*/&lt;/span&gt;
&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; A[S] = {&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;};&lt;span style=&quot;color: #888888;&quot;&gt;/*Input data*/&lt;/span&gt;
&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; Indx[S]= {&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;}; &lt;span style=&quot;color: #888888;&quot;&gt;/*Index of the Table Element*/&lt;/span&gt;
&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; Pre[S]={&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;}; &lt;span style=&quot;color: #888888;&quot;&gt;/*Previous Element*/&lt;/span&gt;

&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;Print&lt;/span&gt;(&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; i){
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; ( i == &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;) &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt;;
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt; {
        Print(Pre[i]);
        printf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;%d&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #0044dd;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;&lt;/span&gt;,A[i]);
    }
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt;;
}
&lt;span style=&quot;color: #888888;&quot;&gt;/*Complexity lg2^N*/&lt;/span&gt;
&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;BS&lt;/span&gt;(&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; v,&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; s,&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; e){
    &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; mid=&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt;(s&amp;lt;=e){
         mid = s+((e-s)/&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;2&lt;/span&gt;);
         &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; (v&amp;lt;=R[mid]){
              &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;(R[mid-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;]&amp;lt;v) &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;break&lt;/span&gt;;
              e = mid-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;
         }
         &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; (v&amp;gt;R[mid]){
            &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;(R[mid+&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;]&amp;gt;=v){mid= mid+&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;break&lt;/span&gt;;}
            s = mid+&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;
         }
    }
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; mid;
}

&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;main&lt;/span&gt;(){
    &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; i,v=&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
    &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; rc;
    &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; s,e,mid;&lt;span style=&quot;color: #888888;&quot;&gt;/*Binary Search*/&lt;/span&gt;

    &lt;span style=&quot;color: #888888;&quot;&gt;/*freopen(&quot;481.in&quot;,&quot;r&quot;,stdin);*/&lt;/span&gt;

    A[&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;]=R[&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;] = INT_MIN;
    Indx[&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;] = &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
    i=&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;,rc=&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;

    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt;(scanf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;%d&quot;&lt;/span&gt;, &amp;amp;A[i]) == &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;){
       v = A[i];
       &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;(v&amp;gt;R[rc-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;]){
           R[rc] = v;
           Indx[rc] = i;
           Pre[i] = Indx[rc-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;];
           rc++;
       }
       &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;(v&amp;lt;R[rc-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;]){&lt;span style=&quot;color: #888888;&quot;&gt;/*Binary Search*/&lt;/span&gt;
           mid = BS(v,&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;,rc-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;);
           R[mid] = v;
           Indx[mid]= i;
           Pre[i] = Indx[mid-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;];
        }
        i++;
    }

    printf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;%d&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #0044dd;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #0044dd;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;&lt;/span&gt;,rc-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;);
    i = Indx[rc-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;];
    Print(i);
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://rizoantoufiq.blogspot.com/feeds/3154355944261342209/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://rizoantoufiq.blogspot.com/2015/10/uva-481-what-goes-up.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1124334728283106452/posts/default/3154355944261342209'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1124334728283106452/posts/default/3154355944261342209'/><link rel='alternate' type='text/html' href='http://rizoantoufiq.blogspot.com/2015/10/uva-481-what-goes-up.html' title='Uva 481 - What Goes Up'/><author><name>Unknown</name><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-1124334728283106452.post-884998246609627319</id><published>2015-10-09T12:26:00.003-07:00</published><updated>2015-10-09T12:38:31.087-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Algorithm Design and Analysis"/><category scheme="http://www.blogger.com/atom/ns#" term="Dynamic Programming"/><title type='text'>Longest Decreasing Subsequence</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Read : &lt;a href=&quot;http://rizoantoufiq.blogspot.com/2015/10/longest-increasing-subsequence.html&quot; target=&quot;_blank&quot;&gt;Longest Increasing Subsequence&lt;/a&gt;&lt;br /&gt;
&lt;b&gt;Dynamic Programming:&lt;/b&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgAS2Qg1Z7OYp3zsHQSyPfg86P9s3UW2xH-Ib4JH_OA7bkzkJ1vzm2Hu_qAoZ2_UiaRgDKQzRJb6BGjIpwWUlrW0B1U6NimPh7se_l9ttFpeMRFTNhkQQVcvmsggxkdLGoJLinAMHvcQqCV/s1600/1.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;228&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgAS2Qg1Z7OYp3zsHQSyPfg86P9s3UW2xH-Ib4JH_OA7bkzkJ1vzm2Hu_qAoZ2_UiaRgDKQzRJb6BGjIpwWUlrW0B1U6NimPh7se_l9ttFpeMRFTNhkQQVcvmsggxkdLGoJLinAMHvcQqCV/s320/1.jpg&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;
Let Given,&lt;br /&gt;
A = { 0 8 4 12 2 10 6 14 1 9 5 13 3 11 7 15 }&lt;br /&gt;
&amp;nbsp;0 :&amp;nbsp;&amp;nbsp; 0&lt;br /&gt;
Indx: [1]&lt;br /&gt;
Pre:&amp;nbsp; [0]&amp;nbsp; /*Note: Parent = NULL , so it is a root node*/&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;8 :&amp;nbsp;&amp;nbsp;&amp;nbsp; 8&lt;br /&gt;
Indx: [2]&lt;br /&gt;
Pre:&amp;nbsp; [0]&lt;br /&gt;
&lt;br /&gt;
4 :&amp;nbsp;&amp;nbsp;&amp;nbsp; 8&amp;nbsp;&amp;nbsp; 4&lt;br /&gt;
Indx: [2] [3]&lt;br /&gt;
Pre:&amp;nbsp; .....[2]&lt;br /&gt;
&lt;br /&gt;
12 :&amp;nbsp; 12&amp;nbsp; 4&lt;br /&gt;
Indx: [4] [3]&lt;br /&gt;
Pre:..[0]&lt;br /&gt;
&lt;br /&gt;
2 :&amp;nbsp;&amp;nbsp;&amp;nbsp; 12&amp;nbsp; 4&amp;nbsp; 2&lt;br /&gt;
Indx: [4] [3] [5]&lt;br /&gt;
Pre: ...........[3]&lt;br /&gt;
&lt;br /&gt;
10 :&amp;nbsp; 12&amp;nbsp; 10&amp;nbsp; 2&lt;br /&gt;
Indx: [4] [6] [5]&lt;br /&gt;
Pre: ....&amp;nbsp; [4]&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;6 :&amp;nbsp;&amp;nbsp; 12 10 6&lt;br /&gt;
Indx: [4] [6] [7]&lt;br /&gt;
Pre: ............[6]&lt;br /&gt;
&lt;br /&gt;
14 :&amp;nbsp; 14 10 6&lt;br /&gt;
Indx: [8] [6] [7]&lt;br /&gt;
Pre:..[0]&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;1 : 14 10 6 1&lt;br /&gt;
&amp;nbsp;9 : 14 10 9 1&lt;br /&gt;
&amp;nbsp;5 : 14 10 9 5&lt;br /&gt;
13 : 14 13 9 5&lt;br /&gt;
&amp;nbsp;3 : 14 13 9 5 3&lt;br /&gt;
11 : 14 13 11 5 3&lt;br /&gt;
&amp;nbsp;7 : 14 13 11 7 3&lt;br /&gt;
15 : 15 13 11 7 3&lt;br /&gt;
&lt;br /&gt;
Length: 5&lt;br /&gt;
Seq: 12 10 9 5 3&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Another Example:&lt;/b&gt;&lt;br /&gt;
&amp;nbsp;1 :&amp;nbsp;&amp;nbsp; &lt;b&gt;&lt;/b&gt;1&lt;br /&gt;
Indx: [1]&lt;br /&gt;
Pre:&amp;nbsp; [0]&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;6 :&amp;nbsp;&amp;nbsp; 6&lt;br /&gt;
Indx: [2]&lt;br /&gt;
Pre:&amp;nbsp; [0]&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;2 : 6 2&lt;br /&gt;
Indx: [2] [3]&lt;br /&gt;
Pre: ..... [2]&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;3 : 6 3&lt;br /&gt;
Indx: [2] [4]&lt;br /&gt;
Pre: ....&amp;nbsp; [2]&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;5 : 6 5&lt;br /&gt;
Indx: [2] [5]&lt;br /&gt;
Pre: ......[2]&lt;br /&gt;
&lt;br /&gt;
Length: 2&lt;br /&gt;
Seq: 6 5&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;Code:&lt;/b&gt;&lt;br /&gt;
--------&lt;br /&gt;
&lt;b&gt;input file:&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;/b&gt;&lt;br /&gt;
&lt;hr /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style=&quot;background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;16&lt;/span&gt;
&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;8&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;4&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;12&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;2&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;10&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;6&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;14&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;9&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;5&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;13&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;3&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;11&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;7&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;15&lt;/span&gt;
&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;8&lt;/span&gt;
-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;7&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;10&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;9&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;2&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;3&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;8&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;8&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;
&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;5&lt;/span&gt;
&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;6&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;2&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;3&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;5&lt;/span&gt;

&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;hr /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style=&quot;background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;&lt;span style=&quot;color: #888888;&quot;&gt;/*&lt;/span&gt;
&lt;span style=&quot;color: #888888;&quot;&gt;Algorithm: Longest Decreasing Seq.&lt;/span&gt;
&lt;span style=&quot;color: #888888;&quot;&gt;Complexity: nlgn&lt;/span&gt;
&lt;span style=&quot;color: #888888;&quot;&gt;*/&lt;/span&gt;
&lt;span style=&quot;color: #cc0000; font-weight: bold;&quot;&gt;#include&amp;lt;stdio.h&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #cc0000; font-weight: bold;&quot;&gt;#include&amp;lt;limits.h&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #cc0000; font-weight: bold;&quot;&gt;#define S 100000&lt;/span&gt;

&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; R[S]={&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;};&lt;span style=&quot;color: #888888;&quot;&gt;/*Generated Table*/&lt;/span&gt;
&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; A[S] = {&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;};&lt;span style=&quot;color: #888888;&quot;&gt;/*Input data*/&lt;/span&gt;
&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; Indx[S]= {&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;}; &lt;span style=&quot;color: #888888;&quot;&gt;/*Index of the Table Element*/&lt;/span&gt;
&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; Pre[S]={&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;}; &lt;span style=&quot;color: #888888;&quot;&gt;/*Previous Element*/&lt;/span&gt;

&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;Print&lt;/span&gt;(&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; i){
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; ( i == &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;) &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt;;
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt; {
        Print(Pre[i]);
        printf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;%d &quot;&lt;/span&gt;,A[i]);
    }
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt;;
}
&lt;span style=&quot;color: #888888;&quot;&gt;/*Complexity lg2^N*/&lt;/span&gt;
&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;BS&lt;/span&gt;(&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; v,&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; s,&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; e){
    &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; mid=&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt;(s&amp;lt;=e){
         mid = s+((e-s)/&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;2&lt;/span&gt;);
         &lt;span style=&quot;color: #888888;&quot;&gt;/*Remove small*/&lt;/span&gt;
         &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; (R[mid]&amp;lt;=v){
              &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; (v&amp;lt;R[mid-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;]|| v == R[mid-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;])&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;break&lt;/span&gt;;
              e = mid-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;
         }
         &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; (v&amp;lt;R[mid]){
            &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;(R[mid+&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;]&amp;lt;v){mid=mid+&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;break&lt;/span&gt;;}
            s = mid+&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;
         }
    }
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; mid;
}


&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;main&lt;/span&gt;(){
    &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; i,v=&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;,j;
    &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; rc;
    &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; s,e,mid;&lt;span style=&quot;color: #888888;&quot;&gt;/*Binary Search*/&lt;/span&gt;
    &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; N;

    freopen(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;LIS.in&quot;&lt;/span&gt;,&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;r&quot;&lt;/span&gt;,stdin);

    A[&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;]=R[&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;] = INT_MAX;
    Indx[&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;] = &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;

    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt;(&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;){
       scanf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;%d&quot;&lt;/span&gt;,&amp;amp;N);
       &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;(N==&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;) &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;break&lt;/span&gt;;

       rc=&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;
       &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt;(i=&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;i&amp;lt;=N;i++){
           scanf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;%d&quot;&lt;/span&gt;,&amp;amp;A[i]);
           v = A[i];
           &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;(v&amp;lt;R[rc-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;]){
              R[rc] = v;
              Indx[rc] = i;
              Pre[i] = Indx[rc-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;];
              rc++;
           }
           &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;(v&amp;gt;R[rc-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;]){&lt;span style=&quot;color: #888888;&quot;&gt;/*Binary Search*/&lt;/span&gt;
              mid = BS(v,&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;,rc-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;);
              R[mid] = v;
              Indx[mid]= i;
              Pre[i] = Indx[mid-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;];
            }
        }
        printf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;Length: %d&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #0044dd;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;&lt;/span&gt;,rc-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;);
        printf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;Seq: &quot;&lt;/span&gt;);
        i = Indx[rc-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;];
        Print(i);
        printf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #0044dd;&quot;&gt;\n\n&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;&lt;/span&gt;);

        &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt;(i=&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;i&amp;lt;=N;i++) Pre[i]=&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;,Indx[i]=&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;,R[i]=&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
    }
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://rizoantoufiq.blogspot.com/feeds/884998246609627319/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://rizoantoufiq.blogspot.com/2015/10/longest-decreasing-subsequence.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1124334728283106452/posts/default/884998246609627319'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1124334728283106452/posts/default/884998246609627319'/><link rel='alternate' type='text/html' href='http://rizoantoufiq.blogspot.com/2015/10/longest-decreasing-subsequence.html' title='Longest Decreasing Subsequence'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgAS2Qg1Z7OYp3zsHQSyPfg86P9s3UW2xH-Ib4JH_OA7bkzkJ1vzm2Hu_qAoZ2_UiaRgDKQzRJb6BGjIpwWUlrW0B1U6NimPh7se_l9ttFpeMRFTNhkQQVcvmsggxkdLGoJLinAMHvcQqCV/s72-c/1.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1124334728283106452.post-3019369013514734329</id><published>2015-10-09T09:23:00.002-07:00</published><updated>2015-10-09T11:29:07.232-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Algorithm Design and Analysis"/><category scheme="http://www.blogger.com/atom/ns#" term="Dynamic Programming"/><title type='text'>Longest Increasing Subsequence</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
The &lt;b&gt;longest increasing subsequence&lt;/b&gt; problem is to find a subsequence of a given sequence
 in which the subsequence&#39;s elements are in sorted order, lowest to 
highest, and in which the subsequence is as long as possible. he longest increasing subsequence problem is solvable in time O(&lt;i&gt;n&lt;/i&gt; log &lt;i&gt;n&lt;/i&gt;), where &lt;i&gt;n&lt;/i&gt; denotes the length of the input sequence.&lt;br /&gt;
&lt;b&gt;Example:&lt;/b&gt;&lt;br /&gt;
In the first 16 terms of the binary Van der Corput sequence&lt;br /&gt;
&lt;dl&gt;&lt;dd&gt;0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15&lt;/dd&gt;&lt;/dl&gt;
a longest increasing subsequence is&lt;br /&gt;
&lt;dl&gt;&lt;dd&gt;0, 2, 6, 9, 11, 15.&lt;/dd&gt;&lt;/dl&gt;
This subsequence has length six; the input sequence has no 
seven-member increasing subsequences. The longest increasing subsequence
 in this example is not unique: for instance,&lt;br /&gt;
&lt;dl&gt;&lt;dd&gt;0, 4, 6, 9, 11, 15 or 0, 4, 6, 9, 13, 15&lt;/dd&gt;&lt;/dl&gt;
are other increasing sub sequences of equal length in the same input sequence [&lt;a href=&quot;https://en.wikipedia.org/wiki/Longest_increasing_subsequence&quot; target=&quot;_blank&quot;&gt;Wiki&lt;/a&gt;].&lt;br /&gt;
&lt;b&gt;Dynamic Problem:&lt;/b&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiibAMLyiBZ1hQSYVDempDg6OmmYHTm3afLfryUINTBwA8zIphuv3HtZr0xz5wMEcedEMDsWyUxl_tTKK35qnpJkNMQp7Jm6oUNmKOxvc_d8Dw4JZ67EUXxJxP4NN0wG9_u6Y2FeixyPRuI/s1600/1.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;121&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiibAMLyiBZ1hQSYVDempDg6OmmYHTm3afLfryUINTBwA8zIphuv3HtZr0xz5wMEcedEMDsWyUxl_tTKK35qnpJkNMQp7Jm6oUNmKOxvc_d8Dw4JZ67EUXxJxP4NN0wG9_u6Y2FeixyPRuI/s400/1.jpg&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhkxQW7e1GogAiZAh9Lk0JDppfzgoGjoMSpiF4YnxQoSi9LdQQKq8fXzVMmQx0eUZaUrUlPiOR_TTpq0izUvw3Oja3fzeQfU4l0pErL8Tc-9b3BqVE3ttrEs1QaBjfbO41FOJVrG92ZK2TQ/s1600/2.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;176&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhkxQW7e1GogAiZAh9Lk0JDppfzgoGjoMSpiF4YnxQoSi9LdQQKq8fXzVMmQx0eUZaUrUlPiOR_TTpq0izUvw3Oja3fzeQfU4l0pErL8Tc-9b3BqVE3ttrEs1QaBjfbO41FOJVrG92ZK2TQ/s400/2.jpg&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;u&gt;&lt;b&gt;Explain the procedure to find LIS (Complexity: O(nlgn):&lt;/b&gt;&lt;/u&gt;&lt;br /&gt;
We maintain three array variable , R[], Indx[] and Pre[] &lt;br /&gt;
rc = 1; &lt;br /&gt;
[1] Take ith element int v, if v &amp;gt; R[rc-1] then R[rc] = v,Indx[rc] =i,Pre[i] =Indx[rc-1]&lt;br /&gt;
[2] if v &lt;span style=&quot;font-size: small;&quot;&gt; (Use Binary Search)&lt;/span&gt;&lt;br /&gt;
[3] R[Pos] = v, Indx[Pos] = i, Pre[i] = Indx[Pos-1]&lt;br /&gt;
&lt;br /&gt;
A = {-7 10 9 2 3 8 8 1} [Let index start from 1]&lt;br /&gt;
R[0] = INT_MIN, Indx [0] = 0&lt;br /&gt;
&lt;br /&gt;
v=-7, R = {-7} ( because v&amp;gt;R[0] ) Indx = {1}, Pre = {0} root&lt;br /&gt;
v = 10, R ={-7 10} , Indx = {1,2}, Pre = {0,1};&lt;br /&gt;
v = 9 , R = {-7 9} , Indx = {1, 3} , Pre = {0,1,1}&lt;br /&gt;
v = 2, R = {-7 2}, Indx = {1,4}, Pre= {0,1,1,1}&lt;br /&gt;
v = 3, R = {-7 2 3}, Indx = {1,4,5}, Pre= {0,1,1,1,4}&lt;br /&gt;
v = 8, R = {-7 2 3 8}, Indx = {1,4,5,6}, Pre= {0,1,1,1,4,5}&lt;br /&gt;
v= 8, R = {-7 2 3 8}, Indx = {1,4,5,6}, Pre= {0,1,1,1,4,5,0}&lt;br /&gt;
v = 1, R = {-7 1 3 8}, Indx = {1,8,5,6}, Pre= {0,1,1,1,4,5,5,0,1}&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;More Clear:&lt;/b&gt; &lt;br /&gt;
index:&amp;nbsp; [1]&amp;nbsp; [2]&amp;nbsp; [3] [4] [5] [6] [7] [8]&lt;br /&gt;
A&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; :&amp;nbsp; -7&amp;nbsp; 10 &amp;nbsp; 9&amp;nbsp;&amp;nbsp; 2&amp;nbsp;&amp;nbsp; 3&amp;nbsp;&amp;nbsp; 8&amp;nbsp;&amp;nbsp; 8&amp;nbsp; 1&lt;br /&gt;
Pre : [0] &lt;br /&gt;
Indx: [1]&lt;br /&gt;
-7 :&amp;nbsp; -7&lt;br /&gt;
&lt;br /&gt;
Pre : [0]&amp;nbsp; [1]&lt;br /&gt;
Indx: [1]&amp;nbsp; [2]&lt;br /&gt;
10:&amp;nbsp;&amp;nbsp;&amp;nbsp; -7 10&lt;br /&gt;
&lt;br /&gt;
Pre : [0] [1]&lt;br /&gt;
Indx: [1] [3]&lt;br /&gt;
&lt;br /&gt;
9 :&amp;nbsp;&amp;nbsp;&amp;nbsp; -7&amp;nbsp; 9&lt;br /&gt;
&lt;br /&gt;
Pre : [0] [1]&lt;br /&gt;
Indx: [1] [4]&lt;br /&gt;
2 :&amp;nbsp;&amp;nbsp;&amp;nbsp; -7&amp;nbsp; 2&lt;br /&gt;
&lt;br /&gt;
Pre : [0] [1] [4]&lt;br /&gt;
Indx: [1] [4] [5]&lt;br /&gt;
&lt;br /&gt;
3 :&amp;nbsp; &amp;nbsp; -7&amp;nbsp;&amp;nbsp; 2 3&lt;br /&gt;
&lt;br /&gt;
Pre : [0] [1] [4] [5]&lt;br /&gt;
Indx: [1] [4] [5] [6]&lt;/div&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
8 :&amp;nbsp;&amp;nbsp;&amp;nbsp; -7&amp;nbsp; 2&amp;nbsp;&amp;nbsp; 3&amp;nbsp; 8&lt;br /&gt;
&lt;br /&gt;
Pre : [0] [1] [4] [5]&lt;br /&gt;
Indx: [1] [4] [5] [6] &lt;br /&gt;
8 :&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; -7 2&amp;nbsp;&amp;nbsp; 3&amp;nbsp;&amp;nbsp; 8&lt;br /&gt;
&lt;br /&gt;
Pre : [0] [1] [4] [5]&lt;br /&gt;
Indx: [1] [8] [5] [6] &lt;br /&gt;
1 :&amp;nbsp;&amp;nbsp;&amp;nbsp; -7&amp;nbsp; 1&amp;nbsp;&amp;nbsp; 3&amp;nbsp; 8&lt;br /&gt;
&lt;b&gt;Print:&lt;/b&gt; A[Indx[4] =6] - &amp;gt; A[Pre[6]=5] - &amp;gt; A[Pre[5] =4] - &amp;gt; A[Pre[4] =1] - &amp;gt; Pre[1]=0 break&lt;br /&gt;
8 3 2 -7 - &amp;gt; -7 2 3 8&lt;br /&gt;
&lt;b&gt;input file: LIS.in&lt;/b&gt;&lt;br /&gt;
&lt;hr /&gt;
&lt;br /&gt;
&lt;div style=&quot;background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;16&lt;/span&gt;
&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;8&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;4&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;12&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;2&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;10&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;6&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;14&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;9&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;5&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;13&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;3&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;11&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;7&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;15&lt;/span&gt;
&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;8&lt;/span&gt;
-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;7&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;10&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;9&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;2&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;3&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;8&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;8&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;
&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;5&lt;/span&gt;
&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;6&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;2&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;3&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;5&lt;/span&gt;
&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;hr /&gt;
&lt;br /&gt;
&lt;div style=&quot;background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;&lt;span style=&quot;color: #888888;&quot;&gt;/*&lt;/span&gt;
&lt;span style=&quot;color: #888888;&quot;&gt;Algorithm: Longest Increasing Seq.&lt;/span&gt;
&lt;span style=&quot;color: #888888;&quot;&gt;Complexity: nlgn&lt;/span&gt;
&lt;span style=&quot;color: #888888;&quot;&gt;*/&lt;/span&gt;
&lt;span style=&quot;color: #cc0000; font-weight: bold;&quot;&gt;#include&amp;lt;stdio.h&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #cc0000; font-weight: bold;&quot;&gt;#include&amp;lt;limits.h&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #cc0000; font-weight: bold;&quot;&gt;#define S 100000&lt;/span&gt;

&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; R[S]={&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;};&lt;span style=&quot;color: #888888;&quot;&gt;/*Generated Table*/&lt;/span&gt;
&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; A[S] = {&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;};&lt;span style=&quot;color: #888888;&quot;&gt;/*Input data*/&lt;/span&gt;
&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; Indx[S]= {&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;}; &lt;span style=&quot;color: #888888;&quot;&gt;/*Index of the Table Element*/&lt;/span&gt;
&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; Pre[S]={&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;}; &lt;span style=&quot;color: #888888;&quot;&gt;/*Previous Element*/&lt;/span&gt;

&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;Print&lt;/span&gt;(&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; i){
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; ( i == &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;) &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt;;
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt; {
        Print(Pre[i]);
        printf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;%d &quot;&lt;/span&gt;,A[i]);
    }
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt;;
}
&lt;span style=&quot;color: #888888;&quot;&gt;/*Complexity lg2^N*/&lt;/span&gt;
&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;BS&lt;/span&gt;(&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; v,&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; s,&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; e){
    &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; mid=&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt;(s&amp;lt;=e){
         mid = s+((e-s)/&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;2&lt;/span&gt;);
         &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; (v&amp;lt;=R[mid]){
              &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;(R[mid-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;]&amp;lt;v) &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;break&lt;/span&gt;;
              e = mid-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;
         }
         &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; (v&amp;gt;R[mid]){
            &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;(R[mid+&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;]&amp;gt;=v){mid= mid+&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;break&lt;/span&gt;;}
            s = mid+&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;
         }
    }
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; mid;
}

&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;main&lt;/span&gt;(){
    &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; i,v=&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
    &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; rc;
    &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; s,e,mid;&lt;span style=&quot;color: #888888;&quot;&gt;/*Binary Search*/&lt;/span&gt;
    &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; N;

    freopen(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;LIS.in&quot;&lt;/span&gt;,&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;r&quot;&lt;/span&gt;,stdin);

    A[&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;]=R[&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;] = INT_MIN;
    Indx[&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;] = &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;

    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt;(&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;){
       scanf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;%d&quot;&lt;/span&gt;,&amp;amp;N);
       &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;(N==&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;) &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;break&lt;/span&gt;;

       rc=&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;
       &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt;(i=&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;i&amp;lt;=N;i++){
           scanf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;%d&quot;&lt;/span&gt;,&amp;amp;A[i]);
           v = A[i];
           &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;(v&amp;gt;R[rc-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;]){
              R[rc] = v;
              Indx[rc] = i;
              Pre[i] = Indx[rc-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;];
              rc++;
           }
           &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;(v&amp;lt;R[rc-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;]){&lt;span style=&quot;color: #888888;&quot;&gt;/*Binary Search*/&lt;/span&gt;
              mid = BS(v,&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;,rc-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;);
              R[mid] = v;
              Indx[mid]= i;
              Pre[i] = Indx[mid-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;];
            }
        }
        printf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;Length: %d&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #0044dd;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;&lt;/span&gt;,rc-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;);
        printf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;Seq: &quot;&lt;/span&gt;);
        i = Indx[rc-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;];
        Print(i);
        printf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #0044dd;&quot;&gt;\n\n&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;&lt;/span&gt;);

        &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt;(i=&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;i&amp;lt;=N;i++) Pre[i]=&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;,Indx[i]=&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;,R[i]=&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
    }
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;u&gt;&lt;b&gt;Explain the procedure to find LIS (Complexity: O(n^2):&lt;/b&gt;&lt;/u&gt;&lt;br /&gt;
&lt;br /&gt;
A : Given Data set&lt;br /&gt;
Pre : Predecessor of a number&lt;br /&gt;
L&amp;nbsp; : Length of the seq save&lt;br /&gt;
max: Max length&lt;br /&gt;
End: End Element index&lt;br /&gt;
Let Index start from 1 &lt;br /&gt;
A = {1 6 2 3 5}&amp;nbsp; N = 5&lt;br /&gt;
i : = 1 , j : = 2 to 5&lt;br /&gt;
A[2:5]={6 2 3 5} &amp;gt;A[i] and L[i]+1 &amp;gt; L[j] &lt;br /&gt;
i.e. L[1]+1 = 2&amp;gt; L[2] =1 , L[1] = 2, Pre[2] = i =1, max=2, End = 2 &lt;br /&gt;
L[1]+1 = 2&amp;gt; L[3] =1 , L[3] = 2 , Pre[3] = i =1&lt;br /&gt;
L[1]+1 = 2&amp;gt; L[4] =1 , L[4] = 2, Pre[4] = i =1&lt;br /&gt;
L[1]+1 = 2&amp;gt; L[5] =1 , L[5] = 2, Pre[5] = i =1&lt;br /&gt;
L: [ 1 2 2 2 2 ]&lt;br /&gt;
Pre: [ 0 1 1 1 1 ]&lt;br /&gt;
max = 2, End = 5&lt;br /&gt;
&lt;br /&gt;
i = 2, j: 3 to 5&lt;br /&gt;
A[3:5] = {2 3 5} &amp;lt; A[i] and L[i]+1 &amp;gt; L[j] ------ &amp;gt; false &lt;br /&gt;
&lt;br /&gt;
L: [ 1 2 2 2 2 ]&lt;br /&gt;
Pre: [ 0 1 1 1 1 ]&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
max = 2, End = 5&lt;br /&gt;
&lt;br /&gt;
i = 3, j: 4 to 5&lt;br /&gt;
A[3:5] = {3 5}&amp;nbsp; &amp;gt;&amp;nbsp; A[i] and L[i]+1 &amp;gt; L[j]&amp;nbsp; &lt;br /&gt;
&lt;br /&gt;
L: [ 1 2 2 3 3 ]&lt;br /&gt;
Pre: [ 0 1 1 3 3 ]&lt;br /&gt;
max = 3, End= 5&lt;br /&gt;
&lt;br /&gt;
i = 4, j: 5 to 5&lt;br /&gt;
A[3:5] = {5} &amp;gt;&amp;nbsp; A[i] and L[i]+1 &amp;gt; L[j]&amp;nbsp; &lt;br /&gt;
L: [ 1 2 2 3 4 ]&lt;br /&gt;
Pre: [ 0 1 1 3 4 ]&lt;br /&gt;
max = 4, End=5&lt;br /&gt;
&lt;br /&gt;
Given Sq: 1 6 2 3 5&lt;br /&gt;
Length: 4&lt;br /&gt;
Sq: 1 2 3 5&lt;br /&gt;
&lt;br /&gt;
&lt;table border=&quot;1&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; style=&quot;width: 566px;&quot;&gt;
 &lt;colgroup&gt;
  &lt;col span=&quot;8&quot;&gt;&lt;/col&gt;
  &lt;col&gt;&lt;/col&gt;
 &lt;/colgroup&gt;
 &lt;tbody&gt;
&lt;tr height=&quot;20&quot;&gt;
   &lt;td height=&quot;20&quot; style=&quot;height: 20px; text-align: center; width: 63px;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center; width: 63px;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center; width: 63px;&quot;&gt;&lt;/td&gt;
   &lt;td colspan=&quot;5&quot; style=&quot;text-align: center; width: 313px;&quot;&gt;j = i+1&lt;/td&gt;
   &lt;td style=&quot;text-align: center; width: 71px;&quot;&gt;&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr height=&quot;20&quot;&gt;
   &lt;td height=&quot;20&quot; style=&quot;height: 20px; text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;1&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;6&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;2&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;3&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;5&lt;/td&gt;
   &lt;td rowspan=&quot;11&quot; style=&quot;text-align: center; width: 71px;&quot;&gt;Check Increasing Value and path Any Node to j and I to j Compare&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr height=&quot;20&quot;&gt;
   &lt;td height=&quot;20&quot; style=&quot;height: 20px; text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;L&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;1&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;1&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;1&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;1&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;1&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr height=&quot;20&quot;&gt;
   &lt;td height=&quot;20&quot; style=&quot;height: 20px; text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;Pre&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;0&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;0&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;0&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;0&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;0&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr height=&quot;20&quot;&gt;
   &lt;td height=&quot;160&quot; rowspan=&quot;8&quot; style=&quot;height: 160px; text-align: center;&quot;&gt;i= 1 to N-1&lt;/td&gt;
   &lt;td colspan=&quot;1&quot; rowspan=&quot;2&quot; style=&quot;text-align: center;&quot;&gt;1&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;L&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;2&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;2&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;2&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;2&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr height=&quot;20&quot;&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;Pre&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;1&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;1&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;1&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;1&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr height=&quot;20&quot;&gt;
   &lt;td colspan=&quot;1&quot; height=&quot;20&quot; rowspan=&quot;2&quot; style=&quot;height: 20px; text-align: center;&quot;&gt;6&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;L&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;2&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;2&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;2&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr height=&quot;20&quot;&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;Pre&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;1&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;1&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;1&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr height=&quot;20&quot;&gt;
   &lt;td colspan=&quot;1&quot; height=&quot;20&quot; rowspan=&quot;2&quot; style=&quot;height: 20px; text-align: center;&quot;&gt;2&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;L&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;3&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;3&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr height=&quot;20&quot;&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;Pre&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;3&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;3&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr height=&quot;20&quot;&gt;
   &lt;td colspan=&quot;1&quot; height=&quot;20&quot; rowspan=&quot;2&quot; style=&quot;height: 20px; text-align: center;&quot;&gt;3&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;L&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;4&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr height=&quot;20&quot;&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;Pre&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;4&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr height=&quot;20&quot;&gt;
   &lt;td height=&quot;20&quot; style=&quot;height: 20px; text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td colspan=&quot;1&quot; rowspan=&quot;2&quot; style=&quot;text-align: center;&quot;&gt;5&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr height=&quot;20&quot;&gt;
   &lt;td height=&quot;20&quot; style=&quot;height: 20px; text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
  &lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;br /&gt;
Pre = {0 1 1 3 4}&lt;br /&gt;
End = 5 Print - &amp;gt; A[5] - &amp;gt; A [Pre[5] = 4] - &amp;gt; A [Pre [4] = 3] - &amp;gt; A[ Pre[3] = 1] - &amp;gt; A[Pre[1] =0]&amp;nbsp; Break&lt;br /&gt;
Max = Max (L) = 4&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Another:&lt;/b&gt;&lt;br /&gt;
A = {-7 10 9 2 3 8 8}&lt;/div&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;/div&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;/div&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
L: [ 1 2 2 2 2 2 2 ]&lt;br /&gt;
Pre: [ 0 1 1 1 1 1 1 ]&lt;br /&gt;
Max = 2 End : 7&lt;br /&gt;
&lt;br /&gt;
L: [ 1 2 2 2 2 2 2 ]&lt;br /&gt;
Pre: [ 0 1 1 1 1 1 1 ]&lt;br /&gt;
Max = 2 End : 7&lt;br /&gt;
&lt;br /&gt;
L: [ 1 2 2 2 2 2 2 ]&lt;br /&gt;
Pre: [ 0 1 1 1 1 1 1 ]&lt;br /&gt;
Max = 2 End : 7&lt;br /&gt;
&lt;br /&gt;
L: [ 1 2 2 2 3 3 3 ]&lt;br /&gt;
Pre: [ 0 1 1 1 4 4 4 ]&lt;br /&gt;
Max = 3 End : 7&lt;br /&gt;
&lt;br /&gt;
L: [ 1 2 2 2 3 4 4 ]&lt;br /&gt;
Pre: [ 0 1 1 1 4 5 5 ]&lt;br /&gt;
Max = 4 End : 7&lt;br /&gt;
&lt;br /&gt;
L: [ 1 2 2 2 3 4 4 ]&lt;br /&gt;
Pre: [ 0 1 1 1 4 5 5 ]&lt;br /&gt;
Max = 4 End : 7&lt;br /&gt;
&lt;br /&gt;
Given Sq: -7 10 9 2 3 8 8&lt;br /&gt;
Length: 4&lt;br /&gt;
Sq: -7 2 3 8&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;table border=&quot;1&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; style=&quot;height: 390px; width: 566px;&quot;&gt;
 &lt;colgroup&gt;
  &lt;col span=&quot;8&quot;&gt;&lt;/col&gt;
  &lt;col&gt;&lt;/col&gt;
  &lt;col span=&quot;2&quot;&gt;&lt;/col&gt;
 &lt;/colgroup&gt;
 &lt;tbody&gt;
&lt;tr height=&quot;20&quot;&gt;
   &lt;td height=&quot;20&quot; style=&quot;height: 20px; text-align: center; width: 63px;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center; width: 63px;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center; width: 63px;&quot;&gt;&lt;/td&gt;
   &lt;td colspan=&quot;7&quot; style=&quot;text-align: center; width: 447px;&quot;&gt;j = i+1&lt;/td&gt;
   &lt;td style=&quot;width: 63px;&quot;&gt;&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr height=&quot;22&quot;&gt;
   &lt;td height=&quot;22&quot; style=&quot;height: 22px; text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;-7&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;10&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;9&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;2&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;3&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;8&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;8&lt;/td&gt;
   &lt;td rowspan=&quot;17&quot; style=&quot;text-align: center;&quot;&gt;Check Increasing Value and path Any Node to j and I to j Compare&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr height=&quot;22&quot;&gt;
   &lt;td height=&quot;22&quot; style=&quot;height: 22px; text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;L&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;1&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;1&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;1&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;1&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;1&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;1&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;1&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr height=&quot;22&quot;&gt;
   &lt;td height=&quot;22&quot; style=&quot;height: 22px; text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;Pre&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;0&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;0&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;0&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;0&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;0&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;0&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;0&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr height=&quot;22&quot;&gt;
   &lt;td height=&quot;258&quot; rowspan=&quot;12&quot; style=&quot;height: 258px; text-align: center; width: 63px;&quot;&gt;i= 1 to N-1&lt;/td&gt;
   &lt;td colspan=&quot;1&quot; rowspan=&quot;2&quot; style=&quot;text-align: center;&quot;&gt;-7&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;L&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;2&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;2&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;2&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;2&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;2&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;2&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr height=&quot;20&quot;&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;Pre&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;1&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;1&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;1&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;1&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;1&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;1&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr height=&quot;22&quot;&gt;
   &lt;td colspan=&quot;1&quot; height=&quot;22&quot; rowspan=&quot;2&quot; style=&quot;height: 22px; text-align: center;&quot;&gt;10&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;L&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;2&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;2&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;2&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;2&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;2&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr height=&quot;22&quot;&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;Pre&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;1&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;1&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;1&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;1&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;1&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr height=&quot;22&quot;&gt;
   &lt;td colspan=&quot;1&quot; height=&quot;22&quot; rowspan=&quot;2&quot; style=&quot;height: 22px; text-align: center;&quot;&gt;9&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;L&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;2&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;2&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;2&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;2&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr height=&quot;22&quot;&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;Pre&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;1&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;1&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;1&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;1&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr height=&quot;22&quot;&gt;
   &lt;td colspan=&quot;1&quot; height=&quot;22&quot; rowspan=&quot;2&quot; style=&quot;height: 22px; text-align: center;&quot;&gt;2&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;L&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;3&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;3&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;3&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr height=&quot;22&quot;&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;Pre&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;4&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;4&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;4&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr height=&quot;22&quot;&gt;
   &lt;td colspan=&quot;1&quot; height=&quot;22&quot; rowspan=&quot;2&quot; style=&quot;height: 22px; text-align: center;&quot;&gt;3&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;L&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;4&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;4&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr height=&quot;22&quot;&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;Pre&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;5&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;5&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr height=&quot;20&quot;&gt;
   &lt;td colspan=&quot;1&quot; height=&quot;20&quot; rowspan=&quot;2&quot; style=&quot;height: 20px; text-align: center;&quot;&gt;8&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;L&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;4&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr height=&quot;20&quot;&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;Pre&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;5&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr height=&quot;20&quot;&gt;
   &lt;td height=&quot;20&quot; style=&quot;height: 20px; text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td colspan=&quot;1&quot; rowspan=&quot;2&quot; style=&quot;text-align: center;&quot;&gt;8&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr height=&quot;20&quot;&gt;
   &lt;td height=&quot;20&quot; style=&quot;height: 20px; text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
   &lt;td style=&quot;text-align: center;&quot;&gt;&lt;/td&gt;
  &lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;br /&gt;
Pre = { 0 1 1 1 4 5 5}&lt;br /&gt;
End = 6 ( Last change Occur); &lt;br /&gt;
A[End = 6] - &amp;gt; A[ Pre [End] = 5] - &amp;gt; A[Pre[5] = 4]&amp;nbsp; - &amp;gt; A[Pre[4]=1] - &amp;gt; A[Pre [1] = 0]&lt;br /&gt;
8 3 2 -7 ==&amp;gt; -7 2 3 8&lt;br /&gt;
&lt;br /&gt;
Max = Max(L) = 4&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Code:&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;------&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style=&quot;background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;&lt;span style=&quot;color: #888888;&quot;&gt;/*Algorithm: LIS&lt;/span&gt;
&lt;span style=&quot;color: #888888;&quot;&gt;Complexity: O(n^2)&lt;/span&gt;
&lt;span style=&quot;color: #888888;&quot;&gt;*/&lt;/span&gt;
&lt;span style=&quot;color: #888888;&quot;&gt;/*&lt;/span&gt;
&lt;span style=&quot;color: #888888;&quot;&gt;Algorithm: LIS&lt;/span&gt;
&lt;span style=&quot;color: #888888;&quot;&gt;Complexity: O(n^2)&lt;/span&gt;
&lt;span style=&quot;color: #888888;&quot;&gt;A [] : Given Data set&lt;/span&gt;
&lt;span style=&quot;color: #888888;&quot;&gt;Pre [] : Predecessor of a number&lt;/span&gt;
&lt;span style=&quot;color: #888888;&quot;&gt;L [] : Length of the seq save&lt;/span&gt;
&lt;span style=&quot;color: #888888;&quot;&gt;max: Max length&lt;/span&gt;
&lt;span style=&quot;color: #888888;&quot;&gt;End: End Element index&lt;/span&gt;
&lt;span style=&quot;color: #888888;&quot;&gt;*/&lt;/span&gt;
&lt;span style=&quot;color: #cc0000; font-weight: bold;&quot;&gt;#include &amp;lt;limits.h&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #cc0000; font-weight: bold;&quot;&gt;#include&amp;lt;stdlib.h&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #cc0000; font-weight: bold;&quot;&gt;#include&amp;lt;stdio.h&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #cc0000; font-weight: bold;&quot;&gt;#define S 10000&lt;/span&gt;

&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;print&lt;/span&gt;(&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; *A, &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; *Pre,&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; End){
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; ( End == &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;) &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt;;
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt;{
      print(A,Pre,Pre[End]);
      printf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;%d &quot;&lt;/span&gt;,A[End]);
  }
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt;;
}

&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;main&lt;/span&gt;(){
  &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; A[S];
  &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; N = &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;16&lt;/span&gt;;
  &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; Pre[S],L[S];
  &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; i,j,max,End;

  freopen(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;LIS.in&quot;&lt;/span&gt;,&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;r&quot;&lt;/span&gt;,stdin);

  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt;(&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;){
    scanf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;%d&quot;&lt;/span&gt;,&amp;amp;N);
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; (N==&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;) &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;break&lt;/span&gt;;
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt; ( i = &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;; i&amp;lt;=N ; i++) scanf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;%d&quot;&lt;/span&gt;,&amp;amp;A[i]);


    &lt;span style=&quot;color: #888888;&quot;&gt;/*Algorithm start: O(n^2)*/&lt;/span&gt;
    max = INT_MIN;
    End = &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt; ( i = &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;; i&amp;lt;=N ; i++) Pre[i]=&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;,L[i]=&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;;
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt; (i = &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;; i &amp;lt;= (N-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;); i++){
      &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt; (j = i+&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;; j &amp;lt;= N; j++){
          &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; ( A[j]&amp;gt; A[i] &amp;amp;&amp;amp; L[i]+&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;&amp;gt;L[j]){
            Pre[j] = i;
            L[j] = L[i]+&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;
            End = j;
            max = L[j];
          }
      }
    }

    &lt;span style=&quot;color: #888888;&quot;&gt;/*Print*/&lt;/span&gt;
    printf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;Given Sq: &quot;&lt;/span&gt;);
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt;(i=&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;i&amp;lt;=N;i++) printf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;%d &quot;&lt;/span&gt;,A[i]);
    printf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #0044dd;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;&lt;/span&gt;);
    printf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;Length: %d&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #0044dd;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;&lt;/span&gt;,max);
    printf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;Sq: &quot;&lt;/span&gt;);
    print(A,Pre,End);
    printf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #0044dd;&quot;&gt;\n\n\n&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;&lt;/span&gt;);
  }
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style=&quot;background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;Given Sq: &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;8&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;4&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;12&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;2&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;10&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;6&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;14&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;9&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;5&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;13&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;3&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;11&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;7&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;15&lt;/span&gt;
&lt;span style=&quot;color: #336699; font-style: italic;&quot;&gt;Length:&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;6&lt;/span&gt;
&lt;span style=&quot;color: #336699; font-style: italic;&quot;&gt;Sq:&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;4&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;6&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;9&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;13&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;15&lt;/span&gt;


Given Sq: -&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;7&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;10&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;9&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;2&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;3&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;8&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;8&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;
&lt;span style=&quot;color: #336699; font-style: italic;&quot;&gt;Length:&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;4&lt;/span&gt;
&lt;span style=&quot;color: #336699; font-style: italic;&quot;&gt;Sq:&lt;/span&gt; -&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;7&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;2&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;3&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;8&lt;/span&gt;


Given Sq: &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;6&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;2&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;3&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;5&lt;/span&gt;
&lt;span style=&quot;color: #336699; font-style: italic;&quot;&gt;Length:&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;4&lt;/span&gt;
&lt;span style=&quot;color: #336699; font-style: italic;&quot;&gt;Sq:&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;2&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;3&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;5&lt;/span&gt;



Process returned &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt; (&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0x0&lt;/span&gt;)   execution time : &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0.797&lt;/span&gt; s
Press any key to &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;continue&lt;/span&gt;.
&lt;/pre&gt;
&lt;/div&gt;
&amp;nbsp;&lt;b&gt;&amp;nbsp;&lt;/b&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://rizoantoufiq.blogspot.com/feeds/3019369013514734329/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://rizoantoufiq.blogspot.com/2015/10/longest-increasing-subsequence.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1124334728283106452/posts/default/3019369013514734329'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1124334728283106452/posts/default/3019369013514734329'/><link rel='alternate' type='text/html' href='http://rizoantoufiq.blogspot.com/2015/10/longest-increasing-subsequence.html' title='Longest Increasing Subsequence'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiibAMLyiBZ1hQSYVDempDg6OmmYHTm3afLfryUINTBwA8zIphuv3HtZr0xz5wMEcedEMDsWyUxl_tTKK35qnpJkNMQp7Jm6oUNmKOxvc_d8Dw4JZ67EUXxJxP4NN0wG9_u6Y2FeixyPRuI/s72-c/1.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1124334728283106452.post-5389679128070423866</id><published>2015-10-05T14:04:00.000-07:00</published><updated>2015-10-09T11:35:34.022-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Uva - Convex Hull"/><category scheme="http://www.blogger.com/atom/ns#" term="Uva Problems &amp; Solution"/><title type='text'>Uva 109 - SCUD Busters </title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Problem: &lt;a href=&quot;https://uva.onlinejudge.org/external/1/109.pdf&quot; target=&quot;_blank&quot;&gt;109 - SCUD Busters &lt;/a&gt;&lt;br /&gt;
&lt;b&gt;Description:&lt;/b&gt;&lt;br /&gt;
[1] Find individual Convex hull for each area (Read:&lt;span id=&quot;goog_1465503285&quot;&gt;&lt;/span&gt;&lt;a href=&quot;http://rizoantoufiq.blogspot.com/2010/10/convex-hull.html&quot; target=&quot;_blank&quot;&gt;Convex Hull) &lt;/a&gt;&lt;span id=&quot;goog_1465503286&quot;&gt;&lt;/span&gt;&lt;br /&gt;
[2] Check: Is a Missile falls inside a convex hull, mark it&lt;br /&gt;
[3] Sum the area of the marked convex hull&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Example:&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;First Kingdom: &lt;/b&gt;Number of Points : 12&lt;br /&gt;
(3 3 ) , (4 6) , (4 11) , (4 8) , (10 6) , (5 7), (6 6), (6 3), (7 9) ,(10 4),(10 9), (1 7)&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;
Convex Hull: (4 11) ,(10 9),(10 6),(10 4),(6 3),(3 3) and (1 7)&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiQvfaCBIh3GzPIRMR-5PM1-tnJGcGpy2WXh-2V9UFrTKVs-Tar3cCUNIg18MKf7PipzBLnMp0ao4w_qiuHKbYBfcWcLUP4_DGn7PuACBhaS7aMCe2QRZUKKmM1LE8QCkHX2jF-VqVmcW_H/s1600/2.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;192&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiQvfaCBIh3GzPIRMR-5PM1-tnJGcGpy2WXh-2V9UFrTKVs-Tar3cCUNIg18MKf7PipzBLnMp0ao4w_qiuHKbYBfcWcLUP4_DGn7PuACBhaS7aMCe2QRZUKKmM1LE8QCkHX2jF-VqVmcW_H/s320/2.jpg&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
Area1: 54.00&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Second Kingdom:&lt;/b&gt; Number of Points: 5&lt;br /&gt;
&lt;br /&gt;
(20 20), (20 40), (40 20), (40 40) and (30 30)&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgsa23i8VStFzfVB8TDn-u6ftJquoH4yIFYzrPb_H1gqazxDAgwz3eQMm2aGkHcnENB-gsTdrbjlIgrKwbb99FnAsDsoa0VvajWVefmG5rO8qxgS2xI_w454wWWg_DrnAZvdbHBbCb_N3HP/s1600/3.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;192&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgsa23i8VStFzfVB8TDn-u6ftJquoH4yIFYzrPb_H1gqazxDAgwz3eQMm2aGkHcnENB-gsTdrbjlIgrKwbb99FnAsDsoa0VvajWVefmG5rO8qxgS2xI_w454wWWg_DrnAZvdbHBbCb_N3HP/s320/3.jpg&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;br /&gt;
Convex Hull: (20 40), (40 40), (40 20), and (20 20)&lt;br /&gt;
&lt;br /&gt;
Area2 : 400&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Third Kingdom:&lt;/b&gt; Number of Points: 3&lt;br /&gt;
&lt;br /&gt;
(10 10), (21 10), and (21 13)&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi5niOC5_7GuJSclOm9Mx-t3BI8kJGa7VwneYVNzUnC5WzG99ktj6DJDNtjQYqbSLAYu9iphz8hUOpIKuVkOlpVgvMDJ5SJsdRyvOGyHbfpNBrLhgFUU2JxTOTtsR7ZmwMbEBFogbre9yP8/s1600/5.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;193&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi5niOC5_7GuJSclOm9Mx-t3BI8kJGa7VwneYVNzUnC5WzG99ktj6DJDNtjQYqbSLAYu9iphz8hUOpIKuVkOlpVgvMDJ5SJsdRyvOGyHbfpNBrLhgFUU2JxTOTtsR7ZmwMbEBFogbre9yP8/s320/5.jpg&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Convex Hull: (21 13), (21 10), (10 10)&lt;br /&gt;
&lt;br /&gt;
Area3 = 16.50&lt;br /&gt;
&lt;br /&gt;
Now i am going to missile attack (Boom!!!!) &lt;br /&gt;
&lt;br /&gt;
(5, 5)&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgStPJLpoEu8z8U0O-I3sz2qEorvcW459LCr5sa_c6lLayI6HjAQiHXV46R1lwIIo0DHfm0so5e2l8hu2GmJY0ZBaC1FBS7xH-VXXDKAYqnCdFkGhWYMFBIaK49lHNjvrstnFrmkK0fSWeD/s1600/6.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;192&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgStPJLpoEu8z8U0O-I3sz2qEorvcW459LCr5sa_c6lLayI6HjAQiHXV46R1lwIIo0DHfm0so5e2l8hu2GmJY0ZBaC1FBS7xH-VXXDKAYqnCdFkGhWYMFBIaK49lHNjvrstnFrmkK0fSWeD/s320/6.jpg&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Destroy Area1&lt;br /&gt;
&lt;br /&gt;
SUM&amp;nbsp; = 0+area1 = 54.00&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
(20 12)&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjtvZcjBfI-pwbOzHm_Kl_80nR5SlTqfnzru9oahdLApKvWihZMjGGXXT5uJqq_JG0h-6j0qYxI2x0XAqVMV-u4Wf-0brxMmn6WaDdnwZeH07Stui9CsVTFTEbCWiFjhOkDmXlSxOJyPt5d/s1600/4.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;193&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjtvZcjBfI-pwbOzHm_Kl_80nR5SlTqfnzru9oahdLApKvWihZMjGGXXT5uJqq_JG0h-6j0qYxI2x0XAqVMV-u4Wf-0brxMmn6WaDdnwZeH07Stui9CsVTFTEbCWiFjhOkDmXlSxOJyPt5d/s320/4.jpg&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Destroy Area3&lt;br /&gt;
SUM = 54.00 + 16.50 = 70.50 (ans)&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Input: &lt;/b&gt;&lt;br /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style=&quot;background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;12&lt;/span&gt;
&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;3&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;3&lt;/span&gt;
&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;4&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;6&lt;/span&gt;
&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;4&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;11&lt;/span&gt;
&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;4&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;8&lt;/span&gt;
&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;10&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;6&lt;/span&gt;
&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;5&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;7&lt;/span&gt;
&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;6&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;6&lt;/span&gt;
&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;6&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;3&lt;/span&gt;
&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;7&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;9&lt;/span&gt;
&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;10&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;4&lt;/span&gt;
&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;10&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;9&lt;/span&gt;
&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;7&lt;/span&gt;
&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;5&lt;/span&gt;
&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;20&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;20&lt;/span&gt;
&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;20&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;40&lt;/span&gt;
&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;40&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;20&lt;/span&gt;
&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;40&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;40&lt;/span&gt;
&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;30&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;30&lt;/span&gt;
&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;3&lt;/span&gt;
&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;10&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;10&lt;/span&gt;
&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;21&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;10&lt;/span&gt;
&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;21&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;13&lt;/span&gt;
-&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;
&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;5&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;5&lt;/span&gt;
&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;20&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;12&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;b&gt;output:&lt;/b&gt; &lt;br /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style=&quot;background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;70.50&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Code:
&lt;/b&gt;&lt;br /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style=&quot;background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;&lt;span style=&quot;color: #888888;&quot;&gt;// UVa 109 - SCUD Busters&lt;/span&gt;
&lt;span style=&quot;color: #cc0000; font-weight: bold;&quot;&gt;#include &amp;lt;iostream&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #cc0000; font-weight: bold;&quot;&gt;#include &amp;lt;cstdio&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #cc0000; font-weight: bold;&quot;&gt;#include &amp;lt;vector&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #cc0000; font-weight: bold;&quot;&gt;#include &amp;lt;algorithm&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #cc0000; font-weight: bold;&quot;&gt;#include &amp;lt;cstring&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #cc0000; font-weight: bold;&quot;&gt;#include &amp;lt;cmath&amp;gt;&lt;/span&gt;
using namespace std;

&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;struct&lt;/span&gt; point {
 &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; x, y;
};

point piv;

&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;cross&lt;/span&gt;(point c, point a, point b) {
 &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; x1 = a.x - c.x;
 &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; x2 = b.x - c.x;
 &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; y1 = a.y - c.y;
 &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; y2 = b.y - c.y;
 &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; x1 * y2 - x2 * y1;
}

&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;bool&lt;/span&gt; operator &amp;lt;(point a, point b) {
 &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; cross(piv, a, b) &amp;lt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
}

&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; main() {
 &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; n = &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;, m;
 vector&amp;lt;point&amp;gt; p[&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;20&lt;/span&gt;];

 &lt;span style=&quot;color: #888888;&quot;&gt;//double area[20];&lt;/span&gt;
 &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;double&lt;/span&gt; area;
 point hull[&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;20&lt;/span&gt;][&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;101&lt;/span&gt;];
 &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; h[&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;20&lt;/span&gt;];

 &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;double&lt;/span&gt; sol = &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
 point fire;
 &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;bool&lt;/span&gt; alive[&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;101&lt;/span&gt;];

 &lt;span style=&quot;color: #888888;&quot;&gt;//freopen(&quot;109.in&quot;,&quot;r&quot;,stdin);&lt;/span&gt;

 cin &amp;gt;&amp;gt; m;
 &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt; (m != -&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;) {
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; j = &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;; j &amp;lt; m; j++) {
   point pp;
   cin &amp;gt;&amp;gt; pp.x &amp;gt;&amp;gt; pp.y;
   p[n].push_back(pp);
  }
  n++;
  cin &amp;gt;&amp;gt; m;
 }

    &lt;span style=&quot;color: #888888;&quot;&gt;//1. find Convex Hull&lt;/span&gt;
 &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; i = &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;; i &amp;lt; n; i++) {
  &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; mn = &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; j = &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;; j &amp;lt; p[i].size(); j++)
   &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; (p[i][j].x &amp;lt; p[i][mn].x || p[i][j].x == p[i][mn].x &amp;amp;&amp;amp; p[i][j].y &amp;lt; p[i][mn].y)
    mn = j;
  piv = p[i][mn];
  p[i][mn] = p[i][&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;];
  p[i][&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;] = piv;
  sort(p[i].begin() + &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;, p[i].end());

  p[i].push_back(piv);
  hull[i][&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;] = p[i][&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;];
  hull[i][&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;] = p[i][&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;];
  h[i] = &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;2&lt;/span&gt;;
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; j = &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;2&lt;/span&gt;; j &amp;lt; p[i].size(); j++) {
   &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt; (h[i] &amp;gt;= &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;2&lt;/span&gt; &amp;amp;&amp;amp; cross(hull[i][h[i] - &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;2&lt;/span&gt;], hull[i][h[i] - &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;], p[i][j]) &amp;gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;)
    h[i]--;
   hull[i][h[i]++] = p[i][j];
  }
 }

    &lt;span style=&quot;color: #888888;&quot;&gt;//2. Is Missile land inside a convex hull, then find area and sum&lt;/span&gt;
 memset(alive, &lt;span style=&quot;color: #003388;&quot;&gt;true&lt;/span&gt;, &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;sizeof&lt;/span&gt;(alive));
 &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt; (cin &amp;gt;&amp;gt; fire.x &amp;gt;&amp;gt; fire.y) {
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; i = &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;; i &amp;lt; n; i++)
   &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; (alive[i]) {
       &lt;span style=&quot;color: #888888;&quot;&gt;// Check missile is in convex hull&lt;/span&gt;
    &lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;bool&lt;/span&gt; mark = &lt;span style=&quot;color: #003388;&quot;&gt;true&lt;/span&gt;;
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; j = &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;; j &amp;lt; h[i]; j++) {
     &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; (cross(hull[i][j - &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;], hull[i][j], fire) &amp;gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;) {
      mark = &lt;span style=&quot;color: #003388;&quot;&gt;false&lt;/span&gt;;
      &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;break&lt;/span&gt;;
     }
    }
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; (mark) {
     alive[i] = &lt;span style=&quot;color: #003388;&quot;&gt;false&lt;/span&gt;;
                    &lt;span style=&quot;color: #888888;&quot;&gt;// Inside- Yes&lt;/span&gt;
     area = &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
              &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: #888888; font-weight: bold;&quot;&gt;int&lt;/span&gt; j = &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;; j &amp;lt; h[i]; j++)
                area += hull[i][j - &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;].x * hull[i][j].y - hull[i][j].x * hull[i][j - &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;].y;
                    area =area/&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;2&lt;/span&gt;;
     sol += area;
    }
   }
 }
 printf(&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;%.2f&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #0044dd;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #dd2200;&quot;&gt;&quot;&lt;/span&gt;, abs(sol));

 &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://rizoantoufiq.blogspot.com/feeds/5389679128070423866/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://rizoantoufiq.blogspot.com/2015/10/uva-109-scud-busters_5.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1124334728283106452/posts/default/5389679128070423866'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1124334728283106452/posts/default/5389679128070423866'/><link rel='alternate' type='text/html' href='http://rizoantoufiq.blogspot.com/2015/10/uva-109-scud-busters_5.html' title='Uva 109 - SCUD Busters '/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiQvfaCBIh3GzPIRMR-5PM1-tnJGcGpy2WXh-2V9UFrTKVs-Tar3cCUNIg18MKf7PipzBLnMp0ao4w_qiuHKbYBfcWcLUP4_DGn7PuACBhaS7aMCe2QRZUKKmM1LE8QCkHX2jF-VqVmcW_H/s72-c/2.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1124334728283106452.post-6283521362324379101</id><published>2015-10-05T09:23:00.000-07:00</published><updated>2015-10-09T11:36:51.921-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Uva - Kadane&#39;s Algorithm"/><category scheme="http://www.blogger.com/atom/ns#" term="Uva Problems &amp; Solution"/><title type='text'>Uva 108 - Maximum Sum</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Problem: &lt;a href=&quot;https://uva.onlinejudge.org/external/1/108.pdf&quot; target=&quot;_blank&quot;&gt;108 - Maximum Sum&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
Description:&lt;br /&gt;
We can solve this problem with O(N^3) order. &lt;span style=&quot;font-size: medium;&quot;&gt;Kadane&#39;s algorithm can be used to find &lt;/span&gt;the contiguous subarray within a one-dimensional array of numbers (containing at least one positive number). The complexity of Kadane&#39;s algorithm for 1D array is O(N).&lt;br /&gt;
&lt;br /&gt;
Read : &lt;a href=&quot;http://rizoantoufiq.blogspot.com/2015/10/maximum-subarray-problem.html&quot; target=&quot;_blank&quot;&gt;Maximum subarray problem&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
-------&lt;br /&gt;
&lt;br /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style=&quot;background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;&lt;span style=&quot;color: #557799;&quot;&gt;#include&amp;lt;stdio.h&amp;gt;&lt;/span&gt;

&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;main&lt;/span&gt;( &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;void&lt;/span&gt; ){

    &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; N;
    &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; t &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
    &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; a[&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;100&lt;/span&gt;][&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;100&lt;/span&gt;];
    &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; pr[&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;100&lt;/span&gt;];
    &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; S &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;31&lt;/span&gt;, s &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;, k, l,j;
    &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; i,x,z,f&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
    &lt;span style=&quot;color: #888888;&quot;&gt;/*freopen(&quot;108.in&quot;,&quot;r&quot;,stdin);*/&lt;/span&gt;

    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt;((scanf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;%d&quot;&lt;/span&gt;,&lt;span style=&quot;color: #333333;&quot;&gt;&amp;amp;&lt;/span&gt;N))&lt;span style=&quot;color: #333333;&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;){
   &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt;(i&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;i&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt;N;i&lt;span style=&quot;color: #333333;&quot;&gt;++&lt;/span&gt;)
  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt;(j&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;j&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt;N;j&lt;span style=&quot;color: #333333;&quot;&gt;++&lt;/span&gt;)
   scanf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;%d&quot;&lt;/span&gt;,&lt;span style=&quot;color: #333333;&quot;&gt;&amp;amp;&lt;/span&gt;a[i][j]);
      S&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;31&lt;/span&gt;;

      &lt;span style=&quot;color: #888888;&quot;&gt;/*Kadan 2D algorithm- O(N^3)*/&lt;/span&gt;
      &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt;( z &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;; z &lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt; N; z&lt;span style=&quot;color: #333333;&quot;&gt;++&lt;/span&gt;){
        &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt;(i &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;; i &lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt; N; i&lt;span style=&quot;color: #333333;&quot;&gt;++&lt;/span&gt;) pr[i] &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
        &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt;(x &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; z; x &lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt; N; x&lt;span style=&quot;color: #333333;&quot;&gt;++&lt;/span&gt;){
            t &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
            s &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;31&lt;/span&gt;;
            j &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
            k &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;; l &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
            &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt;(i &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;; i &lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt; N; i&lt;span style=&quot;color: #333333;&quot;&gt;++&lt;/span&gt;){
                pr[i] &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; pr[i] &lt;span style=&quot;color: #333333;&quot;&gt;+&lt;/span&gt; a[x][i];
                t &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; t &lt;span style=&quot;color: #333333;&quot;&gt;+&lt;/span&gt; pr[i];
                &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;( t &lt;span style=&quot;color: #333333;&quot;&gt;&amp;gt;&lt;/span&gt; s){
                    s &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; t;
                    k &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; i;
                    l &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; j;
                }
                &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;( t &lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt; ){
                    t &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
                    j &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; i &lt;span style=&quot;color: #333333;&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;
                }
            }
            &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;( s &lt;span style=&quot;color: #333333;&quot;&gt;&amp;gt;&lt;/span&gt; S)S &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; s;

        }
      }
      printf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;%d&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #666666; font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;&lt;/span&gt;,S);
    }

    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://rizoantoufiq.blogspot.com/feeds/6283521362324379101/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://rizoantoufiq.blogspot.com/2015/10/uva-108-maximum-sum.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1124334728283106452/posts/default/6283521362324379101'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1124334728283106452/posts/default/6283521362324379101'/><link rel='alternate' type='text/html' href='http://rizoantoufiq.blogspot.com/2015/10/uva-108-maximum-sum.html' title='Uva 108 - Maximum Sum'/><author><name>Unknown</name><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-1124334728283106452.post-1444897694881225601</id><published>2015-10-04T13:09:00.001-07:00</published><updated>2015-10-11T07:48:56.285-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Algorithm Design and Analysis"/><category scheme="http://www.blogger.com/atom/ns#" term="Dynamic Programming"/><title type='text'>Maximum Subarray Problem</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
In computer science, the maximum subarray problem is the task of finding the contiguous subarray within a one-dimensional array of numbers (containing at least one positive number) which has the largest sum. The problem was first posed by Ulf Grenander of Brown University in 1977, as a simplified model for maximum likelihood estimation of patterns in digitized images. A linear time algorithm was found soon afterwards by Jay Kadane of Carnegie-Mellon University (Bentley 1984). [&lt;a href=&quot;https://en.wikipedia.org/wiki/Maximum_subarray_problem&quot; target=&quot;_blank&quot;&gt;Wiki&lt;/a&gt;]&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
[1] A = {-2,&lt;b&gt;1,-1,1,1,-1,2,1&lt;/b&gt;,-5}; [assum, 1st element is indicated by 0]&lt;br /&gt;
Maximum Value = 4, start index = 1, End index = 7&lt;br /&gt;
&lt;br /&gt;
[2] A = {-2,1,-2,&lt;b&gt;1,1,-1,2,1&lt;/b&gt;,-5}; [assum, 1st element is indicated by 0]&lt;br /&gt;
Maximum Value = 4, start index = 3, End index = 7&lt;br /&gt;
&lt;br /&gt;
[3] A = {-2,1,-3,&lt;b&gt;4,-1,2,1&lt;/b&gt;,-5,-4}; [assum, 1st element is indicated by 0]&lt;br /&gt;
Maximum Value = 6, start index = 3, End index =6&lt;br /&gt;
&lt;br /&gt;
[4] A = {2,3,5,4,6,3,5,4,6};&lt;br /&gt;
Maximum Value = 38, start index = 0, End index =8&lt;br /&gt;
&lt;br /&gt;
[5] A = {-1,-2,-3,-4,-5,-6,-7,-8,-9}&lt;br /&gt;
Maximum Value = -1, start index = 0, End index =0&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;Explaination:&lt;br /&gt;
if consider sum is equal to or grater than zero, then we have to follow this algorithm:&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEirA_xhBWVmlFR-JTItiy8Sd8V2KM3uyimwrffoi1QbQl5QYKFmdHXaaEg1P_SV_keaEMLEmGBE0pdTD2cGwoxh8EwXuuh8TlJN2ApRwerm80zZDKaGEVKxNOcItqTKLCDvR5HJYBLzv1V8/s1600/1.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;215&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEirA_xhBWVmlFR-JTItiy8Sd8V2KM3uyimwrffoi1QbQl5QYKFmdHXaaEg1P_SV_keaEMLEmGBE0pdTD2cGwoxh8EwXuuh8TlJN2ApRwerm80zZDKaGEVKxNOcItqTKLCDvR5HJYBLzv1V8/s400/1.jpg&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
if -infinite &amp;lt; sum &amp;lt; +infinite then we have to follow this algorithm:&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgZZmgeccl9LIW7tUlaGsRSWS2bATPO2G7598Fn81rgqVYJhXoE4pU6fBTVbRZSVbK-9YozXVaXauFqDak5AQdrSFX8nfPzDmZBLlBM9-_HIWBqFTg06YNF-0sPlaPmC-1PFZsv9UVyF7k7/s1600/2.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;292&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgZZmgeccl9LIW7tUlaGsRSWS2bATPO2G7598Fn81rgqVYJhXoE4pU6fBTVbRZSVbK-9YozXVaXauFqDak5AQdrSFX8nfPzDmZBLlBM9-_HIWBqFTg06YNF-0sPlaPmC-1PFZsv9UVyF7k7/s400/2.jpg&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
How we find the range of the index:&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhvTLPm4E6EBtoQw_WLwUrP7UwedUMk1u_U-QEKZJq-NS9Qu0ygj4z4MKzg5dJ1aNvnpkswgN_VHpcW8vBAavNKjPoQgdx_hOCexofk9Obl790Hg3R-isJAO-9RPLDgZDEtG2T7AZtFQhpX/s1600/3.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;265&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhvTLPm4E6EBtoQw_WLwUrP7UwedUMk1u_U-QEKZJq-NS9Qu0ygj4z4MKzg5dJ1aNvnpkswgN_VHpcW8vBAavNKjPoQgdx_hOCexofk9Obl790Hg3R-isJAO-9RPLDgZDEtG2T7AZtFQhpX/s400/3.jpg&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;Kadane&#39;s algorithm&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;b&gt;[1] Iterative Solution (here max_Index and min_index is not consider here] &lt;/b&gt;&lt;br /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style=&quot;background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;&lt;span style=&quot;color: #888888;&quot;&gt;/*&lt;/span&gt;
&lt;span style=&quot;color: #888888;&quot;&gt;Problem: Maximum subarray problem&lt;/span&gt;
&lt;span style=&quot;color: #888888;&quot;&gt;Ref: https://en.wikipedia.org/wiki/Maximum_subarray_problem&lt;/span&gt;
&lt;span style=&quot;color: #888888;&quot;&gt;Variable Defination&lt;/span&gt;
&lt;span style=&quot;color: #888888;&quot;&gt;max_e = maximum end here&lt;/span&gt;
&lt;span style=&quot;color: #888888;&quot;&gt;max_so = maximum so far&lt;/span&gt;
&lt;span style=&quot;color: #888888;&quot;&gt;input range INT_MIN &amp;lt; = xi &amp;lt; = INT_MAX&lt;/span&gt;
&lt;span style=&quot;color: #888888;&quot;&gt;*/&lt;/span&gt;
&lt;span style=&quot;color: #557799;&quot;&gt;#include&amp;lt;stdio.h&amp;gt;&lt;/span&gt;

&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;max&lt;/span&gt;(&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; a, &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; b){&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; (a &lt;span style=&quot;color: #333333;&quot;&gt;&amp;gt;&lt;/span&gt; b)&lt;span style=&quot;color: #333333;&quot;&gt;?&lt;/span&gt;a&lt;span style=&quot;color: #333333;&quot;&gt;:&lt;/span&gt;b;}

&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;main&lt;/span&gt;(){
   &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; a[] &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; {&lt;span style=&quot;color: #333333;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;2&lt;/span&gt;,&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;,&lt;span style=&quot;color: #333333;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;2&lt;/span&gt;,&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;,&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;,&lt;span style=&quot;color: #333333;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;,&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;2&lt;/span&gt;,&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;,&lt;span style=&quot;color: #333333;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;5&lt;/span&gt;};
   &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; N&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;9&lt;/span&gt;,i;
   &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; max_e,max_so;

   &lt;span style=&quot;color: #888888;&quot;&gt;/*Print Array*/&lt;/span&gt;
   printf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;Array: { &quot;&lt;/span&gt;);
   &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt; (i &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;; i&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt;N ; i&lt;span style=&quot;color: #333333;&quot;&gt;++&lt;/span&gt;)
            printf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;%d &quot;&lt;/span&gt;,a[i]);
   printf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;} &lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #666666; font-weight: bold;&quot;&gt;\n\n&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;&lt;/span&gt;);


   &lt;span style=&quot;color: #888888;&quot;&gt;/*Kadan&#39;s Algorithm*/&lt;/span&gt;
   max_e&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; max_so &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;a[&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;];
   &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt;(i&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;i &lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt;N;i&lt;span style=&quot;color: #333333;&quot;&gt;++&lt;/span&gt;){
       max_e &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; max(a[i], max_e &lt;span style=&quot;color: #333333;&quot;&gt;+&lt;/span&gt; a[i]);
       max_so &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; max(max_so, max_e);
   }


   printf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;Maximum Sum of Continuous Sub-Array: %d&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #666666; font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;&lt;/span&gt;,max_so);

  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;b&gt;[2] Recursive Method:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style=&quot;background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;&lt;span style=&quot;color: #888888;&quot;&gt;/*&lt;/span&gt;
&lt;span style=&quot;color: #888888;&quot;&gt;Variable Defination&lt;/span&gt;
&lt;span style=&quot;color: #888888;&quot;&gt;N = Number of Total Element&lt;/span&gt;
&lt;span style=&quot;color: #888888;&quot;&gt;a[] = array&lt;/span&gt;
&lt;span style=&quot;color: #888888;&quot;&gt;GS = Global Starting Index&lt;/span&gt;
&lt;span style=&quot;color: #888888;&quot;&gt;GE = Global End Index&lt;/span&gt;
&lt;span style=&quot;color: #888888;&quot;&gt;CMax = Current Maxumum Sum&lt;/span&gt;
&lt;span style=&quot;color: #888888;&quot;&gt;CSI = Current Start Index&lt;/span&gt;
&lt;span style=&quot;color: #888888;&quot;&gt;CEI = Current End Index&lt;/span&gt;
&lt;span style=&quot;color: #888888;&quot;&gt;Max = Final Maximum Number&lt;/span&gt;
&lt;span style=&quot;color: #888888;&quot;&gt;Ref: http://www.algorithmist.com/index.php/Kadane%27s_Algorithm&lt;/span&gt;
&lt;span style=&quot;color: #888888;&quot;&gt;*/&lt;/span&gt;
&lt;span style=&quot;color: #557799;&quot;&gt;#include&amp;lt;stdio.h&amp;gt;&lt;/span&gt;

&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; GS &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;, GE &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;

&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;Kadane&lt;/span&gt; (&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;a,&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; N,&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; Max){
    &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; CMax,CSI,CEI;

    CMax &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
    CSI &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;

    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt; (CEI &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;; CEI &lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt; N ; CEI&lt;span style=&quot;color: #333333;&quot;&gt;++&lt;/span&gt;){

        CMax &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; CMax &lt;span style=&quot;color: #333333;&quot;&gt;+&lt;/span&gt; a[CEI];

        &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;(CMax &lt;span style=&quot;color: #333333;&quot;&gt;&amp;gt;&lt;/span&gt; Max){
          GS  &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; CSI, GE &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; CEI;
          Max &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; Kadane(a,N,CMax);
        }

        &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;(CMax &lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;) {
            CMax &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
            CSI &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; CEI &lt;span style=&quot;color: #333333;&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;
        }
    }
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; Max;
}


&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;main&lt;/span&gt;(){
    &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; a[] &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; {&lt;span style=&quot;color: #333333;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;2&lt;/span&gt;,&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;,&lt;span style=&quot;color: #333333;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;3&lt;/span&gt;,&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;4&lt;/span&gt;,&lt;span style=&quot;color: #333333;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;,&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;2&lt;/span&gt;,&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;,&lt;span style=&quot;color: #333333;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;5&lt;/span&gt;,&lt;span style=&quot;color: #333333;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;4&lt;/span&gt;};
    &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; N&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;9&lt;/span&gt;,Max;
    &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; i;

    Max &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; Kadane(a,N,a[&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;]);

    &lt;span style=&quot;color: #888888;&quot;&gt;/*Display*/&lt;/span&gt;
    printf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;Array [0-%d]: { &quot;&lt;/span&gt;,N&lt;span style=&quot;color: #333333;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;);
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt; (i &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;; i&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt;N ; i&lt;span style=&quot;color: #333333;&quot;&gt;++&lt;/span&gt;)
            printf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;%d &quot;&lt;/span&gt;,a[i]);
    printf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;} &lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #666666; font-weight: bold;&quot;&gt;\n\n&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;&lt;/span&gt;);
    printf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;Max Sum: %d, Start Index: %d End Index: %d &lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #666666; font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;&lt;/span&gt;,Max,GS,GE);

    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;b&gt;[3] Iterative Method:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style=&quot;background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;&lt;span style=&quot;color: #888888;&quot;&gt;/*&lt;/span&gt;
&lt;span style=&quot;color: #888888;&quot;&gt;Variable Defination&lt;/span&gt;
&lt;span style=&quot;color: #888888;&quot;&gt;N = Number of Total Element&lt;/span&gt;
&lt;span style=&quot;color: #888888;&quot;&gt;a[] = array&lt;/span&gt;
&lt;span style=&quot;color: #888888;&quot;&gt;S = Starting Index&lt;/span&gt;
&lt;span style=&quot;color: #888888;&quot;&gt;E = End Index&lt;/span&gt;
&lt;span style=&quot;color: #888888;&quot;&gt;CMax = Current Maxumum Sum&lt;/span&gt;
&lt;span style=&quot;color: #888888;&quot;&gt;CSI = Current Start Index&lt;/span&gt;
&lt;span style=&quot;color: #888888;&quot;&gt;CEI = Current End Index&lt;/span&gt;
&lt;span style=&quot;color: #888888;&quot;&gt;Max = Final Maximum Number&lt;/span&gt;
&lt;span style=&quot;color: #888888;&quot;&gt;Ref: http://alexeigor.wikidot.com/kadane&lt;/span&gt;
&lt;span style=&quot;color: #888888;&quot;&gt;*/&lt;/span&gt;
&lt;span style=&quot;color: #557799;&quot;&gt;#include&amp;lt;stdio.h&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #557799;&quot;&gt;#include&amp;lt;limits.h&amp;gt;&lt;/span&gt;


&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;main&lt;/span&gt;(){
   &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; a[] &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; {&lt;span style=&quot;color: #333333;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;2&lt;/span&gt;,&lt;span style=&quot;color: #333333;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;,&lt;span style=&quot;color: #333333;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;3&lt;/span&gt;,&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;4&lt;/span&gt;,&lt;span style=&quot;color: #333333;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;,&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;2&lt;/span&gt;,&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;,&lt;span style=&quot;color: #333333;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;5&lt;/span&gt;,&lt;span style=&quot;color: #333333;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;4&lt;/span&gt;};

   &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; N&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;9&lt;/span&gt;,i;

   &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; Max&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;31&lt;/span&gt;,S&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;,E&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;

   &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; Cmax&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;,CSI&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
   &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; CEI;


   printf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;Array: { &quot;&lt;/span&gt;);
   &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt; (i &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;; i&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt;N ; i&lt;span style=&quot;color: #333333;&quot;&gt;++&lt;/span&gt;)
            printf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;%d &quot;&lt;/span&gt;,a[i]);
   printf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;} &lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #666666; font-weight: bold;&quot;&gt;\n\n&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;&lt;/span&gt;);



   &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt;(CEI &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;; CEI &lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt; N; CEI&lt;span style=&quot;color: #333333;&quot;&gt;++&lt;/span&gt;){
     Cmax &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; Cmax &lt;span style=&quot;color: #333333;&quot;&gt;+&lt;/span&gt; a[CEI];
     &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;( Cmax &lt;span style=&quot;color: #333333;&quot;&gt;&amp;gt;&lt;/span&gt; Max){
        Max &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; Cmax;
        E &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; CEI;
        S &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; CSI;
      }
      &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;( Cmax &lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt; ){
         Cmax &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
         CSI &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; CEI &lt;span style=&quot;color: #333333;&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;
      }
    }

    printf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;%d %d-%d&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #666666; font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;&lt;/span&gt;,Max,S,E);

   &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;b&gt;[4] My Designed Algorithm for 1D array O(N)[Nice to Play with it]:
&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
[S = 0 , E = N-1]&lt;br /&gt;
1. Remove starting and End Minus value (SET S and E)&lt;br /&gt;
2. if all are positve then sum = ADD(all Element) break&lt;br /&gt;
3. if all are negative then sum = MAX(all Element) break&lt;br /&gt;
4. For i = S to E begin&lt;br /&gt;
5.&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sum = 0;&lt;br /&gt;
6.&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if a[i] &amp;lt; 0 then a[i] = sum + a[i],sum = 0;&lt;br /&gt;
7.&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else sum = sum+a[i];&lt;br /&gt;
8. end&lt;br /&gt;
9. to be continue from 1 to 8 untile S &amp;gt; E&lt;br /&gt;
10. Return sum;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example-1:&lt;br /&gt;
----------&lt;br /&gt;
&lt;br /&gt;
{-2,1,-1,1,1,-1,2,1,-5}; -- &amp;gt; S = 0 , E =8&lt;br /&gt;
&lt;br /&gt;
{-2,1,-1,1,1,-1,2,1,-5}; -- &amp;gt; S = 1, E = 7&lt;br /&gt;
&lt;br /&gt;
{-2,0,(1+-1),0,0,(-1+1+1),0,(2+1),-5}&lt;br /&gt;
{-2,0,0,0,0,1,0,3,-5}&amp;nbsp; --- &amp;gt; S = 3, E = 7&lt;br /&gt;
&lt;br /&gt;
sum = Add (a, 3, 7) = 4&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example - 2:&lt;br /&gt;
==============&lt;br /&gt;
{-2,1,-2,1,1,-1,2,1,-5};-- &amp;gt; S = 0 , E =8&lt;br /&gt;
&lt;br /&gt;
{-2,1,-2,1,1,-1,2,1,-5};-- &amp;gt; S = 1 , E =7&lt;br /&gt;
&lt;br /&gt;
{-2,0,(-2+1),0,0,(-1+1+1),0,(2+1),-5};-- &amp;gt; S = 3 , E =7&lt;br /&gt;
{-2,0,-1,0,0,1,0,3,-5};-- &amp;gt; S = 3 , E =7&lt;br /&gt;
{-2,0,-1,0,0,1,0,3,-5};-- &amp;gt; S = 4 , E =7&lt;br /&gt;
&lt;br /&gt;
sum = ADD (a,4,7) = 4&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example - 3&lt;br /&gt;
============&lt;br /&gt;
{-2,1,-3,4,-1,2,1,-5,-4}; --&amp;nbsp; &amp;gt; S =0 E =8&lt;br /&gt;
&lt;br /&gt;
{-2,1,-3,4,-1,2,1,-5,-4}; -- &amp;gt; S =1 E =6&lt;br /&gt;
&lt;br /&gt;
{-2,0,-2,0,3,0,3,-5,-4}; -- &amp;gt; S =3 E =6&lt;br /&gt;
&lt;br /&gt;
{-2,0,-2,0,3,0,3,-5,-4}; -- &amp;gt; S =4 E =6&lt;br /&gt;
&lt;br /&gt;
sum = ADD (a,4,6) = 6&lt;br /&gt;
&lt;br /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style=&quot;background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;&lt;span style=&quot;color: #557799;&quot;&gt;#include&amp;lt;stdio.h&amp;gt;&lt;/span&gt;

&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;max&lt;/span&gt;(&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;a,&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; s,&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; e){
    &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; m &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; a[s];
    &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; i;
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt; (i&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;s&lt;span style=&quot;color: #333333;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;i&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;=&lt;/span&gt;e;i&lt;span style=&quot;color: #333333;&quot;&gt;++&lt;/span&gt;)
        &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; (m&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt;a[i]) m &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; a[i];

    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; m;
}

&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;ADD&lt;/span&gt;(&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;a,&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; s,&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; e){
    &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; sm &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;,i;
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt; (i&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;s;i&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;=&lt;/span&gt;e;i&lt;span style=&quot;color: #333333;&quot;&gt;++&lt;/span&gt;) sm&lt;span style=&quot;color: #333333;&quot;&gt;+=&lt;/span&gt;a[i];

    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; sm;
}

&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;main&lt;/span&gt;(){
   &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; a[] &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; {&lt;span style=&quot;color: #333333;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;2&lt;/span&gt;,&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;,&lt;span style=&quot;color: #333333;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;3&lt;/span&gt;,&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;4&lt;/span&gt;,&lt;span style=&quot;color: #333333;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;,&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;2&lt;/span&gt;,&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;,&lt;span style=&quot;color: #333333;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;5&lt;/span&gt;,&lt;span style=&quot;color: #333333;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;4&lt;/span&gt;};


   &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; N&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;9&lt;/span&gt;;
   &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; i,sum,flag;

   &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; S &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;, E &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; N&lt;span style=&quot;color: #333333;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;,PS &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;,PE &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;

   &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt; (S&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt;E){
          &lt;span style=&quot;color: #888888;&quot;&gt;/*Eliminate starting minus value*/&lt;/span&gt;
          i &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; S;
          PS &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; S;
          &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt;(a[i]&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;i&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;=&lt;/span&gt;E) i &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; i&lt;span style=&quot;color: #333333;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;
          S &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; i;

          printf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;S: %d&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #666666; font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;&lt;/span&gt;,S);
          &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; (S&lt;span style=&quot;color: #333333;&quot;&gt;&amp;gt;&lt;/span&gt;E){ sum &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; max(a,PS,E); &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;break&lt;/span&gt;;}



          &lt;span style=&quot;color: #888888;&quot;&gt;/*Eliminate End minus value*/&lt;/span&gt;
          i&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; E;
          PE &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; E;
          &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt;(a[i]&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: #333333;&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; i&lt;span style=&quot;color: #333333;&quot;&gt;&amp;gt;&lt;/span&gt;S) i &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; i&lt;span style=&quot;color: #333333;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;
          E &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; i;
          printf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;E: %d&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #666666; font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;&lt;/span&gt;,E);

          &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;(S &lt;span style=&quot;color: #333333;&quot;&gt;==&lt;/span&gt; PS &lt;span style=&quot;color: #333333;&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; E &lt;span style=&quot;color: #333333;&quot;&gt;==&lt;/span&gt; PE){ sum &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; ADD(a,S,PE); &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;break&lt;/span&gt;;}


          &lt;span style=&quot;color: #888888;&quot;&gt;/*Process*/&lt;/span&gt;
          sum &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
          flag &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
          &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt; (i &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; S; i&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;=&lt;/span&gt;E; i&lt;span style=&quot;color: #333333;&quot;&gt;++&lt;/span&gt;){
             &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; (a[i]&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;){
                   a[i] &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; sum&lt;span style=&quot;color: #333333;&quot;&gt;+&lt;/span&gt;a[i];
                   sum &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
                   &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; (flag &lt;span style=&quot;color: #333333;&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;){S &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; i;flag &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;}
             }
             &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt;{
                sum &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; sum &lt;span style=&quot;color: #333333;&quot;&gt;+&lt;/span&gt; a[i];
                a[i] &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
             }
          }

          &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; (flag &lt;span style=&quot;color: #333333;&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;)&lt;span style=&quot;color: #888888;&quot;&gt;/*All are positive value*/&lt;/span&gt;
               S &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; i;
          a[E] &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; sum;

          printf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;S: %d&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #666666; font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;&lt;/span&gt;,S);

          &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt; (i &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;; i&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt;N ; i&lt;span style=&quot;color: #333333;&quot;&gt;++&lt;/span&gt;)
            printf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;%d &quot;&lt;/span&gt;,a[i]);
          printf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #666666; font-weight: bold;&quot;&gt;\n\n&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;&lt;/span&gt;);
   }

   printf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;Sum: %d&quot;&lt;/span&gt;,sum);
   &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
[5] Kadane&#39;s 2D algorithm O(N3)&lt;br /&gt;
&lt;br /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style=&quot;background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;&lt;span style=&quot;color: #557799;&quot;&gt;#include &amp;lt;iostream&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #557799;&quot;&gt;#include &amp;lt;algorithm&amp;gt;&lt;/span&gt;
 
using namespace std;
 
&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;main&lt;/span&gt;( &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;void&lt;/span&gt; )
{
    &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; N;
    &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; t &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
    &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; a[&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;100&lt;/span&gt;][&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;100&lt;/span&gt;];
    &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; pr[&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;100&lt;/span&gt;];
    &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; S &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;31&lt;/span&gt;, s &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;, k, l, x1 &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;,x2 &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;,y1 &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;,y2 &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;,j;
 
    cin &lt;span style=&quot;color: #333333;&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; N;
 
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt;( &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; i &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;; i &lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt; N; i&lt;span style=&quot;color: #333333;&quot;&gt;++&lt;/span&gt;)
        &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt;( j &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;; j &lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt; N; j&lt;span style=&quot;color: #333333;&quot;&gt;++&lt;/span&gt;)
            cin &lt;span style=&quot;color: #333333;&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; a[i][j];
 
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt;( &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; z &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;; z &lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt; N; z&lt;span style=&quot;color: #333333;&quot;&gt;++&lt;/span&gt;)
    {
        &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt;(&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; i &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;; i &lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt; N; i&lt;span style=&quot;color: #333333;&quot;&gt;++&lt;/span&gt;) pr[i] &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
 
        &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt;(&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; x &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; z; x &lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt; N; x&lt;span style=&quot;color: #333333;&quot;&gt;++&lt;/span&gt;)
        {
            t &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
            s &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;31&lt;/span&gt;;
            j &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
            k &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;; l &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
            &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt;(&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; i &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;; i &lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt; N; i&lt;span style=&quot;color: #333333;&quot;&gt;++&lt;/span&gt;)
            {
                pr[i] &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; pr[i] &lt;span style=&quot;color: #333333;&quot;&gt;+&lt;/span&gt; a[x][i];
                t &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; t &lt;span style=&quot;color: #333333;&quot;&gt;+&lt;/span&gt; pr[i];
                &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;( t &lt;span style=&quot;color: #333333;&quot;&gt;&amp;gt;&lt;/span&gt; s)
                {
                    s &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; t;
                    k &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; i;
                    l &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; j;
                }
                &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;( t &lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt; )
                {
                    t &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
                    j &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; i &lt;span style=&quot;color: #333333;&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;;
                }
            }
            &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;( s &lt;span style=&quot;color: #333333;&quot;&gt;&amp;gt;&lt;/span&gt; S)
            {
                S &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; s;
                x1 &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; x;
                y1 &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; k;
                x2 &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; z;
                y2 &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; l;
            }
        }
    }
 
    cout &lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; x1 &lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot; &quot;&lt;/span&gt; &lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; y1 &lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot; &quot;&lt;/span&gt; &lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; x2 &lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot; &quot;&lt;/span&gt;  &lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; y2 &lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; endl;
    cout &lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; S;
 
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
}

&lt;span style=&quot;color: #997700; font-weight: bold;&quot;&gt;Ref:&lt;/span&gt; http&lt;span style=&quot;color: #333333;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #888888;&quot;&gt;//alexeigor.wikidot.com/kadane&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://rizoantoufiq.blogspot.com/feeds/1444897694881225601/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://rizoantoufiq.blogspot.com/2015/10/maximum-subarray-problem.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1124334728283106452/posts/default/1444897694881225601'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1124334728283106452/posts/default/1444897694881225601'/><link rel='alternate' type='text/html' href='http://rizoantoufiq.blogspot.com/2015/10/maximum-subarray-problem.html' title='Maximum Subarray Problem'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEirA_xhBWVmlFR-JTItiy8Sd8V2KM3uyimwrffoi1QbQl5QYKFmdHXaaEg1P_SV_keaEMLEmGBE0pdTD2cGwoxh8EwXuuh8TlJN2ApRwerm80zZDKaGEVKxNOcItqTKLCDvR5HJYBLzv1V8/s72-c/1.jpg" height="72" width="72"/><thr:total>0</thr:total></entry></feed>