<?xml version="1.0" encoding="UTF-8" standalone="no"?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:blogger="http://schemas.google.com/blogger/2008" xmlns:gd="http://schemas.google.com/g/2005" xmlns:georss="http://www.georss.org/georss" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:thr="http://purl.org/syndication/thread/1.0"><id>tag:blogger.com,1999:blog-6801225484322010450</id><updated>2024-12-19T08:52:46.892+05:30</updated><category term="Core Java"/><category term="Interview Questions"/><category term="Data Structure"/><category term="Collections"/><category term="Spring"/><category term="Threads"/><category term="String"/><category term="amazon interview questions"/><category term="Linked List"/><category term="Algorithm"/><category term="Binary Search Tree"/><category term="HashMap"/><category term="Java 7"/><category term="Java 8"/><category term="Java Interview Questions"/><category term="Stack"/><category term="ArrayList"/><category term="Clone()"/><category term="Design Pattern"/><category term="Exception"/><category term="File"/><category term="MySQL"/><category term="Queue"/><category term="Sorting"/><category term="TreeMap"/><category term="Array"/><category term="Binary Tree"/><category term="Comparator"/><category term="Dynamic Programming"/><category term="Image"/><category term="Interface"/><category term="Java 8 API"/><category term="Java Properties"/><category term="LinkedList"/><category term="List"/><category term="Spring Data Access"/><category term="Spring Security"/><category term="recursion"/><category term="Abstract Class"/><category term="Apache POI"/><category term="Callable"/><category term="Character"/><category term="Checked Exception"/><category term="Comparable"/><category term="Create Binary Tree"/><category term="DOM and SAX Parser"/><category term="Date and Time"/><category term="Dependency Injection"/><category term="Email"/><category term="Excel Sheet"/><category term="Factory Pattern"/><category term="Finally"/><category term="Hashtable"/><category term="Hibernate"/><category term="Inorder"/><category term="Inversion of Control"/><category term="Iterator"/><category term="JSP"/><category term="LinkedHashMap"/><category term="Patterns"/><category term="Postorder"/><category term="PriorityQueue"/><category term="RESTFull"/><category term="Runnable"/><category term="SOAP"/><category term="Selection sort"/><category term="Spring Boot"/><category term="Spring IOC"/><category term="StringJoiner"/><category term="Thread pool"/><category term="TimeZone"/><category term="Unchecked Exception"/><category term="Vector"/><category term="Web Services"/><category term="Wrapper Classes"/><category term="XML"/><category term="ZIP"/><category term="immutable class"/><category term="jQuery"/><category term="java.util.Date"/><category term="log4j API"/><category term="try catch"/><category term="@PropertySource"/><category term="@Value"/><category term="AES"/><category term="AES algorithm"/><category term="Add two numbers represented by linked lists using recursive"/><category term="Addition of 2 Linked List"/><category term="Advanced Encryption Standard"/><category term="Anagrams"/><category term="Array rotate"/><category term="Autoboxing"/><category term="BIOS"/><category term="BST"/><category term="BST maximum height"/><category term="Binary Search"/><category term="Binary Search Tree Height"/><category term="Binary Tree Lead node"/><category term="Binary Tree size"/><category term="Bubble sort"/><category term="Cache"/><category term="Calendar"/><category term="Captcha"/><category term="Case Insensitive HashMap"/><category term="Change  File Modified Date and Time in Java"/><category term="Change File Permission"/><category term="Character Occurrences"/><category term="Character to String"/><category term="Checking Pangram or Not"/><category term="Class.forName"/><category term="ClassNotFoundException"/><category term="Clustering"/><category term="Concatenate List"/><category term="Concrete methods"/><category term="Condition"/><category term="Constructor Injection"/><category term="Consumer Interface"/><category term="Convert Date/Time for given Timezone - java"/><category term="Convert JSON to Java Object using Google Gson library"/><category term="Count Duplicate elements in an array"/><category term="Count set bits in an integer"/><category term="Counting nodes in a tree? Yes how to get Binary Tree size"/><category term="Covariant"/><category term="Create singly linkedlist"/><category term="Custom Annotation with Example"/><category term="Custom Object"/><category term="DNSJava"/><category term="Daemon Thread"/><category term="DataTable"/><category term="Decoding"/><category term="Decryption"/><category term="Deep copy"/><category term="Diameter of a Binary Tree"/><category term="Difference between IdentityHashMap and HashMap"/><category term="Do you know"/><category term="Domain Name lookup"/><category term="Drive Details"/><category term="Dynamic Programming - Find nth Fibonacci numberm Dynamic Programming"/><category term="Dynamic clustering"/><category term="Dynamic scaling"/><category term="Eclipse shortcuts"/><category term="Encoding"/><category term="Encryption"/><category term="Enum.ordinal() Method in Java"/><category term="Enumeration"/><category term="Enumeration in java"/><category term="Environment"/><category term="Error"/><category term="Even Odd Numbers"/><category term="Executor framework"/><category term="ExecutorService"/><category term="External Iterator"/><category term="Factory class"/><category term="File search"/><category term="Filter"/><category term="Find Nth node"/><category term="Find loop in linkedlist"/><category term="Find next greater number with same set of digits"/><category term="Find the Occurrences Of Each Character In a String"/><category term="Find the middle of a given linked list"/><category term="Find the smallest window in a string containing all characters of another string (With smallest window)"/><category term="Finding Minimum Distinct Ids"/><category term="Finonacci"/><category term="Folder search"/><category term="Future"/><category term="Generics"/><category term="Google GSON"/><category term="Google Maps API"/><category term="Grayscale"/><category term="Grayscale image"/><category term="Hazelcast"/><category term="Heap"/><category term="Hotspot JVM Options"/><category term="How to Design a simple LRU cache in Java"/><category term="How to Iterate Map key and value"/><category term="How to concatenate 2 List in Java"/><category term="How to create a graph using Java"/><category term="How to create simple and easy singly LinkedList in Java"/><category term="How to delete a Node from Linked List in java"/><category term="How to detect loop in a linked list or not"/><category term="How to do Iterative Postorder Traversal in BST"/><category term="How to do Level order traversal in Binary Search Tree"/><category term="How to do simple matrix multiplication"/><category term="How to find integer pairs with given sum"/><category term="How to find pairs of number to get magic sum"/><category term="How to find the largest subarray with sum 0"/><category term="How to get Maximum Subarray Sum in optimal way"/><category term="How to get all Java TimeZone ID's"/><category term="How to get height of BST using recursive way"/><category term="How to get integer pairs with given sum from integer array"/><category term="How to print singly linked list in reverse order"/><category term="How to reverse a String without special characters"/><category term="How to reverse a number using stack?"/><category term="IP"/><category term="IdentityHashMap"/><category term="Image border"/><category term="Image resizing"/><category term="Implement Stack in Java"/><category term="Implementing Queue in Java"/><category term="Inorder Predecessor and Successor of Binary Search Tree"/><category term="Insertion sort"/><category term="Interface default method in Java 8"/><category term="Interface extends another interface"/><category term="Interface extends another interface in Java"/><category term="Internal Iterator"/><category term="Invert bits of a number"/><category term="Iterator Design Pattern"/><category term="JAR"/><category term="JAX-RS"/><category term="JAX-WS"/><category term="JDBC example"/><category term="JDBCTemplate"/><category term="JSON Date Format"/><category term="JSON to Java Object"/><category term="JSP Custom error page"/><category term="Java 8 - ForEach loop"/><category term="Java 8 - Stream and Filter"/><category term="Java 8 - StringJoiner with examples"/><category term="Java Memory"/><category term="Java Object to JSON"/><category term="Java RegEx"/><category term="Java Timer"/><category term="JdbcDaoSupport"/><category term="JsonSerializer"/><category term="LCS"/><category term="LRU"/><category term="Lambda Expression"/><category term="Least Recently Used Cache"/><category term="Leetcode: 463. Island Perimeter"/><category term="Level order traversal"/><category term="LinkedHashSet"/><category term="ListIterator"/><category term="Locale"/><category term="Log4j"/><category term="Log4j Appender"/><category term="Login Page"/><category term="Longest Common Subsequence"/><category term="Longest Common Subsequence (LCS) algorithm using Dynamic Programming in java"/><category term="Longest Substring without duplicate character"/><category term="Longest palindrome Sub-sequence"/><category term="Mail API"/><category term="MailSender"/><category term="Marker Interface"/><category term="Matcher"/><category term="Maximum sub array"/><category term="Merge 2 Sorted Linked Lists"/><category term="Method Reference"/><category term="Mirroring Binary Tree"/><category term="Morgan Stanley Interview questions"/><category term="Multiply 2 numbers using recursion"/><category term="Multiply without * operator"/><category term="Multithreading in java"/><category term="Mutable Objects"/><category term="MySQL import and export commands"/><category term="MySql Timewait"/><category term="MySql commands and query"/><category term="N'th node from the end of a Linked List"/><category term="NoClassDefFoundError"/><category term="OS Details"/><category term="Object Sorting"/><category term="Object class"/><category term="Object reference by value"/><category term="ObjectInputStream"/><category term="ObjectOutputStream"/><category term="Odd even using ReentrantLock and Condition"/><category term="PID"/><category term="Pair of given sum"/><category term="Pangram"/><category term="Pangram or not"/><category term="Pass by reference"/><category term="Pass by value"/><category term="Pattern"/><category term="Permutation"/><category term="Pre-Authentication"/><category term="Predecessor"/><category term="Predecessor and Successor"/><category term="Preorder"/><category term="Print all leaf nodes of a Binary Tree from left to right"/><category term="Print all pairs with given sum"/><category term="Printing ODD EVEN"/><category term="PriorityBlockingQueue"/><category term="Process"/><category term="QR Code"/><category term="Quicksort"/><category term="Read web page"/><category term="ReentrantLock"/><category term="ReentrantLock and Condition"/><category term="Reflection"/><category term="Regular Expression"/><category term="Regular Expressions"/><category term="Remove all consecutive duplicates from the string"/><category term="Remove duplicate"/><category term="Reverse Linked List"/><category term="Reverse LinkedList"/><category term="Reverse number using Stack"/><category term="Reverse words in a given String using recursion"/><category term="Reverse words in a given string"/><category term="Rotate array by N elements"/><category term="Rotate matrix by clockwise"/><category term="SMS"/><category term="Scanner class"/><category term="Segregate 0s and 1s in an array by looping only once"/><category term="Semaphore"/><category term="Separate Logger for specific classes or package"/><category term="Serializable"/><category term="Serialization"/><category term="Serve static resources from different locations using spring"/><category term="Setter Injection"/><category term="Shallow copy"/><category term="Simple"/><category term="SimpleDateFormat"/><category term="Smallest substring which contains all characters of other String"/><category term="Smallest window"/><category term="Smallest window with all characters of other string"/><category term="Sorting HashMap by Values including duplicate in Java"/><category term="Sorting Hashmap"/><category term="Sorting Integer array without any sorting algorithm or loop"/><category term="Sorting array without loop"/><category term="Split the array into two equal Sum subarrays"/><category term="Split()"/><category term="Spring DataSoruce"/><category term="Spring Resource Loader"/><category term="Spring with hibernate"/><category term="Stack using Linked List"/><category term="Static Vs Volatile"/><category term="Stream API"/><category term="Streams"/><category term="String Permutation using Recursion"/><category term="String Replace"/><category term="String Reverse"/><category term="String.join"/><category term="StringBuffer"/><category term="StringBuilder"/><category term="StringTokenizer"/><category term="Successor"/><category term="Synchronized Map"/><category term="Tag Interface"/><category term="ThreadLocal"/><category term="Throw"/><category term="Throws"/><category term="Traversal"/><category term="TreeSet"/><category term="Try-with-Resources"/><category term="Unboxing"/><category term="Unix commands"/><category term="Variable arity"/><category term="Volatile"/><category term="What are the Java TimeZone ID's"/><category term="XOR"/><category term="boxing"/><category term="caching in java"/><category term="collect"/><category term="collectors"/><category term="count"/><category term="delete node"/><category term="duplicate element"/><category term="entrySet()"/><category term="equals"/><category term="equals() method"/><category term="exception handling in JSP"/><category term="executable jar"/><category term="facebook interview questions"/><category term="final"/><category term="finalize"/><category term="finding missing number in an array"/><category term="google interview questions"/><category term="graph creation"/><category term="hashcode"/><category term="how to get only left child in BST"/><category term="jQuery notify"/><category term="java.sql.Date"/><category term="java.sql.Timestamp"/><category term="join()"/><category term="keySet()"/><category term="left nodes"/><category term="loop"/><category term="merge List"/><category term="middle node"/><category term="mysqldump"/><category term="next greater number"/><category term="notify()"/><category term="notifyAll()"/><category term="read Object from file"/><category term="remove null from List"/><category term="rotate matrix"/><category term="scale-able data distribution platform"/><category term="sending email"/><category term="sending email using embedded image"/><category term="sorting hashmap based on values"/><category term="spring JDBC"/><category term="subarray with sum 0"/><category term="synchronization"/><category term="traverse the linked list only once to get middle node"/><category term="user defined Immutable Class"/><category term="user defined exception"/><category term="values()"/><category term="wait()"/><category term="words reverse"/><category term="write object to file"/><title type="text">Java Discover</title><subtitle type="html">Java Discover is a Java technical blog mainly created for sharing our thoughts and programming question to the world of Java Programmers..</subtitle><link href="https://javadiscover.blogspot.com/feeds/posts/default" rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml"/><link href="https://www.blogger.com/feeds/6801225484322010450/posts/default?max-results=10" rel="self" type="application/atom+xml"/><link href="https://javadiscover.blogspot.com/" rel="alternate" type="text/html"/><link href="http://pubsubhubbub.appspot.com/" rel="hub"/><link href="https://www.blogger.com/feeds/6801225484322010450/posts/default?start-index=11&amp;max-results=10" rel="next" type="application/atom+xml"/><author><name>Java Discover</name><uri>http://www.blogger.com/profile/18049511268651382259</uri><email>noreply@blogger.com</email><gd:image height="16" rel="http://schemas.google.com/g/2005#thumbnail" src="https://img1.blogblog.com/img/b16-rounded.gif" width="16"/></author><generator uri="http://www.blogger.com" version="7.00">Blogger</generator><openSearch:totalResults>251</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>10</openSearch:itemsPerPage><xhtml:meta content="noindex" name="robots" xmlns:xhtml="http://www.w3.org/1999/xhtml"/><entry><id>tag:blogger.com,1999:blog-6801225484322010450.post-835651608268451853</id><published>2020-06-28T01:34:00.000+05:30</published><updated>2020-06-28T01:34:08.570+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Interview Questions"/><category scheme="http://www.blogger.com/atom/ns#" term="Leetcode: 463. Island Perimeter"/><title type="text">Leetcode: 463. Island Perimeter</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;br /&gt;
&lt;div style="background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, &amp;quot;Segoe UI&amp;quot;, &amp;quot;PingFang SC&amp;quot;, &amp;quot;Hiragino Sans GB&amp;quot;, &amp;quot;Microsoft YaHei&amp;quot;, &amp;quot;Helvetica Neue&amp;quot;, Helvetica, Arial, sans-serif, &amp;quot;Apple Color Emoji&amp;quot;, &amp;quot;Segoe UI Emoji&amp;quot;, &amp;quot;Segoe UI Symbol&amp;quot;; font-size: 14px; margin-bottom: 1em;"&gt;
You are given a map in the form of a two-dimensional integer grid where 1 represents land and 0 represents water.&lt;/div&gt;
&lt;div style="background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, &amp;quot;Segoe UI&amp;quot;, &amp;quot;PingFang SC&amp;quot;, &amp;quot;Hiragino Sans GB&amp;quot;, &amp;quot;Microsoft YaHei&amp;quot;, &amp;quot;Helvetica Neue&amp;quot;, Helvetica, Arial, sans-serif, &amp;quot;Apple Color Emoji&amp;quot;, &amp;quot;Segoe UI Emoji&amp;quot;, &amp;quot;Segoe UI Symbol&amp;quot;; font-size: 14px; margin-bottom: 1em;"&gt;
Grid cells are connected horizontally/vertically (not diagonally). The grid is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).&lt;/div&gt;
&lt;div style="background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, &amp;quot;Segoe UI&amp;quot;, &amp;quot;PingFang SC&amp;quot;, &amp;quot;Hiragino Sans GB&amp;quot;, &amp;quot;Microsoft YaHei&amp;quot;, &amp;quot;Helvetica Neue&amp;quot;, Helvetica, Arial, sans-serif, &amp;quot;Apple Color Emoji&amp;quot;, &amp;quot;Segoe UI Emoji&amp;quot;, &amp;quot;Segoe UI Symbol&amp;quot;; font-size: 14px; margin-bottom: 1em;"&gt;
The island doesn't have "lakes" (water inside that isn't connected to the water around the island). One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.&lt;/div&gt;
&lt;div style="background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, &amp;quot;Segoe UI&amp;quot;, &amp;quot;PingFang SC&amp;quot;, &amp;quot;Hiragino Sans GB&amp;quot;, &amp;quot;Microsoft YaHei&amp;quot;, &amp;quot;Helvetica Neue&amp;quot;, Helvetica, Arial, sans-serif, &amp;quot;Apple Color Emoji&amp;quot;, &amp;quot;Segoe UI Emoji&amp;quot;, &amp;quot;Segoe UI Symbol&amp;quot;; font-size: 14px; margin-bottom: 1em;"&gt;
&amp;nbsp;&lt;span style="font-weight: bolder;"&gt;Example:&lt;/span&gt;&lt;/div&gt;
&lt;pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, &amp;quot;Liberation Mono&amp;quot;, Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; overflow: auto; padding: 10px 15px; white-space: pre-wrap;"&gt;&lt;span style="box-sizing: border-box; font-weight: bolder;"&gt;Input:&lt;/span&gt;
[[0,1,0,0],
 [1,1,1,0],
 [0,1,0,0],
 [1,1,0,0]]

&lt;span style="box-sizing: border-box; font-weight: bolder;"&gt;Output:&lt;/span&gt; 16

&lt;span style="box-sizing: border-box; font-weight: bolder;"&gt;Explanation:&lt;/span&gt; The perimeter is the 16 yellow stripes in the image below:

&lt;img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="border-style: none; box-sizing: border-box; height: 213px; max-width: 100%; vertical-align: middle; width: 221px;" /&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;b&gt;PROGRAM:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;"&gt;
&lt;pre style="line-height: 125%; margin: 0;"&gt;&lt;span style="color: #008800; font-weight: bold;"&gt;class&lt;/span&gt; &lt;span style="color: #bb0066; font-weight: bold;"&gt;Solution&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="line-height: 125%; margin: 0;"&gt; &lt;/pre&gt;
&lt;pre style="line-height: 125%; margin: 0;"&gt;    &lt;span style="color: #008800; font-weight: bold;"&gt;public&lt;/span&gt; &lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; &lt;span style="color: #0066bb; font-weight: bold;"&gt;islandPerimeter&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;&lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt;&lt;span style="color: #333333;"&gt;[][]&lt;/span&gt; grid&lt;span style="color: #333333;"&gt;)&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="line-height: 125%; margin: 0;"&gt;        &lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; wallCount &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;0&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
        &lt;span style="color: #008800; font-weight: bold;"&gt;for&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;&lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; i&lt;span style="color: #333333;"&gt;=&lt;/span&gt;&lt;span style="color: #0000dd; font-weight: bold;"&gt;0&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt;i&lt;span style="color: #333333;"&gt;&amp;lt;&lt;/span&gt;grid&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;length&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt;i&lt;span style="color: #333333;"&gt;++){&lt;/span&gt;
            &lt;span style="color: #008800; font-weight: bold;"&gt;for&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;&lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; j&lt;span style="color: #333333;"&gt;=&lt;/span&gt;&lt;span style="color: #0000dd; font-weight: bold;"&gt;0&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt;j&lt;span style="color: #333333;"&gt;&amp;lt;&lt;/span&gt;grid&lt;span style="color: #333333;"&gt;[&lt;/span&gt;&lt;span style="color: #0000dd; font-weight: bold;"&gt;0&lt;/span&gt;&lt;span style="color: #333333;"&gt;].&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;length&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt;j&lt;span style="color: #333333;"&gt;++){&lt;/span&gt;
                &lt;span style="color: #008800; font-weight: bold;"&gt;if&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;grid&lt;span style="color: #333333;"&gt;[&lt;/span&gt;i&lt;span style="color: #333333;"&gt;][&lt;/span&gt;j&lt;span style="color: #333333;"&gt;]&lt;/span&gt; &lt;span style="color: #333333;"&gt;==&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;1&lt;/span&gt;&lt;span style="color: #333333;"&gt;){&lt;/span&gt;
                    
                    &lt;span style="color: #008800; font-weight: bold;"&gt;if&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;j &lt;span style="color: #333333;"&gt;==&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;0&lt;/span&gt;&lt;span style="color: #333333;"&gt;)&lt;/span&gt; wallCount&lt;span style="color: #333333;"&gt;++;&lt;/span&gt;
                    &lt;span style="color: #008800; font-weight: bold;"&gt;else&lt;/span&gt; &lt;span style="color: #0066bb; font-weight: bold;"&gt;if&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;j &lt;span style="color: #333333;"&gt;&amp;gt;&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;0&lt;/span&gt; &lt;span style="color: #333333;"&gt;&amp;amp;&amp;amp;&lt;/span&gt; grid&lt;span style="color: #333333;"&gt;[&lt;/span&gt;i&lt;span style="color: #333333;"&gt;][&lt;/span&gt;j&lt;span style="color: #333333;"&gt;-&lt;/span&gt;&lt;span style="color: #0000dd; font-weight: bold;"&gt;1&lt;/span&gt;&lt;span style="color: #333333;"&gt;]&lt;/span&gt; &lt;span style="color: #333333;"&gt;==&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;0&lt;/span&gt;&lt;span style="color: #333333;"&gt;)&lt;/span&gt; wallCount&lt;span style="color: #333333;"&gt;++;&lt;/span&gt;
                    
                    &lt;span style="color: #008800; font-weight: bold;"&gt;if&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;i &lt;span style="color: #333333;"&gt;==&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;0&lt;/span&gt;&lt;span style="color: #333333;"&gt;)&lt;/span&gt; wallCount&lt;span style="color: #333333;"&gt;++;&lt;/span&gt;
                    &lt;span style="color: #008800; font-weight: bold;"&gt;else&lt;/span&gt; &lt;span style="color: #0066bb; font-weight: bold;"&gt;if&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;i &lt;span style="color: #333333;"&gt;&amp;gt;&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;0&lt;/span&gt; &lt;span style="color: #333333;"&gt;&amp;amp;&amp;amp;&lt;/span&gt; grid&lt;span style="color: #333333;"&gt;[&lt;/span&gt;i&lt;span style="color: #333333;"&gt;-&lt;/span&gt;&lt;span style="color: #0000dd; font-weight: bold;"&gt;1&lt;/span&gt;&lt;span style="color: #333333;"&gt;][&lt;/span&gt;j&lt;span style="color: #333333;"&gt;]&lt;/span&gt; &lt;span style="color: #333333;"&gt;==&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;0&lt;/span&gt;&lt;span style="color: #333333;"&gt;)&lt;/span&gt; wallCount&lt;span style="color: #333333;"&gt;++;&lt;/span&gt;
                    
                    &lt;span style="color: #008800; font-weight: bold;"&gt;if&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;j&lt;span style="color: #333333;"&gt;+&lt;/span&gt;&lt;span style="color: #0000dd; font-weight: bold;"&gt;1&lt;/span&gt; &lt;span style="color: #333333;"&gt;&amp;gt;=&lt;/span&gt; grid&lt;span style="color: #333333;"&gt;[&lt;/span&gt;&lt;span style="color: #0000dd; font-weight: bold;"&gt;0&lt;/span&gt;&lt;span style="color: #333333;"&gt;].&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;length&lt;/span&gt;&lt;span style="color: #333333;"&gt;)&lt;/span&gt; wallCount&lt;span style="color: #333333;"&gt;++;&lt;/span&gt;
                    &lt;span style="color: #008800; font-weight: bold;"&gt;else&lt;/span&gt; &lt;span style="color: #0066bb; font-weight: bold;"&gt;if&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;grid&lt;span style="color: #333333;"&gt;[&lt;/span&gt;i&lt;span style="color: #333333;"&gt;][&lt;/span&gt;j&lt;span style="color: #333333;"&gt;+&lt;/span&gt;&lt;span style="color: #0000dd; font-weight: bold;"&gt;1&lt;/span&gt;&lt;span style="color: #333333;"&gt;]&lt;/span&gt; &lt;span style="color: #333333;"&gt;==&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;0&lt;/span&gt;&lt;span style="color: #333333;"&gt;)&lt;/span&gt; wallCount&lt;span style="color: #333333;"&gt;++;&lt;/span&gt;
                    
                    &lt;span style="color: #008800; font-weight: bold;"&gt;if&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;i&lt;span style="color: #333333;"&gt;+&lt;/span&gt;&lt;span style="color: #0000dd; font-weight: bold;"&gt;1&lt;/span&gt; &lt;span style="color: #333333;"&gt;&amp;gt;=&lt;/span&gt; grid&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;length&lt;/span&gt;&lt;span style="color: #333333;"&gt;)&lt;/span&gt; wallCount&lt;span style="color: #333333;"&gt;++;&lt;/span&gt;
                    &lt;span style="color: #008800; font-weight: bold;"&gt;else&lt;/span&gt; &lt;span style="color: #0066bb; font-weight: bold;"&gt;if&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;grid&lt;span style="color: #333333;"&gt;[&lt;/span&gt;i&lt;span style="color: #333333;"&gt;+&lt;/span&gt;&lt;span style="color: #0000dd; font-weight: bold;"&gt;1&lt;/span&gt;&lt;span style="color: #333333;"&gt;][&lt;/span&gt;j&lt;span style="color: #333333;"&gt;]&lt;/span&gt; &lt;span style="color: #333333;"&gt;==&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;0&lt;/span&gt;&lt;span style="color: #333333;"&gt;)&lt;/span&gt; wallCount&lt;span style="color: #333333;"&gt;++;&lt;/span&gt;
                &lt;span style="color: #333333;"&gt;}&lt;/span&gt;
            &lt;span style="color: #333333;"&gt;}&lt;/span&gt;
        &lt;span style="color: #333333;"&gt;}&lt;/span&gt;
        &lt;span style="color: #008800; font-weight: bold;"&gt;return&lt;/span&gt; wallCount&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
    &lt;span style="color: #333333;"&gt;}&lt;/span&gt;
&lt;span style="color: #333333;"&gt;}&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;OUTPUT:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
16&lt;/div&gt;
</content><link href="https://www.blogger.com/feeds/6801225484322010450/posts/default/835651608268451853" rel="edit" type="application/atom+xml"/><link href="https://www.blogger.com/feeds/6801225484322010450/posts/default/835651608268451853" rel="self" type="application/atom+xml"/><link href="https://javadiscover.blogspot.com/2020/06/leetcode-463-island-perimeter.html" rel="alternate" title="Leetcode: 463. Island Perimeter" type="text/html"/><author><name>Java Discover</name><uri>http://www.blogger.com/profile/18049511268651382259</uri><email>noreply@blogger.com</email><gd:image height="16" rel="http://schemas.google.com/g/2005#thumbnail" src="https://img1.blogblog.com/img/b16-rounded.gif" width="16"/></author></entry><entry><id>tag:blogger.com,1999:blog-6801225484322010450.post-147329011556628780</id><published>2019-07-30T22:37:00.001+05:30</published><updated>2019-07-30T22:37:58.703+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Core Java"/><category scheme="http://www.blogger.com/atom/ns#" term="Enum.ordinal() Method in Java"/><category scheme="http://www.blogger.com/atom/ns#" term="Interview Questions"/><title type="text">Enum.ordinal() Method in Java</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
Returns the ordinal of this enumeration constant (its position in its enum declaration, where the initial constant is assigned an ordinal of zero). For example, let's have weekdays in an enum starting from Monday, Tuesday till Sunday. So the ordinal of Monday will be 0, Tuesday will be 1, etc.,&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhNv7N1__U7UqlMl2gcsL8mnh01AEFCOCIcKXXhUJrb3BG7fLI8oQt_kVAHnJ1ZMcMNRyUV43ApM0sUzz60nQtOKmmdeOtOPJtNEYX54GN4wtOs7aNkN_p9oQ-Q7y1W_q-DAFlOSN-eduQ/s1600/Screenshot+2019-07-30+at+10.35.24+PM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="Enum.ordinal() Method in Java" border="0" data-original-height="586" data-original-width="1308" height="178" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhNv7N1__U7UqlMl2gcsL8mnh01AEFCOCIcKXXhUJrb3BG7fLI8oQt_kVAHnJ1ZMcMNRyUV43ApM0sUzz60nQtOKmmdeOtOPJtNEYX54GN4wtOs7aNkN_p9oQ-Q7y1W_q-DAFlOSN-eduQ/s400/Screenshot+2019-07-30+at+10.35.24+PM.png" title="Enum.ordinal() Method in Java" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
Let's see simple Java for the same.&lt;br /&gt;
&lt;br /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;"&gt;
&lt;pre style="line-height: 125%; margin: 0;"&gt;&lt;span style="color: #008800; font-weight: bold;"&gt;enum&lt;/span&gt; Days&lt;span style="color: #333333;"&gt;{&lt;/span&gt;
&lt;span style="color: #888888;"&gt;// 0 1  2     3         4       5  6 &lt;/span&gt;
 MONDAY&lt;span style="color: #333333;"&gt;,&lt;/span&gt; TUESDAY&lt;span style="color: #333333;"&gt;,&lt;/span&gt; WEDNESDAY&lt;span style="color: #333333;"&gt;,&lt;/span&gt; THURSDAY&lt;span style="color: #333333;"&gt;,&lt;/span&gt; FRIDAY&lt;span style="color: #333333;"&gt;,&lt;/span&gt; SATURDAY&lt;span style="color: #333333;"&gt;,&lt;/span&gt; SUNDAY&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
&lt;span style="color: #333333;"&gt;}&lt;/span&gt;

&lt;span style="color: #008800; font-weight: bold;"&gt;public&lt;/span&gt; &lt;span style="color: #008800; font-weight: bold;"&gt;class&lt;/span&gt; &lt;span style="color: #bb0066; font-weight: bold;"&gt;EnumOrdinal&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;
 &lt;span style="color: #008800; font-weight: bold;"&gt;public&lt;/span&gt; &lt;span style="color: #008800; font-weight: bold;"&gt;static&lt;/span&gt; &lt;span style="color: #333399; font-weight: bold;"&gt;void&lt;/span&gt; &lt;span style="color: #0066bb; font-weight: bold;"&gt;main&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;String&lt;span style="color: #333333;"&gt;[]&lt;/span&gt; args&lt;span style="color: #333333;"&gt;)&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;
  Days mon &lt;span style="color: #333333;"&gt;=&lt;/span&gt; Days&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;MONDAY&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
  Days wed &lt;span style="color: #333333;"&gt;=&lt;/span&gt; Days&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;WEDNESDAY&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
  Days tus &lt;span style="color: #333333;"&gt;=&lt;/span&gt; Days&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;TUESDAY&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
  Days fri &lt;span style="color: #333333;"&gt;=&lt;/span&gt; Days&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;FRIDAY&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
  Days sun &lt;span style="color: #333333;"&gt;=&lt;/span&gt; Days&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;SUNDAY&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
  
  System&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;out&lt;/span&gt;&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;println&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;&lt;span style="background-color: #fff0f0;"&gt;"Monday ordinal    : "&lt;/span&gt;&lt;span style="color: #333333;"&gt;+&lt;/span&gt;mon&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;ordinal&lt;/span&gt;&lt;span style="color: #333333;"&gt;());&lt;/span&gt;
  System&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;out&lt;/span&gt;&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;println&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;&lt;span style="background-color: #fff0f0;"&gt;"Wednesday ordinal : "&lt;/span&gt;&lt;span style="color: #333333;"&gt;+&lt;/span&gt;wed&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;ordinal&lt;/span&gt;&lt;span style="color: #333333;"&gt;());&lt;/span&gt;
  System&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;out&lt;/span&gt;&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;println&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;&lt;span style="background-color: #fff0f0;"&gt;"Tuesday ordinal   : "&lt;/span&gt;&lt;span style="color: #333333;"&gt;+&lt;/span&gt;tus&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;ordinal&lt;/span&gt;&lt;span style="color: #333333;"&gt;());&lt;/span&gt;
  System&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;out&lt;/span&gt;&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;println&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;&lt;span style="background-color: #fff0f0;"&gt;"Friday ordinal    : "&lt;/span&gt;&lt;span style="color: #333333;"&gt;+&lt;/span&gt;fri&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;ordinal&lt;/span&gt;&lt;span style="color: #333333;"&gt;());&lt;/span&gt;
  System&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;out&lt;/span&gt;&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;println&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;&lt;span style="background-color: #fff0f0;"&gt;"Sunday ordinal    : "&lt;/span&gt;&lt;span style="color: #333333;"&gt;+&lt;/span&gt;sun&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;ordinal&lt;/span&gt;&lt;span style="color: #333333;"&gt;());&lt;/span&gt;
 &lt;span style="color: #333333;"&gt;}&lt;/span&gt;
&lt;span style="color: #333333;"&gt;}&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;OUTPUT:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;"&gt;
&lt;pre style="line-height: 125%; margin: 0;"&gt;Monday ordinal    &lt;span style="color: #333333;"&gt;:&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;0&lt;/span&gt;
Wednesday ordinal &lt;span style="color: #333333;"&gt;:&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;2&lt;/span&gt;
Tuesday ordinal   &lt;span style="color: #333333;"&gt;:&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;1&lt;/span&gt;
Friday ordinal    &lt;span style="color: #333333;"&gt;:&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;4&lt;/span&gt;
Sunday ordinal    &lt;span style="color: #333333;"&gt;:&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;6&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link href="https://www.blogger.com/feeds/6801225484322010450/posts/default/147329011556628780" rel="edit" type="application/atom+xml"/><link href="https://www.blogger.com/feeds/6801225484322010450/posts/default/147329011556628780" rel="self" type="application/atom+xml"/><link href="https://javadiscover.blogspot.com/2019/07/enumordinal-method-in-java.html" rel="alternate" title="Enum.ordinal() Method in Java" type="text/html"/><author><name>Java Discover</name><uri>http://www.blogger.com/profile/18049511268651382259</uri><email>noreply@blogger.com</email><gd:image height="16" rel="http://schemas.google.com/g/2005#thumbnail" src="https://img1.blogblog.com/img/b16-rounded.gif" width="16"/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" height="72" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhNv7N1__U7UqlMl2gcsL8mnh01AEFCOCIcKXXhUJrb3BG7fLI8oQt_kVAHnJ1ZMcMNRyUV43ApM0sUzz60nQtOKmmdeOtOPJtNEYX54GN4wtOs7aNkN_p9oQ-Q7y1W_q-DAFlOSN-eduQ/s72-c/Screenshot+2019-07-30+at+10.35.24+PM.png" width="72"/></entry><entry><id>tag:blogger.com,1999:blog-6801225484322010450.post-9187588117343390712</id><published>2019-07-10T11:16:00.001+05:30</published><updated>2019-07-10T11:16:44.323+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Character Occurrences"/><category scheme="http://www.blogger.com/atom/ns#" term="Core Java"/><category scheme="http://www.blogger.com/atom/ns#" term="Find the Occurrences Of Each Character In a String"/><category scheme="http://www.blogger.com/atom/ns#" term="Interview Questions"/><category scheme="http://www.blogger.com/atom/ns#" term="Java Interview Questions"/><title type="text">Find the Occurrences Of Each Character In a String</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
Find the Occurrences Of Each Character In a String without using collection.&lt;br /&gt;
Here we are going to create simple array to store the character occurrence. If we look at ASCII value of each character including space then it starts from 32 to 126. So we will create array size of 94 which can hold all the character occurrences.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhbFfM2ghvoGAGLWuPnEGzE0GNQPlw6AoihCIEHEr0_RqLLMjfzxX4H1GLOCob2aXoDuMc1FVmCEFYXS8SK9Iv-TlxqajeJJCSwiTit63EVx1WZIVOHTPcjiIr4PnWU7jtdK-S1ZoxAI8Y/s1600/Find+the+Occurrences+Of+Each+Character+In+a+String.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="Find the Occurrences Of Each Character In a String" border="0" data-original-height="1098" data-original-width="1144" height="383" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhbFfM2ghvoGAGLWuPnEGzE0GNQPlw6AoihCIEHEr0_RqLLMjfzxX4H1GLOCob2aXoDuMc1FVmCEFYXS8SK9Iv-TlxqajeJJCSwiTit63EVx1WZIVOHTPcjiIr4PnWU7jtdK-S1ZoxAI8Y/s400/Find+the+Occurrences+Of+Each+Character+In+a+String.png" title="Find the Occurrences Of Each Character In a String" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
Let's see simple example in Java how to implement in a simple way.&lt;br /&gt;
&lt;br /&gt;
&lt;div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;"&gt;
&lt;pre style="line-height: 125%; margin: 0;"&gt;&lt;span style="color: #008800; font-weight: bold;"&gt;public&lt;/span&gt; &lt;span style="color: #008800; font-weight: bold;"&gt;class&lt;/span&gt; &lt;span style="color: #bb0066; font-weight: bold;"&gt;CharacterOccurrance&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;

 &lt;span style="color: #008800; font-weight: bold;"&gt;public&lt;/span&gt; &lt;span style="color: #008800; font-weight: bold;"&gt;static&lt;/span&gt; &lt;span style="color: #333399; font-weight: bold;"&gt;void&lt;/span&gt; &lt;span style="color: #0066bb; font-weight: bold;"&gt;main&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;String&lt;span style="color: #333333;"&gt;[]&lt;/span&gt; args&lt;span style="color: #333333;"&gt;)&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;
  String str &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="background-color: #fff0f0;"&gt;"Java Discover~"&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt;

  CharacterOccurrance obj &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="color: #008800; font-weight: bold;"&gt;new&lt;/span&gt; CharacterOccurrance&lt;span style="color: #333333;"&gt;();&lt;/span&gt;
  &lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt;&lt;span style="color: #333333;"&gt;[]&lt;/span&gt; occurrance &lt;span style="color: #333333;"&gt;=&lt;/span&gt; obj&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;findCharOccurrance&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;str&lt;span style="color: #333333;"&gt;);&lt;/span&gt;
  obj&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;printOccurrance&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;occurrance&lt;span style="color: #333333;"&gt;);&lt;/span&gt;
 &lt;span style="color: #333333;"&gt;}&lt;/span&gt;

 &lt;span style="color: #008800; font-weight: bold;"&gt;public&lt;/span&gt; &lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt;&lt;span style="color: #333333;"&gt;[]&lt;/span&gt; &lt;span style="color: #0066bb; font-weight: bold;"&gt;findCharOccurrance&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;String str&lt;span style="color: #333333;"&gt;)&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;
  &lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt;&lt;span style="color: #333333;"&gt;[]&lt;/span&gt; occurrance &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="color: #008800; font-weight: bold;"&gt;new&lt;/span&gt; &lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt;&lt;span style="color: #333333;"&gt;[&lt;/span&gt;&lt;span style="color: #0000dd; font-weight: bold;"&gt;95&lt;/span&gt;&lt;span style="color: #333333;"&gt;];&lt;/span&gt;
  &lt;span style="color: #008800; font-weight: bold;"&gt;for&lt;/span&gt; &lt;span style="color: #333333;"&gt;(&lt;/span&gt;&lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; i &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;0&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt; i &lt;span style="color: #333333;"&gt;&amp;lt;&lt;/span&gt; str&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;length&lt;/span&gt;&lt;span style="color: #333333;"&gt;();&lt;/span&gt; i&lt;span style="color: #333333;"&gt;++)&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;
   &lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; index &lt;span style="color: #333333;"&gt;=&lt;/span&gt; str&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;charAt&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;i&lt;span style="color: #333333;"&gt;);&lt;/span&gt;
   occurrance&lt;span style="color: #333333;"&gt;[&lt;/span&gt;index &lt;span style="color: #333333;"&gt;-&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;32&lt;/span&gt;&lt;span style="color: #333333;"&gt;]++;&lt;/span&gt;
  &lt;span style="color: #333333;"&gt;}&lt;/span&gt;
  &lt;span style="color: #008800; font-weight: bold;"&gt;return&lt;/span&gt; occurrance&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
 &lt;span style="color: #333333;"&gt;}&lt;/span&gt;

 &lt;span style="color: #008800; font-weight: bold;"&gt;public&lt;/span&gt; &lt;span style="color: #333399; font-weight: bold;"&gt;void&lt;/span&gt; &lt;span style="color: #0066bb; font-weight: bold;"&gt;printOccurrance&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;&lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt;&lt;span style="color: #333333;"&gt;[]&lt;/span&gt; occurrance&lt;span style="color: #333333;"&gt;)&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;
  System&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;out&lt;/span&gt;&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;println&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;&lt;span style="background-color: #fff0f0;"&gt;"Character : Occurrance"&lt;/span&gt;&lt;span style="color: #333333;"&gt;);&lt;/span&gt;
  &lt;span style="color: #008800; font-weight: bold;"&gt;for&lt;/span&gt; &lt;span style="color: #333333;"&gt;(&lt;/span&gt;&lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; i &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;0&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt; i &lt;span style="color: #333333;"&gt;&amp;lt;&lt;/span&gt; occurrance&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;length&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt; i&lt;span style="color: #333333;"&gt;++)&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;
   &lt;span style="color: #008800; font-weight: bold;"&gt;if&lt;/span&gt; &lt;span style="color: #333333;"&gt;(&lt;/span&gt;occurrance&lt;span style="color: #333333;"&gt;[&lt;/span&gt;i&lt;span style="color: #333333;"&gt;]&lt;/span&gt; &lt;span style="color: #333333;"&gt;&amp;gt;&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;0&lt;/span&gt;&lt;span style="color: #333333;"&gt;)&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;
    &lt;span style="color: #333399; font-weight: bold;"&gt;char&lt;/span&gt; ch &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="color: #333333;"&gt;(&lt;/span&gt;&lt;span style="color: #333399; font-weight: bold;"&gt;char&lt;/span&gt;&lt;span style="color: #333333;"&gt;)&lt;/span&gt; &lt;span style="color: #333333;"&gt;(&lt;/span&gt;i &lt;span style="color: #333333;"&gt;+&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;32&lt;/span&gt;&lt;span style="color: #333333;"&gt;);&lt;/span&gt;
    System&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;out&lt;/span&gt;&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;println&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;ch &lt;span style="color: #333333;"&gt;+&lt;/span&gt; &lt;span style="background-color: #fff0f0;"&gt;" \t  : "&lt;/span&gt; &lt;span style="color: #333333;"&gt;+&lt;/span&gt; occurrance&lt;span style="color: #333333;"&gt;[&lt;/span&gt;i&lt;span style="color: #333333;"&gt;]);&lt;/span&gt;
   &lt;span style="color: #333333;"&gt;}&lt;/span&gt;
  &lt;span style="color: #333333;"&gt;}&lt;/span&gt;
 &lt;span style="color: #333333;"&gt;}&lt;/span&gt;
&lt;span style="color: #333333;"&gt;}&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;OUTPUT:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;"&gt;
&lt;pre style="line-height: 125%; margin: 0;"&gt;Character &lt;span style="color: #333333;"&gt;:&lt;/span&gt; Occurrance
     &lt;span style="color: #333333;"&gt;:&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;1          &amp;lt;----- Space&lt;/span&gt;
D    &lt;span style="color: #333333;"&gt;:&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;1&lt;/span&gt;
J    &lt;span style="color: #333333;"&gt;:&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;1&lt;/span&gt;
a    &lt;span style="color: #333333;"&gt;:&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;2&lt;/span&gt;
c    &lt;span style="color: #333333;"&gt;:&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;1&lt;/span&gt;
e    &lt;span style="color: #333333;"&gt;:&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;1&lt;/span&gt;
i    &lt;span style="color: #333333;"&gt;:&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;1&lt;/span&gt;
o    &lt;span style="color: #333333;"&gt;:&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;1&lt;/span&gt;
r    &lt;span style="color: #333333;"&gt;:&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;1&lt;/span&gt;
s    &lt;span style="color: #333333;"&gt;:&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;1&lt;/span&gt;
v    &lt;span style="color: #333333;"&gt;:&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;2&lt;/span&gt;
&lt;span style="color: #333333;"&gt;~&lt;/span&gt;    &lt;span style="color: #333333;"&gt;:&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;1&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link href="https://www.blogger.com/feeds/6801225484322010450/posts/default/9187588117343390712" rel="edit" type="application/atom+xml"/><link href="https://www.blogger.com/feeds/6801225484322010450/posts/default/9187588117343390712" rel="self" type="application/atom+xml"/><link href="https://javadiscover.blogspot.com/2019/07/find-occurrences-of-each-character-in.html" rel="alternate" title="Find the Occurrences Of Each Character In a String" type="text/html"/><author><name>Java Discover</name><uri>http://www.blogger.com/profile/18049511268651382259</uri><email>noreply@blogger.com</email><gd:image height="16" rel="http://schemas.google.com/g/2005#thumbnail" src="https://img1.blogblog.com/img/b16-rounded.gif" width="16"/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" height="72" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhbFfM2ghvoGAGLWuPnEGzE0GNQPlw6AoihCIEHEr0_RqLLMjfzxX4H1GLOCob2aXoDuMc1FVmCEFYXS8SK9Iv-TlxqajeJJCSwiTit63EVx1WZIVOHTPcjiIr4PnWU7jtdK-S1ZoxAI8Y/s72-c/Find+the+Occurrences+Of+Each+Character+In+a+String.png" width="72"/></entry><entry><id>tag:blogger.com,1999:blog-6801225484322010450.post-2238385481227816631</id><published>2019-06-27T23:41:00.001+05:30</published><updated>2019-06-27T23:41:09.535+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="amazon interview questions"/><category scheme="http://www.blogger.com/atom/ns#" term="Core Java"/><category scheme="http://www.blogger.com/atom/ns#" term="Dynamic Programming"/><category scheme="http://www.blogger.com/atom/ns#" term="Interview Questions"/><category scheme="http://www.blogger.com/atom/ns#" term="Longest palindrome Sub-sequence"/><title type="text">Longest palindrome Sub-sequence from the given String using Dynamic Programming</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
Write a program to find the longest sub-sequence palindrome from the given string by using dynamic programming. For example&lt;br /&gt;
&lt;br /&gt;
Input String :&amp;nbsp;ABCDQRDC&lt;br /&gt;
&lt;br /&gt;
Longest sub-sequence palindrome: 5&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhSHsPPn0t2kiME6pIGr2G0mrI_0YVUCy4qZ2I3OQKSwey0cN3wu93KNzAuh8vzoc3QnhhB2ii8M2D1PrgXgmCqXjJ_2tfMPAM9y5BOJzIj5ijBspjKdOoFCV1ZW5rsQn2Ygnfv421LcU4/s1600/Screenshot+2019-01-10+at+11.15.40+PM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="Longest palindrome Sub-sequence from the given String using Dynamic Programming" border="0" data-original-height="308" data-original-width="628" height="156" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhSHsPPn0t2kiME6pIGr2G0mrI_0YVUCy4qZ2I3OQKSwey0cN3wu93KNzAuh8vzoc3QnhhB2ii8M2D1PrgXgmCqXjJ_2tfMPAM9y5BOJzIj5ijBspjKdOoFCV1ZW5rsQn2Ygnfv421LcU4/s320/Screenshot+2019-01-10+at+11.15.40+PM.png" title="Longest palindrome Sub-sequence from the given String using Dynamic Programming" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;&amp;nbsp;So let's see how we will solve and find the longest subsequence palindrome using dynamic programming.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;&lt;br /&gt;&lt;/li&gt;
&lt;li&gt;Construct the solution matrix of size NxN where N is the length of given string.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;We will calculate the size of each character sequence palindrome size and will the memorised in the solution matrix which will referred for the next incremental sequence.&lt;/li&gt;
&lt;li&gt;For example in above example all strings of 1 character will be palindrome size of 1. A, B, C, D, Q, R, D, C&lt;/li&gt;
&lt;li&gt;On next cycle it will add another character and makes 2 character string like&amp;nbsp;&lt;/li&gt;
&lt;li&gt;AB, BC, CD, DQ, QR, RD, DC and goes on like&amp;nbsp;&lt;/li&gt;
&lt;li&gt;ABC, BCD, CDQ, DQR, QRD, RDC etc.,&lt;/li&gt;
&lt;li&gt;Each sequences palindrome size will be stored in the solution array and finally returns the maximum size as output.&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
Now lets see simple Java code&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;"&gt;
&lt;pre style="line-height: 125%; margin: 0;"&gt; &lt;span style="color: #008800; font-weight: bold;"&gt;public&lt;/span&gt; &lt;span style="color: #008800; font-weight: bold;"&gt;class&lt;/span&gt; &lt;span style="color: #bb0066; font-weight: bold;"&gt;LongestPalindromeSequence&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;

 &lt;span style="color: #008800; font-weight: bold;"&gt;public&lt;/span&gt; &lt;span style="color: #008800; font-weight: bold;"&gt;static&lt;/span&gt; &lt;span style="color: #333399; font-weight: bold;"&gt;void&lt;/span&gt; &lt;span style="color: #0066bb; font-weight: bold;"&gt;main&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;String&lt;span style="color: #333333;"&gt;[]&lt;/span&gt; args&lt;span style="color: #333333;"&gt;)&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;

  String str &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="background-color: #fff0f0;"&gt;"ABCDQRDC"&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt;

  LongestPalindromeSequence obj &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="color: #008800; font-weight: bold;"&gt;new&lt;/span&gt; LongestPalindromeSequence&lt;span style="color: #333333;"&gt;();&lt;/span&gt;

  System&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;out&lt;/span&gt;&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;println&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;&lt;span style="background-color: #fff0f0;"&gt;"\nLength of Longest palindrome subsequence : "&lt;/span&gt; &lt;span style="color: #333333;"&gt;+&lt;/span&gt; obj&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;lps&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;str&lt;span style="color: #333333;"&gt;));&lt;/span&gt;

 &lt;span style="color: #333333;"&gt;}&lt;/span&gt;

 &lt;span style="color: #008800; font-weight: bold;"&gt;public&lt;/span&gt; &lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; &lt;span style="color: #0066bb; font-weight: bold;"&gt;lps&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;String seq&lt;span style="color: #333333;"&gt;)&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;
  &lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; n &lt;span style="color: #333333;"&gt;=&lt;/span&gt; seq&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;length&lt;/span&gt;&lt;span style="color: #333333;"&gt;();&lt;/span&gt;
  &lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; j&lt;span style="color: #333333;"&gt;,&lt;/span&gt; k&lt;span style="color: #333333;"&gt;,&lt;/span&gt; i&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
  &lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; L&lt;span style="color: #333333;"&gt;[][]&lt;/span&gt; &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="color: #008800; font-weight: bold;"&gt;new&lt;/span&gt; &lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt;&lt;span style="color: #333333;"&gt;[&lt;/span&gt;n&lt;span style="color: #333333;"&gt;][&lt;/span&gt;n&lt;span style="color: #333333;"&gt;];&lt;/span&gt;

  &lt;span style="color: #888888;"&gt;// Strings of 1 character will be palindrome size of 1 &lt;/span&gt;
  &lt;span style="color: #888888;"&gt;// starting with upper matrix and all single character falls diagonally and size will be 1&lt;/span&gt;
  &lt;span style="color: #008800; font-weight: bold;"&gt;for&lt;/span&gt; &lt;span style="color: #333333;"&gt;(&lt;/span&gt;j &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;0&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt; j &lt;span style="color: #333333;"&gt;&amp;lt;&lt;/span&gt; n&lt;span style="color: #333333;"&gt;;&lt;/span&gt; j&lt;span style="color: #333333;"&gt;++)&lt;/span&gt;
   L&lt;span style="color: #333333;"&gt;[&lt;/span&gt;j&lt;span style="color: #333333;"&gt;][&lt;/span&gt;j&lt;span style="color: #333333;"&gt;]&lt;/span&gt; &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;1&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt;

  &lt;span style="color: #008800; font-weight: bold;"&gt;for&lt;/span&gt; &lt;span style="color: #333333;"&gt;(&lt;/span&gt;i &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;2&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt; i &lt;span style="color: #333333;"&gt;&amp;lt;=&lt;/span&gt; n&lt;span style="color: #333333;"&gt;;&lt;/span&gt; i&lt;span style="color: #333333;"&gt;++)&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;
   &lt;span style="color: #008800; font-weight: bold;"&gt;for&lt;/span&gt; &lt;span style="color: #333333;"&gt;(&lt;/span&gt;j &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;0&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt; j &lt;span style="color: #333333;"&gt;&amp;lt;&lt;/span&gt; n &lt;span style="color: #333333;"&gt;-&lt;/span&gt; i &lt;span style="color: #333333;"&gt;+&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;1&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt; j&lt;span style="color: #333333;"&gt;++)&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;

    k &lt;span style="color: #333333;"&gt;=&lt;/span&gt; j &lt;span style="color: #333333;"&gt;+&lt;/span&gt; i &lt;span style="color: #333333;"&gt;-&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;1&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt;

    &lt;span style="color: #008800; font-weight: bold;"&gt;if&lt;/span&gt; &lt;span style="color: #333333;"&gt;(&lt;/span&gt;seq&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;charAt&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;j&lt;span style="color: #333333;"&gt;)&lt;/span&gt; &lt;span style="color: #333333;"&gt;==&lt;/span&gt; seq&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;charAt&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;k&lt;span style="color: #333333;"&gt;))&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;
     L&lt;span style="color: #333333;"&gt;[&lt;/span&gt;j&lt;span style="color: #333333;"&gt;][&lt;/span&gt;k&lt;span style="color: #333333;"&gt;]&lt;/span&gt; &lt;span style="color: #333333;"&gt;=&lt;/span&gt; L&lt;span style="color: #333333;"&gt;[&lt;/span&gt;j &lt;span style="color: #333333;"&gt;+&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;1&lt;/span&gt;&lt;span style="color: #333333;"&gt;][&lt;/span&gt;k &lt;span style="color: #333333;"&gt;-&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;1&lt;/span&gt;&lt;span style="color: #333333;"&gt;]&lt;/span&gt; &lt;span style="color: #333333;"&gt;+&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;2&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt;

    &lt;span style="color: #333333;"&gt;}&lt;/span&gt; &lt;span style="color: #008800; font-weight: bold;"&gt;else&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;
     L&lt;span style="color: #333333;"&gt;[&lt;/span&gt;j&lt;span style="color: #333333;"&gt;][&lt;/span&gt;k&lt;span style="color: #333333;"&gt;]&lt;/span&gt; &lt;span style="color: #333333;"&gt;=&lt;/span&gt; Math&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;max&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;L&lt;span style="color: #333333;"&gt;[&lt;/span&gt;j&lt;span style="color: #333333;"&gt;][&lt;/span&gt;k &lt;span style="color: #333333;"&gt;-&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;1&lt;/span&gt;&lt;span style="color: #333333;"&gt;],&lt;/span&gt; L&lt;span style="color: #333333;"&gt;[&lt;/span&gt;j &lt;span style="color: #333333;"&gt;+&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;1&lt;/span&gt;&lt;span style="color: #333333;"&gt;][&lt;/span&gt;k&lt;span style="color: #333333;"&gt;]);&lt;/span&gt;
    &lt;span style="color: #333333;"&gt;}&lt;/span&gt;
   &lt;span style="color: #333333;"&gt;}&lt;/span&gt;
  &lt;span style="color: #333333;"&gt;}&lt;/span&gt;

  &lt;span style="color: #888888;"&gt;/*System.out.println("Final solution Matrix :::: ");&lt;/span&gt;
&lt;span style="color: #888888;"&gt;  for (j = 0; j &amp;lt; n; j++) {&lt;/span&gt;
&lt;span style="color: #888888;"&gt;   System.out.print(seq.charAt(j) +" - ");&lt;/span&gt;
&lt;span style="color: #888888;"&gt;   for (k = 0; k &amp;lt; n; k++) {&lt;/span&gt;
&lt;span style="color: #888888;"&gt;    System.out.print(L[j][k] + "\t");&lt;/span&gt;
&lt;span style="color: #888888;"&gt;   }&lt;/span&gt;
&lt;span style="color: #888888;"&gt;   System.out.println();&lt;/span&gt;
&lt;span style="color: #888888;"&gt;  }*/&lt;/span&gt;
  &lt;span style="color: #008800; font-weight: bold;"&gt;return&lt;/span&gt; L&lt;span style="color: #333333;"&gt;[&lt;/span&gt;&lt;span style="color: #0000dd; font-weight: bold;"&gt;0&lt;/span&gt;&lt;span style="color: #333333;"&gt;][&lt;/span&gt;n &lt;span style="color: #333333;"&gt;-&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;1&lt;/span&gt;&lt;span style="color: #333333;"&gt;];&lt;/span&gt;
 &lt;span style="color: #333333;"&gt;}&lt;/span&gt;
&lt;span style="color: #333333;"&gt;}&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;OUTPUT:&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;"&gt;
&lt;pre style="line-height: 125%; margin: 0;"&gt;Length of Longest palindrome subsequence &lt;span style="color: #333333;"&gt;:&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;5&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link href="https://www.blogger.com/feeds/6801225484322010450/posts/default/2238385481227816631" rel="edit" type="application/atom+xml"/><link href="https://www.blogger.com/feeds/6801225484322010450/posts/default/2238385481227816631" rel="self" type="application/atom+xml"/><link href="https://javadiscover.blogspot.com/2019/06/longest-palindrome-sub-sequence-from.html" rel="alternate" title="Longest palindrome Sub-sequence from the given String using Dynamic Programming" type="text/html"/><author><name>Java Discover</name><uri>http://www.blogger.com/profile/18049511268651382259</uri><email>noreply@blogger.com</email><gd:image height="16" rel="http://schemas.google.com/g/2005#thumbnail" src="https://img1.blogblog.com/img/b16-rounded.gif" width="16"/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" height="72" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhSHsPPn0t2kiME6pIGr2G0mrI_0YVUCy4qZ2I3OQKSwey0cN3wu93KNzAuh8vzoc3QnhhB2ii8M2D1PrgXgmCqXjJ_2tfMPAM9y5BOJzIj5ijBspjKdOoFCV1ZW5rsQn2Ygnfv421LcU4/s72-c/Screenshot+2019-01-10+at+11.15.40+PM.png" width="72"/></entry><entry><id>tag:blogger.com,1999:blog-6801225484322010450.post-2567250919307651526</id><published>2019-06-27T23:37:00.000+05:30</published><updated>2019-06-28T00:19:02.801+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Core Java"/><category scheme="http://www.blogger.com/atom/ns#" term="Data Structure"/><category scheme="http://www.blogger.com/atom/ns#" term="graph creation"/><category scheme="http://www.blogger.com/atom/ns#" term="How to create a graph using Java"/><title type="text">How to create a graph using Java</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
Let's see simple graph creation using core java and printing all the vertex along with list of edges which goes from that particular Vertex.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEifNjOZ5FGln6DXMcO56_9qZu3W2uU6PH0FT6depxfbRd_UZcLbzzE2XJr2HHGCrM0cLSKpD6q5xwW9U7hyETlm_Mzi1z_W9LiXEY6SZFN8H2aAtQUp8RszI012dxY-u1OY-X1PJbTdhV0/s1600/How+to+create+graph+in+java.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="How to create a graph using Java" border="0" data-original-height="1200" data-original-width="1600" height="240" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEifNjOZ5FGln6DXMcO56_9qZu3W2uU6PH0FT6depxfbRd_UZcLbzzE2XJr2HHGCrM0cLSKpD6q5xwW9U7hyETlm_Mzi1z_W9LiXEY6SZFN8H2aAtQUp8RszI012dxY-u1OY-X1PJbTdhV0/s320/How+to+create+graph+in+java.jpg" title="How to create a graph using Java" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;"&gt;
&lt;pre style="line-height: 125%; margin: 0;"&gt;&lt;span style="color: #008800; font-weight: bold;"&gt;public&lt;/span&gt; &lt;span style="color: #008800; font-weight: bold;"&gt;class&lt;/span&gt; &lt;span style="color: #bb0066; font-weight: bold;"&gt;MyGraph&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;

 &lt;span style="color: #008800; font-weight: bold;"&gt;public&lt;/span&gt; &lt;span style="color: #008800; font-weight: bold;"&gt;static&lt;/span&gt; &lt;span style="color: #333399; font-weight: bold;"&gt;void&lt;/span&gt; &lt;span style="color: #0066bb; font-weight: bold;"&gt;main&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;String&lt;span style="color: #333333;"&gt;[]&lt;/span&gt; args&lt;span style="color: #333333;"&gt;)&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;
  
  &lt;span style="color: #888888;"&gt;/* Initilize graph with 5 vertex and as &lt;/span&gt;
&lt;span style="color: #888888;"&gt;   * going to be directed graph&lt;/span&gt;
&lt;span style="color: #888888;"&gt;   */&lt;/span&gt;
  
  Graph myGra &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="color: #008800; font-weight: bold;"&gt;new&lt;/span&gt; Graph&lt;span style="color: #333333;"&gt;(&lt;/span&gt;&lt;span style="color: #0000dd; font-weight: bold;"&gt;5&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt; &lt;span style="background-color: #fff0f0;"&gt;"directed"&lt;/span&gt;&lt;span style="color: #333333;"&gt;);&lt;/span&gt;
  
  &lt;span style="color: #888888;"&gt;/*&lt;/span&gt;
&lt;span style="color: #888888;"&gt;   * Add vertex &lt;/span&gt;
&lt;span style="color: #888888;"&gt;   */&lt;/span&gt;
  myGra&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;addVertex&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;&lt;span style="background-color: #fff0f0;"&gt;"A"&lt;/span&gt;&lt;span style="color: #333333;"&gt;);&lt;/span&gt;
  myGra&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;addVertex&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;&lt;span style="background-color: #fff0f0;"&gt;"B"&lt;/span&gt;&lt;span style="color: #333333;"&gt;);&lt;/span&gt;
  myGra&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;addVertex&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;&lt;span style="background-color: #fff0f0;"&gt;"C"&lt;/span&gt;&lt;span style="color: #333333;"&gt;);&lt;/span&gt;
  myGra&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;addVertex&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;&lt;span style="background-color: #fff0f0;"&gt;"D"&lt;/span&gt;&lt;span style="color: #333333;"&gt;);&lt;/span&gt;
  myGra&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;addVertex&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;&lt;span style="background-color: #fff0f0;"&gt;"E"&lt;/span&gt;&lt;span style="color: #333333;"&gt;);&lt;/span&gt;
  
  &lt;span style="color: #888888;"&gt;/*&lt;/span&gt;
&lt;span style="color: #888888;"&gt;   * Add edges between each vertex and their distance &lt;/span&gt;
&lt;span style="color: #888888;"&gt;   */&lt;/span&gt;
  myGra&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;addEdge&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;&lt;span style="background-color: #fff0f0;"&gt;"A"&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt; &lt;span style="background-color: #fff0f0;"&gt;"B"&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt;&lt;span style="color: #0000dd; font-weight: bold;"&gt;5&lt;/span&gt;&lt;span style="color: #333333;"&gt;);&lt;/span&gt;
  myGra&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;addEdge&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;&lt;span style="background-color: #fff0f0;"&gt;"A"&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt; &lt;span style="background-color: #fff0f0;"&gt;"E"&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt;&lt;span style="color: #0000dd; font-weight: bold;"&gt;1&lt;/span&gt;&lt;span style="color: #333333;"&gt;);&lt;/span&gt;
  myGra&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;addEdge&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;&lt;span style="background-color: #fff0f0;"&gt;"B"&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt; &lt;span style="background-color: #fff0f0;"&gt;"C"&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt;&lt;span style="color: #0000dd; font-weight: bold;"&gt;2&lt;/span&gt;&lt;span style="color: #333333;"&gt;);&lt;/span&gt;
  myGra&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;addEdge&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;&lt;span style="background-color: #fff0f0;"&gt;"C"&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt; &lt;span style="background-color: #fff0f0;"&gt;"D"&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt;&lt;span style="color: #0000dd; font-weight: bold;"&gt;2&lt;/span&gt;&lt;span style="color: #333333;"&gt;);&lt;/span&gt;
  myGra&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;addEdge&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;&lt;span style="background-color: #fff0f0;"&gt;"E"&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt; &lt;span style="background-color: #fff0f0;"&gt;"D"&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt;&lt;span style="color: #0000dd; font-weight: bold;"&gt;2&lt;/span&gt;&lt;span style="color: #333333;"&gt;);&lt;/span&gt;
  myGra&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;addEdge&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;&lt;span style="background-color: #fff0f0;"&gt;"E"&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt; &lt;span style="background-color: #fff0f0;"&gt;"A"&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt;&lt;span style="color: #0000dd; font-weight: bold;"&gt;2&lt;/span&gt;&lt;span style="color: #333333;"&gt;);&lt;/span&gt;
  
  &lt;span style="color: #888888;"&gt;// Print the created graph&lt;/span&gt;
  myGra&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;printMyGraph&lt;/span&gt;&lt;span style="color: #333333;"&gt;();&lt;/span&gt;
  
 &lt;span style="color: #333333;"&gt;}&lt;/span&gt;
&lt;span style="color: #333333;"&gt;}&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;"&gt;
&lt;pre style="line-height: 125%; margin: 0;"&gt;&lt;span style="color: #008800; font-weight: bold;"&gt;public&lt;/span&gt; &lt;span style="color: #008800; font-weight: bold;"&gt;class&lt;/span&gt; &lt;span style="color: #bb0066; font-weight: bold;"&gt;Graph&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;

 &lt;span style="color: #888888;"&gt;// Graph direction (directed / undirected graph)&lt;/span&gt;
 &lt;span style="color: #008800; font-weight: bold;"&gt;private&lt;/span&gt; &lt;span style="color: #333399; font-weight: bold;"&gt;boolean&lt;/span&gt; undirected &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="color: #008800; font-weight: bold;"&gt;true&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
 
 &lt;span style="color: #888888;"&gt;// No. of vertex in the graph&lt;/span&gt;
 &lt;span style="color: #008800; font-weight: bold;"&gt;private&lt;/span&gt; Vertex&lt;span style="color: #333333;"&gt;[]&lt;/span&gt; arrayOfVertex&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
 
 &lt;span style="color: #008800; font-weight: bold;"&gt;private&lt;/span&gt; &lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; indexCounter &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;0&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
 
 &lt;span style="color: #888888;"&gt;// Constructor to create graph vertex &lt;/span&gt;
 &lt;span style="color: #008800; font-weight: bold;"&gt;public&lt;/span&gt; &lt;span style="color: #0066bb; font-weight: bold;"&gt;Graph&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;&lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; noOfVertex&lt;span style="color: #333333;"&gt;,&lt;/span&gt; String graphType&lt;span style="color: #333333;"&gt;)&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;
  &lt;span style="color: #008800; font-weight: bold;"&gt;if&lt;/span&gt; &lt;span style="color: #333333;"&gt;(&lt;/span&gt;graphType&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;equalsIgnoreCase&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;&lt;span style="background-color: #fff0f0;"&gt;"directed"&lt;/span&gt;&lt;span style="color: #333333;"&gt;))&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;
   &lt;span style="color: #008800; font-weight: bold;"&gt;this&lt;/span&gt;&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;undirected&lt;/span&gt; &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="color: #008800; font-weight: bold;"&gt;false&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
  &lt;span style="color: #333333;"&gt;}&lt;/span&gt;
  arrayOfVertex &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="color: #008800; font-weight: bold;"&gt;new&lt;/span&gt; Vertex&lt;span style="color: #333333;"&gt;[&lt;/span&gt;noOfVertex&lt;span style="color: #333333;"&gt;];&lt;/span&gt;
 &lt;span style="color: #333333;"&gt;}&lt;/span&gt;
 
 &lt;span style="color: #888888;"&gt;// Vertex class&lt;/span&gt;
 &lt;span style="color: #008800; font-weight: bold;"&gt;class&lt;/span&gt; &lt;span style="color: #bb0066; font-weight: bold;"&gt;Vertex&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;
  &lt;span style="color: #888888;"&gt;// Name of the Vertex&lt;/span&gt;
  &lt;span style="color: #008800; font-weight: bold;"&gt;private&lt;/span&gt; String name&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
  
  &lt;span style="color: #888888;"&gt;// Holds the list of all edges from current vertex &lt;/span&gt;
  &lt;span style="color: #008800; font-weight: bold;"&gt;private&lt;/span&gt; Edge edge&lt;span style="color: #333333;"&gt;;&lt;/span&gt;

  &lt;span style="color: #888888;"&gt;// Create vertex &lt;/span&gt;
  &lt;span style="color: #008800; font-weight: bold;"&gt;public&lt;/span&gt; &lt;span style="color: #0066bb; font-weight: bold;"&gt;Vertex&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;String name&lt;span style="color: #333333;"&gt;,&lt;/span&gt; Edge aNode&lt;span style="color: #333333;"&gt;)&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;
   &lt;span style="color: #008800; font-weight: bold;"&gt;this&lt;/span&gt;&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;name&lt;/span&gt; &lt;span style="color: #333333;"&gt;=&lt;/span&gt; name&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
   &lt;span style="color: #008800; font-weight: bold;"&gt;this&lt;/span&gt;&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;edge&lt;/span&gt; &lt;span style="color: #333333;"&gt;=&lt;/span&gt; aNode&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
  &lt;span style="color: #333333;"&gt;}&lt;/span&gt;
 &lt;span style="color: #333333;"&gt;}&lt;/span&gt;
 
 &lt;span style="color: #888888;"&gt;// Edge between 2 Vertex&lt;/span&gt;
 &lt;span style="color: #008800; font-weight: bold;"&gt;class&lt;/span&gt; &lt;span style="color: #bb0066; font-weight: bold;"&gt;Edge&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;
  &lt;span style="color: #888888;"&gt;// Destination vertex Id&lt;/span&gt;
  &lt;span style="color: #008800; font-weight: bold;"&gt;private&lt;/span&gt; &lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; vertexId&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
  
  &lt;span style="color: #888888;"&gt;// In list point to next edge if its else null &lt;/span&gt;
  &lt;span style="color: #008800; font-weight: bold;"&gt;private&lt;/span&gt; Edge next&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
  
  &lt;span style="color: #888888;"&gt;// Weight of current edge&lt;/span&gt;
  &lt;span style="color: #008800; font-weight: bold;"&gt;private&lt;/span&gt; &lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; weight&lt;span style="color: #333333;"&gt;;&lt;/span&gt;

  &lt;span style="color: #888888;"&gt;// Create edge&lt;/span&gt;
  &lt;span style="color: #008800; font-weight: bold;"&gt;public&lt;/span&gt; &lt;span style="color: #0066bb; font-weight: bold;"&gt;Edge&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;&lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; vertexId&lt;span style="color: #333333;"&gt;,&lt;/span&gt; Edge next&lt;span style="color: #333333;"&gt;,&lt;/span&gt; &lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; weight&lt;span style="color: #333333;"&gt;)&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;
   &lt;span style="color: #008800; font-weight: bold;"&gt;this&lt;/span&gt;&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;vertexId&lt;/span&gt; &lt;span style="color: #333333;"&gt;=&lt;/span&gt; vertexId&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
   &lt;span style="color: #008800; font-weight: bold;"&gt;this&lt;/span&gt;&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;next&lt;/span&gt; &lt;span style="color: #333333;"&gt;=&lt;/span&gt; next&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
   &lt;span style="color: #008800; font-weight: bold;"&gt;this&lt;/span&gt;&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;weight&lt;/span&gt; &lt;span style="color: #333333;"&gt;=&lt;/span&gt; weight&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
  &lt;span style="color: #333333;"&gt;}&lt;/span&gt;
 &lt;span style="color: #333333;"&gt;}&lt;/span&gt;

 &lt;span style="color: #888888;"&gt;// Adding Vertex &lt;/span&gt;
 &lt;span style="color: #008800; font-weight: bold;"&gt;public&lt;/span&gt; &lt;span style="color: #333399; font-weight: bold;"&gt;void&lt;/span&gt; &lt;span style="color: #0066bb; font-weight: bold;"&gt;addVertex&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;String vertexName&lt;span style="color: #333333;"&gt;)&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;
  arrayOfVertex&lt;span style="color: #333333;"&gt;[&lt;/span&gt;indexCounter&lt;span style="color: #333333;"&gt;]&lt;/span&gt; &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="color: #008800; font-weight: bold;"&gt;new&lt;/span&gt; Vertex&lt;span style="color: #333333;"&gt;(&lt;/span&gt;vertexName&lt;span style="color: #333333;"&gt;,&lt;/span&gt; &lt;span style="color: #008800; font-weight: bold;"&gt;null&lt;/span&gt;&lt;span style="color: #333333;"&gt;);&lt;/span&gt;
  indexCounter&lt;span style="color: #333333;"&gt;++;&lt;/span&gt;
 &lt;span style="color: #333333;"&gt;}&lt;/span&gt;

 &lt;span style="color: #888888;"&gt;// Adding Edge&lt;/span&gt;
 &lt;span style="color: #008800; font-weight: bold;"&gt;public&lt;/span&gt; &lt;span style="color: #333399; font-weight: bold;"&gt;void&lt;/span&gt; &lt;span style="color: #0066bb; font-weight: bold;"&gt;addEdge&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;String sVertex&lt;span style="color: #333333;"&gt;,&lt;/span&gt; String dVertex&lt;span style="color: #333333;"&gt;,&lt;/span&gt; &lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; weight&lt;span style="color: #333333;"&gt;)&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;
  &lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; sId &lt;span style="color: #333333;"&gt;=&lt;/span&gt; indexOfName&lt;span style="color: #333333;"&gt;(&lt;/span&gt;sVertex&lt;span style="color: #333333;"&gt;);&lt;/span&gt;
  &lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; dId &lt;span style="color: #333333;"&gt;=&lt;/span&gt; indexOfName&lt;span style="color: #333333;"&gt;(&lt;/span&gt;dVertex&lt;span style="color: #333333;"&gt;);&lt;/span&gt;

  &lt;span style="color: #888888;"&gt;/*&lt;/span&gt;
&lt;span style="color: #888888;"&gt;   * Find source and destination vertexId and create new Edge and &lt;/span&gt;
&lt;span style="color: #888888;"&gt;   * point it to source edge link&lt;/span&gt;
&lt;span style="color: #888888;"&gt;   */&lt;/span&gt;
  arrayOfVertex&lt;span style="color: #333333;"&gt;[&lt;/span&gt;sId&lt;span style="color: #333333;"&gt;].&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;edge&lt;/span&gt; &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="color: #008800; font-weight: bold;"&gt;new&lt;/span&gt; Edge&lt;span style="color: #333333;"&gt;(&lt;/span&gt;dId&lt;span style="color: #333333;"&gt;,&lt;/span&gt; arrayOfVertex&lt;span style="color: #333333;"&gt;[&lt;/span&gt;sId&lt;span style="color: #333333;"&gt;].&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;edge&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt; weight&lt;span style="color: #333333;"&gt;);&lt;/span&gt;
  
  &lt;span style="color: #888888;"&gt;/*&lt;/span&gt;
&lt;span style="color: #888888;"&gt;   * If undirected then create 2 edge's between source and destination and &lt;/span&gt;
&lt;span style="color: #888888;"&gt;   * destination to source &lt;/span&gt;
&lt;span style="color: #888888;"&gt;   */&lt;/span&gt;
  &lt;span style="color: #008800; font-weight: bold;"&gt;if&lt;/span&gt; &lt;span style="color: #333333;"&gt;(&lt;/span&gt;undirected&lt;span style="color: #333333;"&gt;)&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;
   arrayOfVertex&lt;span style="color: #333333;"&gt;[&lt;/span&gt;dId&lt;span style="color: #333333;"&gt;].&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;edge&lt;/span&gt; &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="color: #008800; font-weight: bold;"&gt;new&lt;/span&gt; Edge&lt;span style="color: #333333;"&gt;(&lt;/span&gt;sId&lt;span style="color: #333333;"&gt;,&lt;/span&gt; arrayOfVertex&lt;span style="color: #333333;"&gt;[&lt;/span&gt;dId&lt;span style="color: #333333;"&gt;].&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;edge&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt; weight&lt;span style="color: #333333;"&gt;);&lt;/span&gt;
  &lt;span style="color: #333333;"&gt;}&lt;/span&gt;
 &lt;span style="color: #333333;"&gt;}&lt;/span&gt;

 &lt;span style="color: #888888;"&gt;/*&lt;/span&gt;
&lt;span style="color: #888888;"&gt;  * Getting indexId of given vertex name&lt;/span&gt;
&lt;span style="color: #888888;"&gt;  */&lt;/span&gt;
 &lt;span style="color: #008800; font-weight: bold;"&gt;private&lt;/span&gt; &lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; &lt;span style="color: #0066bb; font-weight: bold;"&gt;indexOfName&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;String name&lt;span style="color: #333333;"&gt;)&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;
  &lt;span style="color: #008800; font-weight: bold;"&gt;for&lt;/span&gt; &lt;span style="color: #333333;"&gt;(&lt;/span&gt;&lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; v &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;0&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt; v &lt;span style="color: #333333;"&gt;&amp;lt;&lt;/span&gt; arrayOfVertex&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;length&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt; v&lt;span style="color: #333333;"&gt;++)&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;
   &lt;span style="color: #008800; font-weight: bold;"&gt;if&lt;/span&gt; &lt;span style="color: #333333;"&gt;(&lt;/span&gt;arrayOfVertex&lt;span style="color: #333333;"&gt;[&lt;/span&gt;v&lt;span style="color: #333333;"&gt;].&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;name&lt;/span&gt;&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;equals&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;name&lt;span style="color: #333333;"&gt;))&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;
    &lt;span style="color: #008800; font-weight: bold;"&gt;return&lt;/span&gt; v&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
   &lt;span style="color: #333333;"&gt;}&lt;/span&gt;
  &lt;span style="color: #333333;"&gt;}&lt;/span&gt;
  &lt;span style="color: #008800; font-weight: bold;"&gt;return&lt;/span&gt; &lt;span style="color: #333333;"&gt;-&lt;/span&gt;&lt;span style="color: #0000dd; font-weight: bold;"&gt;1&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
 &lt;span style="color: #333333;"&gt;}&lt;/span&gt;

 &lt;span style="color: #888888;"&gt;/*&lt;/span&gt;
&lt;span style="color: #888888;"&gt;  * Print the graph in vertex order and listing all outgoing edges from that vertex&lt;/span&gt;
&lt;span style="color: #888888;"&gt;  */&lt;/span&gt;
 &lt;span style="color: #008800; font-weight: bold;"&gt;public&lt;/span&gt; &lt;span style="color: #333399; font-weight: bold;"&gt;void&lt;/span&gt; &lt;span style="color: #0066bb; font-weight: bold;"&gt;printMyGraph&lt;/span&gt;&lt;span style="color: #333333;"&gt;()&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;
  System&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;out&lt;/span&gt;&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;println&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;&lt;span style="background-color: #fff0f0;"&gt;"VERTEX\t----&amp;gt; EDGES WITH WEIGHT"&lt;/span&gt;&lt;span style="color: #333333;"&gt;);&lt;/span&gt;
  &lt;span style="color: #008800; font-weight: bold;"&gt;for&lt;/span&gt; &lt;span style="color: #333333;"&gt;(&lt;/span&gt;&lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; v &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;0&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt; v &lt;span style="color: #333333;"&gt;&amp;lt;&lt;/span&gt; arrayOfVertex&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;length&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt; v&lt;span style="color: #333333;"&gt;++)&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;
   System&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;out&lt;/span&gt;&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;print&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;arrayOfVertex&lt;span style="color: #333333;"&gt;[&lt;/span&gt;v&lt;span style="color: #333333;"&gt;].&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;name&lt;/span&gt; &lt;span style="color: #333333;"&gt;+&lt;/span&gt;&lt;span style="background-color: #fff0f0;"&gt;" \t ----&amp;gt; "&lt;/span&gt;&lt;span style="color: #333333;"&gt;);&lt;/span&gt;
   &lt;span style="color: #008800; font-weight: bold;"&gt;for&lt;/span&gt; &lt;span style="color: #333333;"&gt;(&lt;/span&gt;Edge aNode &lt;span style="color: #333333;"&gt;=&lt;/span&gt; arrayOfVertex&lt;span style="color: #333333;"&gt;[&lt;/span&gt;v&lt;span style="color: #333333;"&gt;].&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;edge&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt; aNode &lt;span style="color: #333333;"&gt;!=&lt;/span&gt; &lt;span style="color: #008800; font-weight: bold;"&gt;null&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt; aNode &lt;span style="color: #333333;"&gt;=&lt;/span&gt; aNode&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;next&lt;/span&gt;&lt;span style="color: #333333;"&gt;)&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;
    System&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;out&lt;/span&gt;&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;print&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt; &lt;span style="background-color: #fff0f0;"&gt;"&amp;lt;==&amp;gt;"&lt;/span&gt;&lt;span style="color: #333333;"&gt;+&lt;/span&gt; arrayOfVertex&lt;span style="color: #333333;"&gt;[&lt;/span&gt;aNode&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;vertexId&lt;/span&gt;&lt;span style="color: #333333;"&gt;].&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;name&lt;/span&gt; &lt;span style="color: #333333;"&gt;+&lt;/span&gt; &lt;span style="background-color: #fff0f0;"&gt;":"&lt;/span&gt; &lt;span style="color: #333333;"&gt;+&lt;/span&gt; aNode&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;weight&lt;/span&gt;&lt;span style="color: #333333;"&gt;);&lt;/span&gt;
   &lt;span style="color: #333333;"&gt;}&lt;/span&gt;
   System&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;out&lt;/span&gt;&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;println&lt;/span&gt;&lt;span style="color: #333333;"&gt;();&lt;/span&gt;
  &lt;span style="color: #333333;"&gt;}&lt;/span&gt;

 &lt;span style="color: #333333;"&gt;}&lt;/span&gt;
&lt;span style="color: #333333;"&gt;}&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
OUTPUT:&lt;br /&gt;
&lt;br /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;"&gt;
&lt;pre style="line-height: 125%; margin: 0;"&gt;&lt;b&gt;VERTEX         &lt;span style="color: #333333;"&gt;----&amp;gt;&lt;/span&gt; EDGES WITH WEIGHT
&lt;/b&gt;A          &lt;span style="color: #333333;"&gt;----&amp;gt;&lt;/span&gt; &lt;span style="color: #333333;"&gt;&amp;lt;==&amp;gt;&lt;/span&gt;&lt;span style="color: #997700; font-weight: bold;"&gt;E:&lt;/span&gt;&lt;span style="color: #0000dd; font-weight: bold;"&gt;1&lt;/span&gt;&lt;span style="color: #333333;"&gt;&amp;lt;==&amp;gt;&lt;/span&gt;&lt;span style="color: #997700; font-weight: bold;"&gt;B:&lt;/span&gt;&lt;span style="color: #0000dd; font-weight: bold;"&gt;5&lt;/span&gt;
B          &lt;span style="color: #333333;"&gt;----&amp;gt;&lt;/span&gt; &lt;span style="color: #333333;"&gt;&amp;lt;==&amp;gt;&lt;/span&gt;&lt;span style="color: #997700; font-weight: bold;"&gt;C:&lt;/span&gt;&lt;span style="color: #0000dd; font-weight: bold;"&gt;2&lt;/span&gt;
C          &lt;span style="color: #333333;"&gt;----&amp;gt;&lt;/span&gt; &lt;span style="color: #333333;"&gt;&amp;lt;==&amp;gt;&lt;/span&gt;&lt;span style="color: #997700; font-weight: bold;"&gt;D:&lt;/span&gt;&lt;span style="color: #0000dd; font-weight: bold;"&gt;2&lt;/span&gt;
D          &lt;span style="color: #333333;"&gt;----&amp;gt;&lt;/span&gt; 
E          &lt;span style="color: #333333;"&gt;----&amp;gt;&lt;/span&gt; &lt;span style="color: #333333;"&gt;&amp;lt;==&amp;gt;&lt;/span&gt;&lt;span style="color: #997700; font-weight: bold;"&gt;A:&lt;/span&gt;&lt;span style="color: #0000dd; font-weight: bold;"&gt;2&lt;/span&gt;&lt;span style="color: #333333;"&gt;&amp;lt;==&amp;gt;&lt;/span&gt;&lt;span style="color: #997700; font-weight: bold;"&gt;D:&lt;/span&gt;&lt;span style="color: #0000dd; font-weight: bold;"&gt;2&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link href="https://www.blogger.com/feeds/6801225484322010450/posts/default/2567250919307651526" rel="edit" type="application/atom+xml"/><link href="https://www.blogger.com/feeds/6801225484322010450/posts/default/2567250919307651526" rel="self" type="application/atom+xml"/><link href="https://javadiscover.blogspot.com/2019/06/how-to-create-graph-using-java.html" rel="alternate" title="How to create a graph using Java" type="text/html"/><author><name>Java Discover</name><uri>http://www.blogger.com/profile/18049511268651382259</uri><email>noreply@blogger.com</email><gd:image height="16" rel="http://schemas.google.com/g/2005#thumbnail" src="https://img1.blogblog.com/img/b16-rounded.gif" width="16"/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" height="72" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEifNjOZ5FGln6DXMcO56_9qZu3W2uU6PH0FT6depxfbRd_UZcLbzzE2XJr2HHGCrM0cLSKpD6q5xwW9U7hyETlm_Mzi1z_W9LiXEY6SZFN8H2aAtQUp8RszI012dxY-u1OY-X1PJbTdhV0/s72-c/How+to+create+graph+in+java.jpg" width="72"/></entry><entry><id>tag:blogger.com,1999:blog-6801225484322010450.post-6721605222102368794</id><published>2019-02-07T14:54:00.000+05:30</published><updated>2019-02-07T14:56:39.892+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Add two numbers represented by linked lists using recursive"/><category scheme="http://www.blogger.com/atom/ns#" term="Addition of 2 Linked List"/><title type="text">Add two numbers represented by linked lists using recursive</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
Add two numbers represented by linked lists using recursive method and generate the 3td linked with addition of 1st and 2nd linked list.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&lt;style type="text/css"&gt;
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco}
span.s1 {color: #7f0055}
span.s2 {color: #6a3e3e}
span.Apple-tab-span {white-space:pre}
&lt;/style&gt;


&lt;br /&gt;
&lt;div class="p1"&gt;
&lt;span class="s2"&gt;val_1&lt;/span&gt;[] = { &lt;span class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;7, 6, 5 }&lt;/div&gt;
&lt;div class="p1"&gt;
&lt;span class="s2"&gt;val_2&lt;/span&gt;[] = { 9, 6, 4, 1, 6 }&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
then the output should be {&lt;span style="font-family: &amp;quot;monaco&amp;quot;; font-size: 11px;"&gt;9, 7, 1, 8, 1&lt;/span&gt;} basically its addition of 1st and 2nd linked lists as same normal 2 numbers addition.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj0IbyIRaq21gQgYGukh4pQ8BHL6au8BUjfIfEcHVvS5h3AwGjSX3ILB9aqw4zPFarK1ml59vGO2iro0lVjEi8NMZM-lq_eSKUnmlBOSk8c_VUFKyT3xm8RCrc5WwNXlcpdhMh8kEhN5AE/s1600/Screenshot+2019-02-07+at+2.49.41+PM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="Add two numbers represented by linked lists using recursive" border="0" data-original-height="188" data-original-width="312" height="192" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj0IbyIRaq21gQgYGukh4pQ8BHL6au8BUjfIfEcHVvS5h3AwGjSX3ILB9aqw4zPFarK1ml59vGO2iro0lVjEi8NMZM-lq_eSKUnmlBOSk8c_VUFKyT3xm8RCrc5WwNXlcpdhMh8kEhN5AE/s320/Screenshot+2019-02-07+at+2.49.41+PM.png" title="Add two numbers represented by linked lists using recursive" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;
Let's see simple java code to create linked lists using input array and to get the addition of 2 linked list in 3rd linked list.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;"&gt;
&lt;pre style="line-height: 125%; margin: 0;"&gt;&lt;span style="color: #008800; font-weight: bold;"&gt;public&lt;/span&gt; &lt;span style="color: #008800; font-weight: bold;"&gt;class&lt;/span&gt; &lt;span style="color: #bb0066; font-weight: bold;"&gt;SumOfTwoLL&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;
 
 &lt;span style="color: #008800; font-weight: bold;"&gt;class&lt;/span&gt; &lt;span style="color: #bb0066; font-weight: bold;"&gt;NODE&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;

  &lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; data&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
  NODE next&lt;span style="color: #333333;"&gt;;&lt;/span&gt;

  &lt;span style="color: #008800; font-weight: bold;"&gt;public&lt;/span&gt; &lt;span style="color: #0066bb; font-weight: bold;"&gt;NODE&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;&lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; data&lt;span style="color: #333333;"&gt;)&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;
   &lt;span style="color: #008800; font-weight: bold;"&gt;this&lt;/span&gt;&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;data&lt;/span&gt; &lt;span style="color: #333333;"&gt;=&lt;/span&gt; data&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
   &lt;span style="color: #008800; font-weight: bold;"&gt;this&lt;/span&gt;&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;next&lt;/span&gt; &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="color: #008800; font-weight: bold;"&gt;null&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
  &lt;span style="color: #333333;"&gt;}&lt;/span&gt;
 &lt;span style="color: #333333;"&gt;}&lt;/span&gt;

 &lt;span style="color: #008800; font-weight: bold;"&gt;public&lt;/span&gt; &lt;span style="color: #008800; font-weight: bold;"&gt;static&lt;/span&gt; &lt;span style="color: #333399; font-weight: bold;"&gt;void&lt;/span&gt; &lt;span style="color: #0066bb; font-weight: bold;"&gt;main&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;String&lt;span style="color: #333333;"&gt;[]&lt;/span&gt; args&lt;span style="color: #333333;"&gt;)&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;

  SumOfTwoLL obj &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="color: #008800; font-weight: bold;"&gt;new&lt;/span&gt; SumOfTwoLL&lt;span style="color: #333333;"&gt;();&lt;/span&gt;
  
  &lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; val_1&lt;span style="color: #333333;"&gt;[]&lt;/span&gt; &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="color: #008800; font-weight: bold;"&gt;new&lt;/span&gt; &lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt;&lt;span style="color: #333333;"&gt;[]&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;       &lt;span style="color: #0000dd; font-weight: bold;"&gt;7&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;6&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;5&lt;/span&gt; &lt;span style="color: #333333;"&gt;};&lt;/span&gt;
  &lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; val_2&lt;span style="color: #333333;"&gt;[]&lt;/span&gt; &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="color: #008800; font-weight: bold;"&gt;new&lt;/span&gt; &lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt;&lt;span style="color: #333333;"&gt;[]&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;9&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;6&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;4&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;1&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;6&lt;/span&gt; &lt;span style="color: #333333;"&gt;};&lt;/span&gt;

  &lt;span style="color: #888888;"&gt;// Create linked list out of arrays&lt;/span&gt;
  NODE first &lt;span style="color: #333333;"&gt;=&lt;/span&gt; obj&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;createLinkedList&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;val_1&lt;span style="color: #333333;"&gt;);&lt;/span&gt;
  NODE second &lt;span style="color: #333333;"&gt;=&lt;/span&gt; obj&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;createLinkedList&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;val_2&lt;span style="color: #333333;"&gt;);&lt;/span&gt;
  
  &lt;span style="color: #888888;"&gt;// Check no. of nodes in both Linked list&lt;/span&gt;
  &lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; t1 &lt;span style="color: #333333;"&gt;=&lt;/span&gt; obj&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;noOfNodes&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;first&lt;span style="color: #333333;"&gt;);&lt;/span&gt;
  &lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; t2 &lt;span style="color: #333333;"&gt;=&lt;/span&gt; obj&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;noOfNodes&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;second&lt;span style="color: #333333;"&gt;);&lt;/span&gt;
  
  &lt;span style="color: #888888;"&gt;// Adjusting Linked List with leading zero node's to make it equal size&lt;/span&gt;
  &lt;span style="color: #008800; font-weight: bold;"&gt;if&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;t1 &lt;span style="color: #333333;"&gt;&amp;lt;&lt;/span&gt; t2&lt;span style="color: #333333;"&gt;)&lt;/span&gt; first &lt;span style="color: #333333;"&gt;=&lt;/span&gt; obj&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;adjustLinkedList&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;first&lt;span style="color: #333333;"&gt;,&lt;/span&gt; &lt;span style="color: #333333;"&gt;(&lt;/span&gt;t2&lt;span style="color: #333333;"&gt;-&lt;/span&gt;t1&lt;span style="color: #333333;"&gt;));&lt;/span&gt;
  &lt;span style="color: #008800; font-weight: bold;"&gt;else&lt;/span&gt; &lt;span style="color: #0066bb; font-weight: bold;"&gt;if&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;t2 &lt;span style="color: #333333;"&gt;&amp;lt;&lt;/span&gt; t1&lt;span style="color: #333333;"&gt;)&lt;/span&gt; second &lt;span style="color: #333333;"&gt;=&lt;/span&gt; obj&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;adjustLinkedList&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;second&lt;span style="color: #333333;"&gt;,&lt;/span&gt; &lt;span style="color: #333333;"&gt;(&lt;/span&gt;t1&lt;span style="color: #333333;"&gt;-&lt;/span&gt;t2&lt;span style="color: #333333;"&gt;));&lt;/span&gt;
  
  &lt;span style="color: #888888;"&gt;// add method call&lt;/span&gt;
  NODE third &lt;span style="color: #333333;"&gt;=&lt;/span&gt; obj&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;addTwoLL&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;first&lt;span style="color: #333333;"&gt;,&lt;/span&gt; second&lt;span style="color: #333333;"&gt;,&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;0&lt;/span&gt;&lt;span style="color: #333333;"&gt;);&lt;/span&gt;
  
  obj&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;print&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;third&lt;span style="color: #333333;"&gt;);&lt;/span&gt;
  
 &lt;span style="color: #333333;"&gt;}&lt;/span&gt;
 
 &lt;span style="color: #888888;"&gt;// Method to get sum of 2 Linked List&lt;/span&gt;
 &lt;span style="color: #008800; font-weight: bold;"&gt;private&lt;/span&gt; NODE &lt;span style="color: #0066bb; font-weight: bold;"&gt;addTwoLL&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;NODE first&lt;span style="color: #333333;"&gt;,&lt;/span&gt; NODE second&lt;span style="color: #333333;"&gt;,&lt;/span&gt; &lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; cnt&lt;span style="color: #333333;"&gt;)&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;
  
  &lt;span style="color: #008800; font-weight: bold;"&gt;if&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;first &lt;span style="color: #333333;"&gt;!=&lt;/span&gt; &lt;span style="color: #008800; font-weight: bold;"&gt;null&lt;/span&gt; &lt;span style="color: #333333;"&gt;&amp;amp;&amp;amp;&lt;/span&gt; second &lt;span style="color: #333333;"&gt;!=&lt;/span&gt; &lt;span style="color: #008800; font-weight: bold;"&gt;null&lt;/span&gt;&lt;span style="color: #333333;"&gt;){&lt;/span&gt;
   
   &lt;span style="color: #888888;"&gt;// Recursive call moving to last node &lt;/span&gt;
   NODE node &lt;span style="color: #333333;"&gt;=&lt;/span&gt; addTwoLL&lt;span style="color: #333333;"&gt;(&lt;/span&gt;first&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;next&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt; second&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;next&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt; cnt&lt;span style="color: #333333;"&gt;+&lt;/span&gt;&lt;span style="color: #0000dd; font-weight: bold;"&gt;1&lt;/span&gt;&lt;span style="color: #333333;"&gt;);&lt;/span&gt;
   
   &lt;span style="color: #888888;"&gt;// Calculating sum from last 2 values &lt;/span&gt;
   &lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; val &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="color: #333333;"&gt;(&lt;/span&gt;first&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;data&lt;/span&gt; &lt;span style="color: #333333;"&gt;+&lt;/span&gt; second&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;data&lt;/span&gt;&lt;span style="color: #333333;"&gt;)&lt;/span&gt; &lt;span style="color: #333333;"&gt;+&lt;/span&gt;node&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;data&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
   
   &lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; carry &lt;span style="color: #333333;"&gt;=&lt;/span&gt; val&lt;span style="color: #333333;"&gt;/&lt;/span&gt;&lt;span style="color: #0000dd; font-weight: bold;"&gt;10&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
   &lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; sum &lt;span style="color: #333333;"&gt;=&lt;/span&gt; val&lt;span style="color: #333333;"&gt;%&lt;/span&gt;&lt;span style="color: #0000dd; font-weight: bold;"&gt;10&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
   
   &lt;span style="color: #888888;"&gt;// If carry present or not the first node in list then create a node with carry value&lt;/span&gt;
   &lt;span style="color: #008800; font-weight: bold;"&gt;if&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;cnt &lt;span style="color: #333333;"&gt;!=&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;0&lt;/span&gt; &lt;span style="color: #333333;"&gt;||&lt;/span&gt; carry &lt;span style="color: #333333;"&gt;&amp;gt;&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;0&lt;/span&gt;&lt;span style="color: #333333;"&gt;)&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;
   
    NODE carryNode &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="color: #008800; font-weight: bold;"&gt;new&lt;/span&gt; NODE&lt;span style="color: #333333;"&gt;(&lt;/span&gt;carry&lt;span style="color: #333333;"&gt;);&lt;/span&gt;
    node&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;data&lt;/span&gt; &lt;span style="color: #333333;"&gt;=&lt;/span&gt; sum&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
    carryNode&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;next&lt;/span&gt; &lt;span style="color: #333333;"&gt;=&lt;/span&gt; node&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
    &lt;span style="color: #008800; font-weight: bold;"&gt;return&lt;/span&gt; carryNode&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
   
   &lt;span style="color: #333333;"&gt;}&lt;/span&gt;&lt;span style="color: #008800; font-weight: bold;"&gt;else&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;
    &lt;span style="color: #888888;"&gt;// Assign final sum to the previous cycle carry node&lt;/span&gt;
    node&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;data&lt;/span&gt; &lt;span style="color: #333333;"&gt;=&lt;/span&gt; sum&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
    &lt;span style="color: #008800; font-weight: bold;"&gt;return&lt;/span&gt; node&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
   &lt;span style="color: #333333;"&gt;}&lt;/span&gt;
  &lt;span style="color: #333333;"&gt;}&lt;/span&gt;
  &lt;span style="color: #888888;"&gt;// Create last carry node with zero&lt;/span&gt;
  &lt;span style="color: #008800; font-weight: bold;"&gt;return&lt;/span&gt; &lt;span style="color: #008800; font-weight: bold;"&gt;new&lt;/span&gt; &lt;span style="color: #0066bb; font-weight: bold;"&gt;NODE&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;&lt;span style="color: #0000dd; font-weight: bold;"&gt;0&lt;/span&gt;&lt;span style="color: #333333;"&gt;);&lt;/span&gt;
 &lt;span style="color: #333333;"&gt;}&lt;/span&gt;
 

 &lt;span style="color: #008800; font-weight: bold;"&gt;private&lt;/span&gt; NODE &lt;span style="color: #0066bb; font-weight: bold;"&gt;adjustLinkedList&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;NODE ll&lt;span style="color: #333333;"&gt;,&lt;/span&gt; &lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; noOfNodesToAdd&lt;span style="color: #333333;"&gt;){&lt;/span&gt;
  &lt;span style="color: #008800; font-weight: bold;"&gt;while&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;noOfNodesToAdd &lt;span style="color: #333333;"&gt;&amp;gt;&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;0&lt;/span&gt;&lt;span style="color: #333333;"&gt;){&lt;/span&gt;
   NODE tmp &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="color: #008800; font-weight: bold;"&gt;new&lt;/span&gt; NODE&lt;span style="color: #333333;"&gt;(&lt;/span&gt;&lt;span style="color: #0000dd; font-weight: bold;"&gt;0&lt;/span&gt;&lt;span style="color: #333333;"&gt;);&lt;/span&gt;
   tmp&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;next&lt;/span&gt; &lt;span style="color: #333333;"&gt;=&lt;/span&gt; ll&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
   ll &lt;span style="color: #333333;"&gt;=&lt;/span&gt; tmp&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
   noOfNodesToAdd&lt;span style="color: #333333;"&gt;--;&lt;/span&gt;
  &lt;span style="color: #333333;"&gt;}&lt;/span&gt;
  &lt;span style="color: #008800; font-weight: bold;"&gt;return&lt;/span&gt; ll&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
 &lt;span style="color: #333333;"&gt;}&lt;/span&gt;
 
 
 &lt;span style="color: #008800; font-weight: bold;"&gt;private&lt;/span&gt; &lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; &lt;span style="color: #0066bb; font-weight: bold;"&gt;noOfNodes&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;NODE ll&lt;span style="color: #333333;"&gt;){&lt;/span&gt;
  &lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; count &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;0&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
  &lt;span style="color: #008800; font-weight: bold;"&gt;while&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;ll &lt;span style="color: #333333;"&gt;!=&lt;/span&gt;  &lt;span style="color: #008800; font-weight: bold;"&gt;null&lt;/span&gt;&lt;span style="color: #333333;"&gt;){&lt;/span&gt;
   count &lt;span style="color: #333333;"&gt;++;&lt;/span&gt;
   ll &lt;span style="color: #333333;"&gt;=&lt;/span&gt; ll&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;next&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
  &lt;span style="color: #333333;"&gt;}&lt;/span&gt;
  &lt;span style="color: #008800; font-weight: bold;"&gt;return&lt;/span&gt; count&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
  
 &lt;span style="color: #333333;"&gt;}&lt;/span&gt;
 
 &lt;span style="color: #008800; font-weight: bold;"&gt;public&lt;/span&gt; NODE &lt;span style="color: #0066bb; font-weight: bold;"&gt;createLinkedList&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;&lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; val&lt;span style="color: #333333;"&gt;[])&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;
  NODE start &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="color: #008800; font-weight: bold;"&gt;null&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
  &lt;span style="color: #008800; font-weight: bold;"&gt;for&lt;/span&gt; &lt;span style="color: #333333;"&gt;(&lt;/span&gt;&lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; i &lt;span style="color: #333333;"&gt;:&lt;/span&gt; val&lt;span style="color: #333333;"&gt;)&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;
   NODE tmp &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="color: #008800; font-weight: bold;"&gt;new&lt;/span&gt; NODE&lt;span style="color: #333333;"&gt;(&lt;/span&gt;i&lt;span style="color: #333333;"&gt;);&lt;/span&gt;
   &lt;span style="color: #008800; font-weight: bold;"&gt;if&lt;/span&gt; &lt;span style="color: #333333;"&gt;(&lt;/span&gt;start &lt;span style="color: #333333;"&gt;==&lt;/span&gt; &lt;span style="color: #008800; font-weight: bold;"&gt;null&lt;/span&gt;&lt;span style="color: #333333;"&gt;)&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;
    start &lt;span style="color: #333333;"&gt;=&lt;/span&gt; tmp&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
   &lt;span style="color: #333333;"&gt;}&lt;/span&gt; &lt;span style="color: #008800; font-weight: bold;"&gt;else&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;
    NODE mover &lt;span style="color: #333333;"&gt;=&lt;/span&gt; start&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
    &lt;span style="color: #008800; font-weight: bold;"&gt;while&lt;/span&gt; &lt;span style="color: #333333;"&gt;(&lt;/span&gt;mover&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;next&lt;/span&gt; &lt;span style="color: #333333;"&gt;!=&lt;/span&gt; &lt;span style="color: #008800; font-weight: bold;"&gt;null&lt;/span&gt;&lt;span style="color: #333333;"&gt;)&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;
     mover &lt;span style="color: #333333;"&gt;=&lt;/span&gt; mover&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;next&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
    &lt;span style="color: #333333;"&gt;}&lt;/span&gt;
    mover&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;next&lt;/span&gt; &lt;span style="color: #333333;"&gt;=&lt;/span&gt; tmp&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
   &lt;span style="color: #333333;"&gt;}&lt;/span&gt;
  &lt;span style="color: #333333;"&gt;}&lt;/span&gt;
  &lt;span style="color: #008800; font-weight: bold;"&gt;return&lt;/span&gt; start&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
 &lt;span style="color: #333333;"&gt;}&lt;/span&gt;
 

 &lt;span style="color: #008800; font-weight: bold;"&gt;public&lt;/span&gt; &lt;span style="color: #333399; font-weight: bold;"&gt;void&lt;/span&gt; &lt;span style="color: #0066bb; font-weight: bold;"&gt;print&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;NODE tmp&lt;span style="color: #333333;"&gt;){&lt;/span&gt;
  &lt;span style="color: #008800; font-weight: bold;"&gt;while&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;tmp &lt;span style="color: #333333;"&gt;!=&lt;/span&gt; &lt;span style="color: #008800; font-weight: bold;"&gt;null&lt;/span&gt; &lt;span style="color: #333333;"&gt;){&lt;/span&gt;
   System&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;out&lt;/span&gt;&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;print&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;tmp&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;data&lt;/span&gt; &lt;span style="color: #333333;"&gt;+&lt;/span&gt;&lt;span style="background-color: #fff0f0;"&gt;", "&lt;/span&gt;&lt;span style="color: #333333;"&gt;);&lt;/span&gt;
   tmp &lt;span style="color: #333333;"&gt;=&lt;/span&gt; tmp&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;next&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
  &lt;span style="color: #333333;"&gt;}&lt;/span&gt;
 &lt;span style="color: #333333;"&gt;}&lt;/span&gt;
&lt;span style="color: #333333;"&gt;}&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;OUTPUT:&lt;/b&gt;&lt;br /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;"&gt;
&lt;pre style="line-height: 125%; margin: 0;"&gt;&lt;span style="color: #0000dd; font-weight: bold;"&gt;9&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;7&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;1&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;8&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;1&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt; 
&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;style type="text/css"&gt;
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco}
&lt;/style&gt;


&lt;/div&gt;
</content><link href="https://www.blogger.com/feeds/6801225484322010450/posts/default/6721605222102368794" rel="edit" type="application/atom+xml"/><link href="https://www.blogger.com/feeds/6801225484322010450/posts/default/6721605222102368794" rel="self" type="application/atom+xml"/><link href="https://javadiscover.blogspot.com/2019/02/add-two-numbers-represented-by-linked.html" rel="alternate" title="Add two numbers represented by linked lists using recursive" type="text/html"/><author><name>Java Discover</name><uri>http://www.blogger.com/profile/18049511268651382259</uri><email>noreply@blogger.com</email><gd:image height="16" rel="http://schemas.google.com/g/2005#thumbnail" src="https://img1.blogblog.com/img/b16-rounded.gif" width="16"/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" height="72" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj0IbyIRaq21gQgYGukh4pQ8BHL6au8BUjfIfEcHVvS5h3AwGjSX3ILB9aqw4zPFarK1ml59vGO2iro0lVjEi8NMZM-lq_eSKUnmlBOSk8c_VUFKyT3xm8RCrc5WwNXlcpdhMh8kEhN5AE/s72-c/Screenshot+2019-02-07+at+2.49.41+PM.png" width="72"/></entry><entry><id>tag:blogger.com,1999:blog-6801225484322010450.post-3548945147390937176</id><published>2019-01-17T11:24:00.000+05:30</published><updated>2019-01-17T11:29:37.169+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Core Java"/><category scheme="http://www.blogger.com/atom/ns#" term="Interview Questions"/><category scheme="http://www.blogger.com/atom/ns#" term="recursion"/><category scheme="http://www.blogger.com/atom/ns#" term="Reverse words in a given String using recursion"/><title type="text">Reverse words in a given String using recursion</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
Reverse words in a given String using recursion. We need to reverse words by words in a given line and not the character in a given string. We need to solve this by using backtracking and recursion logic.&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhhUMAiERUan6bk2fow4UblCjWYolSKLpXukB27hvXgMNWeu_fCiHUydPMM1md2k-Sz5i_oMnP6qXm2mvporYpUdve5GlXcTvXgUqZy1gPQcaPCElIo9hvYXmMCf63wrClxa6zjBvv3F3o/s1600/Screenshot+2019-01-17+at+11.28.15+AM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="Reverse words in a given String using recursion" border="0" data-original-height="198" data-original-width="290" height="136" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhhUMAiERUan6bk2fow4UblCjWYolSKLpXukB27hvXgMNWeu_fCiHUydPMM1md2k-Sz5i_oMnP6qXm2mvporYpUdve5GlXcTvXgUqZy1gPQcaPCElIo9hvYXmMCf63wrClxa6zjBvv3F3o/s200/Screenshot+2019-01-17+at+11.28.15+AM.png" title="Reverse words in a given String using recursion" width="200" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Let's see simple Java code for getting the given of words in a reverse order by using recursion.&lt;br /&gt;
&lt;br /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;"&gt;
&lt;pre style="line-height: 125%; margin: 0;"&gt;&lt;span style="color: #008800; font-weight: bold;"&gt;public&lt;/span&gt; &lt;span style="color: #008800; font-weight: bold;"&gt;class&lt;/span&gt; &lt;span style="color: #bb0066; font-weight: bold;"&gt;ReverseLineByWords&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;

 &lt;span style="color: #008800; font-weight: bold;"&gt;public&lt;/span&gt; &lt;span style="color: #008800; font-weight: bold;"&gt;static&lt;/span&gt; &lt;span style="color: #333399; font-weight: bold;"&gt;void&lt;/span&gt; &lt;span style="color: #0066bb; font-weight: bold;"&gt;main&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;String&lt;span style="color: #333333;"&gt;[]&lt;/span&gt; args&lt;span style="color: #333333;"&gt;)&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;
  
  String str &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="background-color: #fff0f0;"&gt;"Hello Java Discover"&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
  
  String finalStr &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="color: #008800; font-weight: bold;"&gt;new&lt;/span&gt; ReverseLineByWords&lt;span style="color: #333333;"&gt;().&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;reverseLine&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;str&lt;span style="color: #333333;"&gt;);&lt;/span&gt;
  
  System&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;out&lt;/span&gt;&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;println&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;finalStr&lt;span style="color: #333333;"&gt;);&lt;/span&gt;
  
 &lt;span style="color: #333333;"&gt;}&lt;/span&gt;
 
 &lt;span style="color: #008800; font-weight: bold;"&gt;public&lt;/span&gt; String &lt;span style="color: #0066bb; font-weight: bold;"&gt;reverseLine&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;String str&lt;span style="color: #333333;"&gt;){&lt;/span&gt;
  String word &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="background-color: #fff0f0;"&gt;""&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
  &lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; i &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;0&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
  &lt;span style="color: #008800; font-weight: bold;"&gt;for&lt;/span&gt;&lt;span style="color: #333333;"&gt;(;&lt;/span&gt;i&lt;span style="color: #333333;"&gt;&amp;lt;&lt;/span&gt;str&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;length&lt;/span&gt;&lt;span style="color: #333333;"&gt;();&lt;/span&gt;i&lt;span style="color: #333333;"&gt;++){&lt;/span&gt;
   &lt;span style="color: #008800; font-weight: bold;"&gt;if&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;str&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;charAt&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;i&lt;span style="color: #333333;"&gt;)&lt;/span&gt; &lt;span style="color: #333333;"&gt;==&lt;/span&gt; &lt;span style="color: #0044dd;"&gt;' '&lt;/span&gt;&lt;span style="color: #333333;"&gt;)&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;
    word &lt;span style="color: #333333;"&gt;=&lt;/span&gt; word &lt;span style="color: #333333;"&gt;+&lt;/span&gt; &lt;span style="background-color: #fff0f0;"&gt;" "&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
    &lt;span style="color: #008800; font-weight: bold;"&gt;break&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
   &lt;span style="color: #333333;"&gt;}&lt;/span&gt;&lt;span style="color: #008800; font-weight: bold;"&gt;else&lt;/span&gt;&lt;span style="color: #333333;"&gt;{&lt;/span&gt;
    word &lt;span style="color: #333333;"&gt;=&lt;/span&gt; word &lt;span style="color: #333333;"&gt;+&lt;/span&gt; str&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;charAt&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;i&lt;span style="color: #333333;"&gt;);&lt;/span&gt;
   &lt;span style="color: #333333;"&gt;}&lt;/span&gt;
  &lt;span style="color: #333333;"&gt;}&lt;/span&gt;

  &lt;span style="color: #008800; font-weight: bold;"&gt;if&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;i &lt;span style="color: #333333;"&gt;&amp;lt;&lt;/span&gt; str&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;length&lt;/span&gt;&lt;span style="color: #333333;"&gt;())&lt;/span&gt; 
   &lt;span style="color: #008800; font-weight: bold;"&gt;return&lt;/span&gt; &lt;span style="color: #0066bb; font-weight: bold;"&gt;reverseLine&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;str&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;substring&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;i&lt;span style="color: #333333;"&gt;+&lt;/span&gt;&lt;span style="color: #0000dd; font-weight: bold;"&gt;1&lt;/span&gt;&lt;span style="color: #333333;"&gt;))&lt;/span&gt; &lt;span style="color: #333333;"&gt;+&lt;/span&gt; word&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
  
  &lt;span style="color: #008800; font-weight: bold;"&gt;return&lt;/span&gt; &lt;span style="color: #333333;"&gt;(&lt;/span&gt;word &lt;span style="color: #333333;"&gt;+&lt;/span&gt; &lt;span style="background-color: #fff0f0;"&gt;" "&lt;/span&gt;&lt;span style="color: #333333;"&gt;);&lt;/span&gt;
 &lt;span style="color: #333333;"&gt;}&lt;/span&gt;
&lt;span style="color: #333333;"&gt;}&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;OUTPUT:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;"&gt;
&lt;pre style="line-height: 125%; margin: 0;"&gt;Discover Java Hello 
&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;style type="text/css"&gt;
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco; color: #2a00ff}
&lt;/style&gt;&lt;br /&gt;
&lt;style type="text/css"&gt;
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco}
&lt;/style&gt;&lt;/div&gt;
</content><link href="https://www.blogger.com/feeds/6801225484322010450/posts/default/3548945147390937176" rel="edit" type="application/atom+xml"/><link href="https://www.blogger.com/feeds/6801225484322010450/posts/default/3548945147390937176" rel="self" type="application/atom+xml"/><link href="https://javadiscover.blogspot.com/2019/01/reverse-words-in-given-string-using.html" rel="alternate" title="Reverse words in a given String using recursion" type="text/html"/><author><name>Java Discover</name><uri>http://www.blogger.com/profile/18049511268651382259</uri><email>noreply@blogger.com</email><gd:image height="16" rel="http://schemas.google.com/g/2005#thumbnail" src="https://img1.blogblog.com/img/b16-rounded.gif" width="16"/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" height="72" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhhUMAiERUan6bk2fow4UblCjWYolSKLpXukB27hvXgMNWeu_fCiHUydPMM1md2k-Sz5i_oMnP6qXm2mvporYpUdve5GlXcTvXgUqZy1gPQcaPCElIo9hvYXmMCf63wrClxa6zjBvv3F3o/s72-c/Screenshot+2019-01-17+at+11.28.15+AM.png" width="72"/></entry><entry><id>tag:blogger.com,1999:blog-6801225484322010450.post-3124877667102876613</id><published>2019-01-08T06:18:00.001+05:30</published><updated>2019-01-08T06:48:21.130+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Core Java"/><category scheme="http://www.blogger.com/atom/ns#" term="Interview Questions"/><category scheme="http://www.blogger.com/atom/ns#" term="rotate matrix"/><category scheme="http://www.blogger.com/atom/ns#" term="Rotate matrix by clockwise"/><title type="text">Rotate matrix by clockwise</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
Given a NxN matrix and need to rotate by clockwise as given below&lt;br /&gt;
&lt;br /&gt;
Examples:&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi1gn6EXRip2oFMgl6aw_DivSMI6MyxzLx3kTL0fkvUmU6aY1n7EopK8h0pOxMSIjL8m6xSU9wG2NeSTBJi8fZKZT3rwn1vaJfwEeZ4CuY1OGFugBUxed9E0B-m-r4VmDIP0MSiWMqSGkk/s1600/Screenshot+2019-01-08+at+6.14.14+AM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="Rotate matrix by clockwise" border="0" data-original-height="570" data-original-width="562" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi1gn6EXRip2oFMgl6aw_DivSMI6MyxzLx3kTL0fkvUmU6aY1n7EopK8h0pOxMSIjL8m6xSU9wG2NeSTBJi8fZKZT3rwn1vaJfwEeZ4CuY1OGFugBUxed9E0B-m-r4VmDIP0MSiWMqSGkk/s320/Screenshot+2019-01-08+at+6.14.14+AM.png" title="Rotate matrix by clockwise" width="312" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class="p2"&gt;
&lt;span class="Apple-converted-space"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="p2"&gt;
&lt;br /&gt;&lt;/div&gt;
Let's see simple java code to rotate given NxN matrix.&lt;br /&gt;
&lt;br /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;"&gt;
&lt;pre style="line-height: 125%; margin: 0;"&gt;&lt;span style="color: #008800; font-weight: bold;"&gt;public&lt;/span&gt; &lt;span style="color: #008800; font-weight: bold;"&gt;class&lt;/span&gt; &lt;span style="color: #bb0066; font-weight: bold;"&gt;MatrixRotate&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;

 &lt;span style="color: #008800; font-weight: bold;"&gt;private&lt;/span&gt; &lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; prev&lt;span style="color: #333333;"&gt;,&lt;/span&gt; curr&lt;span style="color: #333333;"&gt;;&lt;/span&gt;

 &lt;span style="color: #008800; font-weight: bold;"&gt;public&lt;/span&gt; &lt;span style="color: #008800; font-weight: bold;"&gt;static&lt;/span&gt; &lt;span style="color: #333399; font-weight: bold;"&gt;void&lt;/span&gt; &lt;span style="color: #0066bb; font-weight: bold;"&gt;main&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;String&lt;span style="color: #333333;"&gt;[]&lt;/span&gt; args&lt;span style="color: #333333;"&gt;)&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;

  &lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt;&lt;span style="color: #333333;"&gt;[][]&lt;/span&gt; matrix &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="color: #008800; font-weight: bold;"&gt;new&lt;/span&gt; &lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt;&lt;span style="color: #333333;"&gt;[][]&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;1&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;2&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;3&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;4&lt;/span&gt; &lt;span style="color: #333333;"&gt;},&lt;/span&gt; 
                   &lt;span style="color: #333333;"&gt;{&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;5&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;6&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;7&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;8&lt;/span&gt; &lt;span style="color: #333333;"&gt;},&lt;/span&gt; 
            &lt;span style="color: #333333;"&gt;{&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;9&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;10&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;11&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;12&lt;/span&gt; &lt;span style="color: #333333;"&gt;},&lt;/span&gt; 
            &lt;span style="color: #333333;"&gt;{&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;13&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;14&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;15&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;16&lt;/span&gt; &lt;span style="color: #333333;"&gt;}&lt;/span&gt; &lt;span style="color: #333333;"&gt;};&lt;/span&gt;
  

  MatrixRotate obj &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="color: #008800; font-weight: bold;"&gt;new&lt;/span&gt; MatrixRotate&lt;span style="color: #333333;"&gt;();&lt;/span&gt;
  obj&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;rotateMatrix&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;matrix&lt;span style="color: #333333;"&gt;);&lt;/span&gt;

  &lt;span style="color: #008800; font-weight: bold;"&gt;for&lt;/span&gt; &lt;span style="color: #333333;"&gt;(&lt;/span&gt;&lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; i &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;0&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt; i &lt;span style="color: #333333;"&gt;&amp;lt;&lt;/span&gt; matrix&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;length&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt; i&lt;span style="color: #333333;"&gt;++)&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;
   System&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;out&lt;/span&gt;&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;println&lt;/span&gt;&lt;span style="color: #333333;"&gt;();&lt;/span&gt;
   &lt;span style="color: #008800; font-weight: bold;"&gt;for&lt;/span&gt; &lt;span style="color: #333333;"&gt;(&lt;/span&gt;&lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; j &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;0&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt; j &lt;span style="color: #333333;"&gt;&amp;lt;&lt;/span&gt; matrix&lt;span style="color: #333333;"&gt;[&lt;/span&gt;&lt;span style="color: #0000dd; font-weight: bold;"&gt;0&lt;/span&gt;&lt;span style="color: #333333;"&gt;].&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;length&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt; j&lt;span style="color: #333333;"&gt;++)&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;
    System&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;out&lt;/span&gt;&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;print&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;matrix&lt;span style="color: #333333;"&gt;[&lt;/span&gt;i&lt;span style="color: #333333;"&gt;][&lt;/span&gt;j&lt;span style="color: #333333;"&gt;]&lt;/span&gt; &lt;span style="color: #333333;"&gt;+&lt;/span&gt; &lt;span style="background-color: #fff0f0;"&gt;", "&lt;/span&gt;&lt;span style="color: #333333;"&gt;);&lt;/span&gt;
   &lt;span style="color: #333333;"&gt;}&lt;/span&gt;
  &lt;span style="color: #333333;"&gt;}&lt;/span&gt;
 &lt;span style="color: #333333;"&gt;}&lt;/span&gt;

 &lt;span style="color: #008800; font-weight: bold;"&gt;public&lt;/span&gt; &lt;span style="color: #333399; font-weight: bold;"&gt;void&lt;/span&gt; &lt;span style="color: #0066bb; font-weight: bold;"&gt;rotateMatrix&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;&lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt;&lt;span style="color: #333333;"&gt;[][]&lt;/span&gt; matrix&lt;span style="color: #333333;"&gt;)&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;

  &lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; r &lt;span style="color: #333333;"&gt;=&lt;/span&gt; matrix&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;length&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
  &lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; c &lt;span style="color: #333333;"&gt;=&lt;/span&gt; matrix&lt;span style="color: #333333;"&gt;[&lt;/span&gt;&lt;span style="color: #0000dd; font-weight: bold;"&gt;0&lt;/span&gt;&lt;span style="color: #333333;"&gt;].&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;length&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
  &lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; row &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;0&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt; col &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;0&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt;

  &lt;span style="color: #008800; font-weight: bold;"&gt;for&lt;/span&gt; &lt;span style="color: #333333;"&gt;(&lt;/span&gt;&lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; cnt &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;0&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt;cnt&lt;span style="color: #333333;"&gt;&amp;lt;(&lt;/span&gt;r&lt;span style="color: #333333;"&gt;*&lt;/span&gt;c&lt;span style="color: #333333;"&gt;);&lt;/span&gt;cnt&lt;span style="color: #333333;"&gt;++)&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;
   
   prev &lt;span style="color: #333333;"&gt;=&lt;/span&gt; matrix&lt;span style="color: #333333;"&gt;[&lt;/span&gt;row &lt;span style="color: #333333;"&gt;+&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;1&lt;/span&gt;&lt;span style="color: #333333;"&gt;][&lt;/span&gt;col&lt;span style="color: #333333;"&gt;];&lt;/span&gt;

   &lt;span style="color: #008800; font-weight: bold;"&gt;for&lt;/span&gt; &lt;span style="color: #333333;"&gt;(&lt;/span&gt;&lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; i &lt;span style="color: #333333;"&gt;=&lt;/span&gt; col&lt;span style="color: #333333;"&gt;;&lt;/span&gt; i &lt;span style="color: #333333;"&gt;&amp;lt;&lt;/span&gt; c&lt;span style="color: #333333;"&gt;;&lt;/span&gt; i&lt;span style="color: #333333;"&gt;++)&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;
    swap&lt;span style="color: #333333;"&gt;(&lt;/span&gt;row&lt;span style="color: #333333;"&gt;,&lt;/span&gt; i&lt;span style="color: #333333;"&gt;,&lt;/span&gt; matrix&lt;span style="color: #333333;"&gt;); cnt++;&lt;/span&gt;&lt;span style="color: #333333;"&gt;   &lt;/span&gt;&lt;span style="color: #333333;"&gt;}&lt;/span&gt;
   row&lt;span style="color: #333333;"&gt;++;&lt;/span&gt;

   &lt;span style="color: #008800; font-weight: bold;"&gt;for&lt;/span&gt; &lt;span style="color: #333333;"&gt;(&lt;/span&gt;&lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; i &lt;span style="color: #333333;"&gt;=&lt;/span&gt; row&lt;span style="color: #333333;"&gt;;&lt;/span&gt; i &lt;span style="color: #333333;"&gt;&amp;lt;&lt;/span&gt; r&lt;span style="color: #333333;"&gt;;&lt;/span&gt; i&lt;span style="color: #333333;"&gt;++)&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;
    swap&lt;span style="color: #333333;"&gt;(&lt;/span&gt;i&lt;span style="color: #333333;"&gt;,&lt;/span&gt; c &lt;span style="color: #333333;"&gt;-&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;1&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt; matrix&lt;span style="color: #333333;"&gt;); cnt++;&lt;/span&gt;   &lt;span style="color: #333333;"&gt;}&lt;/span&gt;
   c&lt;span style="color: #333333;"&gt;--;&lt;/span&gt;

   &lt;span style="color: #008800; font-weight: bold;"&gt;if&lt;/span&gt; &lt;span style="color: #333333;"&gt;(&lt;/span&gt;row &lt;span style="color: #333333;"&gt;&amp;lt;&lt;/span&gt; r&lt;span style="color: #333333;"&gt;)&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;
    &lt;span style="color: #008800; font-weight: bold;"&gt;for&lt;/span&gt; &lt;span style="color: #333333;"&gt;(&lt;/span&gt;&lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; i &lt;span style="color: #333333;"&gt;=&lt;/span&gt; c &lt;span style="color: #333333;"&gt;-&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;1&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt; i &lt;span style="color: #333333;"&gt;&amp;gt;=&lt;/span&gt; col&lt;span style="color: #333333;"&gt;;&lt;/span&gt; i&lt;span style="color: #333333;"&gt;--)&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;
     swap&lt;span style="color: #333333;"&gt;(&lt;/span&gt;r &lt;span style="color: #333333;"&gt;-&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;1&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt; i&lt;span style="color: #333333;"&gt;,&lt;/span&gt; matrix&lt;span style="color: #333333;"&gt;); cnt++;&lt;/span&gt;
    &lt;span style="color: #333333;"&gt;}&lt;/span&gt;
   &lt;span style="color: #333333;"&gt;}&lt;/span&gt;
   r&lt;span style="color: #333333;"&gt;--;&lt;/span&gt;

   &lt;span style="color: #008800; font-weight: bold;"&gt;if&lt;/span&gt; &lt;span style="color: #333333;"&gt;(&lt;/span&gt;col &lt;span style="color: #333333;"&gt;&amp;lt;&lt;/span&gt; c&lt;span style="color: #333333;"&gt;)&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;
    &lt;span style="color: #008800; font-weight: bold;"&gt;for&lt;/span&gt; &lt;span style="color: #333333;"&gt;(&lt;/span&gt;&lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; i &lt;span style="color: #333333;"&gt;=&lt;/span&gt; r &lt;span style="color: #333333;"&gt;-&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;1&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt; i &lt;span style="color: #333333;"&gt;&amp;gt;=&lt;/span&gt; row&lt;span style="color: #333333;"&gt;;&lt;/span&gt; i&lt;span style="color: #333333;"&gt;--)&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;
     swap&lt;span style="color: #333333;"&gt;(&lt;/span&gt;i&lt;span style="color: #333333;"&gt;,&lt;/span&gt; col&lt;span style="color: #333333;"&gt;,&lt;/span&gt; matrix&lt;span style="color: #333333;"&gt;); cnt++;&lt;/span&gt;
    &lt;span style="color: #333333;"&gt;}&lt;/span&gt;
   &lt;span style="color: #333333;"&gt;}&lt;/span&gt;
   col&lt;span style="color: #333333;"&gt;++;&lt;/span&gt;
  &lt;span style="color: #333333;"&gt;}&lt;/span&gt;
 &lt;span style="color: #333333;"&gt;}&lt;/span&gt;

 &lt;span style="color: #008800; font-weight: bold;"&gt;public&lt;/span&gt; &lt;span style="color: #333399; font-weight: bold;"&gt;void&lt;/span&gt; &lt;span style="color: #0066bb; font-weight: bold;"&gt;swap&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;&lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; i&lt;span style="color: #333333;"&gt;,&lt;/span&gt; &lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; j&lt;span style="color: #333333;"&gt;,&lt;/span&gt; &lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; matrix&lt;span style="color: #333333;"&gt;[][])&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;
  curr &lt;span style="color: #333333;"&gt;=&lt;/span&gt; matrix&lt;span style="color: #333333;"&gt;[&lt;/span&gt;i&lt;span style="color: #333333;"&gt;][&lt;/span&gt;j&lt;span style="color: #333333;"&gt;];&lt;/span&gt;
  matrix&lt;span style="color: #333333;"&gt;[&lt;/span&gt;i&lt;span style="color: #333333;"&gt;][&lt;/span&gt;j&lt;span style="color: #333333;"&gt;]&lt;/span&gt; &lt;span style="color: #333333;"&gt;=&lt;/span&gt; prev&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
  prev &lt;span style="color: #333333;"&gt;=&lt;/span&gt; curr&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
 &lt;span style="color: #333333;"&gt;}&lt;/span&gt;
&lt;span style="color: #333333;"&gt;}&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;OUTPUT:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;"&gt;
&lt;pre style="line-height: 125%; margin: 0;"&gt;&lt;span style="color: #0000dd; font-weight: bold;"&gt; 5&lt;/span&gt;&lt;span style="color: #333333;"&gt;, &lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;1&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt;  &lt;span style="color: #0000dd; font-weight: bold;"&gt;2&lt;/span&gt;&lt;span style="color: #333333;"&gt;, &lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;3&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt; 
&lt;span style="color: #0000dd; font-weight: bold;"&gt; 9&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;10&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt;  &lt;span style="color: #0000dd; font-weight: bold;"&gt;6&lt;/span&gt;&lt;span style="color: #333333;"&gt;, &lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;4&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt; 
&lt;span style="color: #0000dd; font-weight: bold;"&gt;13&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;11&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt;  &lt;span style="color: #0000dd; font-weight: bold;"&gt;7&lt;/span&gt;&lt;span style="color: #333333;"&gt;, &lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;8&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt; 
&lt;span style="color: #0000dd; font-weight: bold;"&gt;14&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;15&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;16&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;12&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt; 
&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;style type="text/css"&gt;
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco}
span.Apple-tab-span {white-space:pre}
&lt;/style&gt;&lt;style type="text/css"&gt;
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco; min-height: 15.0px}
p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco}
&lt;/style&gt;&lt;style type="text/css"&gt;
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco}
span.Apple-tab-span {white-space:pre}
&lt;/style&gt;&lt;style type="text/css"&gt;
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco; min-height: 15.0px}
p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco}
&lt;/style&gt;&lt;/div&gt;
</content><link href="https://www.blogger.com/feeds/6801225484322010450/posts/default/3124877667102876613" rel="edit" type="application/atom+xml"/><link href="https://www.blogger.com/feeds/6801225484322010450/posts/default/3124877667102876613" rel="self" type="application/atom+xml"/><link href="https://javadiscover.blogspot.com/2019/01/rotate-matrix-by-clockwise.html" rel="alternate" title="Rotate matrix by clockwise" type="text/html"/><author><name>Java Discover</name><uri>http://www.blogger.com/profile/18049511268651382259</uri><email>noreply@blogger.com</email><gd:image height="16" rel="http://schemas.google.com/g/2005#thumbnail" src="https://img1.blogblog.com/img/b16-rounded.gif" width="16"/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" height="72" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi1gn6EXRip2oFMgl6aw_DivSMI6MyxzLx3kTL0fkvUmU6aY1n7EopK8h0pOxMSIjL8m6xSU9wG2NeSTBJi8fZKZT3rwn1vaJfwEeZ4CuY1OGFugBUxed9E0B-m-r4VmDIP0MSiWMqSGkk/s72-c/Screenshot+2019-01-08+at+6.14.14+AM.png" width="72"/></entry><entry><id>tag:blogger.com,1999:blog-6801225484322010450.post-4883095046466140556</id><published>2019-01-04T02:32:00.001+05:30</published><updated>2019-01-04T11:28:33.005+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Core Java"/><category scheme="http://www.blogger.com/atom/ns#" term="Find the smallest window in a string containing all characters of another string (With smallest window)"/><category scheme="http://www.blogger.com/atom/ns#" term="Interview Questions"/><category scheme="http://www.blogger.com/atom/ns#" term="Smallest window"/><title type="text">Find the smallest window in a string containing all characters of another string (With smallest window)</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;div style="background-color: white; border: 0px; color: rgba(0, 0, 0, 0.84); font-size: 16px; line-height: 1.71429; margin-bottom: 10px; padding: 0px; vertical-align: baseline;"&gt;
&lt;span style="font-family: &amp;quot;times&amp;quot; , &amp;quot;times new roman&amp;quot; , serif;"&gt;Given two strings string1 and string2, find the smallest substring in string1 containing all characters of string2 efficiently.&lt;br /&gt;&lt;b&gt;For Example:&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgwoe0lQYBXM6tp_B-uodoq6XQbmPFY2nsgrHvWvDo1UgVa9XASJ4airgnQEJqgX16HiuMjQut3-gXbKPQZaT4pf9h3Ahnb8CEiqA9S1CztlNibnVj3syVtcgX71o2veQEeqXGYOZlCtV0/s1600/Screenshot+2019-01-04+at+2.27.44+AM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="Find the smallest window in a string containing all characters of another string (With smallest window)" border="0" data-original-height="429" data-original-width="896" height="190" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgwoe0lQYBXM6tp_B-uodoq6XQbmPFY2nsgrHvWvDo1UgVa9XASJ4airgnQEJqgX16HiuMjQut3-gXbKPQZaT4pf9h3Ahnb8CEiqA9S1CztlNibnVj3syVtcgX71o2veQEeqXGYOZlCtV0/s400/Screenshot+2019-01-04+at+2.27.44+AM.png" title="Find the smallest window in a string containing all characters of another string (With smallest window)" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;span style="font-family: &amp;quot;times&amp;quot; , &amp;quot;times new roman&amp;quot; , serif;"&gt;&lt;b&gt;Steps:&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;Generate all substrings which starts with any one character from the pattern string&lt;/li&gt;
&lt;li&gt;Iterate all substrings which we generated and compare each character from the pattern string with the substring and check for all characters present or not&lt;/li&gt;
&lt;li&gt;If all characters present in substring then trim extra characters and choose the smallest window string&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;b&gt;Java code:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;"&gt;
&lt;pre style="line-height: 125%; margin: 0;"&gt;&lt;span style="color: #008800; font-weight: bold;"&gt;public&lt;/span&gt; &lt;span style="color: #008800; font-weight: bold;"&gt;class&lt;/span&gt; &lt;span style="color: #bb0066; font-weight: bold;"&gt;StringInSmallestWindow&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;

 &lt;span style="color: #008800; font-weight: bold;"&gt;public&lt;/span&gt; &lt;span style="color: #008800; font-weight: bold;"&gt;static&lt;/span&gt; &lt;span style="color: #333399; font-weight: bold;"&gt;void&lt;/span&gt; &lt;span style="color: #0066bb; font-weight: bold;"&gt;main&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;String&lt;span style="color: #333333;"&gt;[]&lt;/span&gt; args&lt;span style="color: #333333;"&gt;)&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;

  String str &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="background-color: #fff0f0;"&gt;"niharika"&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
  String findStr &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="background-color: #fff0f0;"&gt;"ir"&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
  String result &lt;span style="color: #333333;"&gt;=&lt;/span&gt; null&lt;span style="color: #333333;"&gt;;&lt;/span&gt;

  &lt;span style="color: #008800; font-weight: bold;"&gt;if&lt;/span&gt; &lt;span style="color: #333333;"&gt;(&lt;/span&gt;str&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;length&lt;/span&gt;&lt;span style="color: #333333;"&gt;()&lt;/span&gt; &lt;span style="color: #333333;"&gt;&amp;lt;&lt;/span&gt; findStr&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;length&lt;/span&gt;&lt;span style="color: #333333;"&gt;())&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;
   System&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;out&lt;/span&gt;&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;println&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;&lt;span style="background-color: #fff0f0;"&gt;"Pattern string is greater than source string"&lt;/span&gt;&lt;span style="color: #333333;"&gt;);&lt;/span&gt;
   &lt;span style="color: #008800; font-weight: bold;"&gt;return&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
  &lt;span style="color: #333333;"&gt;}&lt;/span&gt;

  &lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; j &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;0&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
  String&lt;span style="color: #333333;"&gt;[]&lt;/span&gt; arr &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="color: #008800; font-weight: bold;"&gt;new&lt;/span&gt; String&lt;span style="color: #333333;"&gt;[&lt;/span&gt;str&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;length&lt;/span&gt;&lt;span style="color: #333333;"&gt;()];&lt;/span&gt;
  &lt;span style="color: #888888;"&gt;// Get all substring which starts with any one character from the&lt;/span&gt;
  &lt;span style="color: #888888;"&gt;// pattern string&lt;/span&gt;
  &lt;span style="color: #008800; font-weight: bold;"&gt;for&lt;/span&gt; &lt;span style="color: #333333;"&gt;(&lt;/span&gt;&lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; i &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;0&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt; i &lt;span style="color: #333333;"&gt;&amp;lt;&lt;/span&gt; str&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;length&lt;/span&gt;&lt;span style="color: #333333;"&gt;();&lt;/span&gt; i&lt;span style="color: #333333;"&gt;++)&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;
   &lt;span style="color: #008800; font-weight: bold;"&gt;if&lt;/span&gt; &lt;span style="color: #333333;"&gt;(&lt;/span&gt;findStr&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;contains&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;str&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;charAt&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;i&lt;span style="color: #333333;"&gt;)&lt;/span&gt; &lt;span style="color: #333333;"&gt;+&lt;/span&gt; &lt;span style="background-color: #fff0f0;"&gt;""&lt;/span&gt;&lt;span style="color: #333333;"&gt;)&lt;/span&gt; &lt;span style="color: #333333;"&gt;&amp;amp;&amp;amp;&lt;/span&gt; str&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;substring&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;i&lt;span style="color: #333333;"&gt;).&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;length&lt;/span&gt;&lt;span style="color: #333333;"&gt;()&lt;/span&gt; &lt;span style="color: #333333;"&gt;&amp;gt;=&lt;/span&gt; findStr&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;length&lt;/span&gt;&lt;span style="color: #333333;"&gt;())&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;
    arr&lt;span style="color: #333333;"&gt;[&lt;/span&gt;j&lt;span style="color: #333333;"&gt;++]&lt;/span&gt; &lt;span style="color: #333333;"&gt;=&lt;/span&gt; str&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;substring&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;i&lt;span style="color: #333333;"&gt;);&lt;/span&gt;
   &lt;span style="color: #333333;"&gt;}&lt;/span&gt;
  &lt;span style="color: #333333;"&gt;}&lt;/span&gt;

  &lt;span style="color: #888888;"&gt;// Iterate all substrings which we generated&lt;/span&gt;
  &lt;span style="color: #008800; font-weight: bold;"&gt;for&lt;/span&gt; &lt;span style="color: #333333;"&gt;(&lt;/span&gt;&lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; pnt &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;0&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt; pnt &lt;span style="color: #333333;"&gt;&amp;lt;&lt;/span&gt; j&lt;span style="color: #333333;"&gt;;&lt;/span&gt; pnt&lt;span style="color: #333333;"&gt;++)&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;

   String string &lt;span style="color: #333333;"&gt;=&lt;/span&gt; arr&lt;span style="color: #333333;"&gt;[&lt;/span&gt;pnt&lt;span style="color: #333333;"&gt;];&lt;/span&gt;
   &lt;span style="color: #333399; font-weight: bold;"&gt;char&lt;/span&gt;&lt;span style="color: #333333;"&gt;[]&lt;/span&gt; tmp &lt;span style="color: #333333;"&gt;=&lt;/span&gt; findStr&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;toCharArray&lt;/span&gt;&lt;span style="color: #333333;"&gt;();&lt;/span&gt;

   &lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; cnt &lt;span style="color: #333333;"&gt;=&lt;/span&gt; tmp&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;length&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
   &lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; lastIndex &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;0&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt;

   &lt;span style="color: #888888;"&gt;// Compare each character from the pattern string with the substring&lt;/span&gt;
   &lt;span style="color: #888888;"&gt;// and check for all characters present or not&lt;/span&gt;
   &lt;span style="color: #008800; font-weight: bold;"&gt;for&lt;/span&gt; &lt;span style="color: #333333;"&gt;(&lt;/span&gt;&lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; i &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;0&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt; i &lt;span style="color: #333333;"&gt;&amp;lt;&lt;/span&gt; tmp&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;length&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt; i&lt;span style="color: #333333;"&gt;++)&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;

    &lt;span style="color: #008800; font-weight: bold;"&gt;if&lt;/span&gt; &lt;span style="color: #333333;"&gt;(&lt;/span&gt;string&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;contains&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;tmp&lt;span style="color: #333333;"&gt;[&lt;/span&gt;i&lt;span style="color: #333333;"&gt;]&lt;/span&gt; &lt;span style="color: #333333;"&gt;+&lt;/span&gt; &lt;span style="background-color: #fff0f0;"&gt;""&lt;/span&gt;&lt;span style="color: #333333;"&gt;))&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;
     &lt;span style="color: #008800; font-weight: bold;"&gt;if&lt;/span&gt; &lt;span style="color: #333333;"&gt;(&lt;/span&gt;lastIndex &lt;span style="color: #333333;"&gt;&amp;lt;&lt;/span&gt; string&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;indexOf&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;tmp&lt;span style="color: #333333;"&gt;[&lt;/span&gt;i&lt;span style="color: #333333;"&gt;]))&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;
      lastIndex &lt;span style="color: #333333;"&gt;=&lt;/span&gt; string&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;indexOf&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;tmp&lt;span style="color: #333333;"&gt;[&lt;/span&gt;i&lt;span style="color: #333333;"&gt;]);&lt;/span&gt;
     &lt;span style="color: #333333;"&gt;}&lt;/span&gt;
     string &lt;span style="color: #333333;"&gt;=&lt;/span&gt; string&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;substring&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;&lt;span style="color: #0000dd; font-weight: bold;"&gt;0&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt; string&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;indexOf&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;tmp&lt;span style="color: #333333;"&gt;[&lt;/span&gt;i&lt;span style="color: #333333;"&gt;]))&lt;/span&gt; &lt;span style="color: #333333;"&gt;+&lt;/span&gt; &lt;span style="color: #0044dd;"&gt;'\0'&lt;/span&gt;
       &lt;span style="color: #333333;"&gt;+&lt;/span&gt; string&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;substring&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;string&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;indexOf&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;tmp&lt;span style="color: #333333;"&gt;[&lt;/span&gt;i&lt;span style="color: #333333;"&gt;])&lt;/span&gt; &lt;span style="color: #333333;"&gt;+&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;1&lt;/span&gt;&lt;span style="color: #333333;"&gt;);&lt;/span&gt;
     tmp&lt;span style="color: #333333;"&gt;[&lt;/span&gt;i&lt;span style="color: #333333;"&gt;]&lt;/span&gt; &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="color: #0044dd;"&gt;'\0'&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
     cnt&lt;span style="color: #333333;"&gt;--;&lt;/span&gt;
    &lt;span style="color: #333333;"&gt;}&lt;/span&gt;
   &lt;span style="color: #333333;"&gt;}&lt;/span&gt;

   &lt;span style="color: #888888;"&gt;// If all characters present in substring then trim extra characters&lt;/span&gt;
   &lt;span style="color: #888888;"&gt;// and choose the smallest window string&lt;/span&gt;
   &lt;span style="color: #008800; font-weight: bold;"&gt;if&lt;/span&gt; &lt;span style="color: #333333;"&gt;(&lt;/span&gt;cnt &lt;span style="color: #333333;"&gt;&amp;lt;=&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;0&lt;/span&gt;&lt;span style="color: #333333;"&gt;)&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;
    arr&lt;span style="color: #333333;"&gt;[&lt;/span&gt;pnt&lt;span style="color: #333333;"&gt;]&lt;/span&gt; &lt;span style="color: #333333;"&gt;=&lt;/span&gt; arr&lt;span style="color: #333333;"&gt;[&lt;/span&gt;pnt&lt;span style="color: #333333;"&gt;].&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;substring&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;&lt;span style="color: #0000dd; font-weight: bold;"&gt;0&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt; lastIndex &lt;span style="color: #333333;"&gt;+&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;1&lt;/span&gt;&lt;span style="color: #333333;"&gt;);&lt;/span&gt;
    result &lt;span style="color: #333333;"&gt;=&lt;/span&gt; arr&lt;span style="color: #333333;"&gt;[&lt;/span&gt;pnt&lt;span style="color: #333333;"&gt;].&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;length&lt;/span&gt;&lt;span style="color: #333333;"&gt;()&lt;/span&gt; &lt;span style="color: #333333;"&gt;&amp;lt;&lt;/span&gt; result&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;length&lt;/span&gt;&lt;span style="color: #333333;"&gt;()&lt;/span&gt; &lt;span style="color: #333333;"&gt;?&lt;/span&gt; arr&lt;span style="color: #333333;"&gt;[&lt;/span&gt;pnt&lt;span style="color: #333333;"&gt;]&lt;/span&gt; &lt;span style="color: #333333;"&gt;:&lt;/span&gt; result&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
   &lt;span style="color: #333333;"&gt;}&lt;/span&gt;

  &lt;span style="color: #333333;"&gt;}&lt;/span&gt;
  System&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;out&lt;/span&gt;&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;println&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;&lt;span style="background-color: #fff0f0;"&gt;"\nInput String           : "&lt;/span&gt; &lt;span style="color: #333333;"&gt;+&lt;/span&gt; str&lt;span style="color: #333333;"&gt;);&lt;/span&gt;
  System&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;out&lt;/span&gt;&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;println&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;&lt;span style="background-color: #fff0f0;"&gt;"\n2nd String to find     : "&lt;/span&gt; &lt;span style="color: #333333;"&gt;+&lt;/span&gt; findStr&lt;span style="color: #333333;"&gt;);&lt;/span&gt;
  System&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;out&lt;/span&gt;&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;println&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;&lt;span style="background-color: #fff0f0;"&gt;"\nSmallest window string : "&lt;/span&gt; &lt;span style="color: #333333;"&gt;+&lt;/span&gt; result&lt;span style="color: #333333;"&gt;);&lt;/span&gt;
 &lt;span style="color: #333333;"&gt;}&lt;/span&gt;
&lt;span style="color: #333333;"&gt;}&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;OUTPUT:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;"&gt;
&lt;pre style="line-height: 125%; margin: 0;"&gt;Input String           &lt;span style="color: #333333;"&gt;:&lt;/span&gt; niharika

&lt;span style="color: #0000dd; font-weight: bold;"&gt;2&lt;/span&gt;nd String to find     &lt;span style="color: #333333;"&gt;:&lt;/span&gt; ir

Smallest window string &lt;span style="color: #333333;"&gt;:&lt;/span&gt; ri
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link href="https://www.blogger.com/feeds/6801225484322010450/posts/default/4883095046466140556" rel="edit" type="application/atom+xml"/><link href="https://www.blogger.com/feeds/6801225484322010450/posts/default/4883095046466140556" rel="self" type="application/atom+xml"/><link href="https://javadiscover.blogspot.com/2019/01/find-smallest-window-in-string.html" rel="alternate" title="Find the smallest window in a string containing all characters of another string (With smallest window)" type="text/html"/><author><name>Java Discover</name><uri>http://www.blogger.com/profile/18049511268651382259</uri><email>noreply@blogger.com</email><gd:image height="16" rel="http://schemas.google.com/g/2005#thumbnail" src="https://img1.blogblog.com/img/b16-rounded.gif" width="16"/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" height="72" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgwoe0lQYBXM6tp_B-uodoq6XQbmPFY2nsgrHvWvDo1UgVa9XASJ4airgnQEJqgX16HiuMjQut3-gXbKPQZaT4pf9h3Ahnb8CEiqA9S1CztlNibnVj3syVtcgX71o2veQEeqXGYOZlCtV0/s72-c/Screenshot+2019-01-04+at+2.27.44+AM.png" width="72"/></entry><entry><id>tag:blogger.com,1999:blog-6801225484322010450.post-90787554068688728</id><published>2019-01-02T02:57:00.001+05:30</published><updated>2019-01-02T02:57:04.832+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="amazon interview questions"/><category scheme="http://www.blogger.com/atom/ns#" term="Core Java"/><category scheme="http://www.blogger.com/atom/ns#" term="finding missing number in an array"/><category scheme="http://www.blogger.com/atom/ns#" term="Interview Questions"/><category scheme="http://www.blogger.com/atom/ns#" term="XOR"/><title type="text">How to find missing number in a sequential array ?</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
Given a list of sequential integers and need to finding a missing number from the list of numbers. For example if we have an array like&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjgRlAyp3j0K00lRiK8R6Zs_cPDXqg7IVYXvW7NUdHDIc69xtXbXOf2SMi0xK3JSVpAo5omaxK1s1Ljlqa0nT3iYOKv1OOXwqEGAtzNAltbmsWHzeI2h0AuGoA2j113wcc8XmHrujkZkAo/s1600/Screenshot+2019-01-02+at+2.53.31+AM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="Finding missing number" border="0" data-original-height="232" data-original-width="958" height="75" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjgRlAyp3j0K00lRiK8R6Zs_cPDXqg7IVYXvW7NUdHDIc69xtXbXOf2SMi0xK3JSVpAo5omaxK1s1Ljlqa0nT3iYOKv1OOXwqEGAtzNAltbmsWHzeI2h0AuGoA2j113wcc8XmHrujkZkAo/s320/Screenshot+2019-01-02+at+2.53.31+AM.png" title="Finding missing number" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Example:1&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;array&lt;/b&gt; = {1,2,3,5,6}&lt;br /&gt;
Finding missing number 4&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Example:2&lt;/b&gt;&lt;br /&gt;
array = {14,15,16,18,19,20}&lt;br /&gt;
then the missing number is 17&lt;br /&gt;
&lt;br /&gt;
Using XOR operation and finding the missing number.&lt;br /&gt;
&lt;br /&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;Iterate all the elements and do XOR with each other.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Iterate all the numbers between given smallest and greatest number. In our above example between 1 to 6 and 14 to 20.&lt;/li&gt;
&lt;li&gt;Next do XOR with both results which gives the missing number&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
Lets see simple Java code implementation&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;"&gt;
&lt;pre style="line-height: 125%; margin: 0;"&gt;&lt;span style="color: #008800; font-weight: bold;"&gt;public&lt;/span&gt; &lt;span style="color: #008800; font-weight: bold;"&gt;class&lt;/span&gt; &lt;span style="color: #bb0066; font-weight: bold;"&gt;FindingMissingNumber&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;

 &lt;span style="color: #008800; font-weight: bold;"&gt;public&lt;/span&gt; &lt;span style="color: #008800; font-weight: bold;"&gt;static&lt;/span&gt; &lt;span style="color: #333399; font-weight: bold;"&gt;void&lt;/span&gt; &lt;span style="color: #0066bb; font-weight: bold;"&gt;main&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;String&lt;span style="color: #333333;"&gt;[]&lt;/span&gt; args&lt;span style="color: #333333;"&gt;)&lt;/span&gt; &lt;span style="color: #333333;"&gt;{&lt;/span&gt;
  
  &lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt;&lt;span style="color: #333333;"&gt;[]&lt;/span&gt; arr &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="color: #008800; font-weight: bold;"&gt;new&lt;/span&gt; &lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt;&lt;span style="color: #333333;"&gt;[]{&lt;/span&gt;&lt;span style="color: #0000dd; font-weight: bold;"&gt;11&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt;&lt;span style="color: #0000dd; font-weight: bold;"&gt;12&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt;&lt;span style="color: #0000dd; font-weight: bold;"&gt;13&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt;&lt;span style="color: #0000dd; font-weight: bold;"&gt;15&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt;&lt;span style="color: #0000dd; font-weight: bold;"&gt;16&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt;&lt;span style="color: #0000dd; font-weight: bold;"&gt;17&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt;&lt;span style="color: #0000dd; font-weight: bold;"&gt;18&lt;/span&gt;&lt;span style="color: #333333;"&gt;,&lt;/span&gt;&lt;span style="color: #0000dd; font-weight: bold;"&gt;19&lt;/span&gt;&lt;span style="color: #333333;"&gt;};&lt;/span&gt;
  &lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; sr &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;11&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
  &lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; er &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;19&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
  
  FindingMissingNumber obj &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="color: #008800; font-weight: bold;"&gt;new&lt;/span&gt; FindingMissingNumber&lt;span style="color: #333333;"&gt;();&lt;/span&gt;
  
  System&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;out&lt;/span&gt;&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;println&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;&lt;span style="background-color: #fff0f0;"&gt;"Missing number using XOR : "&lt;/span&gt;&lt;span style="color: #333333;"&gt;+&lt;/span&gt;obj&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;findMissingNumberUsingXOR&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;arr&lt;span style="color: #333333;"&gt;,&lt;/span&gt; sr&lt;span style="color: #333333;"&gt;,&lt;/span&gt; er&lt;span style="color: #333333;"&gt;));&lt;/span&gt;
 &lt;span style="color: #333333;"&gt;}&lt;/span&gt;
 
 &lt;span style="color: #008800; font-weight: bold;"&gt;public&lt;/span&gt; &lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; &lt;span style="color: #0066bb; font-weight: bold;"&gt;findMissingNumberUsingXOR&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;&lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt;&lt;span style="color: #333333;"&gt;[]&lt;/span&gt; arr&lt;span style="color: #333333;"&gt;,&lt;/span&gt; &lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; sr&lt;span style="color: #333333;"&gt;,&lt;/span&gt; &lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; er&lt;span style="color: #333333;"&gt;){&lt;/span&gt;
  
  &lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; xor &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;0&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
  &lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; actualXor &lt;span style="color: #333333;"&gt;=&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;0&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
  
  &lt;span style="color: #008800; font-weight: bold;"&gt;for&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;&lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; i&lt;span style="color: #333333;"&gt;=&lt;/span&gt;&lt;span style="color: #0000dd; font-weight: bold;"&gt;0&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt;i&lt;span style="color: #333333;"&gt;&amp;lt;&lt;/span&gt;arr&lt;span style="color: #333333;"&gt;.&lt;/span&gt;&lt;span style="color: #0000cc;"&gt;length&lt;/span&gt;&lt;span style="color: #333333;"&gt;;&lt;/span&gt;i&lt;span style="color: #333333;"&gt;++){&lt;/span&gt;
   xor &lt;span style="color: #333333;"&gt;=&lt;/span&gt; xor &lt;span style="color: #333333;"&gt;^&lt;/span&gt; arr&lt;span style="color: #333333;"&gt;[&lt;/span&gt;i&lt;span style="color: #333333;"&gt;];;&lt;/span&gt;
  &lt;span style="color: #333333;"&gt;}&lt;/span&gt;
  
  &lt;span style="color: #008800; font-weight: bold;"&gt;for&lt;/span&gt;&lt;span style="color: #333333;"&gt;(&lt;/span&gt;&lt;span style="color: #333399; font-weight: bold;"&gt;int&lt;/span&gt; i&lt;span style="color: #333333;"&gt;=&lt;/span&gt;sr&lt;span style="color: #333333;"&gt;;&lt;/span&gt;i&lt;span style="color: #333333;"&gt;&amp;lt;=&lt;/span&gt;er&lt;span style="color: #333333;"&gt;;&lt;/span&gt;i&lt;span style="color: #333333;"&gt;++){&lt;/span&gt;
   actualXor &lt;span style="color: #333333;"&gt;=&lt;/span&gt; actualXor &lt;span style="color: #333333;"&gt;^&lt;/span&gt; i&lt;span style="color: #333333;"&gt;;&lt;/span&gt;
  &lt;span style="color: #333333;"&gt;}&lt;/span&gt;
  
  &lt;span style="color: #008800; font-weight: bold;"&gt;return&lt;/span&gt; &lt;span style="color: #333333;"&gt;(&lt;/span&gt;xor &lt;span style="color: #333333;"&gt;^&lt;/span&gt; actualXor&lt;span style="color: #333333;"&gt;);&lt;/span&gt;
 &lt;span style="color: #333333;"&gt;}&lt;/span&gt;
&lt;span style="color: #333333;"&gt;}&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
OUTPUT:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;"&gt;
&lt;pre style="line-height: 125%; margin: 0;"&gt;Missing number using XOR &lt;span style="color: #333333;"&gt;:&lt;/span&gt; &lt;span style="color: #0000dd; font-weight: bold;"&gt;14&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
</content><link href="https://www.blogger.com/feeds/6801225484322010450/posts/default/90787554068688728" rel="edit" type="application/atom+xml"/><link href="https://www.blogger.com/feeds/6801225484322010450/posts/default/90787554068688728" rel="self" type="application/atom+xml"/><link href="https://javadiscover.blogspot.com/2019/01/how-to-find-missing-number-in.html" rel="alternate" title="How to find missing number in a sequential array ?" type="text/html"/><author><name>Java Discover</name><uri>http://www.blogger.com/profile/18049511268651382259</uri><email>noreply@blogger.com</email><gd:image height="16" rel="http://schemas.google.com/g/2005#thumbnail" src="https://img1.blogblog.com/img/b16-rounded.gif" width="16"/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" height="72" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjgRlAyp3j0K00lRiK8R6Zs_cPDXqg7IVYXvW7NUdHDIc69xtXbXOf2SMi0xK3JSVpAo5omaxK1s1Ljlqa0nT3iYOKv1OOXwqEGAtzNAltbmsWHzeI2h0AuGoA2j113wcc8XmHrujkZkAo/s72-c/Screenshot+2019-01-02+at+2.53.31+AM.png" width="72"/></entry></feed>