<?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: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-6294663875929591018</id><updated>2012-01-25T02:14:21.951-08:00</updated><category term='flash'/><category term='child'/><category term='postgres'/><category term='solution'/><category term='earth'/><category term='bigendian'/><category term='books'/><category term='bmi'/><category term='as3'/><category term='development'/><category term='tms'/><category term='low-level'/><category term='adobe'/><category term='lion'/><category term='query'/><category term='array'/><category term='cocoa'/><category term='motivation'/><category term='actionscript3'/><category term='objective-c'/><category term='practice'/><category term='reverse an integer'/><category term='csm'/><category term='format specifier'/><category term='developer'/><category term='body mass index'/><category term='transform'/><category term='training'/><category term='offsetoff'/><category term='table'/><category term='XOR'/><category term='psgtech'/><category term='java'/><category term='binary tree'/><category term='enqueue'/><category term='geo'/><category term='pointer'/><category term='reconstruction'/><category term='root'/><category term='ui'/><category term='output'/><category term='palindrome'/><category term='interview'/><category term='college project'/><category term='find kth from last'/><category term='mac'/><category term='project euler'/><category term='design'/><category term='oracle10g'/><category term='certified scrum master'/><category term='TRIE'/><category term='mini project'/><category term='merge sort'/><category term='race'/><category term='puts'/><category term='google'/><category term='ruby'/><category term='space'/><category term='return'/><category term='loop'/><category term='manipulation'/><category term='ADT'/><category term='perl'/><category term='application'/><category term='ideal'/><category term='osx'/><category term='least common ancestor'/><category term='node'/><category term='full-adder'/><category term='reverse a number'/><category term='cs5'/><category term='OOAD'/><category term='game of life'/><category term='row'/><category term='days'/><category term='codejam'/><category term='conways'/><category term='ieee'/><category term='knowledge'/><category term='memcpy'/><category term='completeness'/><category term='program'/><category term='O(logn)'/><category term='memmove'/><category term='prime numbers'/><category term='queue'/><category term='rcp'/><category term='ruby-on-rails'/><category term='tmsc5402'/><category term='quick sort'/><category term='scrum'/><category term='data structures'/><category term='sql'/><category term='words'/><category term='euler'/><category term='spatial'/><category term='linked list'/><category term='littleEndian'/><category term='numbers'/><category term='leaf'/><category term='questions'/><category term='recursively'/><category term='classic'/><category term='problem'/><category term='calculator'/><category term='yield'/><category term='block'/><category term='swing'/><category term='C'/><category term='puzzle'/><category term='sdo_geometry'/><category term='windows 7'/><category term='encryption'/><category term='iphone'/><category term='depth first search'/><category term='window'/><category term='O(n)'/><category term='binary search tree'/><category term='worst'/><category term='eclipse'/><category term='versatility'/><category term='xp'/><category term='reverse'/><category term='15 game'/><category term='offset'/><category term='connect by prior'/><category term='abstract'/><category term='helloworld'/><category term='c5402'/><category term='select'/><category term='maths'/><category term='sequence'/><category term='macros'/><category term='language'/><category term='thoughtworks'/><category term='bubble sort'/><category term='algorithm'/><category term='school'/><category term='game'/><category term='fibonacci'/><category term='printf'/><category term='date comparison'/><category term='swt'/><category term='endian'/><category term='oracle'/><category term='awt'/><category term='xcode'/><category term='coding'/><category term='single linked list'/><category term='tms 320 series'/><category term='fun'/><category term='vector graphics'/><category term='cat'/><category term='fibonacci series'/><category term='initial'/><category term='vista'/><category term='OS'/><category term='string manipulation'/><category term='xcode4'/><category term='map'/><category term='C++'/><category term='string'/><category term='tms 320c'/><category term='delete'/><category term='agile'/><category term='mapviewer'/><category term='trees'/><category term='amazon'/><category term='OR'/><category term='in-order'/><category term='right'/><category term='windows'/><category term='tweak'/><category term='traversal'/><category term='sentence'/><category term='operating system'/><category term='linux'/><category term='progress software'/><category term='pre-order'/><category term='old'/><category term='worst case'/><category term='tutorial'/><category term='trigger'/><category term='sorting'/><category term='tricky'/><category term='learn'/><category term='singly linked list'/><category term='life'/><category term='stack'/><category term='off-topic'/><category term='matrix'/><category term='clock'/><category term='dequeue'/><category term='search'/><category term='dsp'/><category term='versus'/><category term='big O'/><title type='text'>Data Structures in Java/C++</title><subtitle type='html'>Comprehensive code coverage for data structures in java and c++</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://www.technicalypto.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default?start-index=101&amp;max-results=100'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>120</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-7690449753093972396</id><published>2011-12-13T07:59:00.000-08:00</published><updated>2011-12-13T08:07:31.981-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='problem'/><category scheme='http://www.blogger.com/atom/ns#' term='traversal'/><category scheme='http://www.blogger.com/atom/ns#' term='reconstruction'/><category scheme='http://www.blogger.com/atom/ns#' term='pre-order'/><category scheme='http://www.blogger.com/atom/ns#' term='interview'/><category scheme='http://www.blogger.com/atom/ns#' term='algorithm'/><category scheme='http://www.blogger.com/atom/ns#' term='in-order'/><category scheme='http://www.blogger.com/atom/ns#' term='coding'/><category scheme='http://www.blogger.com/atom/ns#' term='binary tree'/><title type='text'>Reconstruct a binary tree given its Inorder and Preorder traversals in Java</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;This is one of the interesting problems that I encountered recently. Given two sequences one being an in-order traversed sequence and other being a pre-ordered traversed sequence, a binary tree has to be reconstructed out of it.&lt;br /&gt;&lt;br /&gt;This is theoretically possible. This problem can be cracked if we form a common pattern between these two traversed values. In a pre-order traversal, the root element will always be the first element. This element when searched in an in-order traversal will have all elements to the left on the left side of the array and all elements to the right on the right side of the array.&lt;br /&gt;&lt;br /&gt;Applying this solution recursively will give the binary tree for us. I have included a Swing UI so as to visualize the output easily.&lt;br /&gt;&lt;br /&gt;Example,&lt;br /&gt;Pre Order Sequence = A B D E C F G H I&lt;br /&gt;In Order Sequence = D B E A F C H G I&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-l_LnfNxVpTg/Tud1tZvfKjI/AAAAAAAACXA/LGDkjQEbH1U/s1600/Screenshot-BinaryTree.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-l_LnfNxVpTg/Tud1tZvfKjI/AAAAAAAACXA/LGDkjQEbH1U/s1600/Screenshot-BinaryTree.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;I tweaked a bit from this &lt;a href="http://www.uta.fi/%7Ejl/pguibook/other.html"&gt;source&lt;/a&gt; for UI.&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;pre class="brush:java"&gt;package dsa.btree;&lt;br /&gt;&lt;br /&gt;import javax.swing.JFrame;&lt;br /&gt;&lt;br /&gt;public class PreOrderInOrderReconstructor {&lt;br /&gt; public static void main(String[] args) {&lt;br /&gt;  PreOrderInOrderReconstructor pir = new PreOrderInOrderReconstructor();&lt;br /&gt;  String[] preOrderSequence = { "A", "B", "D", "E", "C", "F", "G", "H", "I" };&lt;br /&gt;  String[] inOrderSequence = { "D", "B", "E", "A", "F", "C", "H", "G", "I" };&lt;br /&gt;  Node root = pir.reConstruct(preOrderSequence, 0,&lt;br /&gt;    preOrderSequence.length - 1, inOrderSequence, 0,&lt;br /&gt;    inOrderSequence.length - 1);&lt;br /&gt;  JFrame f = new JFrame("BinaryTree");&lt;br /&gt;  f.getContentPane().add(new BinaryTreeView(root));&lt;br /&gt;  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);&lt;br /&gt;  f.setBounds(50, 50, 400, 400);&lt;br /&gt;  f.setVisible(true);&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; private Node reConstruct(String[] preOrderSequence, int pMin, int pMax,&lt;br /&gt;   String[] inOrderSequence, int iMin, int iMax) {&lt;br /&gt;  if ((pMax - pMin) &amp;lt; 0) {&lt;br /&gt;   return null;&lt;br /&gt;  }&lt;br /&gt;  if ((pMax - pMin) == 0) {&lt;br /&gt;   return new Node(preOrderSequence[pMin]);&lt;br /&gt;  }&lt;br /&gt;  Node n = new Node(preOrderSequence[pMin]);&lt;br /&gt;  int inorderPos = findInOrderPos(inOrderSequence,&lt;br /&gt;    preOrderSequence[pMin], iMin, iMax);&lt;br /&gt;  int leftBias = inorderPos - iMin;&lt;br /&gt;  n.addLeft(reConstruct(preOrderSequence, pMin + 1, pMin + leftBias,&lt;br /&gt;    inOrderSequence, iMin, iMin + leftBias - 1));&lt;br /&gt;  n.addRight(reConstruct(preOrderSequence, pMin + leftBias + 1, pMax,&lt;br /&gt;    inOrderSequence, iMin + leftBias + 1, iMax));&lt;br /&gt;  return n;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; private int findInOrderPos(String[] inOrderSequence, String string,&lt;br /&gt;   int minIndex, int maxIndex) {&lt;br /&gt;  for (int i = minIndex; i &amp;lt; maxIndex; i++) {&lt;br /&gt;   if (inOrderSequence[i].equals(string)) {&lt;br /&gt;    return i;&lt;br /&gt;   }&lt;br /&gt;  }&lt;br /&gt;  return -1;&lt;br /&gt; }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;The Node structure&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:java"&gt;package dsa.btree;&lt;br /&gt;&lt;br /&gt;/**&lt;br /&gt; * A class representing one node of the tree.&lt;br /&gt; */&lt;br /&gt;public class Node {&lt;br /&gt; private String content = null;&lt;br /&gt; private Node left = null;&lt;br /&gt; private Node right = null;&lt;br /&gt; &lt;br /&gt; public Node(String c) {&lt;br /&gt;  this.content = c;&lt;br /&gt; }&lt;br /&gt; &lt;br /&gt; public String getContent() { return content; }&lt;br /&gt; &lt;br /&gt; public Node getLeft() { return left; }&lt;br /&gt; &lt;br /&gt; public Node setLeft(String content) {&lt;br /&gt;  left = new Node(content);&lt;br /&gt;  return left;&lt;br /&gt; }&lt;br /&gt; &lt;br /&gt; public Node getRight() { return right; }&lt;br /&gt; &lt;br /&gt; public void addRight(Node reConstruct) {&lt;br /&gt;  this.right = reConstruct;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; public void addLeft(Node reConstruct) {&lt;br /&gt;  this.left = reConstruct;&lt;br /&gt; }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;The code for UI&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:java"&gt;package dsa.btree;&lt;br /&gt;&lt;br /&gt;import java.awt.Dimension;&lt;br /&gt;import java.awt.FontMetrics;&lt;br /&gt;import java.awt.Graphics;&lt;br /&gt;import java.awt.Graphics2D;&lt;br /&gt;import java.awt.Rectangle;&lt;br /&gt;import java.util.HashMap;&lt;br /&gt;import javax.swing.JPanel;&lt;br /&gt;&lt;br /&gt;public class BinaryTreeView extends JPanel {&lt;br /&gt; &lt;br /&gt; private static final long serialVersionUID = 1L;&lt;br /&gt; private Node root = null;&lt;br /&gt; private HashMap&amp;lt;Node, Rectangle&amp;gt; nodeLocations = null;&lt;br /&gt; private HashMap&amp;lt;Node, Dimension&amp;gt; subtreeSizes = null;&lt;br /&gt; private boolean dirty = true;&lt;br /&gt; private int parent2child = 20, child2child = 30;&lt;br /&gt; &lt;br /&gt; private Dimension empty = new Dimension(0, 0);&lt;br /&gt; private FontMetrics fm = null;&lt;br /&gt; &lt;br /&gt; public BinaryTreeView(Node root) {&lt;br /&gt;  this.root = root;&lt;br /&gt;  nodeLocations = new HashMap&amp;lt;Node, Rectangle&amp;gt;();&lt;br /&gt;  subtreeSizes = new HashMap&amp;lt;Node, Dimension&amp;gt;();&lt;br /&gt; }&lt;br /&gt; &lt;br /&gt; private void calculateLocations() {&lt;br /&gt;  nodeLocations.clear();&lt;br /&gt;  subtreeSizes.clear();&lt;br /&gt;  if (root != null) {&lt;br /&gt;   calculateSubtreeSize(root);&lt;br /&gt;   calculateLocation(root, Integer.MAX_VALUE, Integer.MAX_VALUE, 0);&lt;br /&gt;  }&lt;br /&gt; }&lt;br /&gt; &lt;br /&gt; private Dimension calculateSubtreeSize(Node n) {&lt;br /&gt;  if (n == null) return new Dimension(0, 0);&lt;br /&gt;  Dimension ld = calculateSubtreeSize(n.getLeft());&lt;br /&gt;  Dimension rd = calculateSubtreeSize(n.getRight());&lt;br /&gt;  int h = fm.getHeight() + parent2child + Math.max(ld.height, rd.height);&lt;br /&gt;  int w = ld.width + child2child + rd.width;&lt;br /&gt;  Dimension d = new Dimension(w, h);&lt;br /&gt;  subtreeSizes.put(n, d);&lt;br /&gt;  return d;&lt;br /&gt; }&lt;br /&gt; &lt;br /&gt; private void calculateLocation(Node n, int left, int right, int top) {&lt;br /&gt;  if (n == null) return;&lt;br /&gt;  Dimension ld = (Dimension) subtreeSizes.get(n.getLeft());&lt;br /&gt;  if (ld == null) ld = empty;&lt;br /&gt;  Dimension rd = (Dimension) subtreeSizes.get(n.getRight());&lt;br /&gt;  if (rd == null) rd = empty;&lt;br /&gt;  int center = 0;&lt;br /&gt;  if (right != Integer.MAX_VALUE)&lt;br /&gt;   center = right - rd.width - child2child/2;&lt;br /&gt;  else if (left != Integer.MAX_VALUE)&lt;br /&gt;   center = left + ld.width + child2child/2;&lt;br /&gt;  int width = fm.stringWidth(n.getContent().toString());&lt;br /&gt;  Rectangle r = new Rectangle(center - width/2 - 3, top, width + 6, fm.getHeight());&lt;br /&gt;  nodeLocations.put(n, r);&lt;br /&gt;  calculateLocation(n.getLeft(), Integer.MAX_VALUE, center - child2child/2, top + fm.getHeight() + parent2child);&lt;br /&gt;  calculateLocation(n.getRight(), center + child2child/2, Integer.MAX_VALUE, top + fm.getHeight() + parent2child);&lt;br /&gt; }&lt;br /&gt; &lt;br /&gt; private void drawTree(Graphics2D g, Node n, int px, int py, int yoffs) {&lt;br /&gt;  if (n == null) return;&lt;br /&gt;  Rectangle r = (Rectangle) nodeLocations.get(n);&lt;br /&gt;  g.draw(r);&lt;br /&gt;  g.drawString(n.getContent().toString(), r.x + 3, r.y + yoffs);&lt;br /&gt;  if (px != Integer.MAX_VALUE)&lt;br /&gt;   g.drawLine(px, py, r.x + r.width/2, r.y);&lt;br /&gt;  drawTree(g, n.getLeft(), r.x + r.width/2, r.y + r.height, yoffs);&lt;br /&gt;  drawTree(g, n.getRight(), r.x + r.width/2, r.y + r.height, yoffs);&lt;br /&gt; }&lt;br /&gt; &lt;br /&gt; public void paint(Graphics g) {&lt;br /&gt;  super.paint(g);&lt;br /&gt;  fm = g.getFontMetrics();&lt;br /&gt;  if (dirty) {&lt;br /&gt;   calculateLocations();&lt;br /&gt;   dirty = false;&lt;br /&gt;  }&lt;br /&gt;  Graphics2D g2d = (Graphics2D) g;&lt;br /&gt;  g2d.translate(getWidth() / 2, parent2child);&lt;br /&gt;  drawTree(g2d, root, Integer.MAX_VALUE, Integer.MAX_VALUE, fm.getLeading() + fm.getAscent());&lt;br /&gt;  fm = null;&lt;br /&gt; }&lt;br /&gt; &lt;br /&gt;}&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;Hope you learnt something today along with me.&lt;br /&gt;&lt;br /&gt;Cheers!&lt;br /&gt;Braga&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-7690449753093972396?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/7690449753093972396/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=7690449753093972396' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/7690449753093972396'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/7690449753093972396'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2011/12/reconstruct-binary-tree-given-its.html' title='Reconstruct a binary tree given its Inorder and Preorder traversals in Java'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/-l_LnfNxVpTg/Tud1tZvfKjI/AAAAAAAACXA/LGDkjQEbH1U/s72-c/Screenshot-BinaryTree.png' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-6726999071160953365</id><published>2011-11-27T04:36:00.000-08:00</published><updated>2011-11-27T04:38:50.933-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='array'/><category scheme='http://www.blogger.com/atom/ns#' term='data structures'/><category scheme='http://www.blogger.com/atom/ns#' term='problem'/><category scheme='http://www.blogger.com/atom/ns#' term='O(n)'/><category scheme='http://www.blogger.com/atom/ns#' term='interview'/><category scheme='http://www.blogger.com/atom/ns#' term='solution'/><category scheme='http://www.blogger.com/atom/ns#' term='O(logn)'/><category scheme='http://www.blogger.com/atom/ns#' term='coding'/><title type='text'>Given a sorted array, find k in least possible time in Java</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;Given a sorted array and a value k, write a program to find whether that number is present in that array or not.&lt;br /&gt;&lt;br /&gt;The first solution that comes to the mind for this problem is to traverse through the entire array and find whether it is having the value k and return true or false. This takes O(n) running time. But then, we are presented with a hint that the input array is sorted (lets assume in ascending order). This problem can be attacked by doing a divide and rule analysis.&lt;br /&gt;&lt;br /&gt;This can be done recursively. See whether k is inbetween the last and first element in the array. If it is, then divide the array into two and repeat the same. If its not, simply return false. Yes, its as simple as that. Following is the java code for the same.&lt;/div&gt;&lt;br /&gt;&lt;pre class="brush:java"&gt;package dsa.arrays;&lt;br /&gt;&lt;br /&gt;public class SortedFindK {&lt;br /&gt; &lt;br /&gt; public static void main(String[] args) {&lt;br /&gt;  SortedFindK ssfm = new SortedFindK();&lt;br /&gt;  ssfm.findKOfArray(null, 7);&lt;br /&gt;  ssfm.findKOfArray(new int[]{1,4,6,7,45,78,88}, 45);&lt;br /&gt;  ssfm.findKOfArray(new int[]{1,2,3,4,5,6,8,9,14}, 99);&lt;br /&gt;  ssfm.findKOfArray(new int[]{-1,0,1,2,3,4,5}, 2);&lt;br /&gt;  ssfm.findKOfArray(new int[]{1,2,3,4,5}, 5);&lt;br /&gt;  ssfm.findKOfArray(new int[]{}, 3);&lt;br /&gt;  ssfm.findKOfArray(new int[]{1}, 9);&lt;br /&gt; }&lt;br /&gt;  &lt;br /&gt; public void findKOfArray(int[] csArray, int k){&lt;br /&gt;  if(csArray == null || csArray.length == 0) { &lt;br /&gt;   System.out.print(&amp;quot;\nHave some dignity. Send a valid array with at least 1 element&amp;quot;);&lt;br /&gt;   return;&lt;br /&gt;  }&lt;br /&gt;  int length = csArray.length;&lt;br /&gt;  System.out.print(&amp;quot;\n&amp;quot;+findKRecursively(csArray, 0 , length-1, k));&lt;br /&gt; }&lt;br /&gt; &lt;br /&gt; public boolean findKRecursively (int[] arr, int start, int end, int k){&lt;br /&gt;  if(arr[start]==k || arr[end]==k){&lt;br /&gt;   return true;&lt;br /&gt;  }&lt;br /&gt;  if(k &amp;gt; arr[start] &amp;amp;&amp;amp; k &amp;lt; arr[end]){&lt;br /&gt;   if(k &amp;lt;= arr[end/2]){&lt;br /&gt;    return findKRecursively(arr, start, end/2, k);&lt;br /&gt;   }else{&lt;br /&gt;    return findKRecursively(arr, end/2 + 1, end, k);&lt;br /&gt;   }&lt;br /&gt;  }else{&lt;br /&gt;   return false;&lt;br /&gt;  }&lt;br /&gt; }&lt;br /&gt; &lt;br /&gt;}&lt;br /&gt;/*&lt;br /&gt;Have some dignity. Send a valid array with at least 1 element&lt;br /&gt;true&lt;br /&gt;false&lt;br /&gt;true&lt;br /&gt;true&lt;br /&gt;Have some dignity. Send a valid array with at least 1 element&lt;br /&gt;false&lt;br /&gt;*/&lt;/pre&gt;&lt;br /&gt;Leave some comments if you have anything to say. &lt;br /&gt;&lt;br /&gt;Cheers!&lt;br /&gt;Braga&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-6726999071160953365?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/6726999071160953365/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=6726999071160953365' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/6726999071160953365'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/6726999071160953365'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2011/11/given-sorted-array-find-k-in-least.html' title='Given a sorted array, find k in least possible time in Java'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-7147427383891331705</id><published>2011-11-26T04:51:00.000-08:00</published><updated>2011-11-26T04:56:47.423-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='array'/><category scheme='http://www.blogger.com/atom/ns#' term='problem'/><category scheme='http://www.blogger.com/atom/ns#' term='interview'/><category scheme='http://www.blogger.com/atom/ns#' term='solution'/><category scheme='http://www.blogger.com/atom/ns#' term='coding'/><title type='text'>Find middle element in a circularly sorted array</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;Given a circularly sorted array, find its middle element in best possible time.&lt;br /&gt;&lt;br /&gt;A circularly sorted array is an array that is obtained by shifting an array k times. For example, the following is a circularly sorted array,&lt;br /&gt;&lt;br /&gt;&lt;div style="text-align: center;"&gt;&lt;span style="font-size: x-large;"&gt;&lt;b&gt;[5, 6, 7, 8, 9, 1, 2, 3, 4]&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: center;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;If you sort this array and find the middle element it will be 5. But then, sorting is a painful operation which will take O(nlogn) timing. So it is out of the equation right away. If you closely observe this problem, it is enough to find the shifting index, the place where the sorting varies. For example, the shifting index in the above example is 5.&amp;nbsp; Once that is found, then it is a matter of applying the bias over the length.&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;This program can be attacked in a recursive manner. Take the first and last elements in the array and compare them. If they are sorted, then the last element is greater than the first element. If this condition is not met, we will have to divide this array into two halves and try applying the same logic. If you do this, at some point you will arrive at a situation where you are left with only two elements or lesser. The end value is the shift index. Once the shift index is found, it is easy to find the middle element(s) for odd and even cases. Java code is below.&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:java"&gt;package dsa.arrays;&lt;br /&gt;&lt;br /&gt;public class ShiftedSortedFindMiddle {&lt;br /&gt; &lt;br /&gt; public static void main(String[] args) {&lt;br /&gt;  ShiftedSortedFindMiddle ssfm = new ShiftedSortedFindMiddle();&lt;br /&gt;  ssfm.findMiddleOfArray(null);&lt;br /&gt;  ssfm.findMiddleOfArray(new int[]{15,16,1,3,6,7});&lt;br /&gt;  ssfm.findMiddleOfArray(new int[]{2,3,1});&lt;br /&gt;  ssfm.findMiddleOfArray(new int[]{6,7,8,1,2,3,4,5});&lt;br /&gt;  ssfm.findMiddleOfArray(new int[]{1,2,3,4,5});&lt;br /&gt;  ssfm.findMiddleOfArray(new int[]{});&lt;br /&gt;  ssfm.findMiddleOfArray(new int[]{1});&lt;br /&gt; }&lt;br /&gt;  &lt;br /&gt; public void findMiddleOfArray(int[] csArray){&lt;br /&gt;  if(csArray == null || csArray.length == 0) { &lt;br /&gt;   System.out.print("\nAre you Nuts? Send a valid array with at least one element you moron");&lt;br /&gt;   return;&lt;br /&gt;  }&lt;br /&gt;  int length = csArray.length;&lt;br /&gt;  int switchIndex = recursiveFind(csArray, 0 , length-1);&lt;br /&gt;  System.out.print("\nMiddle Element(s) : " + csArray[(length/2 + switchIndex)%length]);&lt;br /&gt;  if (length%2 == 0){&lt;br /&gt;   System.out.print(" and " + csArray[(-1 + (length/2) + switchIndex)%length]);&lt;br /&gt;  }&lt;br /&gt; }&lt;br /&gt; &lt;br /&gt; public int recursiveFind (int[] arr, int start, int end){&lt;br /&gt;  if(end-start == 1){&lt;br /&gt;   return end;&lt;br /&gt;  }&lt;br /&gt;  if(arr[start] &amp;gt; arr[end]){&lt;br /&gt;   if(arr[start] &amp;gt; arr[end/2]){&lt;br /&gt;    return recursiveFind(arr, start, end/2);&lt;br /&gt;   }else if(arr[start/2] &amp;gt; arr[end]){&lt;br /&gt;    return recursiveFind(arr, end/2 + 1, end);&lt;br /&gt;   }else{&lt;br /&gt;    return end;&lt;br /&gt;   }&lt;br /&gt;  }&lt;br /&gt;  return start;&lt;br /&gt; }&lt;br /&gt; &lt;br /&gt;}&lt;br /&gt;/*&lt;br /&gt;Are you Nuts? Send a valid array with at least one element you moron&lt;br /&gt;Middle Element(s) : 7 and 6&lt;br /&gt;Middle Element(s) : 2&lt;br /&gt;Middle Element(s) : 5 and 4&lt;br /&gt;Are you Nuts? Send a valid array with at least one element you moron&lt;br /&gt;Middle Element(s) : 1&lt;br /&gt;*/&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;Leave comments if something is unclear.&lt;br /&gt;&lt;br /&gt;Cheers!&lt;br /&gt;Braga&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-7147427383891331705?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/7147427383891331705/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=7147427383891331705' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/7147427383891331705'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/7147427383891331705'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2011/11/given-circularly-sorted-array-find-its.html' title='Find middle element in a circularly sorted array'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-8295729480689566538</id><published>2011-11-24T08:52:00.000-08:00</published><updated>2011-12-01T10:57:28.789-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='dequeue'/><category scheme='http://www.blogger.com/atom/ns#' term='data structures'/><category scheme='http://www.blogger.com/atom/ns#' term='enqueue'/><category scheme='http://www.blogger.com/atom/ns#' term='queue'/><category scheme='http://www.blogger.com/atom/ns#' term='interview'/><category scheme='http://www.blogger.com/atom/ns#' term='algorithm'/><category scheme='http://www.blogger.com/atom/ns#' term='ADT'/><category scheme='http://www.blogger.com/atom/ns#' term='abstract'/><category scheme='http://www.blogger.com/atom/ns#' term='stack'/><category scheme='http://www.blogger.com/atom/ns#' term='coding'/><title type='text'>Queue using Stack in Java</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;A Queue can constructed with its underlying data structure being a stack. Typically, we will need two Stacks one for enqueue and the other for dequeue.&lt;br /&gt;&lt;br /&gt;During every enqueue() operation over a queue, we will keep pushing value to a first stack, lets say stack1.&lt;br /&gt;&lt;br /&gt;During every dequeue() operation, we will have to simply pop an element from stack2 if stack2 is not empty. If stack2 is empty, we will need to pop all elements from stack1 one by one and push it to stack2. And then pop an element from stack2.&lt;br /&gt;&lt;br /&gt;Take for example, I am inserting elements from 1 to 6 to a queue. This is how the following queue and the corresponding stack will look like,&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-7IHamFFbDOg/TtfJYvfKGUI/AAAAAAAACWU/VHCtTAMsytc/s1600/Queue+using+Stack+Step+1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-7IHamFFbDOg/TtfJYvfKGUI/AAAAAAAACWU/VHCtTAMsytc/s1600/Queue+using+Stack+Step+1.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Then trying a dequeue() operation will try to immediately pop values from stack 2.&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-38EzZIY0rRY/TtfNl4wzsWI/AAAAAAAACWc/SAWswE8J-iw/s1600/Queue+using+Stack+Step+2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-38EzZIY0rRY/TtfNl4wzsWI/AAAAAAAACWc/SAWswE8J-iw/s1600/Queue+using+Stack+Step+2.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;So, it will pop all values from stack1 and push them to stack2 as follows,&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-ykyXW5jIEME/TtfNwNkqt3I/AAAAAAAACWk/Q0oYxsE7mtU/s1600/Queue+using+Stack+Step+3.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-ykyXW5jIEME/TtfNwNkqt3I/AAAAAAAACWk/Q0oYxsE7mtU/s1600/Queue+using+Stack+Step+3.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;Now the dequeue() operation shall be performed with ease since stack2 is not empty.&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-DJbIDcZj9hM/TtfN5klLzAI/AAAAAAAACWs/jMLuaWQjKJI/s1600/Queue+using+Stack+Step+4.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-DJbIDcZj9hM/TtfN5klLzAI/AAAAAAAACWs/jMLuaWQjKJI/s1600/Queue+using+Stack+Step+4.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;Finally, more enqueue will add or keep pushing values to the stack.&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-hkdfDxSOCZQ/TtfOB6DGdSI/AAAAAAAACW0/MvCuHXEvzBI/s1600/Queue+using+Stack+Step+5.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-hkdfDxSOCZQ/TtfOB6DGdSI/AAAAAAAACW0/MvCuHXEvzBI/s1600/Queue+using+Stack+Step+5.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Following is the Java code. Note, I have not comprehensively covered the entire methods in a Queue, but this should be well more than enough.&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:java"&gt;package dsa.stack;&lt;br /&gt;&lt;br /&gt;import java.util.Stack;&lt;br /&gt;&lt;br /&gt;/**&lt;br /&gt; * An implementation of Queue using two Stacks&lt;br /&gt; * @author Braga&lt;br /&gt; */&lt;br /&gt;&lt;br /&gt;public class QueueStack&amp;lt;T&amp;gt;{&lt;br /&gt; private Stack&amp;lt;T&amp;gt; firstStack;&lt;br /&gt; private Stack&amp;lt;T&amp;gt; secondStack;&lt;br /&gt; &lt;br /&gt; public QueueStack(){&lt;br /&gt;  firstStack = new Stack&amp;lt;T&amp;gt;();&lt;br /&gt;  secondStack = new Stack&amp;lt;T&amp;gt;();&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; public QueueStack&amp;lt;T&amp;gt; enQueue(T item) {&lt;br /&gt;  firstStack.push(item);&lt;br /&gt;  return this;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; public T deQueue() {&lt;br /&gt;  if(!secondStack.isEmpty()){&lt;br /&gt;   return secondStack.pop();&lt;br /&gt;  }else if(!firstStack.isEmpty()){&lt;br /&gt;   while(!firstStack.isEmpty()){&lt;br /&gt;    secondStack.push(firstStack.pop());&lt;br /&gt;   }&lt;br /&gt;   return secondStack.pop();&lt;br /&gt;  }&lt;br /&gt;  return null;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; public int size() {&lt;br /&gt;  return firstStack.size() + secondStack.size();&lt;br /&gt; }&lt;br /&gt; &lt;br /&gt; public boolean isEmpty(){&lt;br /&gt;  if (size() == 0) {&lt;br /&gt;   return true;&lt;br /&gt;  }&lt;br /&gt;  return false;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;And a test class for this with output&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:java"&gt;package dsa.stack;&lt;br /&gt;&lt;br /&gt;public class TestQueue {&lt;br /&gt; public static void main(String[] args) {&lt;br /&gt;  QueueStack&amp;lt;Integer&amp;gt; queueUsingStack = new QueueStack&amp;lt;Integer&amp;gt;();&lt;br /&gt;  queueUsingStack.enQueue(1).enQueue(2).enQueue(3).enQueue(4).enQueue(5).enQueue(6);&lt;br /&gt;  System.out.println("Queue current size is : "+queueUsingStack.size());&lt;br /&gt;  System.out.println(queueUsingStack.deQueue());&lt;br /&gt;  System.out.println(queueUsingStack.deQueue());&lt;br /&gt;  System.out.println(queueUsingStack.deQueue());&lt;br /&gt;  System.out.println(queueUsingStack.deQueue());&lt;br /&gt;  queueUsingStack.enQueue(10).enQueue(11).enQueue(12);&lt;br /&gt;  System.out.println(queueUsingStack.deQueue());&lt;br /&gt;  System.out.println(queueUsingStack.deQueue());&lt;br /&gt;  System.out.println(queueUsingStack.deQueue());&lt;br /&gt;  System.out.println(queueUsingStack.deQueue());&lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;/*&lt;br /&gt;Queue current size is : 6&lt;br /&gt;1&lt;br /&gt;2&lt;br /&gt;3&lt;br /&gt;4&lt;br /&gt;5&lt;br /&gt;6&lt;br /&gt;10&lt;br /&gt;11&lt;br /&gt;*/&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Cheers!&lt;br /&gt;Braga&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-8295729480689566538?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/8295729480689566538/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=8295729480689566538' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/8295729480689566538'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/8295729480689566538'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2011/11/queue-using-stack-in-java.html' title='Queue using Stack in Java'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/-7IHamFFbDOg/TtfJYvfKGUI/AAAAAAAACWU/VHCtTAMsytc/s72-c/Queue+using+Stack+Step+1.png' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-3237784312956489993</id><published>2011-11-18T03:28:00.000-08:00</published><updated>2011-11-18T03:28:06.156-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='traversal'/><category scheme='http://www.blogger.com/atom/ns#' term='interview'/><category scheme='http://www.blogger.com/atom/ns#' term='binary search tree'/><category scheme='http://www.blogger.com/atom/ns#' term='coding'/><category scheme='http://www.blogger.com/atom/ns#' term='binary tree'/><title type='text'>Program to check whether a tree is a BST</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;Write a program to check whether a given tree is a binary search tree.&lt;br /&gt;&lt;br /&gt;We all know that a binary search tree follows the rule : for any given node with value N, all the values in the left of that node is lesser than N and all the values in the right of that node will be greater than or equal to N. For example,&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-cCdzKjzJzwU/TsZASkWufWI/AAAAAAAACWE/-hQkioTZlYs/s1600/Binary+search+tree+proper.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://1.bp.blogspot.com/-cCdzKjzJzwU/TsZASkWufWI/AAAAAAAACWE/-hQkioTZlYs/s1600/Binary+search+tree+proper.gif" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;This is a tricky question. We may try to immediately run through all the nodes checking only the immediate left and right nodes and coming to a conclusion that it is a binary search tree. But this program may not work for this scenario.&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-U478a-9orZw/TsZAU_4vo9I/AAAAAAAACWM/GO5Ec6dyRjg/s1600/Binary+Search+tree+problematic.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-U478a-9orZw/TsZAU_4vo9I/AAAAAAAACWM/GO5Ec6dyRjg/s1600/Binary+Search+tree+problematic.gif" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;The simplest solution to this problem is to do an inorder traversal through the entire tree and find whether it is sorted or not. Typically we can do that by pushing into any array. Even better is to calculate the minimum value at each step. Program is as follows. Please leave some comments if something is confusing. I will get back immediately.&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;pre class="brush:java"&gt;package dsa.tree;&lt;br /&gt;&lt;br /&gt;public class Tree {&lt;br /&gt; Node root;&lt;br /&gt;&lt;br /&gt; public static void main(String args[]) {&lt;br /&gt;  Tree anyTree = new Tree();&lt;br /&gt;  anyTree.root = new Node(3);&lt;br /&gt;  anyTree.root.left = new Node(2);&lt;br /&gt;  anyTree.root.right = new Node(5);&lt;br /&gt;  anyTree.root.left.left = new Node(1);&lt;br /&gt;  anyTree.root.left.right = new Node(4);&lt;br /&gt;  &lt;br /&gt;  &lt;br /&gt;  if(anyTree.isBst(anyTree.root)){&lt;br /&gt;   System.out.println("Is BST");&lt;br /&gt;  }else{&lt;br /&gt;   System.out.println("Is Not bst");&lt;br /&gt;  }&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; private int MINVALUE = -1;&lt;br /&gt; &lt;br /&gt; public boolean isBst(Node node) {&lt;br /&gt;  if(node.left!=null){&lt;br /&gt;   if(!isBst(node.left)) return false;&lt;br /&gt;  }&lt;br /&gt;  if(node.data &gt; MINVALUE){&lt;br /&gt;   MINVALUE = node.data;&lt;br /&gt;  }else{&lt;br /&gt;   return false;&lt;br /&gt;  }&lt;br /&gt;  if(node.right!=null){&lt;br /&gt;   if(!isBst(node.right)) return false;&lt;br /&gt;  }&lt;br /&gt;  return true;&lt;br /&gt; }&lt;br /&gt; &lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Cheers!&lt;br /&gt;Braga&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-3237784312956489993?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/3237784312956489993/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=3237784312956489993' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/3237784312956489993'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/3237784312956489993'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2011/11/program-to-check-whether-tree-is-bst.html' title='Program to check whether a tree is a BST'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/-cCdzKjzJzwU/TsZASkWufWI/AAAAAAAACWE/-hQkioTZlYs/s72-c/Binary+search+tree+proper.gif' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-4177042661764431936</id><published>2011-08-31T12:08:00.000-07:00</published><updated>2011-08-31T12:14:45.981-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='windows 7'/><category scheme='http://www.blogger.com/atom/ns#' term='windows'/><category scheme='http://www.blogger.com/atom/ns#' term='operating system'/><category scheme='http://www.blogger.com/atom/ns#' term='osx'/><category scheme='http://www.blogger.com/atom/ns#' term='ideal'/><category scheme='http://www.blogger.com/atom/ns#' term='off-topic'/><category scheme='http://www.blogger.com/atom/ns#' term='completeness'/><category scheme='http://www.blogger.com/atom/ns#' term='lion'/><category scheme='http://www.blogger.com/atom/ns#' term='versatility'/><category scheme='http://www.blogger.com/atom/ns#' term='developer'/><category scheme='http://www.blogger.com/atom/ns#' term='OS'/><category scheme='http://www.blogger.com/atom/ns#' term='linux'/><category scheme='http://www.blogger.com/atom/ns#' term='vista'/><category scheme='http://www.blogger.com/atom/ns#' term='xp'/><category scheme='http://www.blogger.com/atom/ns#' term='mac'/><title type='text'>Windows, Linux and OSX</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;Windows is the most used Operating System from an end user point of view. Next is Mac and the last ofcourse is linux. But this is not the case for a developer. Most of the development forums I've found online for the past so many years have given me results that has dealt fairly equal with these three major operating systems.&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-dr0SL_xA6sI/Tl6FVxfAfkI/AAAAAAAACVg/PN0d-htTJPg/s1600/Windows_logo.png" imageanchor="1" style="clear: right; float: right; margin-bottom: 1em; margin-left: 1em;"&gt;&lt;img border="0" height="200" src="http://3.bp.blogspot.com/-dr0SL_xA6sI/Tl6FVxfAfkI/AAAAAAAACVg/PN0d-htTJPg/s200/Windows_logo.png" width="200" /&gt;&lt;/a&gt;&lt;a href="http://3.bp.blogspot.com/-SsBKYSJECeE/Tl6F_XkTQII/AAAAAAAACVk/F2AYlT7AxM0/s1600/linux-logo2.gif" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="200" src="http://3.bp.blogspot.com/-SsBKYSJECeE/Tl6F_XkTQII/AAAAAAAACVk/F2AYlT7AxM0/s200/linux-logo2.gif" width="166" /&gt;&lt;/a&gt;&lt;a href="http://2.bp.blogspot.com/-5VIHEd2vX8Q/Tl6FVSiIF-I/AAAAAAAACVc/zebytdczHpo/s1600/OS_X_logo.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="200" src="http://2.bp.blogspot.com/-5VIHEd2vX8Q/Tl6FVSiIF-I/AAAAAAAACVc/zebytdczHpo/s200/OS_X_logo.png" width="170" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;So, I would suggest to get your hands dirty with all these, if you have not already. If you have a Windows machine at work, buy a Macbook at home, install Linux (best is Ubuntu) on your desktop. Yes, this involves some cost, but trust me, its definitely worth it and you can see yourself well ahead of the curve as a developer.&lt;br /&gt;&lt;br /&gt;For me, I use Linux at work and Mac at home. I hardly use windows these days but I have used it for a good 10+ years!!&lt;br /&gt;&lt;br /&gt;Cheers,&lt;br /&gt;Braga&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-4177042661764431936?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/4177042661764431936/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=4177042661764431936' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/4177042661764431936'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/4177042661764431936'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2011/08/windows-linux-osx.html' title='Windows, Linux and OSX'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/-dr0SL_xA6sI/Tl6FVxfAfkI/AAAAAAAACVg/PN0d-htTJPg/s72-c/Windows_logo.png' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-7528295171483987054</id><published>2011-08-14T14:15:00.000-07:00</published><updated>2011-08-14T14:31:33.281-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ruby'/><category scheme='http://www.blogger.com/atom/ns#' term='versus'/><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='block'/><category scheme='http://www.blogger.com/atom/ns#' term='yield'/><category scheme='http://www.blogger.com/atom/ns#' term='puts'/><category scheme='http://www.blogger.com/atom/ns#' term='date comparison'/><category scheme='http://www.blogger.com/atom/ns#' term='language'/><title type='text'>Blocks in Ruby</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;Blocks are one of the coolest things to have when you are coming from a language like C++ or Java. Say you are writing a function and you want to have some functionality within it which should be handled totally by the caller. Well, blocks come for the rescue.&lt;br /&gt;&lt;br /&gt;Say I have a method like this.&lt;br /&gt;&lt;pre class="brush:ruby"&gt;def test_method&lt;br /&gt;  #Something you do at the start&lt;br /&gt;  #I want to do something that the caller intends to do&lt;br /&gt;  #Something you do at the end&lt;br /&gt;end&lt;/pre&gt;&lt;br /&gt;There is a sweet keyword in Ruby to replace the second line of the test_method called yield.&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:ruby"&gt;def test_method&lt;br /&gt;&amp;nbsp; puts "start"&lt;br /&gt;&amp;nbsp; yield&lt;br /&gt;&amp;nbsp; puts "end"&lt;br /&gt;end&lt;/pre&gt;&lt;br /&gt;And you can now make a call like,&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:ruby"&gt;test_method {puts "From block"}&lt;/pre&gt;&lt;br /&gt;Would output,&lt;br /&gt;start&lt;br /&gt;From block&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;Nothing great if you already are a ruby nerd. But for Java blokes, blocks are something to think for.&lt;br /&gt;&lt;br /&gt;Cheers!&lt;br /&gt;Braga&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-7528295171483987054?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/7528295171483987054/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=7528295171483987054' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/7528295171483987054'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/7528295171483987054'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2011/08/blocks-in-ruby.html' title='Blocks in Ruby'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-5850944627129039802</id><published>2011-08-09T14:25:00.000-07:00</published><updated>2011-08-14T14:35:05.358-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='problem'/><category scheme='http://www.blogger.com/atom/ns#' term='swing'/><category scheme='http://www.blogger.com/atom/ns#' term='game of life'/><category scheme='http://www.blogger.com/atom/ns#' term='fun'/><category scheme='http://www.blogger.com/atom/ns#' term='thoughtworks'/><category scheme='http://www.blogger.com/atom/ns#' term='interview'/><category scheme='http://www.blogger.com/atom/ns#' term='conways'/><category scheme='http://www.blogger.com/atom/ns#' term='ui'/><category scheme='http://www.blogger.com/atom/ns#' term='game'/><title type='text'>Conway's Game of Life in Java!!</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;I recently met with &lt;a href="http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life"&gt;Conway's Game of Life&lt;/a&gt; problem given as a programming assignment by one of the famous MNCs to clear level 1 in interview process. It was so mouth watering that I want to develop an UI for it and publish it in my blog. Spent 3 hours and came up with this.&lt;br /&gt;&lt;br /&gt;&lt;div style="color: #741b47;"&gt;&lt;span style="font-size: small;"&gt;&lt;b&gt;Quick gist about the game from wikipedia&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="color: #741b47;"&gt;&lt;span style="font-size: small;"&gt;The universe of the Game of Life is an infinite two-dimensional orthogonal grid of square &lt;i&gt;cells&lt;/i&gt;, each of which is in one of two possible states, &lt;i&gt;alive&lt;/i&gt; or &lt;i&gt;dead&lt;/i&gt;. Every cell interacts with its eight &lt;i&gt;neighbors&lt;/i&gt;,  which are the cells that are horizontally, vertically, or diagonally  adjacent. At each step in time, the following transitions occur:&lt;/span&gt;&lt;/div&gt;&lt;span style="color: #741b47; font-size: small;"&gt; &lt;/span&gt;&lt;br /&gt;&lt;ol style="color: #741b47;"&gt;&lt;li&gt;&lt;span style="font-size: small;"&gt;Any live cell with fewer than two live neighbours dies, as if caused by under-population.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-size: small;"&gt;Any live cell with two or three live neighbours lives on to the next generation.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-size: small;"&gt;Any live cell with more than three live neighbours dies, as if by overcrowding.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-size: small;"&gt;Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.&lt;/span&gt;&lt;/li&gt;&lt;/ol&gt;&lt;span style="color: #741b47; font-size: small;"&gt; &lt;/span&gt;&lt;br /&gt;&lt;div style="color: #741b47;"&gt;&lt;span style="font-size: small;"&gt;The initial pattern constitutes the &lt;i&gt;seed&lt;/i&gt; of the system. The  first generation is created by applying the above rules simultaneously  to every cell in the seed—births and deaths occur simultaneously, and  the discrete moment at which this happens is sometimes called a &lt;i&gt;tick&lt;/i&gt;  (in other words, each generation is a pure function of the preceding  one). The rules continue to be applied repeatedly to create further  generations.&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;Following is the screenshot and an applet for the same. The Code is free to share and distribute. Visit my &lt;a href="https://github.com/technicalypto/Games/tree/master/GameOfLife"&gt;github link&lt;/a&gt; for this game for the bleeding edge copy. &lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-zZR_5UZJHMY/TkGiGRpOIOI/AAAAAAAACVI/7TgVvQ6IqZo/s1600/Conway%2527s+Game+of+Life.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="400" src="http://3.bp.blogspot.com/-zZR_5UZJHMY/TkGiGRpOIOI/AAAAAAAACVI/7TgVvQ6IqZo/s400/Conway%2527s+Game+of+Life.png" width="343" /&gt;&amp;nbsp;&lt;/a&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;center&gt;&lt;br /&gt;&lt;div&gt;&lt;applet align="middle" code="gameoflife/ui/MainFrameApplet.class" codebase="http://technicalypto.741.com/GameOfLife/" height="600" width="500"&gt;&lt;/applet&gt;&lt;/div&gt;&lt;/center&gt;&lt;br /&gt;&lt;br /&gt;Cheers!!&lt;br /&gt;Braga&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-5850944627129039802?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/5850944627129039802/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=5850944627129039802' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/5850944627129039802'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/5850944627129039802'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2011/08/conways-game-of-life-in-java.html' title='Conway&apos;s Game of Life in Java!!'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/-zZR_5UZJHMY/TkGiGRpOIOI/AAAAAAAACVI/7TgVvQ6IqZo/s72-c/Conway%2527s+Game+of+Life.png' height='72' width='72'/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-9130539465925693240</id><published>2011-07-03T08:53:00.000-07:00</published><updated>2011-07-03T11:30:12.688-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='application'/><category scheme='http://www.blogger.com/atom/ns#' term='development'/><category scheme='http://www.blogger.com/atom/ns#' term='bmi'/><category scheme='http://www.blogger.com/atom/ns#' term='xcode'/><category scheme='http://www.blogger.com/atom/ns#' term='tutorial'/><category scheme='http://www.blogger.com/atom/ns#' term='cocoa'/><category scheme='http://www.blogger.com/atom/ns#' term='calculator'/><category scheme='http://www.blogger.com/atom/ns#' term='xcode4'/><category scheme='http://www.blogger.com/atom/ns#' term='iphone'/><category scheme='http://www.blogger.com/atom/ns#' term='body mass index'/><category scheme='http://www.blogger.com/atom/ns#' term='objective-c'/><title type='text'>iPhone application tutorial - Body Mass Index Calculator : Part 7</title><content type='html'>&lt;div style="margin: auto 33%;"&gt;&lt;ul id="pagination-digg"&gt;&lt;li class="previous"&gt;&lt;a href="http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass_7683.html"&gt;«Previous&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass.html"&gt;1&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass_03.html"&gt;2&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass_6870.html"&gt;3&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass_3195.html"&gt;4&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass_5873.html"&gt;5&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass_7683.html"&gt;6&lt;/a&gt;&lt;/li&gt;&lt;li class="active"&gt;7&lt;/li&gt;&lt;li class="next-off"&gt;Next »&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;br /&gt;&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;br /&gt;The first posting's output and this may vary since that was developed with XCode 3 and this one with XCode 4. There are slight differences between both of them, but that we don't want to know. You can download XCode 4 for $5 from Apple's website.&lt;br /&gt;&lt;br /&gt;I wanted to keep this final section very light. Hence going to show only the output here. :)&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-oND2R0zuTqI/ThCO1cUn08I/AAAAAAAACUk/QjpYgHMFi6M/s1600/o1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-oND2R0zuTqI/ThCO1cUn08I/AAAAAAAACUk/QjpYgHMFi6M/s1600/o1.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/-i9R9eGC_UlA/ThCO2NCUnDI/AAAAAAAACUo/EkbzHoQ5o6M/s1600/o2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/-i9R9eGC_UlA/ThCO2NCUnDI/AAAAAAAACUo/EkbzHoQ5o6M/s1600/o2.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-Q_J6rS-sl-o/ThCO24g0OwI/AAAAAAAACUs/llEhf0uO4gs/s1600/o3.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-Q_J6rS-sl-o/ThCO24g0OwI/AAAAAAAACUs/llEhf0uO4gs/s1600/o3.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;Click &lt;a href="https://docs.google.com/uc?id=0B_z-9Q7kZRM7NjEwODVkNTgtMGUwMy00NWE5LWFmM2ItYzZlZTI2NTQ0NmU4&amp;amp;hl=en_US&amp;amp;export=download&amp;amp;type=.zip"&gt;here&lt;/a&gt; to download the entire source code for this project. It is free to develop and distribute. Hope you guys learnt something.&lt;br /&gt;&lt;br /&gt;Cheers!!&lt;br /&gt;Braga&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-9130539465925693240?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/9130539465925693240/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=9130539465925693240' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/9130539465925693240'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/9130539465925693240'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass_2499.html' title='iPhone application tutorial - Body Mass Index Calculator : Part 7'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/-oND2R0zuTqI/ThCO1cUn08I/AAAAAAAACUk/QjpYgHMFi6M/s72-c/o1.png' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-7918293352946773925</id><published>2011-07-03T08:42:00.000-07:00</published><updated>2011-07-03T11:42:19.524-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='application'/><category scheme='http://www.blogger.com/atom/ns#' term='development'/><category scheme='http://www.blogger.com/atom/ns#' term='bmi'/><category scheme='http://www.blogger.com/atom/ns#' term='xcode'/><category scheme='http://www.blogger.com/atom/ns#' term='cocoa'/><category scheme='http://www.blogger.com/atom/ns#' term='calculator'/><category scheme='http://www.blogger.com/atom/ns#' term='xcode4'/><category scheme='http://www.blogger.com/atom/ns#' term='iphone'/><category scheme='http://www.blogger.com/atom/ns#' term='body mass index'/><category scheme='http://www.blogger.com/atom/ns#' term='objective-c'/><title type='text'>iPhone application tutorial - Body Mass Index Calculator : Part 6</title><content type='html'>&lt;div style="margin: auto 33%;"&gt;&lt;ul id="pagination-digg"&gt;&lt;li class="previous"&gt;&lt;a href="http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass_5873.html"&gt;«Previous&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass.html"&gt;1&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass_03.html"&gt;2&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass_6870.html"&gt;3&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass_3195.html"&gt;4&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass_5873.html"&gt;5&lt;/a&gt;&lt;/li&gt;&lt;li class="active"&gt;6&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass_2499.html"&gt;7&lt;/a&gt;&lt;/li&gt;&lt;li class="next"&gt;&lt;a href="http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass_2499.html"&gt;Next »&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;br /&gt;&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;br /&gt;This is our last part of the tutorial. We have done whatever that needs to be done with respect to the UI. All that is left is the coding part. In the last post, you would have seen that we had a couple of methods defined called as updateResult() and range(). These are supposed to be private methods. But then, I don't want to overload the readers with that concept yet, hence I have not set the access modifier for them. They will come in handy in our implementation part.&lt;br /&gt;&lt;br /&gt;The implementation part involves responding to the actions that are sent from the UI. Lets take a look at the implementation of the height slider and go line by line.&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:cpp"&gt;- (IBAction)heightSlider:(id)sender {&lt;br /&gt;    UISlider *hSlider = (UISlider*) sender;&lt;br /&gt; int hVal = (int)hSlider.value;&lt;br /&gt; NSString *hLabel = [[NSString alloc]initWithFormat:@"%i", hVal];&lt;br /&gt; height.text = hLabel;&lt;br /&gt; [self updateResults];&lt;br /&gt; [hLabel release];&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The first line is nothing but a copy of the method declaration. The return type of the this method is an IBAction. The argument that it takes is the sender's object. If you had known Java, 'id' is a data type similar to Object. It is the sender's object. Since we have attached this method with the Slider, this would be the Slider itself.&lt;br /&gt;&lt;br /&gt;On the fist line inside the method, we are typecasting the sender to the UISlider since we know for sure that the sender is an instance of UISlider. We then get its value, asssign it to a NSString variable followed by assigning it to the height text. Since we have allocated the NSString we should release it. You could notice there is a tiny little call to updateResults. It is nothing but updating the BMI and the Result at the bottom. Its implemenation is,&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:cpp"&gt;- (void)updateResults{&lt;br /&gt;    float h = [height.text floatValue];&lt;br /&gt; float w = [weight.text floatValue];&lt;br /&gt; float bmiVal = w / ((h/100)*(h/100));&lt;br /&gt; NSString *bmiResult = [[NSString alloc]initWithFormat:@"%.1f", bmiVal];&lt;br /&gt; bmi.text = bmiResult;&lt;br /&gt; result.text = [self range:bmiVal];&lt;br /&gt; [bmiResult release];&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;- (NSString *) range:(float)bmiVal{&lt;br /&gt;    NSString *theRange;&lt;br /&gt; &lt;br /&gt; if(bmiVal &amp;lt; 16.5){&lt;br /&gt;  theRange = @"Severly Underweight";&lt;br /&gt; }else if (bmiVal&amp;lt;18.5) {&lt;br /&gt;  theRange = @"Underweight";&lt;br /&gt; }else if (bmiVal&amp;lt;25) {&lt;br /&gt;  theRange = @"Normal";&lt;br /&gt; }else if (bmiVal&amp;lt;30) {&lt;br /&gt;  theRange = @"Overweight";&lt;br /&gt; }else if (bmiVal&amp;lt;35) {&lt;br /&gt;  theRange = @"Obese";&lt;br /&gt; }else {&lt;br /&gt;  theRange = @"Severly Obese";&lt;br /&gt; }&lt;br /&gt; return theRange;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The above code is self explanatory and straightforward to read. We also need to add a similar slider method for weight as we did for height.&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:cpp"&gt;- (IBAction)weightSlider:(id)sender {&lt;br /&gt;    UISlider *wSlider = (UISlider*) sender;&lt;br /&gt; int wVal = (int)wSlider.value;&lt;br /&gt; NSString *wLabel = [[NSString alloc]initWithFormat:@"%i", wVal];&lt;br /&gt; weight.text = wLabel;&lt;br /&gt; [self updateResults];&lt;br /&gt; [wLabel release];&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Whoof thats a lotta coding. Lets see the final output in the final part of this tutorial!&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;Cheers!&lt;br /&gt;Braga&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-7918293352946773925?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/7918293352946773925/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=7918293352946773925' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/7918293352946773925'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/7918293352946773925'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass_7683.html' title='iPhone application tutorial - Body Mass Index Calculator : Part 6'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-3183067517037309580</id><published>2011-07-03T08:29:00.000-07:00</published><updated>2011-07-03T11:41:54.019-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='application'/><category scheme='http://www.blogger.com/atom/ns#' term='development'/><category scheme='http://www.blogger.com/atom/ns#' term='bmi'/><category scheme='http://www.blogger.com/atom/ns#' term='xcode'/><category scheme='http://www.blogger.com/atom/ns#' term='tutorial'/><category scheme='http://www.blogger.com/atom/ns#' term='cocoa'/><category scheme='http://www.blogger.com/atom/ns#' term='calculator'/><category scheme='http://www.blogger.com/atom/ns#' term='iphone'/><category scheme='http://www.blogger.com/atom/ns#' term='body mass index'/><category scheme='http://www.blogger.com/atom/ns#' term='objective-c'/><title type='text'>iPhone application tutorial - Body Mass Index Calculator : Part 5</title><content type='html'>&lt;div style="margin: auto 33%;"&gt;&lt;ul id="pagination-digg"&gt;&lt;li class="previous"&gt;&lt;a href="http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass_3195.html"&gt;«Previous&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass.html"&gt;1&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass_03.html"&gt;2&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass_6870.html"&gt;3&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass_3195.html"&gt;4&lt;/a&gt;&lt;/li&gt;&lt;li class="active"&gt;5&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass_7683.html"&gt;6&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass_2499.html"&gt;7&lt;/a&gt;&lt;/li&gt;&lt;li class="next"&gt;&lt;a href="http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass_7683.html"&gt;Next »&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;br /&gt;&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;br /&gt;In the past section we saw how to design the view and set the basic things up. Now, its time to do some coding. Before that, to check whether everything are Ok, press the ⌘ + B button to build your project. It may prompt you to save the project. If it does, click the Save button. The build should succeed at this point. If it fails, then go and check the logs or tell me the error in the comments section, I would surely help you out!&lt;br /&gt;&lt;br /&gt;Now, back to business. We need to dynamically update the values in the UI. To attach this with the code, we have a binding object defined called as IBOutlet. Using this object, we would be able to set stuff in the code and see it change in the UI. Looks magic. To start binding the UI components and the code. Click on the Editor icon the right top side to see both the Interface Builder and the Interface file side by side.&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-c5xk00maK-0/ThCEf2KxkjI/AAAAAAAACUU/YSCUNIfagUc/s1600/tutorial+08.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-c5xk00maK-0/ThCEf2KxkjI/AAAAAAAACUU/YSCUNIfagUc/s1600/tutorial+08.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;Select your component on the UI - the height label. Hold down the control key and draw a line from the component to the Interface definition, you should be seeing a line coming up automatically for it to be inserted as shown below.&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/-WjCIH3kUmLk/ThCGxCRKRoI/AAAAAAAACUY/0q4jRk7elv8/s1600/tutorial+09.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/-WjCIH3kUmLk/ThCGxCRKRoI/AAAAAAAACUY/0q4jRk7elv8/s1600/tutorial+09.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;Lets name that IBOutlet variable to be height. Do the same for weight, bmi and the result labels. So, we are done with defining our binding variables namely the IBOutlet. Now whatever change you do for these outlets will now be reflected in the UI. We have one more thing left from the hooking up perspective - the Sliders. We need to assign an action when we slide the height and weight sliders. It is pretty simple. Select the height slider, choose the properties from the side and click on the Value changed option and drag it to the interface code. All you should be naming here is the method name which would be height slider. Note, you do not need to press Control key as we did for the Outlets.&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/-8zVUShuj3aA/ThCICg5ZyHI/AAAAAAAACUg/ss6WJ0GC9oc/s1600/tutorial19.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="392" src="http://4.bp.blogspot.com/-8zVUShuj3aA/ThCICg5ZyHI/AAAAAAAACUg/ss6WJ0GC9oc/s640/tutorial19.png" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;You would need to add a couple of more methods in your interface file. The final code would look something like the following&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:cpp"&gt;#import &amp;lt;UIKit/UIKit.h&amp;gt;&lt;br /&gt;&lt;br /&gt;@interface helloXcodeViewController : UIViewController {&lt;br /&gt;    &lt;br /&gt;    IBOutlet UILabel *height;&lt;br /&gt;    IBOutlet UILabel *weight;&lt;br /&gt;    IBOutlet UILabel *bmi;&lt;br /&gt;    IBOutlet UILabel *result;&lt;br /&gt;}&lt;br /&gt;- (IBAction)heightSlider:(id)sender;&lt;br /&gt;- (IBAction)weightSlider:(id)sender;&lt;br /&gt;- (void)updateResults;&lt;br /&gt;- (NSString *) range:(float)bmiVal;&lt;br /&gt;@end&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Cheers!&lt;br /&gt;Braga&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-3183067517037309580?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/3183067517037309580/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=3183067517037309580' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/3183067517037309580'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/3183067517037309580'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass_5873.html' title='iPhone application tutorial - Body Mass Index Calculator : Part 5'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/-c5xk00maK-0/ThCEf2KxkjI/AAAAAAAACUU/YSCUNIfagUc/s72-c/tutorial+08.png' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-7179405007644408475</id><published>2011-07-03T07:55:00.000-07:00</published><updated>2011-07-03T11:41:18.442-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='application'/><category scheme='http://www.blogger.com/atom/ns#' term='development'/><category scheme='http://www.blogger.com/atom/ns#' term='bmi'/><category scheme='http://www.blogger.com/atom/ns#' term='xcode'/><category scheme='http://www.blogger.com/atom/ns#' term='tutorial'/><category scheme='http://www.blogger.com/atom/ns#' term='cocoa'/><category scheme='http://www.blogger.com/atom/ns#' term='calculator'/><category scheme='http://www.blogger.com/atom/ns#' term='iphone'/><category scheme='http://www.blogger.com/atom/ns#' term='body mass index'/><category scheme='http://www.blogger.com/atom/ns#' term='objective-c'/><title type='text'>iPhone application tutorial - Body Mass Index Calculator : Part 4</title><content type='html'>&lt;div style="margin: auto 33%;"&gt;&lt;ul id="pagination-digg"&gt;&lt;li class="previous"&gt;&lt;a href="http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass_6870.html"&gt;«Previous&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass.html"&gt;1&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass_03.html"&gt;2&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass_6870.html"&gt;3&lt;/a&gt;&lt;/li&gt;&lt;li class="active"&gt;4&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass_5873.html"&gt;5&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass_7683.html"&gt;6&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass_2499.html"&gt;7&lt;/a&gt;&lt;/li&gt;&lt;li class="next"&gt;&lt;a href="http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass_5873.html"&gt;Next »&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;br /&gt;&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;br /&gt;Welcome to the part4 of the BMI Calculator iPhone application tutorial. We saw in the last section on how to set components on the UI. I had said that there was a small thing mentioned about the slider.&lt;br /&gt;&lt;br /&gt;Before I tell more about it. Lets strategize how our application should behave. You slide two things in your application namely the heights and weights, the user will be seeing his body mass index calculated and will be given the result on whether he is normal or underweight or obese etc. We will be needing to determine the upper and lower bounds of the heights and weights. I have taken the height range as 120-210 cm and the weight range as 30-130 kg.&lt;br /&gt;&lt;br /&gt;Whenever you click on any component, it corresponding property should be appearing at the right hand panel. Go ahead and click on the height slider. Set the minimum and maximum values of the slider as we discussed before.&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-pmE4GKxhR2A/ThCCGeZgizI/AAAAAAAACUQ/N3iNmh_kzZM/s1600/iphone+tutorial+mini+pic+3.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-pmE4GKxhR2A/ThCCGeZgizI/AAAAAAAACUQ/N3iNmh_kzZM/s1600/iphone+tutorial+mini+pic+3.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;You would also need to set the initial value. Since it needs to be in the center, set the height as 165 (average of 120 and 210). Same you would need to do by selecting the weight slider. 30-130 as lower upper bounds with 80 as the current value.&lt;br /&gt;&lt;br /&gt;Thats it we are done designing our wonderful UI for the iPhone Application. We have some UI-Code hookup and some coding left before we declare our application is complete which we will be doing in the coming posts.&lt;br /&gt;&lt;br /&gt;Cheers!&lt;br /&gt;Braga&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-7179405007644408475?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/7179405007644408475/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=7179405007644408475' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/7179405007644408475'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/7179405007644408475'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass_3195.html' title='iPhone application tutorial - Body Mass Index Calculator : Part 4'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/-pmE4GKxhR2A/ThCCGeZgizI/AAAAAAAACUQ/N3iNmh_kzZM/s72-c/iphone+tutorial+mini+pic+3.png' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-3426593374768959330</id><published>2011-07-03T07:44:00.000-07:00</published><updated>2011-07-03T11:40:52.371-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='application'/><category scheme='http://www.blogger.com/atom/ns#' term='development'/><category scheme='http://www.blogger.com/atom/ns#' term='bmi'/><category scheme='http://www.blogger.com/atom/ns#' term='xcode'/><category scheme='http://www.blogger.com/atom/ns#' term='tutorial'/><category scheme='http://www.blogger.com/atom/ns#' term='cocoa'/><category scheme='http://www.blogger.com/atom/ns#' term='iphone'/><category scheme='http://www.blogger.com/atom/ns#' term='body mass index'/><category scheme='http://www.blogger.com/atom/ns#' term='objective-c'/><title type='text'>iPhone application tutorial - Body Mass Index Calculator : Part 3</title><content type='html'>&lt;div style="margin: auto 33%;"&gt;&lt;ul id="pagination-digg"&gt;&lt;li class="previous"&gt;&lt;a href="http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass_03.html"&gt;«Previous&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass.html"&gt;1&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass_03.html"&gt;2&lt;/a&gt;&lt;/li&gt;&lt;li class="active"&gt;3&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass_3195.html"&gt;4&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass_5873.html"&gt;5&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass_7683.html"&gt;6&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass_2499.html"&gt;7&lt;/a&gt;&lt;/li&gt;&lt;li class="next"&gt;&lt;a href="http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass_3195.html"&gt;Next »&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;br /&gt;Fellas,&lt;br /&gt;&lt;br /&gt;Welcome to the part 3 of the tutorial. You could be thinking, "what the hell did I learn in Part 2?" Well. Lets take one baby step at a time. The second part of the tutorial's project name read "Body Mass Index Calculator". But then I had to change it to helloworldXcode, the name would not mind much, but whatever you choose, choose it wiser.&lt;br /&gt;&lt;br /&gt;On the left hand top side of the you should be able to see your project. Expand it and you should be seeing your interface file (.h), Implementation file (.m) and the UI file (.xib). Clicking on any of these will bring up the corresponding code or UI on the main panel. &lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-dpZ9Q4W_rJ0/ThB-H9I86nI/AAAAAAAACUA/si5HVNzzpN0/s1600/mini+tutorial+pic+01.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-dpZ9Q4W_rJ0/ThB-H9I86nI/AAAAAAAACUA/si5HVNzzpN0/s1600/mini+tutorial+pic+01.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;Go ahead and select the .xib ViewController file. You should now see an empty Canvas appearing on the center of the editor where you can drag and drop stuff. For our body mass index we need few UILabels that are going to be static and few UILabels that are going to change dynamically on the user action.&lt;br /&gt;For dragging and dropping components on to the View you should select the Object from the drop down on the right hand side bottom corner as shown below.&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-Qzbx-1jdmhs/ThB_Wz-r9iI/AAAAAAAACUI/ZYp8kMEtq88/s1600/iphone+mini+tutorial+pic+2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-Qzbx-1jdmhs/ThB_Wz-r9iI/AAAAAAAACUI/ZYp8kMEtq88/s1600/iphone+mini+tutorial+pic+2.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;We also will need a couple of slider for selecting the height and weight. Already sounds cool eh? Here is a screenshot on what stuff you should be dragging and dropping on the center of the editor.&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-DNhUs0MXmJI/ThB_AuZsTNI/AAAAAAAACUE/FyRUVktS2lc/s1600/tutorial+04.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="556" src="http://2.bp.blogspot.com/-DNhUs0MXmJI/ThB_AuZsTNI/AAAAAAAACUE/FyRUVktS2lc/s640/tutorial+04.png" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;After dropping the elements which includes totally 9 labels and two sliders you should be aligning them like shown in the following screenshot. The placement of the labels depends totally on your imagination and creativity, but lets make it professional looking so that our application should sell at least a million times in the App store. That was a joke :)&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/-z_xC4_3Qqv4/ThB_79iKOOI/AAAAAAAACUM/RyhH1ucT86g/s1600/iphone+tutorial+05.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/-z_xC4_3Qqv4/ThB_79iKOOI/AAAAAAAACUM/RyhH1ucT86g/s1600/iphone+tutorial+05.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;You could notice that changing the names of the labels are very simple. Just double click on it and start typing as shown in the above screenshot. With this your basic set up of the view is complete, we just have a few more things left regarding the slider functionality, but we will cover it in the next section.&lt;br /&gt;&lt;br /&gt;Cheers,&lt;br /&gt;Braga&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-3426593374768959330?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/3426593374768959330/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=3426593374768959330' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/3426593374768959330'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/3426593374768959330'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass_6870.html' title='iPhone application tutorial - Body Mass Index Calculator : Part 3'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/-dpZ9Q4W_rJ0/ThB-H9I86nI/AAAAAAAACUA/si5HVNzzpN0/s72-c/mini+tutorial+pic+01.png' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-8081345001810215040</id><published>2011-07-03T07:30:00.000-07:00</published><updated>2011-07-03T11:40:12.689-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='application'/><category scheme='http://www.blogger.com/atom/ns#' term='development'/><category scheme='http://www.blogger.com/atom/ns#' term='bmi'/><category scheme='http://www.blogger.com/atom/ns#' term='xcode'/><category scheme='http://www.blogger.com/atom/ns#' term='tutorial'/><category scheme='http://www.blogger.com/atom/ns#' term='calculator'/><category scheme='http://www.blogger.com/atom/ns#' term='iphone'/><category scheme='http://www.blogger.com/atom/ns#' term='body mass index'/><category scheme='http://www.blogger.com/atom/ns#' term='objective-c'/><title type='text'>iPhone application tutorial - Body Mass Index Calculator : Part 2</title><content type='html'>&lt;div style="margin: auto 33%;"&gt;&lt;ul id="pagination-digg"&gt;&lt;li class="previous"&gt;&lt;a href="http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass.html"&gt;«Previous&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass.html"&gt;1&lt;/a&gt;&lt;/li&gt;&lt;li class="active"&gt;2&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass_6870.html"&gt;3&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass_3195.html"&gt;4&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass_5873.html"&gt;5&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass_7683.html"&gt;6&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass_2499.html"&gt;7&lt;/a&gt;&lt;/li&gt;&lt;li class="next"&gt;&lt;a href="http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass_6870.html"&gt;Next »&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;br /&gt;&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;br /&gt;Welcome to the second part of the BMI Calculator Application for iPhone tutorial. I assume that you have XCode installed in your mac or Mac under VMWare. Launch XCode and select a new View based project.&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-2nTOpZ-oZHA/ThA94q76Q_I/AAAAAAAACT0/kHtuS3bsPDs/s1600/iPhone+tutorial+01.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="433" src="http://3.bp.blogspot.com/-2nTOpZ-oZHA/ThA94q76Q_I/AAAAAAAACT0/kHtuS3bsPDs/s640/iPhone+tutorial+01.png" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;Click on Next. Fill up the basic details and move on.&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-aAX3y1u3bq4/ThA95_t1f_I/AAAAAAAACT4/x-9jNxD-SFM/s1600/iPhone+tutorial+02.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="428" src="http://1.bp.blogspot.com/-aAX3y1u3bq4/ThA95_t1f_I/AAAAAAAACT4/x-9jNxD-SFM/s640/iPhone+tutorial+02.png" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;Once you have selected where to save your project, you should see something like this. Note: We are using Xcode 4 to develop this application, the view may vary from the previous version of Xcodes.&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-MeVmSk8M8Vk/ThA96ltHfNI/AAAAAAAACT8/KzKdvCcdxCQ/s1600/iPhone+tutorial+03.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="611" src="http://1.bp.blogspot.com/-MeVmSk8M8Vk/ThA96ltHfNI/AAAAAAAACT8/KzKdvCcdxCQ/s640/iPhone+tutorial+03.png" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;Thats it in this part of the tutorial. Lets start using the Interface builder and do some coding in the coming posts.&lt;br /&gt;&lt;br /&gt;Cheers,&lt;br /&gt;Braga&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-8081345001810215040?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/8081345001810215040/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=8081345001810215040' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/8081345001810215040'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/8081345001810215040'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass_03.html' title='iPhone application tutorial - Body Mass Index Calculator : Part 2'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/-2nTOpZ-oZHA/ThA94q76Q_I/AAAAAAAACT0/kHtuS3bsPDs/s72-c/iPhone+tutorial+01.png' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-9086926267594439872</id><published>2011-07-02T12:49:00.000-07:00</published><updated>2011-07-03T11:25:11.990-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='application'/><category scheme='http://www.blogger.com/atom/ns#' term='development'/><category scheme='http://www.blogger.com/atom/ns#' term='bmi'/><category scheme='http://www.blogger.com/atom/ns#' term='xcode'/><category scheme='http://www.blogger.com/atom/ns#' term='tutorial'/><category scheme='http://www.blogger.com/atom/ns#' term='cocoa'/><category scheme='http://www.blogger.com/atom/ns#' term='calculator'/><category scheme='http://www.blogger.com/atom/ns#' term='iphone'/><category scheme='http://www.blogger.com/atom/ns#' term='body mass index'/><category scheme='http://www.blogger.com/atom/ns#' term='objective-c'/><title type='text'>iPhone application tutorial - Body Mass Index Calculator : Part 1</title><content type='html'>&lt;div style="margin: auto 33%;"&gt;&lt;ul id="pagination-digg"&gt;&lt;li class="previous-off"&gt;«Previous&lt;/li&gt;&lt;li class="active"&gt;1&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass_03.html"&gt;2&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass_6870.html"&gt;3&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass_3195.html"&gt;4&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass_5873.html"&gt;5&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass_7683.html"&gt;6&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass_2499.html"&gt;7&lt;/a&gt;&lt;/li&gt;&lt;li class="next"&gt;&lt;a href="http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass_03.html"&gt;Next »&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;br /&gt;&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;br /&gt;After an extensive learning on Objective C and covering the basics of iPhone development, I wanted to get my hands dirty with a working application. Following the concept of &lt;a href="http://www.technicalypto.com/2011/07/minimal-markatable-subset.html"&gt;Minimal Marketable Subset&lt;/a&gt;, I thought I would develop a very primitive Body Mass Index calculator. Ok, into business&lt;br /&gt;&lt;br /&gt;Our Final BMI Calculator Application is going to look like this.&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-QpYrh6tbkdM/Tg9ziyOQjbI/AAAAAAAACTw/9e6vnjtN8oE/s1600/Body+Mass+Index+Calculator+iPhone.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="384" src="http://1.bp.blogspot.com/-QpYrh6tbkdM/Tg9ziyOQjbI/AAAAAAAACTw/9e6vnjtN8oE/s640/Body+Mass+Index+Calculator+iPhone.png" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Prerequisities&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Since this is a very narrow development environment (yes you cannot develop this with windows), I would want to give a brief on what you will need, both hardware and software. First of all, you would need a Macbook or a Macbook Pro. A basic Macbook would do. Its based on how far you go. If you are very serious into iPhone development, then I would recommend you get a Macbook Pro. On the software side, you would need the XCode development suite. You can get it from Apple's website, it is free to download. It asks you to create an Apple Id. Go to youtube or google, you should find tons of resources there.&lt;br /&gt;&lt;br /&gt;If you want to create clean iPhone Applications, you should have a very strong C Programming Background. And it would be nice if you are familiar with Object Oriented Concepts. You would need to learn &lt;a href="http://www.youtube.com/watch?v=1Xqn5IHbusA&amp;amp;feature=list_related&amp;amp;playnext=1&amp;amp;list=SP640F44F1C97BA581"&gt;Objective C basics&lt;/a&gt; and then the basics of &lt;a href="http://www.youtube.com/watch?v=abcMmyhKCno"&gt;iPhone development&lt;/a&gt;. Believe me, those links are simply too good. You can learn them both in just a day if you are freak like me.&lt;br /&gt;&lt;br /&gt;Armed up with these basics, we can dive into the tutorial right away from the next post.&lt;br /&gt;&lt;br /&gt;Cheers,&lt;br /&gt;Braga&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-9086926267594439872?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/9086926267594439872/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=9086926267594439872' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/9086926267594439872'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/9086926267594439872'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2011/07/iphone-application-tutorial-body-mass.html' title='iPhone application tutorial - Body Mass Index Calculator : Part 1'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/-QpYrh6tbkdM/Tg9ziyOQjbI/AAAAAAAACTw/9e6vnjtN8oE/s72-c/Body+Mass+Index+Calculator+iPhone.png' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-1554959095706875217</id><published>2011-07-02T12:19:00.000-07:00</published><updated>2011-07-02T12:25:40.257-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='certified scrum master'/><category scheme='http://www.blogger.com/atom/ns#' term='agile'/><category scheme='http://www.blogger.com/atom/ns#' term='csm'/><category scheme='http://www.blogger.com/atom/ns#' term='training'/><category scheme='http://www.blogger.com/atom/ns#' term='practice'/><category scheme='http://www.blogger.com/atom/ns#' term='scrum'/><category scheme='http://www.blogger.com/atom/ns#' term='coding'/><title type='text'>Minimal Markatable Subset</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;a href="http://2.bp.blogspot.com/-OguvJhPE5BA/Tg9vh4s1NgI/AAAAAAAACTg/2Hluf-EavJc/s1600/building_blocks.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="298" src="http://2.bp.blogspot.com/-OguvJhPE5BA/Tg9vh4s1NgI/AAAAAAAACTg/2Hluf-EavJc/s320/building_blocks.jpg" width="320" /&gt;&lt;/a&gt;It has been a long time since my last technical post. Yes, 6 months!! I wanted to write something on iPhone Application development. But before doing that, I want to tell you guys on something that I learnt during my two day training on clearing the Certified Scrum Master. &lt;br /&gt;During this training, I heard this term called "Minimal Marketable Subset". After hearing to what it meant, my view as a developer changed drastically. Basically, I want my code to be perfect. Whenever I develop something, I want it to be highly user interactive, highly easier and the highly optimized. But know what, that is one of the huge mistakes I have done so far. Because of this attitude, I can never be satisfied and declare my product/project as completed. I felt that was the right way to think till I heard of "Minimal Marketable Subset". It is the minimum thing that is needed to get something up and running. You can develop in increments as the application develops. That is the basis of any Agile practice and I have started to adopt it to my programming practice.&lt;br /&gt;To put in simple terms, "do what you can with what you have and do it quick"&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-1554959095706875217?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/1554959095706875217/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=1554959095706875217' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/1554959095706875217'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/1554959095706875217'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2011/07/minimal-markatable-subset.html' title='Minimal Markatable Subset'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/-OguvJhPE5BA/Tg9vh4s1NgI/AAAAAAAACTg/2Hluf-EavJc/s72-c/building_blocks.jpg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-825355672434064601</id><published>2010-12-21T01:08:00.000-08:00</published><updated>2010-12-28T09:31:51.188-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='as3'/><category scheme='http://www.blogger.com/atom/ns#' term='puzzle'/><category scheme='http://www.blogger.com/atom/ns#' term='15 game'/><category scheme='http://www.blogger.com/atom/ns#' term='actionscript3'/><category scheme='http://www.blogger.com/atom/ns#' term='flash'/><category scheme='http://www.blogger.com/atom/ns#' term='cs5'/><category scheme='http://www.blogger.com/atom/ns#' term='game'/><title type='text'>15 game in Flash Actionscript3</title><content type='html'>Having implemented the 15-game in &lt;a href="http://www.technicalypto.com/2010/09/free-game-in-java.html"&gt;Java&lt;/a&gt;, I thought I would attempt the same with Flash + AS3. And the same was done swiftly and I have presented the output and the code here. This is the same as the applet version, you will have to use your arrow keys to play.&lt;br /&gt;&lt;br /&gt;&lt;center&gt;&lt;br /&gt;&lt;object height="400" width="400"&gt; &lt;param name="movie" value="https://docs.google.com/uc?id=0B_z-9Q7kZRM7MWIyMjBjNmEtZTA2YS00YTVmLWJiYjctYTdiNTQxMTM1NGU1&amp;export=open&amp;hl=en&amp;type=.swf"&gt;&lt;embed src="https://docs.google.com/uc?id=0B_z-9Q7kZRM7MWIyMjBjNmEtZTA2YS00YTVmLWJiYjctYTdiNTQxMTM1NGU1&amp;hl=en&amp;export=open&amp;type=.swf" width="400" height="400"&gt; &lt;/embed&gt; &lt;/object&gt;&lt;/center&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:as3"&gt;package{&lt;br /&gt; //----&lt;br /&gt; //IMPORT&lt;br /&gt; //&lt;br /&gt; import flash.display.*;&lt;br /&gt; import flash.events.*;&lt;br /&gt; import flash.text.*;&lt;br /&gt; import flash.ui.Keyboard;&lt;br /&gt; import flash.media.Sound;&lt;br /&gt; import flash.system.System;&lt;br /&gt;&lt;br /&gt; //Class creation&lt;br /&gt; public class Main extends MovieClip {&lt;br /&gt;  &lt;br /&gt;  private var SIZE:int;&lt;br /&gt;  private var panelSize:int = 400;&lt;br /&gt;  private var panelArray:Array = new Array(SIZE, SIZE);&lt;br /&gt;  &lt;br /&gt;  private var parentStage:Stage;&lt;br /&gt;  private var baseClip:MovieClip;&lt;br /&gt;  &lt;br /&gt;  private var music:Sound = new BubbleSound();&lt;br /&gt;  &lt;br /&gt;  public function Main(stage, bClip, difficulty):void {&lt;br /&gt;   SIZE = difficulty;&lt;br /&gt;   parentStage = stage;&lt;br /&gt;   baseClip = bClip;&lt;br /&gt;   stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);&lt;br /&gt;   layout_in_grid();&lt;br /&gt;   &lt;br /&gt;   for(var i:int=0;i&amp;lt;SIZE*100;i++){//RANDOMIZE MOVES&lt;br /&gt;    handleKeyPress(Math.floor(Math.random()*4) + 37);&lt;br /&gt;   }&lt;br /&gt;  }&lt;br /&gt;  &lt;br /&gt;  private function layout_in_grid():void{&lt;br /&gt;   addChild(getGamePanel(SIZE));&lt;br /&gt;  }&lt;br /&gt;  &lt;br /&gt;  private function keyPressed(e:KeyboardEvent):void{&lt;br /&gt;   handleKeyPress(e.keyCode);&lt;br /&gt;   music.play();&lt;br /&gt;   if(areThingsInPlace()){&lt;br /&gt;    baseClip.gotoAndStop(3,&amp;quot;Scene 1&amp;quot;);&lt;br /&gt;    stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyPressed);&lt;br /&gt;    this.parent.removeChild(this);&lt;br /&gt;    var victory:Sound = new VictorySound();&lt;br /&gt;    victory.play();&lt;br /&gt;    return;&lt;br /&gt;   }&lt;br /&gt;  }&lt;br /&gt;  &lt;br /&gt;  private function handleKeyPress(keyCode:int):void{&lt;br /&gt;   var emptyIndex:int = findEmptyIndex();&lt;br /&gt;   var x:int = emptyIndex/SIZE;&lt;br /&gt;   var y:int = emptyIndex%SIZE;&lt;br /&gt;    &lt;br /&gt;   switch (keyCode) {&lt;br /&gt;    case Keyboard.LEFT://LEFT KEY&lt;br /&gt;     if(y==SIZE-1) return;&lt;br /&gt;     doSwap(x,y,x,y+1);&lt;br /&gt;     break;&lt;br /&gt;    case Keyboard.UP://UP KEY&lt;br /&gt;     if(x==SIZE-1) return;&lt;br /&gt;     doSwap(x,y,x+1,y);&lt;br /&gt;     break;&lt;br /&gt;    case Keyboard.RIGHT://RIGHT KEY&lt;br /&gt;     if(y==0) return;&lt;br /&gt;     doSwap(x,y,x,y-1);&lt;br /&gt;     break;&lt;br /&gt;    case Keyboard.DOWN://DOWN KEY&lt;br /&gt;     if(x==0) return;&lt;br /&gt;     doSwap(x,y,x-1,y);&lt;br /&gt;     break;&lt;br /&gt;   }&lt;br /&gt;  }&lt;br /&gt;  &lt;br /&gt;  private function findEmptyIndex():int {&lt;br /&gt;   for(var i:int=0;i&amp;lt;SIZE;i++){&lt;br /&gt;    for (var j:int=0;j&amp;lt;SIZE;j++){&lt;br /&gt;     if(panelArray[i][j].num_txt.text == &amp;#39;0&amp;#39;){&lt;br /&gt;      return i*SIZE + j;&lt;br /&gt;     }&lt;br /&gt;    }&lt;br /&gt;   }&lt;br /&gt;   return 0;&lt;br /&gt;  }&lt;br /&gt;  &lt;br /&gt;  private function doSwap(x:int, y:int, x1:int, y1:int):void{&lt;br /&gt;   var temp:int;&lt;br /&gt;   temp = panelArray[x][y].num_txt;&lt;br /&gt;   panelArray[x][y].num_txt.text = panelArray[x1][y1].num_txt.text;&lt;br /&gt;   panelArray[x1][y1].num_txt.text = temp;&lt;br /&gt;   &lt;br /&gt;   if(panelArray[x][y].num_txt.text == &amp;#39;0&amp;#39;){&lt;br /&gt;    panelArray[x][y].alpha = 0;&lt;br /&gt;    panelArray[x1][y1].alpha = 1;&lt;br /&gt;   }else{&lt;br /&gt;    panelArray[x][y].alpha = 1;&lt;br /&gt;    panelArray[x1][y1].alpha = 0;&lt;br /&gt;   }&lt;br /&gt;    &lt;br /&gt;  }&lt;br /&gt;  &lt;br /&gt;  private function getGamePanel(difficulty:int):MovieClip {&lt;br /&gt;   var gridLayout:GridLayout = new GridLayout(SIZE, panelSize);&lt;br /&gt;    &lt;br /&gt;   var componentsList:Array = getRandomizedList(difficulty);&lt;br /&gt;   var index:int = 0;&lt;br /&gt;   for(var i:int=0;i&amp;lt;difficulty;i++){&lt;br /&gt;    panelArray[i] = new Array(difficulty);&lt;br /&gt;    for(var j:int=0;j&amp;lt;difficulty;j++){&lt;br /&gt;     panelArray[i][j] = componentsList[index++];&lt;br /&gt;     gridLayout.add(panelArray[i][j]);&lt;br /&gt;    }&lt;br /&gt;   }&lt;br /&gt;   gridLayout.x = (parentStage.stageWidth / 2) - (gridLayout.width / 2);&lt;br /&gt;     gridLayout.y = (parentStage.stageHeight / 2) - (gridLayout.height / 2);&lt;br /&gt;&lt;br /&gt;   return gridLayout;&lt;br /&gt;  }&lt;br /&gt;  &lt;br /&gt;  private function areThingsInPlace():Boolean{&lt;br /&gt;   for(var i:int=0;i&amp;lt;SIZE*SIZE-1;i++){&lt;br /&gt;    if(Number(panelArray[Math.floor(i/SIZE)][i%SIZE].num_txt.text) != (i+1)){&lt;br /&gt;     return false;&lt;br /&gt;    }&lt;br /&gt;   }&lt;br /&gt;   return true;&lt;br /&gt;  }&lt;br /&gt;  &lt;br /&gt;  private function getRandomizedList(difficulty:int):Array  {&lt;br /&gt;   var componentSet:Array = new Array(SIZE*SIZE);&lt;br /&gt;   for(var i:int=0;i&amp;lt;difficulty*difficulty-1;i++){&lt;br /&gt;    var square:SquareClip = new SquareClip();&lt;br /&gt;    square.num_txt.text = (i+1).toString(10);&lt;br /&gt;    componentSet[i] = square;&lt;br /&gt;   }&lt;br /&gt;   var emptyClip:SquareClip = new SquareClip();&lt;br /&gt;   emptyClip.num_txt.text = (0).toString(10);&lt;br /&gt;   componentSet[i] = emptyClip;&lt;br /&gt;   return componentSet;&lt;br /&gt;  }&lt;br /&gt; }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;The Layout grid file which I wrote on my own to simulate an exact Grid Layout similar to the one in Java.&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:as3"&gt;package{&lt;br /&gt; //----&lt;br /&gt; //IMPORT&lt;br /&gt; //&lt;br /&gt; import flash.display.*;&lt;br /&gt; import flash.events.*;&lt;br /&gt; import flash.text.*;&lt;br /&gt; &lt;br /&gt; //Class creation&lt;br /&gt; public class GridLayout extends MovieClip{&lt;br /&gt;  &lt;br /&gt;  private var _size:int;&lt;br /&gt;  private var _size_of_grid:int;&lt;br /&gt;  private var _gap:int;&lt;br /&gt;  private var _itemCount:int;&lt;br /&gt;  private var _totalFilled:int;&lt;br /&gt;  &lt;br /&gt;  public function GridLayout(size, size_of_grid):void {&lt;br /&gt;   _size = size;&lt;br /&gt;   _size_of_grid = size_of_grid;&lt;br /&gt;   _totalFilled = 0;&lt;br /&gt;  }&lt;br /&gt;  &lt;br /&gt;  public function add(object):int{&lt;br /&gt;   if(_totalFilled == (_size*_size )){&lt;br /&gt;       trace(&amp;quot;GRID IS FULL&amp;quot;);&lt;br /&gt;     return 0;  &lt;br /&gt;   }&lt;br /&gt;   object.x = ( (_totalFilled % _size) ) * (_size_of_grid/_size)  ;&lt;br /&gt;   object.y =  Math.floor(_totalFilled / _size) * (_size_of_grid/_size) ;&lt;br /&gt;   _totalFilled++;&lt;br /&gt;   addChild(object);&lt;br /&gt;   return 1;&lt;br /&gt;  }&lt;br /&gt;  &lt;br /&gt; }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;Cheers!!&lt;br /&gt;Braga&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-825355672434064601?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/825355672434064601/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=825355672434064601' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/825355672434064601'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/825355672434064601'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/12/15-game-in-flash-actionscript3.html' title='15 game in Flash Actionscript3'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-7174166507356754455</id><published>2010-12-20T15:56:00.000-08:00</published><updated>2010-12-20T15:56:33.913-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='printf'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><category scheme='http://www.blogger.com/atom/ns#' term='coding'/><title type='text'>Print % using printf in C</title><content type='html'>&lt;span style="font-size: large;"&gt;&lt;b&gt;Problem:&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;How can you print % using the printf function? (Remember % is used as a format specifier!!!)&lt;br /&gt;Very Simple, following cases are examples&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:cpp"&gt;#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;&lt;br /&gt;int main()&lt;br /&gt;{&lt;br /&gt;    printf("\%\n"); // warning: unknown conversion type character 0xa in format - but prints&lt;br /&gt;    printf("%%\n"); // correct statement&lt;br /&gt;    printf("%\n");  // warning: unknown conversion type character 0xa in format&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;Cheers!!&lt;br /&gt;Jack&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-7174166507356754455?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/7174166507356754455/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=7174166507356754455' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/7174166507356754455'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/7174166507356754455'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/12/print-using-printf-in-c.html' title='Print % using printf in C'/><author><name>Prabhu Jayaraman</name><uri>http://www.blogger.com/profile/11266928407768534504</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='30' src='http://4.bp.blogspot.com/_N_LXkcmZpN0/TAlTmeYTegI/AAAAAAAAALM/RSKv8--iWME/S220/IMG_0110.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-5318119806899290896</id><published>2010-12-20T15:49:00.000-08:00</published><updated>2010-12-20T15:49:47.281-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='output'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><category scheme='http://www.blogger.com/atom/ns#' term='coding'/><title type='text'>Find Output for this C Program</title><content type='html'>&lt;span style="font-size: large;"&gt;&lt;b&gt;Problem:&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;What would be the output of the following C program? (Is it a valid C program?)&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:cpp"&gt;#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;int main()&lt;br /&gt;{&lt;br /&gt;    int i=43;&lt;br /&gt;    printf("%d\n",printf("%d",printf("%d",i)));&lt;br /&gt;    return 0;&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;span style="font-size: large;"&gt;&lt;b&gt;Explanation:&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;This will produce output “4321”. The return type of printf is “int” which is the number of characters written to stdout.&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:cpp"&gt;printf("%d\n",printf("%d",printf("%d",i)));&lt;br /&gt;printf("%d\n",printf("%d",printf("%d",43))); =&amp;gt; 43&lt;br /&gt;printf("%d\n",printf("%d",2)); =&amp;gt; 2&lt;br /&gt;printf("%d\n",1); =&amp;gt; 1&lt;/pre&gt;&lt;br /&gt;Cheers!!&lt;br /&gt;Jack&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-5318119806899290896?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/5318119806899290896/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=5318119806899290896' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/5318119806899290896'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/5318119806899290896'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/12/find-output-for-this-c-program.html' title='Find Output for this C Program'/><author><name>Prabhu Jayaraman</name><uri>http://www.blogger.com/profile/11266928407768534504</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='30' src='http://4.bp.blogspot.com/_N_LXkcmZpN0/TAlTmeYTegI/AAAAAAAAALM/RSKv8--iWME/S220/IMG_0110.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-96438977652278816</id><published>2010-12-20T15:44:00.000-08:00</published><updated>2010-12-20T15:45:12.444-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='format specifier'/><category scheme='http://www.blogger.com/atom/ns#' term='printf'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><title type='text'>C - %n format specifier in printf</title><content type='html'>Well, what does the format specifier %n of printf function do?&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size: large;"&gt;&lt;b&gt;Explanation:&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;Print nothing, but write number of characters successfully written so far into an integer pointer parameter.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="font-size: large;"&gt;Example:&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;pre class="brush:cpp"&gt;#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;&lt;br /&gt;int main()&lt;br /&gt;{&lt;br /&gt;    int val;&lt;br /&gt;    printf("print the number of characters so far %n doesnt matter what you write here\n", &amp;amp;val);&lt;br /&gt;    printf("val = %d\n", val);&lt;br /&gt;    return 0;&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;span style="font-size: large;"&gt;&lt;b&gt;Output:&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;blah  blah&lt;br /&gt;val = 38&lt;br /&gt;&lt;br /&gt;Cheers!&lt;br /&gt;Jack&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-96438977652278816?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/96438977652278816/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=96438977652278816' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/96438977652278816'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/96438977652278816'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/12/c-n-format-specifier-in-printf.html' title='C - %n format specifier in printf'/><author><name>Prabhu Jayaraman</name><uri>http://www.blogger.com/profile/11266928407768534504</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='30' src='http://4.bp.blogspot.com/_N_LXkcmZpN0/TAlTmeYTegI/AAAAAAAAALM/RSKv8--iWME/S220/IMG_0110.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-4602600960144444865</id><published>2010-12-20T05:52:00.000-08:00</published><updated>2010-12-20T05:52:31.308-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='littleEndian'/><category scheme='http://www.blogger.com/atom/ns#' term='endian'/><category scheme='http://www.blogger.com/atom/ns#' term='bigendian'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><title type='text'>C program to determine endian'ess</title><content type='html'>We shall see a small C program to determine whether a machine's type is little-endian or big-endian.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size: large;"&gt;&lt;b&gt;Definition:&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;i&gt;BigEndian&lt;/i&gt; means that the higher order byte of the number is stored in memory at the lowest address, and the lower order byte at the highest address. The big end comes first.&lt;br /&gt;&lt;br /&gt;eg: OAOBOCOD will be stored as OA(a) OB(a+1) OC(a+2) OD(a+3)&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/_kRev2kjeMro/TQ9fJPmXT2I/AAAAAAAACSY/9YL-F6xRHlc/s1600/Big+Endian.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/_kRev2kjeMro/TQ9fJPmXT2I/AAAAAAAACSY/9YL-F6xRHlc/s1600/Big+Endian.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;i&gt;LittleEndian&lt;/i&gt; means that the lower order byte of the number is stored in memory at the lowest address, and the higher order byte is stored at the highest address i.e., the little  &lt;br /&gt;end comes first. &lt;br /&gt;&lt;br /&gt;eg: OAOBOCOD will be stored as OD(a) OC(a+1) OB(a+2) OA(a+3)&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/_kRev2kjeMro/TQ9fNyqmR6I/AAAAAAAACSc/yVfny49trZ4/s1600/Small+Endian.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/_kRev2kjeMro/TQ9fNyqmR6I/AAAAAAAACSc/yVfny49trZ4/s1600/Small+Endian.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size: large;"&gt;&lt;b&gt;Program:&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:cpp"&gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;&lt;br /&gt;using namespace std;&lt;br /&gt;&lt;br /&gt;int main()&lt;br /&gt;{&lt;br /&gt;   int num = 1;&lt;br /&gt;   if(*(char *)&amp;amp;num == 1)&lt;br /&gt;      printf("Little-Endian\n");&lt;br /&gt;   else // will be NULL&lt;br /&gt;      printf("Big-Endian\n");&lt;br /&gt;&lt;br /&gt;   return 0;&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;Cheers!!&lt;br /&gt;Jack&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-4602600960144444865?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/4602600960144444865/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=4602600960144444865' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/4602600960144444865'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/4602600960144444865'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/12/c-program-to-determine-endianess.html' title='C program to determine endian&apos;ess'/><author><name>Prabhu Jayaraman</name><uri>http://www.blogger.com/profile/11266928407768534504</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='30' src='http://4.bp.blogspot.com/_N_LXkcmZpN0/TAlTmeYTegI/AAAAAAAAALM/RSKv8--iWME/S220/IMG_0110.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_kRev2kjeMro/TQ9fJPmXT2I/AAAAAAAACSY/9YL-F6xRHlc/s72-c/Big+Endian.png' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-8314962667395360265</id><published>2010-12-20T05:39:00.000-08:00</published><updated>2010-12-20T05:40:59.129-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='XOR'/><category scheme='http://www.blogger.com/atom/ns#' term='OR'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><category scheme='http://www.blogger.com/atom/ns#' term='full-adder'/><category scheme='http://www.blogger.com/atom/ns#' term='coding'/><title type='text'>Addition without using the + operator in C</title><content type='html'>Write a C function which does the addition of two integers without using the '+' operator. You can use only the bitwise operators.(Remember the good old method of implementing the full-adder circuit using the OR and XOR gates....)&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/_kRev2kjeMro/TQ9cYt_0EDI/AAAAAAAACSU/NuMryqH7ZBw/s1600/550px-Full-adder.png"/&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;Now, for the code implemented in C.&lt;br /&gt;&lt;pre class="brush:cpp"&gt;#include&amp;lt;stdio.h&amp;gt;&lt;br /&gt;int main()&lt;br /&gt;{&lt;br /&gt;    int a = 12, b = 25;&lt;br /&gt;    while (b)&lt;br /&gt;    {&lt;br /&gt;        int carry = a &amp;amp; b;&lt;br /&gt;        a = a ^ b;&lt;br /&gt;        b = carry &amp;lt;&amp;lt; 1;&lt;br /&gt;    }&lt;br /&gt;    printf("%d\n",a);&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;Cheers!&lt;br /&gt;Jack&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-8314962667395360265?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/8314962667395360265/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=8314962667395360265' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/8314962667395360265'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/8314962667395360265'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/12/addition-without-using-operator.html' title='Addition without using the + operator in C'/><author><name>Prabhu Jayaraman</name><uri>http://www.blogger.com/profile/11266928407768534504</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='30' src='http://4.bp.blogspot.com/_N_LXkcmZpN0/TAlTmeYTegI/AAAAAAAAALM/RSKv8--iWME/S220/IMG_0110.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/_kRev2kjeMro/TQ9cYt_0EDI/AAAAAAAACSU/NuMryqH7ZBw/s72-c/550px-Full-adder.png' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-7499326379813789718</id><published>2010-12-20T05:32:00.000-08:00</published><updated>2010-12-20T05:32:05.015-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='cat'/><category scheme='http://www.blogger.com/atom/ns#' term='maths'/><category scheme='http://www.blogger.com/atom/ns#' term='puzzle'/><category scheme='http://www.blogger.com/atom/ns#' term='off-topic'/><title type='text'>CAT Brain Teaser</title><content type='html'>Well, this can be considered off-topic. A brain teaser in CAT. Only 2% students were able to solve this in the CAT Exam. They should have been really sick :) Enough build up, lets see what the problem is.&lt;br /&gt;&lt;br /&gt;Given,&lt;br /&gt;&lt;br /&gt;5+3+2 = 151022 &lt;br /&gt;9+2+4 = 183652 &lt;br /&gt;8+6+3 = 482466 &lt;br /&gt;5+4+5 = 202541 &lt;br /&gt;&lt;br /&gt;Then, 7+2+5 =  ???&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size: large;"&gt;&lt;b&gt;Solution:&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;5+3+2 = (5*3)(5*2)(5*3 + 5*2 - 3)&lt;br /&gt;9+2+4 = (9*2)(9*4)(9*2 + 9*4 - 2)&lt;br /&gt;8+6+3 = (8*6)(8*3)(8*6 + 8*3 - 6)&lt;br /&gt;5+4+5 = (5*4)(5*5)(5*4 + 5*5 - 4)&lt;br /&gt;&lt;br /&gt;Hence,&lt;br /&gt;&lt;br /&gt;7+2+5 =  (7*2)(7*5)(7*2 + 7*5 - 2) = 143547&lt;br /&gt;&lt;br /&gt;Cheers!!&lt;br /&gt;Jack&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-7499326379813789718?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/7499326379813789718/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=7499326379813789718' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/7499326379813789718'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/7499326379813789718'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/12/cat-brain-teaser.html' title='CAT Brain Teaser'/><author><name>Prabhu Jayaraman</name><uri>http://www.blogger.com/profile/11266928407768534504</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='30' src='http://4.bp.blogspot.com/_N_LXkcmZpN0/TAlTmeYTegI/AAAAAAAAALM/RSKv8--iWME/S220/IMG_0110.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-9001813467452141528</id><published>2010-12-16T18:49:00.000-08:00</published><updated>2010-12-16T18:49:02.607-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='offsetoff'/><category scheme='http://www.blogger.com/atom/ns#' term='knowledge'/><category scheme='http://www.blogger.com/atom/ns#' term='offset'/><category scheme='http://www.blogger.com/atom/ns#' term='low-level'/><category scheme='http://www.blogger.com/atom/ns#' term='macros'/><title type='text'>"Offsetof" Macros in C : What it is and why it is</title><content type='html'>&lt;span style="font-size: large;"&gt;&lt;b&gt;Problem:&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The following is the offset macros which is used many a times. Lets figure out what is it trying to do and what is the advantage of using it.&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:cpp"&gt;#define offsetof(a,b) ((int)(&amp;amp;(((a*)(0))-&amp;gt;b)))&lt;/pre&gt;&lt;br /&gt;&lt;span style="font-size: large;"&gt;&lt;b&gt;Explanation:&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;i&gt;offsetof&lt;/i&gt; tells you where in the memory allocation of the structure you will find a particular member.&lt;br /&gt;&lt;br /&gt;Consider the example,&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:cpp"&gt;/* offsetof example */&lt;br /&gt;#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;#include &amp;lt;stddef.h&amp;gt;&lt;br /&gt;&lt;br /&gt;struct mystruct&lt;br /&gt;{&lt;br /&gt;    char singlechar;&lt;br /&gt;    char arraymember[10];&lt;br /&gt;    char anotherchar;&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;int main ()&lt;br /&gt;{&lt;br /&gt;    printf ("offsetof(mystruct,singlechar) is %d\n",offsetof(mystruct,singlechar));&lt;br /&gt;    printf ("offsetof(mystruct,arraymember) is %d\n",offsetof(mystruct,arraymember));&lt;br /&gt;    printf ("offsetof(mystruct,anotherchar) is %d\n",offsetof(mystruct,anotherchar));&lt;br /&gt;&lt;br /&gt;    return 0;&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;The structure defined takes up 12 bytes:&lt;br /&gt;&lt;br /&gt;* byte 0: singlechar&lt;br /&gt;* byte 1: arraymember[0]&lt;br /&gt;* byte 2: arraymember[1]&lt;br /&gt;* byte 3: arraymember[2]&lt;br /&gt;* ...&lt;br /&gt;* byte 10: arraymember[9]&lt;br /&gt;* byte 11: anotherchar&lt;br /&gt;&lt;br /&gt;The output will be:&lt;br /&gt;&lt;br /&gt;offsetof(mystruct,singlechar) is 0&lt;br /&gt;offsetof(mystruct,arraymember) is 1&lt;br /&gt;offsetof(mystruct,anotherchar) is 11&lt;br /&gt;&lt;br /&gt;If you allocate an object of type, and get a &lt;i&gt;byte*&lt;/i&gt; to the start of the structure, you can use &lt;i&gt;offsetof&lt;/i&gt; to find out where each member is. If you use that pointer offset, and convert it back to the correct type, it will give you a pointer to the member.&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:cpp"&gt;int main()&lt;br /&gt;{&lt;br /&gt;    mystruct s;&lt;br /&gt;    s.anotherchar = 5;&lt;br /&gt;    char* pBeginningOfS = (char*)(void*)(&amp;amp;s);&lt;br /&gt;    char* pAnotherChar = pBeginningOfS + offsetof(mystruct, anotherchar);&lt;br /&gt;    *pAnotherChar = 17;&lt;br /&gt;    printf("anotherchar is %d", s.anotherchar);&lt;br /&gt;    return 0;&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;The output will be:&lt;br /&gt;&lt;br /&gt;anotherchar is 17&lt;br /&gt;&lt;br /&gt;The reason you can't assume that each member will be a specific offset from the beginning of the struct is complicated, and compiler dependent. If you must do something like this (which is really low-level stuff which you should avoid unless you have to), then use a macro like &lt;i&gt;offsetof&lt;/i&gt;, rather than trying to manually specify the offset yourself.&lt;br /&gt;&lt;br /&gt;Hope you learnt something.&lt;br /&gt;&lt;br /&gt;Cheers!!&lt;br /&gt;Jack&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-9001813467452141528?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/9001813467452141528/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=9001813467452141528' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/9001813467452141528'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/9001813467452141528'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/12/offsetof-macros-in-c-what-it-is-and-why.html' title='&quot;Offsetof&quot; Macros in C : What it is and why it is'/><author><name>Prabhu Jayaraman</name><uri>http://www.blogger.com/profile/11266928407768534504</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='30' src='http://4.bp.blogspot.com/_N_LXkcmZpN0/TAlTmeYTegI/AAAAAAAAALM/RSKv8--iWME/S220/IMG_0110.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-676244911934869631</id><published>2010-12-16T07:17:00.000-08:00</published><updated>2010-12-16T07:24:17.101-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='puzzle'/><category scheme='http://www.blogger.com/atom/ns#' term='clock'/><category scheme='http://www.blogger.com/atom/ns#' term='interview'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><title type='text'>How to find angle between hour and minute hands in an analog clock?</title><content type='html'>Given a simple clock, we have to find the angle between the hour and minute hands. Since this is a deliberate question, we should obviously be ignoring the thickness of the hands in the clock.&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/_kRev2kjeMro/TQos1mOBBPI/AAAAAAAACSQ/YvvIdcE6Vqc/s1600/Clock+Puzzle.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://1.bp.blogspot.com/_kRev2kjeMro/TQos1mOBBPI/AAAAAAAACSQ/YvvIdcE6Vqc/s1600/Clock+Puzzle.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;We need to understand the following things before we arrive at our solution. &lt;br /&gt;&lt;ul&gt;&lt;li&gt;The hour hand moves at the rate of 0.5 degrees per minute.&lt;/li&gt;&lt;li&gt;The minute hand moves at the rate of 6 degrees per minute.&lt;/li&gt;&lt;/ul&gt;The reason for the above statements are obvious if you think a layer deep. Now, for the code (works both for C++ and Java)&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:java"&gt;double angle(int h, int m)&lt;br /&gt;{&lt;br /&gt;     double hangle = 0.5D * (h*60 + m);&lt;br /&gt;     double mangle = 6*m;&lt;br /&gt;     angle = abs(hangle – mangle);&lt;br /&gt;     angle = min(angle, 360-angle);&lt;br /&gt;     return angle;&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;Cheers!!&lt;br /&gt;Jack.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-676244911934869631?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/676244911934869631/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=676244911934869631' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/676244911934869631'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/676244911934869631'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/12/how-to-find-angle-between-hour-and.html' title='How to find angle between hour and minute hands in an analog clock?'/><author><name>Prabhu Jayaraman</name><uri>http://www.blogger.com/profile/11266928407768534504</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='30' src='http://4.bp.blogspot.com/_N_LXkcmZpN0/TAlTmeYTegI/AAAAAAAAALM/RSKv8--iWME/S220/IMG_0110.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_kRev2kjeMro/TQos1mOBBPI/AAAAAAAACSQ/YvvIdcE6Vqc/s72-c/Clock+Puzzle.png' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-6575503742408848281</id><published>2010-12-16T07:09:00.000-08:00</published><updated>2010-12-16T07:10:08.729-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='memcpy'/><category scheme='http://www.blogger.com/atom/ns#' term='interview'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><category scheme='http://www.blogger.com/atom/ns#' term='memmove'/><title type='text'>What is the difference between memcpy and memmove?</title><content type='html'>&lt;b&gt;Answer:&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;With memcpy, the destination cannot overlap the source at all. With memmove copying takes place as if an intermediate buffer was used, allowing the destination and source to overlap. This means that memmove might be very slightly slower than memcpy, as it cannot make the same assumptions.&lt;br /&gt;&lt;pre class="brush:cpp"&gt;void * memcpy  ( void * destination, const void * source, size_t num );&lt;br /&gt;void * memmove ( void * destination, const void * source, size_t num );&lt;/pre&gt;&lt;br /&gt;Cheers!!&lt;br /&gt;Jack&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-6575503742408848281?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/6575503742408848281/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=6575503742408848281' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/6575503742408848281'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/6575503742408848281'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/12/what-is-difference-between-memcpy-and.html' title='What is the difference between memcpy and memmove?'/><author><name>Prabhu Jayaraman</name><uri>http://www.blogger.com/profile/11266928407768534504</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='30' src='http://4.bp.blogspot.com/_N_LXkcmZpN0/TAlTmeYTegI/AAAAAAAAALM/RSKv8--iWME/S220/IMG_0110.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-4355005157269007619</id><published>2010-12-16T06:45:00.000-08:00</published><updated>2010-12-16T06:47:16.191-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='data structures'/><category scheme='http://www.blogger.com/atom/ns#' term='singly linked list'/><category scheme='http://www.blogger.com/atom/ns#' term='linked list'/><category scheme='http://www.blogger.com/atom/ns#' term='delete'/><category scheme='http://www.blogger.com/atom/ns#' term='node'/><category scheme='http://www.blogger.com/atom/ns#' term='interview'/><category scheme='http://www.blogger.com/atom/ns#' term='algorithm'/><category scheme='http://www.blogger.com/atom/ns#' term='single linked list'/><category scheme='http://www.blogger.com/atom/ns#' term='adobe'/><category scheme='http://www.blogger.com/atom/ns#' term='pointer'/><title type='text'>Linked List : Given a pointer to any node, delete the node pointed by the pointer</title><content type='html'>Given a linked list like this,&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/_kRev2kjeMro/TQolEhHlnUI/AAAAAAAACSI/fsfL2enEB5E/s1600/Linked+List+only+one+node+given+delete.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/_kRev2kjeMro/TQolEhHlnUI/AAAAAAAACSI/fsfL2enEB5E/s320/Linked+List+only+one+node+given+delete.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;Given a pointer to any node, delete the node pointed by the pointer. &lt;b&gt;&lt;span style="color: red;"&gt;Note&lt;/span&gt;&lt;/b&gt;: no head pointer is given.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="font-size: large;"&gt;Solution:&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Assume a pointer to p3, lets call it to 'p'. Since only pointer to current node is provided, there is no way to delete the current node from the list. But instead of deleting the current node, we can just move the next node data to current node and delete the next node. The algorithm can be explained simply as,&lt;br /&gt;&lt;pre class="brush:cpp"&gt;p-&amp;gt;data = p-&amp;gt;next-&amp;gt;data;&lt;br /&gt;t = p-&amp;gt;next;&lt;br /&gt;p-&amp;gt;next = p-&amp;gt;next-&amp;gt;next;&lt;br /&gt;delete t;&lt;/pre&gt;&lt;br /&gt;Cheers!!&lt;br /&gt;Jack&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-4355005157269007619?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/4355005157269007619/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=4355005157269007619' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/4355005157269007619'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/4355005157269007619'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/12/linked-list-given-pointer-to-any-node.html' title='Linked List : Given a pointer to any node, delete the node pointed by the pointer'/><author><name>Prabhu Jayaraman</name><uri>http://www.blogger.com/profile/11266928407768534504</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='30' src='http://4.bp.blogspot.com/_N_LXkcmZpN0/TAlTmeYTegI/AAAAAAAAALM/RSKv8--iWME/S220/IMG_0110.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_kRev2kjeMro/TQolEhHlnUI/AAAAAAAACSI/fsfL2enEB5E/s72-c/Linked+List+only+one+node+given+delete.png' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-7050998529024953718</id><published>2010-12-15T15:14:00.000-08:00</published><updated>2010-12-15T15:14:51.608-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='program'/><category scheme='http://www.blogger.com/atom/ns#' term='problem'/><category scheme='http://www.blogger.com/atom/ns#' term='fibonacci'/><category scheme='http://www.blogger.com/atom/ns#' term='fibonacci series'/><title type='text'>C program to find fibonacci series using only one variable !!!</title><content type='html'>&lt;b&gt;Problem&lt;/b&gt;:&lt;br /&gt;Generating Fibonacci series using only one variable.&lt;br /&gt;&lt;br /&gt;There are many ways to achieve this. I have given three ways to do this in the following methods. &lt;br /&gt;&lt;br /&gt;&lt;b&gt;Method #1:&lt;/b&gt;&lt;br /&gt;We can have a simple function to do this.&lt;br /&gt;&lt;pre class="brush:cpp"&gt;F(n) = [(x^n) – ((1 – x)^n)]/ sqrt(5);&lt;br /&gt;Where x = (1 + sqrt(5))/2 = 1.6180339887&lt;/pre&gt;&lt;br /&gt;&lt;b&gt;Method #2:&lt;/b&gt;&lt;br /&gt;The following recursive function would do the job.&lt;br /&gt;&lt;pre class="brush:cpp"&gt;int fib(n)&lt;br /&gt;{&lt;br /&gt;     return (n&amp;gt;2) ? n : fib(n-1)+fib(n-2);&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;b&gt;Method #3:&lt;/b&gt;&lt;br /&gt;The following one works great as well!&lt;br /&gt;&lt;pre class="brush:cpp"&gt;#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;&lt;br /&gt;int main (void)&lt;br /&gt;{&lt;br /&gt;     unsigned long i = 1;&lt;br /&gt;     printf ("0\n");&lt;br /&gt;     while (((i &amp;amp; 0xffff0000) &amp;gt;&amp;gt; 16) + (i &amp;amp; 0xffff) &amp;lt;= 0xffff) {&lt;br /&gt;          printf ("%d\n", i &amp;amp; 0xffff);    &lt;br /&gt;          i = ((i &amp;amp; 0xffff) &amp;lt;&amp;lt; 16) | ((i &amp;gt;&amp;gt; 16) + (i &amp;amp; 0xffff));&lt;br /&gt;     }&lt;br /&gt;     return 0;&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;Cheers!!&lt;br /&gt;Jack&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-7050998529024953718?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/7050998529024953718/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=7050998529024953718' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/7050998529024953718'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/7050998529024953718'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/12/c-program-to-find-fibonacci-series.html' title='C program to find fibonacci series using only one variable !!!'/><author><name>Prabhu Jayaraman</name><uri>http://www.blogger.com/profile/11266928407768534504</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='30' src='http://4.bp.blogspot.com/_N_LXkcmZpN0/TAlTmeYTegI/AAAAAAAAALM/RSKv8--iWME/S220/IMG_0110.jpg'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-1071847313234569345</id><published>2010-10-14T05:25:00.000-07:00</published><updated>2010-12-16T07:06:34.675-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='puzzle'/><category scheme='http://www.blogger.com/atom/ns#' term='interview'/><category scheme='http://www.blogger.com/atom/ns#' term='adobe'/><title type='text'>Interview Puzzle - Equal heads</title><content type='html'>&lt;b&gt;Puzzle:&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;There are 50 coins on the table out of which 43 are tail-face up and 7 are head face up. You are blind folded and there is no way to determine which side is up by rubbing, etc. You have to divide the 50 coins in two sets (not necessarily equal) such that both have equal number of coins with heads face up.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Solution:&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Divide the 50 coins into two sets - once set with 43 coins and the other set with 7 coins. Thus, the first set will contain 'x' heads and '43-x' tails and the second set will contain '7-x' heads and 'x' tails.  The possibilities can be tabulated as shown in the left table.&lt;br /&gt;&lt;br /&gt;Now, flip the coins in the second set, so that both will contain 'x' heads. The result after flipping can be seen on the right table. You can see that both sets have equal number of heads whatever be the distribution.&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/_kRev2kjeMro/TQoqzqjos6I/AAAAAAAACSM/IzhmuchDow8/s1600/SET.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://1.bp.blogspot.com/_kRev2kjeMro/TQoqzqjos6I/AAAAAAAACSM/IzhmuchDow8/s1600/SET.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;Cheers!!&lt;br /&gt;Jack&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-1071847313234569345?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/1071847313234569345/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=1071847313234569345' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/1071847313234569345'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/1071847313234569345'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/10/interview-puzzle-equal-heads.html' title='Interview Puzzle - Equal heads'/><author><name>Prabhu Jayaraman</name><uri>http://www.blogger.com/profile/11266928407768534504</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='30' src='http://4.bp.blogspot.com/_N_LXkcmZpN0/TAlTmeYTegI/AAAAAAAAALM/RSKv8--iWME/S220/IMG_0110.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_kRev2kjeMro/TQoqzqjos6I/AAAAAAAACSM/IzhmuchDow8/s72-c/SET.png' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-8550653524046546693</id><published>2010-10-07T09:52:00.000-07:00</published><updated>2010-10-07T10:07:07.390-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='puzzle'/><category scheme='http://www.blogger.com/atom/ns#' term='interview'/><category scheme='http://www.blogger.com/atom/ns#' term='amazon'/><category scheme='http://www.blogger.com/atom/ns#' term='numbers'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><category scheme='http://www.blogger.com/atom/ns#' term='matrix'/><category scheme='http://www.blogger.com/atom/ns#' term='coding'/><title type='text'>Print a Matrix in diagonal zig zag order</title><content type='html'>&lt;span style="font-size: large;"&gt;&lt;b&gt;Problem:&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;Given a square matrix, write a program to print the items in zig zag diagonal order.&lt;br /&gt;&lt;br /&gt;Following is an example,&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/_kRev2kjeMro/TK36s0VpF3I/AAAAAAAACQk/QnS7VOigH1o/s1600/Print_matrix_diagonally.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/_kRev2kjeMro/TK36s0VpF3I/AAAAAAAACQk/QnS7VOigH1o/s1600/Print_matrix_diagonally.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;So the output expected for the above example is,&lt;br /&gt;&lt;br /&gt;1 -&amp;gt; 2 -&amp;gt; 6 -&amp;gt; 3 -&amp;gt; 7 -&amp;gt; 11 -&amp;gt; 4 -&amp;gt; 8 -&amp;gt; 12 -&amp;gt; 16 -&amp;gt; 5 -&amp;gt; 9 -&amp;gt; 13 -&amp;gt; 17 -&amp;gt; 21 -&amp;gt; 10 -&amp;gt; 14 -&amp;gt; 18 -&amp;gt; 22 -&amp;gt; 15 -&amp;gt; 19 -&amp;gt; 23 -&amp;gt; 20 -&amp;gt; 24 -&amp;gt; 25&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size: large;"&gt;&lt;b&gt;Solution:&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;This program is a bit tricky and is very hard to solve immediately when asked in an interview. People who try to attack problems involving matrices always think they need at least two loops to arrive at the solution. But the surprise here is the problem can be solved in just a single loop with a very simple logic. I have provided the solution in C++ below, you may also try to solve the same in the language of your choice!! &lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:cpp"&gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;using namespace std;&lt;br /&gt;&lt;br /&gt;#define MAX 5&lt;br /&gt;&lt;br /&gt;int main(){&lt;br /&gt;    int a[MAX][MAX] = &lt;br /&gt;        {{ 1, 2, 3, 4, 5},&lt;br /&gt;         { 6, 7, 8, 9,10},&lt;br /&gt;         {11,12,13,14,15},&lt;br /&gt;         {16,17,18,19,20},&lt;br /&gt;         {21,22,23,24,25}};&lt;br /&gt;    int i=0,j=0;&lt;br /&gt;    while(i&amp;lt;MAX){&lt;br /&gt;         cout &amp;lt;&amp;lt; a[i][j] &amp;lt;&amp;lt; " -&amp;gt; ";&lt;br /&gt;         if(i==MAX-1){&lt;br /&gt;             i = j+1; j = MAX-1;&lt;br /&gt;         }&lt;br /&gt;         else if(j==0){&lt;br /&gt;             j = i+1; i = 0;&lt;br /&gt;         }&lt;br /&gt;         else{&lt;br /&gt;             i++; j--;&lt;br /&gt;         }&lt;br /&gt;    }&lt;br /&gt;    cout &amp;lt;&amp;lt; endl;&lt;br /&gt;     &lt;br /&gt;    return 0;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Cheers!!&lt;br /&gt;Jack&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-8550653524046546693?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/8550653524046546693/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=8550653524046546693' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/8550653524046546693'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/8550653524046546693'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/10/print-matrix-in-diagonal-zig-zag-order.html' title='Print a Matrix in diagonal zig zag order'/><author><name>Prabhu Jayaraman</name><uri>http://www.blogger.com/profile/11266928407768534504</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='30' src='http://4.bp.blogspot.com/_N_LXkcmZpN0/TAlTmeYTegI/AAAAAAAAALM/RSKv8--iWME/S220/IMG_0110.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_kRev2kjeMro/TK36s0VpF3I/AAAAAAAACQk/QnS7VOigH1o/s72-c/Print_matrix_diagonally.png' height='72' width='72'/><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-9111149051901612765</id><published>2010-09-25T10:41:00.000-07:00</published><updated>2010-09-30T13:13:26.708-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='puzzle'/><category scheme='http://www.blogger.com/atom/ns#' term='game'/><title type='text'>A Free game in Java</title><content type='html'>I wanted to play this game so badly as I use to be very good at it. I was not knowing the proper keyword to find it online. So, what the heck, I went ahead and created one! The front end used was Swing.&lt;br /&gt;The objective of this game is to arrange from numbers 1 through 24 (or depending on the size) in proper order and you have one square free. You can move to that empty sqaure by pressing either of the four arrow keys UP/DOWN/LEFT/RIGHT. All the puzzles formed will have a solution. You can use this code anywhere you want.&lt;br /&gt;&lt;br /&gt;If you have Java installed in your browser, you should be able to see the Applet. Just click anywhere inside the applet and start playing using the arrow keys! Have fun!&lt;br /&gt;&lt;br /&gt;&lt;center&gt;&lt;br /&gt;&lt;table&gt;&lt;tr&gt;   &lt;td&gt;Screenshot&lt;/td&gt;   &lt;td&gt;Applet&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt;   &lt;td&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/_kRev2kjeMro/TJ40SctOm2I/AAAAAAAACQc/AxSjjgEUBa4/s1600/15+puzzle+java.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="320" src="http://4.bp.blogspot.com/_kRev2kjeMro/TJ40SctOm2I/AAAAAAAACQc/AxSjjgEUBa4/s320/15+puzzle+java.png" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;/td&gt;    &lt;td&gt;&lt;center&gt;&lt;div&gt;&lt;applet width="320" height="320" align="center" codebase="http://technicalypto.741.com/BoxPuzzle/" code="boxgame/BoxPuzzle.class"&gt;&lt;/applet&gt;&lt;/div&gt;&lt;/center&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;br /&gt;&lt;/center&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:java"&gt;package boxgame;&lt;br /&gt;&lt;br /&gt;import java.awt.*;&lt;br /&gt;import java.awt.event.KeyEvent;&lt;br /&gt;import java.util.ArrayList;&lt;br /&gt;import java.util.List;&lt;br /&gt;&lt;br /&gt;import javax.swing.*;&lt;br /&gt;&lt;br /&gt;public class BoxPuzzle extends JFrame{&lt;br /&gt;    private static final long serialVersionUID = 1L;&lt;br /&gt;    &lt;br /&gt;    private static final int SIZE = 5;&lt;br /&gt;    JPanel[][] panelArray = new JPanel[SIZE][SIZE];&lt;br /&gt;    private boolean dialogShown = false;&lt;br /&gt;    &lt;br /&gt;    public BoxPuzzle(){&lt;br /&gt;        this.setTitle(SIZE*SIZE-1+&amp;quot; square puzzle&amp;quot;);&lt;br /&gt;        this.add(getGamePanel(SIZE));&lt;br /&gt;        this.setSize(SIZE*75, SIZE*75);&lt;br /&gt;        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);&lt;br /&gt;        this.setLocationRelativeTo(null);&lt;br /&gt;        for(int i=0;i&amp;lt;SIZE*100;i++)//RANDOMIZE MOVES&lt;br /&gt;            handleKeyPress((int)(Math.random()*4) + 37);&lt;br /&gt;        processKeys();&lt;br /&gt;        this.setVisible(true);&lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;    private void processKeys(){&lt;br /&gt;        KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher( &lt;br /&gt;            new KeyEventDispatcher()  {  &lt;br /&gt;                public boolean dispatchKeyEvent(KeyEvent e){&lt;br /&gt;                    if(e.getID() == KeyEvent.KEY_PRESSED){&lt;br /&gt;                        handleKeyPress(e.getKeyCode());&lt;br /&gt;                        if(areThingsInPlace() &amp;amp;&amp;amp; !dialogShown){&lt;br /&gt;                            dialogShown = true;&lt;br /&gt;                            JOptionPane.showMessageDialog(null,&amp;quot;Congratulations!!! YOU WIN!!&amp;quot;);&lt;br /&gt;                            System.exit(1);&lt;br /&gt;                        }&lt;br /&gt;                    }&lt;br /&gt;                    return false;&lt;br /&gt;                }  &lt;br /&gt;        });&lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;    private void handleKeyPress(int keyCode) {&lt;br /&gt;        int emptyIndex = findEmptyIndex();&lt;br /&gt;        int x = emptyIndex/SIZE;&lt;br /&gt;        int y = emptyIndex%SIZE;&lt;br /&gt;        &lt;br /&gt;        switch (keyCode) {&lt;br /&gt;        case 37://LEFT KEY&lt;br /&gt;            if(y==SIZE-1) return;&lt;br /&gt;            doSwap(x,y,x,y+1);&lt;br /&gt;            break;&lt;br /&gt;        case 38://UP KEY&lt;br /&gt;            if(x==SIZE-1) return;&lt;br /&gt;            doSwap(x,y,x+1,y);&lt;br /&gt;            break;&lt;br /&gt;        case 39://RIGHT KEY&lt;br /&gt;            if(y==0) return;&lt;br /&gt;            doSwap(x,y,x,y-1);&lt;br /&gt;            break;&lt;br /&gt;        case 40://DOWN KEY&lt;br /&gt;            if(x==0) return;&lt;br /&gt;            doSwap(x,y,x-1,y);&lt;br /&gt;            break;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;    private void doSwap(int x,int y,int x1,int y1){&lt;br /&gt;        Component temp1,temp2;&lt;br /&gt;        temp1 = panelArray[x][y].getComponent(0);&lt;br /&gt;        temp2 = panelArray[x1][y1].getComponent(0);&lt;br /&gt;        panelArray[x][y].remove(0);&lt;br /&gt;        panelArray[x1][y1].remove(0);&lt;br /&gt;        panelArray[x1][y1].add(temp1);&lt;br /&gt;        panelArray[x][y].add(temp2);&lt;br /&gt;        temp2.requestFocus();&lt;br /&gt;        this.repaint();&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    private int findEmptyIndex() {&lt;br /&gt;        for(int i=0;i&amp;lt;SIZE;i++)&lt;br /&gt;            for (int j=0;j&amp;lt;SIZE;j++)&lt;br /&gt;                if(!(panelArray[i][j].getComponent(0) instanceof JButton))&lt;br /&gt;                    return i*SIZE + j;&lt;br /&gt;        return 0;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    private boolean areThingsInPlace(){&lt;br /&gt;        for(int i=0;i&amp;lt;SIZE*SIZE-1;i++){&lt;br /&gt;            String name = (panelArray[i/SIZE][i%SIZE].getComponent(0)).getName();&lt;br /&gt;            if(name!=null &amp;amp;&amp;amp; !(name.equals(&amp;quot;&amp;quot;+i)))&lt;br /&gt;                return false;&lt;br /&gt;        }&lt;br /&gt;        return true;&lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;    public static void main(String args[]){&lt;br /&gt;        new BoxPuzzle();&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    private JPanel getGamePanel(int difficulty) {&lt;br /&gt;        JPanel pnlGame = new JPanel();&lt;br /&gt;        GridLayout matrixLayout = new GridLayout(difficulty, difficulty);&lt;br /&gt;        pnlGame.setLayout(matrixLayout);&lt;br /&gt;        &lt;br /&gt;        List&amp;lt;JComponent&amp;gt; componentsList = getRandomizedList(difficulty);&lt;br /&gt;        int index = 0;&lt;br /&gt;        for(int i=0;i&amp;lt;difficulty;i++){&lt;br /&gt;            panelArray[i] = new JPanel[difficulty];&lt;br /&gt;            for(int j=0;j&amp;lt;difficulty;j++){&lt;br /&gt;                panelArray[i][j] = new JPanel();&lt;br /&gt;                panelArray[i][j].setLayout(new GridLayout(1,1));&lt;br /&gt;                panelArray[i][j].add(componentsList.get(index++));&lt;br /&gt;                pnlGame.add(panelArray[i][j]);&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;        return pnlGame;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    private List&amp;lt;JComponent&amp;gt; getRandomizedList(int difficulty) {&lt;br /&gt;        List&amp;lt;JComponent&amp;gt; componentSet = new ArrayList&amp;lt;JComponent&amp;gt;();&lt;br /&gt;        for(int i=0;i&amp;lt;difficulty*difficulty-1;i++){&lt;br /&gt;            JButton button = new JButton(&amp;quot;&amp;quot;+(i+1));&lt;br /&gt;            button.setName(&amp;quot;&amp;quot;+i);&lt;br /&gt;            componentSet.add(button);&lt;br /&gt;        }&lt;br /&gt;        componentSet.add(new JPanel());&lt;br /&gt;        return componentSet;&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;Cheers,&lt;br /&gt;Bragaadeesh.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-9111149051901612765?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/9111149051901612765/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=9111149051901612765' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/9111149051901612765'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/9111149051901612765'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/09/free-game-in-java.html' title='A Free game in Java'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_kRev2kjeMro/TJ40SctOm2I/AAAAAAAACQc/AxSjjgEUBa4/s72-c/15+puzzle+java.png' height='72' width='72'/><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-8091719762029824815</id><published>2010-09-10T16:03:00.000-07:00</published><updated>2011-03-22T23:40:52.085-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='data structures'/><category scheme='http://www.blogger.com/atom/ns#' term='words'/><category scheme='http://www.blogger.com/atom/ns#' term='interview'/><category scheme='http://www.blogger.com/atom/ns#' term='tutorial'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><category scheme='http://www.blogger.com/atom/ns#' term='TRIE'/><title type='text'>Trie data structure - In C++</title><content type='html'>&lt;div style="margin: auto 33%;"&gt;&lt;ul id="pagination-digg"&gt;&lt;li class="previous"&gt;&lt;a href="http://www.technicalypto.com/2010/04/trie-data-structure-part-5-sample-ui.html"&gt;«Previous&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2010/04/trie-in-java.html"&gt;1&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2010/04/trie-data-structure-part-2-node-and.html"&gt;2&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2010/04/trie-data-structure-part-3-inserting.html"&gt;3&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2010/04/trie-data-structure-part-4-search.html"&gt;4&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2010/04/trie-data-structure-part-5-complexity.html"&gt;5&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2010/04/trie-data-structure-part-5-sample-ui.html"&gt;6&lt;/a&gt;&lt;/li&gt;&lt;li class="active"&gt;7&lt;/li&gt;&lt;li class="next-off"&gt;Next »&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;Having had a comprehensive coverage of the TRIE data structure in Java, me and my roommate thought it would achieve completion if we have the same implemented in C++. Don't get carried away by the length of the code. Its as simple and easy as the equivalent one in Java. You may go through the comprehensive tutorial &lt;a href="http://www.technicalypto.com/2010/04/trie-in-java.html"&gt;here&lt;/a&gt;. Trust me, it takes only 10 minutes!!.&lt;br /&gt;&lt;br /&gt;Please feel free to ask any questions if you face difficulties in understanding any part of the resource. I would respond to you immediately. &lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:cpp"&gt;/*&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ASSUMPTIONS:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; All characters are case insensitive&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ENHANCEMENTS:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Auto correction of word&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Display all words in postorder&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; SAMPLE DATA: - ALGO, ALL, ALSO, ASSOC, TREE, TRIE&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; +--&amp;gt; [G] ---+--&amp;gt; [O]&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; |&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; +--&amp;gt; [L] ---+--&amp;gt; [L]&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; |&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; |&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; +--&amp;gt; [A] ---+&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; +--&amp;gt; [S] ---+--&amp;gt; [O]&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; |&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; |&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; +--&amp;gt; [S] ---+--&amp;gt; [S] ---+--&amp;gt; [O] ---+--&amp;gt; [C]&lt;br /&gt;[\0] ---+&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; |&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; +--&amp;gt; [E] ---+--&amp;gt; [E]&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; |&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; |&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; +--&amp;gt; [T] ---+--&amp;gt; [R] ---+&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; |&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; +--&amp;gt; [I] ---+--&amp;gt; [E]&lt;br /&gt;&lt;br /&gt;*/&lt;br /&gt;&lt;br /&gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;#include &amp;lt;cctype&amp;gt;&lt;br /&gt;&lt;br /&gt;using namespace std;&lt;br /&gt;&lt;br /&gt;class trie&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; private:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; struct node&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; char&amp;nbsp; character;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // character of the node&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; bool&amp;nbsp; eow;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // indicates a complete word&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; int&amp;nbsp;&amp;nbsp; prefixes;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // indicates how many words have the prefix&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; node* edge[26];&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // references to all possible sons&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }*root;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // trie root node&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; void preorder_display(node *); // preorder display&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; void truncate_node(node *);&amp;nbsp;&amp;nbsp;&amp;nbsp; // Deletes node and sub-nodes&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; void delete_node(node *);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Deletes node if prefixes is 1&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; trie();&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // constructor&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ~trie();&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // destructor&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; void insert_word(char *);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // to insert a word&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; void delete_word(char *);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // to delete a word&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; bool search_word(char *);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // to search a word&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; void display();&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; // display complete trie&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;trie::trie()&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; root = new node();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; root-&amp;gt;character = '\0';&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; root-&amp;gt;prefixes = 0;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; root-&amp;gt;eow = false;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for(int i=0;i&amp;lt;26;i++)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; root-&amp;gt;edge[i] = NULL;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;trie::~trie()&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; truncate_node(root);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void trie::truncate_node(node* n)&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for(int i=0;i&amp;lt;26;i++)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; if(n-&amp;gt;edge[i] != NULL)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; truncate_node(n-&amp;gt;edge[i]);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; delete n;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void trie::insert_word(char* s)&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; node *t = root;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; while(*s != '\0')&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; int c = toupper(*s) - 'A';&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; if(t-&amp;gt;edge[c] == NULL)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; node* n = new node();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; n-&amp;gt;character = *s;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; n-&amp;gt;eow = false;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; n-&amp;gt;prefixes = 1;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; for(int i=0;i&amp;lt;26;i++)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; n-&amp;gt;edge[i] = NULL;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; t-&amp;gt;edge[c] = n;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; t = n;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; else&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; t = t-&amp;gt;edge[c];&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; t-&amp;gt;prefixes++;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; *s++;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; t-&amp;gt;eow = true;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;bool trie::search_word(char* s)&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; node *t = root;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; while(*s != '\0')&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; int c = toupper(*s) - 'A';&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; if(t-&amp;gt;edge[c] == NULL)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; return false;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; else&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; t = t-&amp;gt;edge[c];&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; *s++;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if(t-&amp;gt;eow == true)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; return true;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; else&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; return false;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void trie::delete_word(char* s)&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; node* t = root;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; while(*s != '\0')&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; int c = toupper(*s) - 'A';&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; if(t-&amp;gt;edge[c] == NULL)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; return;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; else if(t-&amp;gt;edge[c]-&amp;gt;prefixes == 1)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; truncate_node(t-&amp;gt;edge[c]);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; t-&amp;gt;edge[c] = NULL;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; return;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; else&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; t = t-&amp;gt;edge[c];&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; *s++;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void trie::display()&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; preorder_display(root);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void trie::preorder_display(node* t)&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if(t == NULL)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; return;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; "iterating :: " &amp;lt;&amp;lt; t-&amp;gt;character &amp;lt;&amp;lt; " :: " &amp;lt;&amp;lt; t-&amp;gt;eow &amp;lt;&amp;lt; " :: " &amp;lt;&amp;lt; t-&amp;gt;prefixes &amp;lt;&amp;lt; endl;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for(int i=0;i&amp;lt;26;i++)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; if(t-&amp;gt;edge[i] != NULL)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; preorder_display(t-&amp;gt;edge[i]);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;}&amp;nbsp;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size: large;"&gt;&lt;b&gt;Demonstration of trie operations&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:cpp"&gt;int main()&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; trie mytrie;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; char *s[] = {"tree","trie","algo","assoc","all","also","ass"};&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for(int i=0;i&amp;lt;sizeof(s)/sizeof(*s);i++)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; mytrie.insert_word(s[i]);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; mytrie.display();&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if(mytrie.search_word("all") == true) cout &amp;lt;&amp;lt; "all exist" &amp;lt;&amp;lt; endl;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; else cout &amp;lt;&amp;lt; "all do not exist" &amp;lt;&amp;lt; endl;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; mytrie.delete_word("all");&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if(mytrie.search_word("all") == true) cout &amp;lt;&amp;lt; "all exist" &amp;lt;&amp;lt; endl;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; else cout &amp;lt;&amp;lt; "all do not exist" &amp;lt;&amp;lt; endl;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; mytrie.display();&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Cheers!!&lt;br /&gt;Jack.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-8091719762029824815?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/8091719762029824815/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=8091719762029824815' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/8091719762029824815'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/8091719762029824815'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/09/trie-data-structure-in-c.html' title='Trie data structure - In C++'/><author><name>Prabhu Jayaraman</name><uri>http://www.blogger.com/profile/11266928407768534504</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='30' src='http://4.bp.blogspot.com/_N_LXkcmZpN0/TAlTmeYTegI/AAAAAAAAALM/RSKv8--iWME/S220/IMG_0110.jpg'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-605202082470946591</id><published>2010-09-09T02:56:00.000-07:00</published><updated>2010-09-09T03:05:04.234-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ruby'/><category scheme='http://www.blogger.com/atom/ns#' term='prime numbers'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><title type='text'>Efficient way to calculate prime numbers</title><content type='html'>In  several puzzles/coding competitions we may need to check for prime  numbers and this is required to be done in considerable time to save  precious run time. The traditional approach is,&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:cpp"&gt;bool is_prime(int x)&lt;br /&gt;{&lt;br /&gt;&amp;nbsp; if(x &amp;lt;= 1)&lt;br /&gt;&amp;nbsp;   return false;&lt;br /&gt;&amp;nbsp; for(int i = 2; i &amp;lt; x; i++)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if(x%i == 0)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return false;&lt;br /&gt;&amp;nbsp; return true;&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;This will however will loop through all elements below x to see if a factor exists. Example, for finding if 13 is prime, we check if its divisible by 2,3,4,....12. This can be optimized to loop through only half the elements since the highest factor is going to be num/2 as,&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:cpp"&gt;bool is_prime(int x)&lt;br /&gt;{&lt;br /&gt;&amp;nbsp; if(x &amp;lt;= 1)&lt;br /&gt;&amp;nbsp;   return false;&lt;br /&gt;&amp;nbsp; for(int i = 2; i &amp;lt;= x/2; i++)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if(x%i == 0)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return false;&lt;br /&gt;&amp;nbsp; return true;&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;But by analysis you can see that for a number say 100, the factors are (1*100),(2*50),(4*25),(5*20),(10*10),(20*5),(25*4),(50*2),(100*1). For finding out if 100 is prime or not we just need to check if 100 is divisible by any number not greater than 10 i.e., it is sufficient to check from 2 to 10. The reason being if its divisible by 4, its also divisible by 25.&lt;br /&gt;&lt;br /&gt;Hence the best approach to check if a number is prime or not will be&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:cpp"&gt;bool is_prime(int x)&lt;br /&gt;{&lt;br /&gt;&amp;nbsp; if(x &amp;lt;= 1)&lt;br /&gt;&amp;nbsp;   return false;&lt;br /&gt;&amp;nbsp; int s = (int) sqrt(x);&lt;br /&gt;&amp;nbsp; for(int i = 2; i &amp;lt;= s; i++)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if(x%i == 0)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return false;&lt;br /&gt;&amp;nbsp; return true;&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Final prime number program in Ruby (bonus)&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:ruby"&gt;def is_prime(n)&lt;br /&gt;&amp;nbsp; return false if n &amp;lt;= 1&lt;br /&gt;&amp;nbsp; 2.upto(Math.sqrt(n).to_i) do |x|&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return false if n%x == 0&lt;br /&gt;&amp;nbsp; end&lt;br /&gt;&amp;nbsp; true&lt;br /&gt;end&lt;/pre&gt;&lt;br /&gt;This approach will check for factors in very minimized number of loops. &lt;br /&gt;&lt;br /&gt;Cheers!!&lt;br /&gt;Bragaadeesh&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-605202082470946591?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/605202082470946591/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=605202082470946591' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/605202082470946591'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/605202082470946591'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/09/efficient-way-to-calculate-prime.html' title='Efficient way to calculate prime numbers'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-48254159627856479</id><published>2010-09-08T12:14:00.000-07:00</published><updated>2010-09-08T12:14:28.164-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='array'/><category scheme='http://www.blogger.com/atom/ns#' term='data structures'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><category scheme='http://www.blogger.com/atom/ns#' term='stack'/><title type='text'>Stack Implementation in C++ through an array</title><content type='html'>Stack is one of the important data structures that every computer programmer should be aware of. It follows the simple LIFO (Last In First Out) principle. Implementation of stack can be done in many ways. One of the simplest way is using Arrays. Here an array is initialized to a maximum value first, lets call it capacity. As and when we push elements onto the array, its size will get increased. When the size reaches the capacity, we should ideally double the array size. But in the code given below I am not doing that.&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/_kRev2kjeMro/TIfez9uBJPI/AAAAAAAACQA/ZY5EJ8RJXBw/s1600/stack.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/_kRev2kjeMro/TIfez9uBJPI/AAAAAAAACQA/ZY5EJ8RJXBw/s320/stack.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:cpp"&gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; using namespace std;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #define STACKSIZE 10&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; class stack&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; private:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int arr[STACKSIZE+1];&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int tos;&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; public:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; stack();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; void push(int x);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int&amp;nbsp; pop();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; bool is_empty();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; bool is_full();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int&amp;nbsp; size();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; void display();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; };&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; stack::stack()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; tos = 0;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; void stack::push(int x)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if(!is_full())&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arr[tos++] = x;&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; else&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; "Stack is full, Can not push " &amp;lt;&amp;lt; x &amp;lt;&amp;lt; endl;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int stack::pop()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if(!is_empty())&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return arr[--tos];&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; else&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; "Stack is empty, cannot pop" &amp;lt;&amp;lt; endl;&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; return -1;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; bool stack::is_empty()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if(tos == 0)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return true;&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; else&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return false;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; bool stack::is_full()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if(tos == STACKSIZE)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return true;&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; else&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return false;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int stack::size()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return tos;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; void stack::display()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if(tos == 0)&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; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; "No elements to display" &amp;lt;&amp;lt; endl;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return;&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; }&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; for(int i=0;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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; arr[i] &amp;lt;&amp;lt; " ";&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; cout &amp;lt;&amp;lt; endl;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int main()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; stack mystack;&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; cout &amp;lt;&amp;lt; mystack.size() &amp;lt;&amp;lt; endl;&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; mystack.push(1);&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(mystack.is_full())&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; "stack is full" &amp;lt;&amp;lt; endl;&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; mystack.pop();&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(mystack.is_empty())&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; "stack is empty" &amp;lt;&amp;lt; endl;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Cheers!!&lt;br /&gt;Bragaadeesh.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-48254159627856479?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/48254159627856479/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=48254159627856479' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/48254159627856479'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/48254159627856479'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/09/stack-implementation-in-c-through-array.html' title='Stack Implementation in C++ through an array'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_kRev2kjeMro/TIfez9uBJPI/AAAAAAAACQA/ZY5EJ8RJXBw/s72-c/stack.png' height='72' width='72'/><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-1405968303929471232</id><published>2010-09-08T12:05:00.000-07:00</published><updated>2010-09-26T05:36:09.831-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='life'/><category scheme='http://www.blogger.com/atom/ns#' term='motivation'/><category scheme='http://www.blogger.com/atom/ns#' term='off-topic'/><title type='text'>Motivation</title><content type='html'>&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/_kRev2kjeMro/TIrD--SOlRI/AAAAAAAACQI/zW2z6AE3Pkk/s1600/motivation.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="200" src="http://2.bp.blogspot.com/_kRev2kjeMro/TIrD--SOlRI/AAAAAAAACQI/zW2z6AE3Pkk/s200/motivation.jpg" width="167" /&gt;&lt;/a&gt;&lt;/div&gt;In his book, &lt;a href="http://en.wikipedia.org/wiki/The_Seven_Habits_of_Highly_Effective_People"&gt;7 Habits of Highly Effective People&lt;/a&gt;, Stephen Covey tells the story of a woodcutter who took a new job for a timber merchant.  The woodcutter was determined to do his best, so when the employer presented him with an axe he went straight to work.  The first day the woodcutter brought 18 trees to the boss.  “Congratulations,” the boss said.  “Keep it up!” With this motivational encouragement the cutter went out the next day with even more determination.  However, at the end of the day he could only bring back 15 trees.  The third day he worked harder still, yet try as he might only 10 trees could be felled.  So it went; each succeeding day yielded fewer trees.  “I must be losing my strength,” the woodcutter thought.  He decided to approach his boss to apologize for his unexplained deteriorating output.  “When was the last time you sharpened your axe?” the boss asked.  The woodcutter stared dumbfounded.  “Sharpen my axe!  I had no time to sharpen my axe.  I have been too busy trying to cut trees!”&lt;br /&gt;&lt;br /&gt;It is possible to be too busy to maintain your effectiveness. Whatever your personal situation, this might be the right time to ask yourself if it is time to “sharpen your axe.”&lt;br /&gt;&lt;br /&gt;Cheers!!&lt;br /&gt;Bragaadeesh.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-1405968303929471232?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/1405968303929471232/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=1405968303929471232' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/1405968303929471232'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/1405968303929471232'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/09/motivation.html' title='Motivation'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_kRev2kjeMro/TIrD--SOlRI/AAAAAAAACQI/zW2z6AE3Pkk/s72-c/motivation.jpg' height='72' width='72'/><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-2285447162228361784</id><published>2010-08-28T11:35:00.000-07:00</published><updated>2010-08-28T11:36:28.057-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ruby'/><category scheme='http://www.blogger.com/atom/ns#' term='project euler'/><category scheme='http://www.blogger.com/atom/ns#' term='euler'/><category scheme='http://www.blogger.com/atom/ns#' term='numbers'/><category scheme='http://www.blogger.com/atom/ns#' term='coding'/><title type='text'>Project Euler : Considering natural numbers of the form, ab, finding the maximum digital sum.</title><content type='html'>&lt;span style="font-size: large;"&gt;&lt;b&gt;Problem 56&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="problem_content"&gt;A googol (10&lt;img alt="^(" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=56" style="display: none;" /&gt;&lt;sup&gt;100&lt;/sup&gt;&lt;img alt=")" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=56" style="display: none;" /&gt;) is a massive number: one followed by one-hundred zeros; 100&lt;img alt="^(" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=56" style="display: none;" /&gt;&lt;sup&gt;100&lt;/sup&gt;&lt;img alt=")" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=56" style="display: none;" /&gt;  is almost unimaginably large: one followed by two-hundred zeros.  Despite their size, the sum of the digits in each number is only 1.&lt;br /&gt;Considering natural numbers of the form, &lt;i&gt;a&lt;img alt="^(" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=56" style="display: none;" /&gt;&lt;sup&gt;b&lt;/sup&gt;&lt;img alt=")" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=56" style="display: none;" /&gt;&lt;/i&gt;, where &lt;i&gt;a, b&lt;/i&gt; &amp;lt; 100, what is the maximum digital sum?&lt;/div&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="font-size: large;"&gt;Solution (in Ruby)&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Nothing much to discuss about the solution, I've implemented it directly as given in the problem statement.&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:ruby"&gt;max = []&lt;br /&gt;1.upto(99) { |x| 1.upto(99){|y| max &amp;lt;&amp;lt; (x**y).to_s.split(//).inject(0){|b,i| b+=i.to_i}} }&lt;br /&gt;puts "The maximum sum is #{max.max}"&lt;/pre&gt;&lt;br /&gt;&lt;span title="The maximum sum is 972"&gt;Hover here to see the solution&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Cheers!!&lt;br /&gt;Bragaadeesh&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-2285447162228361784?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/2285447162228361784/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=2285447162228361784' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/2285447162228361784'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/2285447162228361784'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/08/project-euler-considering-natural.html' title='Project Euler : Considering natural numbers of the form, ab, finding the maximum digital sum.'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-8721798387757487972</id><published>2010-08-27T12:19:00.000-07:00</published><updated>2010-08-27T12:19:32.555-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ruby'/><category scheme='http://www.blogger.com/atom/ns#' term='project euler'/><category scheme='http://www.blogger.com/atom/ns#' term='euler'/><category scheme='http://www.blogger.com/atom/ns#' term='numbers'/><category scheme='http://www.blogger.com/atom/ns#' term='coding'/><title type='text'>Project Euler : Evaluate the sum of all amicable pairs under 10000.</title><content type='html'>&lt;span style="font-size: large;"&gt;&lt;b&gt;Problem 21&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;div&gt;&lt;div class="problem_content"&gt;Let d(&lt;i&gt;n&lt;/i&gt;) be defined as the sum of proper divisors of &lt;i&gt;n&lt;/i&gt; (numbers less than &lt;i&gt;n&lt;/i&gt; which divide evenly into &lt;i&gt;n&lt;/i&gt;).&lt;br /&gt;If d(&lt;i&gt;a&lt;/i&gt;) = &lt;i&gt;b&lt;/i&gt; and d(&lt;i&gt;b&lt;/i&gt;) = &lt;i&gt;a&lt;/i&gt;, where &lt;i&gt;a&lt;/i&gt; &lt;img alt="≠" border="0" height="10" src="http://projecteuler.net/images/symbol_ne.gif" style="vertical-align: middle;" width="11" /&gt; &lt;i&gt;b&lt;/i&gt;, then &lt;i&gt;a&lt;/i&gt; and &lt;i&gt;b&lt;/i&gt; are an amicable pair and each of &lt;i&gt;a&lt;/i&gt; and &lt;i&gt;b&lt;/i&gt; are called amicable numbers.&lt;br /&gt;For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20,  22, 44, 55 and 110; therefore d(220) = 284. The proper divisors of 284  are 1, 2, 4, 71 and 142; so d(284) = 220.&lt;br /&gt;Evaluate the sum of all the amicable numbers under 10000.&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;&lt;span style="font-size: large;"&gt;&lt;b&gt;Solution (in Ruby)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;For this problem, it is vital to write an efficient method to find the sum of divisors of a number. The divisor finding logic is same as the one stated in &lt;a href="http://www.technicalypto.com/2010/08/project-euler-what-is-value-of-first.html"&gt;Problem 12&lt;/a&gt;. Rest is an as-is implementation of the problem statement.&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:ruby"&gt;def divisors_sum(n)&lt;br /&gt;&amp;nbsp; return 1 if n==1&lt;br /&gt;&amp;nbsp; sum = 0&lt;br /&gt;&amp;nbsp; 1.upto(Math.sqrt(n)) {|x| sum+= (x + n/x) if n%x == 0}&lt;br /&gt;&amp;nbsp; sum-=Math.sqrt(n).to_i if (Math.sqrt(n) - Math.sqrt(n).to_i) == 0&lt;br /&gt;&amp;nbsp; return sum-=n&lt;br /&gt;end&lt;br /&gt;sum = 0&lt;br /&gt;1.upto(9999) do |n|&lt;br /&gt;&amp;nbsp; b = divisors_sum(n)&lt;br /&gt;&amp;nbsp; a = divisors_sum(b)&lt;br /&gt;&amp;nbsp; sum+=n if(a!=b &amp;amp;&amp;amp; a==n)&lt;br /&gt;end&lt;br /&gt;puts "The sum of all amicable pairs under 10000 is #{sum}"&lt;/pre&gt;&lt;br /&gt;&lt;span title="The sum of all amicable pairs under 10000 is 31626"&gt;Hover here to see the solution&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Cheers!!&lt;br /&gt;Bragaadeesh&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-8721798387757487972?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/8721798387757487972/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=8721798387757487972' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/8721798387757487972'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/8721798387757487972'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/08/project-euler-evaluate-sum-of-all.html' title='Project Euler : Evaluate the sum of all amicable pairs under 10000.'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-1418882082183794841</id><published>2010-08-27T05:55:00.000-07:00</published><updated>2010-08-27T05:55:48.744-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ruby'/><category scheme='http://www.blogger.com/atom/ns#' term='project euler'/><category scheme='http://www.blogger.com/atom/ns#' term='euler'/><category scheme='http://www.blogger.com/atom/ns#' term='numbers'/><category scheme='http://www.blogger.com/atom/ns#' term='coding'/><title type='text'>Project Euler : How many letters would be needed to write all the numbers in words from 1 to 1000?</title><content type='html'>&lt;span style="font-size: large;"&gt;&lt;b&gt;Problem 17&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="problem_content"&gt;If the numbers 1 to 5 are written out in words: one, two, three,  four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.&lt;br /&gt;If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used? &lt;br /&gt;&lt;br /&gt;&lt;div class="info"&gt;&lt;b&gt;NOTE:&lt;/b&gt; Do not count spaces or hyphens. For  example, 342 (three hundred and forty-two) contains 23 letters and 115  (one hundred and fifteen) contains 20 letters. The use of "and" when  writing out numbers is in compliance with British usage.&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;&lt;span style="font-size: large;"&gt;&lt;b&gt;Solution (in Ruby)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;We will have to first try to come out with the unique words that are present in the numbers. We start with the ones which are one, two upto nine. Then the elevens starting from eleven to nineteen. Then the tens, twenties upto nineties. We should also define a hundred and thousand. After that the logic is directly readable from the ruby code given below.&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:ruby"&gt;ONES = ["zero","one","two","three","four","five","six","seven","eight","nine"]&lt;br /&gt;ELEVENS = ["eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"]&lt;br /&gt;TEN,AND = ["ten","and"]&lt;br /&gt;TENS = ["twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]&lt;br /&gt;HUNDRED, THOUSAND = ["hundred","thousand"]&lt;br /&gt;&lt;br /&gt;def nums_upto_99(n)&lt;br /&gt;&amp;nbsp; return ONES[n] if n &amp;lt; 10&lt;br /&gt;&amp;nbsp; return TEN if n==10&lt;br /&gt;&amp;nbsp; return "#{ELEVENS[n-11]}" if n &amp;lt; 20&lt;br /&gt;&amp;nbsp; return TENS[n/10-2] if n%10==0 &amp;amp;&amp;amp; n&amp;lt; 100&lt;br /&gt;&amp;nbsp; "#{TENS[n/10-2]} #{ONES[n%10]}"&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;def nums_upto_1000(n)&lt;br /&gt;&amp;nbsp; return nums_upto_99(n) if n&amp;lt;100&lt;br /&gt;&amp;nbsp; return "#{ONES[1]} #{THOUSAND}" if n==1000&lt;br /&gt;&amp;nbsp; return "#{ONES[n/100]} #{HUNDRED}" if n%100 == 0&lt;br /&gt;&amp;nbsp; "#{ONES[n/100]} #{HUNDRED} #{AND} #{nums_upto_99(n%100)}"&lt;br /&gt;end&lt;br /&gt;count = 0&lt;br /&gt;1.upto(1000){|n| count+=nums_upto_1000(n).gsub(/ /,'').size}&lt;br /&gt;puts "The total characters are #{count}"&lt;/pre&gt;&lt;br /&gt;&lt;span title="The total characters are 21124"&gt;Hover here to see the solution&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Cheers!!&lt;br /&gt;Bragaadeesh.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-1418882082183794841?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/1418882082183794841/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=1418882082183794841' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/1418882082183794841'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/1418882082183794841'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/08/project-euler.html' title='Project Euler : How many letters would be needed to write all the numbers in words from 1 to 1000?'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-3141799928014473884</id><published>2010-08-26T14:23:00.000-07:00</published><updated>2010-08-26T14:23:32.785-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ruby'/><category scheme='http://www.blogger.com/atom/ns#' term='project euler'/><category scheme='http://www.blogger.com/atom/ns#' term='euler'/><category scheme='http://www.blogger.com/atom/ns#' term='numbers'/><category scheme='http://www.blogger.com/atom/ns#' term='coding'/><title type='text'>Project Euler : Discover all the fractions with an unorthodox cancelling method.</title><content type='html'>&lt;span style="font-size: large;"&gt;&lt;b&gt;Problem 33&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="problem_content"&gt; &lt;br /&gt;The fraction &lt;img alt="^(" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=33" style="display: none;" /&gt;&lt;sup&gt;49&lt;/sup&gt;&lt;img alt=")" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=33" style="display: none;" /&gt;/&lt;img alt="_(" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=33" style="display: none;" /&gt;&lt;sub&gt;98&lt;/sub&gt;&lt;img alt=")" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=33" style="display: none;" /&gt; is a curious fraction, as an inexperienced mathematician in attempting to simplify it may incorrectly believe that &lt;img alt="^(" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=33" style="display: none;" /&gt;&lt;sup&gt;49&lt;/sup&gt;&lt;img alt=")" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=33" style="display: none;" /&gt;/&lt;img alt="_(" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=33" style="display: none;" /&gt;&lt;sub&gt;98&lt;/sub&gt;&lt;img alt=")" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=33" style="display: none;" /&gt; = &lt;img alt="^(" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=33" style="display: none;" /&gt;&lt;sup&gt;4&lt;/sup&gt;&lt;img alt=")" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=33" style="display: none;" /&gt;/&lt;img alt="_(" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=33" style="display: none;" /&gt;&lt;sub&gt;8&lt;/sub&gt;&lt;img alt=")" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=33" style="display: none;" /&gt;, which is correct, is obtained by cancelling the 9s.&lt;br /&gt;We shall consider fractions like, &lt;img alt="^(" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=33" style="display: none;" /&gt;&lt;sup&gt;30&lt;/sup&gt;&lt;img alt=")" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=33" style="display: none;" /&gt;/&lt;img alt="_(" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=33" style="display: none;" /&gt;&lt;sub&gt;50&lt;/sub&gt;&lt;img alt=")" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=33" style="display: none;" /&gt; = &lt;img alt="^(" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=33" style="display: none;" /&gt;&lt;sup&gt;3&lt;/sup&gt;&lt;img alt=")" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=33" style="display: none;" /&gt;/&lt;img alt="_(" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=33" style="display: none;" /&gt;&lt;sub&gt;5&lt;/sub&gt;&lt;img alt=")" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=33" style="display: none;" /&gt;, to be trivial examples.&lt;br /&gt;There are exactly four non-trivial examples of this type of fraction,  less than one in value, and containing two digits in the numerator and  denominator.&lt;br /&gt;If the product of these four fractions is given in its lowest common terms, find the value of the denominator.&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;span style="font-size: large;"&gt;&lt;b&gt;Solution (in Ruby)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;This problem unlike the other problems does not ask us to go to millions and trillions and the scope is such that this can be solved in the least running time. All this problem demands careful inspection of the problem statement itself. Its clear we need to iterate through numbers from 1 to 99. For the outer loop (numerator), its enough we run from 12 since its the first double digit number which does not end with a 0 or their digits being equal. We run upto 97 since the numerator cannot be greater than equal to denominator since the fraction should be less than 1&lt;br /&gt;Same is the case for the inner loop. We should always continue to the next iteration if the last number of either numerator or denominator is 0, or if both digits or equal or if the numerator is greater than or equal to denominator. Rest of the solution is just a language specific implementation of finding whether the two different fractions are indeed equal. &lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:ruby"&gt;n_s, d_s = [1,1]&lt;br /&gt;12.upto(97) do |n|&lt;br /&gt;&amp;nbsp; 13.upto(98) do |d|&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; next if n/10 == n%10 || d/10 == d%10 || n%10 ==0 || d%10 ==0 || n&amp;gt;=d&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; a,b = [n.to_s.split(//), d.to_s.split(//)]&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (a&amp;amp;b).size == 1 &amp;amp;&amp;amp; (a-(a&amp;amp;b))[0].to_f / (b-(a&amp;amp;b))[0].to_f == n.to_f/d.to_f&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; n_s*=(a-(a&amp;amp;b))[0].to_i&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; d_s*=(b-(a&amp;amp;b))[0].to_i&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; end&lt;br /&gt;&amp;nbsp; end&lt;br /&gt;end&lt;br /&gt;puts "The product in its lowest common terms is #{d_s/n_s}"&lt;/pre&gt;&lt;br /&gt;&lt;span title="The product in its lowest common terms is 100"&gt;Hover here to see the solution&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Cheers!!&lt;br /&gt;Bragaadeesh.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-3141799928014473884?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/3141799928014473884/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=3141799928014473884' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/3141799928014473884'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/3141799928014473884'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/08/project-euler-discover-all-fractions.html' title='Project Euler : Discover all the fractions with an unorthodox cancelling method.'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-3765353664173389582</id><published>2010-08-26T13:45:00.000-07:00</published><updated>2010-08-26T13:45:01.874-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ruby'/><category scheme='http://www.blogger.com/atom/ns#' term='project euler'/><category scheme='http://www.blogger.com/atom/ns#' term='euler'/><category scheme='http://www.blogger.com/atom/ns#' term='numbers'/><category scheme='http://www.blogger.com/atom/ns#' term='coding'/><title type='text'>Project Euler : What is the value of the first triangle number to have over five hundred divisors?</title><content type='html'>&lt;span style="font-size: large;"&gt;&lt;b&gt;Problem 12&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="problem_content"&gt;The sequence of triangle numbers is generated by adding the natural numbers. So the 7&lt;img alt="^(" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=12" style="display: none;" /&gt;&lt;sup&gt;th&lt;/sup&gt;&lt;img alt=")" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=12" style="display: none;" /&gt; triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:&lt;br /&gt;&lt;div style="text-align: center;"&gt;1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...&lt;/div&gt;Let us list the factors of the first seven triangle numbers:&lt;br /&gt;&lt;blockquote style="font-family: courier new;"&gt;&lt;b&gt;&amp;nbsp;1&lt;/b&gt;: 1&lt;br /&gt;&lt;b&gt;&amp;nbsp;3&lt;/b&gt;: 1,3&lt;br /&gt;&lt;b&gt;&amp;nbsp;6&lt;/b&gt;: 1,2,3,6&lt;br /&gt;&lt;b&gt;10&lt;/b&gt;: 1,2,5,10&lt;br /&gt;&lt;b&gt;15&lt;/b&gt;: 1,3,5,15&lt;br /&gt;&lt;b&gt;21&lt;/b&gt;: 1,3,7,21&lt;br /&gt;&lt;b&gt;28&lt;/b&gt;: 1,2,4,7,14,28&lt;/blockquote&gt;We can see that 28 is the first triangle number to have over five divisors.&lt;br /&gt;What is the value of the first triangle number to have over five hundred divisors?&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size: large;"&gt;&lt;b&gt;Solution (in Ruby)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The main aspect about this solution is finding an efficient way to calculate the number of divisors. Lets take a simple example. The number 6 has 4 divisors namely 1,2,3,6. For this number its enough we run upto the Square root of 6 which is integer floored value to be 2. 1 is divisible by 6 by 6 times, so 1 and 6 are divisors. 2 is divisble by 6 3 times, so 2 and 3 are divisors. So totally we have 4 divisors.&lt;br /&gt;There is one more point to be noted here. For a square number such as 16, if we apply this formula, we will end up getting 6 divisors --&amp;gt; 1, 2, 4, 4, 8, 16. You may see we may calculate 4 twice. But it occurs only once for a square number. This special case is handled in the solution given below. The rest is an as-is implementation of the problem statement. &lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:ruby"&gt;def divisors_count(n)&lt;br /&gt;&amp;nbsp; return 1 if n==1&lt;br /&gt;&amp;nbsp; count = 0&lt;br /&gt;&amp;nbsp; 1.upto(Math.sqrt(n)) do |x|&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if n%x == 0&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; count+=1 unless n.to_f/x == x&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; count+=1&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; end&lt;br /&gt;&amp;nbsp; end&lt;br /&gt;&amp;nbsp; count&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;INFINITY = 1.0/0.0&lt;br /&gt;1.upto(INFINITY) do |n|&lt;br /&gt;&amp;nbsp; if divisors_count(n*(n+1)/2) &amp;gt; 500&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; puts "The first triangle number to have over 500 divisors is #{n*(n+1)/2}"&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; break&lt;br /&gt;&amp;nbsp; end&lt;br /&gt;end&lt;/pre&gt;&lt;br /&gt;&lt;span title="The first triangle number to have over 500 divisors is 76576500"&gt;Hover here to see the solution&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Cheers!!&lt;br /&gt;Bragaadeesh.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-3765353664173389582?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/3765353664173389582/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=3765353664173389582' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/3765353664173389582'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/3765353664173389582'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/08/project-euler-what-is-value-of-first.html' title='Project Euler : What is the value of the first triangle number to have over five hundred divisors?'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-5801493396049082251</id><published>2010-08-26T07:02:00.000-07:00</published><updated>2010-08-26T07:02:49.384-07:00</updated><title type='text'>Project Euler : How many Lychrel numbers are there below ten-thousand?</title><content type='html'>&lt;b&gt;&lt;span style="font-size: large;"&gt;Problem 55&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="problem_content"&gt;If we take 47, reverse and add, 47 + 74 = 121, which is palindromic.&lt;br /&gt;Not all numbers produce palindromes so quickly. For example,&lt;br /&gt;&lt;div style="margin-left: 50px;"&gt;349 + 943 = 1292,&lt;br /&gt;1292 + 2921 = 4213&lt;br /&gt;4213 + 3124 = 7337&lt;/div&gt;That is, 349 took three iterations to arrive at a palindrome.&lt;br /&gt;Although no one has proved it yet, it is thought that some numbers,  like 196, never produce a palindrome. A number that never forms a  palindrome through the reverse and add process is called a Lychrel  number. Due to the theoretical nature of these numbers, and for the  purpose of this problem, we shall assume that a number is Lychrel until  proven otherwise. In addition you are given that for every number below  ten-thousand, it will either (i) become a palindrome in less than fifty  iterations, or, (ii) no one, with all the computing power that exists,  has managed so far to map it to a palindrome. In fact, 10677 is the  first number to be shown to require over fifty iterations before  producing a palindrome: 4668731596684224866951378664 (53 iterations,  28-digits).&lt;br /&gt;Surprisingly, there are palindromic numbers that are themselves Lychrel numbers; the first example is 4994.&lt;br /&gt;How many Lychrel numbers are there below ten-thousand?&lt;br /&gt;&lt;div class="info"&gt;NOTE: Wording was modified slightly on 24 April 2007 to emphasise the theoretical nature of Lychrel numbers.&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;&lt;span style="font-size: large;"&gt;&lt;b&gt;Solution (in Ruby)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;This problem had a higher difficulty rating in project euler problems. But it was one of the straightforward ones that I solved it with Ruby. Nothing to say about the solution, its an as-is implementation of the problem statement.&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:ruby"&gt;def is_lynchrel(n)&lt;br /&gt;&amp;nbsp; 50.times do&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; n += n.to_s.reverse.to_i&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return false if n.to_s == n.to_s.reverse&lt;br /&gt;&amp;nbsp; end&lt;br /&gt;&amp;nbsp; true&lt;br /&gt;end&lt;br /&gt;count = 0&lt;br /&gt;1.upto(9999) do |n|&lt;br /&gt;&amp;nbsp; count +=1 if is_lynchrel(n)&lt;br /&gt;end&lt;br /&gt;puts "Lynchrel numbers below 10000 is #{count}"&lt;/pre&gt;&lt;br /&gt;&lt;span title="Lynchrel numbers below 10000 is 249"&gt;Hover here to see the solution&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Cheers!!&lt;br /&gt;Bragaadeesh.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-5801493396049082251?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/5801493396049082251/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=5801493396049082251' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/5801493396049082251'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/5801493396049082251'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/08/project-euler-how-many-lychrel-numbers.html' title='Project Euler : How many Lychrel numbers are there below ten-thousand?'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-631946216596833431</id><published>2010-08-26T03:58:00.000-07:00</published><updated>2010-08-26T03:59:08.449-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ruby'/><category scheme='http://www.blogger.com/atom/ns#' term='project euler'/><category scheme='http://www.blogger.com/atom/ns#' term='euler'/><category scheme='http://www.blogger.com/atom/ns#' term='numbers'/><category scheme='http://www.blogger.com/atom/ns#' term='coding'/><title type='text'>Project Euler : Concealed Square</title><content type='html'>&lt;b&gt;&lt;span style="font-size: large;"&gt;Problem 206&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Find the unique positive integer whose square has the form 1_2_3_4_5_6_7_8_9_0,&lt;br /&gt;where each “_” is a single digit.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size: large;"&gt;&lt;b&gt;Solution (in Ruby)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Before we look at the optimal solution, lets look at what brute force has to offer us. There are nine slots in the number which leaves us to iterate on 1000000000 times (1 trillion times). This is simply not doable, it will take years to run through this loop. So brute force is out of the equation. We need to find a digit or two to reduce the loop count.&lt;br /&gt;&lt;br /&gt;So its wiser to look at some of the property of square numbers. Square number ending with zero, has to have the previous digit also 0, which leaves us with only 8 slots now. One more property is that the square root for this particular number has to start with 1. The reason is in finding the square root, we need pair numbers from the back and need to find the square root of the first hanging digit/pair. In this case its 1. Square root 1 is 1 and its enough to inspect only one digit. This leaves us 1 more slot less in our problem. And one final property is the ending digit. Here its 9. Only numbers that end with 3 and 7 produces 9. This leaves our permutation reduce by one more digit (since its enough for us to step 10 times every time in the loop). We need to find the lower bound of the number which is 10203040506070809. The square root of this number is 101010101. We shall end with either 3 or 7. And start with the maximum digit in that sequence which is 199999999. Thats it!! Now we've shortened the problem to be solved within seconds!!&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:ruby"&gt;(101010107..199999999).step(10) do |n|&lt;br /&gt;&amp;nbsp; if ((n*n).to_s.gsub /(.)./,'\1') == "123456789" || (((n-4)*(n-4)).to_s.gsub /(.)./,'\1') == "123456789"&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; puts "The unique positive number is #{n*10}"&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; break&lt;br /&gt;&amp;nbsp; end&lt;br /&gt;end&lt;/pre&gt;&lt;br /&gt;&lt;span title="The unique positive number is 1389019170"&gt;Hover here to see the solution&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Cheers!!&lt;br /&gt;Bragaadeesh&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-631946216596833431?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/631946216596833431/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=631946216596833431' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/631946216596833431'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/631946216596833431'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/08/project-euler-concealed-square.html' title='Project Euler : Concealed Square'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-3334523220694428768</id><published>2010-08-25T13:51:00.000-07:00</published><updated>2010-08-25T13:53:03.883-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ruby'/><category scheme='http://www.blogger.com/atom/ns#' term='project euler'/><category scheme='http://www.blogger.com/atom/ns#' term='euler'/><category scheme='http://www.blogger.com/atom/ns#' term='numbers'/><category scheme='http://www.blogger.com/atom/ns#' term='coding'/><title type='text'>Project Euler : How many circular primes are there below one million?</title><content type='html'>&lt;span style="font-size: large;"&gt;&lt;b&gt;Problem 35&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="problem_content"&gt;&lt;br /&gt;The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime.&lt;br /&gt;There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97.&lt;br /&gt;How many circular primes are there below one million?&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="font-size: large;"&gt;Solution (in Ruby)&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;The solution to this problem invovles one performance enhancement in finding the prime numbers. If you want to check whether the same number is prime more than once in your program, it is highly important to have a look on the running time. That is why I am using an array to store the number as the index itself. This would make sure that the number given is found to be prime or not in O(1) time. Rest of the solution did what the problem statement demands&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:ruby"&gt;def find_prime(n)&lt;br /&gt;&amp;nbsp; return false if n &amp;lt;= 1&lt;br /&gt;&amp;nbsp; 2.upto(Math.sqrt(n).to_i) do |x|&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return false if n%x == 0&lt;br /&gt;&amp;nbsp; end&lt;br /&gt;&amp;nbsp; true&lt;br /&gt;end&lt;br /&gt;large = []&lt;br /&gt;1.upto(1000000) { |each_num| large[each_num] = true if find_prime(each_num) }&lt;br /&gt;count = 0&lt;br /&gt;1.upto(1000000) do |num|&lt;br /&gt;&amp;nbsp; str = num.to_s&lt;br /&gt;&amp;nbsp; all = (0...str.length).collect { |i| large[((str * 2)[i, str.length]).to_i] ? true : false }&lt;br /&gt;&amp;nbsp; count+=1 if all.inject(0){|b,i| b&amp;amp;&amp;amp;=i}&lt;br /&gt;end&lt;br /&gt;puts "The count of such primes is #{count}"&lt;/pre&gt;&lt;br /&gt;&lt;span title=""&gt;Hover here to see the solution&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Cheers!!&lt;br /&gt;Bragaadeesh&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-3334523220694428768?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/3334523220694428768/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=3334523220694428768' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/3334523220694428768'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/3334523220694428768'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/08/project-euler-how-many-circular-primes.html' title='Project Euler : How many circular primes are there below one million?'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-3351104857974373612</id><published>2010-08-25T12:00:00.000-07:00</published><updated>2010-08-25T12:02:24.058-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ruby'/><category scheme='http://www.blogger.com/atom/ns#' term='project euler'/><category scheme='http://www.blogger.com/atom/ns#' term='euler'/><category scheme='http://www.blogger.com/atom/ns#' term='numbers'/><category scheme='http://www.blogger.com/atom/ns#' term='coding'/><title type='text'>Project Euler : Find the sum of all numbers less than one million, which are palindromic in base 10 and base 2.</title><content type='html'>&lt;b&gt;&lt;span style="font-size: large;"&gt;Problem 36&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="problem_content"&gt;The decimal number, 585 = 1001001001&lt;img alt="_(" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=36" style="display: none;" /&gt;&lt;sub&gt;2&lt;/sub&gt;&lt;img alt=")" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=36" style="display: none;" /&gt; (binary), is palindromic in both bases.&lt;br /&gt;Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2.&lt;br /&gt;&lt;div class="info"&gt;(Please note that the palindromic number, in either base, may not include leading zeros.)&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;&lt;span style="font-size: large;"&gt;&lt;b&gt;Solution (in Ruby)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The solution is one the simplest and straightforward when implemented in Ruby.&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:ruby"&gt;sum = 0&lt;br /&gt;1.upto(1000000) do |num|&lt;br /&gt;&amp;nbsp; sum+=num if num.to_s == num.to_s.reverse &amp;amp;&amp;amp; num.to_s(base=2) == num.to_s(base=2).reverse&lt;br /&gt;end&lt;br /&gt;puts "Sum is #{sum}"&lt;/pre&gt;&lt;br /&gt;&lt;span title="Sum is 872187"&gt;Hover here to see the solution&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Cheers!!&lt;br /&gt;Bragaadeesh&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-3351104857974373612?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/3351104857974373612/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=3351104857974373612' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/3351104857974373612'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/3351104857974373612'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/08/project-euler-find-sum-of-all-numbers_25.html' title='Project Euler : Find the sum of all numbers less than one million, which are palindromic in base 10 and base 2.'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-2862038268447166737</id><published>2010-08-25T11:45:00.000-07:00</published><updated>2010-08-25T11:58:07.257-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ruby'/><category scheme='http://www.blogger.com/atom/ns#' term='project euler'/><category scheme='http://www.blogger.com/atom/ns#' term='euler'/><category scheme='http://www.blogger.com/atom/ns#' term='numbers'/><category scheme='http://www.blogger.com/atom/ns#' term='coding'/><title type='text'>Project Euler : How many triangle words does the list of common English words contain?</title><content type='html'>&lt;span style="font-size: large;"&gt;&lt;b&gt;Problem 42&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="problem_content"&gt;The &lt;i&gt;n&lt;/i&gt;&lt;img alt="^(" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=42" style="display: none;" /&gt;&lt;sup&gt;th&lt;/sup&gt;&lt;img alt=")" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=42" style="display: none;" /&gt; term of the sequence of triangle numbers is given by, &lt;i&gt;t&lt;img alt="_(" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=42" style="display: none;" /&gt;&lt;sub&gt;n&lt;/sub&gt;&lt;img alt=")" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=42" style="display: none;" /&gt;&lt;/i&gt; = ½&lt;i&gt;n&lt;/i&gt;(&lt;i&gt;n&lt;/i&gt;+1); so the first ten triangle numbers are:&lt;br /&gt;&lt;div style="text-align: center;"&gt;1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...&lt;/div&gt;By converting each letter in a word to a number corresponding to its  alphabetical position and adding these values we form a word value. For  example, the word value for SKY is 19 + 11 + 25 = 55 = &lt;i&gt;t&lt;/i&gt;&lt;img alt="_(" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=42" style="display: none;" /&gt;&lt;sub&gt;10&lt;/sub&gt;&lt;img alt=")" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=42" style="display: none;" /&gt;. If the word value is a triangle number then we shall call the word a triangle word.&lt;br /&gt;Using &lt;a href="http://projecteuler.net/project/words.txt"&gt;words.txt&lt;/a&gt;  (right click and 'Save Link/Target As...'), a 16K text file containing  nearly two-thousand common English words, how many are triangle words?&lt;/div&gt;&lt;br /&gt;&lt;span style="font-size: large;"&gt;&lt;b&gt;Solution (in Ruby)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The main place where we need to attack this problem involves in finding the roots of the quadratic equation. We know the roots of the quadratic equation &lt;a href="http://1.bp.blogspot.com/_kRev2kjeMro/THVjeSvQkAI/AAAAAAAACPo/F_gpN1xmWHo/s1600/equation%282%29.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://1.bp.blogspot.com/_kRev2kjeMro/THVjeSvQkAI/AAAAAAAACPo/F_gpN1xmWHo/s320/equation%282%29.png" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/_kRev2kjeMro/THVjuX2vncI/AAAAAAAACPw/cahqBdpVOsk/s1600/equation.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/_kRev2kjeMro/THVjuX2vncI/AAAAAAAACPw/cahqBdpVOsk/s320/equation.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;If you apply this equation to the simple formula to find the triangle numbers, we will find out that a = 1 and b = 1 and c will be twice the triangle number. There will be two roots possible, one will be positive and other will be negative. It is enough to find the positive root. If that root is a whole number, then that will be a proper triangle number.&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:ruby"&gt;names = File.new("/words.txt","r").gets.split(/,/)&lt;br /&gt;count = 0&lt;br /&gt;names.each do |n|&lt;br /&gt;&amp;nbsp; value = n.gsub!(/^"(.*?)"$/,'\1').split(//).inject(0){|b,i| b+i[0]-64}&lt;br /&gt;&amp;nbsp; positive_root = (-1+Math.sqrt(1+8*value))/2&lt;br /&gt;&amp;nbsp; count+=1 if positive_root == positive_root.ceil&lt;br /&gt;end&lt;br /&gt;puts "The total number of words is #{count}"&lt;/pre&gt;&lt;br /&gt;&lt;span title="The total number of words is 162"&gt;Hover here to see the solution&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Cheers!!&lt;br /&gt;Bragaadeesh&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-2862038268447166737?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/2862038268447166737/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=2862038268447166737' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/2862038268447166737'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/2862038268447166737'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/08/project-euler-how-many-triangle-words.html' title='Project Euler : How many triangle words does the list of common English words contain?'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_kRev2kjeMro/THVjeSvQkAI/AAAAAAAACPo/F_gpN1xmWHo/s72-c/equation%282%29.png' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-4381658622626351540</id><published>2010-08-23T13:08:00.000-07:00</published><updated>2010-08-23T13:09:03.878-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ruby'/><category scheme='http://www.blogger.com/atom/ns#' term='project euler'/><category scheme='http://www.blogger.com/atom/ns#' term='euler'/><category scheme='http://www.blogger.com/atom/ns#' term='numbers'/><category scheme='http://www.blogger.com/atom/ns#' term='coding'/><title type='text'>Project Euler : Using an efficient algorithm find the maximal sum in the triangle?</title><content type='html'>&lt;span style="font-size: large;"&gt;&lt;b&gt;Problem 67&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;By starting at the top of the triangle below and moving to adjacent  numbers on the row below, the maximum total from top to bottom is 23.&lt;br /&gt;&lt;div style="font-family: courier new; font-size: 12pt; text-align: center;"&gt;&lt;span style="color: red;"&gt;&lt;b&gt;3&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: red;"&gt;&lt;b&gt;7&lt;/b&gt;&lt;/span&gt; 4&lt;br /&gt;2 &lt;span style="color: red;"&gt;&lt;b&gt;4&lt;/b&gt;&lt;/span&gt; 6&lt;br /&gt;8 5 &lt;span style="color: red;"&gt;&lt;b&gt;9&lt;/b&gt;&lt;/span&gt; 3&lt;/div&gt;That is, 3 + 7 + 4 + 9 = 23.&lt;br /&gt;Find the maximum total from top to bottom in &lt;a href="http://projecteuler.net/project/triangle.txt"&gt;triangle.txt&lt;/a&gt; (right click and 'Save Link/Target As...'), a 15K text file containing a triangle with one-hundred rows.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size: large;"&gt;&lt;b&gt;Solution (in Ruby) &lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;To attack this problem, we should think in the reverse order as exploring all the possible routes would take 20 billion years to solve this problem!! We should start at the bottom but before level. For each number in that level, replace with the sum of that number and the maximum of the below two numbers. Do this for each level and bingo! we will arrive at the solution. So easy now isnt it? :) Thats 'after' you know it. Ruby code is presented below. There is another problem (Problem 18) (the minor input version) which also is the same one like the one above. &lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:ruby"&gt;input = ''&lt;br /&gt;&lt;br /&gt;f = File.new("/triangle.txt","r")&lt;br /&gt;while (line = f.gets)&lt;br /&gt;&amp;nbsp; input += line&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;class Row&lt;br /&gt;&amp;nbsp; attr_accessor :index, :data&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;index = 0&lt;br /&gt;triangle =&amp;nbsp; input.lines.map{|each_line| r = Row.new; r.index = index; r.data=[]; index+=1; sub_index = 0&lt;br /&gt;&amp;nbsp; each_line.split.map{|n| r.data[sub_index] = n.to_i; sub_index+=1}; r}&lt;br /&gt;&lt;br /&gt;(triangle.size - 2).downto(0) do |n|&lt;br /&gt;&amp;nbsp; 0.upto(triangle[n].data.size-1) do |sub_index|&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; triangle[n].data[sub_index] += [ triangle[n+1].data[sub_index+1] , triangle[n+1].data[sub_index] ].max&lt;br /&gt;&amp;nbsp; end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;puts "The maximum value is #{triangle[0].data}"&lt;/pre&gt;&lt;br /&gt;&lt;span title="The maximum value is 7273"&gt;Hover here to see the solution&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Cheers!!&lt;br /&gt;Bragaadeesh&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-4381658622626351540?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/4381658622626351540/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=4381658622626351540' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/4381658622626351540'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/4381658622626351540'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/08/project-euler-using-efficient-algorithm.html' title='Project Euler : Using an efficient algorithm find the maximal sum in the triangle?'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-8740279252170126297</id><published>2010-08-23T04:09:00.000-07:00</published><updated>2010-08-23T04:10:20.683-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ruby'/><category scheme='http://www.blogger.com/atom/ns#' term='project euler'/><category scheme='http://www.blogger.com/atom/ns#' term='euler'/><category scheme='http://www.blogger.com/atom/ns#' term='numbers'/><category scheme='http://www.blogger.com/atom/ns#' term='coding'/><title type='text'>Project Euler : Find a quadratic formula that produces the maximum number of primes for consecutive values of n.</title><content type='html'>&lt;span style="font-size: large;"&gt;&lt;b&gt;Problem 27&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="problem_content"&gt;Euler published the remarkable quadratic formula:&lt;br /&gt;&lt;div style="text-align: center;"&gt;&lt;i&gt;n&lt;/i&gt;² + &lt;i&gt;n&lt;/i&gt; + 41&lt;/div&gt;It turns out that the formula will produce 40 primes for the consecutive values &lt;i&gt;n&lt;/i&gt; = 0 to 39. However, when &lt;i&gt;n&lt;/i&gt; = 40, 40&lt;img alt="^(" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=27" style="display: none;" /&gt;&lt;sup&gt;2&lt;/sup&gt;&lt;img alt=")" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=27" style="display: none;" /&gt; + 40 + 41 = 40(40 + 1) + 41 is divisible by 41, and certainly when &lt;i&gt;n&lt;/i&gt; = 41, 41² + 41 + 41 is clearly divisible by 41.&lt;br /&gt;Using computers, the incredible formula &amp;nbsp;&lt;i&gt;n&lt;/i&gt;² - 79&lt;i&gt;n&lt;/i&gt; + 1601 was discovered, which produces 80 primes for the consecutive values &lt;i&gt;n&lt;/i&gt; = 0 to 79. The product of the coefficients, -79 and 1601, is -126479.&lt;br /&gt;Considering quadratics of the form:&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;n&lt;/i&gt;² + &lt;i&gt;an&lt;/i&gt; + &lt;i&gt;b&lt;/i&gt;, where |&lt;i&gt;a&lt;/i&gt;| &amp;lt; 1000 and |&lt;i&gt;b&lt;/i&gt;| &amp;lt; 1000&lt;br /&gt;&lt;br /&gt;&lt;div class="info" style="text-align: right;"&gt;where |&lt;i&gt;n&lt;/i&gt;| is the modulus/absolute value of &lt;i&gt;n&lt;/i&gt;&lt;br /&gt;e.g. |11| = 11 and |-4| = 4&lt;/div&gt;&lt;/blockquote&gt;Find the product of the coefficients, &lt;i&gt;a&lt;/i&gt; and &lt;i&gt;b&lt;/i&gt;, for the quadratic expression that produces the maximum number of primes for consecutive values of &lt;i&gt;n&lt;/i&gt;, starting with &lt;i&gt;n&lt;/i&gt; = 0.&lt;/div&gt;&lt;br /&gt;&lt;span style="font-size: large;"&gt;&lt;b&gt;Solution (in Ruby) &lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Normally we would want to run through 1000 numbers in the outer loop and 1000 numbers in the inner loop to attack this problem. But we can significantly reduce that by just running 168 x 168 times by calculating the number of primes below 1000 first. This would reduce our running time. The rest of the solution is an as-is implementation of the problem statement.&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:ruby"&gt;def is_prime(n)&lt;br /&gt;&amp;nbsp; return false if n &amp;lt;= 1&lt;br /&gt;&amp;nbsp; 2.upto(Math.sqrt(n).to_i) do |x|&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return false if n%x == 0&lt;br /&gt;&amp;nbsp; end&lt;br /&gt;&amp;nbsp; true&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;max_primes = []; a_s = []; b_s = []&lt;br /&gt;INFINITY = 1.0/0.0&lt;br /&gt;&lt;br /&gt;primes_upto_1000 = []&lt;br /&gt;1.upto(999) do |each_num|&lt;br /&gt;&amp;nbsp; primes_upto_1000 &amp;lt;&amp;lt; each_num if is_prime(each_num)&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;primes_upto_1000.each do |a|&lt;br /&gt;&amp;nbsp; primes_upto_1000.each do |b|&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; each_prime = 0&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0.upto(INFINITY) do |n|&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break unless is_prime(n**2 - a*n + b)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; each_prime+=1 &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; end&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; max_primes&amp;lt;&lt;each_prime; a_s=""&gt;&amp;lt;&amp;lt; -a ; b_s &amp;lt;&amp;lt; b&lt;br /&gt;&amp;nbsp; end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;puts "Product of a and b is #{a_s[max_primes.index(max_primes.max)] * b_s[max_primes.index(max_primes.max)]}" &lt;/pre&gt;&lt;br /&gt;&lt;span title="Product of a and b is -59231"&gt;Hover here to see the solution&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Cheers!!&lt;br /&gt;Bragaadeesh.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-8740279252170126297?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/8740279252170126297/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=8740279252170126297' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/8740279252170126297'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/8740279252170126297'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/08/project-euler-find-quadratic-formula.html' title='Project Euler : Find a quadratic formula that produces the maximum number of primes for consecutive values of n.'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-5801096367319737397</id><published>2010-08-23T03:31:00.000-07:00</published><updated>2010-08-23T03:32:08.943-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ruby'/><category scheme='http://www.blogger.com/atom/ns#' term='project euler'/><category scheme='http://www.blogger.com/atom/ns#' term='euler'/><category scheme='http://www.blogger.com/atom/ns#' term='numbers'/><category scheme='http://www.blogger.com/atom/ns#' term='coding'/><title type='text'>Project Euler : What is the total of all the name scores in the file of first names?</title><content type='html'>&lt;span style="font-size: large;"&gt;&lt;b&gt;Problem 22&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="problem_content"&gt;Using &lt;a href="http://projecteuler.net/project/names.txt"&gt;names.txt&lt;/a&gt;  (right click and 'Save Link/Target As...'), a 46K text file containing  over five-thousand first names, begin by sorting it into alphabetical  order. Then working out the alphabetical value for each name, multiply  this value by its alphabetical position in the list to obtain a name  score.&lt;br /&gt;For example, when the list is sorted into alphabetical order, COLIN,  which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list.  So, COLIN would obtain a score of 938 &lt;img alt="×" border="0" height="9" src="http://projecteuler.net/images/symbol_times.gif" style="vertical-align: middle;" width="9" /&gt; 53 = 49714.&lt;br /&gt;What is the total of all the name scores in the file?&lt;/div&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="font-size: large;"&gt;Solution (in Ruby)&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;All we had to do is read the file convert all the values into an array and sort it. Then for each word we need to apply the rule as stated in the problem. If you could look at the code inside the loop, i would be ripping the starting and trailing quotes '"' and then converting them to index numbers. ie A for 1, B for 2 upto Z for 26. Rest is self explanatory. &lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:ruby"&gt;names = File.new("/names.txt","r").gets.split(/,/).sort&lt;br /&gt;sum = 0&lt;br /&gt;index = 1&lt;br /&gt;names.each do |n|&lt;br /&gt;&amp;nbsp; sum += n.gsub!(/^"(.*?)"$/,'\1').split(//).inject(0){|b,i| b+i[0]-64} * index&lt;br /&gt;&amp;nbsp; index+=1&lt;br /&gt;end&lt;br /&gt;puts "The total of all the name scores is #{sum}"&lt;/pre&gt;&lt;br /&gt;&lt;span title="The total of all the name scores is 871198282"&gt;Hover here to see the solution&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Cheers!!&lt;br /&gt;Bragaadeesh.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-5801096367319737397?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/5801096367319737397/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=5801096367319737397' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/5801096367319737397'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/5801096367319737397'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/08/project-euler-what-is-total-of-all-name.html' title='Project Euler : What is the total of all the name scores in the file of first names?'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-347936445708876656</id><published>2010-08-23T02:05:00.000-07:00</published><updated>2010-09-27T05:27:24.668-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ruby'/><category scheme='http://www.blogger.com/atom/ns#' term='days'/><category scheme='http://www.blogger.com/atom/ns#' term='project euler'/><category scheme='http://www.blogger.com/atom/ns#' term='euler'/><category scheme='http://www.blogger.com/atom/ns#' term='numbers'/><category scheme='http://www.blogger.com/atom/ns#' term='date comparison'/><category scheme='http://www.blogger.com/atom/ns#' term='coding'/><title type='text'>Project Euler : How many Sundays fell on the first of the month during the twentieth century?</title><content type='html'>&lt;span style="font-size: large;"&gt;&lt;b&gt;Problem 19&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="problem_content"&gt;You are given the following information, but you may prefer to do some research for yourself.&lt;br /&gt;&lt;ul&gt;&lt;li&gt;1 Jan 1900 was a Monday.&lt;/li&gt;&lt;li&gt;Thirty days has September,&lt;br /&gt;April, June and November.&lt;br /&gt;All the rest have thirty-one,&lt;br /&gt;Saving February alone,&lt;br /&gt;Which has twenty-eight, rain or shine.&lt;br /&gt;And on leap years, twenty-nine.&lt;/li&gt;&lt;li&gt;A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.&lt;/li&gt;&lt;/ul&gt;How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?&lt;/div&gt;&lt;span style="font-size: large;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size: large;"&gt;&lt;b&gt;Solution (in Ruby)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;This particular solution does not involve any hard calculations, just run through the dates starting from the first sunday step by 7 days and check if the sunday lies on the first day of the month.&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:ruby"&gt;require 'date'&lt;br /&gt;d1, d2, total_sundays = [Date::civil(1901, 1, 1), Date::civil(2000, 12, 31), 0]&lt;br /&gt;d1 +=1 while (d1.wday != 0)&lt;br /&gt;d1.step(d2, 7){|date| total_sundays+=1 if date.day == 1}&lt;br /&gt;puts "Total number of Sundays : #{total_sundays}"&lt;/pre&gt;&lt;br /&gt;&lt;span title="Total number of Sundays : 171"&gt;Hover here to see the solution&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-347936445708876656?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/347936445708876656/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=347936445708876656' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/347936445708876656'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/347936445708876656'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/08/project-euler-how-many-sundays-fell-on.html' title='Project Euler : How many Sundays fell on the first of the month during the twentieth century?'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-644205107287200993</id><published>2010-08-22T13:39:00.000-07:00</published><updated>2010-08-22T13:39:05.004-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ruby'/><category scheme='http://www.blogger.com/atom/ns#' term='project euler'/><category scheme='http://www.blogger.com/atom/ns#' term='euler'/><category scheme='http://www.blogger.com/atom/ns#' term='numbers'/><category scheme='http://www.blogger.com/atom/ns#' term='coding'/><title type='text'>Project Euler : Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits in some order.</title><content type='html'>&lt;span style="font-size: large;"&gt;&lt;b&gt;Problem 52&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="problem_content"&gt;It can be seen that the number, 125874, and its double, 251748, contain exactly the same digits, but in a different order.&lt;br /&gt;Find the smallest positive integer, &lt;i&gt;x&lt;/i&gt;, such that 2&lt;i&gt;x&lt;/i&gt;, 3&lt;i&gt;x&lt;/i&gt;, 4&lt;i&gt;x&lt;/i&gt;, 5&lt;i&gt;x&lt;/i&gt;, and 6&lt;i&gt;x&lt;/i&gt;, contain the same digits.&lt;/div&gt;&lt;span style="font-size: large;"&gt;&lt;b&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size: large;"&gt;&lt;b&gt;Solution(in Ruby) &lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The solution to this problem is simple and straight forward as shown below. Just convert the problem statement into code with tweaks here and there, we should arrive at the solution smoothly. Do not get scared by the catch statement, its just to break nested loops. Moreover, it is enough to inspect numbers that start with 1. The obvious reason being numbers that begin with 2 get more digits when multiplied by 6 which would defeat our purpose.&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:ruby"&gt;INFINITY = 1.0 / 0.0&lt;br /&gt;catch (:done) do&lt;br /&gt;&amp;nbsp; 1.upto(INFINITY) do |i|&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ((10**i)...(2*10**i)).each do |x|&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; y = x.to_s.split(//)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if ( y - (x*2).to_s.split(//) ).size == 0 &amp;amp;&amp;amp; y.size == (x*2).to_s.split(//).size &amp;amp;&amp;amp;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ( y - (x*3).to_s.split(//) ).size == 0 &amp;amp;&amp;amp; y.size == (x*3).to_s.split(//).size &amp;amp;&amp;amp;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ( y - (x*4).to_s.split(//) ).size == 0 &amp;amp;&amp;amp; y.size == (x*4).to_s.split(//).size &amp;amp;&amp;amp;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ( y - (x*5).to_s.split(//) ).size == 0 &amp;amp;&amp;amp; y.size == (x*5).to_s.split(//).size &amp;amp;&amp;amp;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ( y - (x*6).to_s.split(//) ).size == 0 &amp;amp;&amp;amp; y.size == (x*6).to_s.split(//).size&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; puts "The minimum unique number is #{x}"&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; throw :done&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; end&lt;br /&gt;&amp;nbsp; end&lt;br /&gt;end&lt;/pre&gt;&lt;br /&gt;&lt;span title="The minimum unique number is 142857"&gt;Hover here to see the solution&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Cheers!!&lt;br /&gt;Bragaadeesh&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-644205107287200993?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/644205107287200993/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=644205107287200993' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/644205107287200993'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/644205107287200993'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/08/project-euler-find-smallest-positive.html' title='Project Euler : Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits in some order.'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-4101478626404146691</id><published>2010-08-22T13:15:00.000-07:00</published><updated>2010-08-22T13:15:40.678-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ruby'/><category scheme='http://www.blogger.com/atom/ns#' term='project euler'/><category scheme='http://www.blogger.com/atom/ns#' term='euler'/><category scheme='http://www.blogger.com/atom/ns#' term='numbers'/><category scheme='http://www.blogger.com/atom/ns#' term='coding'/><title type='text'>Project Euler : Find the minimal path sum from the left column to the right column.</title><content type='html'>&lt;span style="font-size: large;"&gt;&lt;b&gt;Problem 82&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;The minimal path sum in the 5 by 5 matrix below, by starting in any  cell in the left column and finishing in any cell in the right column,  and only moving up, down, and right, is indicated in red and bold; the  sum is equal to 994.&lt;br /&gt;&lt;div style="text-align: center;"&gt; &lt;table align="center" border="0" cellpadding="0" cellspacing="0"&gt;&lt;tbody&gt;&lt;tr&gt; &lt;td&gt;&lt;img align="middle" alt="" height="120" src="http://projecteuler.net/images/bracket_left.gif" width="8" /&gt;&lt;/td&gt; &lt;td&gt; &lt;table border="0" cellpadding="3" cellspacing="0"&gt;&lt;tbody&gt;&lt;tr&gt; &lt;td&gt;131&lt;/td&gt;&lt;td&gt;673&lt;/td&gt;&lt;td&gt;&lt;span style="color: #dd0000;"&gt;&lt;b&gt;234&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;&lt;td&gt;&lt;span style="color: #dd0000;"&gt;&lt;b&gt;103&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;&lt;td&gt;&lt;span style="color: #dd0000;"&gt;&lt;b&gt;18&lt;/b&gt;&lt;/span&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td&gt;&lt;span style="color: #dd0000;"&gt;&lt;b&gt;201&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;&lt;td&gt;&lt;span style="color: #dd0000;"&gt;&lt;b&gt;96&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;&lt;td&gt;&lt;span style="color: #dd0000;"&gt;&lt;b&gt;342&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;&lt;td&gt;965&lt;/td&gt;&lt;td&gt;150&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td&gt;630&lt;/td&gt;&lt;td&gt;803&lt;/td&gt;&lt;td&gt;746&lt;/td&gt;&lt;td&gt;422&lt;/td&gt;&lt;td&gt;111&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td&gt;537&lt;/td&gt;&lt;td&gt;699&lt;/td&gt;&lt;td&gt;497&lt;/td&gt;&lt;td&gt;121&lt;/td&gt;&lt;td&gt;956&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td&gt;805&lt;/td&gt;&lt;td&gt;732&lt;/td&gt;&lt;td&gt;524&lt;/td&gt;&lt;td&gt;37&lt;/td&gt;&lt;td&gt;331&lt;/td&gt; &lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/td&gt; &lt;td&gt;&lt;img align="middle" alt="" height="120" src="http://projecteuler.net/images/bracket_right.gif" width="8" /&gt;&lt;/td&gt; &lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;Find the minimal path sum, in &lt;a href="http://projecteuler.net/project/matrix.txt"&gt;matrix.txt&lt;/a&gt;  (right click and 'Save Link/Target As...'), a 31K text file containing a  80 by 80 matrix, from the left column to the right column.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size: large;"&gt;&lt;b&gt;Solution(in Ruby)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;This problem has to be attacked in the reverse order ie starting from the right to left. Lets take the last but before column. For each cell in that column, find the minimum possible sum for that cell. To find the minimum possible sum, you will have to find all the 'L' paths explored, their sum has be calculated and stored and the minimum sum has to be stored in that cell. After traversing that entire column, replace that particular column value with the minimal sum. Repeat this process for the immediate left column until you exhaust all the columns reaching the left most column. The minimal value of the leftmost column is the minimal sum which is exactly what we look for.&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:ruby"&gt;require 'matrix'&lt;br /&gt;&lt;br /&gt;input = ''&lt;br /&gt;file = File.new("/matrix.txt","r")&lt;br /&gt;while (line = file.gets) &lt;br /&gt;&amp;nbsp; input += line&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;class Cell&lt;br /&gt;&amp;nbsp; attr_accessor :min_sum, :value&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;x = Matrix.rows(input.lines.map{|each_line| each_line.split(/,/).map{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; |n|&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; c = Cell.new&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; c.min_sum = c.value = n.to_i&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; c&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }})&lt;br /&gt;&lt;br /&gt;MAX = x.row_size&lt;br /&gt;&lt;br /&gt;(MAX-2).downto(0) do |column|&lt;br /&gt;&amp;nbsp; 0.upto(MAX-1) do |row|&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; val_array = []&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; aggregate = x[row,column].value&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; val_array &amp;lt;&amp;lt; aggregate + x[row,column+1].value&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; (row+1).upto(MAX-1) do |down_traverse|&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; aggregate += x[down_traverse,column].value&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; val_array &amp;lt;&amp;lt; aggregate + x[down_traverse,column+1].value&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; end&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; aggregate = x[row,column].value&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; (row-1).downto(0) do |up_traverse|&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; aggregate += x[up_traverse,column].value&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; val_array &amp;lt;&amp;lt; aggregate + x[up_traverse,column+1].value&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; end&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; x[row,column].min_sum = val_array.min&lt;br /&gt;&amp;nbsp; end&lt;br /&gt;&amp;nbsp; 0.upto(MAX-1) do |each_row|&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; x[each_row,column].value = x[each_row,column].min_sum&lt;br /&gt;&amp;nbsp; end&lt;br /&gt;end&lt;br /&gt;val_array = []&lt;br /&gt;0.upto(MAX-1) do |val|&lt;br /&gt;&amp;nbsp; val_array &amp;lt;&amp;lt; x[val,0].value&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;puts "The minimum sum is #{val_array.min}"&lt;/pre&gt;&lt;br /&gt;&lt;span title="The minimum sum is 260324"&gt;Hover here to see the solution&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Cheers!!&lt;br /&gt;Bragaadeesh.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-4101478626404146691?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/4101478626404146691/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=4101478626404146691' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/4101478626404146691'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/4101478626404146691'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/08/project-euler-find-minimal-path-sum.html' title='Project Euler : Find the minimal path sum from the left column to the right column.'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-2291734896801914797</id><published>2010-08-21T13:13:00.000-07:00</published><updated>2010-08-22T13:03:34.317-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ruby'/><category scheme='http://www.blogger.com/atom/ns#' term='project euler'/><category scheme='http://www.blogger.com/atom/ns#' term='euler'/><category scheme='http://www.blogger.com/atom/ns#' term='numbers'/><category scheme='http://www.blogger.com/atom/ns#' term='coding'/><title type='text'>Project Euler : Find the longest sequence using a starting number under one million.</title><content type='html'>&lt;span style="font-size: large;"&gt;&lt;b&gt;Problem 14&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="problem_content"&gt;The following iterative sequence is defined for the set of positive integers:&lt;br /&gt;&lt;div style="margin-left: 50px;"&gt;&lt;var&gt;n&lt;/var&gt; --&amp;gt; &lt;var&gt;n&lt;/var&gt;/2 (&lt;var&gt;n&lt;/var&gt; is even)&lt;br /&gt;&lt;var&gt;n&lt;/var&gt; --&amp;gt; 3&lt;var&gt;n&lt;/var&gt; + 1 (&lt;var&gt;n&lt;/var&gt; is odd)&lt;/div&gt;Using the rule above and starting with 13, we generate the following sequence:&lt;br /&gt;&lt;div style="text-align: center;"&gt;13 --&amp;gt; 40 --&amp;gt; 20 --&amp;gt; 10 --&amp;gt; 5 --&amp;gt; 16 --&amp;gt; 8 --&amp;gt; 4 --&amp;gt; 2 --&amp;gt; 1&lt;/div&gt;It can be seen that this sequence (starting at 13 and finishing at 1)  contains 10 terms. Although it has not been proved yet (Collatz  Problem), it is thought that all starting numbers finish at 1.&lt;br /&gt;Which starting number, under one million, produces the longest chain?&lt;br /&gt;&lt;div class="info"&gt;&lt;b&gt;NOTE:&lt;/b&gt; Once the chain starts the terms are allowed to go above one million.&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;&lt;span style="font-size: large;"&gt;&lt;b&gt;Solution (in Ruby)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;In order to solve this problem, we first need to find a pattern by attacking in the reverse order. If you could see if the number gotten is a power of two, it will diminish all the way upto 1. So its wiser to store their frequency (power of 2 numbers) first in the array. The next interesting property is that if you find the chain count for a number you do not want to find that again. Combining these two properties, we can very well get a linear solution for the input count of 1 million solving the problem within seconds.&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:ruby"&gt;MAX_NUM = 1000000&lt;br /&gt;n = Array.new(MAX_NUM)&lt;br /&gt;n[0] = 0&lt;br /&gt;n[1] = 1&lt;br /&gt;counter = 1&lt;br /&gt;index = 1&lt;br /&gt;while (counter &amp;lt; MAX_NUM)&lt;br /&gt;&amp;nbsp; n[counter] = index&lt;br /&gt;&amp;nbsp; index+=1&lt;br /&gt;&amp;nbsp; counter*=2&lt;br /&gt;end&lt;br /&gt;(MAX_NUM-1).downto(2) do |num|&lt;br /&gt;&amp;nbsp; if n[num]&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; next&lt;br /&gt;&amp;nbsp; end&lt;br /&gt;&amp;nbsp; rolling = num&lt;br /&gt;&amp;nbsp; sequence = 0&lt;br /&gt;&amp;nbsp; while !n[rolling]&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if rolling % 2 == 0&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rolling = rolling/2&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; else&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rolling = 3*rolling + 1&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; end&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; sequence+=1&lt;br /&gt;&amp;nbsp; end&lt;br /&gt;&amp;nbsp; n[num] = sequence + n[rolling]&lt;br /&gt;end&lt;br /&gt;puts "The maximum chained number is #{n.index(n.max)}"&lt;/pre&gt;&lt;br /&gt;&lt;span title="The maximum chained number is 837799"&gt;Hover here to see the solution&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Cheers!!&lt;br /&gt;Bragaadeesh&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-2291734896801914797?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/2291734896801914797/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=2291734896801914797' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/2291734896801914797'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/2291734896801914797'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/08/project-euler-find-longest-sequence.html' title='Project Euler : Find the longest sequence using a starting number under one million.'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-4911344544306052385</id><published>2010-08-21T05:35:00.000-07:00</published><updated>2010-08-22T13:02:45.764-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ruby'/><category scheme='http://www.blogger.com/atom/ns#' term='project euler'/><category scheme='http://www.blogger.com/atom/ns#' term='euler'/><category scheme='http://www.blogger.com/atom/ns#' term='numbers'/><category scheme='http://www.blogger.com/atom/ns#' term='coding'/><title type='text'>Project Euler : Calculate the sum of all the primes below two million.</title><content type='html'>&lt;span style="font-size: large;"&gt;&lt;b&gt;Problem 10&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.&lt;br /&gt;Find the sum of all the primes below two million.&lt;br /&gt;&lt;span style="font-size: large;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size: large;"&gt;&lt;b&gt;Solution(in Ruby) &lt;/b&gt;&lt;/span&gt;&lt;br /&gt;The solution to this problem involves the tricky implementation of prime number functionality. Rest is just a matter of programming.&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:ruby"&gt;def is_prime(n)&lt;br /&gt;&amp;nbsp; return false if n &amp;lt;= 1&lt;br /&gt;&amp;nbsp; 2.upto(Math.sqrt(n).to_i) do |x|&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return false if n%x == 0&lt;br /&gt;&amp;nbsp; end&lt;br /&gt;&amp;nbsp; true&lt;br /&gt;end&lt;br /&gt;sum = 0&lt;br /&gt;2.upto(2000000) do |num|&lt;br /&gt;&amp;nbsp; x+=num if is_prime(num)&lt;br /&gt;end&lt;br /&gt;puts "Sum is #{sum}"&lt;/pre&gt;&lt;br /&gt;&lt;span title="Sum is 142913828922"&gt;Hover here to see the solution&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Cheers!&lt;br /&gt;Bragaadeesh&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-4911344544306052385?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/4911344544306052385/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=4911344544306052385' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/4911344544306052385'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/4911344544306052385'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/08/project-euler-calculate-sum-of-all.html' title='Project Euler : Calculate the sum of all the primes below two million.'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-6980309159899724676</id><published>2010-08-21T05:21:00.000-07:00</published><updated>2010-08-22T13:02:26.485-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ruby'/><category scheme='http://www.blogger.com/atom/ns#' term='project euler'/><category scheme='http://www.blogger.com/atom/ns#' term='euler'/><category scheme='http://www.blogger.com/atom/ns#' term='numbers'/><category scheme='http://www.blogger.com/atom/ns#' term='coding'/><title type='text'>Project Euler : Find the greatest product of five consecutive digits in the 1000-digit number.</title><content type='html'>&lt;span style="font-size: large;"&gt;&lt;b&gt;Problem 8&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Find the greatest product of five consecutive digits in the 1000-digit number.&lt;br /&gt;73167176531330624919225119674426574742355349194934&lt;br /&gt;96983520312774506326239578318016984801869478851843&lt;br /&gt;85861560789112949495459501737958331952853208805511&lt;br /&gt;12540698747158523863050715693290963295227443043557&lt;br /&gt;66896648950445244523161731856403098711121722383113&lt;br /&gt;62229893423380308135336276614282806444486645238749&lt;br /&gt;30358907296290491560440772390713810515859307960866&lt;br /&gt;70172427121883998797908792274921901699720888093776&lt;br /&gt;65727333001053367881220235421809751254540594752243&lt;br /&gt;52584907711670556013604839586446706324415722155397&lt;br /&gt;53697817977846174064955149290862569321978468622482&lt;br /&gt;83972241375657056057490261407972968652414535100474&lt;br /&gt;82166370484403199890008895243450658541227588666881&lt;br /&gt;16427171479924442928230863465674813919123162824586&lt;br /&gt;17866458359124566529476545682848912883142607690042&lt;br /&gt;24219022671055626321111109370544217506941658960408&lt;br /&gt;07198403850962455444362981230987879927244284909188&lt;br /&gt;84580156166097919133875499200524063689912560717606&lt;br /&gt;05886116467109405077541002256983155200055935729725&lt;br /&gt;71636269561882670428252483600823257530420752963450&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size: large;"&gt;&lt;b&gt;Solution (in Ruby) &lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The solution to this problem can be solved by just naked eyes. Anyway, I have presented the code in Ruby.&lt;br /&gt;&lt;pre class="brush:ruby"&gt;num = '73167176531330624919225119674426574742355349194934&lt;br /&gt;96983520312774506326239578318016984801869478851843&lt;br /&gt;85861560789112949495459501737958331952853208805511&lt;br /&gt;12540698747158523863050715693290963295227443043557&lt;br /&gt;66896648950445244523161731856403098711121722383113&lt;br /&gt;62229893423380308135336276614282806444486645238749&lt;br /&gt;30358907296290491560440772390713810515859307960866&lt;br /&gt;70172427121883998797908792274921901699720888093776&lt;br /&gt;65727333001053367881220235421809751254540594752243&lt;br /&gt;52584907711670556013604839586446706324415722155397&lt;br /&gt;53697817977846174064955149290862569321978468622482&lt;br /&gt;83972241375657056057490261407972968652414535100474&lt;br /&gt;82166370484403199890008895243450658541227588666881&lt;br /&gt;16427171479924442928230863465674813919123162824586&lt;br /&gt;17866458359124566529476545682848912883142607690042&lt;br /&gt;24219022671055626321111109370544217506941658960408&lt;br /&gt;07198403850962455444362981230987879927244284909188&lt;br /&gt;84580156166097919133875499200524063689912560717606&lt;br /&gt;05886116467109405077541002256983155200055935729725&lt;br /&gt;71636269561882670428252483600823257530420752963450'&lt;br /&gt;f = ''&lt;br /&gt;num.lines{|x| f += x.chop}&lt;br /&gt;y = f.split(//)&lt;br /&gt;arr = []&lt;br /&gt;0.upto(995) do |index|&lt;br /&gt;&amp;nbsp; arr &amp;lt;&amp;lt; y[index].to_i * y[index+1].to_i * y[index+2].to_i * y[index+3].to_i * y[index+4].to_i&lt;br /&gt;end&lt;br /&gt;puts arr.max&lt;/pre&gt;&lt;br /&gt;&lt;span title="The maximum product is 40824"&gt;Hover here to see the solution&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Cheers!!&lt;br /&gt;Bragaadeesh.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-6980309159899724676?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/6980309159899724676/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=6980309159899724676' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/6980309159899724676'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/6980309159899724676'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/08/project-euler-find-greatest-product-of.html' title='Project Euler : Find the greatest product of five consecutive digits in the 1000-digit number.'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-4631634598482217434</id><published>2010-08-21T04:42:00.000-07:00</published><updated>2010-08-22T13:02:06.057-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ruby'/><category scheme='http://www.blogger.com/atom/ns#' term='project euler'/><category scheme='http://www.blogger.com/atom/ns#' term='euler'/><category scheme='http://www.blogger.com/atom/ns#' term='numbers'/><category scheme='http://www.blogger.com/atom/ns#' term='coding'/><title type='text'>Project Euler : Find the largest palindrome made from the product of two 3-digit numbers.</title><content type='html'>&lt;span style="font-size: large;"&gt;&lt;b&gt;Problem 4&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="problem_content"&gt;A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 x 99.&lt;br /&gt;Find the largest palindrome made from the product of two 3-digit numbers.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="font-size: large;"&gt;Solution (in Ruby)&lt;/span&gt;&lt;/b&gt; &lt;br /&gt;&lt;br /&gt;The solution to this problem is pretty straight forward. All we had to do is run from 101 to 999 and multiply all the combination and push them into an array and find the maximum value.&lt;/div&gt;&lt;br /&gt;&lt;pre class="brush:java"&gt;arr = []&lt;br /&gt;101.upto(999) do |i|&lt;br /&gt;&amp;nbsp; 101.upto(999) do |j|&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; prod = i*j&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; arr &amp;lt;&amp;lt; prod if prod.to_s == prod.to_s.reverse&lt;br /&gt;&amp;nbsp; end&lt;br /&gt;end&lt;br /&gt;puts "The largest palindromic number is #{arr.max}"&lt;/pre&gt;&lt;br /&gt;&lt;span title="The largest palindromic number is 906609"&gt;Hover here to see the solution&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Cheers!!&lt;br /&gt;Bragaadeesh.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-4631634598482217434?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/4631634598482217434/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=4631634598482217434' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/4631634598482217434'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/4631634598482217434'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/08/project-euler-find-largest-palindrome.html' title='Project Euler : Find the largest palindrome made from the product of two 3-digit numbers.'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-3722050664989944969</id><published>2010-08-21T03:44:00.000-07:00</published><updated>2010-08-22T13:01:10.022-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ruby'/><category scheme='http://www.blogger.com/atom/ns#' term='project euler'/><category scheme='http://www.blogger.com/atom/ns#' term='euler'/><category scheme='http://www.blogger.com/atom/ns#' term='numbers'/><category scheme='http://www.blogger.com/atom/ns#' term='coding'/><title type='text'>Project Euler : Finding maximum prime factor</title><content type='html'>&lt;span style="font-size: large;"&gt;&lt;b&gt;Problem 3&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The prime factors of 13195 are 5, 7, 13 and 29.&lt;br /&gt;What is the largest prime factor of the number 600851475143 ?&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size: large;"&gt;&lt;b&gt;Solution (in Ruby)&lt;/b&gt;&lt;/span&gt; &lt;br /&gt;&lt;br /&gt;The solution to this problem is simple. But the main problem here is to construct a rock solid method that returns whether a given number is prime or not. A prime number is a number which divisible by itself and 1 only. Example of prime numbers are 2,3,11,4999. The following is the ruby code to find whether or not a number is prime or not. You may see that I have used a square root of the number as the upper bound, the simple reason being, there cannot be a number greater than the square root of that number being prime (you can get it if you think deep)&lt;br /&gt;&lt;pre class="brush:ruby"&gt;def is_prime(n)&lt;br /&gt;&amp;nbsp; return false if n &amp;lt;= 1&lt;br /&gt;&amp;nbsp; 2.upto(Math.sqrt(n).to_i) do |x|&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return false if n%x == 0&lt;br /&gt;&amp;nbsp; end&lt;br /&gt;&amp;nbsp; true&lt;br /&gt;end&lt;/pre&gt;&lt;br /&gt;Now that we have written a method that returns whether or not a number is prime, the rest of the problem is fairly simple. Again, we have to use the square root property. It is enough if we run the prime number validation from the square root of the number. We will have to traverse all the way to 2 for this. The first occurring prime factor is ofcourse the solution to our problem.&lt;br /&gt;&lt;pre class="brush:ruby"&gt;Math.sqrt(600851475143).to_i.downto(2) do |n|&lt;br /&gt;&amp;nbsp; if is_prime(n) &amp;amp;&amp;amp; 600851475143%n ==0&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; puts "Maximum prime factor for this number is #{n}"&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; break&lt;br /&gt;&amp;nbsp; end&lt;br /&gt;end&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span title="Maximum prime factor for this number is 6857"&gt;Hover here to see the solution.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Cheers!!&lt;br /&gt;Bragaadeesh.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-3722050664989944969?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/3722050664989944969/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=3722050664989944969' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/3722050664989944969'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/3722050664989944969'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/08/project-euler-finding-maximum-prime.html' title='Project Euler : Finding maximum prime factor'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-1089991355018307425</id><published>2010-08-21T02:36:00.000-07:00</published><updated>2010-08-22T13:03:07.690-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ruby'/><category scheme='http://www.blogger.com/atom/ns#' term='project euler'/><category scheme='http://www.blogger.com/atom/ns#' term='euler'/><category scheme='http://www.blogger.com/atom/ns#' term='numbers'/><category scheme='http://www.blogger.com/atom/ns#' term='coding'/><title type='text'>Project Euler : Finding maximum product in a matrix</title><content type='html'>&lt;span style="font-size: large;"&gt;&lt;b&gt;Problem 11&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="problem_content"&gt;In the 20 x 20 grid below, four numbers along a diagonal line have been marked in red.&lt;br /&gt;&lt;br /&gt;&lt;div style="font-family: courier new; font-size: 10pt; text-align: center;"&gt;08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08&lt;br /&gt;49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00&lt;br /&gt;81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65&lt;br /&gt;52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91&lt;br /&gt;22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80&lt;br /&gt;24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50&lt;br /&gt;32 98 81 28 64 23 67 10 &lt;span style="color: red;"&gt;&lt;b&gt;26&lt;/b&gt;&lt;/span&gt; 38 40 67 59 54 70 66 18 38 64 70&lt;br /&gt;67 26 20 68 02 62 12 20 95 &lt;span style="color: red;"&gt;&lt;b&gt;63&lt;/b&gt;&lt;/span&gt; 94 39 63 08 40 91 66 49 94 21&lt;br /&gt;24 55 58 05 66 73 99 26 97 17 &lt;span style="color: red;"&gt;&lt;b&gt;78&lt;/b&gt;&lt;/span&gt; 78 96 83 14 88 34 89 63 72&lt;br /&gt;21 36 23 09 75 00 76 44 20 45 35 &lt;span style="color: red;"&gt;&lt;b&gt;14&lt;/b&gt;&lt;/span&gt; 00 61 33 97 34 31 33 95&lt;br /&gt;78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92&lt;br /&gt;16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57&lt;br /&gt;86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58&lt;br /&gt;19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40&lt;br /&gt;04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66&lt;br /&gt;88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69&lt;br /&gt;04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36&lt;br /&gt;20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16&lt;br /&gt;20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54&lt;br /&gt;01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48&lt;/div&gt;&lt;div style="font-family: courier new; font-size: 10pt; text-align: center;"&gt;&lt;br /&gt;&lt;/div&gt;The product of these numbers is 26 x 63 x 78 x 14 = 1788696.&lt;br /&gt;What is the greatest product of four adjacent numbers in any direction (up, down, left, right, or diagonally) in the 20 x 20 grid?&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size: large;"&gt;&lt;b&gt;Solution (In Ruby)&lt;/b&gt;&lt;/span&gt; &lt;br /&gt;&lt;br /&gt;The solution to this particular problem is simple and by simple naked eye viewing we can get what we want. But then, if the numbers are almost in the same range and if the matrix size is more then it will become real difficult to identify the maximum number. The following ruby code will find the maximum number. All we are doing here is finding the product of the numbers in the fashion we are supposed to. There is no trick shortcut here. We 'will' have to traverse the entire matrix for finding the product.&lt;/div&gt;&lt;br /&gt;&lt;pre class="brush:ruby"&gt;require 'matrix'&lt;br /&gt;input =&lt;br /&gt;&amp;nbsp;'08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08&lt;br /&gt;&amp;nbsp; 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00&lt;br /&gt;&amp;nbsp; 81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65&lt;br /&gt;&amp;nbsp; 52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91&lt;br /&gt;&amp;nbsp; 22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80&lt;br /&gt;&amp;nbsp; 24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50&lt;br /&gt;&amp;nbsp; 32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70&lt;br /&gt;&amp;nbsp; 67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21&lt;br /&gt;&amp;nbsp; 24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72&lt;br /&gt;&amp;nbsp; 21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95&lt;br /&gt;&amp;nbsp; 78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92&lt;br /&gt;&amp;nbsp; 16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57&lt;br /&gt;&amp;nbsp; 86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58&lt;br /&gt;&amp;nbsp; 19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40&lt;br /&gt;&amp;nbsp; 04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66&lt;br /&gt;&amp;nbsp; 88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69&lt;br /&gt;&amp;nbsp; 04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36&lt;br /&gt;&amp;nbsp; 20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16&lt;br /&gt;&amp;nbsp; 20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54&lt;br /&gt;&amp;nbsp; 01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48'&lt;br /&gt;&lt;br /&gt;x =&amp;nbsp; Matrix.rows(input.lines.map{|line| line.split.map{|n| n.to_i}})&lt;br /&gt;val = []&lt;br /&gt;0.upto(19) do |r|&lt;br /&gt;&amp;nbsp; 0.upto(19) do |c|&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; val &amp;lt;&amp;lt; x[r,c] * x[r,c+1] * x[r,c+2] * x[r,c+3] if c&amp;lt;=16&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; val &amp;lt;&amp;lt; x[r,c] * x[r+1,c] * x[r+2,c] * x[r+3,c] if r&amp;lt;=16&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; val &amp;lt;&amp;lt; x[r,c] * x[r+1,c+1] * x[r+2, c+2] * x[r+3, c+3] if c&amp;lt;=16 &amp;amp;&amp;amp; r&amp;lt;=16&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; val &amp;lt;&amp;lt; x[r,c] * x[r+1,c-1] * x[r+2, c-2] * x[r+3, c-3] if c&amp;gt;=3 &amp;amp;&amp;amp; r&amp;lt;=16&lt;br /&gt;&amp;nbsp; end&lt;br /&gt;end&lt;br /&gt;puts puts "Maximum product is #{val.max}"&lt;/pre&gt;&lt;br /&gt;&lt;span title="Maximum product is 70600674"&gt;Hover here to see the solution&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Cheers!!&lt;br /&gt;Bragaadeesh&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-1089991355018307425?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/1089991355018307425/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=1089991355018307425' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/1089991355018307425'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/1089991355018307425'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/08/problem-in-20-20-grid-below-four.html' title='Project Euler : Finding maximum product in a matrix'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-1434951079117361197</id><published>2010-08-20T14:44:00.000-07:00</published><updated>2010-08-22T13:05:27.389-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ruby'/><category scheme='http://www.blogger.com/atom/ns#' term='project euler'/><category scheme='http://www.blogger.com/atom/ns#' term='euler'/><category scheme='http://www.blogger.com/atom/ns#' term='numbers'/><category scheme='http://www.blogger.com/atom/ns#' term='coding'/><title type='text'>Project Euler : Find the last ten digits of the series, 1^(1) + 2^(2) + 3^(3) + ... + 1000^(1000).</title><content type='html'>&lt;span style="font-size: large;"&gt;&lt;b&gt;Problem 48&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="problem_content"&gt;The series, 1&lt;img alt="^(" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=48" style="display: none;" /&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;img alt=")" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=48" style="display: none;" /&gt; + 2&lt;img alt="^(" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=48" style="display: none;" /&gt;&lt;sup&gt;2&lt;/sup&gt;&lt;img alt=")" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=48" style="display: none;" /&gt; + 3&lt;img alt="^(" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=48" style="display: none;" /&gt;&lt;sup&gt;3&lt;/sup&gt;&lt;img alt=")" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=48" style="display: none;" /&gt; + ... + 10&lt;img alt="^(" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=48" style="display: none;" /&gt;&lt;sup&gt;10&lt;/sup&gt;&lt;img alt=")" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=48" style="display: none;" /&gt; = 10405071317.&lt;br /&gt;Find the last ten digits of the series, 1&lt;img alt="^(" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=48" style="display: none;" /&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;img alt=")" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=48" style="display: none;" /&gt; + 2&lt;img alt="^(" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=48" style="display: none;" /&gt;&lt;sup&gt;2&lt;/sup&gt;&lt;img alt=")" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=48" style="display: none;" /&gt; + 3&lt;img alt="^(" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=48" style="display: none;" /&gt;&lt;sup&gt;3&lt;/sup&gt;&lt;img alt=")" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=48" style="display: none;" /&gt; + ... + 1000&lt;img alt="^(" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=48" style="display: none;" /&gt;&lt;sup&gt;1000&lt;/sup&gt;&lt;img alt=")" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=48" style="display: none;" /&gt;.&lt;/div&gt;&lt;br /&gt;&lt;span style="font-size: large;"&gt;&lt;b&gt;Solution (In Ruby)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The solution to this problem becomes simple if we just directly implement with the brute force technique. It just costs two lines in Ruby and the source code is given below.&lt;br /&gt;&lt;pre class="brush:ruby"&gt;sum = (1..1000).to_a.inject(0) {|b,i| b+= i**i}.to_s&lt;br /&gt;puts sum.to_s[(sum.size-10)..(sum.size)]&lt;/pre&gt;&lt;span title="9110846700"&gt;Hover here to see the solution.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Cheers!!&lt;br /&gt;Bragaadeesh&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-1434951079117361197?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/1434951079117361197/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=1434951079117361197' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/1434951079117361197'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/1434951079117361197'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/08/project-euler-find-last-ten-digits-of.html' title='Project Euler : Find the last ten digits of the series, 1^(1) + 2^(2) + 3^(3) + ... + 1000^(1000).'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-5152214822446328035</id><published>2010-08-20T13:58:00.000-07:00</published><updated>2010-08-22T13:04:08.077-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ruby'/><category scheme='http://www.blogger.com/atom/ns#' term='project euler'/><category scheme='http://www.blogger.com/atom/ns#' term='euler'/><category scheme='http://www.blogger.com/atom/ns#' term='numbers'/><category scheme='http://www.blogger.com/atom/ns#' term='coding'/><title type='text'>Project Euler : Find the sum of the digits in the number 100!</title><content type='html'>&lt;span style="font-size: large;"&gt;&lt;b&gt;Problem 20&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;n! means n x (n -1) x ... x 3 x 2 x 1&lt;br /&gt;Find the sum of the digits in the number 100!&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size: large;"&gt;&lt;b&gt;Solution (In Ruby)&lt;/b&gt;&lt;/span&gt; &lt;br /&gt;&lt;br /&gt;All we have to do for the above problem is to write a factorial method. Get the value of 100! and split it into characters and simply add them. Source code is given below.&lt;br /&gt;&lt;pre class="brush:ruby"&gt;def fact(n)&lt;br /&gt;  return 1 if n == 0&lt;br /&gt;  n * fact(n-1)&lt;br /&gt;end&lt;br /&gt;puts "Result is #{fact(100).to_s.split(//).inject(0){|b,i| b+i.to_i}}"&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span title="Result is 648"&gt;Hover here to see the result&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Cheers!!&lt;br /&gt;Bragaadeesh&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-5152214822446328035?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/5152214822446328035/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=5152214822446328035' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/5152214822446328035'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/5152214822446328035'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/08/project-euler-find-sum-of-digits-in.html' title='Project Euler : Find the sum of the digits in the number 100!'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-8965913618587068920</id><published>2010-08-20T13:29:00.000-07:00</published><updated>2010-08-22T13:04:41.532-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ruby'/><category scheme='http://www.blogger.com/atom/ns#' term='project euler'/><category scheme='http://www.blogger.com/atom/ns#' term='euler'/><category scheme='http://www.blogger.com/atom/ns#' term='numbers'/><category scheme='http://www.blogger.com/atom/ns#' term='coding'/><title type='text'>Project Euler : What is the sum of both diagonals in a 1001 by 1001 spiral?</title><content type='html'>&lt;b&gt;&lt;span style="font-size: large;"&gt;Problem 28&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;div class="problem_content"&gt;Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is formed as follows:&lt;br /&gt;&lt;div style="font-family: courier new; text-align: center;"&gt;&lt;span style="color: red; font-family: courier new;"&gt;&lt;b&gt;21&lt;/b&gt;&lt;/span&gt; 22 23 24 &lt;span style="color: red; font-family: courier new;"&gt;&lt;b&gt;25&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;20 &amp;nbsp;&lt;span style="color: red; font-family: courier new;"&gt;&lt;b&gt;7&lt;/b&gt;&lt;/span&gt; &amp;nbsp;8 &amp;nbsp;&lt;span style="color: red; font-family: courier new;"&gt;&lt;b&gt;9&lt;/b&gt;&lt;/span&gt; 10&lt;br /&gt;19 &amp;nbsp;6 &amp;nbsp;&lt;span style="color: red; font-family: courier new;"&gt;&lt;b&gt;1&lt;/b&gt;&lt;/span&gt; &amp;nbsp;2 11&lt;br /&gt;18 &amp;nbsp;&lt;span style="color: red; font-family: courier new;"&gt;&lt;b&gt;5&lt;/b&gt;&lt;/span&gt; &amp;nbsp;4 &amp;nbsp;&lt;span style="color: red; font-family: courier new;"&gt;&lt;b&gt;3&lt;/b&gt;&lt;/span&gt; 12&lt;br /&gt;&lt;span style="color: red; font-family: courier new;"&gt;&lt;b&gt;17&lt;/b&gt;&lt;/span&gt; 16 15 14 &lt;span style="color: red; font-family: courier new;"&gt;&lt;b&gt;13&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;It can be verified that the sum of the numbers on the diagonals is 101.&lt;br /&gt;What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed in the same way?&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size: large;"&gt;&lt;b&gt;The solution (in ruby)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size: large;"&gt;&lt;b&gt;&amp;nbsp;&lt;/b&gt;&lt;/span&gt; &lt;br /&gt;The problem initially looks complex and makes us want to use matrices. For a small input such as the one given, it will be easier to solve. But when the size of the matrix increases, it is almost impossible to do a proper traversal and find the edge values. This problem can be attacked easily if we are able to decipher the pattern. If you could notice the given matrix carefully, you may find that at each square corners, we have a square number on the right top corner, leave the first value (1), we can sum it later.&lt;br /&gt;&lt;br /&gt;For the first level, the corner value is 9 = 3&lt;img alt="^(" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=30" style="display: none;" /&gt;&lt;sup&gt;2&lt;/sup&gt;&lt;img alt=")" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=30" style="display: none;" /&gt; &lt;br /&gt;For the second level, corner value is 25 = 5&lt;img alt="^(" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=30" style="display: none;" /&gt;&lt;sup&gt;2&amp;nbsp;&lt;/sup&gt;&lt;br /&gt;&lt;br /&gt;This keeps going. Our job is to find a forumla at each covering square. The forumla that I arrived at each level in terms of n is [4 * (n&lt;img alt="^(" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=30" style="display: none;" /&gt;&lt;sup&gt;2&amp;nbsp;&lt;/sup&gt; - (n-1)*3/2)]. If you apply the above formula for level 3 we will get 24, for level 5, its 76. If you add these two you will get 100. Now add that with 1, you will get the desired answer as 101. Finding that formula is left to the reader as an excercise (it can be found using finite differences)&lt;br /&gt;Now the source code in ruby,&lt;/div&gt;&lt;br /&gt;&lt;pre class="brush:ruby"&gt;result = 1&lt;br /&gt;(3..1001).step(2) do |n|&lt;br /&gt;  val += (n*n - 3 * (n-1)/2)*4&lt;br /&gt;end&lt;br /&gt;puts "Result is #{result}"&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span title="Result is 669171001"&gt;HOVER HERE TO SEE THE ANSWER&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Cheers!!&lt;br /&gt;Bragaadeesh.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-8965913618587068920?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/8965913618587068920/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=8965913618587068920' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/8965913618587068920'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/8965913618587068920'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/08/project-euler-what-is-sum-of-both.html' title='Project Euler : What is the sum of both diagonals in a 1001 by 1001 spiral?'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-290720784213689908</id><published>2010-08-20T12:34:00.000-07:00</published><updated>2010-08-22T13:05:08.082-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ruby'/><category scheme='http://www.blogger.com/atom/ns#' term='project euler'/><category scheme='http://www.blogger.com/atom/ns#' term='euler'/><category scheme='http://www.blogger.com/atom/ns#' term='numbers'/><category scheme='http://www.blogger.com/atom/ns#' term='coding'/><title type='text'>Project Euler : Find the sum of all the numbers that can be written as the sum of 5th powers of their digits.</title><content type='html'>&lt;span style="font-size: large;"&gt;&lt;b&gt;Problem 30&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="problem_content"&gt;Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:&lt;br /&gt;&lt;blockquote&gt;1634 = 1&lt;img alt="^(" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=30" style="display: none;" /&gt;&lt;sup&gt;4&lt;/sup&gt;&lt;img alt=")" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=30" style="display: none;" /&gt; + 6&lt;img alt="^(" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=30" style="display: none;" /&gt;&lt;sup&gt;4&lt;/sup&gt;&lt;img alt=")" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=30" style="display: none;" /&gt; + 3&lt;img alt="^(" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=30" style="display: none;" /&gt;&lt;sup&gt;4&lt;/sup&gt;&lt;img alt=")" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=30" style="display: none;" /&gt; + 4&lt;img alt="^(" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=30" style="display: none;" /&gt;&lt;sup&gt;4&lt;/sup&gt;&lt;img alt=")" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=30" style="display: none;" /&gt;&lt;br /&gt;8208 = 8&lt;img alt="^(" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=30" style="display: none;" /&gt;&lt;sup&gt;4&lt;/sup&gt;&lt;img alt=")" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=30" style="display: none;" /&gt; + 2&lt;img alt="^(" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=30" style="display: none;" /&gt;&lt;sup&gt;4&lt;/sup&gt;&lt;img alt=")" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=30" style="display: none;" /&gt; + 0&lt;img alt="^(" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=30" style="display: none;" /&gt;&lt;sup&gt;4&lt;/sup&gt;&lt;img alt=")" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=30" style="display: none;" /&gt; + 8&lt;img alt="^(" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=30" style="display: none;" /&gt;&lt;sup&gt;4&lt;/sup&gt;&lt;img alt=")" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=30" style="display: none;" /&gt;&lt;br /&gt;9474 = 9&lt;img alt="^(" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=30" style="display: none;" /&gt;&lt;sup&gt;4&lt;/sup&gt;&lt;img alt=")" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=30" style="display: none;" /&gt; + 4&lt;img alt="^(" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=30" style="display: none;" /&gt;&lt;sup&gt;4&lt;/sup&gt;&lt;img alt=")" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=30" style="display: none;" /&gt; + 7&lt;img alt="^(" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=30" style="display: none;" /&gt;&lt;sup&gt;4&lt;/sup&gt;&lt;img alt=")" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=30" style="display: none;" /&gt; + 4&lt;img alt="^(" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=30" style="display: none;" /&gt;&lt;sup&gt;4&lt;/sup&gt;&lt;img alt=")" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=30" style="display: none;" /&gt;&lt;/blockquote&gt;&lt;div class="info"&gt;As 1 = 1&lt;img alt="^(" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=30" style="display: none;" /&gt;&lt;sup&gt;4&lt;/sup&gt;&lt;img alt=")" src="http://projecteuler.net/index.php?section=problems&amp;amp;id=30" style="display: none;" /&gt; is not a sum it is not included.&lt;/div&gt;The sum of these numbers is 1634 + 8208 + 9474 = 19316.&lt;br /&gt;Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.&lt;/div&gt;&lt;br /&gt;&lt;span style="font-size: large;"&gt;&lt;b&gt;The Solution (in Ruby)&lt;/b&gt;&lt;/span&gt; &lt;br /&gt;&lt;br /&gt;This problem is a bit tricky and can be solved with a trial and error method. Best way to attack this problem is to first arrive at a solution for the 4th power as given in the problem, convince ourselves that the solution that we have coded is correct. Then run the same for 5th powers. Of course the upper bound is a mystery, but who cares you have trial and error to know the answer.&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: ruby"&gt;final_result = 0&lt;br /&gt;2.upto(200000) do |x|&lt;br /&gt;  sum = x.to_s.split(//).inject(0){|b,i| b+i.to_i**5}&lt;br /&gt;  final_result += sum if sum == x&lt;br /&gt;end&lt;br /&gt;puts "The magic sum is #{final_result}"&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span title="The magic sum is 443839"&gt;HOVER HERE TO SEE THE SOLUTION!!&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Cheers!!&lt;br /&gt;Bragaadeesh&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-290720784213689908?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/290720784213689908/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=290720784213689908' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/290720784213689908'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/290720784213689908'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/08/project-euler-find-sum-of-all-numbers.html' title='Project Euler : Find the sum of all the numbers that can be written as the sum of 5th powers of their digits.'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-7805158627200713894</id><published>2010-08-08T13:13:00.000-07:00</published><updated>2010-08-08T23:06:11.547-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='problem'/><category scheme='http://www.blogger.com/atom/ns#' term='codejam'/><category scheme='http://www.blogger.com/atom/ns#' term='google'/><category scheme='http://www.blogger.com/atom/ns#' term='coding'/><title type='text'>Google CodeJam 2010 : "Rope Intranet" Problem with solution</title><content type='html'>&lt;b&gt;Problem&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="border-collapse: separate; color: black; font-family: Times; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,sans-serif; font-size: small;"&gt;A company is located in two very tall buildings. The company intranet connecting the buildings consists of many wires, each connecting a window on the first building to a window on the second building.&lt;br /&gt;You are looking at those buildings from the side, so that one of the buildings is to the left and one is to the right. The windows on the left building are seen as points on its right wall, and the windows on the right building are seen as points on its left wall. Wires are straight segments connecting a window on the left building to a window on the right building.&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="border-collapse: separate; color: black; font-family: Times; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,sans-serif; font-size: small;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="border-collapse: separate; color: black; font-family: Times; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,sans-serif; font-size: small;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="border-collapse: separate; color: black; font-family: Times; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,sans-serif; font-size: small;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/_kRev2kjeMro/TF8M23kSg5I/AAAAAAAACPg/dT9hXrgv_Bw/s1600/download.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/_kRev2kjeMro/TF8M23kSg5I/AAAAAAAACPg/dT9hXrgv_Bw/s320/download.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="border-collapse: separate; color: black; font-family: Times; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,sans-serif; font-size: small;"&gt;&lt;br /&gt;You've noticed that no two wires share an endpoint (in other words, there's at most one wire going out of each window). However, from your viewpoint, some of the wires intersect midway. You've also noticed that exactly two wires meet at each intersection point.&lt;br /&gt;On the above picture, the intersection points are the black circles, while the windows are the white circles.&lt;br /&gt;How many intersection points do you see?&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;h3 style="font-size: 14px; font-weight: normal;"&gt;&lt;span class="Apple-style-span" style="border-collapse: separate; color: black; font-family: Times; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,sans-serif; font-size: small;"&gt;Input&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;&lt;span class="Apple-style-span" style="border-collapse: separate; color: black; font-family: Times; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,sans-serif; font-size: small;"&gt;The first line of the input gives the number of test cases,&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;b&gt;T&lt;/b&gt;.&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;b&gt;T&lt;/b&gt;&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;test cases follow. Each case begins with a line containing an integer&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;b&gt;N&lt;/b&gt;, denoting the number of wires you see.&lt;br /&gt;The next&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;b&gt;N&lt;/b&gt;&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;lines each describe one wire with two integers&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;b&gt;A&lt;sub&gt;i&lt;/sub&gt;&lt;/b&gt;&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;and&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;b&gt;B&lt;sub&gt;i&lt;/sub&gt;&lt;/b&gt;. These describe the windows that this wire connects:&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;b&gt;A&lt;sub&gt;i&lt;/sub&gt;&lt;/b&gt;&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;is the height of the window on the left building, and&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;b&gt;B&lt;sub&gt;i&lt;/sub&gt;&lt;/b&gt;is the height of the window on the right building.&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;h3 style="font-size: 14px; font-weight: normal;"&gt;&lt;span class="Apple-style-span" style="border-collapse: separate; color: black; font-family: Times; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,sans-serif; font-size: small;"&gt;Output&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;&lt;span class="Apple-style-span" style="border-collapse: separate; color: black; font-family: Times; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,sans-serif; font-size: small;"&gt;For each test case, output one line containing "Case #x: y", where x is the case number (starting from 1) and y is the number of intersection points you see.&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;h3 style="font-size: 14px; font-weight: normal;"&gt;&lt;span class="Apple-style-span" style="border-collapse: separate; color: black; font-family: Times; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,sans-serif; font-size: small;"&gt;Limits&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;&lt;span class="Apple-style-span" style="border-collapse: separate; color: black; font-family: Times; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,sans-serif; font-size: small;"&gt;1 ≤&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;b&gt;T&lt;/b&gt;&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;≤ 15.&lt;br /&gt;1 ≤&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;b&gt;A&lt;sub&gt;i&lt;/sub&gt;&lt;/b&gt;&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;≤ 10&lt;sup&gt;4&lt;/sup&gt;.&lt;br /&gt;1 ≤&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;b&gt;B&lt;sub&gt;i&lt;/sub&gt;&lt;/b&gt;&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;≤ 10&lt;sup&gt;4&lt;/sup&gt;.&lt;br /&gt;Within each test case, all&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;b&gt;A&lt;sub&gt;i&lt;/sub&gt;&lt;/b&gt;&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;are different.&lt;br /&gt;Within each test case, all&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;b&gt;B&lt;sub&gt;i&lt;/sub&gt;&lt;/b&gt;&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;are different.&lt;br /&gt;No three wires intersect at the same point.&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;h4 style="font-size: 14px; font-weight: normal;"&gt;&lt;span class="Apple-style-span" style="border-collapse: separate; color: black; font-family: Times; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,sans-serif; font-size: small;"&gt;Small dataset&lt;/span&gt;&lt;/span&gt;&lt;/h4&gt;&lt;span class="Apple-style-span" style="border-collapse: separate; color: black; font-family: Times; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,sans-serif; font-size: small;"&gt;1 ≤&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;b&gt;N&lt;/b&gt;&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;≤ 2.&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;h4 style="font-size: 14px; font-weight: normal;"&gt;&lt;span class="Apple-style-span" style="border-collapse: separate; color: black; font-family: Times; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,sans-serif; font-size: small;"&gt;Large dataset&lt;/span&gt;&lt;/span&gt;&lt;/h4&gt;&lt;span class="Apple-style-span" style="border-collapse: separate; color: black; font-family: Times; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,sans-serif; font-size: small;"&gt;1 ≤&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;b&gt;N&lt;/b&gt;&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;≤ 1000.&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;h3 style="font-size: 14px; font-weight: normal;"&gt;&lt;span class="Apple-style-span" style="border-collapse: separate; color: black; font-family: Times; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,sans-serif; font-size: small;"&gt;Sample&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;&lt;div class="problem-io-wrapper" style="background-color: #efefef; border: 1px solid rgb(204, 204, 204); margin-bottom: 2em; overflow-x: auto; overflow-y: hidden; padding-bottom: 2em; width: 468px;"&gt;&lt;table style="border-collapse: collapse; border-width: 0pt; font-size: small; margin: 0px; padding: 0px; text-align: left; vertical-align: top;"&gt;&lt;tbody&gt;&lt;tr style="border-collapse: collapse; border-width: 0pt; margin: 0px; padding: 0px; text-align: left; vertical-align: top;"&gt;&lt;td style="border-collapse: collapse; border-width: 0pt; margin: 0px; padding: 0px 1em; text-align: left; vertical-align: top;"&gt;&lt;br /&gt;&lt;span class="io-table-header" style="font-size: 13px;"&gt;Input&lt;/span&gt;&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;/td&gt;&lt;td style="border-collapse: collapse; border-width: 0pt; margin: 0px; padding: 0px 1em; text-align: left; vertical-align: top;"&gt;&lt;br /&gt;&lt;span class="io-table-header" style="font-size: 13px;"&gt;Output&lt;/span&gt;&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr style="border-collapse: collapse; border-width: 0pt; margin: 0px; padding: 0px; text-align: left; vertical-align: top;"&gt;&lt;td style="border-collapse: collapse; border-width: 0pt; margin: 0px; padding: 0px 1em; text-align: left; vertical-align: top;"&gt;&lt;code style="color: black; font-family: 'bogus font here',monospace; font-size: 13px;"&gt;2&lt;br /&gt;3&lt;br /&gt;1 10&lt;br /&gt;5 5&lt;br /&gt;7 7&lt;br /&gt;2&lt;br /&gt;1 1&lt;br /&gt;2 2&lt;/code&gt;&lt;/td&gt;&lt;td style="border-collapse: collapse; border-width: 0pt; margin: 0px; padding: 0px 1em; text-align: left; vertical-align: top;"&gt;&lt;code style="color: black; font-family: 'bogus font here',monospace; font-size: 13px;"&gt;Case #1: 2&lt;br /&gt;Case #2: 0&lt;br /&gt;&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="border-collapse: separate; color: black; font-family: Times; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,sans-serif; font-size: small;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style="font-family: &amp;quot;Helvetica Neue&amp;quot;,Arial,Helvetica,sans-serif;"&gt;&lt;span style="font-size: large;"&gt;&lt;b&gt;Solution&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;This is one of the simpler problems that I was able to solve in the Codejam round. Following is the source code.&lt;br /&gt;&lt;pre class="brush:java"&gt;private long solve(int[] a, int[] b) {&lt;br /&gt;  &lt;br /&gt;  PointMesh p = new PointMesh();&lt;br /&gt;  &lt;br /&gt;  for(int i=0;i&amp;lt;a.length;i++){&lt;br /&gt;   p.add(a[i], b[i]);&lt;br /&gt;  }&lt;br /&gt;  &lt;br /&gt;  return p.intersects;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; class PointMesh{&lt;br /&gt;  long intersects;&lt;br /&gt;  List&amp;lt;Line2D&amp;gt; lineList = new ArrayList&amp;lt;Line2D&amp;gt;();&lt;br /&gt;  Line2D line1 = new Line2D.Double();&lt;br /&gt;  Line2D r = new Line2D.Double();  &lt;br /&gt;  &lt;br /&gt;  void add(int a,int b){&lt;br /&gt;   Point2D p1 = new Point2D.Double(0, a);&lt;br /&gt;   Point2D p2 = new Point2D.Double(10, b);&lt;br /&gt;   Line2D newLine = new Line2D.Double(p1,p2);&lt;br /&gt;   &lt;br /&gt;   for(Line2D eachLine:lineList){&lt;br /&gt;    if(eachLine.intersectsLine(newLine))&lt;br /&gt;     intersects++;&lt;br /&gt;   lineList.add(newLine);&lt;br /&gt;  }&lt;br /&gt; }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Complete source code : &lt;a href="http://docs.google.com/uc?id=0B_z-9Q7kZRM7NjE4NDcyNTAtNmUxYS00ODNmLTlmZmEtYzY3NDVlMjc4MDgy&amp;amp;export=download&amp;amp;hl=en"&gt;here&lt;/a&gt;&lt;br /&gt;Practice inputs : &lt;a href="http://docs.google.com/uc?id=0B_z-9Q7kZRM7YWQ3YWJhZGItN2UxYS00Yzg3LThiOTktNzA4MWU1NWE2YTU5&amp;amp;export=download&amp;amp;hl=en"&gt;here&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Cheers!!&lt;br /&gt;Bragaadeesh.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-7805158627200713894?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/7805158627200713894/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=7805158627200713894' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/7805158627200713894'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/7805158627200713894'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/08/google-codejam-2010-rope-intranet.html' title='Google CodeJam 2010 : &quot;Rope Intranet&quot; Problem with solution'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/_kRev2kjeMro/TF8M23kSg5I/AAAAAAAACPg/dT9hXrgv_Bw/s72-c/download.png' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-1400792846247335110</id><published>2010-07-24T15:20:00.000-07:00</published><updated>2010-07-24T15:26:45.641-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='problem'/><category scheme='http://www.blogger.com/atom/ns#' term='codejam'/><category scheme='http://www.blogger.com/atom/ns#' term='google'/><category scheme='http://www.blogger.com/atom/ns#' term='TRIE'/><title type='text'>Google CodeJam 2010 : "File Fix-it" Problem with solution</title><content type='html'>&lt;span class="Apple-style-span" style="border-collapse: separate; color: black; font-family: Times; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,sans-serif; font-size: small;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;h3 style="font-weight: normal;"&gt;&lt;span style="font-size: large;"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="border-collapse: separate; color: black; font-family: Times; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,sans-serif;"&gt;Problem&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/h3&gt;&lt;span class="Apple-style-span" style="border-collapse: separate; color: black; font-family: Times; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,sans-serif; font-size: small;"&gt;On Unix computers, data is stored in&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;i&gt;directories&lt;/i&gt;. There is one&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;i&gt;root directory&lt;/i&gt;, and this might have several directories contained inside of it, each with different names. These directories might have even more directories contained inside of them, and so on.&lt;br /&gt;A directory is uniquely identified by its name and its parent directory (the directory it is directly contained in). This is usually encoded in a&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;i&gt;path&lt;/i&gt;, which consists of several parts each preceded by a forward slash ('/'). The final part is the name of the directory, and everything else gives the path of its parent directory. For example, consider the path:&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre style="background-color: whitesmoke; border: 1px solid silver; color: black; font-family: 'bogus font here',monospace; font-size: 12px; margin: 2em; overflow: auto; padding: 0.5em; width: 447px;"&gt;&lt;span class="Apple-style-span" style="border-collapse: separate; color: black; font-family: Times; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,sans-serif; font-size: small;"&gt;/home/gcj/finals&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;span class="Apple-style-span" style="border-collapse: separate; color: black; font-family: Times; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,sans-serif; font-size: small;"&gt;This refers to the directory with name "finals" in the directory described by "/home/gcj", which in turn refers to the directory with name "gcj" in the directory described by the path "/home". In this path, there is only one part, which means it refers to the directory with the name "home" in the root directory.&lt;br /&gt;To create a directory, you can use the&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;i&gt;mkdir&lt;/i&gt;&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;command. You specify a path, and then&lt;i&gt;mkdir&lt;/i&gt;&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;will create the directory described by that path, but&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;i&gt;only if&lt;/i&gt;&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;the parent directory already exists. For example, if you wanted to create the "/home/gcj/finals" and "/home/gcj/quals" directories from scratch, you would need four commands:&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre style="background-color: whitesmoke; border: 1px solid silver; color: black; font-family: 'bogus font here',monospace; font-size: 12px; margin: 2em; overflow: auto; padding: 0.5em; width: 447px;"&gt;&lt;span class="Apple-style-span" style="border-collapse: separate; color: black; font-family: Times; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,sans-serif; font-size: small;"&gt;mkdir /home&lt;br /&gt;mkdir /home/gcj&lt;br /&gt;mkdir /home/gcj/finals&lt;br /&gt;mkdir /home/gcj/quals&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;span class="Apple-style-span" style="border-collapse: separate; color: black; font-family: Times; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,sans-serif; font-size: small;"&gt;&lt;br /&gt;Given the full set of directories already existing on your computer, and a set of new directories you want to create if they do not already exist, how many&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;i&gt;mkdir&lt;/i&gt;&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;commands do you need to use?&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;h3 style="font-size: 14px; font-weight: normal;"&gt;&lt;span class="Apple-style-span" style="border-collapse: separate; color: black; font-family: Times; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,sans-serif; font-size: small;"&gt;Input&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;&lt;span class="Apple-style-span" style="border-collapse: separate; color: black; font-family: Times; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,sans-serif; font-size: small;"&gt;The first line of the input gives the number of test cases,&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;b&gt;T&lt;/b&gt;.&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;b&gt;T&lt;/b&gt;&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;test cases follow. Each case begins with a line containing two integers&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;b&gt;N&lt;/b&gt;&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;and&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;b&gt;M&lt;/b&gt;, separated by a space.&lt;br /&gt;The next&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;b&gt;N&lt;/b&gt;&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;lines each give the path of one directory that already exists on your computer. This list will include every directory already on your computer other than the root directory. (The root directory is on every computer, so there is no need to list it explicitly.)&lt;br /&gt;The next&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;b&gt;M&lt;/b&gt;&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;lines each give the path of one directory that you want to create.&lt;br /&gt;Each of the paths in the input is formatted as in the problem statement above. Specifically, a path consists of one or more lower-case alpha-numeric strings (i.e., strings containing only the symbols 'a'-'z' and '0'-'9'), each preceded by a single forward slash. These alpha-numeric strings are never empty.&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;h3 style="font-size: 14px; font-weight: normal;"&gt;&lt;span class="Apple-style-span" style="border-collapse: separate; color: black; font-family: Times; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,sans-serif; font-size: small;"&gt;Output&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;&lt;span class="Apple-style-span" style="border-collapse: separate; color: black; font-family: Times; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,sans-serif; font-size: small;"&gt;For each test case, output one line containing "Case #x: y", where x is the case number (starting from 1) and y is the number of&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;i&gt;mkdir&lt;/i&gt;&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;you need.&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;h3 style="font-size: 14px; font-weight: normal;"&gt;&lt;span class="Apple-style-span" style="border-collapse: separate; color: black; font-family: Times; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,sans-serif; font-size: small;"&gt;Limits&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;&lt;span class="Apple-style-span" style="border-collapse: separate; color: black; font-family: Times; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,sans-serif; font-size: small;"&gt;1 ≤&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;b&gt;T&lt;/b&gt;&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;≤ 100.&lt;br /&gt;No path will have more than 100 characters in it.&lt;br /&gt;No path will appear twice in the list of directories already on your computer, or in the list of directories you wish to create. A path may appear once in both lists however. (See example case #2 below).&lt;br /&gt;If a directory is listed as being on your computer, then its parent directory will also be listed, unless the parent is the root directory.&lt;br /&gt;The input file will be no longer than 100,000 bytes in total.&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;h4 style="font-size: 14px; font-weight: normal;"&gt;&lt;span class="Apple-style-span" style="border-collapse: separate; color: black; font-family: Times; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,sans-serif; font-size: small;"&gt;Small dataset&lt;/span&gt;&lt;/span&gt;&lt;/h4&gt;&lt;span class="Apple-style-span" style="border-collapse: separate; color: black; font-family: Times; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,sans-serif; font-size: small;"&gt;0 ≤&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;b&gt;N&lt;/b&gt;&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;≤ 10.&lt;br /&gt;1 ≤&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;b&gt;M&lt;/b&gt;&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;≤ 10.&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;h4 style="font-size: 14px; font-weight: normal;"&gt;&lt;span class="Apple-style-span" style="border-collapse: separate; color: black; font-family: Times; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,sans-serif; font-size: small;"&gt;Large dataset&lt;/span&gt;&lt;/span&gt;&lt;/h4&gt;&lt;span class="Apple-style-span" style="border-collapse: separate; color: black; font-family: Times; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,sans-serif; font-size: small;"&gt;0 ≤&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;b&gt;N&lt;/b&gt;&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;≤ 100.&lt;br /&gt;1 ≤&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;b&gt;M&lt;/b&gt;&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;≤ 100.&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;h3 style="font-size: 14px; font-weight: normal;"&gt;&lt;span class="Apple-style-span" style="border-collapse: separate; color: black; font-family: Times; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,sans-serif; font-size: small;"&gt;Sample&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;&lt;div class="problem-io-wrapper" style="background-color: #efefef; border: 1px solid rgb(204, 204, 204); margin-bottom: 2em; overflow-x: auto; overflow-y: hidden; padding-bottom: 2em; width: 468px;"&gt;&lt;table style="border-collapse: collapse; border-width: 0pt; font-size: small; margin: 0px; padding: 0px; text-align: left; vertical-align: top;"&gt;&lt;tbody&gt;&lt;tr style="border-collapse: collapse; border-width: 0pt; margin: 0px; padding: 0px; text-align: left; vertical-align: top;"&gt;&lt;td style="border-collapse: collapse; border-width: 0pt; margin: 0px; padding: 0px 1em; text-align: left; vertical-align: top;"&gt;&lt;br /&gt;&lt;span class="io-table-header" style="font-size: 13px;"&gt;Input&lt;/span&gt;&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;/td&gt;&lt;td style="border-collapse: collapse; border-width: 0pt; margin: 0px; padding: 0px 1em; text-align: left; vertical-align: top;"&gt;&lt;br /&gt;&lt;span class="io-table-header" style="font-size: 13px;"&gt;Output&lt;/span&gt;&lt;span class="Apple-converted-space"&gt;&amp;nbsp;&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr style="border-collapse: collapse; border-width: 0pt; margin: 0px; padding: 0px; text-align: left; vertical-align: top;"&gt;&lt;td style="border-collapse: collapse; border-width: 0pt; margin: 0px; padding: 0px 1em; text-align: left; vertical-align: top;"&gt;&lt;code style="color: black; font-family: 'bogus font here',monospace; font-size: 13px;"&gt;3&lt;br /&gt;0 2&lt;br /&gt;/home/gcj/finals&lt;br /&gt;/home/gcj/quals&lt;br /&gt;2 1&lt;br /&gt;/chicken&lt;br /&gt;/chicken/egg&lt;br /&gt;/chicken&lt;br /&gt;1 3&lt;br /&gt;/a&lt;br /&gt;/a/b&lt;br /&gt;/a/c&lt;br /&gt;/b/b&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="border-collapse: separate; color: black; font-family: Times; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,sans-serif; font-size: small;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size: large;"&gt;&lt;b&gt;Solution&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;To attack this problem we can use the famous TRIE data structure. We have &lt;a href="http://www.technicalypto.com/2010/04/trie-in-java.html"&gt;already seen&lt;/a&gt; enough about this data structure in steps and this becomes so handy in this particular problem.&lt;br /&gt;A TRIE data structure simply stores the data that becomes easy for retrieval. But lets not bother about the retrieval part in this particular problem. We will simply increase our counter whenever we add a new folder to the existing directory structure. The place where we optimize this problem involves in its running time.&amp;nbsp; Whenever we see a new directory structure, we simply pass through it in the length times instead which is linear in time jargons. The solution is given below.&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:java"&gt;private int solve(String[] already, String[] fresh) {&lt;br /&gt;  Trie trieDSA = new Trie();&lt;br /&gt;  &lt;br /&gt;  for(int i=0;i&amp;lt;already.length;i++){&lt;br /&gt;   trieDSA.insert(already[i],false);&lt;br /&gt;  }&lt;br /&gt;  &lt;br /&gt;  for(int i=0;i&amp;lt;fresh.length;i++){&lt;br /&gt;   trieDSA.insert(fresh[i], true);&lt;br /&gt;  }&lt;br /&gt;  &lt;br /&gt;  return trieDSA.counter;&lt;br /&gt; }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;So clean, isnt it? The TRIE and the Node classes are given below. Remember, please go through my &lt;a href="http://www.technicalypto.com/2010/04/trie-in-java.html"&gt;TRIE data structure introduction&lt;/a&gt; for few minutes, its definitely a knowledgeable one! Trust me :)&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: java"&gt;class Node {&lt;br /&gt; String currentPath;&lt;br /&gt; boolean marker; &lt;br /&gt; Collection&amp;lt;Node&amp;gt; child;&lt;br /&gt; &lt;br /&gt; public Node(String path){&lt;br /&gt;  child = new HashSet&amp;lt;Node&amp;gt;();&lt;br /&gt;  marker = false;&lt;br /&gt;  currentPath = path;&lt;br /&gt; }&lt;br /&gt; &lt;br /&gt; public Node subNode(String path){&lt;br /&gt;  if(child!=null){&lt;br /&gt;   for(Node eachChild:child){&lt;br /&gt;    if(eachChild.currentPath.equals(path)){&lt;br /&gt;     return eachChild;&lt;br /&gt;    }&lt;br /&gt;   }&lt;br /&gt;  }&lt;br /&gt;  return null;&lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;class Trie{&lt;br /&gt; private Node root;&lt;br /&gt; &lt;br /&gt; public int counter = 0;&lt;br /&gt;&lt;br /&gt; public Trie(){&lt;br /&gt;  root = new Node("");&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; public void insert(String pathArray, boolean shouldTrack){&lt;br /&gt;  Node current = root; &lt;br /&gt;  &lt;br /&gt;  String[] paths = pathArray.substring(1).split("\\/");&lt;br /&gt;  &lt;br /&gt;  if(paths.length==0){&lt;br /&gt;   //DO NOTHING&lt;br /&gt;   current.marker=true;&lt;br /&gt;  }&lt;br /&gt;   &lt;br /&gt;   &lt;br /&gt;  for(int i=0;i&amp;lt;paths.length;i++){&lt;br /&gt;   Node child = current.subNode(paths[i]);&lt;br /&gt;   if(child!=null){ &lt;br /&gt;    current = child;&lt;br /&gt;   }&lt;br /&gt;   else{&lt;br /&gt;    current.child.add(new Node(paths[i]));&lt;br /&gt;    current = current.subNode(paths[i]);&lt;br /&gt;    if(shouldTrack){&lt;br /&gt;     counter++;&lt;br /&gt;    }&lt;br /&gt;   }&lt;br /&gt;   // Set marker to indicate end of the word&lt;br /&gt;   if(i==paths.length-1)&lt;br /&gt;    current.marker = true;&lt;br /&gt;  } &lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Complete source code : &lt;a href="http://docs.google.com/uc?id=0B_z-9Q7kZRM7OGVjZGQwMzUtYTI3My00NWJjLTk5NWQtMWVlNzM5YTI0NDVi&amp;amp;export=download&amp;amp;hl=en"&gt;here&lt;/a&gt;&lt;br /&gt;Sample Input : &lt;a href="http://docs.google.com/uc?id=0B_z-9Q7kZRM7YWI4OGFiYWMtOTJiOC00MjM4LTk3MGYtOWMwMjAzYmM1YTFl&amp;amp;export=download&amp;amp;hl=en"&gt;here&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Cheers!!&lt;br /&gt;Bragaadeesh.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-1400792846247335110?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/1400792846247335110/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=1400792846247335110' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/1400792846247335110'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/1400792846247335110'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/07/google-codejam-2010-file-fix-it-problem.html' title='Google CodeJam 2010 : &quot;File Fix-it&quot; Problem with solution'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-8352052893353851963</id><published>2010-06-06T03:14:00.000-07:00</published><updated>2010-06-06T06:27:06.212-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ruby'/><category scheme='http://www.blogger.com/atom/ns#' term='helloworld'/><category scheme='http://www.blogger.com/atom/ns#' term='ruby-on-rails'/><title type='text'>Hello World in Ruby</title><content type='html'>After getting a decent knowledge of &lt;a href="http://www.technicalypto.com/2010/05/lets-ruby.html"&gt;what Ruby is&lt;/a&gt; and the &lt;a href="http://www.technicalypto.com/2010/06/useful-books-on-ruby-and-ruby-on-rails.html"&gt;useful resources&lt;/a&gt; that are available, lets get into action by creating our first hello world program.&lt;br /&gt;&lt;br /&gt;Ruby and Ruby on Rails development becomes amazingly easy using Netbeans IDE where you can perform almost all the tasks in one single IDE. &lt;a href="http://netbeans.org/downloads/index.html"&gt;Download&lt;/a&gt; and install the Netbeans-Ruby version. You should be having JDK installed in your box, thats pretty much the pre-requisite for Netbeans to install.&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/_kRev2kjeMro/TAt0fc9y_MI/AAAAAAAACOQ/a31EJCi6UU4/s1600/Netbeans+and+Ruby.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://1.bp.blogspot.com/_kRev2kjeMro/TAt0fc9y_MI/AAAAAAAACOQ/a31EJCi6UU4/s320/Netbeans+and+Ruby.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;Once your installation is done, launch Netbeans go to File --&amp;gt; New Project. A new project wizard should come up. Select Ruby Application, click Next and click Finish. Thats it!!!! You dont even have to type a Hello World. The new project template does it for you! So sweet isnt it? Now, click on the big green Run button and the console should print out Hello World without a hitch.&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/_kRev2kjeMro/TAt0svWzleI/AAAAAAAACOg/Pp8FJJXGzBs/s1600/hello+world+ruby.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/_kRev2kjeMro/TAt0svWzleI/AAAAAAAACOg/Pp8FJJXGzBs/s320/hello+world+ruby.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;Way to go!! We shall try out some simple programs in the coming posts.&lt;br /&gt;&lt;br /&gt;Cheers!&lt;br /&gt;Bragaadeesh.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-8352052893353851963?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/8352052893353851963/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=8352052893353851963' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/8352052893353851963'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/8352052893353851963'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/06/hello-world-in-ruby.html' title='Hello World in Ruby'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_kRev2kjeMro/TAt0fc9y_MI/AAAAAAAACOQ/a31EJCi6UU4/s72-c/Netbeans+and+Ruby.png' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-450886151554323096</id><published>2010-06-04T11:44:00.000-07:00</published><updated>2010-09-26T13:36:50.557-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ruby'/><category scheme='http://www.blogger.com/atom/ns#' term='books'/><category scheme='http://www.blogger.com/atom/ns#' term='ruby-on-rails'/><title type='text'>Useful books on Ruby and Ruby on Rails</title><content type='html'>Before we jump into the real discussion on Ruby and Ruby on Rails, I felt it will be nicer if I could provide some of the very good books available out there. Programming Ruby 1.9 is the starter pack that everyone who is willing to work on Ruby has to go through.&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;b&gt;&lt;span style="font-family: &amp;quot;Helvetica Neue&amp;quot;,Arial,Helvetica,sans-serif; font-size: x-large;"&gt;Agile Web Development using Rails 3rd Edition&amp;nbsp;&lt;/span&gt;&lt;/b&gt;&lt;/li&gt;&lt;li&gt;&lt;b&gt;&lt;span style="font-family: &amp;quot;Helvetica Neue&amp;quot;,Arial,Helvetica,sans-serif; font-size: x-large;"&gt;Programming Ruby 1.9 &lt;/span&gt;&lt;/b&gt;&lt;/li&gt;&lt;li&gt;&lt;b&gt;&lt;span style="font-family: &amp;quot;Helvetica Neue&amp;quot;,Arial,Helvetica,sans-serif; font-size: x-large;"&gt;Head First Rails&lt;/span&gt;&lt;/b&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;Cheers!&lt;br /&gt;Bragaadeesh.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-450886151554323096?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/450886151554323096/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=450886151554323096' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/450886151554323096'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/450886151554323096'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/06/useful-books-on-ruby-and-ruby-on-rails.html' title='Useful books on Ruby and Ruby on Rails'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-3995279550850472464</id><published>2010-05-29T05:36:00.000-07:00</published><updated>2010-06-02T03:23:52.848-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ruby'/><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='learn'/><title type='text'>Lets Ruby!</title><content type='html'>I have been working mostly&amp;nbsp;in&amp;nbsp;Java for the past 4 years and my new work demands me to learn Ruby. So I've started looking into it for the past week and thought I would share my learning process so that it would be useful for someone out there. And thats the objective of this series of posts for someone having a Java background and wanting to learn Ruby.&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/_kRev2kjeMro/TAF3_msIZfI/AAAAAAAACNo/l0wRMwxD3JI/s1600/Java+Ruby+small.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/_kRev2kjeMro/TAF3_msIZfI/AAAAAAAACNo/l0wRMwxD3JI/s640/Java+Ruby+small.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;Lets get into business. I am really excited to see the way Ruby language syntax is and I am eager to learn this mouth-watering scripting language of coolest nature. During my initial analysis, I learnt that Ruby is,&lt;br /&gt;&lt;ol&gt;&lt;li&gt;Implemented in many other high level languages such as C, Java, .Net etc.,&lt;/li&gt;&lt;li&gt;Is relatively slower.&lt;/li&gt;&lt;li&gt;Ruby is a high level language made of a high level language.&lt;/li&gt;&lt;li&gt;Not suitable for large applications.&lt;/li&gt;&lt;li&gt;Completely open source and is in a budding state.&lt;/li&gt;&lt;li&gt;Has a framework called Rails which is good for Agile development&lt;/li&gt;&lt;li&gt;Community out there is getting better day by day and finding help immediately should not be a problem as time goes by.&lt;/li&gt;&lt;li&gt;Has significant changes between releases which many developers wont welcome right away.&lt;/li&gt;&lt;li&gt;Running time cannot be easily estimated since the language has several underlying implementation in several languages. But there are various profilers available to test them.&lt;/li&gt;&lt;li&gt;Books are always outdated by the time when you finish them.&lt;/li&gt;&lt;/ol&gt;&lt;div&gt;Now that the premise being established on what Ruby is, we shall learn it and have a comparison/understanding on how it relates to Java.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Cheers,&lt;/div&gt;&lt;div&gt;Bragaadeesh.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-3995279550850472464?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/3995279550850472464/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=3995279550850472464' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/3995279550850472464'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/3995279550850472464'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/05/lets-ruby.html' title='Lets Ruby!'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_kRev2kjeMro/TAF3_msIZfI/AAAAAAAACNo/l0wRMwxD3JI/s72-c/Java+Ruby+small.png' height='72' width='72'/><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-6774127791701175732</id><published>2010-05-28T13:13:00.000-07:00</published><updated>2010-06-04T11:52:49.784-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='tweak'/><category scheme='http://www.blogger.com/atom/ns#' term='google'/><category scheme='http://www.blogger.com/atom/ns#' term='off-topic'/><title type='text'>How to exploit Google docs</title><content type='html'>Now that google has removed the restriction on its documents, it is time for us to start exploiting it.&lt;br /&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;No need to upload your pictures in some free image webhosting websites where you wont be having the 100% surity of whether it might come up all time or you'l see a "bandwidth exceeded" message. Upload it to your own google account and with some tweak, we can link it directly in the webpage. You can do the same with Picasa, but you will have to create an album every time and it becomes kind of annoying to maintain it.&lt;/div&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;Same goes with the flash presentations as well. No need to host it to any unreliable free websites. Upload it to your own google document. Plus if you want any referring files you can very well upload it to Google Docs. Yes, there is a limit on the size per account but still 7+ GB will become handy for small and medium bloggers.&lt;/div&gt;&lt;br /&gt;Ok, the steps are very very simple.&lt;br /&gt;&lt;div style="color: #4c1130; font-family: &amp;quot;Trebuchet MS&amp;quot;,sans-serif;"&gt;1. Login to http://docs.google.com. Sign in with your google id and password.&lt;/div&gt;&lt;div style="color: #4c1130; font-family: &amp;quot;Trebuchet MS&amp;quot;,sans-serif;"&gt;2. Click on upload from the left top and select "any" file type you want only restriction is it cannot exceed 100 MB. Do not forget to uncheck "Convert documents, presentations, and spreadsheets to the corresponding Google Docs formats" if you feel you dont want Google mess up your documents.&lt;/div&gt;&lt;div style="color: #4c1130; font-family: &amp;quot;Trebuchet MS&amp;quot;,sans-serif;"&gt;3. After you upload the file, select the file and click on Share and select "Get the link to share". You should be getting a link in a text bar as shown below&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/_kRev2kjeMro/TAAiwTWRa5I/AAAAAAAACNI/HoA0zx_TeaI/s1600/GoogleDoc.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://1.bp.blogspot.com/_kRev2kjeMro/TAAiwTWRa5I/AAAAAAAACNI/HoA0zx_TeaI/s640/GoogleDoc.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div style="font-family: &amp;quot;Trebuchet MS&amp;quot;,sans-serif;"&gt;&lt;span style="color: #4c1130;"&gt;4. To append the file in your page all you got to do is add this to the url &lt;/span&gt;&lt;b style="color: red;"&gt;&amp;amp;export=open&amp;amp;type=.swf&lt;/b&gt;&lt;/div&gt;&lt;br /&gt;This way you can directly embed your flash or jpg or png directly in your pages (the type=.xxx will vary depending on the file you've uploaded). If you want to give a direct download link append this command at the end&lt;b style="color: red;"&gt; &amp;amp;export=download&amp;amp;confirm=no_antivirus&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;The last one &amp;amp;confirm=no_antivirus can be given to files of .exe and .zip extensions.&lt;br /&gt;&lt;br /&gt;Hope this helped. A sample flash file embedded from google doc can be found &lt;a href="http://www.technicalypto.com/2010/04/college-flash-presentation.html"&gt;here&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;Cheers!!&lt;br /&gt;Bragaadeesh.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-6774127791701175732?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/6774127791701175732/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=6774127791701175732' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/6774127791701175732'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/6774127791701175732'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/05/how-to-exploit-google-docs.html' title='How to exploit Google docs'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_kRev2kjeMro/TAAiwTWRa5I/AAAAAAAACNI/HoA0zx_TeaI/s72-c/GoogleDoc.png' height='72' width='72'/><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-3526554670620211219</id><published>2010-05-11T14:31:00.000-07:00</published><updated>2010-05-11T14:31:00.749-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='interview'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><category scheme='http://www.blogger.com/atom/ns#' term='coding'/><title type='text'>Print Matrix in a circular path (Interview question)</title><content type='html'>Hi friends,&lt;br /&gt;&lt;br /&gt;This is one of the interview questions I faced recently and thought I would share it.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Question:&lt;/b&gt;&lt;br /&gt;Given a matrix print all its value in a circular fashion like shown in the image below.&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/_kRev2kjeMro/S-nMf8nbseI/AAAAAAAACMo/4NLtplZBJ5g/s1600/Matrix.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/_kRev2kjeMro/S-nMf8nbseI/AAAAAAAACMo/4NLtplZBJ5g/s640/Matrix.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Solution:&lt;/b&gt;&lt;br /&gt;To attack this problem we definitely want to have a left-right, top-bottom, right-left and bottom-top spans. For attaining this, we obviously want to have 4 loops. The C++ code is given below.&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:cpp"&gt;//============================================================================&lt;br /&gt;// Name        : circular_matrix_read.cpp&lt;br /&gt;// Author      : c++&lt;br /&gt;// Description : output given matrix elements circularly&lt;br /&gt;//============================================================================&lt;br /&gt;&lt;br /&gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;using namespace std;&lt;br /&gt;&lt;br /&gt;#define HEIGHT 6&lt;br /&gt;#define WIDTH  3&lt;br /&gt;&lt;br /&gt;int main()&lt;br /&gt;{&lt;br /&gt; //int array[HEIGHT][WIDTH] = {{1,2,3,4,5},{16,17,18,19,6},{15,24,25,20,7},{14,23,22,21,8},{13,12,11,10,9}};&lt;br /&gt; //int array[HEIGHT][WIDTH] = {{1,2,3,4,5,6},{14,15,16,17,18,7},{13,12,11,10,9,8}};&lt;br /&gt; int array[HEIGHT][WIDTH] = {{1,2,3},{14,15,4},{13,16,5},{12,17,6},{11,18,7},{10,9,8}};&lt;br /&gt; int maxh = HEIGHT-1, maxw = WIDTH-1;&lt;br /&gt; int minh = 0, minw = 0;&lt;br /&gt; while(1)&lt;br /&gt; {&lt;br /&gt;  if(minh &amp;gt; maxh || minw &amp;gt; maxw)&lt;br /&gt;   break;&lt;br /&gt;&lt;br /&gt;  //top-left to top-right&lt;br /&gt;  for(int i=minh,j=minw;j&amp;lt;=maxw;j++)&lt;br /&gt;   cout &amp;lt;&amp;lt; array[i][j] &amp;lt;&amp;lt; endl;&lt;br /&gt;  minh++;&lt;br /&gt;&lt;br /&gt;  if(minh &amp;gt; maxh || minw &amp;gt; maxw)&lt;br /&gt;   break;&lt;br /&gt;&lt;br /&gt;  //top-right to bottom-right&lt;br /&gt;  for(int i=minh,j=maxw;i&amp;lt;=maxh;i++)&lt;br /&gt;   cout &amp;lt;&amp;lt; array[i][j] &amp;lt;&amp;lt; endl;&lt;br /&gt;  maxw--;&lt;br /&gt;&lt;br /&gt;  if(minh &amp;gt; maxh || minw &amp;gt; maxw)&lt;br /&gt;   break;&lt;br /&gt;&lt;br /&gt;  //bottom-right to bottom-left&lt;br /&gt;  for(int i=maxh,j=maxw;j&amp;gt;=minw;j--)&lt;br /&gt;   cout &amp;lt;&amp;lt; array[i][j] &amp;lt;&amp;lt; endl;&lt;br /&gt;  maxh--;&lt;br /&gt;&lt;br /&gt;  if(minh &amp;gt; maxh || minw &amp;gt; maxw)&lt;br /&gt;   break;&lt;br /&gt;&lt;br /&gt;  //bottom-left to top-left&lt;br /&gt;  for(int i=maxh,j=minw;i&amp;gt;=minh;i--)&lt;br /&gt;   cout &amp;lt;&amp;lt; array[i][j] &amp;lt;&amp;lt; endl;&lt;br /&gt;  minw++;&lt;br /&gt; }&lt;br /&gt; return 0;&lt;br /&gt;}&lt;br /&gt;//1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Cheers!&lt;br /&gt;Bragaadeesh.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-3526554670620211219?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/3526554670620211219/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=3526554670620211219' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/3526554670620211219'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/3526554670620211219'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/05/print-matrix-in-circular-path-interview.html' title='Print Matrix in a circular path (Interview question)'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_kRev2kjeMro/S-nMf8nbseI/AAAAAAAACMo/4NLtplZBJ5g/s72-c/Matrix.png' height='72' width='72'/><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-7055174967711312025</id><published>2010-05-09T14:24:00.000-07:00</published><updated>2010-05-17T12:38:31.664-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='codejam'/><category scheme='http://www.blogger.com/atom/ns#' term='solution'/><category scheme='http://www.blogger.com/atom/ns#' term='google'/><category scheme='http://www.blogger.com/atom/ns#' term='coding'/><title type='text'>Google CodeJam 2010 : Theme Park with Solution</title><content type='html'>Guys,&lt;br /&gt;&lt;br /&gt;This is one of the three problems asked in the 2010 Edition of Google Codejam Qualification Round. The question is quite interesting and simple to solve and I did solve. But the problem here is the running time. If not a proper strategy is followed, then the running time of the algorithm will be more and eventually you cannot submit the answer in time for large data set. Lets look at the problem.&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/_kRev2kjeMro/S-chqdYibLI/AAAAAAAACMA/2Ixj1TRezKU/s1600/RollerCoaster.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://1.bp.blogspot.com/_kRev2kjeMro/S-chqdYibLI/AAAAAAAACMA/2Ixj1TRezKU/s640/RollerCoaster.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;h3 style="font-family: Verdana,sans-serif;"&gt;&lt;span style="font-size: x-large;"&gt;Problem&lt;/span&gt;&lt;/h3&gt;Roller coasters are so much fun! It seems like everybody who visits the theme park wants to ride the roller coaster. Some people go alone; other people go in groups, and don't want to board the roller coaster unless they can all go together. And &lt;i&gt;everyone&lt;/i&gt; who rides the roller coaster wants to ride again. A ride costs 1 Euro per person; your job is to figure out how much money the roller coaster will make today. &lt;br /&gt;The roller coaster can hold &lt;b&gt;k&lt;/b&gt; people at once. People queue for it in groups. Groups board the roller coaster, one at a time, until there are no more groups left or there is no room for the next group; then the roller coaster goes, whether it's full or not. Once the ride is over, all of its passengers re-queue in the same order. The roller coaster will run &lt;b&gt;R&lt;/b&gt; times in a day. &lt;br /&gt;For example, suppose &lt;b&gt;R&lt;/b&gt;=4, &lt;b&gt;k&lt;/b&gt;=6, and there are four groups of people with sizes: 1, 4, 2, 1. The first time the roller coaster goes, the first two groups [1, 4] will ride, leaving an empty seat (the group of 2 won't fit, and the group of 1 can't go ahead of them). Then they'll go to the back of the queue, which now looks like 2, 1, 1, 4. The second time, the coaster will hold 4 people: [2, 1, 1]. Now the queue looks like 4, 2, 1, 1. The third time, it will hold 6 people: [4, 2]. Now the queue looks like [1, 1, 4, 2]. Finally, it will hold 6 people: [1, 1, 4]. The roller coaster has made a total of 21 Euros!&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/_kRev2kjeMro/S-clUCB9HHI/AAAAAAAACMI/EFWa_M16oM4/s1600/google-codejam-rollercoaster-1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/_kRev2kjeMro/S-clUCB9HHI/AAAAAAAACMI/EFWa_M16oM4/s640/google-codejam-rollercoaster-1.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/_kRev2kjeMro/S-cliyEwmPI/AAAAAAAACMQ/w21GU3wq1b4/s1600/google-codejam-rollercoaster-2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://1.bp.blogspot.com/_kRev2kjeMro/S-cliyEwmPI/AAAAAAAACMQ/w21GU3wq1b4/s640/google-codejam-rollercoaster-2.png" /&gt;&lt;/a&gt;&lt;a href="http://2.bp.blogspot.com/_kRev2kjeMro/S-clmK1wTgI/AAAAAAAACMY/nvXrU__jxaU/s1600/google-codejam-rollercoaster-3.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/_kRev2kjeMro/S-clmK1wTgI/AAAAAAAACMY/nvXrU__jxaU/s640/google-codejam-rollercoaster-3.png" /&gt;&lt;/a&gt;&lt;a href="http://2.bp.blogspot.com/_kRev2kjeMro/S-cloqSmWcI/AAAAAAAACMg/CcMrgI7HOgs/s1600/google-codejam-rollercoaster-4.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/_kRev2kjeMro/S-cloqSmWcI/AAAAAAAACMg/CcMrgI7HOgs/s640/google-codejam-rollercoaster-4.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;h3&gt;&amp;nbsp;&lt;/h3&gt;&lt;h3&gt;Input&lt;/h3&gt;The first line of the input gives the number of test cases, &lt;b&gt;T&lt;/b&gt;.  &lt;b&gt;T&lt;/b&gt; test cases follow, with each test case consisting of two lines.  The first line contains three space-separated integers: &lt;b&gt;R&lt;/b&gt;, &lt;b&gt;k&lt;/b&gt; and &lt;b&gt;N&lt;/b&gt;.  The second line contains &lt;b&gt;N&lt;/b&gt; space-separated integers &lt;b&gt;g&lt;sub&gt;i&lt;/sub&gt;&lt;/b&gt;, each of which is the size of a group that wants to ride.  &lt;b&gt;g&lt;sub&gt;0&lt;/sub&gt;&lt;/b&gt; is the size of the first group, &lt;b&gt;g&lt;sub&gt;1&lt;/sub&gt;&lt;/b&gt; is the size of the second group, etc. &lt;br /&gt;&lt;h3&gt;Output&lt;/h3&gt;For each test case, output one line containing "Case #x: y", where x is the case number (starting from 1) and y is the number of Euros made by the roller coaster. &lt;br /&gt;&lt;h3&gt;Limits&lt;/h3&gt;1 ≤ &lt;b&gt;T&lt;/b&gt; ≤ 50.&lt;br /&gt;&lt;b&gt;g&lt;sub&gt;i&lt;/sub&gt;&lt;/b&gt; ≤ &lt;b&gt;k&lt;/b&gt;. &lt;br /&gt;&lt;h4&gt;Small dataset&lt;/h4&gt;1 ≤ &lt;b&gt;R&lt;/b&gt; ≤ 1000.&lt;br /&gt;1 ≤ &lt;b&gt;k&lt;/b&gt; ≤ 100.&lt;br /&gt;1 ≤ &lt;b&gt;N&lt;/b&gt; ≤ 10.&lt;br /&gt;1 ≤ &lt;b&gt;g&lt;sub&gt;i&lt;/sub&gt;&lt;/b&gt; ≤ 10.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Large dataset&lt;/h4&gt;1 ≤ &lt;b&gt;R&lt;/b&gt; ≤ 10&lt;sup&gt;8&lt;/sup&gt;.&lt;br /&gt;1 ≤ &lt;b&gt;k&lt;/b&gt; ≤ 10&lt;sup&gt;9&lt;/sup&gt;.&lt;br /&gt;1 ≤ &lt;b&gt;N&lt;/b&gt; ≤ 1000.&lt;br /&gt;1 ≤ &lt;b&gt;g&lt;sub&gt;i&lt;/sub&gt;&lt;/b&gt; ≤ 10&lt;sup&gt;7&lt;/sup&gt;.&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Sample&lt;/h3&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;br /&gt;&lt;span class="io-table-header"&gt;Input&lt;/span&gt; &lt;br /&gt;&amp;nbsp; &lt;/td&gt; &lt;td&gt;&lt;br /&gt;&lt;span class="io-table-header"&gt;Output&lt;/span&gt; &lt;br /&gt;&amp;nbsp; &lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td&gt;&lt;code&gt; 3&lt;br /&gt;4 6 4&lt;br /&gt;1 4 2 1&lt;br /&gt;100 10 1&lt;br /&gt;1&lt;br /&gt;5 5 10&lt;br /&gt;2 4 2 3 4 2 1 2 1 3&lt;br /&gt;&lt;/code&gt; &lt;/td&gt; &lt;td&gt;&lt;code&gt; Case #1: 21&lt;br /&gt;Case #2: 100&lt;br /&gt;Case #3: 20&lt;br /&gt;&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;div style="font-family: Verdana,sans-serif;"&gt;&lt;span style="font-size: x-large;"&gt;Solution&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;The solution for this problem may look simple and if we follow the problem as is, its pretty straight-forward. So, here is the solution that I initially arrived at,&lt;br /&gt;&lt;pre class="brush:java"&gt;private int solve(int R, int k, int[] groups){&lt;br /&gt;  &lt;br /&gt;  int moneyMade = 0;&lt;br /&gt;  LinkedList&amp;lt;Integer&amp;gt; groupsQueue = new LinkedList&amp;lt;Integer&amp;gt;();&lt;br /&gt;  &lt;br /&gt;  for(int i=0;i&amp;lt;groups.length;i++){&lt;br /&gt;   groupsQueue.add(groups[i]);&lt;br /&gt;  }&lt;br /&gt;  &lt;br /&gt;  while(R&amp;gt;0){&lt;br /&gt;   //calculate the people that can fit in&lt;br /&gt;   int eachSum = 0;&lt;br /&gt;   int i;&lt;br /&gt;   for(i=0;i&amp;lt;groups.length;i++){&lt;br /&gt;    eachSum+=groupsQueue.get(i);&lt;br /&gt;    if(eachSum&amp;gt;k){&lt;br /&gt;     eachSum-=groupsQueue.get(i);&lt;br /&gt;     break;&lt;br /&gt;    }&lt;br /&gt;   }&lt;br /&gt;   moneyMade+=eachSum;&lt;br /&gt;   //alterQueue&lt;br /&gt;   for(int j=0;j&amp;lt;i;j++){&lt;br /&gt;    groupsQueue.add(groupsQueue.poll());&lt;br /&gt;   }&lt;br /&gt;   R--;&lt;br /&gt;  }&lt;br /&gt;  &lt;br /&gt;  return moneyMade;&lt;br /&gt; }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;If you look at the above solution, I have followed the problem by the word. But turned out this cannot take the large data input set simply because of the amount of time it takes to run. The mole in the above solution is that I calculate the eachSum everytime for all R iterations. The large dataset is having R upto 10^8 and Group to be 10^7. So the runtime in worst case will be 10^16 which even the fastest computers invented these days struggle to solve in small time.&lt;br /&gt;&lt;br /&gt;So, I had to attack my problem in a different manner. All Google CodeJam problems dont have any space criterion, so its wise to make use of it. So, what would be ideal here is to precalculate the sum and extent for each group before hand. This would take a max of 10^3 * 10^3 = 10^6 iterations. The results are stored in a separate array for faster recovery. &lt;br /&gt;&lt;br /&gt;Then iterate through the entire R once and finish the problem in constant time. The matured code is given below.&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:java"&gt;private long solve(long R, long k, long[] groups){&lt;br /&gt;  int len = groups.length;&lt;br /&gt;  long[] sums = new long[len];&lt;br /&gt;  int[] span = new int[len];&lt;br /&gt;  //irrespective of R, calculate and keep the extent and sum in separate arrays&lt;br /&gt;  for(int x=0;x&amp;lt;len;x++){//Maximum 1000 runs&lt;br /&gt;   int sum = 0; int extent=0;&lt;br /&gt;   for(int y=x;;y++){&lt;br /&gt;    if(sum+groups[y%len]&amp;gt;k || len==extent){&lt;br /&gt;     break;&lt;br /&gt;    }&lt;br /&gt;    extent++;&lt;br /&gt;    sum+=groups[y%len];&lt;br /&gt;   }&lt;br /&gt;   sums[x] = sum;&lt;br /&gt;   span[x] = extent;&lt;br /&gt;  }&lt;br /&gt;  long totalSum = 0;&lt;br /&gt;  int i = 0;&lt;br /&gt;  for(int r=0;r&amp;lt;R;r++){&lt;br /&gt;   totalSum += sums[i];&lt;br /&gt;   i+=span[i];&lt;br /&gt;   i=i%len;&lt;br /&gt;  }&lt;br /&gt;  return totalSum;&lt;br /&gt; }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Files you may need.&lt;br /&gt;1. &lt;a href="http://docs.google.com/uc?id=0B_z-9Q7kZRM7Y2FmMjg0ZmUtODQwZi00OGEzLWJiMmItMGU0YzE5ZTQyZTdm&amp;amp;export=download&amp;amp;hl=en"&gt;Input file&lt;/a&gt; - Small dataset&lt;br /&gt;2. &lt;a href="http://docs.google.com/uc?id=0B_z-9Q7kZRM7MjM3NDVjMWQtMzJkYy00ZmJhLTkyZTEtOTM2MTFhMjJiMmIy&amp;amp;export=download&amp;amp;hl=en"&gt;Input file&lt;/a&gt; - Large dataset&lt;br /&gt;3. &lt;a href="http://docs.google.com/uc?id=0B_z-9Q7kZRM7M2YxNDFjMDMtMWI0Yi00Y2U5LWI2YzYtYzkyN2ZjZDJiODAw&amp;amp;export=download&amp;amp;hl=en"&gt;Output file&lt;/a&gt; - Small dataset&lt;br /&gt;4. &lt;a href="http://docs.google.com/uc?id=0B_z-9Q7kZRM7OTE0N2I2NmItOGMxMS00Y2FhLTk0MTQtYWM3OWY3YmU5MjRk&amp;amp;export=download&amp;amp;hl=en"&gt;Output file&lt;/a&gt; - Large dataset&lt;br /&gt;5. &lt;a href="http://docs.google.com/uc?id=0B_z-9Q7kZRM7ZjA5NGEyZTUtYmMxOS00YzIzLTgyZGYtN2ZjMDhkZWI0M2Ix&amp;amp;export=download&amp;amp;hl=en"&gt;Complete Source code&lt;/a&gt; in Java&lt;br /&gt;&lt;br /&gt;Cheers!,&lt;br /&gt;Bragaadeesh.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-7055174967711312025?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/7055174967711312025/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=7055174967711312025' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/7055174967711312025'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/7055174967711312025'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/05/google-codejam-2010-theme-park-with.html' title='Google CodeJam 2010 : Theme Park with Solution'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_kRev2kjeMro/S-chqdYibLI/AAAAAAAACMA/2Ixj1TRezKU/s72-c/RollerCoaster.png' height='72' width='72'/><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-4921146982740131184</id><published>2010-05-04T10:06:00.000-07:00</published><updated>2010-05-04T10:17:27.511-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='problem'/><category scheme='http://www.blogger.com/atom/ns#' term='codejam'/><category scheme='http://www.blogger.com/atom/ns#' term='perl'/><category scheme='http://www.blogger.com/atom/ns#' term='google'/><category scheme='http://www.blogger.com/atom/ns#' term='coding'/><title type='text'>Google CodeJam : Alien Language Problem with Solution</title><content type='html'>This is the problem asked in 2009 Google Codejam's qualification round. Lets get into business straightaway.&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: xml"&gt;Problem&lt;br /&gt;&lt;br /&gt;After years of study, scientists at Google Labs have discovered an alien language transmitted from a faraway planet. The alien language is very unique in that every word consists of exactly L lowercase letters. Also, there are exactly D words in this language.&lt;br /&gt;&lt;br /&gt;Once the dictionary of all the words in the alien language was built, the next breakthrough was to discover that the aliens have been transmitting messages to Earth for the past decade. Unfortunately, these signals are weakened due to the distance between our two planets and some of the words may be misinterpreted. In order to help them decipher these messages, the scientists have asked you to devise an algorithm that will determine the number of possible interpretations for a given pattern.&lt;br /&gt;&lt;br /&gt;A pattern consists of exactly L tokens. Each token is either a single lowercase letter (the scientists are very sure that this is the letter) or a group of unique lowercase letters surrounded by parenthesis ( and ). For example: (ab)d(dc) means the first letter is either a or b, the second letter is definitely d and the last letter is either d or c. Therefore, the pattern (ab)d(dc) can stand for either one of these 4 possibilities: add, adc, bdd, bdc.&lt;br /&gt;&lt;br /&gt;Input&lt;br /&gt;&lt;br /&gt;The first line of input contains 3 integers, L, D and N separated by a space. D lines follow, each containing one word of length L. These are the words that are known to exist in the alien language. N test cases then follow, each on its own line and each consisting of a pattern as described above. You may assume that all known words provided are unique.&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;&lt;br /&gt;For each test case, output&lt;br /&gt;&lt;br /&gt;Case #X: K&lt;br /&gt;&lt;br /&gt;where X is the test case number, starting from 1, and K indicates how many words in the alien language match the pattern.&lt;br /&gt;&lt;br /&gt;Limits&lt;br /&gt;&lt;br /&gt;Small dataset&lt;br /&gt;&lt;br /&gt;1 ≤ L ≤ 10&lt;br /&gt;1 ≤ D ≤ 25&lt;br /&gt;1 ≤ N ≤ 10&lt;br /&gt;&lt;br /&gt;Large dataset&lt;br /&gt;&lt;br /&gt;1 ≤ L ≤ 15&lt;br /&gt;1 ≤ D ≤ 5000&lt;br /&gt;1 ≤ N ≤ 500&lt;br /&gt;&lt;br /&gt;Sample&lt;br /&gt;&lt;br /&gt;Input&lt;br /&gt;   &lt;br /&gt;3 5 4&lt;br /&gt;abc&lt;br /&gt;bca&lt;br /&gt;dac&lt;br /&gt;dbc&lt;br /&gt;cba&lt;br /&gt;(ab)(bc)(ca)&lt;br /&gt;abc&lt;br /&gt;(abc)(abc)(abc)&lt;br /&gt;(zyx)bc&lt;br /&gt;&lt;br /&gt;Output&lt;br /&gt;Case #1: 2&lt;br /&gt;Case #2: 1&lt;br /&gt;Case #3: 3&lt;br /&gt;Case #4: 0&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The solution for this problem is relatively simple compared to the other codejam problems that we are going to solve in the future. The solution is implemented in Perl language. It uses the regex pattern matching method.&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: perl"&gt;#!/usr/local/bin/perl&lt;br /&gt;&lt;br /&gt;use strict;&lt;br /&gt;use warnings;&lt;br /&gt;&lt;br /&gt;print &amp;quot;Enter file name :: &amp;quot;;&lt;br /&gt;my $infile = &amp;lt;STDIN&amp;gt;;&lt;br /&gt;&lt;br /&gt;open(INP,&amp;quot;&amp;lt;$infile&amp;quot;) or die &amp;quot;can not open file for reading&amp;quot;;&lt;br /&gt;&lt;br /&gt;my $line_num = 0;&lt;br /&gt;my $num_of_words = 0;&lt;br /&gt;my $num_of_testcases = 0;&lt;br /&gt;my @words_array;&lt;br /&gt;my @testcases_array;&lt;br /&gt;&lt;br /&gt;while(&amp;lt;INP&amp;gt;)&lt;br /&gt;{&lt;br /&gt; if($line_num == 0)&lt;br /&gt; {&lt;br /&gt;  my @tmp = split(/\s/,$_);&lt;br /&gt;  $num_of_words = $tmp[1];&lt;br /&gt;  $num_of_testcases = $tmp[2];&lt;br /&gt; }&lt;br /&gt; elsif($line_num &amp;gt;= 1 and $line_num &amp;lt; ($num_of_words + 1))&lt;br /&gt; {&lt;br /&gt;  s/[\s\t\n]//;&lt;br /&gt;  $words_array[$line_num - 1] = $_;&lt;br /&gt; }&lt;br /&gt; elsif($line_num &amp;gt;= ($num_of_words + 1) &amp;amp;&amp;amp; $line_num &amp;lt; ($num_of_words + $num_of_testcases + 1))&lt;br /&gt; {&lt;br /&gt;  s/[\s\t\n]//;&lt;br /&gt;  s/\(/[/g;&lt;br /&gt;  s/\)/]/g;&lt;br /&gt;  $testcases_array[$line_num - 1 - $num_of_words] = $_;&lt;br /&gt; }&lt;br /&gt; $line_num++;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;my $words_passed = 0;&lt;br /&gt;my $testcase_num = 1;&lt;br /&gt;foreach my $pattern (@testcases_array)&lt;br /&gt;{&lt;br /&gt; foreach my $word (@words_array)&lt;br /&gt; {&lt;br /&gt;  if($word =~ m/$pattern/)&lt;br /&gt;  {&lt;br /&gt;   $words_passed++;&lt;br /&gt;  }&lt;br /&gt; }&lt;br /&gt; print &amp;quot;Case #$testcase_num: $words_passed\n&amp;quot;;&lt;br /&gt; $words_passed = 0;&lt;br /&gt; $testcase_num++;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;close(INP);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Input file of large dataset : &lt;a href="http://docs.google.com/uc?id=0B_z-9Q7kZRM7MTU1ZTY0OWMtMWQ0NC00ZWE0LTgzMTEtYzA2ZDIwYTcwMDE3&amp;export=download&amp;hl=en"&gt;Download&lt;/a&gt;&lt;br /&gt;Output file of large dataset : &lt;a href="http://docs.google.com/uc?id=0B_z-9Q7kZRM7NmNlOWQ5Y2ItZTQxYy00NWZmLWI1OGQtZTkxMWE4YzUzNmE3&amp;export=download&amp;hl=en"&gt;Download&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Cheers!&lt;br /&gt;Bragaadeesh&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-4921146982740131184?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/4921146982740131184/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=4921146982740131184' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/4921146982740131184'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/4921146982740131184'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/05/google-codejam-alien-language-problem.html' title='Google CodeJam : Alien Language Problem with Solution'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-3161455638264935265</id><published>2010-05-03T11:57:00.000-07:00</published><updated>2010-05-03T11:59:17.548-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='array'/><category scheme='http://www.blogger.com/atom/ns#' term='problem'/><category scheme='http://www.blogger.com/atom/ns#' term='interview'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><category scheme='http://www.blogger.com/atom/ns#' term='coding'/><title type='text'>Find three numbers in an array which forms a maximum product (for signed integers)</title><content type='html'>This is a problem that we had &lt;a href="http://www.technicalypto.com/2010/04/find-three-numbers-in-that-array-which.html"&gt;already seen&lt;/a&gt;. But it gives more kick if the input has negative elements and zero!!!&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Question: &lt;/b&gt;&lt;br /&gt;Given an array of integers (signed integers), find three numbers in that array which form the maximum product. [O(nlogn), O(n) solutions are available ].&lt;br /&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;int[] MaxProduct(int[] input, int size)&lt;/div&gt;&lt;br /&gt;The solution involves in finding three maximum and two minimum numbers. If the minimum numbers are negatives and if their product is greater than the two maximum number's product, then they have to considered for maximum product. Handling all these scenarios with comprehensive test cases, please find the code below for the problem implemented in C++&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: cpp"&gt;//============================================================================&lt;br /&gt;// Name        : three_largest_elems.cpp&lt;br /&gt;// Author      : Prabhu Jayaraman&lt;br /&gt;// Version     : v2&lt;br /&gt;// Copyright   : open&lt;br /&gt;// Description : To find three signed numbers in an array with max product&lt;br /&gt;//========================================================a====================&lt;br /&gt;&lt;br /&gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;using namespace std;&lt;br /&gt;&lt;br /&gt;#define MAX 10&lt;br /&gt;&lt;br /&gt;int* MaxProduct(const int input[], const int size)&lt;br /&gt;{&lt;br /&gt; int* output = new int[3];&lt;br /&gt; int negative = 0;&lt;br /&gt; for(int i = 0; i &amp;lt; 3; i++)&lt;br /&gt; {&lt;br /&gt;  output[i] = -999999;&lt;br /&gt; }&lt;br /&gt; int min[2] = {0,0};&lt;br /&gt;&lt;br /&gt; for(int i=0;i&amp;lt;size;i++)&lt;br /&gt; {&lt;br /&gt;  // find two smallest negative numbers&lt;br /&gt;  if(input[i] &amp;lt;= 0)&lt;br /&gt;  {&lt;br /&gt;   if(input[i] &amp;lt; min[0])&lt;br /&gt;   {&lt;br /&gt;    min[1] = min[0];&lt;br /&gt;    min[0] = input[i];&lt;br /&gt;   }&lt;br /&gt;   else if(input[i] &amp;lt; min[1])&lt;br /&gt;   {&lt;br /&gt;    min[1] = input[i];&lt;br /&gt;   }&lt;br /&gt;   negative++;&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  // find three largest positive numbers&lt;br /&gt;  if(input[i] &amp;gt; output[0])&lt;br /&gt;  {&lt;br /&gt;   output[2] = output[1];&lt;br /&gt;   output[1] = output[0];&lt;br /&gt;   output[0] = input[i];&lt;br /&gt;  }&lt;br /&gt;  else if(input[i] &amp;gt; output[1])&lt;br /&gt;  {&lt;br /&gt;   output[2] = output[1];&lt;br /&gt;   output[1] = input[i];&lt;br /&gt;  }&lt;br /&gt;  else if(input[i] &amp;gt; output[2])&lt;br /&gt;  {&lt;br /&gt;   output[2] = input[i];&lt;br /&gt;  }&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; if(size != negative)&lt;br /&gt; {&lt;br /&gt;  if((min[0] * min[1]) &amp;gt; (output[0] * output[1]) || (min[0] * min[1]) &amp;gt; (output[1] * output[2]))&lt;br /&gt;  {&lt;br /&gt;   output[1] = min[0];&lt;br /&gt;   output[2] = min[1];&lt;br /&gt;  }&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; return output;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int main()&lt;br /&gt;{&lt;br /&gt; const int input[MAX] = {-6,-1,-2,-33,-4,-15,-7,-28,-9,-10};&lt;br /&gt; int* result = 0;&lt;br /&gt; result = MaxProduct(input,MAX);&lt;br /&gt;&lt;br /&gt; for(int i = 0; i &amp;lt; 3; i++)&lt;br /&gt; {&lt;br /&gt;  cout &amp;lt;&amp;lt; (i+1) &amp;lt;&amp;lt; "# element : " &amp;lt;&amp;lt; result[i] &amp;lt;&amp;lt; endl;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; const int input1[MAX] = {0,-1,-2,-33,4,15,-7,-28,-9,-10};&lt;br /&gt; int* result1 = 0;&lt;br /&gt; result1 = MaxProduct(input1,MAX);&lt;br /&gt;&lt;br /&gt; for(int i = 0; i &amp;lt; 3; i++)&lt;br /&gt; {&lt;br /&gt;  cout &amp;lt;&amp;lt; (i+1) &amp;lt;&amp;lt; "# element : " &amp;lt;&amp;lt; result1[i] &amp;lt;&amp;lt; endl;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; const int input2[MAX] = {0,-1,-2,-33,-4,-15,-7,-28,-9,-10};&lt;br /&gt; int* result2 = 0;&lt;br /&gt; result2 = MaxProduct(input2,MAX);&lt;br /&gt;&lt;br /&gt; for(int i = 0; i &amp;lt; 3; i++)&lt;br /&gt; {&lt;br /&gt;  cout &amp;lt;&amp;lt; (i+1) &amp;lt;&amp;lt; "# element : " &amp;lt;&amp;lt; result2[i] &amp;lt;&amp;lt; endl;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; const int input3[MAX] = {6,1,2,33,4,15,7,28,9,10};&lt;br /&gt; int* result3 = 0;&lt;br /&gt; result3 = MaxProduct(input3,MAX);&lt;br /&gt;&lt;br /&gt; for(int i = 0; i &amp;lt; 3; i++)&lt;br /&gt; {&lt;br /&gt;  cout &amp;lt;&amp;lt; (i+1) &amp;lt;&amp;lt; "# element : " &amp;lt;&amp;lt; result3[i] &amp;lt;&amp;lt; endl;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; return 0;&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;Cheers!&lt;br /&gt;Bragaadeesh.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-3161455638264935265?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/3161455638264935265/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=3161455638264935265' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/3161455638264935265'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/3161455638264935265'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/05/find-three-numbers-in-array-which-forms.html' title='Find three numbers in an array which forms a maximum product (for signed integers)'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-6411185361328123472</id><published>2010-05-03T10:43:00.000-07:00</published><updated>2010-05-03T10:43:17.395-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='array'/><category scheme='http://www.blogger.com/atom/ns#' term='problem'/><category scheme='http://www.blogger.com/atom/ns#' term='interview'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><category scheme='http://www.blogger.com/atom/ns#' term='coding'/><title type='text'>Given a sorted array find the pair that forms the sum in O(n) time</title><content type='html'>This problem is quite frequently asked and we had already seen a solution in O(n) time but using a hash table &lt;a href="http://www.technicalypto.com/2010/02/given-array-find-number-that-sums-up.html"&gt;here&lt;/a&gt;. However in this posting we shall see how to attack this problem when the input data is sorted.&lt;br /&gt;&lt;div style="font-family: &amp;quot;Helvetica Neue&amp;quot;,Arial,Helvetica,sans-serif;"&gt;&lt;span style="font-size: small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: &amp;quot;Helvetica Neue&amp;quot;,Arial,Helvetica,sans-serif;"&gt;&lt;span style="font-size: small;"&gt;For example for the input,&lt;/span&gt;&lt;/div&gt;&lt;span style="font-family: &amp;quot;Helvetica Neue&amp;quot;,Arial,Helvetica,sans-serif; font-size: small;"&gt;[-12,-6,-4,-2,0,1,2,4,6,7,8,12,13,20,24] and the sum bein 0, the matching pair is -12 and 12&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The solution for this problem relies upon the knowledge of the input data : sorted. We will be having two pointers, one from the start of the Array called as min and the other from the end of the array called as max. We always calculate the sum with the values present in the minimum and maximum indexes. If the sum is greater than what is needed, we will be decrementing the max pointer and for the other case, we will be incrementing the min pointer. This happens till min exceeds max.&lt;br /&gt;&lt;br /&gt;Following the solution in C++&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: cpp"&gt;//============================================================================&lt;br /&gt;// Name        : array_find_sum.cpp&lt;br /&gt;// Author      : Prabhu Jayaraman&lt;br /&gt;// Version     : v1&lt;br /&gt;// Copyright   : Free&lt;br /&gt;// Description : Find elements in the array that equals to given sum&lt;br /&gt;//============================================================================&lt;br /&gt;&lt;br /&gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;using namespace std;&lt;br /&gt;&lt;br /&gt;#define MAX 15&lt;br /&gt;&lt;br /&gt;int main()&lt;br /&gt;{&lt;br /&gt; int array[MAX] = {-12,-6,-4,-2,0,1,2,4,6,7,8,12,13,20,24};&lt;br /&gt; const int find_sum = 0;&lt;br /&gt; int max_index = MAX - 1;&lt;br /&gt; int min_index = 0;&lt;br /&gt; while(min_index &amp;lt; max_index)&lt;br /&gt; {&lt;br /&gt;  if(array[min_index] + array[max_index-min_index] == find_sum)&lt;br /&gt;  {&lt;br /&gt;   cout &amp;lt;&amp;lt; array[min_index] &amp;lt;&amp;lt; &amp;quot; &amp;amp; &amp;quot; &amp;lt;&amp;lt; array[max_index-min_index] &amp;lt;&amp;lt; &amp;quot; Matched&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;   return 0;&lt;br /&gt;  }&lt;br /&gt;  if(array[min_index]+array[max_index-min_index] &amp;lt; find_sum)&lt;br /&gt;  {&lt;br /&gt;   min_index++;&lt;br /&gt;   max_index++;&lt;br /&gt;  }&lt;br /&gt;  if(array[min_index]+array[max_index-min_index] &amp;gt; find_sum)&lt;br /&gt;  {&lt;br /&gt;   max_index--;&lt;br /&gt;  }&lt;br /&gt; }&lt;br /&gt; cout &amp;lt;&amp;lt; &amp;quot;NO MATCH&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt; return 0;&lt;br /&gt;}&lt;br /&gt;//-12 &amp;amp; 12 matched&lt;/pre&gt;&lt;br /&gt;Cheers!&lt;br /&gt;Bragaadeesh&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-6411185361328123472?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/6411185361328123472/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=6411185361328123472' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/6411185361328123472'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/6411185361328123472'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/05/given-sorted-array-find-pair-that-forms.html' title='Given a sorted array find the pair that forms the sum in O(n) time'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-2775244551291342963</id><published>2010-05-03T10:00:00.000-07:00</published><updated>2010-05-03T10:33:07.428-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='program'/><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='array'/><category scheme='http://www.blogger.com/atom/ns#' term='problem'/><category scheme='http://www.blogger.com/atom/ns#' term='interview'/><category scheme='http://www.blogger.com/atom/ns#' term='coding'/><title type='text'>Compact a given array coding problem</title><content type='html'>This problem may look very simple and indeed it is because the solution can be arrived at very easily. But to arrive at a clean and an efficient may take sometime. This question is asked to test the cleanliness and simplicity in providing solutions. Enough hype, here is the question.&lt;br /&gt;&lt;br /&gt;&lt;div style="font-family: &amp;quot;Helvetica Neue&amp;quot;,Arial,Helvetica,sans-serif;"&gt;&lt;span style="font-size: large;"&gt;Given an array of random numbers and -1 placed in-between, compact that array ie., all the -1s are to be removed and the final output should be the last valid index with the fresh array. For example.&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;img style="border:0px" src="http://3.bp.blogspot.com/_kRev2kjeMro/S98IiSJoY3I/AAAAAAAACL4/9TWuNi0tmJ4/s640/Input.png"/&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style="font-family: &amp;quot;Helvetica Neue&amp;quot;,Arial,Helvetica,sans-serif;"&gt;&lt;span style="font-size: large;"&gt;You should not swap the values just the last valid index along with the array is enough to decipher the not -1 values.&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;The solution to this problem is seemingly simple and the same has been implemented in Java below. &lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: java"&gt;public class CompactArray {&lt;br /&gt; public static void main(String[] args) {&lt;br /&gt;  CompactArray c = new CompactArray();&lt;br /&gt;  int[] sampleArray = {-1,5,3,-1,3,5,2,1,-1,6};&lt;br /&gt;  System.out.print(" Input : ");printArray(sampleArray, sampleArray.length);&lt;br /&gt;  int validIndex = c.compact(sampleArray);&lt;br /&gt;  System.out.print("Output : ");printArray(sampleArray, validIndex);&lt;br /&gt; }&lt;br /&gt; &lt;br /&gt; private int compact(int[] arr){&lt;br /&gt;  int k = 0;&lt;br /&gt;  for(int i=0;i&amp;lt;arr.length;i++){&lt;br /&gt;   if(arr[i]!=-1)&lt;br /&gt;    arr[k++] = arr[i];&lt;br /&gt;  }&lt;br /&gt;  return k;&lt;br /&gt; }&lt;br /&gt; &lt;br /&gt; private static void printArray(int[] sampleArray, int validIndex) {&lt;br /&gt;  for(int i=0;i&amp;lt;validIndex;i++){&lt;br /&gt;   System.out.print(sampleArray[i]+" ");&lt;br /&gt;  }&lt;br /&gt;  System.out.println();&lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;/*&lt;br /&gt;Input : -1 5 3 -1 3 5 2 1 -1 6 &lt;br /&gt;Output : 5 3 3 5 2 1 6 &lt;br /&gt;*/&lt;/pre&gt;&lt;br /&gt;Cheers!&lt;br /&gt;Bragaadeesh.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-2775244551291342963?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/2775244551291342963/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=2775244551291342963' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/2775244551291342963'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/2775244551291342963'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/05/compact-given-array-coding-problem.html' title='Compact a given array coding problem'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/_kRev2kjeMro/S98IiSJoY3I/AAAAAAAACL4/9TWuNi0tmJ4/s72-c/Input.png' height='72' width='72'/><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-5690593856523130450</id><published>2010-04-28T12:04:00.000-07:00</published><updated>2010-05-02T09:47:37.028-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='program'/><category scheme='http://www.blogger.com/atom/ns#' term='O(n)'/><category scheme='http://www.blogger.com/atom/ns#' term='interview'/><category scheme='http://www.blogger.com/atom/ns#' term='O(logn)'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><category scheme='http://www.blogger.com/atom/ns#' term='coding'/><title type='text'>Find three numbers in an array which forms a maximum product</title><content type='html'>This problem again is asked as a coding question in one of the top companies. I have provided the solution for this in C++.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Question:&lt;/b&gt;&lt;span style="font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size: small;"&gt;Given an array of integers (unsigned integers &amp;gt; 0), find three numbers in that array which form the maximum product. [O(nlogn), O(n) solutions are available ].&lt;br /&gt;int[] MaxProduct(int[] input, int size)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Solution:&lt;/b&gt;&lt;br /&gt;The program expects both the solutions, one in O(n) time and the other one in O(nlogn). To achieve O(nlogn), sort the entire set by using quick sort or merge sort and take the top three values. &lt;br /&gt;I have provided the code for the O(n) case. It does a single scan on the entire set of elements once and finds the largest three numbers with the logic as shown below.&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:cpp"&gt;//============================================================================&lt;br /&gt;// Name        : three_largest_elems.cpp&lt;br /&gt;// Author      : Prabhu Jayaraman (My honorable room mate, college mate, work colleague)&lt;br /&gt;// Version     : v1&lt;br /&gt;// Copyright   : open&lt;br /&gt;// Description : To find three largest numbers in an array in C++, Ansi-style&lt;br /&gt;//============================================================================&lt;br /&gt;&lt;br /&gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;using namespace std;&lt;br /&gt;&lt;br /&gt;int main()&lt;br /&gt;{&lt;br /&gt; int a[10] = {1,2,33,4,15,6,7,28,9,10};&lt;br /&gt; int largest_1 = 0, largest_2 = 0, largest_3 = 0;&lt;br /&gt;&lt;br /&gt; for(int i=0;i&amp;lt;10;i++)&lt;br /&gt; {&lt;br /&gt;  if(a[i] &amp;gt; largest_3 &amp;amp;&amp;amp; a[i] &amp;lt; largest_2)&lt;br /&gt;  {&lt;br /&gt;   largest_3 = a[i];&lt;br /&gt;  }&lt;br /&gt;  else if(a[i] &amp;gt; largest_2 &amp;amp;&amp;amp; a[i] &amp;lt; largest_1)&lt;br /&gt;  {&lt;br /&gt;   largest_3 = largest_2;&lt;br /&gt;   largest_2 = a[i];&lt;br /&gt;  }&lt;br /&gt;  else if(a[i] &amp;gt; largest_1)&lt;br /&gt;  {&lt;br /&gt;   largest_3 = largest_2;&lt;br /&gt;   largest_2 = largest_1;&lt;br /&gt;   largest_1 = a[i];&lt;br /&gt;  }&lt;br /&gt; }&lt;br /&gt; cout &amp;lt;&amp;lt; "largest :" &amp;lt;&amp;lt; largest_1 &amp;lt;&amp;lt; endl;&lt;br /&gt; cout &amp;lt;&amp;lt; "2nd largest :" &amp;lt;&amp;lt; largest_2 &amp;lt;&amp;lt; endl;&lt;br /&gt; cout &amp;lt;&amp;lt; "3rd largest :" &amp;lt;&amp;lt; largest_3 &amp;lt;&amp;lt; endl;&lt;br /&gt; return 0;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Cheers!!&lt;br /&gt;Bragaadeesh&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-5690593856523130450?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/5690593856523130450/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=5690593856523130450' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/5690593856523130450'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/5690593856523130450'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/04/find-three-numbers-in-that-array-which.html' title='Find three numbers in an array which forms a maximum product'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-223161511381048981</id><published>2010-04-28T10:48:00.000-07:00</published><updated>2010-04-28T10:51:00.366-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='problem'/><category scheme='http://www.blogger.com/atom/ns#' term='puzzle'/><category scheme='http://www.blogger.com/atom/ns#' term='interview'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><category scheme='http://www.blogger.com/atom/ns#' term='coding'/><title type='text'>Find the last person seated around in a circle program/puzzle</title><content type='html'>This is a programming question asked at one of the class companies. Following is the question&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size: large;"&gt;&lt;span style="background-color: #f3f3f3; color: blue;"&gt;“n” people are seated around in a circle. At each step, the “m”th person is eliminated from the circle. The next iteration continues from the person next to the eliminated person. In the end, there will be only one person remaining. Given “n” and “m”, write a program to find out the person who will be remaining at the end.&lt;/span&gt;&lt;br style="background-color: #f3f3f3; color: blue;" /&gt;&lt;span style="background-color: #f3f3f3; color: blue;"&gt;int FindWinner(int n, int m)&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Me and my roommate both implemented a solution for the above problem one in Java and the other in C++ respectively. Please see the code and the ouput for the above problem below.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="font-size: large;"&gt;Implementation in Java&lt;/span&gt;&lt;/b&gt; &lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: java"&gt;package brainteasers;&lt;br /&gt;&lt;br /&gt;public class FindWinner {&lt;br /&gt; public static void main(String[] args) {&lt;br /&gt;  FindWinner finder = new FindWinner();&lt;br /&gt;  char winner = finder.findWinner(10,3);&lt;br /&gt;  System.out.println("\nAnd the winner is "+(char)(winner-32)+"!!");&lt;br /&gt; }&lt;br /&gt; &lt;br /&gt; private char findWinner(int n, int m){&lt;br /&gt;  char[] people = getPeople(n);&lt;br /&gt;  boolean[] eliminated = new boolean[n];&lt;br /&gt;  //always start from 1st person&lt;br /&gt;  int remainingGuys = n;&lt;br /&gt;  int current = 0;&lt;br /&gt;  &lt;br /&gt;  while(remainingGuys&amp;gt;1){&lt;br /&gt;   int inkiPinki=0;&lt;br /&gt;   &lt;br /&gt;   while( eliminated[current] || ++inkiPinki != m )&lt;br /&gt;       current = (current+1) % n;&lt;br /&gt;   &lt;br /&gt;   eliminated[current] = true;&lt;br /&gt;   printStatus( eliminated, people );&lt;br /&gt;   remainingGuys--;&lt;br /&gt;   &lt;br /&gt;   while(eliminated[(current+1)%n]){&lt;br /&gt;    current++;&lt;br /&gt;   }&lt;br /&gt;   &lt;br /&gt;   current = (current+1)%n;&lt;br /&gt;  }&lt;br /&gt;  &lt;br /&gt;  System.out.println();&lt;br /&gt;  return people[current];&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; private void printStatus(boolean[] eliminated, char[] people) {&lt;br /&gt;  System.out.println();&lt;br /&gt;  for(int i=0;i&amp;lt;eliminated.length;i++){&lt;br /&gt;   char output;&lt;br /&gt;   if(eliminated[i]){&lt;br /&gt;    output = people[i];&lt;br /&gt;   }else{&lt;br /&gt;    output = (char)(people[i] - 32);&lt;br /&gt;   }&lt;br /&gt;   System.out.print(output+" ");&lt;br /&gt;  }&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; private char[] getPeople(int n) {&lt;br /&gt;  char[] people = new char[n];&lt;br /&gt;  System.out.println("Participants are...");&lt;br /&gt;  for(int i=0;i&amp;lt;n;i++){&lt;br /&gt;   people[i] = (char)(i+97);&lt;br /&gt;   System.out.print((char)(people[i]-32)+" ");&lt;br /&gt;  }&lt;br /&gt;  System.out.println();&lt;br /&gt;  return people;&lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;/*&lt;br /&gt;Participants are...&lt;br /&gt;A B C D E F G H I J &lt;br /&gt;&lt;br /&gt;A B c D E F G H I J &lt;br /&gt;A B c D E f G H I J &lt;br /&gt;A B c D E f G H i J &lt;br /&gt;A b c D E f G H i J &lt;br /&gt;A b c D E f g H i J &lt;br /&gt;a b c D E f g H i J &lt;br /&gt;a b c D E f g h i J &lt;br /&gt;a b c D e f g h i J &lt;br /&gt;a b c D e f g h i j &lt;br /&gt;&lt;br /&gt;And the winner is D!!&lt;br /&gt;*/&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="font-size: large;"&gt;Implementation in C++&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: cpp"&gt;//============================================================================&lt;br /&gt;// Name        : puzzle.cpp&lt;br /&gt;// Author      : Prabhu Jayaraman&lt;br /&gt;// Version     : 0.1&lt;br /&gt;// Copyright   : Free&lt;br /&gt;// Description : Find Last man stand&lt;br /&gt;//============================================================================&lt;br /&gt;&lt;br /&gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;using namespace std;&lt;br /&gt;&lt;br /&gt;int FindWinner(int n, int m)&lt;br /&gt;{&lt;br /&gt; int a[n];&lt;br /&gt; for(int i=0;i&amp;lt;n;i++)&lt;br /&gt; {&lt;br /&gt;  a[i] = 1;&lt;br /&gt; }&lt;br /&gt; int e = 0;&lt;br /&gt; int c = 0;&lt;br /&gt; for(int i=0;;i++)&lt;br /&gt; {&lt;br /&gt;  if(e == (n-1))&lt;br /&gt;   break;&lt;br /&gt;  if(a[i%n] == 1)&lt;br /&gt;   c++;&lt;br /&gt;  if(c == m)&lt;br /&gt;  {&lt;br /&gt;   a[i%n] = 0;&lt;br /&gt;   e++;&lt;br /&gt;   c = 0;&lt;br /&gt;  }&lt;br /&gt; }&lt;br /&gt; for(int i=0;i&amp;lt;n;i++)&lt;br /&gt; {&lt;br /&gt;  if(a[i] == 1)&lt;br /&gt;   return i+1;&lt;br /&gt; }&lt;br /&gt; return -1;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int main()&lt;br /&gt;{&lt;br /&gt; int x = FindWinner(20,19);&lt;br /&gt; cout &amp;lt;&amp;lt; x &amp;lt;&amp;lt; endl;&lt;br /&gt; return 0;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Cheers,&lt;br /&gt;Bragaadeesh&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-223161511381048981?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/223161511381048981/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=223161511381048981' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/223161511381048981'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/223161511381048981'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/04/find-last-person-seated-around-in.html' title='Find the last person seated around in a circle program/puzzle'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-8446347328931694874</id><published>2010-04-10T04:57:00.000-07:00</published><updated>2011-03-22T23:36:16.791-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='data structures'/><category scheme='http://www.blogger.com/atom/ns#' term='swing'/><category scheme='http://www.blogger.com/atom/ns#' term='words'/><category scheme='http://www.blogger.com/atom/ns#' term='tutorial'/><category scheme='http://www.blogger.com/atom/ns#' term='ADT'/><category scheme='http://www.blogger.com/atom/ns#' term='ui'/><category scheme='http://www.blogger.com/atom/ns#' term='TRIE'/><title type='text'>TRIE data structure Part 6 : A Sample UI</title><content type='html'>&lt;div style="margin: auto 33%;"&gt;&lt;ul id="pagination-digg"&gt;&lt;li class="previous"&gt;&lt;a href="http://www.technicalypto.com/2010/04/trie-data-structure-part-5-complexity.html"&gt;«Previous&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2010/04/trie-in-java.html"&gt;1&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2010/04/trie-data-structure-part-2-node-and.html"&gt;2&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2010/04/trie-data-structure-part-3-inserting.html"&gt;3&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2010/04/trie-data-structure-part-4-search.html"&gt;4&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2010/04/trie-data-structure-part-5-complexity.html"&gt;5&lt;/a&gt;&lt;/li&gt;&lt;li class="active"&gt;6&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2010/09/trie-data-structure-in-c.html"&gt;7&lt;/a&gt;&lt;/li&gt;&lt;li class="next"&gt;&lt;a href="http://www.technicalypto.com/2010/09/trie-data-structure-in-c.html"&gt;Next »&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;Lets have a look at a sample application in Swing. We load the TRIE data structure using a dictionary word list. And using the application we can perform a search. This application is a very much prototypical just to check the insert() and search() operations.&lt;br /&gt;&lt;br /&gt;Input file : &lt;a href="http://docs.google.com/uc?id=0B_z-9Q7kZRM7ZDRhZTQ3NmYtYzJlMy00MjIzLWFkNWItZjRlMTM2MGRjMjRj&amp;amp;export=download&amp;amp;hl=en"&gt;words.txt&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Sample Application&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/_kRev2kjeMro/S8BmZCp7u7I/AAAAAAAACLI/TxNR_KtBppA/s1600/TRIE+test+App.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/_kRev2kjeMro/S8BmZCp7u7I/AAAAAAAACLI/TxNR_KtBppA/s640/TRIE+test+App.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;There are two possible outcomes to our search criteria, one true and another false. The following is the dialog shown when a word "article" is entered.&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/_kRev2kjeMro/S8Bmzxk9fpI/AAAAAAAACLQ/fxxzC8MLuJc/s1600/TRIE+does+exist.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/_kRev2kjeMro/S8Bmzxk9fpI/AAAAAAAACLQ/fxxzC8MLuJc/s320/TRIE+does+exist.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;And when a word something like "dasdsa" is entered the following dialog is shown.&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/_kRev2kjeMro/S8Bm-A6FLKI/AAAAAAAACLY/OB1p4NN2kuQ/s1600/TRIE+does+not+exist.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/_kRev2kjeMro/S8Bm-A6FLKI/AAAAAAAACLY/OB1p4NN2kuQ/s320/TRIE+does+not+exist.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;The Java code for the Trie Loader,&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: java"&gt;package dsa.tries;&lt;br /&gt;&lt;br /&gt;import java.io.BufferedReader;&lt;br /&gt;import java.io.File;&lt;br /&gt;import java.io.FileReader;&lt;br /&gt;import java.io.IOException;&lt;br /&gt;&lt;br /&gt;public class TrieLoader {&lt;br /&gt; &lt;br /&gt; public static Trie trieDSA;&lt;br /&gt;&lt;br /&gt; public static void main(String[] args) {&lt;br /&gt;  TrieLoader trieLoader = new TrieLoader();&lt;br /&gt;  trieLoader.load();&lt;br /&gt;  new TrieTestFrame();&lt;br /&gt; }&lt;br /&gt; &lt;br /&gt; public void load(){&lt;br /&gt;  trieDSA = new Trie();&lt;br /&gt;  BufferedReader br = null;&lt;br /&gt;  try {&lt;br /&gt;   br = new BufferedReader(new FileReader(new File("src/properties/words.txt")));&lt;br /&gt;   String eachLine = null;&lt;br /&gt;   while((eachLine=br.readLine())!=null){&lt;br /&gt;    trieDSA.insert(eachLine);&lt;br /&gt;   }&lt;br /&gt;  } catch (Exception e) {&lt;br /&gt;   e.printStackTrace();&lt;br /&gt;  } finally{&lt;br /&gt;   if(br!=null){&lt;br /&gt;    try {&lt;br /&gt;     br.close();&lt;br /&gt;    } catch (IOException e) {&lt;br /&gt;     e.printStackTrace();&lt;br /&gt;    }&lt;br /&gt;   }&lt;br /&gt;  }&lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The TrieTestFrame,&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: java"&gt;package dsa.tries;&lt;br /&gt;&lt;br /&gt;import java.awt.Dimension;&lt;br /&gt;import java.awt.Toolkit;&lt;br /&gt;import java.awt.event.ActionEvent;&lt;br /&gt;import java.awt.event.ActionListener;&lt;br /&gt;&lt;br /&gt;import javax.swing.JButton;&lt;br /&gt;import javax.swing.JFrame;&lt;br /&gt;import javax.swing.JOptionPane;&lt;br /&gt;import javax.swing.JPanel;&lt;br /&gt;import javax.swing.JTextField;&lt;br /&gt;&lt;br /&gt;public class TrieTestFrame extends JFrame {&lt;br /&gt;&lt;br /&gt; private static final long serialVersionUID = 1L;&lt;br /&gt; &lt;br /&gt; private JPanel basePanel = new JPanel();&lt;br /&gt; private JTextField textField = new JTextField(20);&lt;br /&gt; private JButton button = new JButton("Check");&lt;br /&gt;&lt;br /&gt; public TrieTestFrame(){&lt;br /&gt;  designUI();&lt;br /&gt; }&lt;br /&gt; &lt;br /&gt; private void designUI(){&lt;br /&gt;  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);&lt;br /&gt;  this.setTitle("TRIE Test");&lt;br /&gt;  &lt;br /&gt;  button.addActionListener(new ActionListener() {&lt;br /&gt;   @Override&lt;br /&gt;   public void actionPerformed(ActionEvent e) {&lt;br /&gt;    if(TrieLoader.trieDSA.search(textField.getText())){&lt;br /&gt;     JOptionPane.showMessageDialog(basePanel, "The word you entered exist!!");&lt;br /&gt;    }else{&lt;br /&gt;     JOptionPane.showMessageDialog(basePanel, "The word you entered does not exist!!","Error",JOptionPane.ERROR_MESSAGE);&lt;br /&gt;    }&lt;br /&gt;   }&lt;br /&gt;  });&lt;br /&gt;  &lt;br /&gt;  basePanel.add(textField);&lt;br /&gt;  basePanel.add(button);&lt;br /&gt;  &lt;br /&gt;  add(basePanel);&lt;br /&gt;  &lt;br /&gt;  this.setSize(400,100);&lt;br /&gt;  &lt;br /&gt;  Toolkit tk = Toolkit.getDefaultToolkit();&lt;br /&gt;     Dimension screenSize = tk.getScreenSize();&lt;br /&gt;     int screenHeight = screenSize.height;&lt;br /&gt;     int screenWidth = screenSize.width;&lt;br /&gt;     setLocation(screenWidth / 2 - 200, screenHeight / 2 - 50);&lt;br /&gt;  &lt;br /&gt;  this.setVisible(true);&lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;For the Node and Trie class please click &lt;a href="http://www.technicalypto.com/2010/04/trie-data-structure-part-2-node-and.html"&gt;here&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;Cheers,&lt;br /&gt;Bragaadeesh.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-8446347328931694874?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/8446347328931694874/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=8446347328931694874' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/8446347328931694874'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/8446347328931694874'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/04/trie-data-structure-part-5-sample-ui.html' title='TRIE data structure Part 6 : A Sample UI'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_kRev2kjeMro/S8BmZCp7u7I/AAAAAAAACLI/TxNR_KtBppA/s72-c/TRIE+test+App.png' height='72' width='72'/><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-5232671580901027437</id><published>2010-04-10T04:17:00.000-07:00</published><updated>2011-03-22T23:34:03.043-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='data structures'/><category scheme='http://www.blogger.com/atom/ns#' term='words'/><category scheme='http://www.blogger.com/atom/ns#' term='tutorial'/><category scheme='http://www.blogger.com/atom/ns#' term='ADT'/><category scheme='http://www.blogger.com/atom/ns#' term='TRIE'/><category scheme='http://www.blogger.com/atom/ns#' term='big O'/><title type='text'>TRIE data structure Part 5 : Complexity Analysis</title><content type='html'>&lt;div style="margin: auto 33%;"&gt;&lt;ul id="pagination-digg"&gt;&lt;li class="previous"&gt;&lt;a href="http://www.technicalypto.com/2010/04/trie-data-structure-part-4-search.html"&gt;«Previous&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2010/04/trie-in-java.html"&gt;1&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2010/04/trie-data-structure-part-2-node-and.html"&gt;2&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2010/04/trie-data-structure-part-3-inserting.html"&gt;3&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2010/04/trie-data-structure-part-4-search.html"&gt;4&lt;/a&gt;&lt;/li&gt;&lt;li class="active"&gt;5&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2010/04/trie-data-structure-part-5-sample-ui.html"&gt;6&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2010/09/trie-data-structure-in-c.html"&gt;7&lt;/a&gt;&lt;/li&gt;&lt;li class="next"&gt;&lt;a href="http://www.technicalypto.com/2010/04/trie-data-structure-part-5-sample-ui.html"&gt;Next »&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;Now that we've seen the basic operations on how to work with a TRIE, we shall now see the space and time complexities in order to get a real feel of how good a TRIE data structure is. Lets take the two important operations INSERT and SEARCH to measure the complexity.&lt;br /&gt;&lt;br /&gt;INSERT operation first. Lets always take into account the worst case timing first and later convince ourselves of the practical timings. For every Node in the TRIE we had something called as Collection&lt;node&gt; where the Collection can be either a Set or a List. If we choose Set, the order of whatever operation we perform over that will be in O(1) time, whereas if we use a LinkedList the number of comparisons at worst will be 26 (the number of alphabets). So for moving from one node to another, there will be at least 26 comparisons will be required at each step.&amp;nbsp;&lt;/node&gt;&lt;br /&gt;&lt;br /&gt;Having these in mind, for inserting a word of length 'k' we need (k * 26) comparisons. By Applying the Big O notation it becomes O(k) which will be again O(1). Thus insert operations are performed in constant time irrespective of the length of the input string (this might look lik an understatement, but if we make the length of the input string a worst case maximum, this sentence holds true).&lt;br /&gt;&lt;br /&gt;Same holds true for the search operation as well. The search operation exactly performs the way the insert does and its order is O(k*26) = O(1).&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-5232671580901027437?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/5232671580901027437/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=5232671580901027437' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/5232671580901027437'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/5232671580901027437'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/04/trie-data-structure-part-5-complexity.html' title='TRIE data structure Part 5 : Complexity Analysis'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-452978433787200727</id><published>2010-04-10T03:00:00.000-07:00</published><updated>2011-03-22T23:29:13.712-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='data structures'/><category scheme='http://www.blogger.com/atom/ns#' term='words'/><category scheme='http://www.blogger.com/atom/ns#' term='interview'/><category scheme='http://www.blogger.com/atom/ns#' term='tutorial'/><category scheme='http://www.blogger.com/atom/ns#' term='TRIE'/><title type='text'>TRIE data structure Part 4 : The SEARCH Algorithm</title><content type='html'>&lt;div style="margin: auto 33%;"&gt;&lt;ul id="pagination-digg"&gt;&lt;li class="previous"&gt;&lt;a href="http://www.technicalypto.com/2010/04/trie-data-structure-part-3-inserting.html"&gt;«Previous&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2010/04/trie-in-java.html"&gt;1&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2010/04/trie-data-structure-part-2-node-and.html"&gt;2&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2010/04/trie-data-structure-part-3-inserting.html"&gt;3&lt;/a&gt;&lt;/li&gt;&lt;li class="active"&gt;4&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2010/04/trie-data-structure-part-5-complexity.html"&gt;5&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2010/04/trie-data-structure-part-5-sample-ui.html"&gt;6&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2010/09/trie-data-structure-in-c.html"&gt;7&lt;/a&gt;&lt;/li&gt;&lt;li class="next"&gt;&lt;a href="http://www.technicalypto.com/2010/04/trie-data-structure-part-5-complexity.html"&gt;Next »&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;In the &lt;a href="http://www.technicalypto.com/2010/04/trie-data-structure-part-3-inserting.html"&gt;previous&lt;/a&gt; section we saw how to insert string into TRIE. In this section, we shall see how to perform a search in the TRIE when a string or key is passed.&lt;br /&gt;&lt;br /&gt;Consider the following TRIE as usual.&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/_kRev2kjeMro/S8BJJ98aVJI/AAAAAAAACKo/JFKO5IdXNGY/s1600/TRIE+ADT.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/_kRev2kjeMro/S8BJJ98aVJI/AAAAAAAACKo/JFKO5IdXNGY/s640/TRIE+ADT.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;The search alogirthm involves the following steps&lt;br /&gt;&lt;ol&gt;&lt;li style="font-family: &amp;quot;Helvetica Neue&amp;quot;,Arial,Helvetica,sans-serif;"&gt;For each character in the string, see if there is a child node with that character as the content.&lt;/li&gt;&lt;li style="font-family: &amp;quot;Helvetica Neue&amp;quot;,Arial,Helvetica,sans-serif;"&gt;If that character does not exist, return false&lt;/li&gt;&lt;li style="font-family: &amp;quot;Helvetica Neue&amp;quot;,Arial,Helvetica,sans-serif;"&gt;If that character exist, repeat step 1.&lt;/li&gt;&lt;li style="font-family: &amp;quot;Helvetica Neue&amp;quot;,Arial,Helvetica,sans-serif;"&gt;Do the above steps until the end of string is reached.&amp;nbsp;&lt;/li&gt;&lt;li&gt;&lt;span style="font-family: &amp;quot;Helvetica Neue&amp;quot;,Arial,Helvetica,sans-serif;"&gt;When end of string is reached and if the marker of the current Node is set to true, return true, else return false.&lt;/span&gt;&lt;/li&gt;&lt;/ol&gt;Using the above algorithm, lets perform a search for the key "do".&lt;br /&gt;&lt;ol&gt;&lt;li style="font-family: &amp;quot;Helvetica Neue&amp;quot;,Arial,Helvetica,sans-serif;"&gt;See whether "d" is present in the current node's children. Yes its present, so set the current node to child node which is having character "d".&lt;/li&gt;&lt;li style="font-family: &amp;quot;Helvetica Neue&amp;quot;,Arial,Helvetica,sans-serif;"&gt;See whether "o" is present in the current node's children. Yes its present, so set the current node to child node which is having character "o".&lt;/li&gt;&lt;li&gt;&lt;span style="font-family: &amp;quot;Helvetica Neue&amp;quot;,Arial,Helvetica,sans-serif;"&gt;Since "o" is the end of the word, see whether marker is set to true or false. Marker is set to false which means that "do" is not registered as a word in the TRIE. So, return false.&lt;/span&gt;&lt;/li&gt;&lt;/ol&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/_kRev2kjeMro/S8BL5KHN5sI/AAAAAAAACKw/B0TcTLeDpyA/s1600/Search+for+do.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/_kRev2kjeMro/S8BL5KHN5sI/AAAAAAAACKw/B0TcTLeDpyA/s640/Search+for+do.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;Using the same algorithm, lets perform a search for the key "ball"&lt;br /&gt;&lt;ol style="font-family: &amp;quot;Helvetica Neue&amp;quot;,Arial,Helvetica,sans-serif;"&gt;&lt;li&gt;See whether "b" is present in the current node's children. Yes its present, so set the current node to child node which is having character "b".&lt;/li&gt;&lt;li&gt;See whether "a" is present in the current node's children. Yes its present, so set the current node to child node which is having character "a".&lt;/li&gt;&lt;li&gt;See whether "l" is present in the current node's children. Yes its present, so set the current node to child node which is having character "l".&lt;/li&gt;&lt;li&gt;See whether "l" is present in the current node's children. Yes its present, so set the current node to child node which is having character "l".&lt;/li&gt;&lt;li&gt;Since "l" is the end of the word, see whether marker is set to true or false. Marker is set to true which means that "ball" is registered as a word in the TRIE. So, return true&lt;/li&gt;&lt;/ol&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/_kRev2kjeMro/S8BOM4vHbPI/AAAAAAAACLA/2dnGg-NzalQ/s1600/Search+for+ball+in+a+TRIE+data+structure.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://1.bp.blogspot.com/_kRev2kjeMro/S8BOM4vHbPI/AAAAAAAACLA/2dnGg-NzalQ/s640/Search+for+ball+in+a+TRIE+data+structure.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: java"&gt;public boolean search(String s){&lt;br /&gt;  Node current = root;&lt;br /&gt;  while(current != null){&lt;br /&gt;   for(int i=0;i&amp;lt;s.length();i++){    &lt;br /&gt;    if(current.subNode(s.charAt(i)) == null)&lt;br /&gt;     return false;&lt;br /&gt;    else&lt;br /&gt;     current = current.subNode(s.charAt(i));&lt;br /&gt;   }&lt;br /&gt;   /* &lt;br /&gt;    * This means that a string exists, but make sure its&lt;br /&gt;    * a word by checking its 'marker' flag&lt;br /&gt;    */&lt;br /&gt;   if (current.marker == true)&lt;br /&gt;    return true;&lt;br /&gt;   else&lt;br /&gt;    return false;&lt;br /&gt;  }&lt;br /&gt;  return false; &lt;br /&gt; }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The above java code does the search operation in the TRIE data structure. We shall look more into the running time and efficiency of TRIE in the next part.&lt;br /&gt;&lt;br /&gt;Cheers,&lt;br /&gt;Bragaadeesh.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-452978433787200727?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/452978433787200727/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=452978433787200727' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/452978433787200727'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/452978433787200727'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/04/trie-data-structure-part-4-search.html' title='TRIE data structure Part 4 : The SEARCH Algorithm'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_kRev2kjeMro/S8BJJ98aVJI/AAAAAAAACKo/JFKO5IdXNGY/s72-c/TRIE+ADT.png' height='72' width='72'/><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-3147970333019564010</id><published>2010-04-02T14:36:00.000-07:00</published><updated>2011-03-22T23:31:28.157-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='data structures'/><category scheme='http://www.blogger.com/atom/ns#' term='words'/><category scheme='http://www.blogger.com/atom/ns#' term='interview'/><category scheme='http://www.blogger.com/atom/ns#' term='tutorial'/><category scheme='http://www.blogger.com/atom/ns#' term='ADT'/><category scheme='http://www.blogger.com/atom/ns#' term='TRIE'/><title type='text'>TRIE data structure Part 3 : The INSERT Algorithm</title><content type='html'>&lt;div style="margin: auto 33%;"&gt;&lt;ul id="pagination-digg"&gt;&lt;li class="previous"&gt;&lt;a href="http://www.technicalypto.com/2010/04/trie-data-structure-part-2-node-and.html"&gt;«Previous&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2010/04/trie-in-java.html"&gt;1&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2010/04/trie-data-structure-part-2-node-and.html"&gt;2&lt;/a&gt;&lt;/li&gt;&lt;li class="active"&gt;3&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2010/04/trie-data-structure-part-4-search.html"&gt;4&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2010/04/trie-data-structure-part-5-complexity.html"&gt;5&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2010/04/trie-data-structure-part-5-sample-ui.html"&gt;6&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2010/09/trie-data-structure-in-c.html"&gt;7&lt;/a&gt;&lt;/li&gt;&lt;li class="next"&gt;&lt;a href="http://www.technicalypto.com/2010/04/trie-data-structure-part-4-search.html"&gt;Next »&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;In this section we shall see how the insert() method on the TRIE data structure works. We shall take a specific case and analyze it with pictorial representation.&lt;br /&gt;&lt;br /&gt;Before we begin, assume we already have an existing TRIE as shown below.&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/_kRev2kjeMro/S7ZYyGN-7aI/AAAAAAAACKQ/ZKxwGWS8JAs/s1600/TRIE+ADT.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/_kRev2kjeMro/S7ZYyGN-7aI/AAAAAAAACKQ/ZKxwGWS8JAs/s640/TRIE+ADT.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;Lets see the steps on how to insert a word "bate". Any insertion would ideally be following the below algorithm.&lt;br /&gt;&lt;br /&gt;&lt;ol style="font-family: &amp;quot;Trebuchet MS&amp;quot;,sans-serif;"&gt;&lt;li&gt;If the input string length is zero, then set the marker for the root node to be true.&lt;/li&gt;&lt;li&gt;If the input string length is greater than zero, repeat steps 3 and 4 for each character&lt;/li&gt;&lt;li&gt;If the character is present in the child node of the current node, set the current node point to the child node.&lt;/li&gt;&lt;li&gt;If the character is not present in the child node, then insert a new node and set the current node to that newly inserted node.&lt;/li&gt;&lt;li&gt;Set the marker flag to true when the end character is reached.&lt;/li&gt;&lt;/ol&gt;Now if you go through the already written code for this, you can have a better understanding by comparing it with the above algorithm.&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: java"&gt;public void insert(String s){&lt;br /&gt;  Node current = root; &lt;br /&gt;  if(s.length()==0) //For an empty character&lt;br /&gt;   current.marker=true;&lt;br /&gt;  for(int i=0;i&amp;lt;s.length();i++){&lt;br /&gt;   Node child = current.subNode(s.charAt(i));&lt;br /&gt;   if(child!=null){ &lt;br /&gt;    current = child;&lt;br /&gt;   }&lt;br /&gt;   else{&lt;br /&gt;    current.child.add(new Node(s.charAt(i)));&lt;br /&gt;    current = current.subNode(s.charAt(i));&lt;br /&gt;   }&lt;br /&gt;   // Set marker to indicate end of the word&lt;br /&gt;   if(i==s.length()-1)&lt;br /&gt;    current.marker = true;&lt;br /&gt;  } &lt;br /&gt; }&amp;nbsp;&lt;/pre&gt;&lt;br /&gt;Now lets see how the word "bate" is getting inserted. Since the word "bate" is having length greater than zero, we can start inspecting each word.&lt;br /&gt;&lt;ul&gt;&lt;li style="font-family: &amp;quot;Trebuchet MS&amp;quot;,sans-serif;"&gt;See whether "b" is present in the current node's children (which is root). Yes its present, so set the current node to the child node which is having the character "b".&lt;/li&gt;&lt;li style="font-family: &amp;quot;Trebuchet MS&amp;quot;,sans-serif;"&gt;See whether "a" is present in the current node's children. Yes its present, so set the current node to the child node which is having the character "a".&lt;/li&gt;&lt;li style="font-family: &amp;quot;Trebuchet MS&amp;quot;,sans-serif;"&gt;See whether "t" is present in the current node's children. Yes its present, so set the current node to the child node which is having the character "t".&lt;/li&gt;&lt;li&gt;&lt;span style="font-family: &amp;quot;Trebuchet MS&amp;quot;,sans-serif;"&gt;See whether "e" is present in the current node's children. No, its not present, so create a new node with character set to "e". Since "e" is the end of the word, set the marker flag to true.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/_kRev2kjeMro/S7Zi0JH-mKI/AAAAAAAACKg/M_JIUFoXEyU/s1600/TRIE+ADT+INSERT.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/_kRev2kjeMro/S7Zi0JH-mKI/AAAAAAAACKg/M_JIUFoXEyU/s640/TRIE+ADT+INSERT.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;The above picture shows how the word "bate" is inserted into the existing TRIE data structure. This example clearly shows how the insertion in a TRIE happens.&lt;br /&gt;We shall take a look on the Search operation in the &lt;a href="http://www.technicalypto.com/2010/04/trie-data-structure-part-4-search.html"&gt;next part&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;Cheers,&lt;br /&gt;Bragaadeesh.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-3147970333019564010?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/3147970333019564010/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=3147970333019564010' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/3147970333019564010'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/3147970333019564010'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/04/trie-data-structure-part-3-inserting.html' title='TRIE data structure Part 3 : The INSERT Algorithm'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/_kRev2kjeMro/S7ZYyGN-7aI/AAAAAAAACKQ/ZKxwGWS8JAs/s72-c/TRIE+ADT.png' height='72' width='72'/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-5156955383882717618</id><published>2010-04-02T13:37:00.000-07:00</published><updated>2011-03-22T23:23:12.097-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='data structures'/><category scheme='http://www.blogger.com/atom/ns#' term='words'/><category scheme='http://www.blogger.com/atom/ns#' term='interview'/><category scheme='http://www.blogger.com/atom/ns#' term='tutorial'/><category scheme='http://www.blogger.com/atom/ns#' term='TRIE'/><title type='text'>TRIE data structure Part 2 : Node and TRIE class in Java</title><content type='html'>&lt;div style="margin: auto 33%;"&gt;&lt;ul id="pagination-digg"&gt;&lt;li class="previous"&gt;&lt;a href="http://www.technicalypto.com/2010/04/trie-in-java.html"&gt;«Previous&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2010/04/trie-in-java.html"&gt;1&lt;/a&gt;&lt;/li&gt;&lt;li class="active"&gt;2&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2010/04/trie-data-structure-part-3-inserting.html"&gt;3&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2010/04/trie-data-structure-part-4-search.html"&gt;4&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2010/04/trie-data-structure-part-5-complexity.html"&gt;5&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2010/04/trie-data-structure-part-5-sample-ui.html"&gt;6&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2010/09/trie-data-structure-in-c.html"&gt;7&lt;/a&gt;&lt;/li&gt;&lt;li class="next"&gt;&lt;a href="http://www.technicalypto.com/2010/04/trie-data-structure-part-3-inserting.html"&gt;Next »&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;In the &lt;a href="http://www.technicalypto.com/2010/04/trie-in-java.html"&gt;first part&lt;/a&gt; of the TRIE ADT, we saw the basics of the TRIE data structure. In this section, lets get our hands dirty by directly looking at the TRIE data structure implemented in Java.&lt;br /&gt;&lt;br /&gt;We already saw the Node structure of the TRIE ADT had a content (char), a marker (boolean) and collection of child nodes (Collection of Node). It now has one more method called as subNode(char). This method takes a character as argument would return the child node of that character type should that be present.&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: java"&gt;package dsa.tries;&lt;br /&gt;&lt;br /&gt;import java.util.Collection;&lt;br /&gt;import java.util.LinkedList;&lt;br /&gt;&lt;br /&gt;/**&lt;br /&gt; * @author Braga&lt;br /&gt; */&lt;br /&gt;public class Node {&lt;br /&gt; char content;&lt;br /&gt; boolean marker; &lt;br /&gt; Collection&amp;lt;Node&amp;gt; child;&lt;br /&gt; &lt;br /&gt; public Node(char c){&lt;br /&gt;  child = new LinkedList&amp;lt;Node&amp;gt;();&lt;br /&gt;  marker = false;&lt;br /&gt;  content = c;&lt;br /&gt; }&lt;br /&gt; &lt;br /&gt; public Node subNode(char c){&lt;br /&gt;  if(child!=null){&lt;br /&gt;   for(Node eachChild:child){&lt;br /&gt;    if(eachChild.content == c){&lt;br /&gt;     return eachChild;&lt;br /&gt;    }&lt;br /&gt;   }&lt;br /&gt;  }&lt;br /&gt;  return null;&lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Now that we've defined our Node, lets go ahead and look at the code for the TRIE class. Fortunately, the TRIE datastructure is insanely simple to implement since it has two major methods insert() and search(). Lets look at the elementary implementation of both these methods. &lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: java"&gt;package dsa.tries;&lt;br /&gt;&lt;br /&gt;public class Trie{&lt;br /&gt; private Node root;&lt;br /&gt;&lt;br /&gt; public Trie(){&lt;br /&gt;  root = new Node(' '); &lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; public void insert(String s){&lt;br /&gt;  Node current = root; &lt;br /&gt;  if(s.length()==0) //For an empty character&lt;br /&gt;   current.marker=true;&lt;br /&gt;  for(int i=0;i&amp;lt;s.length();i++){&lt;br /&gt;   Node child = current.subNode(s.charAt(i));&lt;br /&gt;   if(child!=null){ &lt;br /&gt;    current = child;&lt;br /&gt;   }&lt;br /&gt;   else{&lt;br /&gt;    current.child.add(new Node(s.charAt(i)));&lt;br /&gt;    current = current.subNode(s.charAt(i));&lt;br /&gt;   }&lt;br /&gt;   // Set marker to indicate end of the word&lt;br /&gt;   if(i==s.length()-1)&lt;br /&gt;    current.marker = true;&lt;br /&gt;  } &lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; public boolean search(String s){&lt;br /&gt;  Node current = root;&lt;br /&gt;  while(current != null){&lt;br /&gt;   for(int i=0;i&amp;lt;s.length();i++){    &lt;br /&gt;    if(current.subNode(s.charAt(i)) == null)&lt;br /&gt;     return false;&lt;br /&gt;    else&lt;br /&gt;     current = current.subNode(s.charAt(i));&lt;br /&gt;   }&lt;br /&gt;   /* &lt;br /&gt;    * This means that a string exists, but make sure its&lt;br /&gt;    * a word by checking its 'marker' flag&lt;br /&gt;    */&lt;br /&gt;   if (current.marker == true)&lt;br /&gt;    return true;&lt;br /&gt;   else&lt;br /&gt;    return false;&lt;br /&gt;  }&lt;br /&gt;  return false; &lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;We shall look into the detailed working of the insert() and search() methods in separate posts, but I will tell you the gist of both those methods here. The insert() methods adds a new words to the already existing TRIE data structure. The search() method would return true or false based on whether the search string we specified exist or not.&lt;br /&gt;&lt;br /&gt;Take a quick look at the java code above because we are going to use this in the posts that follow.&lt;br /&gt;&lt;br /&gt;Cheers,&lt;br /&gt;Bragaadeesh.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-5156955383882717618?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/5156955383882717618/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=5156955383882717618' title='8 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/5156955383882717618'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/5156955383882717618'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/04/trie-data-structure-part-2-node-and.html' title='TRIE data structure Part 2 : Node and TRIE class in Java'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>8</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-1437867724396011789</id><published>2010-04-02T06:09:00.000-07:00</published><updated>2011-03-13T00:48:14.913-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='data structures'/><category scheme='http://www.blogger.com/atom/ns#' term='words'/><category scheme='http://www.blogger.com/atom/ns#' term='interview'/><category scheme='http://www.blogger.com/atom/ns#' term='tutorial'/><category scheme='http://www.blogger.com/atom/ns#' term='ADT'/><category scheme='http://www.blogger.com/atom/ns#' term='TRIE'/><title type='text'>TRIE data structure Part 1: The TRIE ADT in Java</title><content type='html'>&lt;div style="margin: auto 33%;"&gt;&lt;ul id="pagination-digg"&gt;&lt;li class="previous-off"&gt;«Previous&lt;/li&gt;&lt;li class="active"&gt;1&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2010/04/trie-data-structure-part-2-node-and.html"&gt;2&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2010/04/trie-data-structure-part-3-inserting.html"&gt;3&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2010/04/trie-data-structure-part-4-search.html"&gt;4&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2010/04/trie-data-structure-part-5-complexity.html"&gt;5&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2010/04/trie-data-structure-part-5-sample-ui.html"&gt;6&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.technicalypto.com/2010/09/trie-data-structure-in-c.html"&gt;7&lt;/a&gt;&lt;/li&gt;&lt;li class="next"&gt;&lt;a href="http://www.technicalypto.com/2010/04/trie-data-structure-part-2-node-and.html"&gt;Next »&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;TRIE is an interesting data-structure used mainly for manipulating with Words in a language. This word is got from the word re&lt;span style="color: red;"&gt;trie&lt;/span&gt;ve. TRIE (pronounced as 'try') has a wide variety of applications in&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Spell checking&lt;/li&gt;&lt;li&gt;Data compression&lt;/li&gt;&lt;li&gt;Computational biology&lt;/li&gt;&lt;li&gt;Routing table for IP addresses&lt;/li&gt;&lt;li&gt;Storing/Querying XML documents etc.,&lt;/li&gt;&lt;/ul&gt;We shall see how to construct a basic TRIE data structure in Java.&lt;br /&gt;&lt;br /&gt;The main abstract methods of the TRIE ADT are,&lt;br /&gt;&lt;pre class="brush: java"&gt;public void insert(String s);&lt;br /&gt;public boolean search(String s);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;In this data-structure, each node holds a character instead of a String. Each node has something called as 'marker' to mark the end of a word. And each node has a &lt;span style="background-color: #cccccc;"&gt;Collection&lt;/span&gt; of child nodes. This &lt;span style="background-color: #cccccc;"&gt;Collection&lt;/span&gt; can be either a Set or a List based on the speed vs space criterion.&lt;br /&gt;&lt;br /&gt;The basic element - Node of a TRIE data structure looks like this,&lt;br /&gt;&lt;pre class="brush: java"&gt;char content;&lt;br /&gt;boolean marker;&lt;br /&gt;Collection&amp;lt;Node&amp;gt; child;&lt;/pre&gt;&lt;br /&gt;A TRIE tree would typically look like the following&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/_kRev2kjeMro/S7Xtql_DlfI/AAAAAAAACKI/8pQFTiifsk0/s1600/TRIE+ADT.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://1.bp.blogspot.com/_kRev2kjeMro/S7Xtql_DlfI/AAAAAAAACKI/8pQFTiifsk0/s640/TRIE+ADT.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;br /&gt;The above TRIE is constructed by inserting the words ball, bat, doll, dork, do, dorm, send, sense. The markers are denoted on the Node using a red star(&lt;span style="color: red;"&gt;*&lt;/span&gt;). We shall look into more about the TRIE data structure in depth in the &lt;a href="http://www.technicalypto.com/2010/04/trie-data-structure-part-2-node-and.html"&gt;next post&lt;/a&gt;.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-1437867724396011789?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/1437867724396011789/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=1437867724396011789' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/1437867724396011789'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/1437867724396011789'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/04/trie-in-java.html' title='TRIE data structure Part 1: The TRIE ADT in Java'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_kRev2kjeMro/S7Xtql_DlfI/AAAAAAAACKI/8pQFTiifsk0/s72-c/TRIE+ADT.png' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-5866626616458290996</id><published>2010-03-06T02:33:00.000-08:00</published><updated>2010-03-06T02:33:52.091-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='reverse'/><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='data structures'/><category scheme='http://www.blogger.com/atom/ns#' term='singly linked list'/><category scheme='http://www.blogger.com/atom/ns#' term='linked list'/><category scheme='http://www.blogger.com/atom/ns#' term='recursively'/><title type='text'>Reverse a Singly Linked List Recursively in Java</title><content type='html'>We have already seen how to &lt;a href="http://www.technicalypto.com/2010/01/java-program-to-reverse-singly-linked.html"&gt;reverse a singly linked list&lt;/a&gt; with illustrative pictures. Now lets see how we can do it recursively. In the previous problem we did it iteratively, now we shall do it recursively.&lt;br /&gt;To attack any problem in a recursive approach, we need to be very clear about the end/boundary conditions. For a linked list, reverse of a null list or reverse of list of size 1 is going to be the same.&lt;br /&gt;Reverse of a linked list of size x will be the reverse of the 'next' element followed by first.&lt;br /&gt;A picture means a thousand words. So, here is what happens internally.&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/_kRev2kjeMro/S5IuvYuEf3I/AAAAAAAACI4/4ZRZgkCYYmY/s1600-h/Reverse+a+Linked+List+Recursively+in+Java.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/_kRev2kjeMro/S5IuvYuEf3I/AAAAAAAACI4/4ZRZgkCYYmY/s640/Reverse+a+Linked+List+Recursively+in+Java.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;Now for the comprehensive Java code (reference for SinglyLinkedList implementation can be found &lt;a href="http://www.technicalypto.com/2010/01/linked-lists.html"&gt;here&lt;/a&gt;)&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: java"&gt;package dsa.linkedlist;&lt;br /&gt;&lt;br /&gt;public class ReverseLinkedListRecursively {&lt;br /&gt; &lt;br /&gt; public static void main(String args[]){&lt;br /&gt;  ReverseLinkedListRecursively reverser = new ReverseLinkedListRecursively();&lt;br /&gt;  SinglyLinkedList&amp;lt;Integer&amp;gt; originalList = reverser.getLabRatList(10);&lt;br /&gt;  System.out.println("Original List : "+originalList.toString());&lt;br /&gt;  originalList.start = reverser.reverse(originalList.start);&lt;br /&gt;  System.out.println("Reversed List : "+originalList.toString());&lt;br /&gt; }&lt;br /&gt; &lt;br /&gt; public Node&amp;lt;Integer&amp;gt; reverse(Node&amp;lt;Integer&amp;gt; list)&lt;br /&gt; {&lt;br /&gt;     if (list == null || list.next==null) return list;&lt;br /&gt;     Node&amp;lt;Integer&amp;gt; nextItem = list.next;&lt;br /&gt;     list.next = null;&lt;br /&gt;     Node&amp;lt;Integer&amp;gt; reverseRest = reverse(nextItem);&lt;br /&gt;     nextItem.next = list;&lt;br /&gt;     return reverseRest;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; &lt;br /&gt; private SinglyLinkedList&amp;lt;Integer&amp;gt; getLabRatList(int count){&lt;br /&gt;  SinglyLinkedList&amp;lt;Integer&amp;gt; sampleList = new SinglyLinkedList&amp;lt;Integer&amp;gt;();&lt;br /&gt;  for(int i=0;i&amp;lt;count;i++){&lt;br /&gt;   sampleList.add(i);&lt;br /&gt;  }&lt;br /&gt;  return sampleList;&lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;/*&lt;br /&gt; * SAMPLE OUTPUT&lt;br /&gt; * Original List : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9&lt;br /&gt; * Reversed List : 9, 8, 7, 6, 5, 4, 3, 2, 1, 0&lt;br /&gt; */&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Cheers,&lt;br /&gt;Bragaadeesh.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-5866626616458290996?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/5866626616458290996/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=5866626616458290996' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/5866626616458290996'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/5866626616458290996'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/03/reverse-singly-linked-list-recursively.html' title='Reverse a Singly Linked List Recursively in Java'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_kRev2kjeMro/S5IuvYuEf3I/AAAAAAAACI4/4ZRZgkCYYmY/s72-c/Reverse+a+Linked+List+Recursively+in+Java.png' height='72' width='72'/><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-7341302503080537677</id><published>2010-02-22T03:47:00.000-08:00</published><updated>2010-03-10T09:54:33.831-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='program'/><category scheme='http://www.blogger.com/atom/ns#' term='palindrome'/><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='string manipulation'/><category scheme='http://www.blogger.com/atom/ns#' term='string'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><title type='text'>Find all possible palindromes in a String in Java C++</title><content type='html'>This is not the regular palindrome finder. Given a string, we should be able to print all the possible palindromes. Let me quickly give a brief example.&lt;br /&gt;For the following text : &lt;b&gt;abccbab&lt;/b&gt;&lt;br /&gt;All the possible palindromes are,&lt;br /&gt;&lt;b&gt;bab&lt;/b&gt; &lt;br /&gt;&lt;b&gt;abccba&lt;/b&gt;&lt;br /&gt;&lt;b&gt;bccb&lt;/b&gt;&lt;br /&gt;&lt;b&gt;cc&lt;/b&gt;&lt;br /&gt;If you could see there are two possible outcomes in palindrome, one is odd and other is even. My initial solution was very worse. What I actually did was did a permutation/combination of all the possible texts and send it to a palindrome() method. It will run in the worst possible time.&lt;br /&gt;However, there is even a simpler solution available. First do a parse for odd occurring palindromes followed by even palindromes.&lt;br /&gt;For odd palindromes run through each character from the text. For each character, see if there the pre and post occuring characters are equal, if they are equal print them and do the same for the next levels. In the following example shown below, assume you are at 'y' and see the previous and next characters are equal. If they are see further more until the palindrome functionality ceases. Print all of them whilst this time.&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/_kRev2kjeMro/S4JtnfWkRYI/AAAAAAAACGM/6jS_3xz7UIc/s1600-h/Palindrome.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/_kRev2kjeMro/S4JtnfWkRYI/AAAAAAAACGM/6jS_3xz7UIc/s320/Palindrome.png" /&gt;&lt;/a&gt;&amp;nbsp;&lt;/div&gt;&lt;br /&gt;Thats it. Do the same for all the characters. Since there is no meaning in doing this for first and last characters, we can very well omit them.&lt;br /&gt;Similar logic holds for even sized palindromes. Here we wont be holding the center character. Rest of the logic remains the same. The java code follows,&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: java"&gt;package dsa.stringmanipulation;&lt;br /&gt;&lt;br /&gt;/**&lt;br /&gt; * Program to print all palindromes in a string&lt;br /&gt; * @author Bragaadeesh&lt;br /&gt; */&lt;br /&gt;public class FindAllPalindromes {&lt;br /&gt; public static void main(String[] args){&lt;br /&gt;  FindAllPalindromes finder = new FindAllPalindromes();&lt;br /&gt;  finder.printAllPalindromes("abcddcbaABCDEDCBA");&lt;br /&gt; }&lt;br /&gt; &lt;br /&gt; public void printAllPalindromes(String inputText){&lt;br /&gt;  if(inputText==null){&lt;br /&gt;   System.out.println("Input cannot be null!");&lt;br /&gt;   return;&lt;br /&gt;  }&lt;br /&gt;  if(inputText.length()&amp;lt;=2){&lt;br /&gt;   System.out.println("Minimum three characters should be present");&lt;br /&gt;  }&lt;br /&gt;  //ODD Occuring Palindromes&lt;br /&gt;  int len = inputText.length();&lt;br /&gt;  for(int i=1;i&amp;lt;len-1;i++){&lt;br /&gt;   for(int j=i-1,k=i+1;j&amp;gt;=0&amp;amp;&amp;amp;k&amp;lt;len;j--,k++){&lt;br /&gt;    if(inputText.charAt(j) == inputText.charAt(k)){&lt;br /&gt;     System.out.println(inputText.subSequence(j,k+1));&lt;br /&gt;    }else{&lt;br /&gt;     break;&lt;br /&gt;    }&lt;br /&gt;   }&lt;br /&gt;  }&lt;br /&gt;  //EVEN Occuring Palindromes&lt;br /&gt;  for(int i=1;i&amp;lt;len-1;i++){&lt;br /&gt;   for(int j=i,k=i+1;j&amp;gt;=0&amp;amp;&amp;amp;k&amp;lt;len;j--,k++){&lt;br /&gt;    if(inputText.charAt(j) == inputText.charAt(k)){&lt;br /&gt;     System.out.println(inputText.subSequence(j,k+1));&lt;br /&gt;    }else{&lt;br /&gt;     break;&lt;br /&gt;    }&lt;br /&gt;   }&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;/*&lt;br /&gt;Sample Output:&lt;br /&gt;DED&lt;br /&gt;CDEDC&lt;br /&gt;BCDEDCB&lt;br /&gt;ABCDEDCBA&lt;br /&gt;dd&lt;br /&gt;cddc&lt;br /&gt;bcddcb&lt;br /&gt;abcddcba&lt;br /&gt;*/&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The code in C++&lt;br /&gt;&lt;pre class="brush: cpp"&gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;using namespace std;&lt;br /&gt;&lt;br /&gt;void printAllPalindromes(char*);&lt;br /&gt;&lt;br /&gt;char* subSequence(char*,int,int);&lt;br /&gt;&lt;br /&gt;int main() {&lt;br /&gt; char *s = &amp;quot;abcddcbaABCDEDCBA&amp;quot;;&lt;br /&gt; printAllPalindromes(s);&lt;br /&gt; return 0;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;char* subSequence(char* mainSequence, int from, int to){&lt;br /&gt; char * tgt = new char[to-from+1];&lt;br /&gt; for(int i=0;i&amp;lt;(to-from);i++){&lt;br /&gt;  tgt[i] = mainSequence[i+from];&lt;br /&gt; }&lt;br /&gt; tgt[to-from] = '\0';&lt;br /&gt; return tgt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void printAllPalindromes(char* inputText) {&lt;br /&gt; if(!inputText) {&lt;br /&gt;  printf(&amp;quot;Input cannot be null!&amp;quot;);&lt;br /&gt;  return;&lt;br /&gt; }&lt;br /&gt; if(strlen(inputText)&amp;lt;=2) {&lt;br /&gt;  printf(&amp;quot;Minimum three characters should be present\n&amp;quot;);&lt;br /&gt; }&lt;br /&gt; //ODD Occuring Palindromes&lt;br /&gt; int len = strlen(inputText);&lt;br /&gt; for(int i=1;i&amp;lt;len-1;i++) {&lt;br /&gt;  for(int j=i-1,k=i+1;j&amp;gt;=0&amp;amp;&amp;amp;k&amp;lt;len;j--,k++) {&lt;br /&gt;   if(inputText[j] == inputText[k]) {&lt;br /&gt;    char* subSeq = subSequence(inputText,j,k+1);&lt;br /&gt;    cout&amp;lt;&amp;lt;subSeq&amp;lt;&amp;lt;endl;&lt;br /&gt;    delete subSeq;&lt;br /&gt;   } else {&lt;br /&gt;    break;&lt;br /&gt;   }&lt;br /&gt;  }&lt;br /&gt; }&lt;br /&gt; //EVEN Occuring Palindromes&lt;br /&gt; for(int i=1;i&amp;lt;len-1;i++) {&lt;br /&gt;  for(int j=i,k=i+1;j&amp;gt;=0&amp;amp;&amp;amp;k&amp;lt;len;j--,k++) {&lt;br /&gt;   if(inputText[j] == inputText[k]) {&lt;br /&gt;    char* subSeq = subSequence(inputText,j,k+1);&lt;br /&gt;    cout&amp;lt;&amp;lt;subSeq&amp;lt;&amp;lt;endl;&lt;br /&gt;    delete subSeq;&lt;br /&gt;   } else {&lt;br /&gt;    break;&lt;br /&gt;   }&lt;br /&gt;  }&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;/*&lt;br /&gt; Sample Output:&lt;br /&gt; DED&lt;br /&gt; CDEDC&lt;br /&gt; BCDEDCB&lt;br /&gt; ABCDEDCBA&lt;br /&gt; dd&lt;br /&gt; cddc&lt;br /&gt; bcddcb&lt;br /&gt; abcddcba&lt;br /&gt; */&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Cheers,&lt;br /&gt;Bragaadeesh.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-7341302503080537677?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/7341302503080537677/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=7341302503080537677' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/7341302503080537677'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/7341302503080537677'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/02/find-all-possible-palindromes-in-string.html' title='Find all possible palindromes in a String in Java C++'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_kRev2kjeMro/S4JtnfWkRYI/AAAAAAAACGM/6jS_3xz7UIc/s72-c/Palindrome.png' height='72' width='72'/><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-4666997112746459558</id><published>2010-02-21T02:03:00.000-08:00</published><updated>2011-03-01T03:59:11.137-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='program'/><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='data structures'/><category scheme='http://www.blogger.com/atom/ns#' term='singly linked list'/><category scheme='http://www.blogger.com/atom/ns#' term='stack'/><title type='text'>Stack using Linked Lists in Java</title><content type='html'>Stack is a data structure that follows the simple FILO (First In, Last out) or LIFO (Last In, First Out) rule. Imagine a real world stack where you arrange Notebooks one over the other. The first notebook you insert will be at the bottom and that will come only at last. The implementation of stack can be done in many ways. We are going to see how to make use of a Singly Linked List to the use.&lt;br /&gt;We can implement stack using a Linked List in the below shown ways. One is to have the END node on top and other is to have the START node at the top. If we recollect the singly linked list data structure, insertAtFirst() is an operation which can be done in O(1) time and insertAtLast() will take O(n) time (because we need to traverse till the last node). So, we can make use of the second method to use stack using linkedlists.&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/_kRev2kjeMro/S4EEc6Y8D7I/AAAAAAAACGE/lRSDE0Y369Q/s1600-h/StackUsingLinkedLists.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/_kRev2kjeMro/S4EEc6Y8D7I/AAAAAAAACGE/lRSDE0Y369Q/s320/StackUsingLinkedLists.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;The three methods that stands out for a stack are &lt;span style="background-color: #999999;"&gt;pop()&lt;/span&gt;, &lt;span style="background-color: #999999;"&gt;push()&lt;/span&gt; and &lt;span style="background-color: #999999; color: black;"&gt;peek()&lt;/span&gt;.&lt;br /&gt;&lt;span style="background-color: #999999;"&gt;push()&lt;/span&gt; - push elements into a stack. We will use the &lt;span style="background-color: #999999;"&gt;insertAtFirst()&lt;/span&gt; method of LinkedList. Throws StackOverflowException when the stack is full.&lt;br /&gt;&lt;span style="background-color: #999999;"&gt;pop()&lt;/span&gt; - remove and returns the top element from a stack. We will use the &lt;span style="background-color: #999999;"&gt;removeAtFirst()&lt;/span&gt; method of LinkedList. Throws StackEmptyException when the stack is empty.&lt;br /&gt;&lt;span style="background-color: #999999;"&gt;peek()&lt;/span&gt; - return the top element from the stack without removing it. We will use the &lt;span style="background-color: #999999;"&gt;getFirst()&lt;/span&gt; method of LinkedList. Throws StackEmptyException when the stack is empty.&lt;br /&gt;&lt;br /&gt;The java code for this looks very simpler. We will make use of the existing SinglyLinkedList class that we have used before.&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: java"&gt;package dsa.stack;&lt;br /&gt;&lt;br /&gt;import dsa.linkedlist.SinglyLinkedList;&lt;br /&gt;&lt;br /&gt;public class Stack&amp;lt;E&amp;gt; extends SinglyLinkedList&amp;lt;E&amp;gt;{&lt;br /&gt; &lt;br /&gt; public static final int MAX_STACK_SIZE = 100;&lt;br /&gt; &lt;br /&gt; public E pop() throws StackEmptyException{&lt;br /&gt;  if(this.size()==0){&lt;br /&gt;   throw new StackEmptyException();&lt;br /&gt;  }&lt;br /&gt;  return this.removeAtFirst();&lt;br /&gt; }&lt;br /&gt; &lt;br /&gt; public E peek() throws StackEmptyException{&lt;br /&gt;  if(this.size()==0){&lt;br /&gt;   throw new StackEmptyException();&lt;br /&gt;  }&lt;br /&gt;  return this.getFirst().data;&lt;br /&gt; }&lt;br /&gt; &lt;br /&gt; public void push(E data) throws StackOverflowException{&lt;br /&gt;  if(this.size()&amp;gt;MAX_STACK_SIZE){&lt;br /&gt;   throw new StackOverflowException();&lt;br /&gt;  }&lt;br /&gt;  this.insertAtFirst(data);&lt;br /&gt; }&lt;br /&gt; &lt;br /&gt; public static void main(String args[]){&lt;br /&gt;  Stack&amp;lt;Integer&amp;gt; stack = new Stack&amp;lt;Integer&amp;gt;();&lt;br /&gt;  try{&lt;br /&gt;   System.out.println("Pushing 1, 2, 3, 4, 5");&lt;br /&gt;   stack.push(1);&lt;br /&gt;   stack.push(2);&lt;br /&gt;   stack.push(3);&lt;br /&gt;   stack.push(4);&lt;br /&gt;   stack.push(5);&lt;br /&gt;   System.out.println("Pop once  : "+stack.pop());&lt;br /&gt;   System.out.println("Peek once : "+stack.peek());&lt;br /&gt;   System.out.println("Pop once  : "+stack.pop());&lt;br /&gt;   System.out.println("Pop once  : "+stack.pop());&lt;br /&gt;   System.out.println("Pop once  : "+stack.pop());&lt;br /&gt;   System.out.println("Pop once  : "+stack.pop());&lt;br /&gt;   System.out.println("Pop once  : "+stack.pop());&lt;br /&gt;  }catch(StackEmptyException e){&lt;br /&gt;   System.out.println(e.getMessage());&lt;br /&gt;  }catch(StackOverflowException e){ &lt;br /&gt;   System.out.println(e.getMessage());&lt;br /&gt;  }&lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;/*&lt;br /&gt;SAMPLE OUTPUT:&lt;br /&gt;Pushing 1, 2, 3, 4, 5&lt;br /&gt;Pop once  : 5&lt;br /&gt;Peek once : 4&lt;br /&gt;Pop once  : 4&lt;br /&gt;Pop once  : 3&lt;br /&gt;Pop once  : 2&lt;br /&gt;Pop once  : 1&lt;br /&gt;Stack is empty!&lt;br /&gt;*/&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre class="brush: java"&gt;package dsa.stack;&lt;br /&gt;&lt;br /&gt;public class StackEmptyException extends Exception{&lt;br /&gt; public StackEmptyException(){&lt;br /&gt;  super("Stack is empty!");&lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;pre class="brush: java"&gt;package dsa.stack;&lt;br /&gt;&lt;br /&gt;public class StackOverflowException extends Exception{&lt;br /&gt; public StackOverflowException(){&lt;br /&gt;  super("Stack Overflown");&lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Cheers,&lt;br /&gt;Bragaadeesh.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6294663875929591018-4666997112746459558?l=www.technicalypto.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.technicalypto.com/feeds/4666997112746459558/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6294663875929591018&amp;postID=4666997112746459558' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/4666997112746459558'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6294663875929591018/posts/default/4666997112746459558'/><link rel='alternate' type='text/html' href='http://www.technicalypto.com/2010/02/stack-using-linked-lists-in-java.html' title='Stack using Linked Lists in Java'/><author><name>b</name><uri>http://www.blogger.com/profile/01173019524783723568</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/_kRev2kjeMro/S4EEc6Y8D7I/AAAAAAAACGE/lRSDE0Y369Q/s72-c/StackUsingLinkedLists.png' height='72' width='72'/><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6294663875929591018.post-3071358421000111394</id><published>2010-02-21T01:44:00.000-08:00</published><updated>2010-02-26T09:30:41.739-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='program'/><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='data structures'/><category scheme='http://www.blogger.com/atom/ns#' term='singly linked list'/><category scheme='http://www.blogger.com/atom/ns#' term='linked list'/><category scheme='http://www.blogger.com/atom/ns#' term='find kth from last'/><title type='text'>Find kth node from the last in a singly linked list without using counter</title><content type='html'>The objective for this program is to find the kth node from the last without actually finding the size of the singly linked list.&lt;br /&gt;For this problem, we need to have two pointers, lets call them FAR and NEAR. We need to initialize them by pointing them to the start. After doing that, move the FAR pointer 'k-1' times ahead. After moving that run a loop until FAR becomes null, amidst that increment both FAR and NEAR pointers.&lt;br /&gt;The below picture shown is done for k=3. In step1, we are moving the pointer k-1=2 times. After that by moving parallely FAR and NEAR pointers, we can find the kth element from the last by getting the data from NEAR pointer.&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/_kRev2kjeMro/S4gFNGMVLuI/AAAAAAAACIw/wdjDvtE4Zu0/s1600-h/Find+kth+from+last+in+a+singly+linked+list.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="236" src="http://4.bp.blogspot.com/_kRev2kjeMro/S4gFNGMVLuI/AAAAAAAACIw/wdjDvtE4Zu0/s640/Find+kth+from+last+in+a+singly+linked+list.png" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;The Java code for this simple program is given below. To try the below program please copy this &lt;a href="http://www.technicalypto.com/2010/01/linked-lists.html"&gt;class&lt;/a&gt; as well.&lt;br /&gt;&lt;pre class="brush: java"&gt;package dsa.linkedlist;&lt;br /&gt;&lt;br /&gt;public class FindKthElementFromLast {&lt;br /&gt; public static void main(String args[]){&lt;br /&gt;  FindKthElementFromLast kthFromLastFinder = new FindKthElementFromLast();&lt;br /&gt;  SinglyLinkedList&amp;lt;Integer&amp;gt; originalList = kthFromLastFinder.getLabRatList(8);&lt;br /&gt;  System.out.println("Original List : "+originalList.toString());&lt;br /&gt;  kthFromLastFinder.findFromLast(originalList, 3);&lt;br /&gt; }&lt;br /&gt; &lt;br /&gt; private void findFromLast(SinglyLinkedList&amp;lt;Integer&amp;gt; singlyLinkedList, int k) {&lt;br /&gt;  Node far, near;&lt;br /&gt;  //initialize far and near pointers&lt;br /&gt;  far = near = singlyLinkedList.start;&lt;br /&gt;  //Move the far pointer k times from the starting position&lt;br /&gt;  System.out.print("kth node from last for k = "+k+" is ");&lt;br /&gt;  while((k--)!=0){&lt;br /&gt;   far = far.next;&lt;br /&gt;  }&lt;br /&
