<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-4859863201677788807</id><updated>2025-09-20T19:46:50.602+05:30</updated><category term="Algorithms/Sortings"/><category term="Python"/><category term="Java"/><category term="C Programs"/><category term="Data Structure"/><category term="Database"/><category term="Pattern Printing"/><category term="Theory"/><category term="Interview"/><category term="Letter Patterns"/><category term="HTML"/><category term="Web Tech"/><title type='text'>Programming Solutions</title><subtitle type='html'>This blog contains programming of various languages like Java, Python, SQL. It provides answers to Leetcode questions. It will also help users in Database, Algorithms, and Sorting.&#xa;</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='https://programmings-java.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/4859863201677788807/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='https://programmings-java.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='https://www.blogger.com/feeds/4859863201677788807/posts/default?start-index=26&amp;max-results=25&amp;redirect=false'/><author><name>Mohd Talha</name><uri>http://www.blogger.com/profile/01763375607514819581</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>72</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-4859863201677788807.post-36017431327037547</id><published>2021-07-08T18:53:00.003+05:30</published><updated>2024-02-11T20:38:20.167+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Interview"/><category scheme="http://www.blogger.com/atom/ns#" term="Theory"/><title type='text'>DBMS Interview Questions and Answers</title><content type='html'>&lt;link rel=&quot;canonical&quot; href=&quot;https://programmings-java.blogspot.com/2021/07/dbms-interview-questions-and-answers.html&quot;/&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; display: none;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiEXSa7pSC38pDwmty9O3PArT4MJGjVp-dyWPxWkjOJb9zOh9k_FX9-WFLgl3QBqtwELvZ__rnlMx1trAUZvGlwMzx00_-8mDzzB-2-1o7QiTG3wgh6RC_96rmbh6XYwvdf9Z2tzI3pXMVX/s592/Temp+%25281%2529.webp&quot; style=&quot;display: block; padding: 1em 0px; text-align: center;&quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; data-original-height=&quot;344&quot; data-original-width=&quot;592&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiEXSa7pSC38pDwmty9O3PArT4MJGjVp-dyWPxWkjOJb9zOh9k_FX9-WFLgl3QBqtwELvZ__rnlMx1trAUZvGlwMzx00_-8mDzzB-2-1o7QiTG3wgh6RC_96rmbh6XYwvdf9Z2tzI3pXMVX/s320/Temp+%25281%2529.webp&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: x-large;&quot;&gt;&lt;b&gt;DBMS Interview Questions and Answers&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: x-large;&quot;&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;b&gt;Data&lt;/b&gt;: It is raw, just a set of facts that by itself doesn&#39;t convey anything.&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Eg: Student scores 90 marks.&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;b&gt;Information&lt;/b&gt;: We need to understand the pattern between factual data and give it meaning.&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Eg: Student got an A grade.&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;b&gt;Knowledge&lt;/b&gt;: Synthesis of data and information lead us to answer the HOW question and take business decision.&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Eg: Student is a Top Performer.&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;b&gt;Database&lt;/b&gt;: It is a shared collection of logically related data, design to meet the needs of an organization.&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;b&gt;DBMS&lt;/b&gt;: Collections of programs to access data. It is a software system that enables the users to define, create, maintain and control access to the database.&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;b&gt;Functions of DBMS&lt;/b&gt;:&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Data management: Store, retrieve and update&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Transaction support: Modify in the database in successful or not&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Recovery&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Security&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Integrity: Maintain accuracy&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Concurrency control: Simultaneous access provided to users&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Utilities: Import, export, manage, backup, etc&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;b&gt;Keys&lt;/b&gt;: It is used to&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white; text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: inherit; font-size: medium;&quot;&gt;identify any record or row of data from the table. It is also used to establish and identify relationships between tables.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;b&gt;Candidate Key&lt;/b&gt;: It is a minimal set of attributes (columns) that can be used to uniquely identify a single tuple (row) in a relation.&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;b&gt;Primary Key&lt;/b&gt;: It is a candidate key that is selected to uniquely identify a tuple in a relation.&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;b&gt;Foreign Key&lt;/b&gt;: It is a set of one or more columns in the child table whose values are required to match with the corresponding columns in the parent table.&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;b&gt;Data Integrity&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Entity Integrity (row): Primary Key&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Domain Integrity (column): Datatypes, check constraint&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Referential Integrity: Foreign Key&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;b&gt;ER Model&lt;/b&gt;: It is a graphical representation of entities and their relationship which help in understanding data independent of the actual database implementation.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;b&gt;Entity&lt;/b&gt;: Real-world object which has an independent existence.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;b&gt;Attribute&lt;/b&gt;: Property that describes an entity.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;b&gt;Cardinality of Relationship&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;1-1: 1 Employee allocated one computer and not shared.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;1-M:&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: large;&quot;&gt;1 Employee allocated many computers and not shared.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;M-M: Many&lt;/span&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&amp;nbsp;Employees allocated many computer and shared.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;b&gt;Normalization&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;It is a technique to remove or reduce redundancy (duplicate) from a table.&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;b&gt;First Normal Form (1NF)&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Table should not contain any multivalued attribute.&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;b&gt;Second Normal Form (2NF)&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Table must be in 1NF.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;All non-prime attributes should be fully functionally dependent on the candidate key.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;OR&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;There should be no partial dependency.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;b&gt;Third Normal Form (3NF)&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Table must be in 2NF.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;There should be no transitive dependency in the table.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;b&gt;Need for 2NF&lt;/b&gt;: Insert, update, delete anomalies&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;b&gt;Need for 3NF&lt;/b&gt;: Cause problem in duplicate rows.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Eg: If 2 students score the same marks then their grades will also be the same, here the rows will be duplicates.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;script async=&quot;&quot; src=&quot;https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js&quot;&gt;&lt;/script&gt;
&lt;ins class=&quot;adsbygoogle&quot; data-ad-client=&quot;ca-pub-1582122381896423&quot; data-ad-format=&quot;fluid&quot; data-ad-layout=&quot;in-article&quot; data-ad-slot=&quot;5469905528&quot; style=&quot;display: block; text-align: center;&quot;&gt;&lt;/ins&gt;
&lt;script&gt;
     (adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;b&gt;Transaction&lt;/b&gt;: It is a set of operations used to perform a logical unit of work. A transaction generally represented a change in the database.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;b&gt;ACID Properties&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;b&gt;Atomicity&lt;/b&gt;: Either all the statements will run or none.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;b&gt;Consistency&lt;/b&gt;: Before and after transactions, sum of money should be the same.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;b&gt;Isolation&lt;/b&gt;: Convert parallel transaction into the serial transaction, because the serial schedule is consistent.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;b&gt;Durability&lt;/b&gt;: Changes made in the database must be permanent.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;b&gt;DDL: Data Definition Language&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;It changes the structure of the table like creating, deleting, altering, etc.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;create&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;alter&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;drop&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;truncate&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;b&gt;DML: Data Manipulation Language&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;It is used to modify the database tables.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;insert&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;update&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;delete&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;b&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;DCL: Data Control Language&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;It is used to grant and revoke authority for any database user.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;grant&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;revoke&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;b&gt;TCL: Transaction Control Language&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;TCL commands can only use with DML commands like INSERT, DELETE and UPDATE only.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;commit&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;rollback&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;savepoint&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;b&gt;Delete vs Drop vs Truncate&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Delete is used to remove a complete table or a particular row (using where clause).&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Drop is used to remove the complete table or database.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Truncate is used to delete tables and set constraints to default.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;b&gt;Stored Procedure&lt;/b&gt;: It is a SQL statement that you can save and can be reused again.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Eg: Create Procedure Procedure_Name AS SQL_Statement GO;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;b&gt;DBMS Abstraction&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;View: User Interface with which users interact.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Logical: Fields, attributes, relations.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Physical: Block of storage where data actually stored.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;a href=&quot;https://comparekare.in/&quot; rel=&quot;nofollow&quot; style=&quot;font-size: large;&quot; target=&quot;_blank&quot;&gt;CompareKarein&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://programmings-java.blogspot.com/feeds/36017431327037547/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://programmings-java.blogspot.com/2021/07/dbms-interview-questions-and-answers.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/4859863201677788807/posts/default/36017431327037547'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/4859863201677788807/posts/default/36017431327037547'/><link rel='alternate' type='text/html' href='https://programmings-java.blogspot.com/2021/07/dbms-interview-questions-and-answers.html' title='DBMS Interview Questions and Answers'/><author><name>Mohd Talha</name><uri>http://www.blogger.com/profile/01763375607514819581</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiEXSa7pSC38pDwmty9O3PArT4MJGjVp-dyWPxWkjOJb9zOh9k_FX9-WFLgl3QBqtwELvZ__rnlMx1trAUZvGlwMzx00_-8mDzzB-2-1o7QiTG3wgh6RC_96rmbh6XYwvdf9Z2tzI3pXMVX/s72-c/Temp+%25281%2529.webp" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4859863201677788807.post-6051005181283334069</id><published>2021-07-06T12:50:00.003+05:30</published><updated>2024-02-11T20:22:06.339+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Interview"/><category scheme="http://www.blogger.com/atom/ns#" term="Theory"/><title type='text'>OOPs Interview Questions and Answers</title><content type='html'>&lt;meta name=&quot;keywords&quot; content=&quot;OOPs Interview Questions and Answers, interview questions, dsa, Python DSA Program, Data Structure and Algorithm, Class, Object, Function, Programmings-Java&quot;&gt;	
&lt;div class=&quot;separator&quot; style=&quot;clear: both; display: none;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg1ztlpmd0ydBk1tcAMfErTkbwuYjm86neitH8iLCtIx0BnPNpU7WFyswwfehTMfhgbVRkNLeZ3sjSzwEFqukFlJkM3Lxmppt4SGFFvKEuzRnmmLSYkYBOPPwYTcj1LiXFCLRgNUbckz6xs/s592/Temp+%25281%2529.webp&quot; style=&quot;display: block; padding: 1em 0px; text-align: center;&quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; data-original-height=&quot;344&quot; data-original-width=&quot;592&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg1ztlpmd0ydBk1tcAMfErTkbwuYjm86neitH8iLCtIx0BnPNpU7WFyswwfehTMfhgbVRkNLeZ3sjSzwEFqukFlJkM3Lxmppt4SGFFvKEuzRnmmLSYkYBOPPwYTcj1LiXFCLRgNUbckz6xs/s320/Temp+%25281%2529.webp&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;h2 style=&quot;height: 0px; text-align: left;&quot;&gt;&lt;span style=&quot;font-size: x-large;&quot;&gt;OOPs Interview Questions and Answers&lt;br /&gt;&lt;/span&gt;&lt;/h2&gt;&lt;p style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;&lt;/p&gt;&lt;div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;b&gt;Class&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;It is a collection of objects of the same type.&lt;/div&gt;&lt;div&gt;It is a blueprint or prototype that defines the variables and methods common to all objects of a certain kind.&lt;/div&gt;&lt;div&gt;Eg. Human&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;b&gt;Object&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;It is a real-world entity or instance of a class.&lt;/div&gt;&lt;div&gt;Eg. Men and women&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;b&gt;Abstraction&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;An act of representing the task to be done in a system in such a way that anyone can use the system without having any internal knowledge of the workings of the system.&lt;/div&gt;&lt;div&gt;Eg: We use readLine() without knowing how it takes input from the Keyboard.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;b&gt;Encapsulation&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;The wrapping of code and data into a single unit.&lt;/div&gt;&lt;div&gt;It acts as a protective layer by ensuring that access to wrapped data is not possible by any code defined outside the class.&lt;/div&gt;&lt;div&gt;Eg: Acess Specifier, getter, setter.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;b&gt;Polymorphism&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;It is a process that allows one thing to have many forms.&lt;/div&gt;&lt;div&gt;It is the process that allows classes to respond to messages in different ways.&lt;/div&gt;&lt;div&gt;Eg:&lt;/div&gt;&lt;div&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Function Overloading: Process having more than one function with the same name but with different numbers or datatype parameters.&lt;/li&gt;&lt;li&gt;Function Overriding: Process of redefining any function in a derived class. The redefined function in the derived class should have the same signature (parameter) in a base class.&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;b&gt;Inheritance&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;A process by which one object acquires the property of another object.&lt;/div&gt;&lt;div&gt;Eg:&lt;/div&gt;&lt;div&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Single Inheritance: From one base class only one derived class is created. Eg. Fan &amp;lt;= Cooler&lt;/li&gt;&lt;li&gt;Hierarchical Inheritance: From one base class more than one derived class is created. Eg. Person &amp;lt;= Student, Player&lt;/li&gt;&lt;li&gt;Multilevel Inheritance: From one base class one derived class is created and this derived class will act as a base class from one more derived class. Eg. A &amp;lt;= B &amp;lt;= C&lt;/li&gt;&lt;li&gt;Multiple Inheritance: A class inherits more than one class. Eg. Parent1, Parent2 &amp;lt;= Child&lt;/li&gt;&lt;li&gt;Hybrid Inheritance: It is a combination of multiple and multilevel inheritances.&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;b&gt;Abstract Class and Method&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;An abstract class can be considered a blueprint of another class.&lt;/div&gt;&lt;div&gt;An abstract method is a method that has a declaration but does not have an implementation.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;b&gt;RealWorld Example of OOPs&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;b&gt;Abstraction: TV Remote&lt;/b&gt; - We press a button and the channel changes, we don&#39;t know how it internally works.&lt;/li&gt;&lt;li&gt;&lt;b&gt;Encapsulation: Car&lt;/b&gt; - There are multiple parts like steering, wheels, etc. All are bound together to drive a car.&lt;/li&gt;&lt;li&gt;&lt;b&gt;Inheritance: Parent-Child&lt;/b&gt; - Child inherits parents properties.&lt;/li&gt;&lt;li&gt;&lt;b&gt;Polymorphism: Person&lt;/b&gt; - One behaves differently in front of elders and shows different behaviour in front of friends.&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Want to check about the Latest Electronic Devices:&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;https://comparekare.in/&quot; rel=&quot;nofollow&quot; style=&quot;font-size: large;&quot; target=&quot;_blank&quot;&gt;CompareKarein&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;b&gt;super, this, self&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;super keyword is used to refer to the immediate parent class object.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;this keyword stores the reference of the object on which the function was invoked.&lt;/div&gt;&lt;div&gt;this keyword is used to access instance variables when the name of the instance variable is the same as any local or formal variable of the function.&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;self is a reference to an object&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;b&gt;Compiler / Interpreter / ByteCode&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;A compiler reads the program, checks for syntax errors and creates an executable file that can be executed at any time. There is no need to recompile every time to execute the program.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;An interpreter reads the program line by line, checks for errors and executes immediately. Source code has to be interpreted each time to execute the program.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Bytecode is a machine instruction given to JVM.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Java has the advantage of both Compiler and Interpreter.&lt;/div&gt;&lt;div&gt;Python has only an Interpreter.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;b&gt;ClassLoader&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;It is a subsystem of JVM used to load class files.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;b&gt;PEP-8 (Python Enhancement Proposal)&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;It is a set of rules used to increase Python code reusability.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;b&gt;Lambda Function&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;It is used when the function is of one line and has no return type and no iteration problem.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;b&gt;Shallow and Deep Copy&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;A shallow copy passes a reference to an object.&lt;/div&gt;&lt;div&gt;Deep copy creates new objects and copies.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://programmings-java.blogspot.com/feeds/6051005181283334069/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://programmings-java.blogspot.com/2021/07/interview-questions-and-answers.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/4859863201677788807/posts/default/6051005181283334069'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/4859863201677788807/posts/default/6051005181283334069'/><link rel='alternate' type='text/html' href='https://programmings-java.blogspot.com/2021/07/interview-questions-and-answers.html' title='OOPs Interview Questions and Answers'/><author><name>Mohd Talha</name><uri>http://www.blogger.com/profile/01763375607514819581</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg1ztlpmd0ydBk1tcAMfErTkbwuYjm86neitH8iLCtIx0BnPNpU7WFyswwfehTMfhgbVRkNLeZ3sjSzwEFqukFlJkM3Lxmppt4SGFFvKEuzRnmmLSYkYBOPPwYTcj1LiXFCLRgNUbckz6xs/s72-c/Temp+%25281%2529.webp" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4859863201677788807.post-3942480458856806752</id><published>2021-07-03T15:21:00.003+05:30</published><updated>2021-07-03T15:24:39.147+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Theory"/><title type='text'>Constructor</title><content type='html'>&lt;h2 style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Constructor&lt;/span&gt;&lt;/h2&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Constructor is a special method of the class whose name is the same as the class name and it runs automatically when an object is created.&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Use of Constructor&lt;/span&gt;&lt;/h4&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;It is used to initialize instance variables immediately after the creation of an object.&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Properties of Constructor&lt;/span&gt;&lt;/h4&gt;&lt;p&gt;&lt;/p&gt;&lt;p style=&quot;height: 0px;&quot;&gt;&lt;/p&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;It has no return type not even void&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Its name is same as that of class&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;It runs automatically after creation of an object&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Compiler automatically creates a default constructor when no constructor is defined&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Types of Constructor&lt;/span&gt;&lt;/h4&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Constructor which does not take any argument is known as a default constructor.&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Default constructor is of two types&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Created by Programmer&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Automatically created by the compiler (when no constructor is defined)&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;span&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Constructor that can take arguments is known as parameterized constructor.&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Constructor Overloading&lt;/span&gt;&lt;/h4&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Defining two or more constructors in a class is known as Constructor Overloading. All&lt;/span&gt;&lt;div&gt;&lt;span style=&quot;font-size: large;&quot;&gt;constructors should have different parameter declarations.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Difference between Constructor and Function&lt;/span&gt;&lt;/h4&gt;&lt;table data-darkreader-inline-border-bottom=&quot;&quot; data-darkreader-inline-border-left=&quot;&quot; data-darkreader-inline-border-right=&quot;&quot; data-darkreader-inline-border-top=&quot;&quot; style=&quot;--darkreader-inline-border-bottom: initial; --darkreader-inline-border-left: initial; --darkreader-inline-border-right: initial; --darkreader-inline-border-top: initial; border-collapse: collapse; border: none; table-layout: fixed; width: 468pt;&quot;&gt;&lt;colgroup&gt;&lt;col&gt;&lt;/col&gt;&lt;col&gt;&lt;/col&gt;&lt;/colgroup&gt;&lt;tbody&gt;&lt;tr style=&quot;height: 0pt;&quot;&gt;&lt;td data-darkreader-inline-border-bottom=&quot;&quot; data-darkreader-inline-border-left=&quot;&quot; data-darkreader-inline-border-right=&quot;&quot; data-darkreader-inline-border-top=&quot;&quot; style=&quot;--darkreader-inline-border-bottom: #867d6f; --darkreader-inline-border-left: #867d6f; --darkreader-inline-border-right: #867d6f; --darkreader-inline-border-top: #867d6f; border-color: rgb(0, 0, 0); border-style: solid; border-width: 1pt; overflow-wrap: break-word; overflow: hidden; padding: 5pt; vertical-align: top;&quot;&gt;&lt;div style=&quot;line-height: 1.2; margin-bottom: 0pt; margin-top: 0pt;&quot;&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #dbd8d3; color: black; font-family: Arial; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;b&gt;Constructor&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/td&gt;&lt;td data-darkreader-inline-border-bottom=&quot;&quot; data-darkreader-inline-border-left=&quot;&quot; data-darkreader-inline-border-right=&quot;&quot; data-darkreader-inline-border-top=&quot;&quot; style=&quot;--darkreader-inline-border-bottom: #867d6f; --darkreader-inline-border-left: #867d6f; --darkreader-inline-border-right: #867d6f; --darkreader-inline-border-top: #867d6f; border-color: rgb(0, 0, 0); border-style: solid; border-width: 1pt; overflow-wrap: break-word; overflow: hidden; padding: 5pt; vertical-align: top;&quot;&gt;&lt;h4 style=&quot;line-height: 1.2; margin-bottom: 0pt; margin-top: 0pt;&quot;&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #dbd8d3; color: black; font-family: Arial; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Function&lt;/span&gt;&lt;/span&gt;&lt;/h4&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr style=&quot;height: 0pt;&quot;&gt;&lt;td data-darkreader-inline-border-bottom=&quot;&quot; data-darkreader-inline-border-left=&quot;&quot; data-darkreader-inline-border-right=&quot;&quot; data-darkreader-inline-border-top=&quot;&quot; style=&quot;--darkreader-inline-border-bottom: #867d6f; --darkreader-inline-border-left: #867d6f; --darkreader-inline-border-right: #867d6f; --darkreader-inline-border-top: #867d6f; border-color: rgb(0, 0, 0); border-style: solid; border-width: 1pt; overflow-wrap: break-word; overflow: hidden; padding: 5pt; vertical-align: top;&quot;&gt;&lt;p dir=&quot;ltr&quot; style=&quot;line-height: 1.2; margin-bottom: 0pt; margin-top: 0pt;&quot;&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #dbd8d3; color: black; font-family: Arial; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Its name is same as class name&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;td data-darkreader-inline-border-bottom=&quot;&quot; data-darkreader-inline-border-left=&quot;&quot; data-darkreader-inline-border-right=&quot;&quot; data-darkreader-inline-border-top=&quot;&quot; style=&quot;--darkreader-inline-border-bottom: #867d6f; --darkreader-inline-border-left: #867d6f; --darkreader-inline-border-right: #867d6f; --darkreader-inline-border-top: #867d6f; border-color: rgb(0, 0, 0); border-style: solid; border-width: 1pt; overflow-wrap: break-word; overflow: hidden; padding: 5pt; vertical-align: top;&quot;&gt;&lt;p dir=&quot;ltr&quot; style=&quot;line-height: 1.2; margin-bottom: 0pt; margin-top: 0pt;&quot;&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #dbd8d3; color: black; font-family: Arial; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Its name is different from class name&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr style=&quot;height: 0pt;&quot;&gt;&lt;td data-darkreader-inline-border-bottom=&quot;&quot; data-darkreader-inline-border-left=&quot;&quot; data-darkreader-inline-border-right=&quot;&quot; data-darkreader-inline-border-top=&quot;&quot; style=&quot;--darkreader-inline-border-bottom: #867d6f; --darkreader-inline-border-left: #867d6f; --darkreader-inline-border-right: #867d6f; --darkreader-inline-border-top: #867d6f; border-color: rgb(0, 0, 0); border-style: solid; border-width: 1pt; overflow-wrap: break-word; overflow: hidden; padding: 5pt; vertical-align: top;&quot;&gt;&lt;p dir=&quot;ltr&quot; style=&quot;line-height: 1.2; margin-bottom: 0pt; margin-top: 0pt;&quot;&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #dbd8d3; color: black; font-family: Arial; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;It cannot return any value&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;td data-darkreader-inline-border-bottom=&quot;&quot; data-darkreader-inline-border-left=&quot;&quot; data-darkreader-inline-border-right=&quot;&quot; data-darkreader-inline-border-top=&quot;&quot; style=&quot;--darkreader-inline-border-bottom: #867d6f; --darkreader-inline-border-left: #867d6f; --darkreader-inline-border-right: #867d6f; --darkreader-inline-border-top: #867d6f; border-color: rgb(0, 0, 0); border-style: solid; border-width: 1pt; overflow-wrap: break-word; overflow: hidden; padding: 5pt; vertical-align: top;&quot;&gt;&lt;p dir=&quot;ltr&quot; style=&quot;line-height: 1.2; margin-bottom: 0pt; margin-top: 0pt;&quot;&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #dbd8d3; color: black; font-family: Arial; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;It can return values&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr style=&quot;height: 0pt;&quot;&gt;&lt;td data-darkreader-inline-border-bottom=&quot;&quot; data-darkreader-inline-border-left=&quot;&quot; data-darkreader-inline-border-right=&quot;&quot; data-darkreader-inline-border-top=&quot;&quot; style=&quot;--darkreader-inline-border-bottom: #867d6f; --darkreader-inline-border-left: #867d6f; --darkreader-inline-border-right: #867d6f; --darkreader-inline-border-top: #867d6f; border-color: rgb(0, 0, 0); border-style: solid; border-width: 1pt; overflow-wrap: break-word; overflow: hidden; padding: 5pt; vertical-align: top;&quot;&gt;&lt;p dir=&quot;ltr&quot; style=&quot;line-height: 1.2; margin-bottom: 0pt; margin-top: 0pt;&quot;&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #dbd8d3; color: black; font-family: Arial; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;It gets invoked when an object is created&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;td data-darkreader-inline-border-bottom=&quot;&quot; data-darkreader-inline-border-left=&quot;&quot; data-darkreader-inline-border-right=&quot;&quot; data-darkreader-inline-border-top=&quot;&quot; style=&quot;--darkreader-inline-border-bottom: #867d6f; --darkreader-inline-border-left: #867d6f; --darkreader-inline-border-right: #867d6f; --darkreader-inline-border-top: #867d6f; border-color: rgb(0, 0, 0); border-style: solid; border-width: 1pt; overflow-wrap: break-word; overflow: hidden; padding: 5pt; vertical-align: top;&quot;&gt;&lt;p dir=&quot;ltr&quot; style=&quot;line-height: 1.2; margin-bottom: 0pt; margin-top: 0pt;&quot;&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #dbd8d3; color: black; font-family: Arial; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;It has to be called explicitly after creation of an object&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr style=&quot;height: 0pt;&quot;&gt;&lt;td data-darkreader-inline-border-bottom=&quot;&quot; data-darkreader-inline-border-left=&quot;&quot; data-darkreader-inline-border-right=&quot;&quot; data-darkreader-inline-border-top=&quot;&quot; style=&quot;--darkreader-inline-border-bottom: #867d6f; --darkreader-inline-border-left: #867d6f; --darkreader-inline-border-right: #867d6f; --darkreader-inline-border-top: #867d6f; border-color: rgb(0, 0, 0); border-style: solid; border-width: 1pt; overflow-wrap: break-word; overflow: hidden; padding: 5pt; vertical-align: top;&quot;&gt;&lt;p dir=&quot;ltr&quot; style=&quot;line-height: 1.2; margin-bottom: 0pt; margin-top: 0pt;&quot;&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #dbd8d3; color: black; font-family: Arial; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;It is used to initialize instance variables&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;td data-darkreader-inline-border-bottom=&quot;&quot; data-darkreader-inline-border-left=&quot;&quot; data-darkreader-inline-border-right=&quot;&quot; data-darkreader-inline-border-top=&quot;&quot; style=&quot;--darkreader-inline-border-bottom: #867d6f; --darkreader-inline-border-left: #867d6f; --darkreader-inline-border-right: #867d6f; --darkreader-inline-border-top: #867d6f; border-color: rgb(0, 0, 0); border-style: solid; border-width: 1pt; overflow-wrap: break-word; overflow: hidden; padding: 5pt; vertical-align: top;&quot;&gt;&lt;p dir=&quot;ltr&quot; style=&quot;line-height: 1.2; margin-bottom: 0pt; margin-top: 0pt;&quot;&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #dbd8d3; color: black; font-family: Arial; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;It is used to perform other tasks&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Points to remember&lt;/span&gt;&lt;/h4&gt;&lt;ul&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Constructor is always defined in public section of the class so that objects can be created in functions of any class.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;When no constructor is defined, compiler automatically creates a default constructor to initialize instance variables.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Default constructor automatically created by the compiler initializes integer variables by 0, floating-point variables by 0.0, boolean variables by false, String and all non-primitive by null.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;/div&gt;

&lt;div class=&quot;separator&quot; style=&quot;clear: both;display:none;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjxqmEAYpFkKJ3VgJ8nlZoH4MV-4MTlcZmGevv-hviq0vn1lcxEzj5w8DQqGxpZTpDqkhTsynCZN1suQqei6wVPCi3WLbck4kYp68zwEDas8Q27pmtggUvOXA9dfbAg7sSCEaiQqAr1cg8-/s767/13.webp&quot; style=&quot;display: block; padding: 1em 0; text-align: center; &quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; width=&quot;320&quot; data-original-height=&quot;346&quot; data-original-width=&quot;767&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjxqmEAYpFkKJ3VgJ8nlZoH4MV-4MTlcZmGevv-hviq0vn1lcxEzj5w8DQqGxpZTpDqkhTsynCZN1suQqei6wVPCi3WLbck4kYp68zwEDas8Q27pmtggUvOXA9dfbAg7sSCEaiQqAr1cg8-/s320/13.webp&quot;/&gt;&lt;/a&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='https://programmings-java.blogspot.com/feeds/3942480458856806752/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://programmings-java.blogspot.com/2021/07/constructor.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/4859863201677788807/posts/default/3942480458856806752'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/4859863201677788807/posts/default/3942480458856806752'/><link rel='alternate' type='text/html' href='https://programmings-java.blogspot.com/2021/07/constructor.html' title='Constructor'/><author><name>Mohd Talha</name><uri>http://www.blogger.com/profile/01763375607514819581</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjxqmEAYpFkKJ3VgJ8nlZoH4MV-4MTlcZmGevv-hviq0vn1lcxEzj5w8DQqGxpZTpDqkhTsynCZN1suQqei6wVPCi3WLbck4kYp68zwEDas8Q27pmtggUvOXA9dfbAg7sSCEaiQqAr1cg8-/s72-c/13.webp" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4859863201677788807.post-5506264115311258104</id><published>2021-07-02T19:47:00.003+05:30</published><updated>2021-07-02T19:47:35.760+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Theory"/><title type='text'>Arrays</title><content type='html'>&lt;h1 style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: x-large;&quot;&gt;Arrays&lt;/span&gt;&lt;/h1&gt;&lt;p style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;An array can be defined as a set of homogeneous data elements stored in contiguous memory locations.&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Declaring an Array&lt;/span&gt;&lt;/h3&gt;&lt;p style=&quot;text-align: left;&quot;&gt;&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;datatype arrayname[]; Eg: int a[];&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;datatype[] arrayname; Eg: int[] a;&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Creating an Array&lt;/span&gt;&lt;/h3&gt;&lt;p style=&quot;text-align: left;&quot;&gt;&lt;/p&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;To create blank array&lt;/span&gt;&lt;/li&gt;&lt;ul&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;arrayname = new datatype[size];&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Eg: ar[] = new int[5];&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;To create filled array&lt;/span&gt;&lt;/li&gt;&lt;ul&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;arrayname = {value1, value2, ...};&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Eg: ar = {3, 4, 5};&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/ul&gt;&lt;div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj4UGlfU08Q71m-13uU-n4LZYWXnRV5yTQrzn8mnXCf9gXTZj4PS6Rk96NT3GdtCH_ama2aklO_yeiqQdI5NGM8cztJr7eGao2vkcyJkoUdI9zfqRdzzBnezdjbNRYilefI43J3NzahSSxl/s1012/what-are-arrays-in-java.webp&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;740&quot; data-original-width=&quot;1012&quot; height=&quot;306&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj4UGlfU08Q71m-13uU-n4LZYWXnRV5yTQrzn8mnXCf9gXTZj4PS6Rk96NT3GdtCH_ama2aklO_yeiqQdI5NGM8cztJr7eGao2vkcyJkoUdI9zfqRdzzBnezdjbNRYilefI43J3NzahSSxl/w419-h306/what-are-arrays-in-java.webp&quot; width=&quot;419&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Array Initializer&lt;/span&gt;&lt;/h3&gt;&lt;p style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;An array initializer is a list of comma-separated values surrounded by braces.&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Eg: {3, 4, 5, 2, 8}&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Getting the size of the Array&lt;/span&gt;&lt;/h3&gt;&lt;p style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;We can get the size of the array using its &lt;b&gt;length property.&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Eg: int ar[] = new int[10];&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;int len = ar.length;&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;System.out.println(len);&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Advantages of using Arrays&lt;/span&gt;&lt;/h3&gt;&lt;div&gt;&lt;p&gt;&lt;/p&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;We can define as many variables as we want in a single statement.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;We can access each element using the loop. This reduces the size of the program.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;p&gt;&lt;/p&gt;&lt;h3&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Disadvantages of using Arrays&lt;/span&gt;&lt;/h3&gt;&lt;/div&gt;&lt;div&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Once an array is created we cannot change its size.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Difference between Linear Search and Binary Search&lt;/span&gt;&lt;/h3&gt;&lt;div&gt;&lt;span id=&quot;docs-internal-guid-85538087-7fff-a59b-a8d1-4a8665fe52c2&quot;&gt;&lt;div align=&quot;left&quot; dir=&quot;ltr&quot; style=&quot;margin-left: 0pt;&quot;&gt;&lt;table data-darkreader-inline-border-bottom=&quot;&quot; data-darkreader-inline-border-left=&quot;&quot; data-darkreader-inline-border-right=&quot;&quot; data-darkreader-inline-border-top=&quot;&quot; style=&quot;--darkreader-inline-border-bottom: initial; --darkreader-inline-border-left: initial; --darkreader-inline-border-right: initial; --darkreader-inline-border-top: initial; border-collapse: collapse; border: none; table-layout: fixed; width: 468pt;&quot;&gt;&lt;colgroup&gt;&lt;col&gt;&lt;/col&gt;&lt;col&gt;&lt;/col&gt;&lt;/colgroup&gt;&lt;tbody&gt;&lt;tr style=&quot;height: 0pt;&quot;&gt;&lt;td data-darkreader-inline-border-bottom=&quot;&quot; data-darkreader-inline-border-left=&quot;&quot; data-darkreader-inline-border-right=&quot;&quot; data-darkreader-inline-border-top=&quot;&quot; style=&quot;--darkreader-inline-border-bottom: #867d6f; --darkreader-inline-border-left: #867d6f; --darkreader-inline-border-right: #867d6f; --darkreader-inline-border-top: #867d6f; border-color: rgb(0, 0, 0); border-style: solid; border-width: 1pt; overflow-wrap: break-word; overflow: hidden; padding: 5pt; vertical-align: top;&quot;&gt;&lt;h4 style=&quot;line-height: 1.2; margin-bottom: 0pt; margin-top: 0pt;&quot;&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #dbd8d3; color: black; font-family: Arial; font-variant-east-asian: normal; font-variant-numeric: normal; font-weight: 700; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Linear Search&lt;/span&gt;&lt;/span&gt;&lt;/h4&gt;&lt;/td&gt;&lt;td data-darkreader-inline-border-bottom=&quot;&quot; data-darkreader-inline-border-left=&quot;&quot; data-darkreader-inline-border-right=&quot;&quot; data-darkreader-inline-border-top=&quot;&quot; style=&quot;--darkreader-inline-border-bottom: #867d6f; --darkreader-inline-border-left: #867d6f; --darkreader-inline-border-right: #867d6f; --darkreader-inline-border-top: #867d6f; border-color: rgb(0, 0, 0); border-style: solid; border-width: 1pt; overflow-wrap: break-word; overflow: hidden; padding: 5pt; vertical-align: top;&quot;&gt;&lt;h4 style=&quot;line-height: 1.2; margin-bottom: 0pt; margin-top: 0pt;&quot;&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #dbd8d3; color: black; font-family: Arial; font-variant-east-asian: normal; font-variant-numeric: normal; font-weight: 700; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Binary Search&lt;/span&gt;&lt;/span&gt;&lt;/h4&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr style=&quot;height: 0pt;&quot;&gt;&lt;td data-darkreader-inline-border-bottom=&quot;&quot; data-darkreader-inline-border-left=&quot;&quot; data-darkreader-inline-border-right=&quot;&quot; data-darkreader-inline-border-top=&quot;&quot; style=&quot;--darkreader-inline-border-bottom: #867d6f; --darkreader-inline-border-left: #867d6f; --darkreader-inline-border-right: #867d6f; --darkreader-inline-border-top: #867d6f; border-color: rgb(0, 0, 0); border-style: solid; border-width: 1pt; overflow-wrap: break-word; overflow: hidden; padding: 5pt; vertical-align: top;&quot;&gt;&lt;p dir=&quot;ltr&quot; style=&quot;line-height: 1.2; margin-bottom: 0pt; margin-top: 0pt;&quot;&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #dbd8d3; color: black; font-family: Arial; font-size: 11pt; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;It searches all the elements in a sequential way&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;td data-darkreader-inline-border-bottom=&quot;&quot; data-darkreader-inline-border-left=&quot;&quot; data-darkreader-inline-border-right=&quot;&quot; data-darkreader-inline-border-top=&quot;&quot; style=&quot;--darkreader-inline-border-bottom: #867d6f; --darkreader-inline-border-left: #867d6f; --darkreader-inline-border-right: #867d6f; --darkreader-inline-border-top: #867d6f; border-color: rgb(0, 0, 0); border-style: solid; border-width: 1pt; overflow-wrap: break-word; overflow: hidden; padding: 5pt; vertical-align: top;&quot;&gt;&lt;p dir=&quot;ltr&quot; style=&quot;line-height: 1.2; margin-bottom: 0pt; margin-top: 0pt;&quot;&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #dbd8d3; color: black; font-family: Arial; font-size: 11pt; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;It checks the middle element and then according to its value it checks either on the left side or right side and leaves the other part&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr style=&quot;height: 0pt;&quot;&gt;&lt;td data-darkreader-inline-border-bottom=&quot;&quot; data-darkreader-inline-border-left=&quot;&quot; data-darkreader-inline-border-right=&quot;&quot; data-darkreader-inline-border-top=&quot;&quot; style=&quot;--darkreader-inline-border-bottom: #867d6f; --darkreader-inline-border-left: #867d6f; --darkreader-inline-border-right: #867d6f; --darkreader-inline-border-top: #867d6f; border-color: rgb(0, 0, 0); border-style: solid; border-width: 1pt; overflow-wrap: break-word; overflow: hidden; padding: 5pt; vertical-align: top;&quot;&gt;&lt;p dir=&quot;ltr&quot; style=&quot;line-height: 1.2; margin-bottom: 0pt; margin-top: 0pt;&quot;&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #dbd8d3; color: black; font-family: Arial; font-size: 11pt; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;It can work on an unsorted array&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;td data-darkreader-inline-border-bottom=&quot;&quot; data-darkreader-inline-border-left=&quot;&quot; data-darkreader-inline-border-right=&quot;&quot; data-darkreader-inline-border-top=&quot;&quot; style=&quot;--darkreader-inline-border-bottom: #867d6f; --darkreader-inline-border-left: #867d6f; --darkreader-inline-border-right: #867d6f; --darkreader-inline-border-top: #867d6f; border-color: rgb(0, 0, 0); border-style: solid; border-width: 1pt; overflow-wrap: break-word; overflow: hidden; padding: 5pt; vertical-align: top;&quot;&gt;&lt;p dir=&quot;ltr&quot; style=&quot;line-height: 1.2; margin-bottom: 0pt; margin-top: 0pt;&quot;&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #dbd8d3; color: black; font-family: Arial; font-size: 11pt; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;It works on sorted arrays only&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr style=&quot;height: 0pt;&quot;&gt;&lt;td data-darkreader-inline-border-bottom=&quot;&quot; data-darkreader-inline-border-left=&quot;&quot; data-darkreader-inline-border-right=&quot;&quot; data-darkreader-inline-border-top=&quot;&quot; style=&quot;--darkreader-inline-border-bottom: #867d6f; --darkreader-inline-border-left: #867d6f; --darkreader-inline-border-right: #867d6f; --darkreader-inline-border-top: #867d6f; border-color: rgb(0, 0, 0); border-style: solid; border-width: 1pt; overflow-wrap: break-word; overflow: hidden; padding: 5pt; vertical-align: top;&quot;&gt;&lt;p dir=&quot;ltr&quot; style=&quot;line-height: 1.2; margin-bottom: 0pt; margin-top: 0pt;&quot;&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #dbd8d3; color: black; font-family: Arial; font-size: 11pt; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;It is slower than binary search&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;td data-darkreader-inline-border-bottom=&quot;&quot; data-darkreader-inline-border-left=&quot;&quot; data-darkreader-inline-border-right=&quot;&quot; data-darkreader-inline-border-top=&quot;&quot; style=&quot;--darkreader-inline-border-bottom: #867d6f; --darkreader-inline-border-left: #867d6f; --darkreader-inline-border-right: #867d6f; --darkreader-inline-border-top: #867d6f; border-color: rgb(0, 0, 0); border-style: solid; border-width: 1pt; overflow-wrap: break-word; overflow: hidden; padding: 5pt; vertical-align: top;&quot;&gt;&lt;p dir=&quot;ltr&quot; style=&quot;line-height: 1.2; margin-bottom: 0pt; margin-top: 0pt;&quot;&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #dbd8d3; color: black; font-family: Arial; font-size: 11pt; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;It is faster than linear search&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;/div&gt;&lt;div align=&quot;left&quot; dir=&quot;ltr&quot; style=&quot;margin-left: 0pt;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div align=&quot;left&quot; dir=&quot;ltr&quot; style=&quot;margin-left: 0pt;&quot;&gt;&lt;h3&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Difference between Bubble Sort and Selection Sort&lt;/span&gt;&lt;/h3&gt;&lt;/div&gt;&lt;div align=&quot;left&quot; dir=&quot;ltr&quot; style=&quot;margin-left: 0pt;&quot;&gt;&lt;span id=&quot;docs-internal-guid-be45875d-7fff-8025-a42f-4da7f1accf4b&quot;&gt;&lt;div align=&quot;left&quot; dir=&quot;ltr&quot; style=&quot;margin-left: 0pt;&quot;&gt;&lt;table style=&quot;border-collapse: collapse; border: none; table-layout: fixed; width: 468pt;&quot;&gt;&lt;colgroup&gt;&lt;col&gt;&lt;/col&gt;&lt;col&gt;&lt;/col&gt;&lt;/colgroup&gt;&lt;tbody&gt;&lt;tr style=&quot;height: 0pt;&quot;&gt;&lt;td style=&quot;border-bottom: solid #000000 1pt; border-left: solid #000000 1pt; border-right: solid #000000 1pt; border-top: solid #000000 1pt; overflow-wrap: break-word; overflow: hidden; padding: 5pt 5pt 5pt 5pt; vertical-align: top;&quot;&gt;&lt;h4 style=&quot;line-height: 1.2; margin-bottom: 0pt; margin-top: 0pt;&quot;&gt;&lt;span style=&quot;font-family: Arial; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Bubble Sort&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/h4&gt;&lt;/td&gt;&lt;td style=&quot;border-bottom: solid #000000 1pt; border-left: solid #000000 1pt; border-right: solid #000000 1pt; border-top: solid #000000 1pt; overflow-wrap: break-word; overflow: hidden; padding: 5pt 5pt 5pt 5pt; vertical-align: top;&quot;&gt;&lt;h4 style=&quot;line-height: 1.2; margin-bottom: 0pt; margin-top: 0pt;&quot;&gt;&lt;span style=&quot;font-family: Arial; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Selection Sort&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/h4&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr style=&quot;height: 0pt;&quot;&gt;&lt;td style=&quot;border-bottom: solid #000000 1pt; border-left: solid #000000 1pt; border-right: solid #000000 1pt; border-top: solid #000000 1pt; overflow-wrap: break-word; overflow: hidden; padding: 5pt 5pt 5pt 5pt; vertical-align: top;&quot;&gt;&lt;p dir=&quot;ltr&quot; style=&quot;line-height: 1.2; margin-bottom: 0pt; margin-top: 0pt;&quot;&gt;&lt;span style=&quot;font-family: Arial; font-size: 11pt; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;It compares adjacent elements, if they are not in order, swap them&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;td style=&quot;border-bottom: solid #000000 1pt; border-left: solid #000000 1pt; border-right: solid #000000 1pt; border-top: solid #000000 1pt; overflow-wrap: break-word; overflow: hidden; padding: 5pt 5pt 5pt 5pt; vertical-align: top;&quot;&gt;&lt;p dir=&quot;ltr&quot; style=&quot;line-height: 1.2; margin-bottom: 0pt; margin-top: 0pt;&quot;&gt;&lt;span style=&quot;font-family: Arial; font-size: 11pt; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;It finds the smallest element of the array and swaps it with the first element&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr style=&quot;height: 0pt;&quot;&gt;&lt;td style=&quot;border-bottom: solid #000000 1pt; border-left: solid #000000 1pt; border-right: solid #000000 1pt; border-top: solid #000000 1pt; overflow-wrap: break-word; overflow: hidden; padding: 5pt 5pt 5pt 5pt; vertical-align: top;&quot;&gt;&lt;p dir=&quot;ltr&quot; style=&quot;line-height: 1.2; margin-bottom: 0pt; margin-top: 0pt;&quot;&gt;&lt;span style=&quot;font-family: Arial; font-size: 11pt; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;It is slower than selection sort because a number of swapping are to be done in each pass.&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;td style=&quot;border-bottom: solid #000000 1pt; border-left: solid #000000 1pt; border-right: solid #000000 1pt; border-top: solid #000000 1pt; overflow-wrap: break-word; overflow: hidden; padding: 5pt 5pt 5pt 5pt; vertical-align: top;&quot;&gt;&lt;p dir=&quot;ltr&quot; style=&quot;line-height: 1.2; margin-bottom: 0pt; margin-top: 0pt;&quot;&gt;&lt;span style=&quot;font-family: Arial; font-size: 11pt; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;It is faster than bubble sort because only one swapping has to be done in each pass.&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;/span&gt;&lt;/div&gt;&lt;/span&gt;&lt;/div&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='https://programmings-java.blogspot.com/feeds/5506264115311258104/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://programmings-java.blogspot.com/2021/07/arrays.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/4859863201677788807/posts/default/5506264115311258104'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/4859863201677788807/posts/default/5506264115311258104'/><link rel='alternate' type='text/html' href='https://programmings-java.blogspot.com/2021/07/arrays.html' title='Arrays'/><author><name>Mohd Talha</name><uri>http://www.blogger.com/profile/01763375607514819581</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj4UGlfU08Q71m-13uU-n4LZYWXnRV5yTQrzn8mnXCf9gXTZj4PS6Rk96NT3GdtCH_ama2aklO_yeiqQdI5NGM8cztJr7eGao2vkcyJkoUdI9zfqRdzzBnezdjbNRYilefI43J3NzahSSxl/s72-w419-h306-c/what-are-arrays-in-java.webp" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4859863201677788807.post-7893400838852808152</id><published>2021-01-13T08:24:00.007+05:30</published><updated>2021-06-12T14:08:23.071+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Algorithms/Sortings"/><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><title type='text'>Linked List Cycle II Program (Leetcode)</title><content type='html'>&lt;span style=&quot;font-size: x-large;&quot;&gt;&lt;b&gt;&lt;u&gt;Linked List Cycle II Program (Leetcode):&lt;/u&gt;&lt;/b&gt;&lt;/span&gt;&lt;div&gt;&lt;span style=&quot;font-size: x-large;&quot;&gt;&lt;b&gt;&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;Given a linked list, return the node where the cycle begins. If there is no cycle, return&amp;nbsp;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;null&lt;/code&gt;.&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the&amp;nbsp;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;next&lt;/code&gt;&amp;nbsp;pointer. Internally,&amp;nbsp;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;pos&lt;/code&gt;&amp;nbsp;is used to denote the index of the node that&amp;nbsp;tail&#39;s&amp;nbsp;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;next&lt;/code&gt;&amp;nbsp;pointer is connected to.&amp;nbsp;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Note that&amp;nbsp;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;pos&lt;/code&gt;&amp;nbsp;is not passed as a parameter&lt;/span&gt;.&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Notice&lt;/span&gt;&amp;nbsp;that you&amp;nbsp;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;should not modify&lt;/span&gt;&amp;nbsp;the linked list.&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&amp;nbsp;&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Example 1:&lt;/span&gt;&lt;/p&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhCpECG6hlxMN5ZSQv5dij_m0saYAeff8MODDGgcFet6OmTjeGjnnxbFXefYnM2W97zraxzysQ-oV8fCCB45VjeREf77k1vwLv_cC5OdX3DdAWHPgQuUIuWPntVckbmRulBZiXxeHsiZBDM/&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;&quot; data-original-height=&quot;171&quot; data-original-width=&quot;531&quot; height=&quot;154&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhCpECG6hlxMN5ZSQv5dij_m0saYAeff8MODDGgcFet6OmTjeGjnnxbFXefYnM2W97zraxzysQ-oV8fCCB45VjeREf77k1vwLv_cC5OdX3DdAWHPgQuUIuWPntVckbmRulBZiXxeHsiZBDM/w398-h154/image.png&quot; width=&quot;398&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;pre style=&quot;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; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Input:&lt;/span&gt; head = [3,2,0,-4], pos = 1
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Output:&lt;/span&gt; tail connects to node index 1
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Explanation:&lt;/span&gt; There is a cycle in the linked list, where tail connects to the second node.
&lt;/pre&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Example 2:&lt;/span&gt;&lt;/p&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh4eplxF0tFuzc0XF8UAOsaOdTmG8NfBCnWZ3nnb56wwE_QWcKgEy9JSX6uN_ZIHMhuK3Fn5GuuSEIwF7FXkHSoBAvrFSrikaxLAAuVeAR5Znyd6cV1-ar6jFZ1MCsYN8_QJKAQxSe516Br/&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;&quot; data-original-height=&quot;105&quot; data-original-width=&quot;201&quot; height=&quot;167&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh4eplxF0tFuzc0XF8UAOsaOdTmG8NfBCnWZ3nnb56wwE_QWcKgEy9JSX6uN_ZIHMhuK3Fn5GuuSEIwF7FXkHSoBAvrFSrikaxLAAuVeAR5Znyd6cV1-ar6jFZ1MCsYN8_QJKAQxSe516Br/&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;pre style=&quot;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; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Input:&lt;/span&gt; head = [1,2], pos = 0
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Output:&lt;/span&gt; tail connects to node index 0
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Explanation:&lt;/span&gt; There is a cycle in the linked list, where tail connects to the first node.
&lt;/pre&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Example 3:&lt;/span&gt;&lt;/p&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEifuxRifYVO-HoNScBkcz-NHdjjVLTQop80KZNolmpnKoApF-_SyXDRAXIsFGLODhwBJF2dXkzQjU-E-K7tXv-A_9O1zEVRwhYkFokX-jz73FBhor1PK7QEURLIOidjlOCdH3dETySifVcl/&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;&quot; data-original-height=&quot;65&quot; data-original-width=&quot;65&quot; height=&quot;88&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEifuxRifYVO-HoNScBkcz-NHdjjVLTQop80KZNolmpnKoApF-_SyXDRAXIsFGLODhwBJF2dXkzQjU-E-K7tXv-A_9O1zEVRwhYkFokX-jz73FBhor1PK7QEURLIOidjlOCdH3dETySifVcl/w88-h88/image.png&quot; width=&quot;88&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;pre style=&quot;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; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Input:&lt;/span&gt; head = [1], pos = -1
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Output:&lt;/span&gt; no cycle
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Explanation:&lt;/span&gt; There is no cycle in the linked list.
&lt;/pre&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&amp;nbsp;&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Constraints:&lt;/span&gt;&lt;/p&gt;&lt;ul style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;The number of the nodes in the list is in the range&amp;nbsp;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;[0, 10&lt;span style=&quot;box-sizing: border-box; line-height: 0; position: relative; top: -0.5em; vertical-align: baseline;&quot;&gt;4&lt;/span&gt;]&lt;/code&gt;.&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;-10&lt;span style=&quot;box-sizing: border-box; line-height: 0; position: relative; top: -0.5em; vertical-align: baseline;&quot;&gt;5&lt;/span&gt;&amp;nbsp;&amp;lt;= Node.val &amp;lt;= 10&lt;span style=&quot;box-sizing: border-box; line-height: 0; position: relative; top: -0.5em; vertical-align: baseline;&quot;&gt;5&lt;/span&gt;&lt;/code&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;pos&lt;/code&gt;&amp;nbsp;is&amp;nbsp;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;-1&lt;/code&gt;&amp;nbsp;or a&amp;nbsp;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;valid index&lt;/span&gt;&amp;nbsp;in the linked list.&lt;/li&gt;&lt;/ul&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&amp;nbsp;&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Follow up:&lt;/span&gt;&amp;nbsp;Can you solve it using&amp;nbsp;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;O(1)&lt;/code&gt;&amp;nbsp;(i.e. constant) memory?&lt;/p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;!--HTML generated using hilite.me--&gt;&lt;/span&gt;&lt;div data-darkreader-inline-bgcolor=&quot;&quot; data-darkreader-inline-bgimage=&quot;&quot; data-darkreader-inline-border-bottom=&quot;&quot; data-darkreader-inline-border-left=&quot;&quot; data-darkreader-inline-border-right=&quot;&quot; data-darkreader-inline-border-top=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #1d1e1f; --darkreader-inline-bgimage: initial; --darkreader-inline-border-bottom: #54595b; --darkreader-inline-border-left: #54595b; --darkreader-inline-border-right: #54595b; --darkreader-inline-border-top: #54595b; background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #968d82; color: #888888;&quot;&gt;# Definition for singly-linked list.&lt;/span&gt;
&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #968d82; color: #888888;&quot;&gt;# class ListNode:&lt;/span&gt;
&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #968d82; color: #888888;&quot;&gt;#     def __init__(self, x):&lt;/span&gt;
&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #968d82; color: #888888;&quot;&gt;#         self.val = x&lt;/span&gt;
&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #968d82; color: #888888;&quot;&gt;#         self.next = None&lt;/span&gt;

&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #74ea70; color: #008800;&quot;&gt;class&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #e44f9d; color: #bb0066;&quot;&gt;Solution&lt;/span&gt;:
    &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #74ea70; color: #008800;&quot;&gt;def&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #63aee4; color: #0066bb;&quot;&gt;detectCycle&lt;/span&gt;(&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #83eb9d; color: #007020;&quot;&gt;self&lt;/span&gt;, head: ListNode) &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #beb8b0; color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt; ListNode:
        first &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #beb8b0; color: #333333;&quot;&gt;=&lt;/span&gt; head
        second &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #beb8b0; color: #333333;&quot;&gt;=&lt;/span&gt; head
        
        &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #74ea70; color: #008800;&quot;&gt;while&lt;/span&gt; first &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #beb8b0; color: #333333;&quot;&gt;!=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #74ea70; color: #008800;&quot;&gt;None&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #dbd8d3; color: black;&quot;&gt;and&lt;/span&gt; second &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #beb8b0; color: #333333;&quot;&gt;!=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #74ea70; color: #008800;&quot;&gt;None&lt;/span&gt;:
            &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #74ea70; color: #008800;&quot;&gt;if&lt;/span&gt; first&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #beb8b0; color: #333333;&quot;&gt;.&lt;/span&gt;next &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #beb8b0; color: #333333;&quot;&gt;==&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #74ea70; color: #008800;&quot;&gt;None&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #dbd8d3; color: black;&quot;&gt;or&lt;/span&gt; second&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #beb8b0; color: #333333;&quot;&gt;.&lt;/span&gt;next &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #beb8b0; color: #333333;&quot;&gt;==&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #74ea70; color: #008800;&quot;&gt;None&lt;/span&gt;:
                &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #74ea70; color: #008800;&quot;&gt;return&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #74ea70; color: #008800;&quot;&gt;None&lt;/span&gt;
            
            first &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #beb8b0; color: #333333;&quot;&gt;=&lt;/span&gt; first&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #beb8b0; color: #333333;&quot;&gt;.&lt;/span&gt;next
            second &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #beb8b0; color: #333333;&quot;&gt;=&lt;/span&gt; second&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #beb8b0; color: #333333;&quot;&gt;.&lt;/span&gt;next&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #beb8b0; color: #333333;&quot;&gt;.&lt;/span&gt;next
            
            
            &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #74ea70; color: #008800;&quot;&gt;if&lt;/span&gt; first &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #beb8b0; color: #333333;&quot;&gt;==&lt;/span&gt; second:
                first &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #beb8b0; color: #333333;&quot;&gt;=&lt;/span&gt; head
                &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #74ea70; color: #008800;&quot;&gt;break&lt;/span&gt;
        &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #74ea70; color: #008800;&quot;&gt;if&lt;/span&gt; first &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #beb8b0; color: #333333;&quot;&gt;==&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #74ea70; color: #008800;&quot;&gt;None&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #dbd8d3; color: black;&quot;&gt;or&lt;/span&gt; second &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #beb8b0; color: #333333;&quot;&gt;==&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #74ea70; color: #008800;&quot;&gt;None&lt;/span&gt;:
            &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #74ea70; color: #008800;&quot;&gt;return&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #74ea70; color: #008800;&quot;&gt;None&lt;/span&gt;
        &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #74ea70; color: #008800;&quot;&gt;while&lt;/span&gt; first &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #beb8b0; color: #333333;&quot;&gt;!=&lt;/span&gt; second:
            first &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #beb8b0; color: #333333;&quot;&gt;=&lt;/span&gt; first&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #beb8b0; color: #333333;&quot;&gt;.&lt;/span&gt;next
            second &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #beb8b0; color: #333333;&quot;&gt;=&lt;/span&gt; second&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #beb8b0; color: #333333;&quot;&gt;.&lt;/span&gt;next
        &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #74ea70; color: #008800;&quot;&gt;return&lt;/span&gt; first&lt;/span&gt;&lt;b style=&quot;font-size: xx-large; text-decoration-line: underline;&quot;&gt;
&lt;/b&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-size: x-large;&quot;&gt;&lt;b&gt;&lt;u&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;/u&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-size: x-large;&quot;&gt;&lt;b&gt;&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;

&lt;div style=&quot;text-align: center;&quot;&gt;&lt;iframe allow=&quot;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture&quot; allowfullscreen=&quot;&quot; frameborder=&quot;0&quot; height=&quot;538&quot; src=&quot;https://www.youtube.com/embed/2Cm0cw7asTY&quot; width=&quot;956&quot;&gt;&lt;/iframe&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://programmings-java.blogspot.com/feeds/7893400838852808152/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://programmings-java.blogspot.com/2021/01/linked-list-cycle-ii-program-leetcode.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/4859863201677788807/posts/default/7893400838852808152'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/4859863201677788807/posts/default/7893400838852808152'/><link rel='alternate' type='text/html' href='https://programmings-java.blogspot.com/2021/01/linked-list-cycle-ii-program-leetcode.html' title='Linked List Cycle II Program (Leetcode)'/><author><name>Mohd Talha</name><uri>http://www.blogger.com/profile/01763375607514819581</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhCpECG6hlxMN5ZSQv5dij_m0saYAeff8MODDGgcFet6OmTjeGjnnxbFXefYnM2W97zraxzysQ-oV8fCCB45VjeREf77k1vwLv_cC5OdX3DdAWHPgQuUIuWPntVckbmRulBZiXxeHsiZBDM/s72-w398-h154-c/image.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4859863201677788807.post-2148546079544176108</id><published>2020-12-26T10:33:00.005+05:30</published><updated>2021-06-12T14:09:33.116+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Algorithms/Sortings"/><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><title type='text'>Reorder List Program (Leetcode)</title><content type='html'>
&lt;p&gt;&lt;b&gt;&lt;u&gt;&lt;span style=&quot;font-size: x-large;&quot;&gt;Reorder List Program (Leetcode:&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;You are given the head of a singly linked list. The list can be represented as:&lt;/p&gt;&lt;pre style=&quot;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; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; white-space: pre-wrap;&quot;&gt;L&lt;span style=&quot;bottom: -0.25em; box-sizing: border-box; line-height: 0; position: relative; vertical-align: baseline;&quot;&gt;0&lt;/span&gt; → L&lt;span style=&quot;bottom: -0.25em; box-sizing: border-box; line-height: 0; position: relative; vertical-align: baseline;&quot;&gt;1&lt;/span&gt; → … → L&lt;span style=&quot;bottom: -0.25em; box-sizing: border-box; line-height: 0; position: relative; vertical-align: baseline;&quot;&gt;n - 1&lt;/span&gt; → L&lt;span style=&quot;bottom: -0.25em; box-sizing: border-box; line-height: 0; position: relative; vertical-align: baseline;&quot;&gt;n&lt;/span&gt;
&lt;/pre&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;em style=&quot;box-sizing: border-box;&quot;&gt;Reorder the list to be on the following form:&lt;/em&gt;&lt;/p&gt;&lt;pre style=&quot;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; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; white-space: pre-wrap;&quot;&gt;L&lt;span style=&quot;bottom: -0.25em; box-sizing: border-box; line-height: 0; position: relative; vertical-align: baseline;&quot;&gt;0&lt;/span&gt; → L&lt;span style=&quot;bottom: -0.25em; box-sizing: border-box; line-height: 0; position: relative; vertical-align: baseline;&quot;&gt;n&lt;/span&gt; → L&lt;span style=&quot;bottom: -0.25em; box-sizing: border-box; line-height: 0; position: relative; vertical-align: baseline;&quot;&gt;1&lt;/span&gt; → L&lt;span style=&quot;bottom: -0.25em; box-sizing: border-box; line-height: 0; position: relative; vertical-align: baseline;&quot;&gt;n - 1&lt;/span&gt; → L&lt;span style=&quot;bottom: -0.25em; box-sizing: border-box; line-height: 0; position: relative; vertical-align: baseline;&quot;&gt;2&lt;/span&gt; → L&lt;span style=&quot;bottom: -0.25em; box-sizing: border-box; line-height: 0; position: relative; vertical-align: baseline;&quot;&gt;n - 2&lt;/span&gt; → …
&lt;/pre&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;You may not modify the values in the list&#39;s nodes. Only nodes themselves may be changed.&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&amp;nbsp;&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Example 1:&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center;&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;https://assets.leetcode.com/uploads/2021/03/04/reorder1linked-list.jpg&quot; style=&quot;background-color: white; border-style: none; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; height: 222px; max-width: 100%; vertical-align: middle; width: 422px;&quot; /&gt;&lt;span face=&quot;-apple-system, BlinkMacSystemFont, &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;&quot; style=&quot;background-color: white; color: #263238;&quot;&gt;&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;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; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Input:&lt;/span&gt; head = [1,2,3,4]
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Output:&lt;/span&gt; [1,4,2,3]
&lt;/pre&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Example 2:&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center;&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;https://assets.leetcode.com/uploads/2021/03/09/reorder2-linked-list.jpg&quot; style=&quot;background-color: white; border-style: none; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; height: 222px; max-width: 100%; vertical-align: middle; width: 542px;&quot; /&gt;&lt;span face=&quot;-apple-system, BlinkMacSystemFont, &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;&quot; style=&quot;background-color: white; color: #263238;&quot;&gt;&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;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; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Input:&lt;/span&gt; head = [1,2,3,4,5]
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Output:&lt;/span&gt; [1,5,2,4,3]
&lt;/pre&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&amp;nbsp;&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Constraints:&lt;/span&gt;&lt;/p&gt;&lt;ul style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;The number of nodes in the list is in the range&amp;nbsp;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;[1, 5 * 10&lt;span style=&quot;box-sizing: border-box; line-height: 0; position: relative; top: -0.5em; vertical-align: baseline;&quot;&gt;4&lt;/span&gt;]&lt;/code&gt;.&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;1 &amp;lt;= Node.val &amp;lt;= 1000&lt;/code&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;!--HTML generated using hilite.me--&gt;&lt;/p&gt;&lt;div data-darkreader-inline-bgcolor=&quot;&quot; data-darkreader-inline-bgimage=&quot;&quot; data-darkreader-inline-border-bottom=&quot;&quot; data-darkreader-inline-border-left=&quot;&quot; data-darkreader-inline-border-right=&quot;&quot; data-darkreader-inline-border-top=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #1c1d1d; --darkreader-inline-bgimage: initial; --darkreader-inline-border-bottom: #505556; --darkreader-inline-border-left: #505556; --darkreader-inline-border-right: #505556; --darkreader-inline-border-top: #505556; background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;print&lt;/span&gt; &lt;span data-darkreader-inline-bgcolor=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #360808; background-color: #fff0f0;&quot;&gt;&#39;hello world!&#39;&lt;/span&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #8e867b; color: #888888;&quot;&gt;# Definition for singly-linked list.&lt;/span&gt;
&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #8e867b; color: #888888;&quot;&gt;# class ListNode:&lt;/span&gt;
&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #8e867b; color: #888888;&quot;&gt;#     def __init__(self, val=0, next=None):&lt;/span&gt;
&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #8e867b; color: #888888;&quot;&gt;#         self.val = val&lt;/span&gt;
&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #8e867b; color: #888888;&quot;&gt;#         self.next = next&lt;/span&gt;
&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d84b95; color: #bb0066; font-weight: bold;&quot;&gt;Solution&lt;/span&gt;:
    &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;def&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #5ea5d9; color: #0066bb; font-weight: bold;&quot;&gt;reorderList&lt;/span&gt;(&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;self&lt;/span&gt;, head: ListNode) &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;None&lt;/span&gt;:
        &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d0cdc8; color: black; font-weight: bold;&quot;&gt;not&lt;/span&gt; head:
            &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt;
        
        temp &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; head
        mid &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; head
        
        &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt; temp &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;!=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;None&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d0cdc8; color: black; font-weight: bold;&quot;&gt;and&lt;/span&gt; temp&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;next &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;!=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;None&lt;/span&gt;:
            mid &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; mid&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;next
            temp &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; temp&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;next&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;next
            
        rev &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;self&lt;/span&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;reverseList(mid)
        
        new &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; head
        &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt; rev&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;next &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;!=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;None&lt;/span&gt;:
            temp_new &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; new&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;next
            new&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;next &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; rev
            
            temp_rev &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; rev&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;next
            rev&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;next &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; temp_new
            
            new &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; temp_new
            rev &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; temp_rev
            
    &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;def&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #5ea5d9; color: #0066bb; font-weight: bold;&quot;&gt;reverseList&lt;/span&gt;(&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;self&lt;/span&gt;, head):
        p1 &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;None&lt;/span&gt;
        p2 &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; head
        
        &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt; p2 &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;!=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;None&lt;/span&gt;:
            p3 &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; p2&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;next
            p2&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;next &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; p1
            p1 &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; p2
            p2 &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; p3
        &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; p1&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;iframe allow=&quot;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture&quot; allowfullscreen=&quot;&quot; frameborder=&quot;0&quot; height=&quot;538&quot; src=&quot;https://www.youtube.com/embed/mc2MVMsXX7s&quot; width=&quot;956&quot;&gt;&lt;/iframe&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='https://programmings-java.blogspot.com/feeds/2148546079544176108/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://programmings-java.blogspot.com/2020/12/reorder-list-program-leetcode.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/4859863201677788807/posts/default/2148546079544176108'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/4859863201677788807/posts/default/2148546079544176108'/><link rel='alternate' type='text/html' href='https://programmings-java.blogspot.com/2020/12/reorder-list-program-leetcode.html' title='Reorder List Program (Leetcode)'/><author><name>Mohd Talha</name><uri>http://www.blogger.com/profile/01763375607514819581</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://img.youtube.com/vi/mc2MVMsXX7s/default.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4859863201677788807.post-212895035897691878</id><published>2020-12-16T17:57:00.005+05:30</published><updated>2024-02-11T20:37:35.932+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Algorithms/Sortings"/><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><title type='text'>Counting Bits Program (Leetcode)</title><content type='html'>&lt;meta content=&quot;Counting Bits Program, Counting Bits Program Python, Leetcode question, Leetcode question solution, Python program, Programmings-Java&quot; name=&quot;keywords&quot;&gt;&lt;/meta&gt;	&lt;link rel=&quot;canonical&quot; href=&quot;https://programmings-java.blogspot.com/2020/12/counting-bits-program-leetcode.html&quot;/&gt;
&lt;p&gt;&lt;span style=&quot;font-size: x-large;&quot;&gt;&lt;b&gt;&lt;u&gt;Counting Bits Program (Leetcode):&lt;/u&gt;&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;text-align: justify;&quot;&gt;The Counting Bits program on LeetCode is a dynamic programming problem designed to calculate the number of 1 bits in the binary representation of all numbers from 0 to a given integer n. The goal is to efficiently generate an array where each element represents the count of set bits (1s) in the binary representation of its corresponding index. The dynamic programming approach involves observing patterns in binary numbers to determine the number of set bits. By using previously computed results and bitwise operations, the program optimally computes the count of set bits for each number in the range. This problem challenges programmers to develop efficient algorithms that minimize time complexity and memory usage while accurately counting the bits for large integers.&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;Given an integer&amp;nbsp;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;n&lt;/code&gt;, return&amp;nbsp;&lt;em style=&quot;box-sizing: border-box;&quot;&gt;an array&amp;nbsp;&lt;/em&gt;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;ans&lt;/code&gt;&lt;em style=&quot;box-sizing: border-box;&quot;&gt;&amp;nbsp;of length&amp;nbsp;&lt;/em&gt;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;n + 1&lt;/code&gt;&lt;em style=&quot;box-sizing: border-box;&quot;&gt;&amp;nbsp;such that for each&amp;nbsp;&lt;/em&gt;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;i&lt;/code&gt;&lt;em style=&quot;box-sizing: border-box;&quot;&gt;&amp;nbsp;&lt;/em&gt;(&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;0 &amp;lt;= i &amp;lt;= n&lt;/code&gt;)&lt;em style=&quot;box-sizing: border-box;&quot;&gt;,&amp;nbsp;&lt;/em&gt;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;ans[i]&lt;/code&gt;&lt;em style=&quot;box-sizing: border-box;&quot;&gt;&amp;nbsp;is the&amp;nbsp;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;number of&amp;nbsp;&lt;/span&gt;&lt;/em&gt;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;1&lt;/code&gt;&lt;em style=&quot;box-sizing: border-box;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;&#39;s&lt;/span&gt;&amp;nbsp;in the binary representation of&amp;nbsp;&lt;/em&gt;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;i&lt;/code&gt;.&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&amp;nbsp;&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Example 1:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;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; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Input:&lt;/span&gt; n = 2
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Output:&lt;/span&gt; [0,1,1]
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Explanation:&lt;/span&gt;
0 --&amp;gt; 0
1 --&amp;gt; 1
2 --&amp;gt; 10
&lt;/pre&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Example 2:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;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; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Input:&lt;/span&gt; n = 5
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Output:&lt;/span&gt; [0,1,1,2,1,2]
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Explanation:&lt;/span&gt;
0 --&amp;gt; 0
1 --&amp;gt; 1
2 --&amp;gt; 10
3 --&amp;gt; 11
4 --&amp;gt; 100
5 --&amp;gt; 101
&lt;/pre&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&amp;nbsp;&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Constraints:&lt;/span&gt;&lt;/p&gt;&lt;ul style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;0 &amp;lt;= n &amp;lt;= 10&lt;span style=&quot;box-sizing: border-box; line-height: 0; position: relative; top: -0.5em; vertical-align: baseline;&quot;&gt;5&lt;/span&gt;&lt;/code&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&amp;nbsp;&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Follow up:&lt;/span&gt;&lt;/p&gt;&lt;ul style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;It is very easy to come up with a solution with a runtime of&amp;nbsp;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;O(n log n)&lt;/code&gt;. Can you do it in linear time?&amp;nbsp;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;O(n)&lt;/code&gt;&amp;nbsp;and possibly in a single pass?&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;Can you do it without using any built-in function (i.e., like&amp;nbsp;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;__builtin_popcount&lt;/code&gt;&amp;nbsp;in C++)?&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span face=&quot;-apple-system, BlinkMacSystemFont, Segoe UI, PingFang SC, Hiragino Sans GB, Microsoft YaHei, Helvetica Neue, Helvetica, Arial, sans-serif, Apple Color Emoji, Segoe UI Emoji, Segoe UI Symbol&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Want to check about the Latest Electronic Devices:&amp;nbsp;&lt;a href=&quot;https://comparekare.in/&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;CompareKarein&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;p&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;!--HTML generated using hilite.me--&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #bb0066; font-weight: bold;&quot;&gt;Solution&lt;/span&gt;:
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;countBits&lt;/span&gt;(&lt;span style=&quot;color: #007020;&quot;&gt;self&lt;/span&gt;, num: &lt;span style=&quot;color: #007020;&quot;&gt;int&lt;/span&gt;) &lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt; List[&lt;span style=&quot;color: #007020;&quot;&gt;int&lt;/span&gt;]:
        
        ans &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; [&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;] &lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt; (num &lt;span style=&quot;color: #333333;&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;)
        &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: #007020;&quot;&gt;range&lt;/span&gt;(num &lt;span style=&quot;color: #333333;&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;):
            &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; i &lt;span style=&quot;color: #333333;&quot;&gt;%&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;2&lt;/span&gt; &lt;span style=&quot;color: #333333;&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;:
                ans[i] &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; ans[i&lt;span style=&quot;color: #333333;&quot;&gt;//&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;2&lt;/span&gt;]
            &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt;:
                ans[i] &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; ans[i &lt;span style=&quot;color: #333333;&quot;&gt;-&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;] &lt;span style=&quot;color: #333333;&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;
        &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; ans
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;
&lt;/span&gt;&lt;p&gt;&lt;/p&gt;

&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;iframe allow=&quot;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture&quot; allowfullscreen=&quot;&quot; frameborder=&quot;0&quot; height=&quot;538&quot; src=&quot;https://www.youtube.com/embed/JxaCV7LIH6g&quot; width=&quot;956&quot;&gt;&lt;/iframe&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://programmings-java.blogspot.com/feeds/212895035897691878/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://programmings-java.blogspot.com/2020/12/counting-bits-program-leetcode.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/4859863201677788807/posts/default/212895035897691878'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/4859863201677788807/posts/default/212895035897691878'/><link rel='alternate' type='text/html' href='https://programmings-java.blogspot.com/2020/12/counting-bits-program-leetcode.html' title='Counting Bits Program (Leetcode)'/><author><name>Mohd Talha</name><uri>http://www.blogger.com/profile/01763375607514819581</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://img.youtube.com/vi/JxaCV7LIH6g/default.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4859863201677788807.post-8663185788919490417</id><published>2020-12-09T15:15:00.008+05:30</published><updated>2024-02-11T20:19:06.143+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Algorithms/Sortings"/><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><title type='text'>Linked List Cycle Program (Leetcode)</title><content type='html'>&lt;p&gt;&lt;span style=&quot;font-size: x-large;&quot;&gt;&lt;b&gt;&lt;u&gt;Linked List Cycle Program (Leetcode):&lt;/u&gt;&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;background-color: white; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; text-align: justify;&quot;&gt;The Linked List Cycle program is a classic problem in computer science, often encountered in technical interviews and coding challenges. The task involves detecting whether a linked list contains a cycle, where a cycle occurs if any node in the list points back to a previous node, creating a loop. The challenge lies in efficiently determining whether such a cycle exists within the linked list. The typical approach involves using two pointers, known as the slow and fast pointers, to traverse the list. The slow pointer moves one node at a time, while the fast pointer moves two nodes at a time. If there is a cycle, the fast pointer will eventually catch up to the slow pointer. By detecting this collision, the program can conclude that a cycle exists. Implementing this algorithm effectively requires careful consideration of edge cases and pointer manipulation to ensure accuracy and efficiency.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;background-color: white; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;;&quot;&gt;Given&amp;nbsp;&lt;/span&gt;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;head&lt;/code&gt;&lt;span style=&quot;background-color: white; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;;&quot;&gt;, the head of a linked list, determines if the linked list has a cycle in it.&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the&amp;nbsp;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;next&lt;/code&gt;&amp;nbsp;pointer. Internally,&amp;nbsp;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;pos&lt;/code&gt;&amp;nbsp;is used to denote the index of the node that the tail&#39;s&amp;nbsp;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;next&lt;/code&gt;&amp;nbsp;pointer is connected to.&amp;nbsp;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Note that&amp;nbsp;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;pos&lt;/code&gt;&amp;nbsp;is not passed as a parameter&lt;/span&gt;.&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;Return&amp;nbsp;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;true&lt;/code&gt;&lt;em style=&quot;box-sizing: border-box;&quot;&gt;&amp;nbsp;if there is a cycle in the linked list&lt;/em&gt;. Otherwise, return&amp;nbsp;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;false&lt;/code&gt;.&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&amp;nbsp;&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Example 1:&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center;&quot;&gt;&lt;span&gt;&lt;img alt=&quot;&quot; height=&quot;108&quot; src=&quot;https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png&quot; style=&quot;background-color: white; border-style: none; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; height: 97px; margin-bottom: 8px; margin-top: 8px; max-width: 100%; vertical-align: middle; width: 300px;&quot; width=&quot;334&quot; /&gt;&lt;span face=&quot;-apple-system, BlinkMacSystemFont, &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;&quot; style=&quot;background-color: white; color: #263238;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;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; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Input:&lt;/span&gt; head = [3,2,0,-4], pos = 1
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Output:&lt;/span&gt; true
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Explanation:&lt;/span&gt; There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
&lt;/pre&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Example 2:&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center;&quot;&gt;&lt;span&gt;&lt;img alt=&quot;&quot; height=&quot;99&quot; src=&quot;https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png&quot; style=&quot;background-color: white; border-style: none; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; height: 74px; max-width: 100%; vertical-align: middle; width: 141px;&quot; width=&quot;189&quot; /&gt;&lt;span face=&quot;-apple-system, BlinkMacSystemFont, &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;&quot; style=&quot;background-color: white; color: #263238;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;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; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Input:&lt;/span&gt; head = [1,2], pos = 0
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Output:&lt;/span&gt; true
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Explanation:&lt;/span&gt; There is a cycle in the linked list, where the tail connects to the 0th node.
&lt;/pre&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Example 3:&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span&gt;&lt;img alt=&quot;&quot; src=&quot;https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png&quot; style=&quot;background-color: white; border-style: none; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; height: 45px; max-width: 100%; vertical-align: middle; width: 45px;&quot; /&gt;&lt;span face=&quot;-apple-system, BlinkMacSystemFont, &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;&quot; style=&quot;background-color: white; color: #263238;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;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; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Input:&lt;/span&gt; head = [1], pos = -1
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Output:&lt;/span&gt; false
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Explanation:&lt;/span&gt; There is no cycle in the linked list.
&lt;/pre&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&amp;nbsp;&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Constraints:&lt;/span&gt;&lt;/p&gt;&lt;ul style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;The number of the nodes in the list is in the range&amp;nbsp;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;[0, 10&lt;span style=&quot;box-sizing: border-box; line-height: 0; position: relative; top: -0.5em; vertical-align: baseline;&quot;&gt;4&lt;/span&gt;]&lt;/code&gt;.&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;-10&lt;span style=&quot;box-sizing: border-box; line-height: 0; position: relative; top: -0.5em; vertical-align: baseline;&quot;&gt;5&lt;/span&gt;&amp;nbsp;&amp;lt;= Node.val &amp;lt;= 10&lt;span style=&quot;box-sizing: border-box; line-height: 0; position: relative; top: -0.5em; vertical-align: baseline;&quot;&gt;5&lt;/span&gt;&lt;/code&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;pos&lt;/code&gt;&amp;nbsp;is&amp;nbsp;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;-1&lt;/code&gt;&amp;nbsp;or a&amp;nbsp;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;valid index&lt;/span&gt;&amp;nbsp;in the linked list.&lt;/li&gt;&lt;/ul&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Follow up:&lt;/span&gt;&amp;nbsp;Can you solve it using&amp;nbsp;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;O(1)&lt;/code&gt;&amp;nbsp;(i.e. constant) memory?&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;color: black; font-family: &amp;quot;Times New Roman&amp;quot;; font-size: large;&quot;&gt;Want to check about the Latest Electronic Devices:&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;https://comparekare.in/&quot; rel=&quot;nofollow&quot; style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;; font-size: large;&quot; target=&quot;_blank&quot;&gt;CompareKarein&lt;/a&gt;&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;br /&gt;&lt;/p&gt;&lt;div data-darkreader-inline-bgcolor=&quot;&quot; data-darkreader-inline-bgimage=&quot;&quot; data-darkreader-inline-border-bottom=&quot;&quot; data-darkreader-inline-border-left=&quot;&quot; data-darkreader-inline-border-right=&quot;&quot; data-darkreader-inline-border-top=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #1c1d1d; --darkreader-inline-bgimage: initial; --darkreader-inline-border-bottom: #505556; --darkreader-inline-border-left: #505556; --darkreader-inline-border-right: #505556; --darkreader-inline-border-top: #505556; background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span color=&quot;var(--darkreader-inline-color)&quot; data-darkreader-inline-bgcolor=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #1c1d1d; background-color: white; font-size: large;&quot;&gt;# Definition for singly-linked list.&lt;/span&gt;&lt;/pre&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #8e867b; color: #888888;&quot;&gt;# class ListNode:&lt;/span&gt;
&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #8e867b; color: #888888;&quot;&gt;#     def __init__(self, x):&lt;/span&gt;
&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #8e867b; color: #888888;&quot;&gt;#         self.val = x&lt;/span&gt;
&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #8e867b; color: #888888;&quot;&gt;#         self.next = None&lt;/span&gt;

&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800;&quot;&gt;class&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d84b95; color: #bb0066;&quot;&gt;Solution&lt;/span&gt;:
    &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800;&quot;&gt;def&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #5ea5d9; color: #0066bb;&quot;&gt;hasCycle&lt;/span&gt;(&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;self&lt;/span&gt;, head: ListNode) &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;bool&lt;/span&gt;:
        first &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; head
        second &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; head
        
        &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800;&quot;&gt;while&lt;/span&gt; first &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;!=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800;&quot;&gt;None&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d0cdc8; color: black;&quot;&gt;and&lt;/span&gt; second &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;!=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800;&quot;&gt;None&lt;/span&gt;:
            &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800;&quot;&gt;if&lt;/span&gt; first&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;next &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;==&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800;&quot;&gt;None&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d0cdc8; color: black;&quot;&gt;or&lt;/span&gt; second&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;next &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;==&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800;&quot;&gt;None&lt;/span&gt;:
                &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800;&quot;&gt;return&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800;&quot;&gt;False&lt;/span&gt;
            
            first &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; first&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;next
            second &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; second&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;next&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;next
            
            &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800;&quot;&gt;if&lt;/span&gt; first &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;==&lt;/span&gt; second:
                &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800;&quot;&gt;return&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800;&quot;&gt;True&lt;/span&gt;
            
        &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800;&quot;&gt;return&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800;&quot;&gt;False&lt;/span&gt;&lt;/span&gt;&lt;b style=&quot;font-size: xx-large; text-decoration-line: underline;&quot;&gt;
&lt;/b&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;span style=&quot;font-size: x-large;&quot;&gt;&lt;b&gt;&lt;u&gt;
&lt;/u&gt;&lt;/b&gt;&lt;/span&gt;&lt;p&gt;&lt;/p&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;br /&gt;&lt;/div&gt;


&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;iframe allow=&quot;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture&quot; allowfullscreen=&quot;&quot; frameborder=&quot;0&quot; height=&quot;538&quot; src=&quot;https://www.youtube.com/embed/2Cm0cw7asTY&quot; width=&quot;956&quot;&gt;&lt;/iframe&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://programmings-java.blogspot.com/feeds/8663185788919490417/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://programmings-java.blogspot.com/2020/12/linked-list-cycle-program-leetcode.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/4859863201677788807/posts/default/8663185788919490417'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/4859863201677788807/posts/default/8663185788919490417'/><link rel='alternate' type='text/html' href='https://programmings-java.blogspot.com/2020/12/linked-list-cycle-program-leetcode.html' title='Linked List Cycle Program (Leetcode)'/><author><name>Mohd Talha</name><uri>http://www.blogger.com/profile/01763375607514819581</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://img.youtube.com/vi/2Cm0cw7asTY/default.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4859863201677788807.post-4190136636409908854</id><published>2020-12-04T17:17:00.003+05:30</published><updated>2021-06-12T14:14:42.677+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Algorithms/Sortings"/><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><title type='text'>Remove Nth Node From End of List Program (Leetcode)</title><content type='html'>&lt;p&gt;&lt;b&gt;&lt;u&gt;&lt;span style=&quot;font-size: x-large;&quot;&gt;Remove Nth Node From End of List Program (Leetcode):&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;u&gt;&lt;span style=&quot;font-size: x-large;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;Given the&amp;nbsp;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;head&lt;/code&gt;&amp;nbsp;of a linked list, remove the&amp;nbsp;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;n&lt;span style=&quot;box-sizing: border-box; line-height: 0; position: relative; top: -0.5em; vertical-align: baseline;&quot;&gt;th&lt;/span&gt;&lt;/code&gt;&amp;nbsp;node from the end of the list and return its head.&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;font-weight: bolder;&quot;&gt;Example 1:&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center;&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;https://assets.leetcode.com/uploads/2020/10/03/remove_ex1.jpg&quot; style=&quot;background-color: white; border-style: none; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; height: 222px; max-width: 100%; vertical-align: middle; width: 542px;&quot; /&gt;&lt;span style=&quot;background-color: white; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;;&quot;&gt;&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;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; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Input:&lt;/span&gt; head = [1,2,3,4,5], n = 2
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Output:&lt;/span&gt; [1,2,3,5]
&lt;/pre&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Example 2:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;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; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Input:&lt;/span&gt; head = [1], n = 1
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Output:&lt;/span&gt; []
&lt;/pre&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Example 3:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;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; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Input:&lt;/span&gt; head = [1,2], n = 1
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Output:&lt;/span&gt; [1]
&lt;/pre&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&amp;nbsp;&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Constraints:&lt;/span&gt;&lt;/p&gt;&lt;ul style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;The number of nodes in the list is&amp;nbsp;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;sz&lt;/code&gt;.&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;1 &amp;lt;= sz &amp;lt;= 30&lt;/code&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;0 &amp;lt;= Node.val &amp;lt;= 100&lt;/code&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;1 &amp;lt;= n &amp;lt;= sz&lt;/code&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&amp;nbsp;&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Follow up:&lt;/span&gt;&amp;nbsp;Could you do this in one pass?&lt;/p&gt;&lt;p&gt;&lt;!--HTML generated using hilite.me--&gt;&lt;/p&gt;&lt;div data-darkreader-inline-bgcolor=&quot;&quot; data-darkreader-inline-bgimage=&quot;&quot; data-darkreader-inline-border-bottom=&quot;&quot; data-darkreader-inline-border-left=&quot;&quot; data-darkreader-inline-border-right=&quot;&quot; data-darkreader-inline-border-top=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #1c1d1d; --darkreader-inline-bgimage: initial; --darkreader-inline-border-bottom: #505556; --darkreader-inline-border-left: #505556; --darkreader-inline-border-right: #505556; --darkreader-inline-border-top: #505556; background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #8e867b; color: #888888;&quot;&gt;# Definition for singly-linked list.&lt;/span&gt;
&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #8e867b; color: #888888;&quot;&gt;# class ListNode:&lt;/span&gt;
&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #8e867b; color: #888888;&quot;&gt;#     def __init__(self, val=0, next=None):&lt;/span&gt;
&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #8e867b; color: #888888;&quot;&gt;#         self.val = val&lt;/span&gt;
&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #8e867b; color: #888888;&quot;&gt;#         self.next = next&lt;/span&gt;
&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d84b95; color: #bb0066; font-weight: bold;&quot;&gt;Solution&lt;/span&gt;:
    &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;def&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #5ea5d9; color: #0066bb; font-weight: bold;&quot;&gt;removeNthFromEnd&lt;/span&gt;(&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;self&lt;/span&gt;, head: ListNode, n: &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;int&lt;/span&gt;) &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt; ListNode:
        count &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;
        start &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; head
        last &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; head
        
        &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt; count &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;&amp;lt;=&lt;/span&gt; n:
            count &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;+=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;
            last &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; last&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;next
            
        &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; last &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;==&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;None&lt;/span&gt;:
            head &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; head&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;next
            &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; head
            
        &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt; last&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;next &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;!=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;None&lt;/span&gt;:
            start &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; start&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;next
            last &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; last&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;next
            
        start&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;next &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; start&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;next&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;next
        &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; head
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;br /&gt;&lt;/div&gt;


&lt;div style=&quot;text-align: center;&quot;&gt;&lt;iframe allow=&quot;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture&quot; allowfullscreen=&quot;&quot; frameborder=&quot;0&quot; height=&quot;538&quot; src=&quot;https://www.youtube.com/embed/W68cM44cw18&quot; width=&quot;956&quot;&gt;&lt;/iframe&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://programmings-java.blogspot.com/feeds/4190136636409908854/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://programmings-java.blogspot.com/2020/12/remove-nth-node-from-end-of-list.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/4859863201677788807/posts/default/4190136636409908854'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/4859863201677788807/posts/default/4190136636409908854'/><link rel='alternate' type='text/html' href='https://programmings-java.blogspot.com/2020/12/remove-nth-node-from-end-of-list.html' title='Remove Nth Node From End of List Program (Leetcode)'/><author><name>Mohd Talha</name><uri>http://www.blogger.com/profile/01763375607514819581</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://img.youtube.com/vi/W68cM44cw18/default.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4859863201677788807.post-7569781676809331038</id><published>2020-11-30T21:57:00.002+05:30</published><updated>2020-12-04T17:09:00.726+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Algorithms/Sortings"/><category scheme="http://www.blogger.com/atom/ns#" term="Data Structure"/><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><title type='text'>Linked List</title><content type='html'>&lt;span style=&quot;font-size: large;&quot;&gt;&lt;b&gt;&lt;u&gt;Linked List Python:&lt;/u&gt;&lt;/b&gt;&lt;/span&gt;&lt;div&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;b&gt;&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;!--HTML generated using hilite.me--&gt;&lt;div data-darkreader-inline-bgcolor=&quot;&quot; data-darkreader-inline-bgimage=&quot;&quot; data-darkreader-inline-border-bottom=&quot;&quot; data-darkreader-inline-border-left=&quot;&quot; data-darkreader-inline-border-right=&quot;&quot; data-darkreader-inline-border-top=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #1c1d1d; --darkreader-inline-bgimage: initial; --darkreader-inline-border-bottom: #505556; --darkreader-inline-border-left: #505556; --darkreader-inline-border-right: #505556; --darkreader-inline-border-top: #505556; background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800;&quot;&gt;class&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d84b95; color: #bb0066;&quot;&gt;Node&lt;/span&gt;:
    &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800;&quot;&gt;def&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #5ea5d9; color: #0066bb;&quot;&gt;__init__&lt;/span&gt;(&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;self&lt;/span&gt;, val &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800;&quot;&gt;None&lt;/span&gt;):
        &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;self&lt;/span&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;val &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; val
        &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;self&lt;/span&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;next &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800;&quot;&gt;None&lt;/span&gt;
        
&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800;&quot;&gt;class&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d84b95; color: #bb0066;&quot;&gt;LinkedList&lt;/span&gt;:
    &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800;&quot;&gt;def&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #5ea5d9; color: #0066bb;&quot;&gt;__init__&lt;/span&gt;(&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;self&lt;/span&gt;):
        &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;self&lt;/span&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;head &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; Node()
        
    &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800;&quot;&gt;def&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #5ea5d9; color: #0066bb;&quot;&gt;addLast&lt;/span&gt;(&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;self&lt;/span&gt;, val):
        newNode &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; Node(val)
        cur &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;self&lt;/span&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;head
        &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800;&quot;&gt;while&lt;/span&gt; cur&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;next &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;!=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800;&quot;&gt;None&lt;/span&gt;:
            cur &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; cur&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;next
        cur&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;next &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; newNode
    
    &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800;&quot;&gt;def&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #5ea5d9; color: #0066bb;&quot;&gt;addBeg&lt;/span&gt;(&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;self&lt;/span&gt;, val):
        new &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; Node(val)
        cur &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;self&lt;/span&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;head
        last &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; cur
        
        cur &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; cur&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;next
        last&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;next &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; new
        new&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;next &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; cur
        
    &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800;&quot;&gt;def&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #5ea5d9; color: #0066bb;&quot;&gt;addPos&lt;/span&gt;(&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;self&lt;/span&gt;, val, pos):
        new &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; Node(val)
        cur &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;self&lt;/span&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;head
        count &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd;&quot;&gt;0&lt;/span&gt;
        &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800;&quot;&gt;while&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800;&quot;&gt;True&lt;/span&gt;:
            last &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; cur
            cur &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; cur&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;next
            &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800;&quot;&gt;if&lt;/span&gt; pos &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;==&lt;/span&gt; count:
                last&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;next &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; new
                new&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;next &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; cur
                &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800;&quot;&gt;return&lt;/span&gt;
            count &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;+=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd;&quot;&gt;1&lt;/span&gt;
            
    &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800;&quot;&gt;def&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #5ea5d9; color: #0066bb;&quot;&gt;removeVal&lt;/span&gt;(&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;self&lt;/span&gt;, val):
        cur &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;self&lt;/span&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;head
        &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800;&quot;&gt;while&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800;&quot;&gt;True&lt;/span&gt;:
            last &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; cur
            cur &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; cur&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;next
            &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800;&quot;&gt;if&lt;/span&gt; cur&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;val &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;==&lt;/span&gt; val:
                last&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;next &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; cur&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;next
                &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800;&quot;&gt;return&lt;/span&gt;
            
    &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800;&quot;&gt;def&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #5ea5d9; color: #0066bb;&quot;&gt;removeIndex&lt;/span&gt;(&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;self&lt;/span&gt;, index):
        cur &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;self&lt;/span&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;head
        count &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd;&quot;&gt;1&lt;/span&gt;
        &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800;&quot;&gt;while&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800;&quot;&gt;True&lt;/span&gt;:
            last &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; cur
            cur &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; cur&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;next
            &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800;&quot;&gt;if&lt;/span&gt; count &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;==&lt;/span&gt; index:
                last&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;next &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; cur&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;next
                &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800;&quot;&gt;return&lt;/span&gt;
            count &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;+=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd;&quot;&gt;1&lt;/span&gt;
        
    &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800;&quot;&gt;def&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #5ea5d9; color: #0066bb;&quot;&gt;printList&lt;/span&gt;(&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;self&lt;/span&gt;):
        l &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; []
        temp &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;self&lt;/span&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;head
        &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800;&quot;&gt;while&lt;/span&gt; temp&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;next &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;!=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800;&quot;&gt;None&lt;/span&gt;:
            temp &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; temp&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;next
            l&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;append(temp&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;val)
        &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;print&lt;/span&gt;(l)
        
linked &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; LinkedList()
linked&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;addLast(&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd;&quot;&gt;1&lt;/span&gt;)
linked&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;addLast(&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd;&quot;&gt;2&lt;/span&gt;)
linked&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;addLast(&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd;&quot;&gt;3&lt;/span&gt;)
linked&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;addLast(&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd;&quot;&gt;5&lt;/span&gt;)
linked&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;printList()

linked&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;addBeg(&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd;&quot;&gt;0&lt;/span&gt;)
linked&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;printList()

linked&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;addPos(&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd;&quot;&gt;4&lt;/span&gt;, &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd;&quot;&gt;1&lt;/span&gt;)
linked&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;printList()

linked&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;removeVal(&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd;&quot;&gt;4&lt;/span&gt;)
linked&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;printList()

linked&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;removeIndex(&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd;&quot;&gt;3&lt;/span&gt;)
linked&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;printList()&lt;b style=&quot;text-decoration-line: underline;&quot;&gt;
&lt;/b&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: center;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;b&gt;&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: center;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;b&gt;&lt;u&gt;&lt;iframe allow=&quot;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture&quot; allowfullscreen=&quot;&quot; frameborder=&quot;0&quot; height=&quot;538&quot; src=&quot;https://www.youtube.com/embed/n_ZrHJ_9MRQ&quot; width=&quot;956&quot;&gt;&lt;/iframe&gt;&lt;/u&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://programmings-java.blogspot.com/feeds/7569781676809331038/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://programmings-java.blogspot.com/2020/11/linked-list.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/4859863201677788807/posts/default/7569781676809331038'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/4859863201677788807/posts/default/7569781676809331038'/><link rel='alternate' type='text/html' href='https://programmings-java.blogspot.com/2020/11/linked-list.html' title='Linked List'/><author><name>Mohd Talha</name><uri>http://www.blogger.com/profile/01763375607514819581</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://img.youtube.com/vi/n_ZrHJ_9MRQ/default.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4859863201677788807.post-8567084410063207039</id><published>2020-11-17T19:07:00.005+05:30</published><updated>2021-06-12T14:16:03.363+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Algorithms/Sortings"/><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><title type='text'>Remove All Adjacent Duplicates In String Program (Leetcode)</title><content type='html'>&lt;h2 style=&quot;text-align: left;&quot;&gt;&lt;b&gt;&lt;u&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Remove All Adjacent Duplicates In String Program (Leetcode):&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;/h2&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;You are given a string&amp;nbsp;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;s&lt;/code&gt;&amp;nbsp;consisting of lowercase English letters. A&amp;nbsp;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;duplicate removal&lt;/span&gt;&amp;nbsp;consists of choosing two&amp;nbsp;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;adjacent&lt;/span&gt;&amp;nbsp;and&amp;nbsp;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;equal&lt;/span&gt;&amp;nbsp;letters and removing them.&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;We repeatedly make&amp;nbsp;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;duplicate removals&lt;/span&gt;&amp;nbsp;on&amp;nbsp;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;s&lt;/code&gt;&amp;nbsp;until we no longer can.&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;Return&amp;nbsp;&lt;em style=&quot;box-sizing: border-box;&quot;&gt;the final string after all such duplicate removals have been made&lt;/em&gt;. It can be proven that the answer is&amp;nbsp;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;unique&lt;/span&gt;.&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&amp;nbsp;&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Example 1:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;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; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Input:&lt;/span&gt; s = &quot;abbaca&quot;
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Output:&lt;/span&gt; &quot;ca&quot;
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Explanation:&lt;/span&gt; 
For example, in &quot;abbaca&quot; we could remove &quot;bb&quot; since the letters are adjacent and equal, and this is the only possible move.  The result of this move is that the string is &quot;aaca&quot;, of which only &quot;aa&quot; is possible, so the final string is &quot;ca&quot;.
&lt;/pre&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Example 2:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;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; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Input:&lt;/span&gt; s = &quot;azxxzy&quot;
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Output:&lt;/span&gt; &quot;ay&quot;
&lt;/pre&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&amp;nbsp;&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Constraints:&lt;/span&gt;&lt;/p&gt;&lt;ul style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;1 &amp;lt;= s.length &amp;lt;= 10&lt;span style=&quot;box-sizing: border-box; line-height: 0; position: relative; top: -0.5em; vertical-align: baseline;&quot;&gt;5&lt;/span&gt;&lt;/code&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;s&lt;/code&gt;&amp;nbsp;consists of lowercase English letters.&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div&gt;&lt;!--HTML generated using hilite.me--&gt;&lt;div data-darkreader-inline-bgcolor=&quot;&quot; data-darkreader-inline-bgimage=&quot;&quot; data-darkreader-inline-border-bottom=&quot;&quot; data-darkreader-inline-border-left=&quot;&quot; data-darkreader-inline-border-right=&quot;&quot; data-darkreader-inline-border-top=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #1c1d1d; --darkreader-inline-bgimage: initial; --darkreader-inline-border-bottom: #505556; --darkreader-inline-border-left: #505556; --darkreader-inline-border-right: #505556; --darkreader-inline-border-top: #505556; background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d84b95; color: #bb0066; font-weight: bold;&quot;&gt;Solution&lt;/span&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;:&lt;/span&gt;
    def &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #5ea5d9; color: #0066bb; font-weight: bold;&quot;&gt;removeDuplicates&lt;/span&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;(&lt;/span&gt;self&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;,&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #e0c463; color: #997700; font-weight: bold;&quot;&gt;S:&lt;/span&gt; str&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;)&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #e0c463; color: #997700; font-weight: bold;&quot;&gt;str:&lt;/span&gt;
        &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; not &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #e0c463; color: #997700; font-weight: bold;&quot;&gt;S:&lt;/span&gt;
            &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; S
        
        res &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;[&lt;/span&gt;S&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;[&lt;/span&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;]]&lt;/span&gt;
        &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt; i in S&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;[&lt;/span&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;:]:&lt;/span&gt;
            &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; res and i &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;==&lt;/span&gt; res&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;[-&lt;/span&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;]:&lt;/span&gt;
                res&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #5483d6; color: #0000cc;&quot;&gt;pop&lt;/span&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;()&lt;/span&gt;
            &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;:&lt;/span&gt;
                res&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #5483d6; color: #0000cc;&quot;&gt;append&lt;/span&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;(&lt;/span&gt;i&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;)&lt;/span&gt;
                
        &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span data-darkreader-inline-bgcolor=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #360808; background-color: #fff0f0;&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #5483d6; color: #0000cc;&quot;&gt;join&lt;/span&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;(&lt;/span&gt;res&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;)&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;p data-darkreader-inline-bgcolor=&quot;&quot; data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #1c1d1d; --darkreader-inline-color: #b6b1aa; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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; margin-top: 0px;&quot;&gt;&amp;nbsp;&lt;/p&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;&lt;iframe allow=&quot;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture&quot; allowfullscreen=&quot;&quot; frameborder=&quot;0&quot; height=&quot;538&quot; src=&quot;https://www.youtube.com/embed/kLS8qSMiQ9w&quot; width=&quot;956&quot;&gt;&lt;/iframe&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://programmings-java.blogspot.com/feeds/8567084410063207039/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://programmings-java.blogspot.com/2020/11/remove-all-adjacent-duplicates-in.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/4859863201677788807/posts/default/8567084410063207039'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/4859863201677788807/posts/default/8567084410063207039'/><link rel='alternate' type='text/html' href='https://programmings-java.blogspot.com/2020/11/remove-all-adjacent-duplicates-in.html' title='Remove All Adjacent Duplicates In String Program (Leetcode)'/><author><name>Mohd Talha</name><uri>http://www.blogger.com/profile/01763375607514819581</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://img.youtube.com/vi/kLS8qSMiQ9w/default.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4859863201677788807.post-513107071746443891</id><published>2020-11-07T16:26:00.006+05:30</published><updated>2021-01-11T15:34:29.221+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Algorithms/Sortings"/><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><title type='text'>Interview Questions | Easy | List | Leetcode</title><content type='html'>&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d0cdc8; color: black; font-size: large;&quot;&gt;&lt;b&gt;&lt;span data-darkreader-inline-bgcolor=&quot;&quot; face=&quot;-apple-system, BlinkMacSystemFont, &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;&quot; style=&quot;--darkreader-inline-bgcolor: #191a1b; --darkreader-inline-color: #a6a19a;&quot;&gt;&lt;u style=&quot;background-color: white;&quot;&gt;Interview Questions | Easy | List | Leetcode:&lt;/u&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d0cdc8; color: black; font-size: large;&quot;&gt;&lt;b&gt;&lt;span data-darkreader-inline-bgcolor=&quot;&quot; face=&quot;-apple-system, BlinkMacSystemFont, &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;&quot; style=&quot;--darkreader-inline-bgcolor: #191a1b; --darkreader-inline-color: #a6a19a; background-color: white;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d0cdc8; color: black; font-size: large;&quot;&gt;&lt;b style=&quot;background-color: white;&quot;&gt;&lt;span data-darkreader-inline-bgcolor=&quot;&quot; face=&quot;-apple-system, BlinkMacSystemFont, &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;&quot; style=&quot;--darkreader-inline-bgcolor: #191a1b; --darkreader-inline-color: #a6a19a;&quot;&gt;344&lt;/span&gt;&lt;span data-darkreader-inline-bgcolor=&quot;&quot; face=&quot;-apple-system, BlinkMacSystemFont, &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;&quot; style=&quot;--darkreader-inline-bgcolor: #191a1b; --darkreader-inline-color: #a6a19a;&quot;&gt;.&amp;nbsp;&lt;/span&gt;&lt;span data-darkreader-inline-bgcolor=&quot;&quot; face=&quot;-apple-system, BlinkMacSystemFont, &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;&quot; style=&quot;--darkreader-inline-bgcolor: #191a1b; --darkreader-inline-color: #a6a19a;&quot;&gt;Reverse String&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;p&gt;&lt;!--HTML generated using hilite.me--&gt;&lt;/p&gt;&lt;div data-darkreader-inline-bgcolor=&quot;&quot; data-darkreader-inline-bgimage=&quot;&quot; data-darkreader-inline-border-bottom=&quot;&quot; data-darkreader-inline-border-left=&quot;&quot; data-darkreader-inline-border-right=&quot;&quot; data-darkreader-inline-border-top=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #1c1d1d; --darkreader-inline-bgimage: initial; --darkreader-inline-border-bottom: #505556; --darkreader-inline-border-left: #505556; --darkreader-inline-border-right: #505556; --darkreader-inline-border-top: #505556; background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d84b95; color: #bb0066; font-weight: bold;&quot;&gt;Solution&lt;/span&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;:&lt;/span&gt;
    def &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #5ea5d9; color: #0066bb; font-weight: bold;&quot;&gt;reverseString&lt;/span&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;(&lt;/span&gt;self&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;,&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #e0c463; color: #997700; font-weight: bold;&quot;&gt;s:&lt;/span&gt; List&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;[&lt;/span&gt;str&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;])&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #e0c463; color: #997700; font-weight: bold;&quot;&gt;None:&lt;/span&gt;
        s&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #5483d6; color: #0000cc;&quot;&gt;reverse&lt;/span&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d0cdc8; color: black; font-size: large;&quot;&gt;&lt;b style=&quot;background-color: white;&quot;&gt;&lt;span data-darkreader-inline-bgcolor=&quot;&quot; face=&quot;-apple-system, BlinkMacSystemFont, &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;&quot; style=&quot;--darkreader-inline-bgcolor: #191a1b; --darkreader-inline-color: #a6a19a;&quot;&gt;136&lt;/span&gt;&lt;span data-darkreader-inline-bgcolor=&quot;&quot; face=&quot;-apple-system, BlinkMacSystemFont, &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;&quot; style=&quot;--darkreader-inline-bgcolor: #191a1b; --darkreader-inline-color: #a6a19a;&quot;&gt;.&amp;nbsp;&lt;/span&gt;&lt;span data-darkreader-inline-bgcolor=&quot;&quot; face=&quot;-apple-system, BlinkMacSystemFont, &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;&quot; style=&quot;--darkreader-inline-bgcolor: #191a1b; --darkreader-inline-color: #a6a19a;&quot;&gt;Single Number&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;p&gt;&lt;!--HTML generated using hilite.me--&gt;&lt;/p&gt;&lt;div data-darkreader-inline-bgcolor=&quot;&quot; data-darkreader-inline-bgimage=&quot;&quot; data-darkreader-inline-border-bottom=&quot;&quot; data-darkreader-inline-border-left=&quot;&quot; data-darkreader-inline-border-right=&quot;&quot; data-darkreader-inline-border-top=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #1c1d1d; --darkreader-inline-bgimage: initial; --darkreader-inline-border-bottom: #505556; --darkreader-inline-border-left: #505556; --darkreader-inline-border-right: #505556; --darkreader-inline-border-top: #505556; background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d84b95; color: #bb0066; font-weight: bold;&quot;&gt;Solution&lt;/span&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;:&lt;/span&gt;
    def &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #5ea5d9; color: #0066bb; font-weight: bold;&quot;&gt;singleNumber&lt;/span&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;(&lt;/span&gt;self&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;,&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #e0c463; color: #997700; font-weight: bold;&quot;&gt;nums:&lt;/span&gt; List&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;[&lt;/span&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #768eb6; color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;])&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #768eb6; color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;:&lt;/span&gt;
        check &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; nums&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;[&lt;/span&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;]&lt;/span&gt;

        &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt; i in nums&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;[&lt;/span&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;:]:&lt;/span&gt;
            check &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; check &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;^&lt;/span&gt; i
            
        &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; check
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span data-darkreader-inline-color=&quot;&quot; face=&quot;-apple-system, BlinkMacSystemFont, Segoe UI, PingFang SC, Hiragino Sans GB, Microsoft YaHei, Helvetica Neue, Helvetica, Arial, sans-serif, Apple Color Emoji, Segoe UI Emoji, Segoe UI Symbol&quot; style=&quot;--darkreader-inline-color: #d0cdc8; color: black; font-size: large;&quot;&gt;&lt;b style=&quot;background-color: white;&quot;&gt;&lt;span data-darkreader-inline-bgcolor=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #191a1b; --darkreader-inline-color: #a6a19a;&quot;&gt;237&lt;/span&gt;&lt;span data-darkreader-inline-bgcolor=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #191a1b; --darkreader-inline-color: #a6a19a;&quot;&gt;.&amp;nbsp;&lt;/span&gt;&lt;span data-darkreader-inline-bgcolor=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #191a1b; --darkreader-inline-color: #a6a19a;&quot;&gt;Delete Node in a Linked List&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;p&gt;&lt;!--HTML generated using hilite.me--&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;span data-darkreader-inline-color=&quot;&quot; face=&quot;-apple-system, BlinkMacSystemFont, Segoe UI, PingFang SC, Hiragino Sans GB, Microsoft YaHei, Helvetica Neue, Helvetica, Arial, sans-serif, Apple Color Emoji, Segoe UI Emoji, Segoe UI Symbol&quot; style=&quot;--darkreader-inline-color: #d0cdc8; color: black;&quot;&gt;&lt;!--HTML generated using hilite.me--&gt;&lt;/span&gt;&lt;/p&gt;&lt;div data-darkreader-inline-bgcolor=&quot;&quot; data-darkreader-inline-bgimage=&quot;&quot; data-darkreader-inline-border-bottom=&quot;&quot; data-darkreader-inline-border-left=&quot;&quot; data-darkreader-inline-border-right=&quot;&quot; data-darkreader-inline-border-top=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #1c1d1d; --darkreader-inline-bgimage: initial; --darkreader-inline-border-bottom: #505556; --darkreader-inline-border-left: #505556; --darkreader-inline-border-right: #505556; --darkreader-inline-border-top: #505556; background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span data-darkreader-inline-color=&quot;&quot; face=&quot;-apple-system, BlinkMacSystemFont, Segoe UI, PingFang SC, Hiragino Sans GB, Microsoft YaHei, Helvetica Neue, Helvetica, Arial, sans-serif, Apple Color Emoji, Segoe UI Emoji, Segoe UI Symbol&quot; style=&quot;--darkreader-inline-color: #d0cdc8; color: black;&quot;&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #8e867b; color: #888888;&quot;&gt;# Definition for singly-linked list.&lt;/span&gt;
&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #8e867b; color: #888888;&quot;&gt;# class ListNode:&lt;/span&gt;
&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #8e867b; color: #888888;&quot;&gt;#     def __init__(self, x):&lt;/span&gt;
&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #8e867b; color: #888888;&quot;&gt;#         self.val = x&lt;/span&gt;
&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #8e867b; color: #888888;&quot;&gt;#         self.next = None&lt;/span&gt;

&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d84b95; color: #bb0066; font-weight: bold;&quot;&gt;Solution&lt;/span&gt;:
    &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;def&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #5ea5d9; color: #0066bb; font-weight: bold;&quot;&gt;deleteNode&lt;/span&gt;(&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;self&lt;/span&gt;, node):
        node&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;val &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; node&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;next&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;val
        to_del &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; node&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;next
        node&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;next &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; node&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;next&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;next
        &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;del&lt;/span&gt;(to_del)
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;span data-darkreader-inline-color=&quot;&quot; face=&quot;-apple-system, BlinkMacSystemFont, Segoe UI, PingFang SC, Hiragino Sans GB, Microsoft YaHei, Helvetica Neue, Helvetica, Arial, sans-serif, Apple Color Emoji, Segoe UI Emoji, Segoe UI Symbol&quot; style=&quot;--darkreader-inline-color: #d0cdc8; color: black;&quot;&gt;
&lt;/span&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span data-darkreader-inline-color=&quot;&quot; face=&quot;-apple-system, BlinkMacSystemFont, Segoe UI, PingFang SC, Hiragino Sans GB, Microsoft YaHei, Helvetica Neue, Helvetica, Arial, sans-serif, Apple Color Emoji, Segoe UI Emoji, Segoe UI Symbol&quot; style=&quot;--darkreader-inline-color: #d0cdc8; color: black; font-size: large;&quot;&gt;&lt;b style=&quot;background-color: white;&quot;&gt;&lt;span data-darkreader-inline-bgcolor=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #191a1b; --darkreader-inline-color: #a6a19a;&quot;&gt;46&lt;/span&gt;&lt;span data-darkreader-inline-bgcolor=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #191a1b; --darkreader-inline-color: #a6a19a;&quot;&gt;.&amp;nbsp;&lt;/span&gt;&lt;span data-darkreader-inline-bgcolor=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #191a1b; --darkreader-inline-color: #a6a19a;&quot;&gt;Permutations&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;p&gt;&lt;!--HTML generated using hilite.me--&gt;&lt;/p&gt;&lt;div data-darkreader-inline-bgcolor=&quot;&quot; data-darkreader-inline-bgimage=&quot;&quot; data-darkreader-inline-border-bottom=&quot;&quot; data-darkreader-inline-border-left=&quot;&quot; data-darkreader-inline-border-right=&quot;&quot; data-darkreader-inline-border-top=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #1c1d1d; --darkreader-inline-bgimage: initial; --darkreader-inline-border-bottom: #505556; --darkreader-inline-border-left: #505556; --darkreader-inline-border-right: #505556; --darkreader-inline-border-top: #505556; background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d84b95; color: #bb0066; font-weight: bold;&quot;&gt;Solution&lt;/span&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;:&lt;/span&gt;
    def &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #5ea5d9; color: #0066bb; font-weight: bold;&quot;&gt;permute&lt;/span&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;(&lt;/span&gt;self&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;,&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #e0c463; color: #997700; font-weight: bold;&quot;&gt;nums:&lt;/span&gt; List&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;[&lt;/span&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #768eb6; color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;])&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt; List&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;[&lt;/span&gt;List&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;[&lt;/span&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #768eb6; color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;]]:&lt;/span&gt;
        from itertools &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;import&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #56accf; color: #0e84b5; font-weight: bold;&quot;&gt;permutations&lt;/span&gt;
        per &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; list&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;(&lt;/span&gt;permutations&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;(&lt;/span&gt;nums&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;))&lt;/span&gt;

        &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; per
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span data-darkreader-inline-color=&quot;&quot; face=&quot;-apple-system, BlinkMacSystemFont, Segoe UI, PingFang SC, Hiragino Sans GB, Microsoft YaHei, Helvetica Neue, Helvetica, Arial, sans-serif, Apple Color Emoji, Segoe UI Emoji, Segoe UI Symbol&quot; style=&quot;--darkreader-inline-color: #d0cdc8; color: black; font-size: large;&quot;&gt;&lt;b style=&quot;background-color: white;&quot;&gt;&lt;span data-darkreader-inline-bgcolor=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #191a1b; --darkreader-inline-color: #a6a19a;&quot;&gt;206&lt;/span&gt;&lt;span data-darkreader-inline-bgcolor=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #191a1b; --darkreader-inline-color: #a6a19a;&quot;&gt;.&amp;nbsp;&lt;/span&gt;&lt;span data-darkreader-inline-bgcolor=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #191a1b; --darkreader-inline-color: #a6a19a;&quot;&gt;Reverse Linked List&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;p&gt;&lt;!--HTML generated using hilite.me--&gt;&lt;/p&gt;&lt;div data-darkreader-inline-bgcolor=&quot;&quot; data-darkreader-inline-bgimage=&quot;&quot; data-darkreader-inline-border-bottom=&quot;&quot; data-darkreader-inline-border-left=&quot;&quot; data-darkreader-inline-border-right=&quot;&quot; data-darkreader-inline-border-top=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #1c1d1d; --darkreader-inline-bgimage: initial; --darkreader-inline-border-bottom: #505556; --darkreader-inline-border-left: #505556; --darkreader-inline-border-right: #505556; --darkreader-inline-border-top: #505556; background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #8e867b; color: #888888;&quot;&gt;# Definition for singly-linked list.&lt;/span&gt;
&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #8e867b; color: #888888;&quot;&gt;# class ListNode:&lt;/span&gt;
&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #8e867b; color: #888888;&quot;&gt;#     def __init__(self, val=0, next=None):&lt;/span&gt;
&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #8e867b; color: #888888;&quot;&gt;#         self.val = val&lt;/span&gt;
&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #8e867b; color: #888888;&quot;&gt;#         self.next = next&lt;/span&gt;

&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d84b95; color: #bb0066; font-weight: bold;&quot;&gt;Solution&lt;/span&gt;:
    &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;def&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #5ea5d9; color: #0066bb; font-weight: bold;&quot;&gt;reverseList&lt;/span&gt;(&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;self&lt;/span&gt;, head: ListNode) &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt; ListNode:

        root &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;None&lt;/span&gt;
        &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt; head &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;!=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;None&lt;/span&gt;:
            root &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; ListNode(head&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;val, root)
            head &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; head&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;next

        &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; root
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;span data-darkreader-inline-color=&quot;&quot; face=&quot;-apple-system, BlinkMacSystemFont, Segoe UI, PingFang SC, Hiragino Sans GB, Microsoft YaHei, Helvetica Neue, Helvetica, Arial, sans-serif, Apple Color Emoji, Segoe UI Emoji, Segoe UI Symbol&quot; style=&quot;--darkreader-inline-color: #d0cdc8; color: black;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span data-darkreader-inline-color=&quot;&quot; face=&quot;-apple-system, BlinkMacSystemFont, Segoe UI, PingFang SC, Hiragino Sans GB, Microsoft YaHei, Helvetica Neue, Helvetica, Arial, sans-serif, Apple Color Emoji, Segoe UI Emoji, Segoe UI Symbol&quot; style=&quot;--darkreader-inline-color: #d0cdc8; color: black; font-size: large;&quot;&gt;&lt;b style=&quot;background-color: white;&quot;&gt;&lt;span data-darkreader-inline-bgcolor=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #191a1b; --darkreader-inline-color: #a6a19a;&quot;&gt;78&lt;/span&gt;&lt;span data-darkreader-inline-bgcolor=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #191a1b; --darkreader-inline-color: #a6a19a;&quot;&gt;.&amp;nbsp;&lt;/span&gt;&lt;span data-darkreader-inline-bgcolor=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #191a1b; --darkreader-inline-color: #a6a19a;&quot;&gt;Subsets&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;p&gt;&lt;!--HTML generated using hilite.me--&gt;&lt;/p&gt;&lt;div data-darkreader-inline-bgcolor=&quot;&quot; data-darkreader-inline-bgimage=&quot;&quot; data-darkreader-inline-border-bottom=&quot;&quot; data-darkreader-inline-border-left=&quot;&quot; data-darkreader-inline-border-right=&quot;&quot; data-darkreader-inline-border-top=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #1c1d1d; --darkreader-inline-bgimage: initial; --darkreader-inline-border-bottom: #505556; --darkreader-inline-border-left: #505556; --darkreader-inline-border-right: #505556; --darkreader-inline-border-top: #505556; background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d84b95; color: #bb0066; font-weight: bold;&quot;&gt;Solution&lt;/span&gt;:
    &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;def&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #5ea5d9; color: #0066bb; font-weight: bold;&quot;&gt;subsets&lt;/span&gt;(&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;self&lt;/span&gt;, nums: List[&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;int&lt;/span&gt;]) &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt; List[List[&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;int&lt;/span&gt;]]:
        &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;from&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #56accf; color: #0e84b5; font-weight: bold;&quot;&gt;itertools&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;import&lt;/span&gt; combinations
        comb &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; []
        &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d0cdc8; color: black; font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;range&lt;/span&gt;(&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;len&lt;/span&gt;(nums) &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;+&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;):
             comb&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;append(&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;list&lt;/span&gt;(combinations(nums, i)))

             ans &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; []
             &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d0cdc8; color: black; font-weight: bold;&quot;&gt;in&lt;/span&gt; comb:
                 &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt; j &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d0cdc8; color: black; font-weight: bold;&quot;&gt;in&lt;/span&gt; i:
                     ans&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;append(j)

        &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; ans
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span data-darkreader-inline-color=&quot;&quot; face=&quot;-apple-system, BlinkMacSystemFont, Segoe UI, PingFang SC, Hiragino Sans GB, Microsoft YaHei, Helvetica Neue, Helvetica, Arial, sans-serif, Apple Color Emoji, Segoe UI Emoji, Segoe UI Symbol&quot; style=&quot;--darkreader-inline-color: #d0cdc8; color: black; font-size: large;&quot;&gt;&lt;b style=&quot;background-color: white;&quot;&gt;&lt;span data-darkreader-inline-bgcolor=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #191a1b; --darkreader-inline-color: #a6a19a;&quot;&gt;412&lt;/span&gt;&lt;span data-darkreader-inline-bgcolor=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #191a1b; --darkreader-inline-color: #a6a19a;&quot;&gt;.&amp;nbsp;&lt;/span&gt;&lt;span data-darkreader-inline-bgcolor=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #191a1b; --darkreader-inline-color: #a6a19a;&quot;&gt;Fizz Buzz&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;p&gt;&lt;!--HTML generated using hilite.me--&gt;&lt;/p&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #bb0066; font-weight: bold;&quot;&gt;Solution&lt;/span&gt;:
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;fizzBuzz&lt;/span&gt;(&lt;span style=&quot;color: #007020;&quot;&gt;self&lt;/span&gt;, n: &lt;span style=&quot;color: #007020;&quot;&gt;int&lt;/span&gt;) &lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt; List[&lt;span style=&quot;color: #007020;&quot;&gt;str&lt;/span&gt;]:
        ans &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; []
        &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: #007020;&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;, n &lt;span style=&quot;color: #333333;&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;):
            &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; i &lt;span style=&quot;color: #333333;&quot;&gt;%&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;15&lt;/span&gt; &lt;span style=&quot;color: #333333;&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;:
                ans&lt;span style=&quot;color: #333333;&quot;&gt;.&lt;/span&gt;append(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;FizzBuzz&quot;&lt;/span&gt;)
            &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;elif&lt;/span&gt; i &lt;span style=&quot;color: #333333;&quot;&gt;%&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;3&lt;/span&gt; &lt;span style=&quot;color: #333333;&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;:
                ans&lt;span style=&quot;color: #333333;&quot;&gt;.&lt;/span&gt;append(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;Fizz&quot;&lt;/span&gt;)
            &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;elif&lt;/span&gt; i &lt;span style=&quot;color: #333333;&quot;&gt;%&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;5&lt;/span&gt; &lt;span style=&quot;color: #333333;&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;:
                ans&lt;span style=&quot;color: #333333;&quot;&gt;.&lt;/span&gt;append(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;Buzz&quot;&lt;/span&gt;)
            &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt;:
                ans&lt;span style=&quot;color: #333333;&quot;&gt;.&lt;/span&gt;append(&lt;span style=&quot;color: #007020;&quot;&gt;str&lt;/span&gt;(i))

        &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; ans
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;

&lt;iframe allow=&quot;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture&quot; allowfullscreen=&quot;&quot; frameborder=&quot;0&quot; height=&quot;538&quot; src=&quot;https://www.youtube.com/embed/vO69ouIIGDw&quot; width=&quot;956&quot;&gt;&lt;/iframe&gt;&lt;div id=&quot;gtx-trans&quot; style=&quot;left: -20px; position: absolute; top: 1575.2px;&quot;&gt;&lt;div class=&quot;gtx-trans-icon&quot;&gt;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://programmings-java.blogspot.com/feeds/513107071746443891/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://programmings-java.blogspot.com/2020/11/interview-questions-easy-list-leetcode.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/4859863201677788807/posts/default/513107071746443891'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/4859863201677788807/posts/default/513107071746443891'/><link rel='alternate' type='text/html' href='https://programmings-java.blogspot.com/2020/11/interview-questions-easy-list-leetcode.html' title='Interview Questions | Easy | List | Leetcode'/><author><name>Mohd Talha</name><uri>http://www.blogger.com/profile/01763375607514819581</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://img.youtube.com/vi/vO69ouIIGDw/default.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4859863201677788807.post-5920065723370948096</id><published>2020-10-28T13:58:00.003+05:30</published><updated>2021-06-12T14:16:55.838+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Algorithms/Sortings"/><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><title type='text'>Longest Palindromic Substring Program (Leetcode)</title><content type='html'>&lt;p&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;b&gt;&lt;u&gt;Longest Palindromic Substring Program (Leetcode):&lt;/u&gt;&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;Given a string&amp;nbsp;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;s&lt;/code&gt;, return&amp;nbsp;&lt;em style=&quot;box-sizing: border-box;&quot;&gt;the longest palindromic substring&lt;/em&gt;&amp;nbsp;in&amp;nbsp;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;s&lt;/code&gt;.&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Example 1:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;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; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Input:&lt;/span&gt; s = &quot;babad&quot;
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Output:&lt;/span&gt; &quot;bab&quot;
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Note:&lt;/span&gt; &quot;aba&quot; is also a valid answer.
&lt;/pre&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Example 2:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;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; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Input:&lt;/span&gt; s = &quot;cbbd&quot;
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Output:&lt;/span&gt; &quot;bb&quot;
&lt;/pre&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Example 3:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;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; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Input:&lt;/span&gt; s = &quot;a&quot;
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Output:&lt;/span&gt; &quot;a&quot;
&lt;/pre&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Example 4:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;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; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Input:&lt;/span&gt; s = &quot;ac&quot;
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Output:&lt;/span&gt; &quot;a&quot;
&lt;/pre&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&amp;nbsp;&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Constraints:&lt;/span&gt;&lt;/p&gt;&lt;ul style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;1 &amp;lt;= s.length &amp;lt;= 1000&lt;/code&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;s&lt;/code&gt;&amp;nbsp;consist of only digits and English letters (lower-case and/or upper-case).&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;!--HTML generated using hilite.me--&gt;&lt;/span&gt;&lt;/p&gt;&lt;div data-darkreader-inline-bgcolor=&quot;&quot; data-darkreader-inline-bgimage=&quot;&quot; data-darkreader-inline-border-bottom=&quot;&quot; data-darkreader-inline-border-left=&quot;&quot; data-darkreader-inline-border-right=&quot;&quot; data-darkreader-inline-border-top=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #1c1d1d; --darkreader-inline-bgimage: initial; --darkreader-inline-border-bottom: #505556; --darkreader-inline-border-left: #505556; --darkreader-inline-border-right: #505556; --darkreader-inline-border-top: #505556; background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d84b95; color: #bb0066; font-weight: bold;&quot;&gt;Solution&lt;/span&gt;:
    &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;def&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #5ea5d9; color: #0066bb; font-weight: bold;&quot;&gt;longestPalindrome&lt;/span&gt;(&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;self&lt;/span&gt;, s: &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;str&lt;/span&gt;) &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;str&lt;/span&gt;:
        length &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;len&lt;/span&gt;(s)
        &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; length &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;==&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;:
            &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span data-darkreader-inline-bgcolor=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #360808; background-color: #fff0f0;&quot;&gt;&#39;&#39;&lt;/span&gt;
        
        dp &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; [[&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;]&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;*&lt;/span&gt;length &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt; j &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d0cdc8; color: black; font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;range&lt;/span&gt;(length)]
        &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d0cdc8; color: black; font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;range&lt;/span&gt;(length):
            dp[i][i] &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;    
  
        count &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; length
        val &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; s[&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;]
        &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d0cdc8; color: black; font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;range&lt;/span&gt;(&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;, length):
            x &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;
            y &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; i
            &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt; j &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d0cdc8; color: black; font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;range&lt;/span&gt;(i, length):
                &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; count &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;&amp;gt;&lt;/span&gt; (&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;2&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;*&lt;/span&gt; length &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;-&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;):
                    &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; s[x] &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;==&lt;/span&gt; s[y] &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d0cdc8; color: black; font-weight: bold;&quot;&gt;and&lt;/span&gt; dp[x &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;+&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;][y &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;-&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;] &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;==&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;:
                        dp[x][y] &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;
                        val &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; s[x: y &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;+&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;]
                &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt;:
                    &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; s[x] &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;==&lt;/span&gt; s[y]:
                        dp[x][y] &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;
                        val &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; s[x: y &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;+&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;]       
                x &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;+=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;
                y &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;+=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;
                count &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;+=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;

        &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; val
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;
&lt;/span&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;


&lt;div style=&quot;text-align: center;&quot;&gt;&lt;iframe allow=&quot;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture&quot; allowfullscreen=&quot;&quot; frameborder=&quot;0&quot; height=&quot;315&quot; src=&quot;https://www.youtube.com/embed/BGg3MhzSK2U&quot; width=&quot;560&quot;&gt;&lt;/iframe&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://programmings-java.blogspot.com/feeds/5920065723370948096/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://programmings-java.blogspot.com/2020/10/longest-palindromic-substring-program.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/4859863201677788807/posts/default/5920065723370948096'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/4859863201677788807/posts/default/5920065723370948096'/><link rel='alternate' type='text/html' href='https://programmings-java.blogspot.com/2020/10/longest-palindromic-substring-program.html' title='Longest Palindromic Substring Program (Leetcode)'/><author><name>Mohd Talha</name><uri>http://www.blogger.com/profile/01763375607514819581</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://img.youtube.com/vi/BGg3MhzSK2U/default.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4859863201677788807.post-9037810423138383625</id><published>2020-10-15T17:04:00.004+05:30</published><updated>2021-06-12T14:17:38.745+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Algorithms/Sortings"/><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><title type='text'>Count Negative Numbers in a Sorted Matrix Program (Leetcode)</title><content type='html'>&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;b&gt;&lt;u&gt;Count Negative Numbers in a Sorted Matrix Program (Leetcode):&lt;/u&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;b&gt;&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;b&gt;&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;Given a&amp;nbsp;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;m x n&lt;/code&gt;&amp;nbsp;matrix&amp;nbsp;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;grid&lt;/code&gt;&amp;nbsp;which is sorted in non-increasing order both row-wise and column-wise, return&amp;nbsp;&lt;em style=&quot;box-sizing: border-box;&quot;&gt;the number of&amp;nbsp;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;negative&lt;/span&gt;&amp;nbsp;numbers in&lt;/em&gt;&amp;nbsp;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;grid&lt;/code&gt;.&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Example 1:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;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; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Input:&lt;/span&gt; grid = [[4,3,2,-1],[3,2,1,-1],[1,1,-1,-2],[-1,-1,-2,-3]]
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Output:&lt;/span&gt; 8
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Explanation:&lt;/span&gt; There are 8 negatives number in the matrix.
&lt;/pre&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Example 2:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;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; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Input:&lt;/span&gt; grid = [[3,2],[1,0]]
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Output:&lt;/span&gt; 0
&lt;/pre&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Example 3:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;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; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Input:&lt;/span&gt; grid = [[1,-1],[-1,-1]]
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Output:&lt;/span&gt; 3
&lt;/pre&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Example 4:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;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; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Input:&lt;/span&gt; grid = [[-1]]
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Output:&lt;/span&gt; 1
&lt;/pre&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&amp;nbsp;&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Constraints:&lt;/span&gt;&lt;/p&gt;&lt;ul style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;m == grid.length&lt;/code&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;n == grid[i].length&lt;/code&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;1 &amp;lt;= m, n &amp;lt;= 100&lt;/code&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;-100 &amp;lt;= grid[i][j] &amp;lt;= 100&lt;/code&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&amp;nbsp;&lt;/p&gt;&lt;span style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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-weight: bolder;&quot;&gt;Follow up:&lt;/span&gt;&lt;span style=&quot;background-color: white; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;;&quot;&gt;&amp;nbsp;Could you find an&amp;nbsp;&lt;/span&gt;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;O(n + m)&lt;/code&gt;&lt;span style=&quot;background-color: white; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;;&quot;&gt;&amp;nbsp;solution?&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;p style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;!--HTML generated using hilite.me--&gt;&lt;/span&gt;&lt;/p&gt;&lt;div data-darkreader-inline-bgcolor=&quot;&quot; data-darkreader-inline-bgimage=&quot;&quot; data-darkreader-inline-border-bottom=&quot;&quot; data-darkreader-inline-border-left=&quot;&quot; data-darkreader-inline-border-right=&quot;&quot; data-darkreader-inline-border-top=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #1c1d1d; --darkreader-inline-bgimage: initial; --darkreader-inline-border-bottom: #505556; --darkreader-inline-border-left: #505556; --darkreader-inline-border-right: #505556; --darkreader-inline-border-top: #505556; background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d84b95; color: #bb0066; font-weight: bold;&quot;&gt;Solution&lt;/span&gt;:
    &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;def&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #5ea5d9; color: #0066bb; font-weight: bold;&quot;&gt;binarySearch&lt;/span&gt;(&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;self&lt;/span&gt;, nums):
        left &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;
        right &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;len&lt;/span&gt;(nums) &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;-&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;
        
        &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt; left &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;&amp;lt;=&lt;/span&gt; right:
            mid &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; (left &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;+&lt;/span&gt; right) &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;//&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;2&lt;/span&gt;
            &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; nums[mid] &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;:
                left &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; mid &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;+&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;
            &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt;:
                right &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; mid &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;-&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;
                
        &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;len&lt;/span&gt;(nums) &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;-&lt;/span&gt; left
    
    &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;def&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #5ea5d9; color: #0066bb; font-weight: bold;&quot;&gt;countNegatives&lt;/span&gt;(&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;self&lt;/span&gt;, grid: List[List[&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;int&lt;/span&gt;]]) &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;int&lt;/span&gt;:
        count &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;
        &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt; row &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d0cdc8; color: black; font-weight: bold;&quot;&gt;in&lt;/span&gt; grid:
            count &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;+=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;self&lt;/span&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;binarySearch(row)
            
        &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; count
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;
&lt;/span&gt;&lt;p&gt;&lt;/p&gt;&lt;p style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;&lt;/p&gt;&lt;/div&gt;

&lt;div style=&quot;text-align: center;&quot;&gt;&lt;iframe allow=&quot;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture&quot; allowfullscreen=&quot;&quot; frameborder=&quot;0&quot; height=&quot;315&quot; src=&quot;https://www.youtube.com/embed/npmfkF5y_R0&quot; width=&quot;560&quot;&gt;&lt;/iframe&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://programmings-java.blogspot.com/feeds/9037810423138383625/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://programmings-java.blogspot.com/2020/10/count-negative-numbers-in-sorted-matrix.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/4859863201677788807/posts/default/9037810423138383625'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/4859863201677788807/posts/default/9037810423138383625'/><link rel='alternate' type='text/html' href='https://programmings-java.blogspot.com/2020/10/count-negative-numbers-in-sorted-matrix.html' title='Count Negative Numbers in a Sorted Matrix Program (Leetcode)'/><author><name>Mohd Talha</name><uri>http://www.blogger.com/profile/01763375607514819581</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://img.youtube.com/vi/npmfkF5y_R0/default.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4859863201677788807.post-8920093350125196960</id><published>2020-10-09T22:12:00.006+05:30</published><updated>2021-06-12T14:18:30.413+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Algorithms/Sortings"/><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><title type='text'>Find First and Last Position of Element in Sorted Array Program (Leetcode)</title><content type='html'>&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;b&gt;Find First and Last Position of Element in Sorted Array &lt;u&gt;Program (Leetcode):&lt;/u&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;b&gt;&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;Given an array of integers&amp;nbsp;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;nums&lt;/code&gt;&amp;nbsp;sorted in ascending order, find the starting and ending position of a given&amp;nbsp;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;target&lt;/code&gt;&amp;nbsp;value.&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;If&amp;nbsp;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;target&lt;/code&gt;&amp;nbsp;is not found in the array, return&amp;nbsp;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;[-1, -1]&lt;/code&gt;.&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;You must&amp;nbsp;write an algorithm with&amp;nbsp;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;O(log n)&lt;/code&gt;&amp;nbsp;runtime complexity.&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Example 1:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;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; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Input:&lt;/span&gt; nums = [5,7,7,8,8,10], target = 8
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Output:&lt;/span&gt; [3,4]
&lt;/pre&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Example 2:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;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; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Input:&lt;/span&gt; nums = [5,7,7,8,8,10], target = 6
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Output:&lt;/span&gt; [-1,-1]
&lt;/pre&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Example 3:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;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; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Input:&lt;/span&gt; nums = [], target = 0
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Output:&lt;/span&gt; [-1,-1]
&lt;/pre&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&amp;nbsp;&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Constraints:&lt;/span&gt;&lt;/p&gt;&lt;ul style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;0 &amp;lt;= nums.length &amp;lt;= 10&lt;span style=&quot;box-sizing: border-box; line-height: 0; position: relative; top: -0.5em; vertical-align: baseline;&quot;&gt;5&lt;/span&gt;&lt;/code&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;-10&lt;span style=&quot;box-sizing: border-box; line-height: 0; position: relative; top: -0.5em; vertical-align: baseline;&quot;&gt;9&lt;/span&gt;&amp;nbsp;&amp;lt;= nums[i]&amp;nbsp;&amp;lt;= 10&lt;span style=&quot;box-sizing: border-box; line-height: 0; position: relative; top: -0.5em; vertical-align: baseline;&quot;&gt;9&lt;/span&gt;&lt;/code&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;nums&lt;/code&gt;&amp;nbsp;is a non-decreasing array.&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;-10&lt;span style=&quot;box-sizing: border-box; line-height: 0; position: relative; top: -0.5em; vertical-align: baseline;&quot;&gt;9&lt;/span&gt;&amp;nbsp;&amp;lt;= target&amp;nbsp;&amp;lt;= 10&lt;span style=&quot;box-sizing: border-box; line-height: 0; position: relative; top: -0.5em; vertical-align: baseline;&quot;&gt;9&lt;/span&gt;&lt;/code&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/span&gt;&lt;/div&gt;&lt;p&gt;&lt;!--HTML generated using hilite.me--&gt;&lt;/p&gt;&lt;div data-darkreader-inline-bgcolor=&quot;&quot; data-darkreader-inline-bgimage=&quot;&quot; data-darkreader-inline-border-bottom=&quot;&quot; data-darkreader-inline-border-left=&quot;&quot; data-darkreader-inline-border-right=&quot;&quot; data-darkreader-inline-border-top=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #1c1d1d; --darkreader-inline-bgimage: initial; --darkreader-inline-border-bottom: #505556; --darkreader-inline-border-left: #505556; --darkreader-inline-border-right: #505556; --darkreader-inline-border-top: #505556; background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #8e867b; color: #888888;&quot;&gt;#check move left or not&lt;/span&gt;
&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;def&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #5ea5d9; color: #0066bb; font-weight: bold;&quot;&gt;binarySearch&lt;/span&gt;(&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;self&lt;/span&gt;, nums, target, check):  
        left &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;
        right &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;len&lt;/span&gt;(nums)
        
        &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt; left &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt; right:
            mid &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; (left &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;+&lt;/span&gt; right) &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;//&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;2&lt;/span&gt;
            &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; (nums[mid] &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;&amp;gt;&lt;/span&gt; target &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d0cdc8; color: black; font-weight: bold;&quot;&gt;or&lt;/span&gt; (check &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d0cdc8; color: black; font-weight: bold;&quot;&gt;and&lt;/span&gt; target &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;==&lt;/span&gt; nums[mid])):
                right &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; mid
            &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt;:
                left &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; mid &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;+&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;
        &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; left
   
&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;def&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #5ea5d9; color: #0066bb; font-weight: bold;&quot;&gt;searchRange&lt;/span&gt;(&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;self&lt;/span&gt;, nums: List[&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;int&lt;/span&gt;], target: &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;int&lt;/span&gt;):
        left &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;self&lt;/span&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;binarySearch(nums, target, &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;True&lt;/span&gt;)
        &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; left &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;==&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;len&lt;/span&gt;(nums) &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d0cdc8; color: black; font-weight: bold;&quot;&gt;or&lt;/span&gt; nums[left] &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;!=&lt;/span&gt; target:
            &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; [&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;-&lt;/span&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;, &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;-&lt;/span&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;]        

        right &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;self&lt;/span&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;binarySearch(nums, target, &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;False&lt;/span&gt;) &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;-&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;
        &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; [left, right]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;


&lt;iframe allow=&quot;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture&quot; allowfullscreen=&quot;&quot; frameborder=&quot;0&quot; height=&quot;538&quot; src=&quot;https://www.youtube.com/embed/QJ3mPvY8s9g&quot; width=&quot;956&quot;&gt;&lt;/iframe&gt;&lt;div id=&quot;gtx-trans&quot; style=&quot;left: -38px; position: absolute; top: 1162.8px;&quot;&gt;&lt;div class=&quot;gtx-trans-icon&quot;&gt;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://programmings-java.blogspot.com/feeds/8920093350125196960/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://programmings-java.blogspot.com/2020/10/find-first-and-last-position-of-element.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/4859863201677788807/posts/default/8920093350125196960'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/4859863201677788807/posts/default/8920093350125196960'/><link rel='alternate' type='text/html' href='https://programmings-java.blogspot.com/2020/10/find-first-and-last-position-of-element.html' title='Find First and Last Position of Element in Sorted Array Program (Leetcode)'/><author><name>Mohd Talha</name><uri>http://www.blogger.com/profile/01763375607514819581</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://img.youtube.com/vi/QJ3mPvY8s9g/default.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4859863201677788807.post-3716726121398190517</id><published>2020-10-03T18:30:00.003+05:30</published><updated>2021-06-12T14:19:10.301+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Algorithms/Sortings"/><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><title type='text'>Longest Substring Without Repeating Characters Program (Leetcode)</title><content type='html'>&lt;h4 style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;Longest Substring Without Repeating Characters (Leetcode):&lt;/u&gt;&lt;/span&gt;&lt;/h4&gt;&lt;div&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;Given a string&amp;nbsp;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;s&lt;/code&gt;, find the length of the&amp;nbsp;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;longest substring&lt;/span&gt;&amp;nbsp;without repeating characters.&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Example 1:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;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; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Input:&lt;/span&gt; s = &quot;abcabcbb&quot;
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Output:&lt;/span&gt; 3
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Explanation:&lt;/span&gt; The answer is &quot;abc&quot;, with the length of 3.
&lt;/pre&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Example 2:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;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; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Input:&lt;/span&gt; s = &quot;bbbbb&quot;
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Output:&lt;/span&gt; 1
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Explanation:&lt;/span&gt; The answer is &quot;b&quot;, with the length of 1.
&lt;/pre&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Example 3:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;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; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Input:&lt;/span&gt; s = &quot;pwwkew&quot;
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Output:&lt;/span&gt; 3
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Explanation:&lt;/span&gt; The answer is &quot;wke&quot;, with the length of 3.
Notice that the answer must be a substring, &quot;pwke&quot; is a subsequence and not a substring.
&lt;/pre&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Example 4:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;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; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Input:&lt;/span&gt; s = &quot;&quot;
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Output:&lt;/span&gt; 0
&lt;/pre&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&amp;nbsp;&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Constraints:&lt;/span&gt;&lt;/p&gt;&lt;ul style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;0 &amp;lt;= s.length &amp;lt;= 5 * 10&lt;span style=&quot;box-sizing: border-box; line-height: 0; position: relative; top: -0.5em; vertical-align: baseline;&quot;&gt;4&lt;/span&gt;&lt;/code&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;s&lt;/code&gt;&amp;nbsp;consists of English letters, digits, symbols, and spaces.&lt;/li&gt;&lt;/ul&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #007020;&quot;&gt;dict&lt;/span&gt; &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; {}
length &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;
start &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;
        
&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: #007020;&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: #007020;&quot;&gt;len&lt;/span&gt;(s)):
        &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; s[i] &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: #007020;&quot;&gt;dict&lt;/span&gt;:
                start &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #007020;&quot;&gt;max&lt;/span&gt;(start, &lt;span style=&quot;color: #007020;&quot;&gt;dict&lt;/span&gt;[s[i]] &lt;span style=&quot;color: #333333;&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;)
        &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; length &lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt; i &lt;span style=&quot;color: #333333;&quot;&gt;-&lt;/span&gt; start &lt;span style=&quot;color: #333333;&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;:
                length &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; i &lt;span style=&quot;color: #333333;&quot;&gt;-&lt;/span&gt; start &lt;span style=&quot;color: #333333;&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;
        &lt;span style=&quot;color: #007020;&quot;&gt;dict&lt;/span&gt;[s[i]] &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; i
        
&lt;span style=&quot;color: #007020;&quot;&gt;print&lt;/span&gt;(length)
&lt;/pre&gt;&lt;/div&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style=&quot;text-align: center;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style=&quot;text-align: center;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style=&quot;text-align: center;&quot;&gt;&lt;iframe allow=&quot;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture&quot; allowfullscreen=&quot;&quot; frameborder=&quot;0&quot; height=&quot;315&quot; src=&quot;https://www.youtube.com/embed/x6XBX2JO8Ys&quot; width=&quot;560&quot;&gt;&lt;/iframe&gt;&lt;/div&gt;

</content><link rel='replies' type='application/atom+xml' href='https://programmings-java.blogspot.com/feeds/3716726121398190517/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://programmings-java.blogspot.com/2020/10/longest-substring-without-repeating.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/4859863201677788807/posts/default/3716726121398190517'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/4859863201677788807/posts/default/3716726121398190517'/><link rel='alternate' type='text/html' href='https://programmings-java.blogspot.com/2020/10/longest-substring-without-repeating.html' title='Longest Substring Without Repeating Characters Program (Leetcode)'/><author><name>Mohd Talha</name><uri>http://www.blogger.com/profile/01763375607514819581</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://img.youtube.com/vi/x6XBX2JO8Ys/default.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4859863201677788807.post-5397742917825102484</id><published>2020-09-30T20:57:00.006+05:30</published><updated>2021-06-12T14:20:15.883+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Algorithms/Sortings"/><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><title type='text'>Search a 2D Matrix Program (Leetcode)</title><content type='html'>&lt;h4 style=&quot;text-align: left;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;Search a 2D Matrix Program (Leetcode):&lt;/u&gt;&lt;/span&gt;&lt;/b&gt;&lt;/h4&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;Write an efficient algorithm that searches for a value in an&amp;nbsp;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;m x n&lt;/code&gt;&amp;nbsp;matrix. This matrix has the following properties:&lt;/p&gt;&lt;ul style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;Integers in each row are sorted from left to right.&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;The first integer of each row is greater than the last integer of the previous row.&lt;/li&gt;&lt;/ul&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&amp;nbsp;&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Example 1:&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center;&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;https://assets.leetcode.com/uploads/2020/10/05/mat.jpg&quot; style=&quot;background-color: white; border-style: none; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; height: 242px; max-width: 100%; vertical-align: middle; width: 322px;&quot; /&gt;&lt;span style=&quot;background-color: white; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;;&quot;&gt;&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;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; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Input:&lt;/span&gt; matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Output:&lt;/span&gt; true
&lt;/pre&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Example 2:&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;text-align: center;&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;https://assets.leetcode.com/uploads/2020/10/05/mat2.jpg&quot; style=&quot;background-color: white; border-style: none; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; height: 242px; max-width: 100%; vertical-align: middle; width: 322px;&quot; /&gt;&lt;span style=&quot;background-color: white; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;;&quot;&gt;&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;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; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Input:&lt;/span&gt; matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Output:&lt;/span&gt; false
&lt;/pre&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&amp;nbsp;&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Constraints:&lt;/span&gt;&lt;/p&gt;&lt;ul style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;m == matrix.length&lt;/code&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;n == matrix[i].length&lt;/code&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;1 &amp;lt;= m, n &amp;lt;= 100&lt;/code&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;-10&lt;span style=&quot;box-sizing: border-box; line-height: 0; position: relative; top: -0.5em; vertical-align: baseline;&quot;&gt;4&lt;/span&gt;&amp;nbsp;&amp;lt;= matrix[i][j], target &amp;lt;= 10&lt;span style=&quot;box-sizing: border-box; line-height: 0; position: relative; top: -0.5em; vertical-align: baseline;&quot;&gt;4&lt;/span&gt;&lt;/code&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;!--HTML generated using hilite.me--&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;(&lt;/span&gt;matrix&lt;span style=&quot;color: #333333;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #0000cc;&quot;&gt;length&lt;/span&gt; &lt;span style=&quot;color: #333333;&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;)&lt;/span&gt;
     System&lt;span style=&quot;color: #333333;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #0000cc;&quot;&gt;out&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #0000cc;&quot;&gt;println&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;false&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;);&lt;/span&gt;     

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

&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;(&lt;/span&gt;row &lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt; matrix&lt;span style=&quot;color: #333333;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #0000cc;&quot;&gt;length&lt;/span&gt; &lt;span style=&quot;color: #333333;&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; col &lt;span style=&quot;color: #333333;&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: #333333;&quot;&gt;{&lt;/span&gt;
     &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;(&lt;/span&gt;matrix&lt;span style=&quot;color: #333333;&quot;&gt;[&lt;/span&gt;row&lt;span style=&quot;color: #333333;&quot;&gt;][&lt;/span&gt;col&lt;span style=&quot;color: #333333;&quot;&gt;]&lt;/span&gt; &lt;span style=&quot;color: #333333;&quot;&gt;&amp;gt;&lt;/span&gt; target&lt;span style=&quot;color: #333333;&quot;&gt;)&lt;/span&gt;
          col &lt;span style=&quot;color: #333333;&quot;&gt;-=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;;&lt;/span&gt;
     &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;(&lt;/span&gt;matrix&lt;span style=&quot;color: #333333;&quot;&gt;[&lt;/span&gt;row&lt;span style=&quot;color: #333333;&quot;&gt;][&lt;/span&gt;col&lt;span style=&quot;color: #333333;&quot;&gt;]&lt;/span&gt; &lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt; target&lt;span style=&quot;color: #333333;&quot;&gt;)&lt;/span&gt;
          row &lt;span style=&quot;color: #333333;&quot;&gt;+=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;;&lt;/span&gt;
     &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt;                   
          System&lt;span style=&quot;color: #333333;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #0000cc;&quot;&gt;out&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #0000cc;&quot;&gt;println&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;true&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;);&lt;/span&gt;
&lt;span style=&quot;color: #333333;&quot;&gt;}&lt;/span&gt;
System&lt;span style=&quot;color: #333333;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #0000cc;&quot;&gt;out&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #0000cc;&quot;&gt;println&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;false&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;);&lt;/span&gt; 
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;
&lt;/span&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;


&lt;div style=&quot;text-align: center;&quot;&gt;&lt;iframe allow=&quot;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture&quot; allowfullscreen=&quot;&quot; frameborder=&quot;0&quot; height=&quot;315&quot; src=&quot;https://www.youtube.com/embed/-nCrTbWj9oc&quot; width=&quot;560&quot;&gt;&lt;/iframe&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://programmings-java.blogspot.com/feeds/5397742917825102484/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://programmings-java.blogspot.com/2020/09/search-2d-matrix-program-leetcode.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/4859863201677788807/posts/default/5397742917825102484'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/4859863201677788807/posts/default/5397742917825102484'/><link rel='alternate' type='text/html' href='https://programmings-java.blogspot.com/2020/09/search-2d-matrix-program-leetcode.html' title='Search a 2D Matrix Program (Leetcode)'/><author><name>Mohd Talha</name><uri>http://www.blogger.com/profile/01763375607514819581</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://img.youtube.com/vi/-nCrTbWj9oc/default.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4859863201677788807.post-8244861672016623461</id><published>2020-09-24T14:00:00.006+05:30</published><updated>2021-06-12T14:21:01.349+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Algorithms/Sortings"/><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><title type='text'>Longest Increasing Subsequence Program (Leetcode)</title><content type='html'>&lt;h4 style=&quot;height: 0px; text-align: left;&quot;&gt;&lt;u&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Longest Increasing Subsequence Program (Leetcode):&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/u&gt;&lt;/h4&gt;&lt;div&gt;&lt;u&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/u&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;Given an integer array&amp;nbsp;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;nums&lt;/code&gt;, return the length of the longest strictly increasing subsequence.&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;A&amp;nbsp;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;subsequence&lt;/span&gt;&amp;nbsp;is a sequence that can be derived from an array by deleting some or no elements without changing the order of the remaining elements. For example,&amp;nbsp;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;[3,6,2,7]&lt;/code&gt;&amp;nbsp;is a subsequence of the array&amp;nbsp;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;[0,3,1,6,2,2,7]&lt;/code&gt;.&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Example 1:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;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; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Input:&lt;/span&gt; nums = [10,9,2,5,3,7,101,18]
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Output:&lt;/span&gt; 4
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Explanation:&lt;/span&gt; The longest increasing subsequence is [2,3,7,101], therefore the length is 4.
&lt;/pre&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Example 2:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;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; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Input:&lt;/span&gt; nums = [0,1,0,3,2,3]
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Output:&lt;/span&gt; 4
&lt;/pre&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Example 3:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;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; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Input:&lt;/span&gt; nums = [7,7,7,7,7,7,7]
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Output:&lt;/span&gt; 1
&lt;/pre&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&amp;nbsp;&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Constraints:&lt;/span&gt;&lt;/p&gt;&lt;ul style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;1 &amp;lt;= nums.length &amp;lt;= 2500&lt;/code&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;-10&lt;span style=&quot;box-sizing: border-box; line-height: 0; position: relative; top: -0.5em; vertical-align: baseline;&quot;&gt;4&lt;/span&gt;&amp;nbsp;&amp;lt;= nums[i] &amp;lt;= 10&lt;span style=&quot;box-sizing: border-box; line-height: 0; position: relative; top: -0.5em; vertical-align: baseline;&quot;&gt;4&lt;/span&gt;&lt;/code&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&amp;nbsp;&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Follow up:&lt;/span&gt;&amp;nbsp;Can you come up with an algorithm that runs in&amp;nbsp;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;O(n log(n))&lt;/code&gt;&amp;nbsp;time complexity?&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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; margin-top: 0px;&quot;&gt;&lt;br /&gt;&lt;/p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;1) O(n^2)&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;!--HTML generated using hilite.me--&gt;&lt;div data-darkreader-inline-bgcolor=&quot;&quot; data-darkreader-inline-bgimage=&quot;&quot; data-darkreader-inline-border-bottom=&quot;&quot; data-darkreader-inline-border-left=&quot;&quot; data-darkreader-inline-border-right=&quot;&quot; data-darkreader-inline-border-top=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #1c1d1d; --darkreader-inline-bgimage: initial; --darkreader-inline-border-bottom: #505556; --darkreader-inline-border-left: #505556; --darkreader-inline-border-right: #505556; --darkreader-inline-border-top: #505556; background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;len&lt;/span&gt;(nums) &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;==&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;:
    &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;

length &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; [&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d0cdc8; color: black; font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;range&lt;/span&gt;(&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;len&lt;/span&gt;(nums))]        
&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d0cdc8; color: black; font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;range&lt;/span&gt;(&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;len&lt;/span&gt;(nums)):
    &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt; j &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d0cdc8; color: black; font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;range&lt;/span&gt;(i):
        &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; nums[j] &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt; nums[i] &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d0cdc8; color: black; font-weight: bold;&quot;&gt;and&lt;/span&gt; length[j] &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;+&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;&amp;gt;&lt;/span&gt; length[i]:
            length[i] &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; length[j] &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;+&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;
                    
&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; (&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;max&lt;/span&gt;(length))
&lt;/pre&gt;&lt;/div&gt;
&lt;br /&gt;&lt;/span&gt;&lt;div style=&quot;font-size: large;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style=&quot;font-size: large;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;u&gt;&lt;span style=&quot;font-size: large;&quot;&gt;2)O(n log(n))&lt;/span&gt;&lt;/u&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;u&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/u&gt;&lt;/div&gt;&lt;div&gt;&lt;p style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;!--HTML generated using hilite.me--&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;binarySearch&lt;/span&gt;(start, end, val, nums, index):
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; start &lt;span style=&quot;color: #333333;&quot;&gt;&amp;gt;&lt;/span&gt; end:
        &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; start
    mid &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; (start &lt;span style=&quot;color: #333333;&quot;&gt;+&lt;/span&gt; end) &lt;span style=&quot;color: #333333;&quot;&gt;//&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;2&lt;/span&gt;
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; nums[index[mid]] &lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt; val:
        start &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; mid &lt;span style=&quot;color: #333333;&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt;:
        end &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; mid &lt;span style=&quot;color: #333333;&quot;&gt;-&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;
        
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; binarySearch(start, end, val, nums, index)

size &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;
index &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; [&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;None&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: #007020;&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: #007020;&quot;&gt;len&lt;/span&gt;(nums) &lt;span style=&quot;color: #333333;&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;)]
&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: black; font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: #007020;&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: #007020;&quot;&gt;len&lt;/span&gt;(nums)):
        val &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; nums[i]
        bin_val &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; binarySearch(&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;, size, val, nums, index)
        index[bin_val] &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; i
        size &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #007020;&quot;&gt;max&lt;/span&gt;(size, bin_val)
        &lt;span style=&quot;color: #888888;&quot;&gt;#print(index)&lt;/span&gt;

&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; size&lt;/span&gt;&lt;b style=&quot;font-family: SFMono-Regular, Consolas, &amp;quot;Liberation Mono&amp;quot;, Menlo, Courier, monospace; font-size: x-large; white-space: pre-wrap;&quot;&gt; &lt;/b&gt;&lt;/pre&gt;&lt;/div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;p&gt;&lt;/p&gt;&lt;/div&gt;

&lt;div style=&quot;text-align: center;&quot;&gt;&lt;iframe allow=&quot;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture&quot; allowfullscreen=&quot;&quot; frameborder=&quot;0&quot; height=&quot;315&quot; src=&quot;https://www.youtube.com/embed/17jMx0-T6n0&quot; width=&quot;560&quot;&gt;&lt;/iframe&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://programmings-java.blogspot.com/feeds/8244861672016623461/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://programmings-java.blogspot.com/2020/09/longest-increasing-subsequence-program.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/4859863201677788807/posts/default/8244861672016623461'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/4859863201677788807/posts/default/8244861672016623461'/><link rel='alternate' type='text/html' href='https://programmings-java.blogspot.com/2020/09/longest-increasing-subsequence-program.html' title='Longest Increasing Subsequence Program (Leetcode)'/><author><name>Mohd Talha</name><uri>http://www.blogger.com/profile/01763375607514819581</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://img.youtube.com/vi/17jMx0-T6n0/default.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4859863201677788807.post-731326903247854279</id><published>2020-09-21T21:28:00.002+05:30</published><updated>2021-01-11T15:26:30.485+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Algorithms/Sortings"/><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><title type='text'>Interview Questions Asked in TCS &amp; Infosys</title><content type='html'>&lt;p&gt;&lt;b&gt;&lt;u&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Interview Questions Asked in TCS &amp;amp; Infosys&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;span style=&quot;font-weight: 400;&quot;&gt;(You will get the video explanation at the bottom of page)&lt;/span&gt;&lt;/span&gt;&lt;/h4&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;u&gt;1) Prime Numbers using Sieve of Eratosthenes:&lt;/u&gt;&lt;/span&gt;&lt;/h4&gt;&lt;p style=&quot;text-align: left;&quot;&gt;# https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes&lt;/p&gt;&lt;p style=&quot;text-align: left;&quot;&gt;&lt;!--HTML generated using hilite.me--&gt;&lt;/p&gt;&lt;div data-darkreader-inline-bgcolor=&quot;&quot; data-darkreader-inline-bgimage=&quot;&quot; data-darkreader-inline-border-bottom=&quot;&quot; data-darkreader-inline-border-left=&quot;&quot; data-darkreader-inline-border-right=&quot;&quot; data-darkreader-inline-border-top=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #1c1d1d; --darkreader-inline-bgimage: initial; --darkreader-inline-border-bottom: #505556; --darkreader-inline-border-left: #505556; --darkreader-inline-border-right: #505556; --darkreader-inline-border-top: #505556; background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;def&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #5ea5d9; color: #0066bb; font-weight: bold;&quot;&gt;prime&lt;/span&gt;(n):
    prime_list &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; [&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;True&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d0cdc8; color: black; font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;range&lt;/span&gt;(n &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;+&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;)]
    p &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;2&lt;/span&gt;
    &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt; p &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;*&lt;/span&gt; p &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;&amp;lt;=&lt;/span&gt; n:
        &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; prime_list[&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;True&lt;/span&gt;]:
            &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d0cdc8; color: black; font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;range&lt;/span&gt;(p &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;*&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;2&lt;/span&gt;, n &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;+&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;, p):
                prime_list[i] &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;False&lt;/span&gt;

        p &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;+=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;
    prime_list[&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;] &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;False&lt;/span&gt;
    prime_list[&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;] &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;False&lt;/span&gt;
    &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; prime_list

ans &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; prime(&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;20&lt;/span&gt;)
&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d0cdc8; color: black; font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;range&lt;/span&gt;(&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;len&lt;/span&gt;(ans)):
    &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; ans[i]:
        &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;print&lt;/span&gt;(i, end &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span data-darkreader-inline-bgcolor=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #360808; background-color: #fff0f0;&quot;&gt;&quot; &quot;&lt;/span&gt;)
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;u&gt;2) Frequency Count:&lt;/u&gt;&lt;/span&gt;&lt;/b&gt;&lt;/h4&gt;&lt;p&gt;&lt;!--HTML generated using hilite.me--&gt;&lt;/p&gt;&lt;div data-darkreader-inline-bgcolor=&quot;&quot; data-darkreader-inline-bgimage=&quot;&quot; data-darkreader-inline-border-bottom=&quot;&quot; data-darkreader-inline-border-left=&quot;&quot; data-darkreader-inline-border-right=&quot;&quot; data-darkreader-inline-border-top=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #1c1d1d; --darkreader-inline-bgimage: initial; --darkreader-inline-border-bottom: #505556; --darkreader-inline-border-left: #505556; --darkreader-inline-border-right: #505556; --darkreader-inline-border-top: #505556; background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;s &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span data-darkreader-inline-bgcolor=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #360808; background-color: #fff0f0;&quot;&gt;&#39;aabbcaabc&#39;&lt;/span&gt;

&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #8e867b; color: #888888;&quot;&gt;#Using Counter&lt;/span&gt;
&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;from&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #56accf; color: #0e84b5; font-weight: bold;&quot;&gt;collections&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;import&lt;/span&gt; Counter
freq &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; Counter(s)
&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;print&lt;/span&gt;(freq)


&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #8e867b; color: #888888;&quot;&gt;#Using Dictionary &lt;/span&gt;
d &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;dict&lt;/span&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;fromkeys(s, &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;)
&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;print&lt;/span&gt;(d)
&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d0cdc8; color: black; font-weight: bold;&quot;&gt;in&lt;/span&gt; s:
    d[i] &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; d[i] &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;+&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;

&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;print&lt;/span&gt;(&lt;span data-darkreader-inline-bgcolor=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #360808; background-color: #fff0f0;&quot;&gt;&quot;Frequency :&quot;&lt;/span&gt;, d)
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;u&gt;3) Nth Largest Number:&lt;/u&gt;&lt;/span&gt;&lt;/b&gt;&lt;/h4&gt;&lt;p&gt;&lt;!--HTML generated using hilite.me--&gt;&lt;/p&gt;&lt;div data-darkreader-inline-bgcolor=&quot;&quot; data-darkreader-inline-bgimage=&quot;&quot; data-darkreader-inline-border-bottom=&quot;&quot; data-darkreader-inline-border-left=&quot;&quot; data-darkreader-inline-border-right=&quot;&quot; data-darkreader-inline-border-top=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #1c1d1d; --darkreader-inline-bgimage: initial; --darkreader-inline-border-bottom: #505556; --darkreader-inline-border-left: #505556; --darkreader-inline-border-right: #505556; --darkreader-inline-border-top: #505556; background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;val &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; [&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;4&lt;/span&gt;, &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;9&lt;/span&gt;, &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;5&lt;/span&gt;, &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;6&lt;/span&gt;, &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;, &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;2&lt;/span&gt;, &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;7&lt;/span&gt;]
&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;import&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #56accf; color: #0e84b5; font-weight: bold;&quot;&gt;heapq&lt;/span&gt;
heapq&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;heapify(val)
&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;print&lt;/span&gt;(heapq&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;nlargest(&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;2&lt;/span&gt;, val))
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;u&gt;4) Combinations:&lt;/u&gt;&lt;/span&gt;&lt;/b&gt;&lt;/h4&gt;&lt;p style=&quot;text-align: left;&quot;&gt;&lt;!--HTML generated using hilite.me--&gt;&lt;/p&gt;&lt;div data-darkreader-inline-bgcolor=&quot;&quot; data-darkreader-inline-bgimage=&quot;&quot; data-darkreader-inline-border-bottom=&quot;&quot; data-darkreader-inline-border-left=&quot;&quot; data-darkreader-inline-border-right=&quot;&quot; data-darkreader-inline-border-top=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #1c1d1d; --darkreader-inline-bgimage: initial; --darkreader-inline-border-bottom: #505556; --darkreader-inline-border-left: #505556; --darkreader-inline-border-right: #505556; --darkreader-inline-border-top: #505556; background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;comb &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; [&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;, &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;2&lt;/span&gt;, &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;3&lt;/span&gt;]
&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;from&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #56accf; color: #0e84b5; font-weight: bold;&quot;&gt;itertools&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;import&lt;/span&gt; combinations

&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #8e867b; color: #888888;&quot;&gt;#print(list(combinations(comb, 1)))&lt;/span&gt;
ans &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; []
&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d0cdc8; color: black; font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;range&lt;/span&gt;(&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;, &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;len&lt;/span&gt;(comb) &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;+&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;):
    ans&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;append(&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;list&lt;/span&gt;(combinations(comb, i)))
&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;print&lt;/span&gt;(&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d0cdc8; color: black; font-weight: bold;&quot;&gt;and&lt;/span&gt;)

&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #8e867b; color: #888888;&quot;&gt;#Binary&lt;/span&gt;
bin_val &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; [&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;str&lt;/span&gt;(&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;bin&lt;/span&gt;(i))[&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;2&lt;/span&gt;:] &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d0cdc8; color: black; font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;range&lt;/span&gt;(&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;, &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;2&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;**&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;len&lt;/span&gt;(comb))]
val &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;list&lt;/span&gt;(&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;map&lt;/span&gt;(&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;str&lt;/span&gt;,comb[:]))
&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;print&lt;/span&gt;(bin_val)

&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d0cdc8; color: black; font-weight: bold;&quot;&gt;in&lt;/span&gt; bin_val:
    temp &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span data-darkreader-inline-bgcolor=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #360808; background-color: #fff0f0;&quot;&gt;&#39;&#39;&lt;/span&gt;
    &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt; j &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d0cdc8; color: black; font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;range&lt;/span&gt;(&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;len&lt;/span&gt;(i)):
        &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; i[j] &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;==&lt;/span&gt; &lt;span data-darkreader-inline-bgcolor=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #360808; background-color: #fff0f0;&quot;&gt;&#39;1&#39;&lt;/span&gt;:
            temp &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;+=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;str&lt;/span&gt;(comb[j])
    val&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;append(temp)

&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;print&lt;/span&gt;(&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;set&lt;/span&gt;(val))
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;&lt;p style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;&lt;/p&gt;&lt;h4 style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;u&gt;5) Middle Element of Linked List in Single Loop:&lt;/u&gt;&lt;/span&gt;&lt;/h4&gt;&lt;p style=&quot;text-align: left;&quot;&gt;&lt;!-- HTML generated using hilite.me --&gt;&lt;div style=&quot;background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;&quot;&gt;&lt;pre style=&quot;margin: 0; line-height: 125%&quot;&gt;&lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #BB0066; font-weight: bold&quot;&gt;Node&lt;/span&gt;:  
    &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: #0066BB; font-weight: bold&quot;&gt;__init__&lt;/span&gt;(&lt;span style=&quot;color: #007020&quot;&gt;self&lt;/span&gt;, data, &lt;span style=&quot;color: #007020&quot;&gt;next&lt;/span&gt; &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;None&lt;/span&gt;):  
        &lt;span style=&quot;color: #007020&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;data &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; data  
        &lt;span style=&quot;color: #007020&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;next &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #007020&quot;&gt;next&lt;/span&gt;                

&lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: #0066BB; font-weight: bold&quot;&gt;print_List&lt;/span&gt;(List):
    l &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; []
    &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;while&lt;/span&gt; List &lt;span style=&quot;color: #333333&quot;&gt;!=&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;None&lt;/span&gt;:
        l&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;append(List&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;data)
        List &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; List&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;next

    &lt;span style=&quot;color: #007020&quot;&gt;print&lt;/span&gt;(l)
              
array &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; [&lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;2&lt;/span&gt;, &lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;3&lt;/span&gt;, &lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;4&lt;/span&gt;, &lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;5&lt;/span&gt;, &lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;6&lt;/span&gt;, &lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;7&lt;/span&gt;]
List &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;None&lt;/span&gt;

&lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: #000000; font-weight: bold&quot;&gt;in&lt;/span&gt; array:
    List &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; Node(i, List)
&lt;span style=&quot;color: #888888&quot;&gt;#print_List(List)&lt;/span&gt;
ptr1 &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; List
count &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;1&lt;/span&gt;

&lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;while&lt;/span&gt; count &lt;span style=&quot;color: #333333&quot;&gt;!=&lt;/span&gt; &lt;span style=&quot;color: #007020&quot;&gt;len&lt;/span&gt;(array) &lt;span style=&quot;color: #333333&quot;&gt;-&lt;/span&gt; &lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;1&lt;/span&gt;:
    &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;if&lt;/span&gt; count &lt;span style=&quot;color: #333333&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;1&lt;/span&gt; &lt;span style=&quot;color: #333333&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;1&lt;/span&gt;:
        ptr1 &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; ptr1&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;next
    count &lt;span style=&quot;color: #333333&quot;&gt;+=&lt;/span&gt; &lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;1&lt;/span&gt;
&lt;span style=&quot;color: #007020&quot;&gt;print&lt;/span&gt;(ptr1&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;data)
&lt;/pre&gt;&lt;/div&gt;
&lt;br /&gt;&lt;p style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;&lt;/p&gt;
&lt;iframe allow=&quot;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture&quot; allowfullscreen=&quot;&quot; frameborder=&quot;0&quot; height=&quot;315&quot; src=&quot;https://www.youtube.com/embed/iXjY0dvcTaE&quot; width=&quot;560&quot;&gt;&lt;/iframe&gt;</content><link rel='replies' type='application/atom+xml' href='https://programmings-java.blogspot.com/feeds/731326903247854279/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://programmings-java.blogspot.com/2020/09/interview-questions-asked-in-tcs-infosys.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/4859863201677788807/posts/default/731326903247854279'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/4859863201677788807/posts/default/731326903247854279'/><link rel='alternate' type='text/html' href='https://programmings-java.blogspot.com/2020/09/interview-questions-asked-in-tcs-infosys.html' title='Interview Questions Asked in TCS &amp; Infosys'/><author><name>Mohd Talha</name><uri>http://www.blogger.com/profile/01763375607514819581</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://img.youtube.com/vi/iXjY0dvcTaE/default.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4859863201677788807.post-8377464793029229559</id><published>2020-09-21T15:30:00.011+05:30</published><updated>2021-01-11T15:27:07.641+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Algorithms/Sortings"/><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><title type='text'>Programming Solutions</title><content type='html'>&lt;h4 style=&quot;text-align: left;&quot;&gt;&lt;b&gt;&lt;u&gt;&lt;span style=&quot;font-size: x-large;&quot;&gt;Programming Solutions&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;/h4&gt;&lt;p style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Hi,&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;b&gt;Programming Solutions&lt;/b&gt; provides a free concept for &lt;b&gt;Computer Programs&lt;/b&gt;. It contains videos lectures of competitive &lt;b&gt;programming&lt;/b&gt; as well as a basic concept for beginners. It will help the user to practice competitive level programming asked in Product Based companies like &lt;b&gt;Facebook, Google, Amazon, Netflix, Apple, Microsoft&lt;/b&gt; and others.&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;text-align: left;&quot;&gt;&lt;/p&gt;&lt;br /&gt;&lt;p style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Sharing you some programs like:&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;b&gt;1) Edit Distance:&amp;nbsp;&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;In this, you have been given two strings and now you have to convert one string to another, and you can only perform three operations like Insert, Replace, Delete.&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;This program is done by &lt;b&gt;Dynamic Programming&lt;/b&gt;.&lt;/span&gt;&lt;/p&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=KZC4ad0rePY&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot; target=&quot;_blank&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;544&quot; data-original-width=&quot;1346&quot; height=&quot;259&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjWXwZhh6um5AVOEQbh6TLtxnzcKhzQkSjaZCRZdIQHDAR0erNOxH61YMU8ZkocZi76xr1peoQX5jGf-ZyyH87IBkXr0JtSlxrOHSJLTi7kikX-qNE97FQbudiPNhUjo_6U4NRk7gxEFmDZ/w640-h259/Edit+Distance.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;Please click on image for an explanation&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;2) Coin Change:&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;p style=&quot;clear: both; text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;In this program, you have been given some coins and an amount. Now, you have to find the minimum numbers of coins needed to reach that amount.&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;clear: both; text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;OR&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;clear: both; text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Find the number of combinations that make up that amount.&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;clear: both; text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Example: Coin : [1, 2, 5]&amp;nbsp; Amount : 5&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;clear: both; text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;We can take one Rs 5, two Rs 2 and one Rs 1 and so on. There is four possible way. It seems to be confusing by writing similar numbers.&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;clear: both; text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;You can check the explanation by clicking below image:&lt;/span&gt;&lt;/p&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://i.ytimg.com/vi/kwKlXXm3BDw/hqdefault.jpg?sqp=-oaymwEZCPYBEIoBSFXyq4qpAwsIARUAAIhCGAFwAQ==&amp;amp;rs=AOn4CLC9N1jTWFrrB27aacW0AQrTcXAJOw&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot; target=&quot;_blank&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;650&quot; data-original-width=&quot;1362&quot; height=&quot;306&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjPFTqLDa7_U2Sgy82PV4QPQrOBP8ysuDXua4EmfG15NsNaA3D9UDUp8frlO-thJrGxrEns4VJaCMC6kofTD7DfjSlEcfyJXzfJiP-G2jxJYBhgVSCedL48r4jRNzV3qJ1xenMFKXcClXPq/w640-h306/Screenshot+%252888%2529.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;3) Longest Mountain in Array:&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;p style=&quot;clear: both; text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;In this program, given a list and you have to find the longest mountain.&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;clear: both; text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Example: [1, 2, 2, 4, 5, 3, 2, 1, 10, 7]&lt;br /&gt;Longest Mountains is: 6 [2, 4, 5, 3, 2, 1].&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;clear: both; text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Because aa mountain has a peak and its left and right side must be in decreasing order. There are some more mountains like [1, 2], [1, 10, 7], but the longest is of 6 lengths.&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;clear: both; text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Check the explanation here:&lt;/span&gt;&lt;/p&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;&lt;iframe allow=&quot;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture&quot; allowfullscreen=&quot;&quot; frameborder=&quot;0&quot; height=&quot;315&quot; src=&quot;https://www.youtube.com/embed/0_iAW8aAHME&quot; width=&quot;560&quot;&gt;&lt;/iframe&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://programmings-java.blogspot.com/feeds/8377464793029229559/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://programmings-java.blogspot.com/2020/09/programming-solutions.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/4859863201677788807/posts/default/8377464793029229559'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/4859863201677788807/posts/default/8377464793029229559'/><link rel='alternate' type='text/html' href='https://programmings-java.blogspot.com/2020/09/programming-solutions.html' title='Programming Solutions'/><author><name>Mohd Talha</name><uri>http://www.blogger.com/profile/01763375607514819581</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjWXwZhh6um5AVOEQbh6TLtxnzcKhzQkSjaZCRZdIQHDAR0erNOxH61YMU8ZkocZi76xr1peoQX5jGf-ZyyH87IBkXr0JtSlxrOHSJLTi7kikX-qNE97FQbudiPNhUjo_6U4NRk7gxEFmDZ/s72-w640-h259-c/Edit+Distance.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4859863201677788807.post-1660462106795583765</id><published>2020-09-18T19:10:00.005+05:30</published><updated>2021-06-12T14:31:34.336+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Algorithms/Sortings"/><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><title type='text'>Partition Equal Subset Sum Program (Leetcode)</title><content type='html'>&lt;h4 style=&quot;text-align: left;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Partition Equal Subset Sum Program (Leetcode):&lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/h4&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;Given a&amp;nbsp;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;non-empty&lt;/span&gt;&amp;nbsp;array&amp;nbsp;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;nums&lt;/code&gt;&amp;nbsp;containing&amp;nbsp;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;only positive integers&lt;/span&gt;, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal.&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Example 1:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;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; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Input:&lt;/span&gt; nums = [1,5,11,5]
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Output:&lt;/span&gt; true
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Explanation:&lt;/span&gt; The array can be partitioned as [1, 5, 5] and [11].
&lt;/pre&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Example 2:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;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; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Input:&lt;/span&gt; nums = [1,2,3,5]
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Output:&lt;/span&gt; false
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Explanation:&lt;/span&gt; The array cannot be partitioned into equal sum subsets.
&lt;/pre&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&amp;nbsp;&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Constraints:&lt;/span&gt;&lt;/p&gt;&lt;ul style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;1 &amp;lt;= nums.length &amp;lt;= 200&lt;/code&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;1 &amp;lt;= nums[i] &amp;lt;= 100&lt;/code&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;!--HTML generated using hilite.me--&gt;&lt;/p&gt;&lt;div data-darkreader-inline-bgcolor=&quot;&quot; data-darkreader-inline-bgimage=&quot;&quot; data-darkreader-inline-border-bottom=&quot;&quot; data-darkreader-inline-border-left=&quot;&quot; data-darkreader-inline-border-right=&quot;&quot; data-darkreader-inline-border-top=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #1c1d1d; --darkreader-inline-bgimage: initial; --darkreader-inline-border-bottom: #505556; --darkreader-inline-border-left: #505556; --darkreader-inline-border-right: #505556; --darkreader-inline-border-top: #505556; background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;total &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;sum&lt;/span&gt;(nums)
&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; total &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;==&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;:
     &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;False&lt;/span&gt;

&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;def&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #5ea5d9; color: #0066bb; font-weight: bold;&quot;&gt;subset&lt;/span&gt;(nums, index, target):
     &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; target &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;==&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;:
          &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;True&lt;/span&gt;

     &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; target &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d0cdc8; color: black; font-weight: bold;&quot;&gt;or&lt;/span&gt; index &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;==&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;len&lt;/span&gt;(nums):
          &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;False&lt;/span&gt;

     &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; subset(nums, index &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;+&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;, target &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;-&lt;/span&gt; nums[index]):
          &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;True&lt;/span&gt;
            
     i &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; index &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;+&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;
     &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt; i &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;len&lt;/span&gt;(nums) &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d0cdc8; color: black; font-weight: bold;&quot;&gt;and&lt;/span&gt; nums[i] &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;==&lt;/span&gt; nums[index]: 
     &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #8e867b; color: #888888;&quot;&gt;#100 100 100 ... multiple times&lt;/span&gt;
          i &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;+=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;
     
&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; subset(nums, i, target)

&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;print&lt;/span&gt;(subset(nums, &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;, total &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;//&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;2&lt;/span&gt;))  
&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #8e867b; color: #888888;&quot;&gt;#total // 2 because for 2 subset, sum must be half.&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;b&gt;This program is similar to &lt;a href=&quot;https://www.youtube.com/watch?v=Py1sMjtnFcY&amp;amp;t=54s&quot; target=&quot;_blank&quot;&gt;Combination Sum 2 or Sum of Subset&lt;/a&gt;.&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;div style=&quot;text-align: center;&quot;&gt;&lt;iframe allow=&quot;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture&quot; allowfullscreen=&quot;&quot; frameborder=&quot;0&quot; height=&quot;538&quot; src=&quot;https://www.youtube.com/embed/yMDC66-Aye4&quot; width=&quot;956&quot;&gt;&lt;/iframe&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://programmings-java.blogspot.com/feeds/1660462106795583765/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://programmings-java.blogspot.com/2020/09/partition-equal-subset-sum-program.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/4859863201677788807/posts/default/1660462106795583765'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/4859863201677788807/posts/default/1660462106795583765'/><link rel='alternate' type='text/html' href='https://programmings-java.blogspot.com/2020/09/partition-equal-subset-sum-program.html' title='Partition Equal Subset Sum Program (Leetcode)'/><author><name>Mohd Talha</name><uri>http://www.blogger.com/profile/01763375607514819581</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://img.youtube.com/vi/yMDC66-Aye4/default.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4859863201677788807.post-6344398458290521463</id><published>2020-09-16T22:04:00.004+05:30</published><updated>2021-06-12T14:32:47.045+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Algorithms/Sortings"/><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><title type='text'>Jump Game Program (Leetcode)</title><content type='html'>&lt;div style=&quot;text-align: left;&quot;&gt;&lt;b&gt;&lt;u&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Jump Game Program (Leetcode):&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;b&gt;&lt;u&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;b&gt;&lt;u&gt;&lt;span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;Given an array of non-negative integers&amp;nbsp;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;nums&lt;/code&gt;, you are initially positioned at the&amp;nbsp;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;first index&lt;/span&gt;&amp;nbsp;of the array.&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;Each element in the array represents your maximum jump length at that position.&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;Determine if you are able to reach the last index.&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Example 1:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;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; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Input:&lt;/span&gt; nums = [2,3,1,1,4]
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Output:&lt;/span&gt; true
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Explanation:&lt;/span&gt; Jump 1 step from index 0 to 1, then 3 steps to the last index.
&lt;/pre&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Example 2:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;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; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Input:&lt;/span&gt; nums = [3,2,1,0,4]
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Output:&lt;/span&gt; false
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Explanation:&lt;/span&gt; You will always arrive at index 3 no matter what. Its maximum jump length is 0, which makes it impossible to reach the last index.&lt;/pre&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Constraints:&lt;/span&gt;&lt;/p&gt;&lt;ul style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;1 &amp;lt;= nums.length &amp;lt;= 10&lt;span style=&quot;box-sizing: border-box; line-height: 0; position: relative; top: -0.5em; vertical-align: baseline;&quot;&gt;4&lt;/span&gt;&lt;/code&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;0 &amp;lt;= nums[i] &amp;lt;= 10&lt;span style=&quot;box-sizing: border-box; line-height: 0; position: relative; top: -0.5em; vertical-align: baseline;&quot;&gt;5&lt;/span&gt;&lt;/code&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span style=&quot;color: #546e7a; font-family: monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;p style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;!--HTML generated using hilite.me--&gt;&lt;/span&gt;&lt;/p&gt;&lt;div data-darkreader-inline-bgcolor=&quot;&quot; data-darkreader-inline-bgimage=&quot;&quot; data-darkreader-inline-border-bottom=&quot;&quot; data-darkreader-inline-border-left=&quot;&quot; data-darkreader-inline-border-right=&quot;&quot; data-darkreader-inline-border-top=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #1c1d1d; --darkreader-inline-bgimage: initial; --darkreader-inline-border-bottom: #505556; --darkreader-inline-border-left: #505556; --darkreader-inline-border-right: #505556; --darkreader-inline-border-top: #505556; background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;ans &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;
&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d0cdc8; color: black; font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;range&lt;/span&gt;(&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;len&lt;/span&gt;(nums)):
    &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; i &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;+&lt;/span&gt; nums[i] &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;&amp;gt;=&lt;/span&gt; ans:
        ans &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; i &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;+&lt;/span&gt; nums[i]
    &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; i &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;==&lt;/span&gt; ans:
        &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;break&lt;/span&gt;
&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;print&lt;/span&gt;(ans &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;len&lt;/span&gt;(nums) &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;-&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;)&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style=&quot;text-align: center;&quot;&gt;&lt;iframe allow=&quot;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture&quot; allowfullscreen=&quot;&quot; frameborder=&quot;0&quot; height=&quot;538&quot; src=&quot;https://www.youtube.com/embed/prRUrM-S0yI&quot; width=&quot;956&quot;&gt;&lt;/iframe&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://programmings-java.blogspot.com/feeds/6344398458290521463/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://programmings-java.blogspot.com/2020/09/jump-game-program-leetcode.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/4859863201677788807/posts/default/6344398458290521463'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/4859863201677788807/posts/default/6344398458290521463'/><link rel='alternate' type='text/html' href='https://programmings-java.blogspot.com/2020/09/jump-game-program-leetcode.html' title='Jump Game Program (Leetcode)'/><author><name>Mohd Talha</name><uri>http://www.blogger.com/profile/01763375607514819581</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://img.youtube.com/vi/prRUrM-S0yI/default.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4859863201677788807.post-8783775144986603364</id><published>2020-09-11T22:46:00.007+05:30</published><updated>2021-07-03T14:51:00.834+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Algorithms/Sortings"/><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><title type='text'>Coin Change 2 Program (Leetcode)</title><content type='html'>&lt;h4 style=&quot;text-align: left;&quot;&gt;&lt;b&gt;&lt;u&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Coin Change 2 Program (Leetcode):&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;/h4&gt;&lt;div&gt;&lt;p data-darkreader-inline-bgcolor=&quot;&quot; data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #1d1e1f; --darkreader-inline-color: #c0bbb3; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;You are given an integer array&amp;nbsp;&lt;code data-darkreader-inline-bgcolor=&quot;&quot; data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #1c2326; --darkreader-inline-color: #9f978d; background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;coins&lt;/code&gt;&amp;nbsp;representing coins of different denominations and an integer&amp;nbsp;&lt;code data-darkreader-inline-bgcolor=&quot;&quot; data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #1c2326; --darkreader-inline-color: #9f978d; background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;amount&lt;/code&gt;&amp;nbsp;representing a total amount of money.&lt;/p&gt;&lt;p data-darkreader-inline-bgcolor=&quot;&quot; data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #1d1e1f; --darkreader-inline-color: #c0bbb3; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;Return&amp;nbsp;&lt;em style=&quot;box-sizing: border-box;&quot;&gt;the number of combinations that make up that amount&lt;/em&gt;. If that amount of money cannot be made up by any combination of the coins, return&amp;nbsp;&lt;code data-darkreader-inline-bgcolor=&quot;&quot; data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #1c2326; --darkreader-inline-color: #9f978d; background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;0&lt;/code&gt;.&lt;/p&gt;&lt;p data-darkreader-inline-bgcolor=&quot;&quot; data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #1d1e1f; --darkreader-inline-color: #c0bbb3; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;You may assume that you have an infinite number of each kind of coin.&lt;/p&gt;&lt;p data-darkreader-inline-bgcolor=&quot;&quot; data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #1d1e1f; --darkreader-inline-color: #c0bbb3; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;The answer is&amp;nbsp;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;guaranteed&lt;/span&gt;&amp;nbsp;to fit into a signed&amp;nbsp;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;32-bit&lt;/span&gt;&amp;nbsp;integer.&lt;/p&gt;&lt;p data-darkreader-inline-bgcolor=&quot;&quot; data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #1d1e1f; --darkreader-inline-color: #c0bbb3; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&amp;nbsp;&lt;/p&gt;&lt;p data-darkreader-inline-bgcolor=&quot;&quot; data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #1d1e1f; --darkreader-inline-color: #c0bbb3; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Example 1:&lt;/span&gt;&lt;/p&gt;&lt;pre data-darkreader-inline-bgcolor=&quot;&quot; data-darkreader-inline-bgimage=&quot;&quot; data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #1c2326; --darkreader-inline-bgimage: initial; --darkreader-inline-color: #c0bbb3; 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; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Input:&lt;/span&gt; amount = 5, coins = [1,2,5]
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Output:&lt;/span&gt; 4
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Explanation:&lt;/span&gt; there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
&lt;/pre&gt;&lt;p data-darkreader-inline-bgcolor=&quot;&quot; data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #1d1e1f; --darkreader-inline-color: #c0bbb3; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Example 2:&lt;/span&gt;&lt;/p&gt;&lt;pre data-darkreader-inline-bgcolor=&quot;&quot; data-darkreader-inline-bgimage=&quot;&quot; data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #1c2326; --darkreader-inline-bgimage: initial; --darkreader-inline-color: #c0bbb3; 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; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Input:&lt;/span&gt; amount = 3, coins = [2]
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Output:&lt;/span&gt; 0
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Explanation:&lt;/span&gt; the amount of 3 cannot be made up just with coins of 2.
&lt;/pre&gt;&lt;p data-darkreader-inline-bgcolor=&quot;&quot; data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #1d1e1f; --darkreader-inline-color: #c0bbb3; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Example 3:&lt;/span&gt;&lt;/p&gt;&lt;pre data-darkreader-inline-bgcolor=&quot;&quot; data-darkreader-inline-bgimage=&quot;&quot; data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #1c2326; --darkreader-inline-bgimage: initial; --darkreader-inline-color: #c0bbb3; 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; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Input:&lt;/span&gt; amount = 10, coins = [10]
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Output:&lt;/span&gt; 1
&lt;/pre&gt;&lt;p data-darkreader-inline-bgcolor=&quot;&quot; data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #1d1e1f; --darkreader-inline-color: #c0bbb3; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&amp;nbsp;&lt;/p&gt;&lt;p data-darkreader-inline-bgcolor=&quot;&quot; data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #1d1e1f; --darkreader-inline-color: #c0bbb3; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Constraints:&lt;/span&gt;&lt;/p&gt;&lt;ul data-darkreader-inline-bgcolor=&quot;&quot; data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #1d1e1f; --darkreader-inline-color: #c0bbb3; background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;code data-darkreader-inline-bgcolor=&quot;&quot; data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #1c2326; --darkreader-inline-color: #9f978d; background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;1 &amp;lt;= coins.length &amp;lt;= 300&lt;/code&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;code data-darkreader-inline-bgcolor=&quot;&quot; data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #1c2326; --darkreader-inline-color: #9f978d; background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;1 &amp;lt;= coins[i] &amp;lt;= 5000&lt;/code&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;All the values of&amp;nbsp;&lt;code data-darkreader-inline-bgcolor=&quot;&quot; data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #1c2326; --darkreader-inline-color: #9f978d; background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;coins&lt;/code&gt;&amp;nbsp;are&amp;nbsp;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;unique&lt;/span&gt;.&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;code data-darkreader-inline-bgcolor=&quot;&quot; data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #1c2326; --darkreader-inline-color: #9f978d; background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;0 &amp;lt;= amount &amp;lt;= 5000&lt;/code&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #9f978d; color: #546e7a; font-family: monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;!--HTML generated using hilite.me--&gt;&lt;/span&gt;&lt;/p&gt;&lt;div data-darkreader-inline-bgcolor=&quot;&quot; data-darkreader-inline-bgimage=&quot;&quot; data-darkreader-inline-border-bottom=&quot;&quot; data-darkreader-inline-border-left=&quot;&quot; data-darkreader-inline-border-right=&quot;&quot; data-darkreader-inline-border-top=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #1d1e1f; --darkreader-inline-bgimage: initial; --darkreader-inline-border-bottom: #54595b; --darkreader-inline-border-left: #54595b; --darkreader-inline-border-right: #54595b; --darkreader-inline-border-top: #54595b; background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;dp &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #beb8b0; color: #333333;&quot;&gt;=&lt;/span&gt; [&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4e84e1; color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #74ea70; color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #dbd8d3; color: black; font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #83eb9d; color: #007020;&quot;&gt;range&lt;/span&gt;(amount &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #beb8b0; color: #333333;&quot;&gt;+&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4e84e1; color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;)]
dp[&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4e84e1; color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;] &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #beb8b0; color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4e84e1; color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;        

&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #74ea70; color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #dbd8d3; color: black; font-weight: bold;&quot;&gt;in&lt;/span&gt; coins:
    &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #74ea70; color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt; j &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #dbd8d3; color: black; font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #83eb9d; color: #007020;&quot;&gt;range&lt;/span&gt;(&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4e84e1; color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;, amount &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #beb8b0; color: #333333;&quot;&gt;+&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4e84e1; color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;):
        &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #74ea70; color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; i &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #beb8b0; color: #333333;&quot;&gt;&amp;lt;=&lt;/span&gt; j:
            dp[j] &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #beb8b0; color: #333333;&quot;&gt;+=&lt;/span&gt; dp[j &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #beb8b0; color: #333333;&quot;&gt;-&lt;/span&gt; i]            

&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #83eb9d; color: #007020;&quot;&gt;print&lt;/span&gt;(dp[amount] &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #74ea70; color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; dp[amount] &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #beb8b0; color: #333333;&quot;&gt;!=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4e84e1; color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #74ea70; color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4e84e1; color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;)
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;
&lt;/span&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjB7itrwtWqvAy0V2BLCc8NKHZozVSnnaDXEOavFIMIw03u2iccyi3syKsfsqkHRmuXwkAmytP6IzGJDedYbat-lBcxrJRELX0Pt5MVfU_pQBgzS7tjEd_8lbDGXmbSBr-PS3IymGGqBwOq/s622/Screenshot+%252888%2529.webp&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;297&quot; data-original-width=&quot;622&quot; height=&quot;265&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjB7itrwtWqvAy0V2BLCc8NKHZozVSnnaDXEOavFIMIw03u2iccyi3syKsfsqkHRmuXwkAmytP6IzGJDedYbat-lBcxrJRELX0Pt5MVfU_pQBgzS7tjEd_8lbDGXmbSBr-PS3IymGGqBwOq/w554-h265/Screenshot+%252888%2529.webp&quot; width=&quot;554&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;br /&gt;&lt;/div&gt;

&lt;iframe allow=&quot;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture&quot; allowfullscreen=&quot;&quot; frameborder=&quot;0&quot; height=&quot;538&quot; src=&quot;https://www.youtube.com/embed/kwKlXXm3BDw&quot; width=&quot;956&quot;&gt;&lt;/iframe&gt;</content><link rel='replies' type='application/atom+xml' href='https://programmings-java.blogspot.com/feeds/8783775144986603364/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://programmings-java.blogspot.com/2020/09/coin-change-2-program-leetcode.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/4859863201677788807/posts/default/8783775144986603364'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/4859863201677788807/posts/default/8783775144986603364'/><link rel='alternate' type='text/html' href='https://programmings-java.blogspot.com/2020/09/coin-change-2-program-leetcode.html' title='Coin Change 2 Program (Leetcode)'/><author><name>Mohd Talha</name><uri>http://www.blogger.com/profile/01763375607514819581</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjB7itrwtWqvAy0V2BLCc8NKHZozVSnnaDXEOavFIMIw03u2iccyi3syKsfsqkHRmuXwkAmytP6IzGJDedYbat-lBcxrJRELX0Pt5MVfU_pQBgzS7tjEd_8lbDGXmbSBr-PS3IymGGqBwOq/s72-w554-h265-c/Screenshot+%252888%2529.webp" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4859863201677788807.post-634144033921501405</id><published>2020-09-11T22:28:00.006+05:30</published><updated>2021-06-12T14:35:12.005+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Algorithms/Sortings"/><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><title type='text'>Coin Change Program (Leetcode)</title><content type='html'>&lt;p style=&quot;text-align: left;&quot;&gt;&lt;b&gt;&lt;u&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Coin Change Program (Leetcode):&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;You are given an integer array&amp;nbsp;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;coins&lt;/code&gt;&amp;nbsp;representing coins of different denominations and an integer&amp;nbsp;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;amount&lt;/code&gt;&amp;nbsp;representing a total amount of money.&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;Return&amp;nbsp;&lt;em style=&quot;box-sizing: border-box;&quot;&gt;the fewest number of coins that you need to make up that amount&lt;/em&gt;. If that amount of money cannot be made up by any combination of the coins, return&amp;nbsp;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;-1&lt;/code&gt;.&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;You may assume that you have an infinite number of each kind of coin.&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Example 1:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;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; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Input:&lt;/span&gt; coins = [1,2,5], amount = 11
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Output:&lt;/span&gt; 3
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Explanation:&lt;/span&gt; 11 = 5 + 5 + 1
&lt;/pre&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Example 2:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;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; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Input:&lt;/span&gt; coins = [2], amount = 3
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Output:&lt;/span&gt; -1
&lt;/pre&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Example 3:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;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; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Input:&lt;/span&gt; coins = [1], amount = 0
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Output:&lt;/span&gt; 0
&lt;/pre&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Example 4:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;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; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Input:&lt;/span&gt; coins = [1], amount = 1
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Output:&lt;/span&gt; 1
&lt;/pre&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Example 5:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;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; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Input:&lt;/span&gt; coins = [1], amount = 2
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Output:&lt;/span&gt; 2
&lt;/pre&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&amp;nbsp;&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Constraints:&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;&lt;ul style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;1 &amp;lt;= coins.length &amp;lt;= 12&lt;/code&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;1 &amp;lt;= coins[i] &amp;lt;= 2&lt;span style=&quot;box-sizing: border-box; line-height: 0; position: relative; top: -0.5em; vertical-align: baseline;&quot;&gt;31&lt;/span&gt;&amp;nbsp;- 1&lt;/code&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;0 &amp;lt;= amount &amp;lt;= 10&lt;span style=&quot;box-sizing: border-box; line-height: 0; position: relative; top: -0.5em; vertical-align: baseline;&quot;&gt;4&lt;/span&gt;&lt;/code&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span style=&quot;color: #546e7a; font-family: monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;p style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;!--HTML generated using hilite.me--&gt;&lt;/span&gt;&lt;/p&gt;&lt;div data-darkreader-inline-bgcolor=&quot;&quot; data-darkreader-inline-bgimage=&quot;&quot; data-darkreader-inline-border-bottom=&quot;&quot; data-darkreader-inline-border-left=&quot;&quot; data-darkreader-inline-border-right=&quot;&quot; data-darkreader-inline-border-top=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #1c1d1d; --darkreader-inline-bgimage: initial; --darkreader-inline-border-bottom: #505556; --darkreader-inline-border-left: #505556; --darkreader-inline-border-right: #505556; --darkreader-inline-border-top: #505556; background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;num &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; [&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;float&lt;/span&gt;(&lt;span data-darkreader-inline-bgcolor=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #360808; background-color: #fff0f0;&quot;&gt;&quot;inf&quot;&lt;/span&gt;) &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d0cdc8; color: black; font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;range&lt;/span&gt;(amount &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;+&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;)]
num[&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;] &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;

&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d0cdc8; color: black; font-weight: bold;&quot;&gt;in&lt;/span&gt; coins:
    &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt; j &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d0cdc8; color: black; font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;range&lt;/span&gt;(amount &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;+&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;):
        &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; i &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;&amp;lt;=&lt;/span&gt; j:
            num[j] &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;min&lt;/span&gt;(num[j], &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;+&lt;/span&gt; num[j &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;-&lt;/span&gt; i])

&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #8e867b; color: #888888;&quot;&gt;#print(num)&lt;/span&gt;
&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; num[amount] &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; num[amount] &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;!=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;float&lt;/span&gt;(&lt;span data-darkreader-inline-bgcolor=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #360808; background-color: #fff0f0;&quot;&gt;&quot;inf&quot;&lt;/span&gt;) &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;-&lt;/span&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p style=&quot;text-align: left;&quot;&gt;&lt;/p&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=Yxp8kUlzrjA&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot; target=&quot;_blank&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;621&quot; data-original-width=&quot;1366&quot; height=&quot;281&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiqxZl4KXitiPNh_cw8ETWsxmiJd6C8kIwQgLXMXxq63LXE8-LJsjNtGWucoZAQsm5ovFzlyp-mNF7jByV8LkROOsjWLFypy4jBe9omI3wCoWSuZWdXxH7z-IzKeNkz0L82JrIodJ8Yg0NF/w625-h281/Screenshot+%252887%2529.png&quot; width=&quot;625&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;br /&gt;&lt;/div&gt;

&lt;div style=&quot;text-align: center;&quot;&gt;&lt;iframe allow=&quot;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture&quot; allowfullscreen=&quot;&quot; frameborder=&quot;0&quot; height=&quot;538&quot; src=&quot;https://www.youtube.com/embed/Yxp8kUlzrjA&quot; width=&quot;956&quot;&gt;&lt;/iframe&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://programmings-java.blogspot.com/feeds/634144033921501405/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://programmings-java.blogspot.com/2020/09/coin-change-program-leetcode.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/4859863201677788807/posts/default/634144033921501405'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/4859863201677788807/posts/default/634144033921501405'/><link rel='alternate' type='text/html' href='https://programmings-java.blogspot.com/2020/09/coin-change-program-leetcode.html' title='Coin Change Program (Leetcode)'/><author><name>Mohd Talha</name><uri>http://www.blogger.com/profile/01763375607514819581</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiqxZl4KXitiPNh_cw8ETWsxmiJd6C8kIwQgLXMXxq63LXE8-LJsjNtGWucoZAQsm5ovFzlyp-mNF7jByV8LkROOsjWLFypy4jBe9omI3wCoWSuZWdXxH7z-IzKeNkz0L82JrIodJ8Yg0NF/s72-w625-h281-c/Screenshot+%252887%2529.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4859863201677788807.post-1587132353868538576</id><published>2020-09-09T18:24:00.011+05:30</published><updated>2024-02-11T20:36:48.200+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Algorithms/Sortings"/><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><title type='text'>Combination Sum II Program (Leetcode)</title><content type='html'>&lt;meta name=&quot;keywords&quot; content=&quot;Combination Sum II Program, Combination Sum II Python, Combination Sum II Leetcode, Leetcode questions, leetcode solutions, interview questions, dsa, Python DSA Program, Data Structure and Algorithm, Programmings-Java&quot;&gt;
&lt;link rel=&quot;canonical&quot; href=&quot;https://programmings-java.blogspot.com/2020/09/combination-sum-ii-program-leetcode.html&quot;/&gt;
&lt;p&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;b&gt;&lt;u&gt;Combination Sum II Program (Leetcode):&lt;/u&gt;&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;text-align: justify;&quot;&gt;The Combination Sum II program on LeetCode is a problem that involves finding all unique combinations in a candidate set where the candidate numbers sum up to a given target. However, each number in the candidates may only be used once in each combination. This challenge extends the classic Combination Sum problem by adding the constraint of using each number only once. To solve this problem efficiently, candidates are sorted, and a backtracking algorithm is typically employed to explore all possible combinations while ensuring uniqueness. The program must handle duplicates in the candidate set and avoid generating duplicate combinations in the final result. This problem requires careful consideration of edge cases and proper handling of duplicates to produce the correct output efficiently.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;background-color: white; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;;&quot;&gt;Given a collection of candidate numbers (&lt;/span&gt;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;candidates&lt;/code&gt;&lt;span style=&quot;background-color: white; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;;&quot;&gt;) and a target number (&lt;/span&gt;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;target&lt;/code&gt;&lt;span style=&quot;background-color: white; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;;&quot;&gt;), find all unique combinations in&amp;nbsp;&lt;/span&gt;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;candidates&lt;/code&gt;&lt;span style=&quot;background-color: white; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;;&quot;&gt;&amp;nbsp;where the candidate numbers sum to&amp;nbsp;&lt;/span&gt;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;target&lt;/code&gt;&lt;span style=&quot;background-color: white; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;;&quot;&gt;.&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;Each number&amp;nbsp;&amp;nbsp;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;candidates&lt;/code&gt;&amp;nbsp;may only be used&amp;nbsp;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;once&lt;/span&gt;&amp;nbsp;in the combination.&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Note:&lt;/span&gt;&amp;nbsp;The solution set must not contain duplicate combinations.&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Example 1:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;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; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Input:&lt;/span&gt; candidates = [10,1,2,7,6,1,5], target = 8
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Output:&lt;/span&gt; 
[
[1,1,6],
[1,2,5],
[1,7],
[2,6]
]
&lt;/pre&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Example 2:&lt;/span&gt;&lt;/p&gt;&lt;pre style=&quot;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; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Input:&lt;/span&gt; candidates = [2,5,2,1,2], target = 5
&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Output:&lt;/span&gt; 
[
[1,2,2],
[5]
]
&lt;/pre&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&amp;nbsp;&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: bolder;&quot;&gt;Constraints:&lt;/span&gt;&lt;/p&gt;&lt;ul style=&quot;background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, BlinkMacSystemFont, &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;; margin-bottom: 1em; margin-top: 0px;&quot;&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;1 &amp;lt;=&amp;nbsp;candidates.length &amp;lt;= 100&lt;/code&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;1 &amp;lt;=&amp;nbsp;candidates[i] &amp;lt;= 50&lt;/code&gt;&lt;/li&gt;&lt;li style=&quot;box-sizing: border-box;&quot;&gt;&lt;code style=&quot;background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; padding: 2px 4px;&quot;&gt;1 &amp;lt;= target &amp;lt;= 30&lt;/code&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span style=&quot;color: #546e7a; font-family: monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;p&gt;&lt;span&gt;&lt;!--HTML generated using hilite.me--&gt;&lt;/span&gt;&lt;/p&gt;&lt;div data-darkreader-inline-bgcolor=&quot;&quot; data-darkreader-inline-bgimage=&quot;&quot; data-darkreader-inline-border-bottom=&quot;&quot; data-darkreader-inline-border-left=&quot;&quot; data-darkreader-inline-border-right=&quot;&quot; data-darkreader-inline-border-top=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #1c1d1d; --darkreader-inline-bgimage: initial; --darkreader-inline-border-bottom: #505556; --darkreader-inline-border-left: #505556; --darkreader-inline-border-right: #505556; --darkreader-inline-border-top: #505556; background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #8e867b; color: #888888;&quot;&gt;#s sum of current set, k index, r  remaining sum&lt;/span&gt;
&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;def&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #5ea5d9; color: #0066bb; font-weight: bold;&quot;&gt;subset&lt;/span&gt;(s, k, r): 
    x[k] &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;
    &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; s &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;+&lt;/span&gt; nums[k] &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;==&lt;/span&gt; target:
        p &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; []
        &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d0cdc8; color: black; font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;range&lt;/span&gt;(k &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;+&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;):
            &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; x[i] &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;==&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;:
                p&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;append(nums[i])

            val&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;append(p)

    &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;elif&lt;/span&gt; s &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;+&lt;/span&gt; r &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;&amp;gt;=&lt;/span&gt; target &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d0cdc8; color: black; font-weight: bold;&quot;&gt;and&lt;/span&gt; s &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;+&lt;/span&gt; nums[k] &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;&amp;lt;=&lt;/span&gt; target:
    subset(s &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;+&lt;/span&gt; nums[k], k &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;+&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;, r &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;-&lt;/span&gt; nums[k])
    &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; s &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;-&lt;/span&gt; nums[k] &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;+&lt;/span&gt; r &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;&amp;gt;=&lt;/span&gt; target &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d0cdc8; color: black; font-weight: bold;&quot;&gt;and&lt;/span&gt; s &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;+&lt;/span&gt; nums[k &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;+&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;] &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;&amp;lt;=&lt;/span&gt; target:
        x[k] &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;
        subset(s, k &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;+&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;, r &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;-&lt;/span&gt; nums[k])

x &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; [&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d0cdc8; color: black; font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;range&lt;/span&gt;(&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;len&lt;/span&gt;(nums))] &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #8e867b; color: #888888;&quot;&gt;#index&lt;/span&gt;
nums&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;sort()

val &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; []
subset(&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;, &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #4a7dd6; color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;, &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #7ddf95; color: #007020;&quot;&gt;sum&lt;/span&gt;(nums))        
ans &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;=&lt;/span&gt; []
&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d0cdc8; color: black; font-weight: bold;&quot;&gt;in&lt;/span&gt; val:
    &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt; i &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d0cdc8; color: black; font-weight: bold;&quot;&gt;not&lt;/span&gt; &lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #d0cdc8; color: black; font-weight: bold;&quot;&gt;in&lt;/span&gt; ans:
        ans&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #b4afa7; color: #333333;&quot;&gt;.&lt;/span&gt;append(i)

&lt;span data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-color: #6ede6b; color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; ans&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;pre data-darkreader-inline-bgcolor=&quot;&quot; data-darkreader-inline-bgimage=&quot;&quot; data-darkreader-inline-color=&quot;&quot; style=&quot;--darkreader-inline-bgcolor: #1b2225; --darkreader-inline-bgimage: initial; --darkreader-inline-color: #b6b1aa; background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; color: #263238; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px; white-space: pre-wrap;&quot;&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiAup4ci-kZCIJZSBOgWmm6qzvq8APDguTG7JwtXznV107zBQym9VWPN_9B2pnzjWYs_VoE2qaao3bVPoTl8OsJ8j_1rifK-b5NTUmY9kyTRAqBcLqrk9vxAGK0-1MxJZFjQGjPfCKM-huT/s824/Combination+Sum+II+Blog.jpg&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;368&quot; data-original-width=&quot;824&quot; height=&quot;222&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiAup4ci-kZCIJZSBOgWmm6qzvq8APDguTG7JwtXznV107zBQym9VWPN_9B2pnzjWYs_VoE2qaao3bVPoTl8OsJ8j_1rifK-b5NTUmY9kyTRAqBcLqrk9vxAGK0-1MxJZFjQGjPfCKM-huT/w500-h222/Combination+Sum+II+Blog.jpg&quot; width=&quot;500&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;p&gt;&lt;/p&gt;&lt;/pre&gt;

&lt;div style=&quot;text-align: center;&quot;&gt;&lt;iframe allow=&quot;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture&quot; allowfullscreen=&quot;&quot; frameborder=&quot;0&quot; height=&quot;538&quot; src=&quot;https://www.youtube.com/embed/Py1sMjtnFcY&quot; width=&quot;956&quot;&gt;&lt;/iframe&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://programmings-java.blogspot.com/feeds/1587132353868538576/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://programmings-java.blogspot.com/2020/09/combination-sum-ii-program-leetcode.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/4859863201677788807/posts/default/1587132353868538576'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/4859863201677788807/posts/default/1587132353868538576'/><link rel='alternate' type='text/html' href='https://programmings-java.blogspot.com/2020/09/combination-sum-ii-program-leetcode.html' title='Combination Sum II Program (Leetcode)'/><author><name>Mohd Talha</name><uri>http://www.blogger.com/profile/01763375607514819581</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiAup4ci-kZCIJZSBOgWmm6qzvq8APDguTG7JwtXznV107zBQym9VWPN_9B2pnzjWYs_VoE2qaao3bVPoTl8OsJ8j_1rifK-b5NTUmY9kyTRAqBcLqrk9vxAGK0-1MxJZFjQGjPfCKM-huT/s72-w500-h222-c/Combination+Sum+II+Blog.jpg" height="72" width="72"/><thr:total>0</thr:total></entry></feed>