tag:blogger.com,1999:blog-72360907686818978092024-02-07T11:04:52.691+05:30CodeityWeb | Learn Coding,Mini Projects,HackerRank SolutionsA Website To Learn Coding,Get Mini Projects With Implementations ,Competitive Programming ,HackerRank Solutions,Sololearn Problem Solutions.CodeityWebhttp://www.blogger.com/profile/01377516264362368650noreply@blogger.comBlogger23125tag:blogger.com,1999:blog-7236090768681897809.post-44414577692240129552021-02-26T18:06:00.000+05:302021-02-26T18:06:25.912+05:30Hackerrank Solutions<h1 style="text-align: left;"><span style="font-size: large;">Hackerrank Solutions By Codeityweb</span></h1><p><span style="font-size: medium;">In this post we are providing the list of hackerrank solutions that are discussed and solved on the codeityweb website .</span></p><p><span style="font-size: medium;">The list consists of hackerrank solutions for Competitive Sql along with other practice question solutions.</span></p><p><span style="font-size: medium;"> </span></p><p><span style="font-size: large;"><b><span style="color: #38761d;"><span>So the list is as follows:</span></span></b></span></p><ul style="text-align: left;"><li><h2><span style="font-size: large;"><b><span style="color: #38761d;"><span><span style="font-size: medium;"><a href="https://codeityweb.blogspot.com/2021/02/find-digits-hackerrank-solution.html" target="_blank">Find Digits Hackerrank Solution</a></span> </span></span></b></span></h2></li></ul><ul style="text-align: left;"><li><h2><span style="font-size: large;"><b><span style="color: #38761d;"><span><span style="font-size: medium;"><a href="https://codeityweb.blogspot.com/2021/02/climbing-the-leaderboard-hackerrank-solution.html" target="_blank"> Climbing the Leaderboard Hackerrank Solution </a></span> </span></span></b></span></h2></li></ul><ul style="text-align: left;"><li><h2><span style="font-size: large;"><b><span style="color: #38761d;"><span><span style="font-size: medium;"><a href="https://codeityweb.blogspot.com/2021/02/sherlock-and-anagrams-hackerrank-solution.html" target="_blank">Sherlock and Anagrams Hackerrank Solution </a></span> </span></span></b></span></h2></li></ul><ul style="text-align: left;"><li><h2><span style="font-size: large;"><b><span style="color: #38761d;"><span><span style="font-size: medium;"><a href="https://codeityweb.blogspot.com/2021/02/compare-triplets-hackerrank-solution.html" target="_blank">Compare The Triplets Hackerrank Solution </a></span> </span></span></b></span></h2></li></ul><ul style="text-align: left;"><li><h2><span style="font-size: medium;"><a href="https://codeityweb.blogspot.com/2021/01/the-report-hackerrank-solution-joins.html" target="_blank">The Report HackerRank Solution</a> <br /></span></h2></li><li><h2><span style="font-size: medium;"><a href="https://codeityweb.blogspot.com/2021/01/top-competitors-hackerrank-solutions.html" target="_blank">Top Competitors HackerRank Solution </a> <br /></span></h2></li><li><h2><span style="font-size: medium;"><a href="https://codeityweb.blogspot.com/2021/01/placements-hackerrank-solutions-sql.html" target="_blank">Placements HackerRank Solution</a> </span></h2></li><li><h2><span style="font-size: medium;"><a href="https://codeityweb.blogspot.com/2021/01/Simple-Method-For-Revising-the-Select-Query-II-Hackerrank-Solution.html" target="_blank">Revising the Select Query II Hackerrank Solution </a> </span></h2></li><li><h2><span style="font-size: medium;"><a href="https://codeityweb.blogspot.com/2021/01/Easiest-way-to-solve-SQL-Revising-the-Select-Query-I-HackerRank.html" target="_blank">Revising the Select Query I HackerRank Solution </a> </span></h2></li><li><h2><span style="font-size: medium;"><a href="https://codeityweb.blogspot.com/2021/01/heres-quick-way-to-solve-pairs.html" target="_blank">2 Ways To Solve The Pairs Hackerrank Problem</a> </span></h2></li><li><h2><span style="font-size: medium;"><a href="https://codeityweb.blogspot.com/2020/10/making-two-strings-anagrams.html" target="_blank">Making Anagrams -Hackerrank Solutions </a> </span></h2></li><li><h2><span style="font-size: medium;"><a href="https://codeityweb.blogspot.com/2020/10/birthday-cake-candles.html" target="_blank">Birthday Cake Candles Hackerrank Solution </a> </span></h2></li><li><h2><span style="font-size: medium;"><a href="https://codeityweb.blogspot.com/2020/10/diagonal-difference.html" target="_blank">Diagonal Difference Hackerrank Solution </a> </span></h2></li><li><h2><span style="font-size: medium;"><a href="https://codeityweb.blogspot.com/2020/09/delete-without-head-pointer.html" target="_blank">Delete without head pointer LinkedList </a> </span></h2></li><li><h2><span style="font-size: medium;"> <a href="https://codeityweb.blogspot.com/2020/09/bits-given-positive-integer-n-print.html" target="_blank">Count Set Bits- Bit manipulation</a> </span></h2><div><span style="font-size: medium;"> </span></div><div><span style="font-size: medium;">If Some Questions are missing you can ask them in comments of any of the posts.</span></div><div><span style="font-size: medium;"> </span></div><div><span style="font-size: medium;">Thank You for Reading! </span></div><div><span style="font-size: medium;"><br /></span></div><div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhQ3vcqfmfEGUbEg17SmLZbPNHTbv4jCDrC3QUS3Jl6CMTMziyxxNaLMV-66xvCLl8haiJNdKBy9lmc9DQBSDEXmthUY7a-fwjwNfVj8693TEm68qh4asN_l9XnhITATqFNq2mur5oKjFMh/s570/slogo_new.png" style="margin-left: 1em; margin-right: 1em;"><img alt="hackerrank solutions" border="0" data-original-height="570" data-original-width="570" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhQ3vcqfmfEGUbEg17SmLZbPNHTbv4jCDrC3QUS3Jl6CMTMziyxxNaLMV-66xvCLl8haiJNdKBy9lmc9DQBSDEXmthUY7a-fwjwNfVj8693TEm68qh4asN_l9XnhITATqFNq2mur5oKjFMh/w320-h320/slogo_new.png" style="display: none;" title="Codeityweb" width="320" /></a></div><br /><span style="font-size: medium;"><br /></span></div></li></ul><div class="blogger-post-footer">Contact Us By Email Or Get in touch by Commenting on posts</div>CodeityWebhttp://www.blogger.com/profile/01377516264362368650noreply@blogger.com0tag:blogger.com,1999:blog-7236090768681897809.post-20571814561014950492021-02-26T17:53:00.001+05:302021-02-26T17:53:43.799+05:30Find Digits Hackerrank Solution |Codeityweb<div><h1 style="text-align: left;"><span style="font-size: x-large;">Find Digits Hackerrank Solution Java and Python</span></h1><h2 style="text-align: left;"><span style="color: #38761d;"><span style="font-size: medium;">In this post we are going to discuss the Find Digits Hackerrank Solution using Java and Python as language of choice.</span></span></h2><p><u><i><span style="font-size: medium;">The Problem Statement is as Follows:</span></i></u></p><div style="text-align: left;"><span style="font-size: medium;">Given a integer find the count of digits that divide the integer.</span></div><div style="text-align: left;"><span style="font-size: medium;"> </span></div><div style="text-align: left;"><span style="font-size: medium;">Example: Suppose int i=12, the digits in the integer are 1 and 2 .</span></div><div style="text-align: left;"><span style="font-size: medium;">Now 12 is divisible by 2 and also it is divisible by 1 i.e 12%2==0 & 12&1==0 thus the count will be 2 and that will be the answer.</span></div><div style="text-align: left;"><span style="font-size: medium;"> </span></div><div style="text-align: left;"><span style="font-size: medium;">Suppose integer is 13, now digits are 1,3 . 13 is not divisible by 3 but divisible by 1 so the count will be 1 .So answer is 1. <br /></span></div><div style="text-align: left;"><span style="font-size: medium;"> </span></div><div style="text-align: left;"><h2 style="text-align: left;"><span style="color: #0b5394;"><span style="font-size: medium;">Algorithm and Approach for Find Digits Hackerrank Solution:</span></span></h2><ul style="text-align: left;"><li><span style="font-size: medium;">First we need to get the digits from the integer for that we use a loop and take modulo by 10.</span></li><li><span style="font-size: medium;">We check if the number is divisible by the digit and increment the counter.</span></li><li><span style="font-size: medium;">We will use try and catch or try and except for Java and Python respectively to deal with ArithmeticException divide by zero since the integer may contain digits like 1202 has 0 as a digit so we need to catch it.</span></li><li><span style="font-size: medium;">Finally return the total count.</span></li></ul><span style="font-size: medium;"> </span></div><div style="text-align: left;"><span style="font-size: medium;"> </span></div><div style="text-align: left;"><h2 style="text-align: left;"><span style="color: #0b5394;"><span style="font-size: medium;">Find Digits Hackerrank Solution Java:</span></span></h2></div><p> <span style="font-size: medium;"><br /></span> <span style="font-size: medium;"><span style="color: red;">static</span> <span style="color: #2b00fe;">int</span> findDigits(<span style="color: #2b00fe;">int</span> n) {<br /><br /></span> <span style="font-size: medium;"><span style="color: red;">int</span> temp=n;<br /></span> <span style="font-size: medium;"><span style="color: red;">int</span> count=0;<br /></span> <span style="font-size: medium;"><br /></span> <span style="font-size: medium;"><span style="color: red;">while</span>(temp>0)<br /> {<br /> <span style="color: #2b00fe;"><span> </span>int</span> digit=temp%<span style="color: #2b00fe;">10</span>;<br /> <span> </span>temp=temp/<span style="color: #2b00fe;">10</span>;<br /> <span style="color: red;"><span> </span>try</span>{<br /> <span> </span><span> </span>if(n%<span style="color: #2b00fe;">digit</span>==0)<br /><span> </span><span> </span> {<br /> <span> </span><span> </span><span> </span>count++;<br /> <span> </span><span> </span>}<br /> <span> </span>}<br /> <span style="color: red;"><span> </span>catch</span>(Exception e)<br /> <span> </span>{<br /> <span style="color: #2b00fe;"><span> </span><span> </span>continue</span>;<br /> <span> </span>}<br /> }<br /></span> <span style="font-size: medium;"><span style="color: red;">return</span> count;<br /> }</span></p><h2 style="text-align: left;"><span style="font-size: medium;"><br /></span></h2><h2 style="text-align: left;"><span style="color: #0b5394;"><span style="font-size: medium;">Find Digit Hackerrank Solution Python:</span></span></h2><p><span style="font-size: medium;"><span style="color: red;">def</span> findDigits(n):<br /> <span> </span>temp=n<br /> <span> </span>count=0<br /> <span> </span><span style="color: red;">while</span> temp>0:<br /> <span> </span></span><span style="font-size: medium;"><span> </span>digit=temp%10<br /></span><span style="font-size: medium;"><span> </span></span><span style="font-size: medium;"><span> </span> temp=temp//10<br /> <span> </span></span><span style="font-size: medium;"><span> </span><span style="color: red;">try</span>:<br /> <span> </span></span><span style="font-size: medium;"><span> </span></span><span style="font-size: medium;"><span> </span><span style="color: red;">if</span> n%digit==0:<br /> <span> </span></span><span style="font-size: medium;"><span> </span></span><span style="font-size: medium;"><span> </span></span><span style="font-size: medium;"><span> </span>count=count+1<br /> <span> </span></span><span style="font-size: medium;"><span> </span><span style="color: red;">except</span>:<br /> <span> </span></span><span style="font-size: medium;"><span> </span></span><span style="font-size: medium;"><span> </span>continue<br /></span><span style="font-size: medium;"><span> </span> <span style="color: red;"> return</span> count</span></p><p><span style="font-size: medium;"> </span></p><p><b><span style="font-size: medium;">Output:</span></b></p><p></p><div class="separator" style="clear: both; text-align: center;"><span style="font-size: medium;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgqjbyeqGvrOqkm0Lb6foQyhfb2GCFqGStK58GCFZGt-Z4-JqkSaJpyKdTcQ9rLsgOIfKFiKOB8xwyJAwrha2jHXkGxKPEllPXiHB6oDFouFSspv-t4_0Hwd-ivWUNBZay0pvf7cKzSRY5D/s944/Screenshot+from+2021-02-26+17-31-22.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="Find Digits Hackerrank Solution" border="0" data-original-height="608" data-original-width="944" height="412" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgqjbyeqGvrOqkm0Lb6foQyhfb2GCFqGStK58GCFZGt-Z4-JqkSaJpyKdTcQ9rLsgOIfKFiKOB8xwyJAwrha2jHXkGxKPEllPXiHB6oDFouFSspv-t4_0Hwd-ivWUNBZay0pvf7cKzSRY5D/w640-h412/Screenshot+from+2021-02-26+17-31-22.png" title="Find Digits Hackerrank Solution by codeityweb" width="640" /></a></span></div><span style="font-size: medium;"><br /> </span><p></p><p><span style="font-size: medium;"><b>Other Posts: </b><br /></span></p><ul style="text-align: left;"><li><h2><span style="font-size: medium;"><a href="https://codeityweb.blogspot.com/2021/02/climbing-the-leaderboard-hackerrank-solution.html" target="_blank">Climbing the Leaderboard Hackerrank Solution</a> </span></h2></li><li><h2><span style="font-size: medium;"><a href="https://codeityweb.blogspot.com/2021/02/sherlock-and-anagrams-hackerrank-solution.html" target="_blank">Sherlock and Anagrams Hackerrank Solution</a></span></h2></li><li><h2><span style="font-size: medium;"><a href="https://codeityweb.blogspot.com/2021/02/compare-triplets-hackerrank-solution.html" target="_blank">Compare The Triplets Hackerrank Solution</a> </span></h2></li></ul></div><h2 style="text-align: left;"><b><span style="font-size: medium;"><a href="https://codeityweb.blogspot.com/p/index.html" target="_blank">All Solutions At One Place:</a></span></b></h2><h2 style="text-align: left;"><b><span style="font-size: medium;"><a href="https://codeityweb.blogspot.com/p/index.html" target="_blank">Click Here</a></span></b></h2><p><span style="font-size: medium;"> </span></p><p><span style="font-size: medium;"> Thank You For Reading!</span></p><p><span style="font-size: medium;"> Any Doubts use comment section or contact form provided in the side bar. <br /></span></p><div class="blogger-post-footer">Contact Us By Email Or Get in touch by Commenting on posts</div>CodeityWebhttp://www.blogger.com/profile/01377516264362368650noreply@blogger.com0tag:blogger.com,1999:blog-7236090768681897809.post-9437731000851731132021-02-11T19:48:00.000+05:302021-02-11T19:48:58.563+05:30Climbing the Leaderboard Hackerrank Solution |Codeityweb<div><div><h1 style="text-align: left;"><span style="font-size: large;">Climbing the Leaderboard Hackerrank Solution </span></h1><h2 style="text-align: left;"><span style="color: #38761d;"><span style="font-size: medium;">In this post we are going to discuss the Climbing the Leaderboard Hackerrank Solution in java.</span></span></h2><p><i><u><span style="font-size: medium;">The problem statement is as follows:</span></u></i></p><span style="font-size: medium;">An arcade game player wants to climb to the top of the leaderboard and track their ranking. The game uses Dense Ranking, so its leaderboard works like this: <br /></span><ul style="text-align: left;"><li><span style="font-size: medium;">The player with the highest score is ranked number 1 on the leaderboard.</span></li><li><span style="font-size: medium;">Players who have equal scores receive the same ranking number, and the next player(s) receive the immediately following ranking number.</span></li></ul><p><span style="font-size: medium;"> </span></p><p><b><span style="font-size: medium;">Task :</span></b></p><p><span style="font-size: medium;">To determine the rank of the player for a particular set of scores.</span></p><p><b><span style="font-size: medium;">Understanding the problem statement:</span></b></p><ul style="text-align: left;"><li><span style="font-size: medium;">You are given an array of scores that are scored by the players say array <b><span style="color: #38761d;">scores[100,90,90,80</span>] </b>and another array of scores for a particular player called as alice say <span style="color: #38761d;"><b>alice_score[70,80,105].</b></span></span></li><li><span style="font-size: medium;"><b> </b>Now your task is to find the rank of alice for his score depending upon the the scores provided of all players.</span></li><li><span style="font-size: medium;">Say in the above example the rank of the players will be <span style="color: #cc0000;">1,2,2,3</span> for <span style="color: #cc0000;">100,90,90,80</span> scores .</span></li><li><span style="font-size: medium;">Now alice first score is 70 which is less then the last value i.e 80 so his rank will be 4.</span></li><li><span style="font-size: medium;">For score=80 since it is same as last score so the rank will be 3.</span></li><li><span style="font-size: medium;">And last score is 105 since it is the greatest score so the rank will be 1st.</span><span style="font-size: medium;"> </span></li></ul><h2 style="text-align: left;"><span style="font-size: medium;">Climbing the Leaderboard Hackerrank Solution Approach:</span></h2><p><span style="font-size: medium;"> We will be using to steps in for this question.</span></p><ol style="text-align: left;"><li><span style="font-size: medium;">To Get the ranks of previous scores like the example given 1,2,2,3.</span></li><li><span style="font-size: medium;">To determine alice rank depending upon his scores.<br /> </span></li></ol><span style="font-size: medium;"></span><h2 style="text-align: left;"><span style="font-size: medium;">Climbing the Leaderboard Hackerrank Solution Step 1 (Setting the ranks for the scores):</span></h2></div><div><ul style="text-align: left;"><li><span style="font-size: medium;">In this step we will use an array called rank to store the rank of the scores.</span></li><li><span style="font-size: medium;">Rank of first score will be 1 followed by the consecutive ranks.</span></li><li><span style="font-size: medium;">Say scores=[80,60,45,45,30],and rank array rank=[].</span></li><li><span style="font-size: medium;">Now rank[0]=1,set the rank of score 80 as 1,now loop through the scores from 60 to 30 and give the ranks as per the scores.</span></li><li><span style="font-size: medium;">if the score are same then the rank will be same as the previous one,rank[1]=2 for 60.</span></li><li><span style="font-size: medium;">rank[2]=3 for 45,rank[3]=3 for 45 since score is same.</span></li><li><span style="font-size: medium;">rank[4]=4 for 30 </span></li></ul><span style="font-size: medium;">In this way our rank array will be created</span></div><div><span style="font-size: medium;"> </span></div><div><i><u><span style="font-size: medium;">Step 1 (Setting the ranks for the scores) code in java:</span></u></i></div><div><i><u><span style="font-size: medium;"> </span></u></i></div><div><span style="font-size: medium;"><span style="color: red;">int</span> rank[]=<span style="color: red;">new</span> int[n];<br /><br />rank[0]=1;<br /><span style="color: red;"><span> </span>for</span>(int i=1;i<n;i++)<br /><span> </span>{<br /> <span style="color: red;"><span> </span><span> </span>if</span>(scores[i]==scores[i-1])<br /> <span> </span><span> </span>{<br /> <span> </span><span> </span><span> </span>rank[i]=rank[i-1];<br /> <span> </span><span> </span>}<br /> <span style="color: red;"><span> </span><span> </span>else</span><br /> <span> </span><span> </span>{<br /> <span> </span><span> </span><span> </span>rank[i]=rank[i-1]+1;<br /> <span> </span><span> </span>}<br /><span> </span>} </span></div><p> <span style="font-size: medium;"><br /></span></p><h2 style="text-align: left;"><span style="font-size: medium;">Climbing the Leaderboard Hackerrank Solution Step 2 (Determine rank for alice scores): </span></h2><ul style="text-align: left;"><li><span style="font-size: medium;">Since their is array of scores for alice so we need to store the ranks in the array for alice scores as well for that we will create result array for alice that we are going to return to the calling function.</span></li><li><span style="font-size: medium;">We run two loops the outer loop will traverse through the alice scores and inner loop will find its rank.</span></li><li><span style="font-size: medium;">To find rank in the inner loop we traverse the scores from back to determine its appropriate position,once we found its appropriate position we give it the rank.</span></li></ul></div><p><span style="font-size: medium;">If you don't get it right now don't worry we will go through it by taking an sample test after the code.</span></p><p><span style="font-size: medium;"><br /></span></p><h2 style="text-align: left;"><span style="font-size: medium;">Climbing the Leaderboard Hackerrank Solution Java:</span></h2><p> <span style="font-size: medium;"><span style="color: red;">static</span> int[] climbingLeaderboard(int[] scores, int[] alice) {<br /><span style="color: red;"><span> </span></span></span></p><p><span style="font-size: medium;"><span style="color: red;"><span><span> </span> </span>int</span> n=scores.length;<br /><span style="color: red;"><span> </span>int</span> m=alice.length;<br /><span style="color: red;"><span> </span>int</span> rank[]=<span style="color: red;">new</span> int[n];<br /><br /></span><span style="font-size: medium;"><span> </span>rank[0]=1;<br /><span style="color: red;"><span> </span>for</span>(int i=1;i<n;i++)<br /></span><span style="font-size: medium;"><span> </span>{<br /> <span style="color: #2b00fe;"><span> </span><span> </span>i<span></span>f</span><span style="color: red;">(</span>scores[i]==scores[i-1])<br /> <span> </span></span><span style="font-size: medium;"><span> </span>{<br /> <span> </span></span><span style="font-size: medium;"><span> </span></span><span style="font-size: medium;"><span> </span>rank[i]=rank[i-1];<br /> <span> </span></span><span style="font-size: medium;"><span> </span>}<br /> <span style="color: red;"><span> </span><span> </span>else</span><br /> <span> </span></span><span style="font-size: medium;"><span> </span>{<br /> <span> </span></span><span style="font-size: medium;"><span> </span></span><span style="font-size: medium;"><span> </span>rank[i]=rank[i-1]+1;<br /> <span> </span></span><span style="font-size: medium;"><span> </span>}<br /></span><span style="font-size: medium;"><span> </span>}<br /><br /><span style="color: red;"><span> </span>int</span> a[]=new int[m];<br /><span style="color: red;"><span> </span>int</span> index=n-1;<br /><br /><span style="color: #2b00fe;"><span> </span>for</span>(int i=0;i<m;i++)<br /></span><span style="font-size: medium;"><span> </span>{<br /> <span style="color: #2b00fe;"><span> <span> </span></span>while</span>(index>=0 <span style="color: #cc0000;">&&</span> scores[index]<=alice[i])<br /> <span> </span></span><span style="font-size: medium;"><span> </span>{<br /> <span style="color: #cc0000;"><span> </span><span> </span><span> </span>index</span>--;<br /> <span> </span></span><span style="font-size: medium;"><span> </span>}<br /> <span style="color: #2b00fe;"><span> </span><span> </span>if</span>(index<0)a[i]=1;<br /> <span style="color: #2b00fe;"><span> </span><span> </span>else</span><br /> <span> </span></span><span style="font-size: medium;"><span> </span>{<br /> <span> </span></span><span style="font-size: medium;"><span> </span></span><span style="font-size: medium;"><span> </span>a[i]=rank[<span style="color: #cc0000;">index</span>]+1;<br /> <span> </span></span><span style="font-size: medium;"><span> </span>}<br /></span><span style="font-size: medium;"><span> </span>}<br /><span style="color: #2b00fe;">return</span> a;<br /> </span></p><p><span style="font-size: medium;">}</span></p><div><div><b><span style="font-size: medium;">Output:</span></b></div><div><span style="font-size: medium;"> </span></div><div><div class="separator" style="clear: both; text-align: center;"><span style="font-size: medium;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEghog6Uoq6GlaAJQP9NTUacgw0hcKkwhRGkJdj8534Wq-W6_mMU1BSkRDhStIxPTJfnOp7dCgH814Mlen5Lj2GLESFGTzWf0uY1amHtYhR9BPxszVa8Oyl4ojbdfRGSS-1YgFDJEvVh-AAU/s970/Screenshot+from+2021-02-11+19-19-51.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="Climbing the Leaderboard Hackerrank Solution |Codeityweb" border="0" data-original-height="473" data-original-width="970" height="312" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEghog6Uoq6GlaAJQP9NTUacgw0hcKkwhRGkJdj8534Wq-W6_mMU1BSkRDhStIxPTJfnOp7dCgH814Mlen5Lj2GLESFGTzWf0uY1amHtYhR9BPxszVa8Oyl4ojbdfRGSS-1YgFDJEvVh-AAU/w640-h312/Screenshot+from+2021-02-11+19-19-51.png" title="Climbing the Leaderboard Hackerrank Solution |Codeityweb" width="640" /></a></span></div> <span style="font-size: medium;"><br /><br /></span><h2 style="text-align: left;"><span style="font-size: medium;">Climbing the Leaderboard Hackerrank Solution Working and Explanation:</span></h2></div><div><ul style="text-align: left;"><li><span style="font-size: medium;"> Lets take the same test case as in the above output and try to understand the working of the code , scores=[100,100,50,40,40,20,10] , alice_score=[5,25,50,120]</span></li></ul><ul style="text-align: left;"><li><span style="font-size: medium;">First we create rank array and set rank[0]=1 as per the code so the rank_array will be rank=[1]</span></li><li><span style="font-size: medium;">Now we traverse through the scores array from i=1 i.e score 2 ,since we already gave rank as 1 for the first score.</span></li><li><span style="font-size: medium;">Here we check if the score is same is previous score then we assing the same rank to it, here at i=1 score[1]=100 which is same as previous one so we give same rank to it so rank array will be rank=[1,1]</span></li><li><span style="font-size: medium;">Now score[2]=50,since the score is not same is previous one so we increment the rank by one, so rank array will be rank=[1,1,2]</span></li><li><span style="font-size: medium;">similarly other ranks will be given score[3]=40,rank =[1,1,2,3].</span></li><li><span style="font-size: medium;">score[4]=40 rank=[1,1,2,3,3]</span></li><li><span style="font-size: medium;">score[5]=20 ,rank=[1,1,2,3,3,4]</span></li><li><span style="font-size: medium;">score[6]=10, rank=[1,1,2,3,3,4,5]</span></li></ul></div><div style="text-align: left;"><span style="font-size: medium;">This was the first step we have successfully ranked the scores now we need to rank the alice scores.</span></div><div style="text-align: left;"><ul style="text-align: left;"><li><span style="font-size: medium;">We traverse through alice scores [5,25,50,120. </span></li><li><span style="font-size: medium;">Now while the score is less than alice score we decrement the index. </span></li></ul></div><div style="text-align: left;"><span style="color: red;"><span style="font-size: medium;">Note :since the scores are also sorted we just need to assign the traversing index once to the last element,so we set index=n-1 .If the alice scores were not sorted we would assign the index=n-1 within the for loop instead to set the index to last element for proper position.</span></span></div><div style="text-align: left;"><ul style="text-align: left;"><li><span style="font-size: medium;">Once we found the proper position we assign the rank for that score same as the rank for previous scores.</span></li></ul></div><div style="text-align: left;"><span style="font-size: medium;">That is it its a simple question , if you find it difficult to understand read the approach and go through the solution again you will definitely.</span></div><div style="text-align: left;"><span style="font-size: medium;"><br /></span></div><div style="text-align: left;"><b><span style="font-size: medium;"> </span></b></div><div style="text-align: left;"><b><span style="font-size: medium;">Other Posts:</span></b></div><div style="text-align: left;"><ul style="text-align: left;"><li><h2><span style="font-size: medium;"><a href="https://codeityweb.blogspot.com/2021/02/sherlock-and-anagrams-hackerrank-solution.html" target="_blank">Sherlock and Anagrams Hackerrank Solution </a></span></h2></li><li><h2><span style="font-size: medium;"><a href="https://codeityweb.blogspot.com/2021/02/compare-triplets-hackerrank-solution.html" target="_blank">Compare The Triplets Hackerrank Solution </a> </span></h2></li><li><h2><span style="font-size: medium;"><a href="https://codeityweb.blogspot.com/2021/01/top-competitors-hackerrank-solutions.html" target="_blank">Top Competitors HackerRank Solution </a></span></h2></li><li><h2><span style="font-size: medium;"><a href="https://codeityweb.blogspot.com/2021/01/placements-hackerrank-solutions-sql.html" target="_blank">Placements HackerRank Solution</a> </span></h2></li><li><h2><span style="font-size: medium;"><a href="https://codeityweb.blogspot.com/2021/01/hackerrank-solutions.html" target="_blank">All Hackerrank Solutions at one place</a></span></h2><h2><span style="font-size: medium;"> </span></h2><h2><span style="font-size: medium;">Thank You For Reading <br /></span></h2></li></ul></div></div><div class="blogger-post-footer">Contact Us By Email Or Get in touch by Commenting on posts</div>CodeityWebhttp://www.blogger.com/profile/01377516264362368650noreply@blogger.com0tag:blogger.com,1999:blog-7236090768681897809.post-44899514796948648082021-02-07T11:59:00.000+05:302021-02-07T11:59:30.573+05:30 How to Create Random Password Generator In Python | Codeityweb<p><span style="font-size: medium;"><br /></span></p><h1 style="text-align: left;"><span style="font-size: medium;"><span style="font-size: x-large;">Creating Strong Random Password Generator In Python</span></span></h1><p><span style="font-size: medium;"><img height="167" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhi6v8fDkJjtXzvQCWhhicPUF_4t9xVTIkSy3YfqbK7dEzF4XGUUATbxLEamrj17BvLQrR0W5GVSvBtl52OibdwAW0v2AOneMksO55DpkQ6H8WF0f7VwJ6ilXTGzWW5jRQL2tOaavmsrFu9/w200-h167/randompasswordgenerator_code_python.png" style="display: none;" width="200" /><br />In this post we will create a Random Password Generator Script in Python <br /><br />For that we will use string and random module present in python<br /><br />So Without any further discussion lets create the script .<br /><br />First we will import the required modules, we don't have to install them they already come with python.<br /><br /> <br /><b>Code: </b><br /></span></p><p><span style="font-size: medium;"></span></p><blockquote><p><span style="font-size: medium;">-<br /></span></p><div style="text-align: left;"><b><span style="font-size: medium;">import string</span></b></div><div style="text-align: left;"><b><span style="font-size: medium;">import random</span></b></div><div style="text-align: left;"><b><span style="font-size: medium;"> </span></b></div><div style="text-align: left;"><b><span style="font-size: medium;">def generateRandomPassword(length):</span></b></div><div style="text-align: left;"><b><span style="font-size: medium;"> </span></b></div><div style="text-align: left;"><b><span style="font-size: medium;"> <span> </span>digits = string.digits</span></b></div><div style="text-align: left;"><b><span style="font-size: medium;"> <span> </span>letters = string.ascii_letters</span></b></div><div style="text-align: left;"><b><span style="font-size: medium;"> <span> </span>punctuation = string.punctuation</span></b></div><div style="text-align: left;"><b><span style="font-size: medium;"> <span> </span>samplepass = letters + digits + </span></b></div><div style="text-align: left;"><b><span style="font-size: medium;"><span> </span><span> </span><span> </span><span> </span><span> </span><span> </span><span> </span>punctuation + punctuation</span></b></div><div style="text-align: left;"><b><span style="font-size: medium;"> <span> </span>password = "".join(random.sample(samplepass, length))</span></b></div><div style="text-align: left;"><b><span style="font-size: medium;"> <span> </span>return password</span></b></div><div style="text-align: left;"><b><span style="font-size: medium;"><span> </span></span></b></div><div style="text-align: left;"><b><span style="font-size: medium;">if __name__ == "__main__":</span></b></div><div style="text-align: left;"><b><span style="font-size: medium;"> <span> </span>length = input("Enter the length of</span></b></div><div style="text-align: left;"><b><span style="font-size: medium;"><span> </span> the Password Required!\n")</span></b></div><div style="text-align: left;"><b><span style="font-size: medium;"> <span> </span>try:</span></b></div><div style="text-align: left;"><b><span style="font-size: medium;"> <span> </span><span> </span>length = int(length)</span></b></div><div style="text-align: left;"><b><span style="font-size: medium;"> <span> </span><span> </span>password = generateRandomPassword(length)</span></b></div><div style="text-align: left;"><b><span style="font-size: medium;"> <span> </span><span> </span>print("Password is : ", password)</span></b></div><div style="text-align: left;"><b><span style="font-size: medium;"> <span> </span>except:</span></b></div><div style="text-align: left;"><b><span style="font-size: medium;"> <span> </span><span> </span>print("Please Enter Numerical Value.")</span></b></div><div style="text-align: left;"><b><span style="font-size: medium;"> <span> </span><span> </span>exit(0)</span></b></div><p><span style="font-size: medium;"><br /><br />-</span></p></blockquote><p><span style="font-size: medium;"><br /> <br /> <br /> <br /><b>Output:</b><br /> <br /><br /><br /><a href="https://www.blogger.com/u/5/#"><img alt="Random Password generator python" border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi44LwvYRYmM6zcFgPM_clLLcPzf9ZN4c5cMKjnBfZFcqe1aGki99XqHKHDYop40U5ASL_Vh_w1zxXgdTnG-kbaY0FXXssLi7Sr7U448EZVu7p7G3PxGTET31Xe4KjsnJEzBcgNB7WV5prp/s16000/passgene.png" title="Length 20" /></a><br /><br /><br /><br /><a href="https://www.blogger.com/u/5/#"><img alt="Random password generator python" border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhg920i7PthbZah8Q2mLSj3PuJNcAbna3B3QnfwASxCAfFQ3MukHXf-606rEiOaqpyaAFrwuk6mIyrYSnIf8qG4T87j68iA1DFxxSBV5_WxETUdvY7tq4arcBxQ3uieCyZDjOazx4VFdlva/s16000/passgen1.png" title="length 15" /></a><br /><br /><br /><br /><a href="https://www.blogger.com/u/5/#"><img alt="Random password generator python" border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhA4Bo_WikWGgmDNBzZ3cF_jZOdZ7nsES3gjg-qvn5F0f6UTLlPOgtl_NZJb-anafBZiJK6Ca325NsztdJY8DeRxr2U2jjPe0lB-gM2b_rBnpShEcrq586gqztj3AjO0Cp8RfVr2r1j5OyG/s16000/passgen2.png" /></a><br /><br /><br /><br /><br /><b>Explanation:</b><br /> <b><br />string.digits</b> = 0123456789<br /> <br /><b>string.letters</b> = abcdefghijklmnopqrstuvwxyz<br /> ABCDEFGHIJKLMNOPQRSTUVWXYZ<br /> <br /><b>string.punctuation</b> = !"#$%&'()*+,-./: ;<=>?@[\]^_`{|}~<br /> <br />Note : Here we used punctuation 2 times just to increase the sample size and increase the chances of picking more punctuation marks in the password<br /> <br />We use <b>random.sample() </b>function to randomly select the characters from the sample space.</span></p><p><span style="font-size: medium;"> </span></p><p><span style="font-size: medium;"></span></p><p><b><span style="font-size: large;">Related Posts:</span></b></p><ul style="text-align: left;"><li><h2><span style="font-size: medium;"> <a href="https://codeityweb.blogspot.com/2021/01/secure-password-storage.html" target="_blank">Creating Your Own Best Password Manager</a></span></h2></li><li><h2><span style="font-size: medium;"><a href="https://codeityweb.blogspot.com/2021/01/password-validator.html" target="_blank">How to Create Password Validator </a> <br /> </span></h2></li></ul><div class="blogger-post-footer">Contact Us By Email Or Get in touch by Commenting on posts</div>CodeityWebhttp://www.blogger.com/profile/01377516264362368650noreply@blogger.com0tag:blogger.com,1999:blog-7236090768681897809.post-69811656767157361352021-02-07T11:58:00.003+05:302021-02-08T10:24:50.322+05:30Create Password Validator Python and Java | Codeityweb<h1 style="text-align: left;"><span style="font-size: x-large;">Password Validator Python and Java </span></h1><span style="font-size: medium;">We already have a Post on <a href="https://codeityweb.blogspot.com/2021/01/random-password-generator.html">Generating Random Password</a> which will generate strong random password for you.<br /><br />We also have a Post on Storing the Random Strong Password At <a href="https://codeityweb.blogspot.com/2021/01/secure-password-storage.html">Secure Password Manager</a>. <b>Post</b><br /><br />But Still if you want to have your own Password and want to check if it is Strong or not.For that we are going to create our Password Validator in python and java.<br /><br /></span><h2 style="text-align: left;"><span style="font-size: large;">Conditions for A strong Password:</span></h2><ul style="text-align: left;"><li><h2><span style="color: #38761d;"><span style="font-size: medium;">Their should be atleast one Upper Case Letter.</span></span></h2></li></ul><ul style="text-align: left;"><li><h2><span style="color: #38761d;"><span style="font-size: medium;">Atleast 2 numbers and 2 Special Characters.</span></span></h2></li></ul><ul style="text-align: left;"><li><h2><span style="color: #38761d;"><span style="font-size: medium;">Their should be atleast one Lower Case Letter.</span></span></h2></li></ul><ul style="text-align: left;"><li><h2><span style="color: #38761d;"><span style="font-size: medium;">Length of the Password Should be greater than 8 or atleast 8. </span></span></h2></li></ul><ul style="text-align: left;"><li><h2><span style="color: #38761d;"><span style="font-size: medium;">Whitespace should not be their.</span></span></h2></li></ul><span style="font-size: medium;"><br /><br />So without further discussion lets dive into the coding part:<br /><br /> <br /><br /></span><h2 style="text-align: left;"><span style="font-size: medium;">Password Validator Java:</span></h2><p><span style="font-size: medium;"><span style="color: red;">package</span> BlogSpot;<br /><br /><span style="color: red;">import</span> java.util.Scanner;<br /><br />public <span style="color: red;">class</span> PasswordValidator {<br /><br /> public <span style="color: red;">String</span> isValid(<span style="color: red;">String</span> password) {<br /><br /> <span style="color: red;">int</span> specialcount = 0;<br /> <span style="color: red;">int</span> digitcount = 0;<br /> <span style="color: red;">boolean</span> uppercase = false;<br /> <span style="color: red;">boolean</span> lowercase = false;<br /> <span style="color: red;">boolean</span> whitespace = false;<br /><br /> <span style="color: red;">if</span> (password.length() >= 8) {</span></p><p><span style="font-size: medium;"><br /> <span style="color: red;">for</span> (char i : password.<span style="color: #0b5394;">toCharArray</span>()) {<br /> <span style="color: red;"><span> </span>if</span> (Character.<span style="color: #0b5394;">isUpperCase</span>(i)) {<br /> <span> </span>uppercase = true;<br /> <span> </span>} <span style="color: red;">else</span> if (Character.<span style="color: #0b5394;">isLowerCase</span>(i)) {<br /> <span> </span>lowercase = true;<br /> <span> </span>} <span style="color: red;">else</span> if (Character.<span style="color: #0b5394;">isDigit</span>(i)) {<br /> <span> </span>digitcount++;<br /> <span> </span>} <span style="color: red;">else</span> if (Character.<span style="color: #0b5394;">isWhitespace</span>(i)) {<br /><span> </span> whitespace = true;<br /> <span> </span>} <span style="color: red;">else</span> {<br /> <span> </span>specialcount++;<br /> <span> </span>}<br /><br /> }<br /><br /> <span style="color: red;">if</span> (uppercase <span style="color: red;">&&</span> lowercase <span style="color: red;">&&</span> digitcount >= 2 <span style="color: red;">&&</span> !whitespace <span style="color: red;"> &&</span> specialcount >= 2) {<br /> <span style="color: red;"><span> </span>return</span> "Valid Password";<br /> }</span></p><p style="text-align: left;"><span style="font-size: medium;"> <span style="color: red;">else</span> if (!uppercase)<br /> <span style="color: red;"><span> </span>return</span> "Atleast One Upper Case Character is Needed";<br /> <span style="color: red;"><span> </span>else</span> if (!lowercase)<br /> <span style="color: red;"><span> </span>return</span> "Atleast One Lower Case Character is Needed";<br /> <span style="color: red;">else if</span> (digitcount < 2)<br /> <span style="color: red;"><span> </span>return</span> "Atleast 2 numbers are needed";<br /> <span style="color: red;">else if</span> (specialcount < 2)<br /> <span style="color: red;"><span> </span>return</span> "Atleast 2 special characters are needed";<br /> else <span style="color: red;">return</span> "White space is not allowed";<br /><br /> } else {<br /> <span style="color: red;"><span> </span>return</span> "Length should be Atleast 8";<br /> }<br /><br /> }<br /><br /> <span style="color: #351c75;">public</span> static <span style="color: #351c75;">void</span> main(<span style="color: #351c75;">String</span> args[]) {<br /> Scanner sc = <span style="color: red;">new</span> Scanner(System.in);<br /> System.out.println("Enter the Password");<br /> String password = sc.nextLine();<br /> String response = <span style="color: red;">new</span> PasswordValidator().isValid(password);<br /> System.out.println(response);<br /> <span style="color: red;">sc.</span>close();<br /><br /> }<br />}<br /><br /><br /><b>Output:</b><br /><br /><br /><a href="https://www.blogger.com/u/0/#"><img alt="Password Validator" border="0" height="60" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhyyhfWzP6HThl2T2wtz9VIhhIV3ru-3jHxVgJyoC4Fl_ABt2nadscV7_gdXCbRHg_WGebQt4a7Dq0m1PT_N6cPtG7P2VqrzHEE0cPn7ShA6LxwFdCMuHGv7pScnG1rie_LViL4TCpUnBPz/w400-h60/upperneeded_java.png" title="Password Validator" width="400" /></a><br /><br /><br /><br /><a href="https://www.blogger.com/u/0/#"><img alt="Password Validator" border="0" height="58" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi1GwO_zobO635kA_rVgx1fw20R0FvIyNhi9LQuyPFspEkd6KttQ003LbrS9RpStk92TJBr3FGffkCa_EAMzFCFa7a9FN5hhsbstpZZlPe2pT62zB4tTLb5B5M2J9VX8tORauqFICfil2Bw/w400-h58/specialneeded_java.png" title="Password Validator" width="400" /></a><br /><br /><br /><br /><a href="https://www.blogger.com/u/0/#"><img alt="Password Validator" border="0" height="91" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEimnJaAWmkOXujlSrDSy8f1eLDXvh-BqTV4N2Imd5EuJDn80KrzOWdlH0rFiImU6iW5xnGgpDAcTnfRMfENcSMpkJ5t7wI5eYoBM-e1jY30oyqGoJYF_ze1C0duotd65FHIieY-7Wl4knfe/w400-h91/validpass_java.png" title="Password Validator" width="400" /></a><br /><br /><br /><br /><a href="https://www.blogger.com/u/0/#"><img alt="Password Validator" border="0" height="66" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgZleQkcdCyoz-31BdhEt71XanLu5zcOS1DnQQq3yCM5KtB4zynegLfiORz6MgQplhZz5xbnm3bzLO2MyrKYk-f3Hzo4ogFlP4VyyQFPbW3BB_IyoC7ybTgyhZOcBs-TvE6oMmy6vk6x2h1/w400-h66/spacenotallowed_java.png" title="Password Validator" width="400" /></a><br /><br /><br /><br /><br /><br /><br /></span></p><h2 style="text-align: left;"><span style="font-size: medium;">Password Validator Code Python:</span></h2><p><span style="font-size: medium;"><br /><span style="color: #cc0000;">def</span> isValid(password):<br /> <span> </span>digitcount = 0<br /> <span> </span>specialcount = 0<br /> <span> </span>uppercase = False<br /> <span> </span>lowercase = False<br /><span> </span>whitespace = False<br /><br /> <span> </span><span style="color: #cc0000;">if</span> len(password) >= 8:<br /> <span> </span><span> </span><span style="color: #cc0000;">for</span> i in range(len(password)):<br /> <span> </span><span> </span><span> </span><span style="color: #cc0000;">if password</span>[i].isupper():<br /> <span> </span><span> </span><span> <span> </span></span>uppercase = True<br /> <span> </span><span> </span><span> </span><span style="color: #cc0000;">elif</span> password[i].islower():<br /> <span> </span><span> </span><span> </span><span> </span>lowercase = True<br /> <span> </span><span> </span><span> </span><span style="color: #cc0000;">elif</span> password[i].isdigit():<br /> <span> </span><span> </span><span> </span><span> </span>digitcount += 1<br /> <span> </span><span> </span><span> </span><span style="color: #cc0000;">elif</span> password[i] == " ":<span> </span><br /><span> </span><span> </span><span> </span><span> </span> whitespace = True<br /> <span> </span><span> </span><span> </span><span style="color: #cc0000;">else</span>:<br /> <span> </span><span> </span><span> </span><span> </span>specialcount += 1<br /><br /> <span> </span><span> </span><span style="color: #cc0000;">if</span> uppercase <span style="color: #cc0000;">and</span> lowercase <span style="color: #cc0000;">and</span> not whitespace <span style="color: #cc0000;">and</span> digitcount >= 2 <span> </span><span style="color: #cc0000;">and</span> specialcount >= 2:<br /> <span> <span> </span><span style="color: #cc0000;"></span></span><span style="color: #cc0000;">return</span> "Valid Password"<br /><br /> <span> <span> </span><span style="color: #cc0000;"></span></span><span style="color: #cc0000;">elif</span> not uppercase:<br /> <span> <span> </span><span> </span> </span><span style="color: #cc0000;">return</span> "Upper case Letter is needed"<br /><br /> <span> </span><span> </span><span style="color: #cc0000;">elif</span> not lowercase:<br /> <span> </span><span> </span><span> </span><span style="color: #cc0000;">return</span> "Lower case Letter is needed"<br /><br /> <span> </span><span> </span><span style="color: #cc0000;">elif</span> whitespace:<br /> <span> </span><span> </span><span> </span>return "White space is not allowed"<br /><br /> <span> </span><span> </span><span style="color: #cc0000;">elif</span> digitcount < 2:<br /> <span> </span><span> </span><span> </span><span style="color: #cc0000;">return</span> "Atleast 2 numbers are needed"<br /> <span> </span><span> </span><span style="color: #cc0000;">else</span>:<br /> <span> </span><span> </span><span> </span><span style="color: #cc0000;">return</span> "Atleast 2 special characters are needed"<br /><span> </span></span></p><p><span style="font-size: medium;"><span><span> </span></span><span style="color: #cc0000;">else</span>:<br /> <span> </span><span> </span><span></span><span style="color: #cc0000;">return</span> "Length should be atleast 8"<br /> </span></p><p><span style="font-size: medium;"><span style="color: #cc0000;">password</span> = input("Enter password :\n")<br />print(<span style="color: #cc0000;">isValid</span>(password))<br /><br /><br /><br /><b>Related posts:</b><br /><br /></span></p><ul style="text-align: left;"><li><span style="font-size: medium;"><a href="https://codeityweb.blogspot.com/2021/01/random-password-generator.html">Strong Random Password Generator</a></span></li></ul><ul style="text-align: left;"><li><span style="font-size: medium;"><a href="https://codeityweb.blogspot.com/2021/01/secure-password-storage.html">How Creating Own Password Manager Can Ease Your Pain</a> </span></li></ul><ul style="text-align: left;"><li><span style="font-size: medium;"><a href="https://codeityweb.blogspot.com/2021/01/secure-password-storage.html">Secure Password Manager</a></span></li></ul><p><span style="font-size: medium;"><br /><br /><br /><br /><b>Conclusion:</b><br /> <br />Thus We have Created our own Password Validator in python and java.<br /> </span></p><div class="blogger-post-footer">Contact Us By Email Or Get in touch by Commenting on posts</div>CodeityWebhttp://www.blogger.com/profile/01377516264362368650noreply@blogger.com0tag:blogger.com,1999:blog-7236090768681897809.post-87593049663399092052021-02-06T19:31:00.000+05:302021-02-06T19:31:19.968+05:30Sherlock and Anagrams Hackerrank Solution | Codeityweb<h1 style="text-align: left;"><span style="font-size: x-large;">Sherlock and Anagrams Hackerrank Solution by Codeityweb </span></h1><p><span style="font-size: medium;">In this post we are going to discuss the Sherlock and Anagrams Hackerrank Solution in java.</span></p><p><span style="font-size: medium;">Before looking at the problem statement and the solution lets see some terms that are needed for understanding the questions.</span></p><div style="text-align: left;"><h2 style="text-align: left;"><span style="font-size: medium;">1.What are Anagrams? </span></h2></div><div style="text-align: left;"><ul style="text-align: left;"><li><span style="font-size: medium;">Two strings are <b><i>anagrams</i></b> of each other if the letters of one string can be rearranged to form the other string.</span></li></ul></div><div style="text-align: left;"><ul style="text-align: left;"><li><span style="font-size: medium;">lets say we have two Strings a="codi" ,b="oicd".Here you can see that the letter number of characters in both the strings are same.</span></li><li><span style="font-size: medium;">a:{c-1,o-1,d-1,i-1},b:{c-1,o-1,d-1,i-1}. So here as you can see the characters interchanges their position but the length of the string and the sum of all characters are same thus both are anagrams of each other.</span></li><li><span style="font-size: medium;">Now lets say x="web",y="webe", x:{w-1,e-1,b-1},y:{w-1,e-2,b-1}.You can see that neither the length of the strings are same nor the total character count is same thus they are not anagram of each other.</span></li></ul></div><div style="text-align: left;"><span style="font-size: medium;"><br /></span></div><p><u><i><span style="font-size: medium;">The Problem statement is as follows:</span></i></u></p><p><span style="font-size: medium;"><br /> Given a string, find the number of pairs of substrings of the string that are anagrams of each other. </span></p><p><span style="font-size: medium;"> <br /></span></p><h2 style="text-align: left;"><span style="font-size: medium;">Approach and Algorithm for Sherlock and Anagrams Hackerrank Solution:</span></h2><ul style="text-align: left;"><li><span style="font-size: medium;">As the question asks to find the count of substrings that are anagrams of each others.</span></li><li><span style="font-size: medium;">So we need to perform two tasks 1) Get All Substrings of the String. 2)Check if the substring are anagram or not.</span></li><li><span style="font-size: medium;">To Find all substring we need to loop through the string and find the substrings using java's inbuild function substring(x,y) ,where x and y are indices of the character of the string.</span></li><li><span style="font-size: medium;">To Check if the strings are anagram of each other , as we see above that anagram means the count of the characters are same in both strings.</span></li><li><span style="font-size: medium;">So we need to count the frequency of each character in the strings ,and then check if both the count is same or not if it is same we return true else we return false.</span></li><li><span style="font-size: medium;">Finally we loop through the substrings and check if they are anagrams or not and increment count if they are anagrams.<br /></span></li></ul><h2 style="text-align: left;"><span style="font-size: medium;"> </span></h2><h2 style="text-align: left;"><span style="font-size: medium;">Check if Two String are anagrams in java:</span></h2><div><p><span style="font-size: medium;"><br /><br /><span style="color: red;">public</span> <span style="color: red;">static</span> boolean isanagram(<span style="color: red;">String</span> a,<span style="color: red;">String</span> b)<br />{<br /> <span style="color: red;"><span> </span>if</span>(a.length()!=b.length())<span style="color: red;">return</span> false;<br /> <span> </span>int[] freq=<span style="color: red;">new</span> int[26];<br /> <span style="color: red;"><span> </span>for</span>(int i=0;i<a.length();i++)<br /> <span> </span>{<br /> <span> <span> </span> </span>freq[a.charAt(i)-'a<span style="color: red;">']++;</span><br /> <span> <span> </span> </span>freq[b.charAt(i)-'a']--;<br /> <span><span> </span> </span>}<br /> <span style="color: red;"><span> </span>for</span>(int i:freq)<br /> <span> </span>{<br /> <span> </span><span> </span>if(i!=0)<span style="color: red;">return</span> false;<br /> <span> </span>}<br /> <span style="color: red;"><span> </span>return</span> true;<br />}</span></p><p><span style="font-size: medium;"><br /></span></p><h2 style="text-align: left;"><span style="font-size: medium;">Sherlock and Anagrams Hackerrank Solution Java:</span></h2><p> <span style="font-size: medium;"><span style="color: red;">static</span> <span style="color: red;">int</span> sherlockAndAnagrams(<span style="color: red;">String</span> s) {<br /> <span style="color: red;"><span> </span>int</span> count=0;<br /> <span> </span>ArrayList<<span style="color: red;">String</span>> al=<span style="color: red;">new</span> ArrayList<>();<br /> <span style="color: red;"><span> </span>for</span>(int i=0;i<s.length();i++)<br /><span> </span> {<br /> <span style="color: red;"><span> </span><span> </span>for</span>(int j=i+1;j<=s.length();j++)<br /> <span> </span><span> </span>{<br /> <span> </span><span> </span>al.add(s.<span style="color: red;">substring</span>(i,j));<br /> <span> </span><span> </span>}<br /> <span> </span><span> </span>}<br /> <span style="color: red;"><span> </span>for</span>(int i=0;i<al.size();i++)<br /> <span> </span>{<br /> <span style="color: red;"><span> <span> </span></span>for</span>(int j=i+1;j<al.size();j++)<br /> <span> </span><span> </span>{<br /> <span> </span><span> </span>if(<span style="color: red;">isanagram</span>(al.get(i),al.get(j)))<br /> <span> </span><span> </span>{<br /> <span> </span><span> </span><span> </span>count++;<br /> <span> </span><span> </span><span> </span>}<br /> <span> </span><span> </span>}<br /> <span> </span>}<br /><br /><span style="color: red;"><span> </span><span> </span>return</span> count;</span></p><p><span style="font-size: medium;">}</span></p><p><b><span style="font-size: medium;"> Output:</span></b></p><div class="separator" style="clear: both; text-align: center;"><span style="font-size: medium;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjOYj9Rj50GqUoiK3tRWDva7YUuxFmH-9CTEpBZy9D5WiksDSarwj1A4YlyRCq6G004_qBXqUNqVOO0VTUkRq4cLaXDIZuOt98-jGZCp9mlPr2dLU2mDXkvhgbzrqFAjVT6POpM8BU3AKai/s1111/sherlockandanagram.png" style="margin-left: 1em; margin-right: 1em;"><img alt="Sherlock and Anagrams Hackerrank Solution" border="0" data-original-height="633" data-original-width="1111" height="364" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjOYj9Rj50GqUoiK3tRWDva7YUuxFmH-9CTEpBZy9D5WiksDSarwj1A4YlyRCq6G004_qBXqUNqVOO0VTUkRq4cLaXDIZuOt98-jGZCp9mlPr2dLU2mDXkvhgbzrqFAjVT6POpM8BU3AKai/w640-h364/sherlockandanagram.png" title="Sherlock and Anagrams Hackerrank Solution | Codeityweb" width="640" /></a></span></div><span style="font-size: medium;"><br /></span><p><b><span style="font-size: medium;"> </span></b></p><p><b><span style="font-size: medium;">Related Posts:</span></b></p><ul style="text-align: left;"><li><span style="font-size: medium;"><a href="https://codeityweb.blogspot.com/2021/01/heres-quick-way-to-solve-pairs.html" target="_blank">Pairs Hackerrank Solution</a> </span></li><li><span style="font-size: medium;"><a href="https://codeityweb.blogspot.com/2020/10/making-two-strings-anagrams.html" target="_blank">Making Anagrams -Hackerrank Solutions </a> </span></li><li><span style="font-size: medium;"><a href="https://codeityweb.blogspot.com/2020/10/birthday-cake-candles.html" target="_blank">Birthday Cake Candles Hackerrank Solution </a> </span></li><li><span style="font-size: medium;"><a href="https://codeityweb.blogspot.com/p/index.html" target="_blank">All Solutions by Codeityweb</a> </span></li></ul><span style="font-size: medium;"> </span></div><div><b><span style="font-size: medium;">Thank you for Reading!</span></b></div><div><span style="font-size: medium;">If some questions are missing you can get in touch by using contact form or by commenting on the posts.</span></div><div><span style="font-size: medium;"> </span><br /></div><div class="blogger-post-footer">Contact Us By Email Or Get in touch by Commenting on posts</div>CodeityWebhttp://www.blogger.com/profile/01377516264362368650noreply@blogger.com0tag:blogger.com,1999:blog-7236090768681897809.post-54535989895723556612021-02-01T16:00:00.000+05:302021-02-01T16:00:14.979+05:30Compare The Triplets Hackerrank Solution |Codeityweb<div><p></p><h1 style="text-align: left;"><span style="font-size: large;">Compare The Triplets Hackerrank Solution |Codeityweb</span></h1><div style="text-align: left;"><span style="font-size: medium;"><span style="color: #38761d;">In this Post we are going to discuss the</span></span></div><div style="text-align: left;"><span style="font-size: medium;"><span style="color: #38761d;"><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEitrAb64IX70LIxqLUOukOEvoupQellj3UhZv2Rgq1JPA2IAiNpOa4hdV-Ftc9RIsy1t3eunawXTuMMoNkb1AUqqVxKOM1HPj9-tuHFkyxtYAysrz0qhfhxvLHVn36O8xIrdTF9rYt-0sAr/s285/Screenshot+from+2021-02-01+15-31-13.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img style="display:none"alt="Compare The Triplets Hackerrank Solution |Codeityweb" border="0" data-original-height="270" data-original-width="285" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEitrAb64IX70LIxqLUOukOEvoupQellj3UhZv2Rgq1JPA2IAiNpOa4hdV-Ftc9RIsy1t3eunawXTuMMoNkb1AUqqVxKOM1HPj9-tuHFkyxtYAysrz0qhfhxvLHVn36O8xIrdTF9rYt-0sAr/s16000/Screenshot+from+2021-02-01+15-31-13.png" title="Compare The Triplets Hackerrank Solution |Codeityweb" /></a></div><br /> </span></span></div><h2 style="text-align: left;"><span style="font-size: medium;"><span style="color: #38761d;">Compare The Triplets Hackerrank Solution in Java and python </span><br /></span></h2><p><span style="font-size: medium;"><i><u>Problem Statement</u></i>:</span></p><p><span style="font-size: medium;">Two Programmers Alice and Bob Created a problem for hackerrank and they got ratings after reviewing their problems .</span></p><p><span style="font-size: medium;">Alice Rating are stored in suppose array A and Bob rating is stored in say array B.</span></p><p><span style="font-size: medium;"><b>Task:</b> </span></p><ul style="text-align: left;"><li><span style="font-size: medium;">Know the Task is to check who got more awards and awards will be calculated by checking who got more number for rating in more number of review.</span></li><li><span style="font-size: medium;">That is if Alice got [3,2,4] rating and Bob got [2,2,1] rating , then we compare the rating at individual index like 3>2 so Alice got 1 Award , 2==2 So no award will be given and 4>1 so again Alice will get 1 Award.</span></li><li><span style="font-size: medium;">So total Alice has 2 Awards and Bob Has 0 award .</span></li><li><span style="font-size: medium;">Then we need to return the answer to the calling function.</span></li><li><span style="font-size: medium;">That is it, its a simple question .</span></li></ul></div><h2 style="text-align: left;"><span style="font-size: medium;"><br /></span></h2><div><h2 style="text-align: left;"><span style="font-size: large;"> Compare The Triplets Hackerrank Solution Python:</span></h2><span style="font-size: medium;"><span style="color: #cc0000;">def</span> compareTriplets(a, b):<br /> <span> </span>solution=[]<br /> <span> </span>a_awarded=0<br /> <span> </span>b_awarded=0<br /> <span> </span><span style="color: #cc0000;">for</span> i <span style="color: #cc0000;">in</span> <span style="color: #cc0000;">range</span>(<span style="color: #cc0000;">len</span>(a)):<br /> <span> </span><span> </span><span style="color: #cc0000;">if</span> a[i]>b[i]:<br /> <span> </span><span> </span><span> </span>a_awarded+=1<br /> <span> </span><span> </span><span style="color: #cc0000;">elif</span> b[i]>a[i]:<span> </span><span></span><br /> <span> </span><span> </span><span> </span>b_awarded+=1<br /> <span> </span><span></span>solution.append(a_awarded)<br /> <span> </span>solution.append(b_awarded)<br /></span> <span style="font-size: medium;"><br /> <span> </span><span style="color: #cc0000;">return</span> solution</span></div><div><span style="font-size: medium;"> </span></div><div><span style="font-size: medium;"> </span></div><div><b><span style="font-size: medium;">Explanation:</span></b></div><div><ul style="text-align: left;"><li><span style="font-size: medium;">As discussed above we need to compare the ratings of Alice and Bob so we run a loop to traverse through the list.</span></li><li><span style="font-size: medium;">We compare the rating of alice with bob and if it is greater we increment the award counter of alice by 1.</span></li><li><span style="font-size: medium;">If Bob has better rating we increment the bob's award counter by 1.</span></li><li><span style="font-size: medium;">We create a list called solution to store the results and then we append alice score and bob's score in the solution list.</span></li><li><span style="font-size: medium;"> Finally we return the list to the calling function.</span></li></ul><h2 style="text-align: left;"><span style="font-size: large;"> </span></h2></div><div style="text-align: left;"><h2><span style="font-size: large;">Compare The Triplets Hackerrank Solution Java:</span></h2></div><p> <span style="font-size: medium;"><br /></span> <span style="font-size: medium;"><span style="color: #cc0000;">static</span> List<Integer> compareTriplets(<span style="color: #cc0000;">List</span><Integer> a, <span style="color: #cc0000;">List</span><Integer> b) {<br /> </span></p><p><span style="font-size: medium;"><span style="color: #cc0000;">List</span><Integer> l=new ArrayList<>();<br /><span style="color: #cc0000;">int</span> av=0;<br /><span style="color: #cc0000;">int</span> bv=0;<br /><br /><span style="color: #cc0000;">for</span>(int i=0;i<a.size();i++)<br />{<br /> <span> </span><span style="color: #cc0000;">if</span>(a.get(i<span style="color: #cc0000;">)></span>b<span style="color: #cc0000;">.</span>get(i))<br /> <span> </span>{<br /> <span> <span> </span> </span>av++;<br /> <span> </span>}<br /> <span> </span><span style="color: #cc0000;">else</span> <span style="color: #cc0000;">if</span>(a.get(i)<<span style="color: #cc0000;">b</span>.get(i))<br /> <span> </span>{<br /> <span> <span> </span> </span>bv++;<br /> <span> </span>}<br />}</span></p><p><span style="font-size: medium;"><br />l.add(av);<br />l.add(bv);<br /><span style="color: #cc0000;">return</span> l;</span></p><p><span style="font-size: medium;"><br /> } </span></p><p><b><span style="font-size: medium;">Explanation:</span></b></p><ul style="text-align: left;"><li><span style="font-size: medium;">The explanation is same as the above ,just here we have provide solution in java .</span></li><li><span style="font-size: medium;">we have used Arraylist as a list to return in scores ,rest is same as the above explanation.</span></li></ul><p><span style="font-size: medium;"> </span></p><p><b><span style="font-size: medium;">Output:</span></b></p><p></p><div class="separator" style="clear: both; text-align: center;"><span style="font-size: medium;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEilMMK4nW_l0efAvrKG0ObS5nqX-kU_6EiRtJwW3FSHhe9p6tQN2T_UWjAJA2BZPqKqQv2jzy6O_2eyRFw_HlAGMBa8Fru4SMsDw2NWcOz9PBgDG80gJLOme37y8t41haqMyN6S3UOnTnI2/s931/Screenshot+from+2021-02-01+15-27-55.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="Compare The Triplets Hackerrank Solution |Codeityweb" border="0" data-original-height="602" data-original-width="931" height="414" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEilMMK4nW_l0efAvrKG0ObS5nqX-kU_6EiRtJwW3FSHhe9p6tQN2T_UWjAJA2BZPqKqQv2jzy6O_2eyRFw_HlAGMBa8Fru4SMsDw2NWcOz9PBgDG80gJLOme37y8t41haqMyN6S3UOnTnI2/w640-h414/Screenshot+from+2021-02-01+15-27-55.png" title="Compare The Triplets Hackerrank Solution |Codeityweb" width="640" /></a></span></div><span style="font-size: medium;"><br /> </span><p></p><p><b><span style="font-size: medium;">Related Posts:</span></b></p><ul style="text-align: left;"><li><h2><span style="font-size: medium;"><a href="https://codeityweb.blogspot.com/2021/02/a-very-big-sum-hackerrank-solution.html" target="_blank">A Very Big Sum Hackerrank Solution</a></span></h2></li><li><h2><span style="font-size: medium;"><a href="https://codeityweb.blogspot.com/2021/01/hackerrank-solutions.html" target="_blank">All Hackerank Solutions</a> </span></h2></li><li><h2><span style="font-size: medium;"><a href="https://codeityweb.blogspot.com/2020/10/diagonal-difference.html" target="_blank">Diagonal Difference Hackerrank Solution</a></span></h2></li></ul><p><b><span style="font-size: medium;">Conclusion:</span></b></p><p><span style="font-size: medium;">You can get this solutions with rest of other solutions in the github repository as well <a href="https://github.com/codeityweb/hackerrank-solutions" target="_blank">Here.</a>We have hackerrank solutions on different topics and projects.</span></p><p><span style="color: #cc0000;"><span style="font-size: medium;">Do Explore the site to get more contents like this.</span></span></p><p><span style="font-size: medium;"><b>Thank you for reading! </b><br /></span></p><div class="blogger-post-footer">Contact Us By Email Or Get in touch by Commenting on posts</div>CodeityWebhttp://www.blogger.com/profile/01377516264362368650noreply@blogger.com0tag:blogger.com,1999:blog-7236090768681897809.post-78086764962497506062021-02-01T15:17:00.003+05:302021-02-01T15:17:32.831+05:30A Very Big Sum Hackerrank Solution |Java,Python| Codeityweb<div><div><h1 style="text-align: left;"><span style="font-size: medium;"> <span style="font-size: large;">A Very Big Sum Hackerrank Solution in java and python | Codeityweb</span></span></h1><p><b><span style="font-size: medium;">Problem Description:</span></b></p><p><span style="font-size: medium;">You are asked to calculate and print the sum of the elements in an array, but the elements in the array may be quite huge and it cannot be stored in int value.</span></p><div style="text-align: left;"><span style="font-weight: normal;"><span style="font-size: medium;">In this post we are going to discuss</span></span></div><div style="text-align: left;"><span style="font-weight: normal;"><span style="font-size: medium;"><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhsFwcsZr9pD-l4KpXFaLtfca-Cje09UEnu6LDrqqFQFyShk-QXM5Uq6XDkBoaEWcSTLYY8Zxd2NvC4IeNuZOV8j2_b0yWBZcG03NmbwUFzfqOZ_QT-pxlRinGPyE_MtvNWl0pDVtTMEbz4/s498/Screenshot+from+2021-02-01+14-45-50.png" style="margin-left: 1em; margin-right: 1em;"><img alt="A Very Big Sum Hackerrank Solution |Java| Codeityweb" border="0" data-original-height="201" data-original-width="498" height="129" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhsFwcsZr9pD-l4KpXFaLtfca-Cje09UEnu6LDrqqFQFyShk-QXM5Uq6XDkBoaEWcSTLYY8Zxd2NvC4IeNuZOV8j2_b0yWBZcG03NmbwUFzfqOZ_QT-pxlRinGPyE_MtvNWl0pDVtTMEbz4/w320-h129/Screenshot+from+2021-02-01+14-45-50.png" style="display: none;" title="A Very Big Sum Hackerrank Solution |Java| Codeityweb" width="320" /></a></div></span></span></div><h2 style="text-align: left;"><span style="color: #38761d;"><span style="font-size: medium;">A very big sum hackerrank solution in java with code and also in python.</span></span></h2><p><span style="font-size: medium;">It is not a difficult question but as a point of warmup exercise we are discussing the question here.<br /></span></p><p><span style="font-size: medium;"><span style="color: #38761d;"><b>Inputs can be like this:</b></span><br /></span></p><p><span style="font-size: medium;">1000000001, 1000000002 ,1000000003, 1000000004, 1000000005</span></p><p><span style="font-size: medium;"> </span></p><h2 style="text-align: left;"><span style="font-size: medium;">A Very Big Sum Hackerrank Solution Java:</span></h2><p> <span style="font-size: medium;"><span style="color: #cc0000;">static</span> <span style="color: #cc0000;">long</span> aVeryBigSum(<span style="color: #cc0000;">long</span>[] ar) {<br /></span> <span style="font-size: medium;"><span style="color: #cc0000;">long</span> sum=0;<br /></span> <span style="font-size: medium;"><span style="color: #cc0000;">for</span>(<span style="color: #cc0000;">long</span> i:ar)<br /> {<br /> sum+=i;<br /> }<br /></span> <span style="font-size: medium;"><span style="color: #cc0000;">return</span> sum;<br /> }</span><span style="font-size: medium;"> </span></p><h3 style="text-align: left;"><span style="font-size: medium;">Explanation:</span></h3><ul style="text-align: left;"><li><span style="font-size: medium;">The Sum is variable is used to store the values on the sum of the array elements.</span></li><li><span style="font-size: medium;">We run a loop to traverse the elements in the array and add them to the sum variable .</span></li><li><span style="font-size: medium;">Note that we cannot use int here because the sum will exceed the storing capacity of the int data type which is 2^31 -1.</span></li></ul><h2 style="text-align: left;"><span style="font-size: medium;"> </span></h2><h2 style="text-align: left;"><span style="font-size: medium;">A Very Big Sum Hackerrank Solution Python:</span></h2><p><span style="font-size: medium;"> </span></p><p><span style="font-size: medium;"><span style="color: red;">def</span> aVeryBigSum(ar):<br /> <span> </span>sum=0;<br /> <span> </span><span style="color: red;">for</span> i in range(<span style="color: red;">len</span>(ar)):<br /> <span> </span></span><span style="font-size: medium;"><span> </span>sum<span style="color: red;">+=</span>ar[i]<br /> <span> </span></span><span style="font-size: medium;"><span> </span><span style="color: red;">return</span> sum</span></p><p><span style="font-size: medium;"> </span></p><h3 style="text-align: left;"><span style="font-size: medium;">Explanation:</span></h3><ul style="text-align: left;"><li><span style="font-size: medium;">Similar to above one we store the sum of array element in the sum variable just the case is that we don't need to worry about the storing capacity of variable.</span></li><li><span style="font-size: medium;">we traverse through the array and calculate the sum and finally return the sum to the calling function.</span></li></ul><span style="font-size: medium;"> </span></div><div><span style="font-size: medium;">Output:</span></div><div><span style="font-size: medium;"> </span></div><div><span style="font-size: medium;"><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgjNJ4sR7wfaFJ1NJ1YmQukf0ac8znPwFbnbxM9cqBdxwZyqtDkGQatPvMKRuLEwitfKq_zgrCNHZbX4fS0WsVUnO6BgUXVbO5CiZsv5lvQt4NYgw7_guagn5knoBk3Bs06xGiv_MF2nqDk/s942/Screenshot+from+2021-02-01+14-44-23.png" style="margin-left: 1em; margin-right: 1em;"><img alt="A Very Big Sum Hackerrank Solution |Python| Codeityweb" border="0" data-original-height="461" data-original-width="942" height="314" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgjNJ4sR7wfaFJ1NJ1YmQukf0ac8znPwFbnbxM9cqBdxwZyqtDkGQatPvMKRuLEwitfKq_zgrCNHZbX4fS0WsVUnO6BgUXVbO5CiZsv5lvQt4NYgw7_guagn5knoBk3Bs06xGiv_MF2nqDk/w640-h314/Screenshot+from+2021-02-01+14-44-23.png" title="A Very Big Sum Hackerrank Solution |Python| Codeityweb" width="640" /></a></div><br /> <br /></span></div><h3 style="text-align: left;"><span style="font-size: medium;">Related Posts:</span></h3><ul style="text-align: left;"><li><h2><span style="font-size: medium;"><a href="https://codeityweb.blogspot.com/2020/10/diagonal-difference.html" target="_blank">Diagonal Difference Hackerrank solution</a></span></h2></li><li><h2><span style="font-size: medium;"><a href="https://codeityweb.blogspot.com/2021/01/hackerrank-solutions.html" target="_blank">All Hackerrank solution </a> </span></h2></li></ul></div><p><span style="font-size: medium;"> </span></p><p><span style="font-size: medium;">We also have a github repository where you could find solutions for other topics as well.<a href="https://github.com/codeityweb/hackerrank-solutions" target="_blank">Click Here</a></span></p><p><span style="font-size: medium;"><br /></span></p><p><span style="font-size: medium;">Do explore the site for more solutions and along with explanation😀.</span></p><p><span style="color: #0b5394;"><span style="font-size: medium;">If you are a blogger and want to submit a blog post you can do that by contacting us through the contact from provided in the site .</span></span></p><p><span style="font-size: medium;"><b>Thank You for Reading!</b><br /></span></p><div><p><span style="font-size: medium;"><br /></span></p><div><p> </p></div></div><div class="blogger-post-footer">Contact Us By Email Or Get in touch by Commenting on posts</div>CodeityWebhttp://www.blogger.com/profile/01377516264362368650noreply@blogger.com0tag:blogger.com,1999:blog-7236090768681897809.post-65598677829712205482021-01-31T15:43:00.003+05:302021-01-31T15:45:28.499+05:30The Report HackerRank Solution | Joins | Codeityweb<div><div><div><h1 style="text-align: left;"><span style="font-size: medium;"> <span style="font-size: x-large;">The Report - SQL | Joins | HackerRank Solution </span></span></h1><p style="text-align: left;"><span style="font-size: medium;">In this post we are going to discuss The Report Hackerrank Solution using Joins.</span></p><p><span style="font-size: medium;"><i><u>So the Question is as follows:</u></i><br />You are given two tables: <b>Students</b> and <b>Grades</b>. Students contains three columns <span style="color: #134f5c;">ID</span>, <span style="color: #134f5c;">Name</span> and <span style="color: #134f5c;">Marks</span>.</span></p><p><span style="font-size: medium;">Grades contains the following data: </span></p><p><b><span style="font-size: medium;">Task:</span></b></p><ul style="text-align: left;"><li><span style="font-size: medium;">The task is to generate a report containing three columns,Name,Grade,Mark.</span></li><li><span style="font-size: medium;">Student who got grades <span style="color: #38761d;"><b>less than 8 </b></span>should not be included.</span></li><li><span style="font-size: medium;">The grades should be in descending order and if grades are same then arrange them as per their Names.</span></li><li><span style="font-size: medium;">Use <span style="color: red;">NULL</span> as "<span style="color: red;">Name</span>" for students who got Grades <span style="color: red;">less then 8</span> .</span></li></ul><p><span style="font-size: medium;"> </span></p><span style="font-size: medium;"><b>Students Table Schema:</b><br /></span><div><span style="font-size: medium;"> <br /></span><div class="separator" style="clear: both; text-align: center;"><span style="font-size: medium;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjrsAdjXQoK60Bm6040rDj_j17ss45UxTU6OU0fVSJRmXqkQfjUWJSUheZUSaHC4ObJsspXgRyZFpiilr33DgByvkJS1HaNRmVB7h86nSIOXgC8FWBLrfL3ZxoEWVg_X3v3ZGy-Qg87uz7l/s313/ThereportStudents.png" style="margin-left: 1em; margin-right: 1em;"><img alt="The Report hackerrank solution" border="0" data-original-height="181" data-original-width="313" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjrsAdjXQoK60Bm6040rDj_j17ss45UxTU6OU0fVSJRmXqkQfjUWJSUheZUSaHC4ObJsspXgRyZFpiilr33DgByvkJS1HaNRmVB7h86nSIOXgC8FWBLrfL3ZxoEWVg_X3v3ZGy-Qg87uz7l/s16000/ThereportStudents.png" title="Students Table" /> </a></span></div><div class="separator" style="clear: both; text-align: center;"><span style="font-size: medium;"> </span></div><b><span style="font-size: medium;">Grades Table Schema:</span></b></div><div><span style="font-size: medium;"> </span></div><div><span style="font-size: medium;"> </span></div><div class="separator" style="clear: both; text-align: center;"><span style="font-size: medium;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEha1iXMXdiGZMuBPb_0oE6clqjOEojazOqF_Udk7CZPlssyevLni5xDxuVrK7HGfMQka3EzdJUazybd5z50-BosplrqnCYs7omc-cSkzDvx7bUepB-93qEIft23B_II0TuHhUwjqy4iHRtE/s470/thereport_Grades.png" style="margin-left: 1em; margin-right: 1em;"><img alt="The report hackerrank solution" border="0" data-original-height="470" data-original-width="317" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEha1iXMXdiGZMuBPb_0oE6clqjOEojazOqF_Udk7CZPlssyevLni5xDxuVrK7HGfMQka3EzdJUazybd5z50-BosplrqnCYs7omc-cSkzDvx7bUepB-93qEIft23B_II0TuHhUwjqy4iHRtE/s16000/thereport_Grades.png" title="Grades Table" /></a></span></div><p><span style="font-size: medium;"><br /></span></p><p><span style="font-size: medium;"><br /></span></p><h4 style="text-align: left;"><b><span style="font-size: medium;">Understanding the Tables And Task:</span></b></h4><ul style="text-align: left;"><li><span style="font-size: medium;">The Student table contains information like the <span style="color: #0b5394;">Name</span>,<span style="color: #0b5394;">Marks</span> obtained and Id,whereas the <span style="color: #0b5394;">Grades</span> table <span style="color: #0b5394;">contains</span> the <span style="color: #0b5394;">Grade</span> allocated for the respective range of marks.</span></li><li><span style="font-size: medium;">The task is to display the names of the students in <span style="color: #cc0000;">decreasing order</span> of their grades.</span></li><li><span style="font-size: medium;">As You can see the Grades and Marks are in two different tables so it should be clicked that we must use joins or something else to combine the columns of the tables in order to fetch the results.</span></li><li><span style="font-size: medium;">So we will use joins to combine the tables and then order them as per their grades.</span></li><li><span style="font-size: medium;">That is it, so lets start.</span></li></ul></div><p><span style="font-size: medium;"><br /></span></p><p><span style="color: #b45f06;"><span style="font-size: medium;">We will use 2 ways to solve the query, nothing much is different in the query but the first one is the solution that we will get easily by thinking and the latter is more elegant way to solve the question.</span></span></p><p><span style="font-size: medium;"><br /></span></p><h2 style="text-align: left;"><b><span style="font-size: medium;">The Report Hackerrank Solution 1:</span></b></h2><p><span style="font-size: medium;"><span style="color: red;">select</span> if(g.Grade<8,"<span style="color: #0b5394;">NULL</span>",s.Name),g.Grade,s.Marks from Students s<br /><span style="color: red;">join</span> Grades g <br /><span style="color: red;">on</span> s.Marks<=g.Max_Mark and s.Marks>=g.Min_Mark <br /><span style="color: red;">order</span> <span style="color: red;">by</span> g.Grade <span style="color: #0b5394;">desc</span>,s.Name </span></p><p><span style="font-size: medium;"> </span></p><h2 style="text-align: left;"><b><span style="font-size: medium;">Explaination:</span></b></h2><ul style="text-align: left;"><li><span style="font-size: medium;">As we need to fetch the Names of students who got grades more than 8 and NULL for rest of the students so we use IF condition to check that the Grade is greater than 8 or not if it is greater than we display the Name else we display NULL.</span></li><li><span style="font-size: medium;">Rest we fetch the Grade from Grades table and Marks from Students table.</span></li><li><span style="font-size: medium;">we join both the tables and since their is no common column in them but we can join them on the basis of the Range of the Marks, so we join them on that basis.</span></li><li><span style="font-size: medium;">Finally we combine the result and order them by their grades in decreasing order and if it is same we order them by the Names.</span></li></ul></div><p><span style="font-size: medium;"><br /></span></p><p><span style="font-size: medium;"> </span></p><h2 style="text-align: left;"><span style="font-size: medium;">The Report Hackerrank Solution 2:</span></h2><p><span style="font-size: medium;"><span style="color: red;">select</span> if(Grade<8,"NULL",Name),Grade,Marks <br /><span style="color: red;">from</span> Students <br /><span style="color: red;">join</span> Grades <br /><span style="color: red;">on</span> Marks <span style="color: #0b5394;">between</span> Min_Mark <span style="color: #0b5394;">and</span> Max_Mark <br /><span style="color: red;">order</span> <span style="color: red;">by</span> Grade desc,Name</span></p><p><span style="font-size: medium;"><br /></span></p><h2 style="text-align: left;"><b><span style="font-size: medium;">Explaination:</span></b></h2><ul style="text-align: left;"><li><span style="font-size: medium;">We use the same if condition to select the Name or NULL but in this case we didn't use Alias .</span></li><li><span style="font-size: medium;">We join the tables and this time use use between to join them ,where as we used greater than (>=) and less than(<=) in previous solution.</span></li><li><span style="font-size: medium;">Finally we order them by the Grades.</span></li></ul></div><p><span style="font-size: medium;"><br /></span></p><p><b><span style="font-size: medium;">Output:</span></b></p><div class="separator" style="clear: both; text-align: center;"><span style="font-size: medium;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg_3EIW8XNu7lGpMKbS98gUwYqx2IoI67Ec73igK-lcRQ2BsgqiF3vI0d7Gra1jBpWTVNqRaRHPlSiWPO1_YXGm85XBEKMBQl9VWxTrD7kja074gkpBmfh0oW1fbM7R7UppeJu7-zxdmnGE/s1043/Screenshot+from+2021-01-25+18-28-07.png" style="margin-left: 1em; margin-right: 1em;"><img alt="The report hackerrank solution" border="0" data-original-height="652" data-original-width="1043" height="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg_3EIW8XNu7lGpMKbS98gUwYqx2IoI67Ec73igK-lcRQ2BsgqiF3vI0d7Gra1jBpWTVNqRaRHPlSiWPO1_YXGm85XBEKMBQl9VWxTrD7kja074gkpBmfh0oW1fbM7R7UppeJu7-zxdmnGE/w640-h400/Screenshot+from+2021-01-25+18-28-07.png" title="Output" width="640" /></a></span></div><span style="color: red;"><b><span style="font-size: medium;"><br /></span></b></span><p><span style="font-size: medium;"><span style="color: red;"><b>Thank You for Reading!For more posts like this do explore the site.</b></span></span></p><p><span style="font-size: medium;"><span style="color: red;"><b><span style="font-size: medium;"><br /></span></b></span></span></p><div><span style="font-size: medium;"><b> </b></span></div><p><span style="font-size: large;"><b>Related posts:</b></span></p><ul style="text-align: left;"><li><span style="font-size: medium;"><a href="https://codeityweb.blogspot.com/2021/01/top-competitors-hackerrank-solutions.html" target="_blank">Top Competitors - HackerRank Solution</a></span></li><li><span style="font-size: medium;"><a href="https://codeityweb.blogspot.com/2021/01/Easiest-way-to-solve-SQL-Revising-the-Select-Query-I-HackerRank.html" target="_blank">Revising the Select Query 1- Hackerrank solution</a> </span></li><li><span style="font-size: medium;"><a href="https://codeityweb.blogspot.com/2021/01/Simple-Method-For-Revising-the-Select-Query-II-Hackerrank-Solution.html" target="_blank">Revising the Select Query II - Hackerrank Solution</a></span></li><li><span style="font-size: medium;"><a href="https://codeityweb.blogspot.com/2021/01/heres-quick-way-to-solve-pairs.html" target="_blank">Pairs - Hackerrank Solution</a></span></li><li><span style="font-size: medium;"><a href="https://codeityweb.blogspot.com/2021/01/placements-hackerrank-solutions-sql.html" target="_blank">Placements- Hackerrank solution</a> <br /></span></li></ul><p><span style="font-size: medium;"><span style="color: red;"><b> </b></span></span></p><div><div><p><span style="font-size: medium;"> </span></p><div><div><span style="font-size: medium;"><br /></span></div></div></div></div><div class="blogger-post-footer">Contact Us By Email Or Get in touch by Commenting on posts</div>CodeityWebhttp://www.blogger.com/profile/01377516264362368650noreply@blogger.com0tag:blogger.com,1999:blog-7236090768681897809.post-67810890240591419042021-01-30T20:39:00.001+05:302021-01-30T20:39:16.761+05:30Top Competitors HackerRank Solution | Codeityweb <div><h1 style="text-align: left;"><span style="font-size: x-large;">Top Competitors HackerRank Solution</span></h1><p style="text-align: left;"><span style="font-size: medium;">In this post we are going to discuss the <b>Top Competitor HackerRank Solution</b>.</span></p><p style="text-align: left;"><span style="font-size: medium;"><u><i>So the problem statement is as follows:</i></u></span></p><p style="text-align: left;"><span style="font-size: medium;"><i>You have to write a query to print the id and name of the hackers who has scored maximum in more than one challenges.And order the output in decreasing order of the number of challenges and if the number of challenges are same then order them by the increasing hackerid.</i></span></p><br /><p style="text-align: left;"><span style="font-size: medium;"><b> </b><br /><b>Schema for the tables:</b><br /></span></p><ul style="text-align: left;"><li><span style="font-size: medium;"><b>Hackers</b>: </span></li></ul><div class="separator" style="clear: both; text-align: center;"><span style="font-size: medium;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhDttjmLORPhogb9W-GujAmxqxO7VVWYmKtSW0jv9VXWpH2pcPrEqfPlowl3CFSopLyp6yKMtM2O5oz-gqe9Ja95EFiJI3n9c1Z7nX3WZXezE0FOLPfDFG598RGwHhFz5Slvuun64iiJTaT/s169/1458526776-67667350b4-ScreenShot2016-03-21at7.45.59AM.png" style="margin-left: 1em; margin-right: 1em;"><img alt="Hackers" border="0" data-original-height="117" data-original-width="169" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhDttjmLORPhogb9W-GujAmxqxO7VVWYmKtSW0jv9VXWpH2pcPrEqfPlowl3CFSopLyp6yKMtM2O5oz-gqe9Ja95EFiJI3n9c1Z7nX3WZXezE0FOLPfDFG598RGwHhFz5Slvuun64iiJTaT/s16000/1458526776-67667350b4-ScreenShot2016-03-21at7.45.59AM.png" title="Hacker_table" /></a></span></div><span style="font-size: medium;"> <br /></span><ul style="text-align: left;"><li><span style="font-size: medium;"> <b>Difficulty</b>: </span></li></ul><div class="separator" style="clear: both; text-align: center;"><span style="font-size: medium;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjWT30J2gaxhhnPCddX9hgLU3IEXnA73Wi9-HkeWHcYXcUz98saqXs1CvIdg85wljfRntTwPdo0mHD_LGPyskbsfk6Ydism3NSKiWczICHqkQUpeS_7beqxw9mUBzxKJjhPFSd6oniwm_g7/s196/1458526915-57eb75d9a2-ScreenShot2016-03-21at7.46.09AM.png" style="margin-left: 1em; margin-right: 1em;"><img alt="Difficulty" border="0" data-original-height="121" data-original-width="196" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjWT30J2gaxhhnPCddX9hgLU3IEXnA73Wi9-HkeWHcYXcUz98saqXs1CvIdg85wljfRntTwPdo0mHD_LGPyskbsfk6Ydism3NSKiWczICHqkQUpeS_7beqxw9mUBzxKJjhPFSd6oniwm_g7/s16000/1458526915-57eb75d9a2-ScreenShot2016-03-21at7.46.09AM.png" title="Difficulty_table" /></a></span></div><span style="font-size: medium;"><br /></span> <span style="font-size: medium;"><br /></span><ul style="text-align: left;"><li><span style="font-size: medium;"><b>Challenges</b>: </span></li></ul><div class="separator" style="clear: both; text-align: center;"><span style="font-size: medium;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiY6DiHJfgj4Afr8zQEPFFdZOXJ1HqnWGsZI6-CnSXqGVlvj_xPh6i15QVMOA4YsqOjVkvQ7ob3fl7qvtHaQJT-CWE6q51mXI-u0gTsoV-mLJqK6lJSUuKXDgjPwIrkSxcARfcwdL1bG2B6/s194/1458527032-f9ca650442-ScreenShot2016-03-21at7.46.17AM.png" style="margin-left: 1em; margin-right: 1em;"><img alt="Challenges" border="0" data-original-height="152" data-original-width="194" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiY6DiHJfgj4Afr8zQEPFFdZOXJ1HqnWGsZI6-CnSXqGVlvj_xPh6i15QVMOA4YsqOjVkvQ7ob3fl7qvtHaQJT-CWE6q51mXI-u0gTsoV-mLJqK6lJSUuKXDgjPwIrkSxcARfcwdL1bG2B6/s16000/1458527032-f9ca650442-ScreenShot2016-03-21at7.46.17AM.png" title="Challenges_Table" /></a></span></div><span style="font-size: medium;"><br /></span><ul style="text-align: left;"><li><span style="font-size: medium;"> <b>Submissions</b>: </span></li></ul><div class="separator" style="clear: both; text-align: center;"><span style="font-size: medium;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhJ9yq8ICxot2fqZkQ42H4nwGmkDvUt4-WcIoQC7JIu4hQHz40UFSNIjUndhilVYH6cbauqCol97_lp8Idj_QpyyIP9I8gEyY2v0T8pP8OTFEFJBdajU9vWm2vuDonECbcS1i-0umJEuxSM/s196/1458527077-298f8e922a-ScreenShot2016-03-21at7.46.29AM.png" style="margin-left: 1em; margin-right: 1em;"><img alt="Submissions" border="0" data-original-height="187" data-original-width="196" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhJ9yq8ICxot2fqZkQ42H4nwGmkDvUt4-WcIoQC7JIu4hQHz40UFSNIjUndhilVYH6cbauqCol97_lp8Idj_QpyyIP9I8gEyY2v0T8pP8OTFEFJBdajU9vWm2vuDonECbcS1i-0umJEuxSM/s16000/1458527077-298f8e922a-ScreenShot2016-03-21at7.46.29AM.png" title="Submissions_Table" /></a></span></div><p><span style="font-size: medium;"><br /> </span></p><p><span style="font-size: medium;"><br /></span></p><h4 style="text-align: left;"><span style="font-size: medium;"><b>Understanding Tables:</b><br /></span></h4><ul style="text-align: left;"><li><span style="font-size: medium;">You can see that we have 4 different tables and each tables are related to each others with a foreign key.</span></li><li><span style="font-size: medium;">Submissions table <b>Related</b> To Challenges Table <b>BY</b> challenge_id</span></li><li><span style="font-size: medium;">Challenges table <b>Related</b> To Difficulty Table <b>BY</b> difficulty_level</span></li><li><span style="font-size: medium;">Hackers table <b>Related</b> To Submissions Table <b>BY</b> hacker_id</span></li></ul><h3 style="text-align: left;"><span style="font-size: medium;"> </span></h3></div><div style="text-align: left;"><h3><span style="font-size: medium;">Solving the Question:</span></h3></div><div><ul style="text-align: left;"><li><span style="font-size: medium;">As we need to find the id and name of hacker with full score in more than 1 challenge,that means we need to somehow compare the scores of the hacker with the score in the difficulty table.</span></li><li><span style="font-size: medium;">Since both the values are in different tables we will need to relate them by either <b>WHERE</b> clause or <b>By using</b> <b>JOINS</b>.</span></li><li><span style="font-size: medium;">We will use both the methods to see how it works .</span></li><li><span style="font-size: medium;">So lets start.</span></li></ul><h2 style="text-align: left;"><span style="font-size: medium;"> </span></h2></div><div style="text-align: left;"><h2><span style="font-size: medium;">Top Competitors HackerRank Solution Using Where Clause:</span></h2></div><div><span style="font-size: medium;"> </span></div><p><span style="font-size: medium;"><span style="color: #351c75;"><span style="color: red;">select </span>h.hacker_id,h.name from <br />hackers h,<br />challenges c ,<br />difficulty d,<br />submissions s <br /><br /><span style="color: red;">where</span> h.hacker_id=s.hacker_id<br />and c.challenge_id=s.challenge_id<br />and c.difficulty_level=d.difficulty_level<br />and s.score=d.score<br /><br /><span style="color: red;">group by</span> h.hacker_id,h.name having count(h.hacker_id)>1<br /><br /><span style="color: red;">order by</span> count(s.score) desc,h.hacker_id</span></span></p><p><span style="font-size: medium;"><span style="color: #351c75;"><b> </b></span></span></p><h3 style="text-align: left;"><span style="font-size: medium;"><b>Explanation:</b></span></h3><ul style="text-align: left;"><li><span style="font-size: medium;"><span style="color: #351c75;"><b> </b><span style="color: black;">We first select the<b> id and name</b> from hackers Table.</span></span></span></li><li><span style="font-size: medium;"><span style="color: #351c75;"><span style="color: black;">Now as discussed earlier that tables are related to each others with foreign keys so we use it to fetch the records from different tables.</span></span></span></li><li><span style="font-size: medium;"><span style="color: #351c75;"><span style="color: black;">We use Group By to group the hackers who has participated in more than one challenge <b>Since it is asked in the question.</b></span></span></span></li><li><span style="font-size: medium;"><span style="color: #351c75;"><span style="color: black;">Now the last step is to sort them , so we use order by to sort them in descending order of the count of the scores i.e number of challenges in which the hacker has got full scores , and if the count is same we sort them as per the hacker id.</span></span></span></li></ul><span style="font-size: medium;"><span style="color: #351c75;"><span style="color: black;"> </span></span></span><p><span style="font-size: medium;"><span style="color: #351c75;"><span style="color: black;"><b> </b></span></span></span></p><h2 style="text-align: left;"><span style="font-size: medium;"><span style="color: #351c75;"><span style="color: black;"><b>Top Competitors HackerRank Solution Using Joins:</b></span></span></span></h2><h2 style="text-align: left;"><span style="font-size: medium;"><span style="color: #351c75;"><span style="color: black;"><b> </b></span></span></span></h2><p><span style="font-size: medium;"><span style="color: red;">select</span> h.hacker_id ,h.name from Submissions <span style="color: red;">as</span> s <br /><br /><span style="color: red;">inner join</span> Challenges <span style="color: red;">as</span> c <span style="color: red;">on</span> s.challenge_id=c.challenge_id <br /><span style="color: red;">inner join</span> Difficulty <span style="color: red;">as </span>d<span style="color: red;"> on</span> c.difficulty_level=d.difficulty_level <br /><span style="color: red;">inner join</span> Hackers <span style="color: red;">as</span> h <span style="color: red;">on</span> h.hacker_id=s.hacker_id<br /><br /><span style="color: red;">where</span> s.score=d.score <br /><br /><span style="color: red;">group by</span> h.hacker_id,h.name<br /><br /><span style="color: red;">having</span> count(s.hacker_id)>1<br /><br /><span style="color: red;">order by </span>count(s.score) desc ,s.hacker_id <span style="color: #351c75;"><span style="color: black;"> </span></span></span></p><p><span style="font-size: medium;"><span style="color: #351c75;"><span style="color: black;"> </span></span></span></p><h3 style="text-align: left;"><span style="font-size: medium;"><span style="color: #351c75;"><span style="color: black;">Explanation:</span></span></span></h3><ul style="text-align: left;"><li><span style="font-size: medium;"><span style="color: #351c75;"><span style="color: black;">It is no different than the Where clause in terms of understanding.</span></span></span></li><li><span style="font-size: medium;"><span style="color: #351c75;"><span style="color: black;">We join all the tables on the basis of the common field.</span></span></span></li><li><span style="font-size: medium;"><span style="color: #351c75;"><span style="color: black;">We use Where Clause to select those hackers who has scored full score.</span></span></span></li><li><span style="font-size: medium;"><span style="color: #351c75;"><span style="color: black;">Then we group them in order to find who has participated in more than 1 challenge and scored full score.</span></span></span></li><li><span style="font-size: medium;"><span style="color: #351c75;"><span style="color: black;">Finally we Sort them as per the number of challenges scored max and if they are same for a particular hacker than we sort than according to the hacker_id.</span></span></span></li></ul><p><span style="font-size: medium;"><span style="color: #351c75;"><span style="color: black;"> </span></span></span></p><h3 style="text-align: left;"><span style="font-size: medium;"><span style="color: #351c75;"><span style="color: black;">Output:</span></span></span></h3><p><span style="font-size: medium;"><span style="color: #351c75;"><span style="color: black;"></span></span></span></p><div class="separator" style="clear: both; text-align: center;"><span style="font-size: medium;"><span style="color: black;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjAJMJlrlfTed4ueMi1wyAiP3apX6vf03MPH3lPcbz3Qxay81QANuAl2vY_J0no4xuXvWf2I7hZKCOQxLrX9RbT5o9t74KjHbHfcCVBhkVRzOYgUG_lXtQYjP93EFlpMEpgJkqDHycZR0L4/s932/Screenshot+from+2021-01-20+11-20-52.png" style="margin-left: 1em; margin-right: 1em;"><img alt="Top Competitor - HackerRank Solution" border="0" data-original-height="633" data-original-width="932" height="271" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjAJMJlrlfTed4ueMi1wyAiP3apX6vf03MPH3lPcbz3Qxay81QANuAl2vY_J0no4xuXvWf2I7hZKCOQxLrX9RbT5o9t74KjHbHfcCVBhkVRzOYgUG_lXtQYjP93EFlpMEpgJkqDHycZR0L4/w400-h271/Screenshot+from+2021-01-20+11-20-52.png" title="Top Competitor - HackerRank Solution" width="400" /></a></span></span></div><p><span style="font-size: medium;"><span style="color: black;"><br /> </span></span></p><p><span style="font-size: medium;"><span style="color: black;"><span style="font-size: medium;"><br /></span></span></span></p><div><span style="font-size: medium;"><b> </b></span></div><p><span style="font-size: large;"><b>Related posts:</b></span></p><ul style="text-align: left;"><li><span style="font-size: medium;"><a href="https://codeityweb.blogspot.com/2021/01/placements-hackerrank-solutions-sql.html" target="_blank">Placements - Hackerrank Solution</a></span><br /></li><li><span style="font-size: medium;"><a href="https://codeityweb.blogspot.com/2021/01/Easiest-way-to-solve-SQL-Revising-the-Select-Query-I-HackerRank.html" target="_blank">Revising the Select Query 1- Hackerrank solution</a> </span></li><li><span style="font-size: medium;"><a href="https://codeityweb.blogspot.com/2021/01/Simple-Method-For-Revising-the-Select-Query-II-Hackerrank-Solution.html" target="_blank">Revising the Select Query II - Hackerrank Solution</a></span></li><li><span style="font-size: medium;"><a href="https://codeityweb.blogspot.com/2021/01/heres-quick-way-to-solve-pairs.html" target="_blank">Pairs - Hackerrank Solution</a> <br /></span></li></ul><p><span style="font-size: medium;"><span style="color: black;"> </span></span></p><p></p><div class="blogger-post-footer">Contact Us By Email Or Get in touch by Commenting on posts</div>CodeityWebhttp://www.blogger.com/profile/01377516264362368650noreply@blogger.com1tag:blogger.com,1999:blog-7236090768681897809.post-70665545969650921732021-01-30T20:39:00.000+05:302021-01-30T20:39:03.402+05:30Placements HackerRank Solution | SQL | Codeityweb<div><div><div><div><h1 style="text-align: left;"><span style="font-size: x-large;"> Placements HackerRank Solutions | Practice | SQL | Advanced Join</span></h1><p style="text-align: left;"><span style="font-size: medium;"><br /></span></p><p style="text-align: left;"><span style="font-size: medium;">In this post we are going to discuss the Placements - hackerrank solution that is a part of SQL practice module on Hackerrank in Advanced join section. So lets start !<br /></span></p><p style="text-align: left;"><b><span style="font-size: large;">Problem Statement:</span></b></p><p><span style="font-size: medium;"><br />You are given three tables: Students, Friends and Packages. Students contains two columns: ID and Name. Friends contains two columns: ID and Friend_ID (ID of the ONLY best friend). Packages contains two columns: ID and Salary (offered salary in $ thousands per month).</span></p><ul style="text-align: left;"><li><b><span style="font-size: medium;">Students Table Schema:</span></b></li></ul><div class="separator" style="clear: both; text-align: center;"><span style="font-size: medium;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjB6mnsNCxhY4fKmPajrwAzQkO-ZQG-zPHoe9LRoR4o3YJ-XY9VeQUGJNATOx3RvvtgqBja1SOg7jq1vYgxStw9Nt10q9ILGL3FvA9F4g7Aa-JZ0mvkcZgoVF7K3n5u_WNXlYLXiqYi1DwS/s318/Students.png" style="margin-left: 1em; margin-right: 1em;"><img alt="Placements-HackerRank Solution" border="0" data-original-height="162" data-original-width="318" height="326" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjB6mnsNCxhY4fKmPajrwAzQkO-ZQG-zPHoe9LRoR4o3YJ-XY9VeQUGJNATOx3RvvtgqBja1SOg7jq1vYgxStw9Nt10q9ILGL3FvA9F4g7Aa-JZ0mvkcZgoVF7K3n5u_WNXlYLXiqYi1DwS/w640-h326/Students.png" title="Students Table" width="640" /></a></span></div><span style="font-size: medium;"><br /></span><ul style="text-align: left;"><li><span style="font-size: medium;"> <b>Friends Table Schema:</b></span></li></ul></div><span style="font-size: medium;"><br /></span><div class="separator" style="clear: both; text-align: center;"><span style="font-size: medium;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjQ9bvOtYXNAAxpyODySEMw524yP6JLgV3sQ20qdKd_m59O9Czx7pHE2zv3EcRBMc8NL2BCzS-9JeA0rBOV26wLN2o2TOw6XBvZrg7YFSY4V-ep6Q3_cxCISNrCLSidp_cZMa7_w3RKPPQF/s318/Friends1.png" style="margin-left: 1em; margin-right: 1em;"><img alt="Placements-Hackerrank solution" border="0" data-original-height="189" data-original-width="318" height="380" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjQ9bvOtYXNAAxpyODySEMw524yP6JLgV3sQ20qdKd_m59O9Czx7pHE2zv3EcRBMc8NL2BCzS-9JeA0rBOV26wLN2o2TOw6XBvZrg7YFSY4V-ep6Q3_cxCISNrCLSidp_cZMa7_w3RKPPQF/w640-h380/Friends1.png" title="Friend table schema" width="640" /></a></span></div><ul style="text-align: left;"><li><b><span style="font-size: medium;">Packages and All Combined Table Schema:</span></b></li></ul></div><span style="font-size: medium;"><br /></span><div class="separator" style="clear: both; text-align: center;"><span style="font-size: medium;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgmHR-iVwhFxPCgVoP5htiVyBZ7iHF1Yu9fWXZSQpFvD8tapuy7FQ2_6jbwl50HzKleEsrmYGGJr_I9XcZQ-QTyC02ICZaGQAfFRahcqLAtZHAJ1XBi-yAkV9NBSNKpKcB7NF9UCwgI4PeB/s499/Packages.png" style="margin-left: 1em; margin-right: 1em;"><img alt="Placements-Hackerrank solution" border="0" data-original-height="499" data-original-width="318" height="640" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgmHR-iVwhFxPCgVoP5htiVyBZ7iHF1Yu9fWXZSQpFvD8tapuy7FQ2_6jbwl50HzKleEsrmYGGJr_I9XcZQ-QTyC02ICZaGQAfFRahcqLAtZHAJ1XBi-yAkV9NBSNKpKcB7NF9UCwgI4PeB/w408-h640/Packages.png" title="Table schemas all" width="408" /></a></span></div><p><span style="font-size: large;"><b> </b></span></p><p><span style="font-size: large;"><b>Task:</b></span></p><span style="font-size: medium;"><br /></span><ul style="text-align: left;"><li><span style="font-size: medium;">Write a query to output the names of those students whose best friends got offered a higher salary than them. </span></li><li><span style="font-size: medium;">Names must be ordered by the salary amount offered to the best friends. </span></li><li><span style="font-size: medium;">It is guaranteed that no two students got same salary offer.</span></li></ul><p><span style="font-size: medium;"> </span></p><p><span style="color: #38761d;"><b><span style="font-size: large;">Understanding the schemas:</span></b></span></p><ul style="text-align: left;"><li><span style="font-size: medium;"> You are given 3 tables Friends , Students and Packages.</span></li><li><span style="font-size: medium;">Students table contains the <b>ID </b>and <b>Name </b>of the student itself, where as the <b>Friends</b> Table contains the <b><span style="color: red;">Mapping</span> </b>of <b>Students</b> with their <b>Best Friend Id's.</b></span></li><li><span style="font-size: medium;">The Third table is Package table where the <b>Student ID</b> is <span style="color: red;"><b>Mapped</b></span> with their respective <b>Salary</b></span></li></ul><p><span style="color: #0b5394;"><span style="font-size: medium;"><b>So if we relate the 3 tables we get that Students table Related to Friends table and Students table is related to Packages table.</b></span></span></p><p><span style="color: #0b5394;"><span style="font-size: medium;"><b>And we need to find the students who got less salary then their best friend.</b></span></span></p><p><span style="font-size: medium;"><b> </b></span></p><p><span style="font-size: large;"><b>So lest see the solution for Placements Question:</b></span></p><p><span style="font-size: medium;"><br /><span style="color: red;">select</span> s.Name <span style="color: #0b5394;">from</span> Students s<br /><span style="color: red;">join</span> Friends f <span style="color: #0b5394;">on</span> s.ID=f.ID<br /><span style="color: red;">join</span> Packages student_salary <span style="color: #0b5394;">on</span> s.ID=student_salary.ID<br /><span style="color: red;">join</span> Packages friend_salary <span style="color: #0b5394;">on</span> f.Friend_Id=friend_salary.ID<br /><br /><span style="color: red;">where</span> friend_salary.Salary>student_salary.Salary<br /><span style="color: red;">order</span> <span style="color: red;">by</span> friend_salary.Salary </span></p><p><span style="font-size: medium;"> </span></p><p><span style="font-size: medium;"> </span></p><p><span style="font-size: large;"><b>Understanding the Solution:</b></span></p><ul style="text-align: left;"><li><span style="font-size: medium;">We used joins in this query as you might have guessed that we need to get data from different tables so we need to join them or use where clause, but the use of where clause solution doesn't click in the first go, so we try to solve it using INNER JOIN.</span></li></ul><span style="font-size: medium;"> </span><ul style="text-align: left;"><li><span style="font-size: medium;">So we join the Students table and Friends Table on the basis of the student id,that means that the virtual table will have <b><span style="color: #274e13;">ID|NAME|FRIEND_ID</span> approximatly.</b></span></li></ul><span style="font-size: medium;"><b> </b></span><ul style="text-align: left;"><li><span style="font-size: medium;">Now we join this virtual table with the Package table to get the Salary of the Student itself. SO the table would be now <span style="color: #274e13;"><b>ID|NAME|FRIEND_ID|STUDENT_SALARY.</b></span></span></li></ul><span style="font-size: medium;"><b> </b></span><ul style="text-align: left;"><li><span style="font-size: medium;"><b>N</b>ow we have to get the salary of the best friend so we again join this with the vitual table , so the table becomes <span style="color: #274e13;"><b>ID|NAME|FRIEND_ID|STUDENT_SALARY|FRIEND_SALARY.</b></span></span></li></ul><span style="font-size: medium;"><b> </b></span><ul style="text-align: left;"><li><span style="font-size: medium;">Finally we need to output the Names of the students who got less salary then their friend so we compare the friend's salary with student's salary, and order them in ascending order of the friend's salary.</span></li></ul><b><span style="font-size: medium;"> </span></b></div><div><b><span style="font-size: medium;">Output:</span></b></div><div><span style="font-size: medium;"> </span></div><div><div class="separator" style="clear: both; text-align: center;"><span style="font-size: medium;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiFBM0H6XEjIOwpZt39HcCKYZ2HB0sc9l89kBeLyVOJpK_h1R9yi6B9KtogUM16aQ15BFb_huhp8Y_rreY44O5c1FlK0V0xA6GPSpK6V2X25-vnTWw1oDbJwyQZo2SaTIFC-7RR1G8SXjcs/s1037/Screenshot+from+2021-01-23+19-16-19.png" style="margin-left: 1em; margin-right: 1em;"><img alt="Placements-Hackerrank solution output" border="0" data-original-height="657" data-original-width="1037" height="406" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiFBM0H6XEjIOwpZt39HcCKYZ2HB0sc9l89kBeLyVOJpK_h1R9yi6B9KtogUM16aQ15BFb_huhp8Y_rreY44O5c1FlK0V0xA6GPSpK6V2X25-vnTWw1oDbJwyQZo2SaTIFC-7RR1G8SXjcs/w640-h406/Screenshot+from+2021-01-23+19-16-19.png" title="Output placements" width="640" /></a></span></div><span style="font-size: medium;"><br /> <br /></span></div><div><span style="font-size: medium;"><b> </b></span></div><p><span style="font-size: large;"><b>Related posts:</b></span></p><ul style="text-align: left;"><li><span style="font-size: medium;"><a href="https://codeityweb.blogspot.com/2021/01/top-competitors-hackerrank-solutions.html" target="_blank">Top Competitors - HackerRank Solution</a></span></li><li><span style="font-size: medium;"><a href="https://codeityweb.blogspot.com/2021/01/Easiest-way-to-solve-SQL-Revising-the-Select-Query-I-HackerRank.html" target="_blank">Revising the Select Query 1- Hackerrank solution</a> </span></li><li><span style="font-size: medium;"><a href="https://codeityweb.blogspot.com/2021/01/Simple-Method-For-Revising-the-Select-Query-II-Hackerrank-Solution.html" target="_blank">Revising the Select Query II - Hackerrank Solution</a></span></li><li><span style="font-size: medium;"><a href="https://codeityweb.blogspot.com/2021/01/heres-quick-way-to-solve-pairs.html" target="_blank">Pairs - Hackerrank Solution</a> </span></li></ul></div><p><b><span style="font-size: large;">Conclusion:</span></b></p><p><span style="font-size: medium;">We have discussed and understood the Placements Hackerrank problem using joins ,for more posts like this do follow the site and subscribe through email to get instant updates for latest posts.</span></p><p><span style="font-size: medium;"><br /></span></p><p><span style="font-size: medium;"><span style="font-size: large;"><b>Thank You For Reading!</b></span><br /></span></p><div><div><span style="font-size: medium;"><b> </b><br /></span></div></div><div class="blogger-post-footer">Contact Us By Email Or Get in touch by Commenting on posts</div>CodeityWebhttp://www.blogger.com/profile/01377516264362368650noreply@blogger.com0tag:blogger.com,1999:blog-7236090768681897809.post-46283022823985086952021-01-21T11:15:00.001+05:302021-01-21T15:13:11.024+05:30How Creating Own Password Manager Can Ease Your Pain. | by Codeityweb | Jan, 2021 <p style="text-align: left;"><span style="font-size: medium;"><br /></span></p><h1 style="text-align: left;"><span style="font-size: x-large;">Creating Your Own Best Password Manager</span></h1><p><span style="font-size: medium;"><br /><span style="color: #38761d;">Their are many Password Manager available but in this post we will create our own offline password manager in python.</span></span></p><p><span style="font-size: medium;"><br /><span style="color: #bf9000;">And I personally use this application for storing my passwords.</span></span></p><p style="text-align: left;"><span style="font-size: medium;">We all uses different applications and websites everyday and it requires us to login ,some of them give us option to login using OAuth and we can signup and login using it.</span><br /></p><p><span style="font-size: medium;">But in many cases we have to register using our Email Id and set a password for our account.<br />We have to make sure the Password is not common so that it cannot be guessed using dictionary attack.<br /> <br />Also the Length of the password should not be small and must include various letters, digits and punctuation marks to make it difficult to crack.<br /> <br />Much like this <span style="color: #674ea7;"><b>Yije4"*{|\/zm+Rgye`.QL>:M^`=&)8_O </b></span><br /><br /><br /><br />We can create few difficult passwords like them, that are hard to crack but we cannot use them on all sites ,because if password of one website is compromised than the websites using the same passwords may also be in danger.<br /><br />So we need to create almost different passwords for every website with more or less difficulty. <br /><br />We have a <a href="https://codeityweb.blogspot.com/2021/01/random-password-generator.html">Random Password Generator</a> Post where we have created a random password generator using python which includes letters , digits and punctuation marks and they are almost hard to crack, but how do we remember them?.</span></p><p><span style="font-size: medium;"><span style="color: #274e13;"><br />So for That we are going to Create A Secure Offline Password Manager of our own to store those passwords and we can use them when they are needed.And indeed it will be our best password manager so far.</span><br /> <br /><br /><b>So lets Start:</b><br /><br />You might have heard that passwords should be hashed and then stored. Because getting the original password is impossible from the hashed passwords.<br /><br /><b>The Question is:</b></span></p><p><span style="font-size: medium;"><br /><b><span style="color: #0b5394;">How to implement password storage with hashing and salting in our application?</span><br /></b><br /></span></p><blockquote><span style="font-size: medium;">For this we will use python hashlib library and we will use sha512 to hash our password.</span></blockquote><span style="font-size: medium;"><br /><br /> <br /><br />So What should be the functionality of our app,basically it should perform CRUD operations. Right?</span><p></p><p><span style="font-size: medium;"><br /><b>So the main functionality of our app will be :</b></span></p><p><span style="font-size: medium;"><b> </b><br /></span></p><ul style="text-align: left;"><li><span style="font-size: medium;">Store the passwords</span></li></ul><ul style="text-align: left;"><li><span style="font-size: medium;">Display the passwords to the user</span></li></ul><ul style="text-align: left;"><li><span style="font-size: medium;">Update the existing record of a website</span></li></ul><ul style="text-align: left;"><li><span style="font-size: medium;">Delete a particular record</span></li></ul><p><span style="font-size: medium;"><br /><b><br />We will not use a simple file or something but we will be using Sqlite3 database for storing our passwords .</b><br /><br /><br /><br /><a href="https://www.blogger.com/u/5/#"><img alt="Best Password Manager" border="0" height="240" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhe5rP8q4PX0VRFd5TGsvt72XOJj_uioUCqQS0F9Sa2SmtPgff-d8gYv0XDQocj0ZObWEExAHAi_v2fcp8_9A_Z6ewjvnRW94lsrc6uLMZeiWDuXvAh38jblTDUdHnnnbnB93eAdIqzi2ny/w640-h240/displaychoicestouser.png" title="Index Menu" width="640" /></a><br /><br /><br />This is how it will look like<br /><br /> <br /><b>Step 1:</b><br /> <br />As we have decided to use sqlite3 database so we need to create a database and tables into it,<b><span style="color: red;">but wait a minute! </span></b><br /> <br />Before Starting it we have some questions like<br /><b>How many tables do we need?</b><br /><b>What will be number of columns in the tables?</b><br />In technical terms What will be the fields of the tables? And so on..<br /><br /><br /><br /><br /><b>So How many Tables do we need?</b><br /></span></p><blockquote><span style="font-size: medium;">The answer is we will require only one table to store our passwords<br />as per our use case.</span></blockquote><span style="font-size: medium;"><br />You can further increase the number of tables to make the app more functional.<br />if you require but to keep it simple and to make an app that we can use instantly we will make only one table.<br /><br /><br /><b>Now What will be number of columns in the tables?</b><br /><blockquote>So we need Website Name, Username, Password, and also Email Id used for that website.Their are 4 columns in total, again you can increase as per requirements.</blockquote><br /> <br /> <br />So our Sqlite3 database will be as:<br /> <br /> <br /> <br /><b><br />Code:</b><br /> </span><p></p><p><span style="font-size: medium;"></span></p><p><b><span style="font-size: medium;"></span></b></p><blockquote><p><b><span style="font-size: medium;">-<br />import sqlite3<br />import hashlib<br />import base64 <br /> <br />class PassManager:<br /><br /> <span> </span>def __init__(self):<br /> <span> </span><span> </span>self.conn = sqlite3.connect("PasswordManager.db")<br /> <span> </span><span> </span>query = '''Create Table if not exists PassStorage(<br /> <span> </span><span> </span>site varchar(255)not null,<br /> <span> </span><span> </span>username varchar(255) not null,<br /> <span> </span><span> </span>password varchar(255) not null,<br /> <span> </span><span> </span>email varchar(255))'''<br /> <span> </span><span> </span>self.conn.execute(query)<br /> <span> </span><span> </span>self.conn.commit() <br /><br /></span></b></p><p><span style="font-size: medium;"><b>-</b></span></p></blockquote><p><span style="font-size: medium;"><b></b><br />we are building PassManager class and we will build our CRUD operation as functions of this class as we move forward.<br /><br /><br /> <br /><b>Step 2:</b><br /><br /><br />Now we have decided how our database will look like, now we have to think of the flow our app .<br /><br /><br />So we need a master username and master password ,so that only the user can access the database and no one else,and user needs to remember only the master credentials to get all the information.<br /> <br /><b></b></span></p><blockquote><span style="font-size: medium;"><b>You may have a question , that is password manager safe and is it really the best password manager app?</b><br /> <br />The answer is simple , yes it is safe because it is standalone offline application that only you will use on your local machine with local database,so no one have access to the details except you☺</span></blockquote><span style="font-size: medium;"><br /> <br /> <br /><br /><a href="https://www.blogger.com/u/5/#"><img alt="Flow of th e app- Best Password Manager" border="0" height="640" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiTSPSObjsowgbE99Jrth8IZAjuxYs3K4VUWQWcjx7rusD5NlUc5JEBJ4ft7kGSjSsK12gy3a5sEUORPRFczcBrlqDt70MpWZDn4Rf5WFUrhqoO1vIfu7wID_tR_OfkGAQ0aEHLgH3QJF6c/w501-h640/passmanagerflow.png" title="Best Password Manager" width="501" /></a><br /> <br /> <span style="color: red;"><br />This is the normal flow of our app.</span><br /><br /><br /><br /><b>Step 3:</b><br /><br /><br />Now lets start to build our app as per the flow.<br />so we start with getting master user credentials and storing them into the database by hashing the password.<br /><br /><br /><br /><b>Code:</b><br /><br /><br />We have Class PassManager in which we initialized the __init__ function and made the connection to the sqlite3 database as soon as the app is run<br /><br /><br /><b>Now the function to create Master User is as follows</b><br /> <br /><a href="https://www.blogger.com/u/5/#"><img alt="Master User - Best Password Manager" border="0" height="519" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjX75ZJOa-wt118FdfjIJgtWAJoqh1Pv29xZ4GzWXrVnTAwOIZxRWHmjDh8Gp43F8q5-DAo7CD6cBOmPVZ_zqzJXLROtb_wrLgRZl_7_rmqWkZcCs3_a8-yWNFrVeE62G5q68LUJXE7h8hg/w640-h519/Screenshot+from+2021-01-05+20-53-21.png" title="Creating master user" width="640" /></a><br /><br /><br /> <br /></span><p></p><ul style="text-align: left;"><li><span style="font-size: medium;"> As we discussed earlier we will store password in hashed form</span><span style="font-size: medium;"> and for that we are using hashlib library.</span><br /></li></ul><ul style="text-align: left;"><li><span style="font-size: medium;">we use salt as combination of username and password you can use</span><span style="font-size: medium;"> as your wish ,but make sure you remember the salt then,</span><span style="font-size: medium;">because it will be used further when we are</span><span style="font-size: medium;"> needed to authenticate the master user.</span><br /></li></ul><p><span style="font-size: medium;"> <br /> <br /><b>Output:</b><br /> <br /><a href="https://www.blogger.com/u/5/#"><img alt="Create Best Password Manager" border="0" height="161" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh9IY_45qQmpjTA48XFNXOIp1wIEHyodfYFHGTqY1U0wNEZaLHPKuXyQMqa5tHNdSLxrzID2VD0g-84dVkADUN7czZUxRHvdONZRDvc81o-3yKruMrS1y9Gelsjre63sFLgPYJg1hINzdAq/w640-h161/setmasteruser.png" title="Best Password Manager" width="640" /></a><br /> <br /> <br /> <br /><b>Step 4:</b><br /><br />Now we will move to create the functions for performing the CRUD<br />operations.<br /> <br /> <br /> <br /><br /><a href="https://www.blogger.com/u/5/#"><img alt="Best Password Manager" border="0" height="241" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhe5rP8q4PX0VRFd5TGsvt72XOJj_uioUCqQS0F9Sa2SmtPgff-d8gYv0XDQocj0ZObWEExAHAi_v2fcp8_9A_Z6ewjvnRW94lsrc6uLMZeiWDuXvAh38jblTDUdHnnnbnB93eAdIqzi2ny/w640-h241/displaychoicestouser.png" width="640" /></a><br /> <br /> <br /><br /><b>1.Store password in the database for various website </b><span> </span><span> </span><span> </span><span> </span><span> </span><span> </span><span> </span><span> </span><span> </span><span> </span><span> </span><span> </span><span> </span><span> </span><span> </span><span> </span><span> </span><span> </span><span> </span><span> </span><span> </span><span> </span><span> </span><span> </span><span> </span><span> </span><span> </span><span> </span><span> </span><span> </span><span> </span><span> </span><span> </span><span> </span><span></span>Now we cannot store the passwords directly into the database<br />we need to encrypt it so that it is not accessed by anyone<br />without the KEY.<br />Also we cannot hash the passwords because we need to display them back not like our master password which we don't need to revert.<br /> <br />So we are going to user fernet library from cryptography<br />module in python<br /> <br />If you don't have it installed just run<br /><span style="color: red;"><b>pip install cryptography</b></span><br /> <br />we are going to need authentication of master more often<br />so we will create a function to authenticate it.<br /> <br /><br /><b>Code:</b><br /><br /><a href="https://www.blogger.com/u/5/#"><img alt="Auth-Best Password Manager" border="0" height="257" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjuT_wSx0BoaIcnKTiB6BPCx_Y6ZH0vMnHRECAby83l-bWuuepw98ei0FeCM5uCGYekC3j9vsBkcDlBPZGNbRXrE8GHwkkmrE499ugpLQoZsHv1sDy_ssVpaHEtfIJOUlakibKlyMyz5G3T/w640-h257/Screenshot+from+2021-01-05+20-53-41.png" width="640" /></a><br /> <br /></span></p><ul style="text-align: left;"><li><span style="font-size: medium;">so here we ask for credentials and make the hash using the salt</span><span style="font-size: medium;">and password.</span></li></ul><div><ul style="text-align: left;"><li><span style="font-size: medium;">Hence we told early that you can have any salt but remember it.</span><span style="font-size: medium;">So we fire SQL query to fetch the master user hashedPassword from</span><span style="font-size: medium;"> the database and compare it with our generated hash.</span></li></ul><div><ul style="text-align: left;"><li><span style="font-size: medium;"> if it matched we return the master password that we are going</span><span style="font-size: medium;"> to use the different functions for CRUD operations later on.</span><br /></li></ul><p><span style="font-size: medium;"> <br /></span></p><ul style="text-align: left;"><li><span style="font-size: medium;">if it is not matched then we return "PDM" i.e password Dont match.</span></li></ul><p><span style="font-size: medium;"> <br /> <br /><br />As we have an authentication function now we can move<br />to make our <b>1st CRUD operation function to Store the user Passwords</b><br /> <br /><br /><b>Code:</b><br /><br /><a href="https://www.blogger.com/u/5/#"><img alt="Store pasword - Best Password Manager" border="0" height="558" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjmHlMcotfeH7D5N6Kqlij53omi-fTCDsC8vFOkAtBk7j_HFwKoEDLJB_0TO9vV5iwTk9AAxbq8q_3tJul0XwdiJYDEx9hatMcknhA_mMIw1NgrbhvTjytYVSBrsK7ZelJsWyoc23Pwv3RM/w640-h558/Screenshot+from+2021-01-05+20-53-56.png" width="640" /></a><br /><br /><br /><br /> <br /></span></p><ul style="text-align: left;"><li><span style="font-size: medium;"> In this function we first check if the site and username already exists</span><span style="font-size: medium;"> in the database if so we ask them to update the password instead of </span><span style="font-size: medium;">creating a new record.</span><br /></li></ul><ul style="text-align: left;"><li><span style="font-size: medium;">if not we ask the website name , username,password and email id used</span><span style="font-size: medium;"> for the website.</span><br /></li></ul><p><span style="font-size: medium;"> <br /> <br /> <br /> <br /><a href="https://www.blogger.com/u/5/#"><img alt="Output-Best Password Manager" border="0" height="286" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgUsTZQwXRq1dKLLwaUKREdxL2AcDB7dTs-bjfmiiwxK9e7HqFx_EcC-6LAQ_L5Bbz2JVYh9YR91CBdbVZavFJiKH0xvLxOmKc-Tmn7jPUPU_G0d-u6KwwunmXDB0nKE8xDsoVXNmf04xZj/w640-h286/storepasswordindatabase.png" width="640" /></a><br /><br /></span></p><ul style="text-align: left;"><li><span style="font-size: medium;"> As mentioned earlier we are using fernet for encrypting the passwords</span><span style="font-size: medium;"> fernet requires fernet key to encrypt and decrypt the message.</span><br /></li></ul><p><span style="font-size: medium;"> <br /></span></p><ul style="text-align: left;"><li><span style="font-size: medium;">we can create a fernet key by using built in funtion provided by</span><span style="font-size: medium;"> fernet called as generate_key(), but in that case we need to store</span><span style="font-size: medium;"> the key some where safe and if the key is found the passwords can be </span><span style="font-size: medium;">seen.</span><br /></li></ul><p><span style="font-size: medium;"> <br /></span></p><ul style="text-align: left;"><li><span style="font-size: medium;">To avoid this problem we are creating are own key generation function called</span><span style="font-size: medium;"> fernet_generate_passwordBased_key which takes master key as an</span><span style="font-size: medium;"> argument and by using this generates the key.</span><br /></li></ul><p><span style="font-size: medium;"> <br /></span></p><ul style="text-align: left;"><li><span style="font-size: medium;">Thus the encryption of the passwords depends on the master password</span><span style="font-size: medium;"> hence you need to remember the master password if your forgot it</span><span style="font-size: medium;"> then it is not possible to decrypt the passwords for the websites.</span><br /></li></ul><p><span style="font-size: medium;"> <br /><br /> <br /><br /><b>2.Update a record:</b><br /> Now every thing is easy just some <b>SQL</b> is needed to perform our <br /><b>CRUD</b> operations<br /> <br />lets take a look<br /> <br /><br /><b>Code:</b><br /> <br /><br /><a href="https://www.blogger.com/u/5/#"><img alt="Update password-Best Password Manager" border="0" height="363" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiG9-IqnNkq51fJA8lt0E3cindKEAVJ9wKrMVeaBvive-fGWmVpFCZHdylPlDZbXqNnIUUFUWuesfAv8jJP0dagpKHd0rYnRofghxqMn5dD41_nnURjB4_8fJsxIOI_XUjId_s2BlizVUj_/w640-h363/Screenshot+from+2021-01-05+20-54-21.png" width="640" /></a><br /> <br /><b>Show Passwords:</b><br /><br /> <br /><a href="https://www.blogger.com/u/5/#"><img alt="Show password -Best Password Manager" border="0" height="235" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjJxxTxI0jSRSetz7uLlmhGvDVyZxo9iD_dVUPClwp1cd4i9HVXnuWety6t0aZwVFMtxik1uqz_CNwkXhXVzz6fadk2BQMs_UxrTbqKnhpPBuOv9b3VtOe5H2BgDpv4SvMPDq8HkeBcE-PB/w640-h235/Screenshot+from+2021-01-05+20-54-08.png" width="640" /></a><br /><br /> <br /> <br /> <br /></span></p><ul style="text-align: left;"><li><span style="font-size: medium;">Here we first authenticate the user by calling the authenticate</span><span style="font-size: medium;"> function that we created earlier and then ask for the site and username </span><span style="font-size: medium;">that we need to update.</span><br /></li></ul><p><span style="font-size: medium;"> <br /></span></p><ul style="text-align: left;"><li><span style="font-size: medium;">First we check if the site and username is present if yes then we</span><span style="font-size: medium;"> ask for new credentials and we encrypt the new password using our </span><span style="font-size: medium;">fernet function and store it in database.</span><br /></li></ul><p><span style="font-size: medium;"> <br /></span></p><ul style="text-align: left;"><li><span style="font-size: medium;">if the site and username is not present then we send appropriate response</span><span style="font-size: medium;"> to the user.</span><br /></li></ul><p><span style="font-size: medium;"> <br /> <br /><br /><a href="https://www.blogger.com/u/5/#"><img alt="Output-Best Password Manager" border="0" height="179" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhOatNgLM2YUU_tapsugQ9xueYzFIMkJWj6FM7BX2mGm02HW9gTcz5OF7YaeL2utpp4RLeHXmWBO688r8W3icp3ExBkgB9VUaAhiWPA1WgSDTNc_wfokOBtBOdEDyZ0NLp57XO-KsfqrdX4/w640-h179/displayallpasswords.png" width="640" /></a><br /><br /> <br /><br /> <b>We try to update first record now.</b><br /> <br /><br /><a href="https://www.blogger.com/u/5/#"><img alt="Output-Best Password Manager" border="0" height="307" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgbERe_tzUuvWO5q-z4GhZYR3KB3tv8j8ugMaPK0X77VoI5iDU8NfyitoSOxMSXPYjy8K_dmprbchxu2-IDFQbPFv9CbKIELIsrt14Hq9iGfMaJklThiwwChVqTE1Xjj5omvStFC0WZGGZP/w640-h307/updatetherecord.png" width="640" /></a><br /> <br /><b>After running the Operation the database looks like</b><br /> <br /><br /><a href="https://www.blogger.com/u/5/#"><img alt="Secure Password Manager" border="0" height="163" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhOK7seLI2XI0Fb-J9TfecoWNsSzTUzJc0T9v4vD1llSd4cuXhUWRV0r69l4yb_PnRayZik7IFs8sTV50b7xRvZrv029MY5wABIx2BNyJIVrc8Dhgs7C5L-srt0cFlNZYhbHf1PFYZCQfKP/w640-h163/displayafterupdatingrecord.png" width="640" /></a><br /> <br /> <br /> <br /><br /><b>3.Display all records:</b><br /><br />For displaying the records we first authenticate the master user.<br />Then we fetch all records except the master password and display them<br />now as the passwords are encrypted we need to decrypt it.</span></p><p><span style="font-size: medium;"> so we use fernet decrypt function and pass masterkey to decrypt our<br />passwords and finally display the records.<br /> <br /> <br /> <b><br />4. Delete a record:</b><br /><br /><br />Now to perform deletion we ask the user to enter the site and username<br />that they need to delete . <br />we check if the site is present or not if present we delete the record.<br />if not we return the appropriate response<br /><br /><br /><br /><b>Code:</b><br /><br /><br /> <br /><a href="https://www.blogger.com/u/5/#"><img alt="Delete record-Best Password Manager" border="0" height="527" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi33b8BfQwcLLGQrWWvfbz3vN2jLD9JTJpOvufyw2Unjbsy4NnuEyXTkhukcp_CRfhLNpqcUiWWxKFqhnH0lPnR-dovdWfy1M9hSe_tzQTyPvlEuThnynWyywjZYLtzpVuolVJMCDJ-pERM/w640-h527/Screenshot+from+2021-01-05+20-54-33.png" width="640" /></a><br /><br /> <br /> <br /><b>Output:</b><br /> <br /> <br /><br /><a href="https://www.blogger.com/u/5/#"><img alt="output" border="0" height="163" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhHwpU_j4TJuTv3T7rV528uGQ_-cx0UgW0Sc6yWQJD4CZ0SduyY6jVmbFKfz4Aa50XChrsNFZA3VbTHJDIG-Yigd3__INx1u2Xh3NjT6akgEDUt3lvW6sO9DG_cGNdakajszKcIu-ly_QT-/w640-h163/displayafterupdatingrecord.png" width="640" /></a><br /> <br /> <b><br />We try to delete the last record </b><br /><br /><br /><a href="https://www.blogger.com/u/5/#"><img alt="output" border="0" height="238" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiva18hWEAHehyJYlSAOQKS-YB8v6_MoTst54Ie4AaLLFsYlysVHmHU1bCFXOyHpeOhYtildn_qEgUmBDviDsupjdXZTcfXLNqPas2lfQGbboDR76D80RF30BWsdZ1Z-ncmAHUFypxflUhe/w640-h238/deletetherecord.png" width="640" /></a><br /> <b><br />After running the operation</b><br /><br /><br /><a href="https://www.blogger.com/u/5/#"><img alt="output-Best Password Manager" border="0" height="166" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEieMMTmpFaN5DZzA02NHFL6eRQ51vYzFA9skzR4Bz-P7ROkEHs04oJiRH7Z81Mtx0wYhOInWdeEtNbqxYPBrubG3AtlQVTz2gCRG3T1DMw4GbIWrkEucMgMVlF6rGjrc3uDpDuxB4CefXt0/w640-h166/displayafterdeletingrecord.png" width="640" /></a><br /><br /><br /> <br /> <b><br />Thus we have Successfully created our CRUD Operations.</b><br /><br /><br />Now we will include the <a href="https://codeityweb.blogspot.com/2021/01/random-password-generator.html">Random Password Generator</a><u> </u>code in our app so that we can easily generate random passwords and store them in our database.<br /> <br /><b>Output:</b><br /> <br /> <br /><a href="https://www.blogger.com/u/5/#"><img alt="random password generator -Best Password Manager" border="0" height="104" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjKVMsLpjIFYHzM2B357_Lr6KBCt__bRyhok2GRWalN4M4c3yWEV4enTtghCWhN0TnZn6zdkStSOlG6NmCyfDb2VA0sPNGZcRDlmGTQmORnKW0j4amOo9s8w0J8PeheO2_1SKoAKHua9G5a/w640-h104/generaterandompasswords.png" width="640" /></a><br /> <br /><br /><b><span style="font-size: large;"><br />✌✌✌✌ We Finally Created our Own Secure Password Manager and Probably the best password manager for us. </span></b><br /> <br /> <br /> <br /> <br /> <span style="font-size: large;"><b>To Get Full Code Visit: <a href="https://github.com/codeityweb/Password-Manager" target="_blank">Github</a></b></span> <br /><br /> <br /></span></p></div></div><div class="blogger-post-footer">Contact Us By Email Or Get in touch by Commenting on posts</div>CodeityWebhttp://www.blogger.com/profile/01377516264362368650noreply@blogger.com0tag:blogger.com,1999:blog-7236090768681897809.post-36302117303760338502021-01-18T10:17:00.007+05:302021-01-30T20:40:58.324+05:30Revising the Select Query II Hackerrank Solution | Codeityweb<div><h1 style="text-align: left;"> Revising the Select Query II</h1><p style="text-align: left;">In this post we are going to discuss the SQl hackerrank Revising the select query 2 solution.</p><p style="text-align: left;">In the previous post we have discussed the <b><a href="https://codeityweb.blogspot.com/2021/01/Easiest-way-to-solve-SQL-Revising-the-Select-Query-I-HackerRank.html" target="_blank">Revising the Select Query 1 </a></b>Solution click on the link to view that post.</p><p style="text-align: left;"> </p><p><i>So the problem statement for this is as follows:</i><br />Query the <b>NAME</b> field for all American cities in the <b>CITY</b> table with populations larger than <b>120000</b>. The CountryCode for America is <b>USA</b>. </p><p>The CITY table is described as follows:</p><p> </p><p></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg8WrmQO0s2pRNmFb_TQNa4L1jGtBBngiw34q17ygYXHxE3w8Iwq9LzEmuIKihi-HERb8xEqOZSF87vXOJYPMZkOv-HF5jYHRKqNGaYCPrkljNmZJgPAD2t7DoCNfVvXYvIgK9fthVNU2Pu/s365/1449729804-f21d187d0f-CITY.jpeg" style="margin-left: 1em; margin-right: 1em;"><img alt="Revising the select query 2-hackerrank solution" border="0" data-original-height="300" data-original-width="365" height="329" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg8WrmQO0s2pRNmFb_TQNa4L1jGtBBngiw34q17ygYXHxE3w8Iwq9LzEmuIKihi-HERb8xEqOZSF87vXOJYPMZkOv-HF5jYHRKqNGaYCPrkljNmZJgPAD2t7DoCNfVvXYvIgK9fthVNU2Pu/w400-h329/1449729804-f21d187d0f-CITY.jpeg" title="Revising the select query 2-hackerrank solution" width="400" /></a></div><br /><p></p><h1 style="text-align: left;"> </h1><h1 style="text-align: left;">SQL Query:</h1><p> <span style="color: #2b00fe;">select NAME from CITY where COUNTRYCODE='USA' and POPULATION>=120000</span></p><p><br /></p><h3 style="text-align: left;"><b>Explaination:</b></h3><ul style="text-align: left;"><li>In this question we are asked to query the names of all american cities whose population is more than 120000.</li><li>So we first select all city names with <b>Select NAME from CITY </b>and then we apply the condition that the country should be america i.e COUNTRYCODE is "<b>USA</b>" and <b>population</b> is greater than 120000 .</li><li>Finally we run the query and got the outcome as follows.</li></ul></div><p></p><p><br /></p><h3 style="text-align: left;">Output:</h3><p><b>Sample Test Case</b></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjTafPwYpM_fbj8PIaS62AFD18y7X7799m7cVIcmM50hwl4rkRTqWjBb7VZg_b5AQ337LAz0Ua8asF1a6J_aa9KCKUduL5z0FmC6EAU0tifu8Jvty-0ig9TNYRxDeBtqiXVMKrWJ9M1YYNL/s1021/Screenshot+from+2021-01-18+10-12-33.png" style="margin-left: 1em; margin-right: 1em;"><img alt="Revising the select query 2-hackerrank" border="0" data-original-height="589" data-original-width="1021" height="370" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjTafPwYpM_fbj8PIaS62AFD18y7X7799m7cVIcmM50hwl4rkRTqWjBb7VZg_b5AQ337LAz0Ua8asF1a6J_aa9KCKUduL5z0FmC6EAU0tifu8Jvty-0ig9TNYRxDeBtqiXVMKrWJ9M1YYNL/w640-h370/Screenshot+from+2021-01-18+10-12-33.png" width="640" /></a></div><p></p><p><br /></p><p><b>Test Cases:</b></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiThacZPP3Sfq3bKHJnUiRRNxmSmFFUPWz9qBXHny1Mjms59t-FqNCb3UB-roSoExvhZLYdyUwSEv9JpJEaxOeePSjS723r-WqXpsWHy2DtgTjN8VMLbkVaLI7rvwusTg5dmc2q8YhjDmWq/s1046/Screenshot+from+2021-01-18+10-13-02.png" style="margin-left: 1em; margin-right: 1em;"><img alt="Revising the select query 2 -hackerrank solution" border="0" data-original-height="691" data-original-width="1046" height="422" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiThacZPP3Sfq3bKHJnUiRRNxmSmFFUPWz9qBXHny1Mjms59t-FqNCb3UB-roSoExvhZLYdyUwSEv9JpJEaxOeePSjS723r-WqXpsWHy2DtgTjN8VMLbkVaLI7rvwusTg5dmc2q8YhjDmWq/w640-h422/Screenshot+from+2021-01-18+10-13-02.png" width="640" /></a></div><br /><p><br /></p><h3 style="text-align: left;">Thank You for Reading!<br /></h3><div class="blogger-post-footer">Contact Us By Email Or Get in touch by Commenting on posts</div>CodeityWebhttp://www.blogger.com/profile/01377516264362368650noreply@blogger.com0tag:blogger.com,1999:blog-7236090768681897809.post-68467520888715145032021-01-18T09:50:00.006+05:302021-01-30T20:41:57.573+05:30Revising the Select Query I HackerRank Solution | Codeityweb<h1 style="text-align: left;">Revising the Select Query 1</h1><p style="text-align: left;">In this post we are going to discuss SQL hackerrank revising the select query 1 solution.</p><p style="text-align: left;">So here is the problem statement.<br /><br /><br />Query all columns for all American cities in the <b>CITY</b> table with populations larger than 100000. The <b>CountryCode</b> for America is USA. <br /><br />The <b>CITY</b> table is described as follows: </p><p style="text-align: left;"> </p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiiwY1Hd62LRroKftoWnU6WpIV7twKu3dftQyWUxfWYn3bVitKqmF1EGaJdJAkp8NRkbRU9yB8xhSpA6ald51dK4ou1He0sd2Z63yLUHjAk1eF8xU1KBgdJ2CJh1ZjgfE-iOdyebCOaGmS8/s365/1449729804-f21d187d0f-CITY.jpeg" style="margin-left: 1em; margin-right: 1em;"><img alt="City Table" border="0" data-original-height="300" data-original-width="365" height="263" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiiwY1Hd62LRroKftoWnU6WpIV7twKu3dftQyWUxfWYn3bVitKqmF1EGaJdJAkp8NRkbRU9yB8xhSpA6ald51dK4ou1He0sd2Z63yLUHjAk1eF8xU1KBgdJ2CJh1ZjgfE-iOdyebCOaGmS8/w320-h263/1449729804-f21d187d0f-CITY.jpeg" title="Revising the select query 1- Hackerrank" width="320" /></a></div><p></p><p style="text-align: left;"><br /></p><p style="text-align: left;">The solution is easy and straight forward.</p><h1 style="text-align: left;">Sql Statement:</h1><p style="text-align: left;"><span style="color: #2b00fe;">select * from CITY where COUNTRYCODE='USA' and POPULATION>=100000;</span></p><p style="text-align: left;"><br /></p><h1 style="text-align: left;">Explaination:</h1><ul style="text-align: left;"><li>The question says to select all cities where country code is <b>USA</b> and whose population is more than 100000,So we use <b>Select * from city </b>to select all cities from the table.</li><li>Now we give a condition mentioning that we want only those cities whose country code is <b>USA </b>and the population must be greater than or atleast equal to<b> 1 lakh i.e 100000</b></li><li>Finally we run the query to check the outcomes , here is what we get.</li></ul><p><b> </b></p><p><b>Output:</b></p><p><b> </b></p><div class="separator" style="clear: both; text-align: center;"><b><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhAkONrieukM_tJpGAbHT51EsN49YKqposagJ8etmo5Vpr4lmpnl7E_GA0d7MB4wVEIbYBnhIemr93Gd5Z647MgwsVzAALzafBRfKYRL0fTrsASfbGpv1w8jyseFqGJUrVZt2TNnkUjPRK5/s996/Screenshot+from+2021-01-18+09-43-40.png" style="margin-left: 1em; margin-right: 1em;"><img alt="Revising the select query 1- hackerrank solution" border="0" data-original-height="677" data-original-width="996" height="435" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhAkONrieukM_tJpGAbHT51EsN49YKqposagJ8etmo5Vpr4lmpnl7E_GA0d7MB4wVEIbYBnhIemr93Gd5Z647MgwsVzAALzafBRfKYRL0fTrsASfbGpv1w8jyseFqGJUrVZt2TNnkUjPRK5/w640-h435/Screenshot+from+2021-01-18+09-43-40.png" title="Output-Revising the select query 1- hackerrank" width="640" /></a></b></div><b><br /></b><br /><p></p><div><h3 style="text-align: left;">Thank you for Reading!<br /></h3></div><div class="blogger-post-footer">Contact Us By Email Or Get in touch by Commenting on posts</div>CodeityWebhttp://www.blogger.com/profile/01377516264362368650noreply@blogger.com0tag:blogger.com,1999:blog-7236090768681897809.post-2643385778194048782021-01-08T16:29:00.014+05:302021-01-26T10:14:43.534+05:302 Ways To Solve The Pairs Hackerrank Problem | Codeityweb<span style="font-size: medium;"><br /></span><h1 style="text-align: left;"><span style="font-size: x-large;">Pairs Hackerrank</span></h1><span style="font-size: medium;"><br /> In This post we are going to discuss the Pairs Hackerrank Solution .<br /><br /><br /><b>The Problem is as Follows:</b><br /><br />You will be given an array of integers and a target value. Determine the number of pairs of array elements that have a difference equal to a target value. <br /><br />For example, given an array of [1, 2, 3, 4] and a target value of 1, we have three values meeting the condition: 2-1=1, 3-2=1 , 4-3=1<br /><br /> <br /><br /><b>Function Description:</b><br /><br />Complete the pairs function below. It must return an integer representing the number of element pairs having the required difference. <br /><br /><b>pairs has the following parameter(s): </b><br /><br />k: an integer, the target difference<br />arr: an array of integers<br /><br /> <br /><br /> <b>Constraints:</b><br /><br /></span><ul style="text-align: left;"><li><span style="font-size: medium;">2 <= Size of Array <= 10^5</span></li></ul><ul style="text-align: left;"><li><span style="font-size: medium;">0 < k < 10^9</span></li></ul><ul style="text-align: left;"><li><span style="font-size: medium;">0 < arr[i] <= 2^31 -1</span></li></ul><span style="font-size: medium;"> <br /><b><br />Question Definition In Short:</b><br /><br /></span><blockquote><span style="font-size: medium;">You will be given a unsorted array of integers, and a variable K, you have to find the total number of pairs (2 elements) Whose Difference is Equal To K.</span></blockquote><span style="font-size: medium;"><br /><br /><b>Solution:</b><br /><br /> <br /><br /><b>1st Method:</b><br /><br /><span style="color: #bf9000;">The first solution is direct and it will be mostly the solution you will get in short time thinking, the solution could be we compare all pairs in the array and find the difference of them and check if it is equal to K and increment the count.</span><br /><br /> <br /></span><ul style="text-align: left;"><li><span style="font-size: medium;">Array [3,4,2,6] and K=2 </span></li></ul><ul style="text-align: left;"><li><span style="font-size: medium;">We scan the array by comparing every element with all remaining element and finding their difference. </span></li></ul><ul style="text-align: left;"><li><span style="font-size: medium;">So 1st Element is 3 We compare it remaining elements so, Absolute value of |(3-4)|=1 and it is not equal to K=2. So we compare it with next element i.e 2 . </span></li></ul><ul style="text-align: left;"><li><span style="font-size: medium;">|3-2|=1, Not equal to K,compare with next, |3-6|=3 Not equal to K. </span></li></ul><ul style="text-align: left;"><li><span style="font-size: medium;">So We conclude that No pairs are formed with element 3 , now we move to next element i.e 4 </span></li></ul><ul style="text-align: left;"><li><span style="font-size: medium;">Compare 4 with 2 |4-2|=2 Is Equal to K thus increment counter by 1 , Count=1. </span></li></ul><ul style="text-align: left;"><li><span style="font-size: medium;">Next element |4-6|=2 Equal to K , Counter increment by 1, Count=2. </span></li></ul><ul style="text-align: left;"><li><span style="font-size: medium;">Thus 2 Pairs are formed with Element 4. </span></li></ul><ul style="text-align: left;"><li><span style="font-size: medium;">Now Element 2 , |2-6|=4 Not Equal To K , counter remains same Count =2. </span></li></ul><ul style="text-align: left;"><li><span style="font-size: medium;"> Next Element is 6 No more element to compare Thus we Stop Here. </span></li></ul><ul style="text-align: left;"><li><span style="font-size: medium;">Thus Their are Two Pairs (4,2) and (6,4) whose difference is K=2. </span></li></ul><span style="font-size: medium;"> <br /><br />Now Lets Code it and see What we get:<br /><br /> <br /><b><br />Pairs Hackerrank Solution Java:<br /></b><br /><blockquote>-<br /><br /><b>static int pairs(int k, int[] arr) {<br /><br /> int count=0;<br /><br /> for(int i=0;i<arr.length-1;i++)<br /> {<br /> for(int j=i+1;j<arr.length;j++){<br /> if(Math.abs(arr[i]-arr[j])==k)count++;<br /> }<br /> }<br /><br /> return count;<br />}</b><br /> <br /> -</blockquote><br /> <br /><b>We test it On Test Cases:</b><br /> <br /> <br /><br /><a href="https://www.blogger.com/u/5/#"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgOD_f89vNJoVEIVWWI4YpxhL9EHMlRigaGXPXkBviuKQcV0-LPR1kplfGXzJw4frwxYAaOrmYxxMWMqcd9pmyBHe_tTZd6lgNN-Kqn8mtZsA_PcnzDEaYM1Y8a1CXeGlAvCpNmRixPUWnd/w640-h368/Pairs_Hackerrank_solution_java_sampletestcases.png" /></a><br /> <br /><br /><b>All Test Cases:</b><br /> <br /><a href="https://www.blogger.com/u/5/#"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjcxaETt8B9uocTjbmrkEhrNKPYnEtECkvbbDMFGzs7Z8qoIrKdAxMG63n9CvUFBSePzzbeYM9geOqGGXvYxUs3bRMv9jN7jJGTQ21nlyp8mg-2LatfBrpEBsgmLFnUevJGPvF7XyG6ouDf/w640-h404/Pairs_hackerank_timeout.png" /></a><br /> <b><br />Solution 2:</b><br /> <br /><span style="color: #38761d;">It is difficult in beginning to come up to the best solution, but you<br />have to first try to solve the question, and then try to optimized it.<br /> Solution:</span><br /><br /><br />It Is to be noted that it is not given in the question that the pairs should be in order, that means [4,2,6] we can make a pair of (4,2) and (2,4) both will be same.<br />That means We can now modify the Array to reduce the timecomplexity.<br /><br /><br /></span><ul style="text-align: left;"><li><span style="font-size: medium;">First Sort the array.</span></li></ul><ul style="text-align: left;"><li><span style="font-size: medium;">Now take 2 pointers one pointing to first element and 2nd pointing to second element.</span></li></ul><ul style="text-align: left;"><li><span style="font-size: medium;"> Run a loop and check if the difference between the two element is greater or not if it is greater, than it means the pair will be to the right side of the array. So Increment the first pointer and continue the loop.</span></li></ul><ul style="text-align: left;"><li><span style="font-size: medium;">If it is not greater than it means that the pair is between the first pointer and other element that is not included by our second pointer, so we move our second pointer.</span></li></ul><ul style="text-align: left;"><li><span style="font-size: medium;">Now if it is neither greater nor smaller that means it is equal to K, so we increment our count and the second pointer to keep searching for other pairs.</span></li></ul><p><span style="font-size: medium;"><br /><br /><b>Pairs Hackerrank Solution Java:</b><br /><br /><br /></span></p><blockquote><p><span style="font-size: medium;">-<br /><br /><b>static int pairs(int k, int[] arr) {<br /></b></span><b> <span style="font-size: medium;"><br />int count=0;<br />Arrays.sort(arr);<br />int i=0;<br />int j=1;<br />while(j<arr.length)<br />{</span></b></p><p><b><span style="font-size: medium;"><br /> if(arr[j]-arr[i]>k)i++;<br /> else if(arr[j]-arr[i]<k)j++;<br /> else{count++;<br /> j++;}</span></b></p><p><span style="font-size: medium;"><b><br />}<br />return count;<br />}</b><br /></span> <span style="font-size: medium;"><br /></span> <span style="font-size: medium;"></span></p><p><span style="font-size: medium;">-</span></p></blockquote><p><span style="font-size: medium;"><br /><b>Output:</b><br /></span> <span style="font-size: medium;"><br /><br /><a href="https://www.blogger.com/u/5/#"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhSeq78RghWO3QeRaY7GaeSu8hw-QWvYKuFa9HTAB-b6whaga875c3DNPP9AifFYCZPbIt3IwOqpc2eHjJokNDIq6Tb0HjloonkhAcL5BKzEsdbwhlPA-GaK48lH98m1csivrinJcyUtnFy/w640-h446/pairs_hackerrank_solution_java.png" /></a><br /></span> <span style="font-size: medium;"><br /></span> <span style="font-size: medium;"><br /><b>Pairs Hackerrank Solution Python:</b><br /></span> <span style="font-size: medium;"><br /></span> <span style="font-size: medium;"></span></p><blockquote><p><span style="font-size: medium;">-<br /><b><br />def pairs(k, arr):<br /> <span> </span>count=0<br /> <span> </span>arr.sort()<br /> <br /> <span> </span>i=0<br /> <span> </span>j=1<br /> <br /> <span> </span>while j<len(arr):<br /> <span> </span><span> </span>if arr[j]-arr[i]>k:<br /> <span> </span><span> </span><span> </span>i+=1<br /> <span> </span><span> </span>elif arr[j]-arr[i]<k:<br /> <span> </span><span> </span><span> </span>j+=1<br /> <span> </span><span> </span>else:<br /> <span> </span><span> </span><span> </span>count+=1<br /> <span> </span><span> </span><span> </span>j+=1<br /> <span> </span>return count</b></span></p><p><span style="font-size: medium;">-</span></p></blockquote><p><span style="font-size: medium;"><br /></span> <span style="font-size: medium;"><br /></span> <span style="font-size: medium;"><b>Output:</b><br /></span> <span style="font-size: medium;"><br /><a href="https://www.blogger.com/u/5/#"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhSeq78RghWO3QeRaY7GaeSu8hw-QWvYKuFa9HTAB-b6whaga875c3DNPP9AifFYCZPbIt3IwOqpc2eHjJokNDIq6Tb0HjloonkhAcL5BKzEsdbwhlPA-GaK48lH98m1csivrinJcyUtnFy/w640-h446/pairs_hackerrank_solution_java.png" /></a> <br /> The 2nd Solution works because we have first sorted the array, <br />time complexity for sorting O(nlogn) for Algorithm like Merge Sort<br />,Quick Sort.<br />And loop to traverse the element so overall time complexity is<br />O(nlogn)<br />which is less than previous one.<br /></span> <span style="font-size: medium;"><br /></span> <span style="font-size: medium;"><br /><b>Conculsion:</b><br /></span> <span style="font-size: medium;"><br />If the Question is asked in interview or something it is good<br />to tell them the first solution.<br />And if they asked to optimized it then you can try to <br />tell them the other solutions.<br /><br /></span> <span style="font-size: medium;"><br /><b><span style="font-size: large;">Hope You Like It. Thank You For Reading!</span></b><br /></span> </p><div class="blogger-post-footer">Contact Us By Email Or Get in touch by Commenting on posts</div>CodeityWebhttp://www.blogger.com/profile/01377516264362368650noreply@blogger.com0tag:blogger.com,1999:blog-7236090768681897809.post-50773654701019406762021-01-06T19:07:00.004+05:302021-01-26T10:15:00.598+05:30How to Implement Bubble Sort In Python | Codeityweb<h1 style="text-align: left;"><span style="font-size: x-large;">Bubble Sort In Python</span></h1><p><span style="font-size: medium;"> </span></p><p><span style="font-size: medium;"><img height="127" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgShE0tSPg6AEUCtIHmMY-aaor4ldxRNDWazZ51ePZNu_TwFHsIZUFF484WVpDC3wy8H0AEPS_2CMQ3PHy9Eq2SlqPQZ2D4VVnsofwFGN8k2MSEHoNvYWCUq8IzyNu0fHuxvpXjxEHSb5v1/w200-h127/bubblesort_level3_code_python.png" style="display: none;" width="200" /></span></p><p><span style="font-size: medium;"><br /> </span></p><p><span style="font-size: medium;">We already have a post on in depth of bubble sort with code in java and comparison of different levels of bubble sort <a href="https://codeityweb.blogspot.com/2020/09/bubble-sort-bubble-sort-is-most-easy.html">Click Here </a><br /><br /><span style="color: #38761d;">In this post we will see how Bubble sort is implemented in python<br /><br />We will use Bubble Sort of 3 level</span><br /><br />In Bubble sort after every iteration <br />the largest number is positioned at its appropriate end position. <br /><br /></span> <span style="font-size: medium;"><br /><b><span style="font-size: large;">Algorithm:</span></b></span></p><p><span style="font-size: medium;"><br /></span></p><ul style="text-align: left;"><li><span style="font-size: medium;">We run two loops the outer loop will be for iterating on the array and the inner loop will perform the comparison and swapping part.</span></li></ul><ul style="text-align: left;"><li><span style="font-size: medium;">If the next element is smaller than the current element than we swap it with current element. </span></li></ul><p><span style="font-size: medium;"><br /></span> <span style="font-size: medium;"><br /><b>1. Level 1</b></span></p><p><span style="font-size: medium;"></span></p><blockquote><p><b><span style="font-size: medium;"> -</span></b></p><p><b><span style="font-size: medium;">def bubbleSortLevel1(arr):<br /> <span> </span>iterations=0<br /> <span> </span>for i in range(0,len(arr)):<br /> <span> </span><span> </span>for j in range(0,len(arr)-1):<br /> <span> </span><span> </span><span> </span>if arr[j]>arr[j+1]:<br /> <span> </span><span> </span><span> </span><span> </span>arr[j],arr[j+1]=arr[j+1],arr[j]<br /> <span> </span><span> </span><span> </span>iterations+=1<br /><br /> <span> </span><span> </span><span></span>print("i=",i," " ,arr)<br /><br /> <span> </span>return iterations<br /><br /><br /></span></b> <b><span style="font-size: medium;">-</span></b></p></blockquote><p><span style="font-size: medium;"><br /><br /><b>Output:</b><br /><br /><br /><a href="https://www.blogger.com/u/5/#"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjFN3VR8I2vOhnbw9lxNa-E47OFN4OMM90wn2lRVcNz7eock6SJxcgsIM8Nt9eKVC8FCUiS3poPHMQ_O6S6yJkwnWL-83M7bIl0MerYEm2jPvQEakrOoAJ4M1xRL2eZJdpEkQX-6yENnahT/w640-h221/bubblesortlevel1python.png" /></a><br /><br /><br /><br /><b>You Can see here that the total iterations made are 72</b><br /><br /><br /><br /><br /><br /><b>2 Level 2:</b><br /><br /><br /><span style="color: #38761d;">In this level we add outer loop iterator in the condition of for loop , because you can see that after every iterations the highest number is positioned to its appropriate end position, thus to avoid extra comparisons we reduce the number of iterations of inner loop.</span> <br /></span></p><p><span style="font-size: medium;"></span></p><blockquote><b><span style="font-size: medium;">-<br /></span></b> <b><span style="font-size: medium;"><br />def bubbleSortLevel2(arr):<br /> <span> </span>iterations=0<br /> <span> </span>for i in range(0,len(arr)):<br /> <span> </span><span> </span>for j in range(0,len(arr)-i-1):<br /> <span> </span><span> </span><span> </span>if arr[j]>arr[j+1]:<br /> <span> </span><span> </span><span> </span><span> </span>arr[j],arr[j+1]=arr[j+1],arr[j]<br /> <span> </span><span> </span><span> </span><span> </span>iterations=iterations+1<br /> <span> </span><span> </span>print("i=", i, " ", arr)<br /><br /> <span> </span>return iterations<br /><br /><br /><br /></span></b> <b><span style="font-size: medium;">-</span></b></blockquote><span style="font-size: medium;"><br /><b><br />Output:</b><br /><br /><br /><a href="https://www.blogger.com/u/5/#"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgQFjEeZUixs9GcAPx4VsWpjDpYV5ECl3udyeoWguQ0frhABgI9nTBfvelTD8-G05JGnWz-DU6s7UAPXBaZkKWTr1frieSj21ib2Azc2FVlwJ_LZVnr6THDg1g97iYgR4DwtdGgyhL-aqYM/w640-h288/bubblesortlevel2python.png" /></a><br /><br /></span> <span style="font-size: medium;"><br /><br /> Now as you can see that just be doing simple change the number of iterations are reduced to half in this case.<br /><br /><br />Now imagine if the search space is huge than their will be a drastic decrease in the time consumption <br /><br /></span> <span style="font-size: medium;"><br /><br /><b>3 Level 3:</b><br /><br />In this level we use a flag variable to avoid extra comparison if the array is already sorted .<br /><br /></span> <span style="font-size: medium;"></span><blockquote><b><span style="font-size: medium;">-<br /><br />def bubbleSortLevel3(arr):<br /> <span> </span>iterations=0<br /> <span> </span>for i in range(0,len(arr)):<br /> <span> </span><span> </span>flag=True<br /> <span> </span><span> </span>for j in range(0,len(arr)-i-1):<br /> <span> </span><span> </span><span> </span>if arr[j]>arr[j+1]:<br /> <span> </span><span> </span><span> </span><span> </span>flag=False<br /> <span> </span><span> </span><span> </span><span> </span>arr[j],arr[j+1]=arr[j+1],arr[j]<br /> <span> </span><span> </span><span> </span><span> </span>iterations=iterations+1<br /> <span> </span><span> </span>if flag:<br /> <span> </span><span> </span><span> </span>break<br /><br /> <span> </span><span> </span>print("i=", i, " ", arr)<br /><br /> <span> </span>return iterations<br /><br /><br /><br /></span></b> <b><span style="font-size: medium;">-</span></b></blockquote><span style="font-size: medium;"><br /><br /><span style="font-size: large;"><b><br />Comparison of Above Three Levels:</b></span><br /></span> <span style="font-size: medium;"><br /><br />lets say we have the list as <br />arr = [-98, -35, -15, 0, 2, 4, 6, 9, 19]<br /><br /></span><span style="font-size: medium;"><br />Now lets take a look how the different levels behave on this input list<br /><br />We have used Sorted List to see how these techniques perform if the list is already sorted .<br /><br />Ideally their should be no swapping , so lets check <br /><br /><br /><br /><br /><b>1 Level 1:</b><br /><br /></span> <span style="font-size: medium;"><a href="https://www.blogger.com/u/5/#"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgdqjVP-itAUQdueOkdwQvpgWE5DMhz-yDigL0pJFJlXZKQkDxG6Rq6DNEiJki2sHLUotVSvzsYB4ji5Lk4MrZcHtOVf4xRp2qScltIII5Tm0osk6un7evNcp2pO5tDIn6TYAYmtRgcCYT8/w640-h269/bubblesortlevel1python_comp.png" /></a><br /><br /><br /><br /> Total Iterations performed in level 1 are 72<br /><br /></span> <span style="font-size: medium;"><br /><br /><b>2 Level 2:</b><br /><br /><br /><a href="https://www.blogger.com/u/5/#"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhhm9JsLaApAph6O0co_8sI8yJYoTgWNrOzCFZJH61Od5AdTcXLtmw8g-d92SQDpJ-zQCtLxUoGUGYoIWqnXihzQYPhz9vxi5_cK9waB2Mk3yvz7BV41jH9p1ZJQXVeTF0qxfZQ-YsCWVCH/w640-h244/bubblesortlevel2python_comp.png" /></a><br /><br /><br /><br />Total Iterations performed in level 2 are 36<br /><br /><br /><b>3 Level 3:</b><br /><br /><br /><a href="https://www.blogger.com/u/5/#"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjtUKVj0sYkINAb_kmiCbrFlXL9YJ9jqNuKjy23oLR2jY3YC9lhJIeDtvtYn5pyxFhgBvHsAmz7pVplkdoKDpzqzs8R3fpUq3rloMLa_EBHn-nrCCA9kuXGXG2-2nUkVDSISvux0GTKPOge/w640-h121/bubblesortlevel3python_comp.png" /></a><br /><br /></span> <span style="font-size: medium;"><br /><br /> <b>Total Iterations performed in level 3 are 8 </b><br /><br />so we can see that even if the list is sorted the level 1 and level 2 perform unnecessary comparisons ,where as in level 3 the comparisons are reduced thus level 3 is more efficient as compared to others.<br /><br /></span><span style="font-size: medium;"><br /><br /><span style="font-size: large;"><b>Related Posts:</b></span><br /><br /></span> <span style="font-size: medium;"><br /></span><p></p><ul style="text-align: left;"><li><span style="font-size: medium;"><a href="https://codeityweb.blogspot.com/2020/09/bubble-sort-bubble-sort-is-most-easy.html">Bubble Sort In Java</a> </span></li></ul><ul style="text-align: left;"><li><span style="font-size: medium;"><a href="https://codeityweb.blogspot.com/2020/12/selection-sort.html">How to Implement Selection Sort</a> </span></li></ul><p><span style="font-size: medium;"><br /></span> <span style="font-size: medium;"><br /><br /><b><span style="font-size: large;">Conclusion:</span></b><br /></span> <span style="font-size: medium;"><br />Thus we can use any Sorting level for sorting if the search space that is the array in this case is small.But if the space is large enough then it is good to use level 2 or level 3 sorting technique <br /><br /></span></p><div class="blogger-post-footer">Contact Us By Email Or Get in touch by Commenting on posts</div>CodeityWebhttp://www.blogger.com/profile/01377516264362368650noreply@blogger.com0tag:blogger.com,1999:blog-7236090768681897809.post-24547999902038334442021-01-06T18:56:00.006+05:302021-01-26T10:15:21.909+05:30Everything You Need To Know About Selection Sort | Codeityweb<div><div><h2 style="text-align: left;">How to Implement <b>Selection Sort In Python and Java<br /></b></h2><p style="text-align: left;"><span style="font-size: medium;">Selection sort is a sorting algorithm in which we select the minimum or maximum element from the array and place it to its appropriate starting position in every iteration. <br /></span></p><p style="text-align: left;"> </p><h3 style="text-align: left;"><span style="font-size: large;"><b>Algorithm:</b></span></h3><ul style="text-align: left;"><li><span style="font-size: medium;"><span style="color: #0b5394;"><b>"i"</b> </span>is the iterator of outer loop and <b>"j" </b>is the iterator of inner loop.<br /></span></li><li><span style="font-size: medium;">We run two loops outer loop will run from<b> 0 to length of the array </b>and the inner loop will run from<b> i+1 </b>to length of array <b>"i "</b> is the iterator of outer loop</span></li><li><span style="font-size: medium;">At every iteration we select <b>"i"th</b> element as <b>minimum element.</b></span></li><li><span style="font-size: medium;">In the inner loop we check if the <b>"j" th </b>element <b>is smaller</b> than the minimum element then we make the minimum element as "j" th element.</span></li><li><span style="font-size: medium;">After the iteration we swap the values of<b> "i" th</b> element and minimum element.</span></li><li><span style="font-size: medium;">We repeat the steps until the array is sorted.</span></li></ul> <span><a name='more'></a></span></div><h3 style="text-align: left;"><span style="font-size: large;">Working of Selection Sort:</span></h3><p><span style="font-size: medium;">To Understand the technique properly lets run the algorithm on sample array<br /><b><br /> arr = [15, 10, 4, 20, 1] </b></span></p><p><span style="font-size: medium;">In our <b><a href="https://codeityweb.blogspot.com/2020/09/bubble-sort-bubble-sort-is-most-easy.html" target="_blank">Bubble Sort</a> Post </b>we have explained the working using an example of students of different height .</span></p><p><span style="font-size: medium;">Their students asks each other and compare their heights and if their is proper comparison then they swap their positions , and finally at the end the students are sorted as per their heights.</span></p><p><span style="font-size: medium;">So here as well we will consider the same example , but In this case their is a monitor who will sort the students and not the students themselves.</span></p><span><!--more--></span><p><b><br /></b></p><p><span style="font-size: large;"><b>Initial Students position:</b></span></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj2MIrdLVXyu9LQCzBl7GCnd378_H8RM1fbYXDulBmXEAGSbkNUb-YgkcJv0sVOvkwfi9MaV1jG2ZqaD-PmbhMwMZZC5XITeg5g1V6dtIK-C0sb1i3Neyw3Hv5ntaHZx3gFiCCv9cfHgELZ/s414/sinitial.png" style="margin-left: 1em; margin-right: 1em;"><img alt="Selection sort in python and java" border="0" data-original-height="258" data-original-width="414" height="319" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj2MIrdLVXyu9LQCzBl7GCnd378_H8RM1fbYXDulBmXEAGSbkNUb-YgkcJv0sVOvkwfi9MaV1jG2ZqaD-PmbhMwMZZC5XITeg5g1V6dtIK-C0sb1i3Neyw3Hv5ntaHZx3gFiCCv9cfHgELZ/w640-h319/sinitial.png" width="640" /></a></div><p></p><p><span style="color: #38761d;"><b><br /></b></span></p><p><span style="color: #38761d;"><b>So now the monitor arrives and see the students now he decides to use Selection Sort to Sort the students from smallest to highest heights.</b></span></p><p><b> </b></p><p><span style="font-size: large;"><b>Iteration 1:</b></span></p><ul style="text-align: left;"><li><span style="font-size: medium;">Now the monitor don't have access to all students heights so he considers 1st student<b> Height 15 unit </b>as the smallest height student and he starts comparing his height with the 2nd student <b> Height 10 unit </b>he sees that he is smaller than the previous smaller height student so the considers student with <b>Height 10 unit as smallest student.</b></span></li><li><span style="font-size: medium;">Again the monitor compares height of 3rd student with the minimum height student , He found that he is again smaller than the previous student. <b>4<10 , </b>So he make him as smallest height student.</span></li><li><span style="font-size: medium;"> He check Height 20 with minimum so far i.e 4 , <b>but 20>4</b> so the <span style="color: #134f5c;"><b>keep 4 as smallest. <br /></b></span></span></li><li><span style="font-size: medium;">Finally he reaches the last student and compare his height with the minimum height , he sees that student with <b>Height 1 unit</b> is smaller than Minimum height student so far, so he <b>considers him as smallest.</b> </span></li><li><span style="font-size: medium;">So now he has checked all students height and found the student with <b>Height 1 unit is smallest </b>, so he swaps his position with the student at 1st position because he was looking for 1st position only.</span></li></ul> <p><span style="font-size: medium;"><span style="color: #38761d;">Thus after first Iteration the students become:As you can see now the smallest height student is at its right position.</span></span><br /></p><p></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh_3vaiC_sFrTVi-ey8E08J_z_Oj0Pp3zinybkv1paJm7y1e5KWlDy56NzQndE6kF5ahNZNHgMaejBv6LxyjuN5zyj42jDYQ5OYxNAJw5orr-WkCRFYjDg_WtbAVl4DcjdDn06QrMyHtiTa/s418/s1.png" style="margin-left: 1em; margin-right: 1em;"><img alt="Selection sort in python and java" border="0" data-original-height="261" data-original-width="418" height="309" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh_3vaiC_sFrTVi-ey8E08J_z_Oj0Pp3zinybkv1paJm7y1e5KWlDy56NzQndE6kF5ahNZNHgMaejBv6LxyjuN5zyj42jDYQ5OYxNAJw5orr-WkCRFYjDg_WtbAVl4DcjdDn06QrMyHtiTa/w640-h309/s1.png" width="640" /></a></div><br /> <br /><p></p><p><span style="font-size: large;"><b>Iteration 2:</b></span></p><ul style="text-align: left;"><li><span style="font-size: medium;">Now the monitor searches for the student <b>who can fit at the position 2</b> ,so he repeats the same process.</span></li><li><span style="font-size: medium;">He found that <b>Height 4 unit </b>is the next smallest height so he places him their.</span></li></ul></div><p><span style="font-size: medium;"><span style="color: #38761d;">Thus after Second Iteration the students become:As you can see now the next smallest height student is at its right position.</span></span></p><p></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiDG0VwRpyB3iGg4L6OGuFkxc0dJ04WurcgqiprzRuaoC6mdy5hcPYwndJCjBsTNkT8EykjmExPfbYgk6gn2nqzoKxpUgNB3UJxTAQ9osP6vSwmzUUvI_RO9oGL9zlk9ozUy2BkBTXI1Lvm/s416/s3.png" style="margin-left: 1em; margin-right: 1em;"><img alt="Selection sort in python and java" border="0" data-original-height="273" data-original-width="416" height="324" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiDG0VwRpyB3iGg4L6OGuFkxc0dJ04WurcgqiprzRuaoC6mdy5hcPYwndJCjBsTNkT8EykjmExPfbYgk6gn2nqzoKxpUgNB3UJxTAQ9osP6vSwmzUUvI_RO9oGL9zlk9ozUy2BkBTXI1Lvm/w640-h324/s3.png" width="640" /></a></div><br /><span style="font-size: large;"> </span><p><span style="font-size: large;"><b>Iteration 3: </b></span></p><p><span style="font-size: medium;">In this iteration he finds the student to fit at position 3 since student with <b>Height 10 unit</b> is the right student and he is already at his right position, so their is no swapping needed.</span></p><p> </p><p><span style="font-size: large;"><b>Iteration 4:</b></span></p><p> </p><p><span style="font-size: medium;">Now similarly he finds right student at position 4 and places him their.</span></p><p> </p><p></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhruo8cEpLw4vzzb3XAFF3PfWkq_xKMBFHby2J_0db_YmGgaLclyY7FJUXVhxNKzZGUdxX95A4zHQtyU3UjWQEDnOTdb9sF8VjYi-IV-LiOfMIgsqc-o2B5DmIvV7jf3q5WPLe3VqyCXzJz/s420/s4.png" style="margin-left: 1em; margin-right: 1em;"><img alt="Selection sort in python and java" border="0" data-original-height="270" data-original-width="420" height="318" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhruo8cEpLw4vzzb3XAFF3PfWkq_xKMBFHby2J_0db_YmGgaLclyY7FJUXVhxNKzZGUdxX95A4zHQtyU3UjWQEDnOTdb9sF8VjYi-IV-LiOfMIgsqc-o2B5DmIvV7jf3q5WPLe3VqyCXzJz/w640-h318/s4.png" width="640" /></a></div><p><b> </b></p><p><span style="font-size: medium;"><b>Iteration 5:</b></span></p><p><span style="font-size: medium;"> As all students are at their appropriate position no swapping is done in this iteration and Now you can see that all students are sorted in ascending order of their heights.</span></p><h3 style="text-align: left;"> </h3><h3 style="text-align: left;"><span style="font-size: large;"> Selection Sort Java:</span></h3><h3 style="text-align: left;"></h3><br /><br /><b><span style="font-size: medium;"><blockquote>-<br />public class SelectionSort{<br /><br /> <br />public void sort(int arr[]) {<br /> <span> </span>int min = 0;<br /> <span> </span>for (int i = 0; i < arr.length; i++) {<br /> <span> </span><span> </span>min = i;<br /> <span> </span><span> </span>for (int j = i + 1; j < arr.length; j++) {<br /> <span> </span><span> </span>if (arr[j] < arr[min]) {<br /> <span> </span><span> </span>min = j;<br /> <span> </span>}<br /> }<br /> <span> </span>int temp = arr[i];<br /> <span> </span>arr[i] = arr[min];<br /> <span> </span>arr[min] = temp;<br /> <span> </span>System.out.println("Iteration : " + (i + 1));<br /> <span> </span>print(arr);<br /> <span> </span>}<br /> }<br /><br /><br /> public static void print(int arr[]) {<br /><br /> <span> </span>for (int i : arr) {<br /> <span> </span>System.out.print(i + " ");<br /> <span> </span>}<br /> <span> </span>System.out.println();<br /> <span> </span>System.out.println();<br /> }<br /><br /><br /> public static void main(String[] args) {<br /><br /> <span> </span>int arr[] = { 15, 10, 4, 20, 1 };<br /> <span> </span>System.out.print("Array Before : ");<br /> <span> </span>print(arr);<br /> <span> </span>new SelectionSort().sort(arr);<br /> <span> </span>System.out.print("Array After: ");<br /> <span> </span>print(arr);<br /> <span> </span>}<br />}<br /><br /><br /><br /> -</blockquote></span></b><p></p><h3 style="text-align: left;">Output:</h3><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRYg65ohtlyJVqfn0Gl-eRGVEStubRZ3ga8m_GjWzT-L5lQY8RzUN2lS5DH8VoFUM6RYLn76l9QpODqDHCRoh4YjgOGrgpd3up2sk7raeKPovS2_45D7bdE4yTM9dwKWeh4LTOVKtTpevr/s539/selectionsort_output_java.png" style="margin-left: 1em; margin-right: 1em;"><img alt="Selection sort java output" border="0" data-original-height="406" data-original-width="539" height="382" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRYg65ohtlyJVqfn0Gl-eRGVEStubRZ3ga8m_GjWzT-L5lQY8RzUN2lS5DH8VoFUM6RYLn76l9QpODqDHCRoh4YjgOGrgpd3up2sk7raeKPovS2_45D7bdE4yTM9dwKWeh4LTOVKtTpevr/w640-h382/selectionsort_output_java.png" width="640" /></a></div><br /><p><br /></p><h3 style="text-align: left;"><br /></h3><h3 style="text-align: left;"><span style="font-size: large;">Selection Sort Python:</span><br /></h3><b><span style="font-size: medium;"></span></b><blockquote><b><span style="font-size: medium;">-<br />def selectionSort(arr):<br /> <span> </span>for i in range(len(arr)):<br /> <span> </span><span> </span>min = i<br /> <span> </span><span> </span>for j in range(i+1, len(arr)):<br /> <span> </span><span> </span><span> </span>if arr[j] < arr[min]:<br /> <span> </span><span> </span><span> </span><span> </span>min = j<br /> <span> </span><span> </span><span> </span><span> </span>arr[i], arr[min] = arr[min], arr[i]<br /> <span> </span><span> </span><span></span>print(arr)<br /><br /><br />if __name__ == "__main__":<br /> <span> </span>arr = [15, 10, 4, 20, 1]<br /> <span> </span>print("List Before Sorting:", arr)<br /> <span> </span>selectionSort(arr)<br /> <span> </span>print("List After Sorting", arr)</span></b><p><b><span style="font-size: medium;"> -</span></b></p></blockquote><p><b><span style="font-size: medium;"></span></b></p><h3 style="text-align: left;"><b> Output:</b></h3><p> </p><p></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgSFUDRb6Ax9IwW9hGoxPifvh1IYZMjF_HpsTB-85JkgWpyKOK3slAocFQYAnnoAk-e7jBQ_Dzrkjr094X1rCWAOLLVqP4HHNBye3TfQQb2igFOyBKwAjXze0PoqfzYYzhskfNXPDYT6Y6z/s504/selectionsort_output_python.png" style="margin-left: 1em; margin-right: 1em;"><img alt="Selection sort in python output" border="0" data-original-height="157" data-original-width="504" height="184" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgSFUDRb6Ax9IwW9hGoxPifvh1IYZMjF_HpsTB-85JkgWpyKOK3slAocFQYAnnoAk-e7jBQ_Dzrkjr094X1rCWAOLLVqP4HHNBye3TfQQb2igFOyBKwAjXze0PoqfzYYzhskfNXPDYT6Y6z/w640-h184/selectionsort_output_python.png" width="640" /></a></div><br /><p></p><table class="infobox"><tbody><tr><td><br /></td></tr><tr><th scope="row"><br /></th><td><br /></td></tr></tbody></table><p><span style="font-size: large;"><b>Parameter Check:</b></span></p><p><span style="font-size: large;"><b> </b></span></p><p><b>Worst Case Performance(Time-Complexity)</b>: О(<i>n</i><sup>2</sup>),Due to Two nested loops to <span> </span><span> </span><span> </span><span> </span><span> </span><span> </span><span> </span><span> </span><span> </span><span> </span><span> </span><span> </span><span> </span><span> </span><span> </span><span> </span><span> </span><span> </span><span> </span><span> </span>understand it in short.</p><p><b>Worst Case Space Complexity(Space-Complexity)</b>: O(1) auxiliary Space</p><p><b>Is it Stable:</b> No</p><p> <br /></p><p><span style="font-size: large;"><b> </b></span></p><p><span style="font-size: large;"><b>Related Posts:</b></span></p><ul style="text-align: left;"><li><a href="https://codeityweb.blogspot.com/2020/09/bubble-sort-bubble-sort-is-most-easy.html" target="_blank">Bubble Sort</a> </li></ul><ul style="text-align: left;"><li><a href="https://codeityweb.blogspot.com/2020/12/bubble-sort-python.html" target="_blank">Bubble Sort In Python</a></li></ul><p><b> </b></p><p><b> </b></p><h4 style="text-align: left;"><span style="font-size: large;"><b>Conclusion:</b></span></h4><p><span style="color: red;"><span style="font-size: medium;">In this way we can sort arrays in java and
python , sorting string array can also be done using the same code just
the difference will be in the comparison of two strings as it is
different then comparing two integers.</span></span></p><p> </p><p> <br /></p><div class="blogger-post-footer">Contact Us By Email Or Get in touch by Commenting on posts</div>CodeityWebhttp://www.blogger.com/profile/01377516264362368650noreply@blogger.com0tag:blogger.com,1999:blog-7236090768681897809.post-15365721705852668552020-12-30T12:00:00.006+05:302021-01-03T15:42:58.082+05:30Bubble Sort<h1 style="text-align: left;">Bubble Sort</h1><p style="text-align: left;">Bubble sort is the most easy sorting algorithm in which we compare adjacent elements and swap them depending to our conditions.</p><p style="text-align: left;">suppose our array is <b>int arr[]= {10,8,9,5,7,6,3,0,1,-3,-6,-9};</b> </p><p style="text-align: left;"> we want to sort the array in ascending order then after sorting the array would look like <b>{-9 ,-6 ,-3, 0, 1 ,3 ,5, 6, 7, 8 ,9, 10 };</b><span style="color: #2b00fe;"><br /></span></p><p><span style="color: #2b00fe;"><b>We will use Bubble Sort of 3 level</b></span></p><p><b>Algorithm:</b></p><ol style="text-align: left;"><li>We run two loops the outer loop will be for iterating on the array and the inner loop will perform the comparison and swapping part.</li><li>If the next element is smaller than the current element than we swap it with current element.</li></ol><span><a name='more'></a></span> <h4 style="text-align: left;"><b>Working of Bubble Sort:</b></h4><div style="text-align: left;">lets take array as <br /><div style="background-color: #011627; color: #d6deeb; font-family: "Droid Sans Mono", "monospace", monospace, "Droid Sans Fallback"; font-size: 16px; font-weight: normal; line-height: 22px; white-space: pre;"><div><span style="color: #d6deeb;"> arr </span><span style="color: #c792ea;">=</span><span style="color: #d6deeb;"> </span><span style="color: #d9f5dd;">[</span><span style="color: #f78c6c;">5</span><span style="color: #d6deeb;">, </span><span style="color: #f78c6c;">3</span><span style="color: #d6deeb;">, </span><span style="color: #f78c6c;">4</span><span style="color: #d6deeb;">, </span><span style="color: #f78c6c;">2</span><span style="color: #d6deeb;">, </span><span style="color: #f78c6c;">1</span><span style="color: #d9f5dd;">]</span></div></div></div><p>If we plot the array it look like this<br /> <br /></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiT0h-HjiRgMfKMGF7cOtqCBVSaK19c8OPiCha-dOKfDhTicT8hRY0yyqxrp_12M1HCUEEHaGfRoBI9VQDB-LCGP2E2_q0AadgbshBh5Kbbhnw-pcwiQ4T1Bq6o-WIs5nQvHjGpZDLK8SgB/s443/binitial.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="266" data-original-width="443" height="289" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiT0h-HjiRgMfKMGF7cOtqCBVSaK19c8OPiCha-dOKfDhTicT8hRY0yyqxrp_12M1HCUEEHaGfRoBI9VQDB-LCGP2E2_q0AadgbshBh5Kbbhnw-pcwiQ4T1Bq6o-WIs5nQvHjGpZDLK8SgB/w640-h289/binitial.png" width="640" /></a></div><p></p><div><p><span style="color: #38761d;">Now to understand the working properly consider this elements as students with the respective height in units.</span></p><p><span style="color: #38761d;"><span style="color: black;"><b>Iteration 1: </b></span><br /></span></p><ul style="text-align: left;"><li><span>In first iteration student 1 asks student 2 that Am i greater than you? He says yes, So they swap their position.<b>arr=[3,5,4,2,1]</b></span></li><li><span>Now student with height 5 asks student with height 4 Am i greater than you ? He says yes! So they swap their position.<b> arr[3,4,5,2,1]</b></span></li><li><span>Again 5 asks 2 , and 5>2 , so they swap .<b> arr[3,4,2,5,1]</b></span></li><li><span style="color: #38761d;"><span style="color: black;">5>1 so They swap position.<b> arr=[3,4,2,1,5]</b></span><b><br /></b></span></li></ul><span style="color: #0b5394;"><span><b>Thus after First Iteration the array becomes arr=[3,4,2,1,5]</b></span></span></div><div><span style="color: #0b5394;"><span><b> </b></span></span></div><div><span style="color: #38761d;"><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhpwmrLCuE7AHYm7l_YHhUQf2RnDG0VL33jcGdnd2zYS3gLb5jVIPjFRSQ98GG2PYE-Tr-eKC4ny4R8hm-tpAy1riLoNJIZWoE3CNyngoK3w4g-pRhdwBW5gE7KSf2t5nGkiwYRyMNSP_NT/s481/b1.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="274" data-original-width="481" height="327" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhpwmrLCuE7AHYm7l_YHhUQf2RnDG0VL33jcGdnd2zYS3gLb5jVIPjFRSQ98GG2PYE-Tr-eKC4ny4R8hm-tpAy1riLoNJIZWoE3CNyngoK3w4g-pRhdwBW5gE7KSf2t5nGkiwYRyMNSP_NT/w640-h327/b1.png" width="640" /></a></div><br /><span><!--more--></span><b><br /></b></span></div><div><b><span>Iteration 2:</span></b></div><div><b><span> </span></b></div><div><ul style="text-align: left;"><li>The array is now [3,4,2,1,5], similarly 3 asks 4 Am i greater?He says No.So they don't swap their position.</li><li>4>2 , yes, so they swap their position.</li><li>Similarly after repeating the same condition, the next highest height student come to its right position.</li></ul> </div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiO29Ub27fbMQP7t3V53lHZUuJggvRsPttTwYNYxFeG_S4DjfmsIZH841lojgNaX2aPDHIR9GA4p0xbDQNyeW8bNrRdUtUC4xBOLMc-66yIm8VEzE2RBgwJhToP92eLruzmqHYCuUb-VSe_/s418/b2.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="274" data-original-width="418" height="322" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiO29Ub27fbMQP7t3V53lHZUuJggvRsPttTwYNYxFeG_S4DjfmsIZH841lojgNaX2aPDHIR9GA4p0xbDQNyeW8bNrRdUtUC4xBOLMc-66yIm8VEzE2RBgwJhToP92eLruzmqHYCuUb-VSe_/w640-h322/b2.png" width="640" /></a></div><br /><div style="text-align: left;"><b> </b></div><div style="text-align: left;"><b>Iteration 3:</b></div><div style="text-align: left;"><b> </b></div><div style="text-align: left;"><b> </b></div><div style="text-align: left;"> </div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEirrKMA-FajAD9K1Pooku-Bp6DTGc0Z8wfmgL0QQrO0eyzTihP98VY9iY0Rsj-Tzt3BAl05cXs4TyUSjkbu_w7bolTiQvOr2f5y3KDgglBnDCOd_XTJlff397d4oNj-JPU6Vysb3BwcYKqk/s410/b3.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="276" data-original-width="410" height="296" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEirrKMA-FajAD9K1Pooku-Bp6DTGc0Z8wfmgL0QQrO0eyzTihP98VY9iY0Rsj-Tzt3BAl05cXs4TyUSjkbu_w7bolTiQvOr2f5y3KDgglBnDCOd_XTJlff397d4oNj-JPU6Vysb3BwcYKqk/w640-h296/b3.png" width="640" /></a></div><div style="text-align: left;"><b> </b></div><div style="text-align: left;"><b>Iteration 4:</b></div><div style="text-align: left;"><b> </b></div><div style="text-align: left;"><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgiwXD__2kcaft8l-5skrM0kfAajH5LvgxzQ66uEl_0tim65EdsCH7NC6FmJBGf3aA_Uz46jEKMoMoS4DdvRDDUAZ0aFyn9ogmNTWQR53gzyYqE95giDKLUyPthuZGFHX07bZ5yKTAI9puP/s423/b5.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="273" data-original-width="423" height="344" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgiwXD__2kcaft8l-5skrM0kfAajH5LvgxzQ66uEl_0tim65EdsCH7NC6FmJBGf3aA_Uz46jEKMoMoS4DdvRDDUAZ0aFyn9ogmNTWQR53gzyYqE95giDKLUyPthuZGFHX07bZ5yKTAI9puP/w640-h344/b5.png" width="640" /></a></div><div style="text-align: left;"><br /></div><div style="text-align: left;"><br /></div><div><p><span style="color: #38761d;"><b>Its that simple algorithm, but we use bubble sort of 3 different levels to reduce the time required to sort the array</b></span></p><p><span style="color: #04ff00;"><b><span style="color: #38761d;">In every iteration the largest number is positioned at its appropriate end position. </span></b></span></p><p><span style="color: #04ff00;"><b><span style="color: #38761d;"><span></span></span></b></span></p><!--more--><p></p><p><span style="color: #04ff00;"><span style="color: black;"><b>1.Level 1</b></span></span></p><div style="background-color: white; color: black; font-family: "Droid Sans Mono", "monospace", monospace, "Droid Sans Fallback"; font-size: 16px; font-weight: normal; line-height: 22px; white-space: pre;"><div><span style="color: blue;">public</span><span style="color: black;"> </span><span style="color: blue;">int</span><span style="color: black;"> bubbleSortLevel1(</span><span style="color: blue;">int</span><span style="color: black;">[] arr) {</span></div><div><span style="color: black;"> </span><span style="color: blue;">int</span><span style="color: black;"> total_iterations=</span><span style="color: #098658;">0</span><span style="color: black;">;</span></div><div> <span style="color: blue;">for</span><span style="color: black;">(</span><span style="color: blue;">int</span><span style="color: black;"> i=</span><span style="color: #098658;">0</span><span style="color: black;">;i<arr.length;i++)</span></div><div><span style="color: black;"> {</span></div><div><span style="color: black;"> </span><span style="color: blue;">for</span><span style="color: black;">(</span><span style="color: blue;">int</span><span style="color: black;"> j=</span><span style="color: #098658;">0</span><span style="color: black;">;j<arr.length-</span><span style="color: #098658;">1</span><span style="color: black;">;j++)</span></div><div><span style="color: black;"> {</span></div><div><span style="color: black;"> </span><span style="color: blue;">if</span><span style="color: black;">(arr[j]>arr[j+</span><span style="color: #098658;">1</span><span style="color: black;">])</span></div><div><span style="color: black;"> {</span></div><div><span style="color: black;"> </span><span style="color: blue;">int</span><span style="color: black;"> temp=arr[j];</span></div><div><span style="color: black;"> arr[j]=arr[j+</span><span style="color: #098658;">1</span><span style="color: black;">];</span></div><div><span style="color: black;"> arr[j+</span><span style="color: #098658;">1</span><span style="color: black;">]=temp;</span></div><div><span style="color: black;"> }</span></div><div><span style="color: black;"> total_iterations++;</span></div><div><span style="color: black;"> }</span></div><div><span style="color: black;"> System.out.print(</span><span style="color: #a31515;">"i="</span><span style="color: black;">+i+</span><span style="color: #a31515;">" : "</span><span style="color: black;">);</span></div><div><span style="color: black;"> print(arr);</span></div><div><span style="color: black;"> }</span></div><div><span style="color: black;"> </span><span style="color: blue;">return</span><span style="color: black;"> total_iterations;</span></div><div><span style="color: black;"> }</span></div><div><span style="color: black;"><b>Output:</b> </span></div></div><p><span style="color: #04ff00;"></span></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgTpsC2O6ICK8kZf8hS_bvtI__Mqaq1GAUK4_hTGzM3rHiWcIExhhY_HT3G7dCTeg0PxL-NuwCo2Nbk4KnZMen1k4jxCwHX_8cOvlsusxM5wK8SCQh2EH1SVNVwCk23UxLUjjnSAmYlJ-1_/s723/bubblesortlevel1.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="399" data-original-width="723" height="243" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgTpsC2O6ICK8kZf8hS_bvtI__Mqaq1GAUK4_hTGzM3rHiWcIExhhY_HT3G7dCTeg0PxL-NuwCo2Nbk4KnZMen1k4jxCwHX_8cOvlsusxM5wK8SCQh2EH1SVNVwCk23UxLUjjnSAmYlJ-1_/w640-h243/bubblesortlevel1.png" width="640" /></a></div><p></p><p><span style="color: #674ea7;">You can see that we require <b>132 Iterations for sorting the Array<span></span></b></span></p><!--more--><p></p><p><span style="color: #04ff00;"><b><span style="color: black;">2.Level 2</span></b></span></p><p><span style="color: #04ff00;"><b><span style="color: black;"><span style="color: #38761d;">In this level we add outer loop iterator in the condition of for loop , because you can see that after every iterations the highest number is positioned to its appropriate end position, thus to avoid extra comparisons we reduce the number of iterations of inner loop. </span><br /></span></b></span></p><div style="background-color: white; color: black; font-family: "Droid Sans Mono", "monospace", monospace, "Droid Sans Fallback"; font-size: 16px; font-weight: normal; line-height: 22px; white-space: pre;"><div><span style="color: blue;">public</span><span style="color: black;"> </span><span style="color: blue;">int</span><span style="color: black;"> bubbleSortLevel2(</span><span style="color: blue;">int</span><span style="color: black;">[] arr) {</span></div><div> <span style="color: blue;">int</span><span style="color: black;"> total_iterations=</span><span style="color: #098658;">0</span><span style="color: black;">;</span></div><div><span style="color: blue;"><span style="color: black;"> </span>for</span><span style="color: black;">(</span><span style="color: blue;">int</span><span style="color: black;"> i=</span><span style="color: #098658;">0</span><span style="color: black;">;i<arr.length;i++)</span></div><div><span style="color: black;"> {</span></div><div> <span style="color: blue;">for</span><span style="color: black;">(</span><span style="color: blue;">int</span><span style="color: black;"> j=</span><span style="color: #098658;">0</span><span style="color: black;">;j<<span style="color: red;">arr.length-i-</span></span><span style="color: red;">1</span><span style="color: black;">;j++)</span></div><div><span style="color: black;"> {</span></div><div><span style="color: black;"> </span><span style="color: blue;">if</span><span style="color: black;">(arr[j]>arr[j+</span><span style="color: #098658;">1</span><span style="color: black;">])</span></div><div><span style="color: black;"> {</span></div><div><span style="color: black;"> </span><span style="color: blue;">int</span><span style="color: black;"> temp=arr[j];</span></div><div><span style="color: black;"> arr[j]=arr[j+</span><span style="color: #098658;">1</span><span style="color: black;">];</span></div><div><span style="color: black;"> arr[j+</span><span style="color: #098658;">1</span><span style="color: black;">]=temp;</span></div><div><span style="color: black;"> }</span></div><div><span style="color: black;"> total_iterations++;</span></div><div><span style="color: black;"> }</span></div><div><span style="color: black;"> System.out.print(</span><span style="color: #a31515;">"i="</span><span style="color: black;">+i+</span><span style="color: #a31515;">" : "</span><span style="color: black;">);</span></div><div><span style="color: black;"> print(arr);</span></div><div><span style="color: black;"> }</span></div><div><span style="color: black;"> </span><span style="color: blue;">return</span><span style="color: black;"> total_iterations;</span></div><div><span style="color: black;"> }</span></div><div><span style="color: #38761d;">In this level we added a "i" variable in the inner loop to reduce</span></div><div><span style="color: #38761d;"> the number of iterations</span></div><div><span style="color: black;"> </span></div><div><b><span style="color: black;">Output:</span></b></div><div><span style="color: black;"> </span></div><div><span style="color: black;"><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgKpiMEJU6tJnvqsfPjtf3SmQkuFdGPnQKeWP68dm0myt0dH1ydZa2N2PSRK7RahNcGT1tepA5q8qQ6-0gSQt_IYnq2dFRkXcrAvg3ArzIU-Gkm8e1bedvYnemrFsRJWOSuLOsoaS8-kxfW/s805/bubblesortlevel2.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="395" data-original-width="805" height="217" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgKpiMEJU6tJnvqsfPjtf3SmQkuFdGPnQKeWP68dm0myt0dH1ydZa2N2PSRK7RahNcGT1tepA5q8qQ6-0gSQt_IYnq2dFRkXcrAvg3ArzIU-Gkm8e1bedvYnemrFsRJWOSuLOsoaS8-kxfW/w640-h217/bubblesortlevel2.png" width="640" /></a></div><span style="color: #3d85c6;"><br /></span></span></div></div><p><span style="color: #3d85c6;"><b>You can see now the iterations are decreased to 66 , thus if the search space is huge enough it will be a good decrement in the time consumption.</b></span></p><p><span style="color: #04ff00;"><b><span style="color: black;"> <span></span></span></b></span></p><!--more--><p></p><p><span style="color: #04ff00;"><b><span style="color: black;">3.Level 3</span></b></span><span style="color: black;"> </span></p><p><span style="color: #6aa84f;"><b>In this level we use a <span style="color: #ffa400;">flag variable</span> to avoid extra comparison if the array is already sorted .</b></span></p><div style="background-color: white; color: black; font-family: "Droid Sans Mono", "monospace", monospace, "Droid Sans Fallback"; font-size: 16px; font-weight: normal; line-height: 22px; white-space: pre;"><div><span style="color: blue;">public</span><span style="color: black;"> </span><span style="color: blue;">int</span><span style="color: black;"> bubbleSortLevel3(</span><span style="color: blue;">int</span><span style="color: black;">[] arr) {</span></div><div><span style="color: black;"> </span><span style="color: blue;">int</span><span style="color: black;"> total_iterations=</span><span style="color: #098658;">0</span><span style="color: black;">;</span></div><div><span style="color: blue;"> <span style="color: black;"> </span>for</span><span style="color: black;">(</span><span style="color: blue;">int</span><span style="color: black;"> i=</span><span style="color: #098658;">0</span><span style="color: black;">;i<arr.length;i++)</span></div><div><span style="color: black;"> {</span></div><div><span style="color: black;"> </span><span style="color: blue;">boolean</span><span style="color: black;"> flag=</span><span style="color: blue;">true</span><span style="color: black;">;</span></div><div><span style="color: black;"> </span><span style="color: blue;">for</span><span style="color: black;">(</span><span style="color: blue;">int</span><span style="color: black;"> j=</span><span style="color: #098658;">0</span><span style="color: black;">;j<arr.length-i-</span><span style="color: #098658;">1</span><span style="color: black;">;j++)</span></div><div><span style="color: black;"> {</span></div><div><span style="color: black;"> </span><span style="color: blue;">if</span><span style="color: black;">(arr[j]>arr[j+</span><span style="color: #098658;">1</span><span style="color: black;">])</span></div><div><span style="color: black;"> {</span></div><div><span style="color: black;"> flag=</span><span style="color: blue;">false</span><span style="color: black;">;</span></div><div><span style="color: black;"> </span><span style="color: blue;">int</span><span style="color: black;"> temp=arr[j];</span></div><div><span style="color: black;"> arr[j]=arr[j+</span><span style="color: #098658;">1</span><span style="color: black;">];</span></div><div><span style="color: black;"> arr[j+</span><span style="color: #098658;">1</span><span style="color: black;">]=temp;</span></div><div><span style="color: black;"> }</span></div><div><span style="color: black;"> total_iterations++;</span></div><div><span style="color: black;"> }</span></div><div><span style="color: black;"> </span><span style="color: blue;">if</span><span style="color: black;">(flag) {</span></div><div><span style="color: black;"> </span><span style="color: blue;">break</span><span style="color: black;">;</span></div><div><span style="color: black;"> }</span></div><div><span style="color: black;"> System.out.print(</span><span style="color: #a31515;">"i="</span><span style="color: black;">+i+</span><span style="color: #a31515;">" : "</span><span style="color: black;">);</span></div><div><span style="color: black;"> print(arr);</span></div><div><span style="color: black;"> }</span></div><div><span style="color: black;"> </span><span style="color: blue;">return</span><span style="color: black;"> total_iterations;</span></div><div><span style="color: black;"> }</span></div></div><p><span style="color: #04ff00;"><b><span style="color: black;">Output:</span></b></span></p><p><span style="color: #04ff00;"><b><span style="color: black;"> </span> </b><br /> </span><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg8NAl_RJddle7EIxj3ieYDhtMXWy5Sn6b1_MlAitoTmcUF-gge5vu1AmABOnPKL-2fr_6OKqS5bDLnkbon-rGsqF2UOjmz_c7x-lApsN8RTUQR4D6WssBZKqjQddY-Rnf2NfaH67dAf1cm/s903/level3bubble2.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="163" data-original-width="903" height="78" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg8NAl_RJddle7EIxj3ieYDhtMXWy5Sn6b1_MlAitoTmcUF-gge5vu1AmABOnPKL-2fr_6OKqS5bDLnkbon-rGsqF2UOjmz_c7x-lApsN8RTUQR4D6WssBZKqjQddY-Rnf2NfaH67dAf1cm/w640-h78/level3bubble2.png" width="640" /></a></p><p><b><span style="font-size: medium;"><span></span></span></b></p><!--more--><b><span style="font-size: medium;">Comparing Level 2 and Level 3 Bubble Sort:</span></b><p></p><p><span style="color: #6aa84f;"><b>Lets see it with comparing level 2 and level 3 bubble sort</b></span></p><p><span style="color: #6aa84f;"><b>We will use <span style="color: #073763;">int arr2[]= {-9 ,-6 ,-3, 0, 1 ,3 ,5, 6, 7, 8 ,9, 10 };</span> as the array is already sorted so their is no need for extra computation </b></span></p><p><span style="color: #04ff00;"><b><span style="color: black;"><span style="color: #6aa84f;">So lets check our both level codes </span><br /></span></b></span></p><p><span style="color: #04ff00;"><b><span style="color: black;">Level 2 Bubble Sort:</span></b></span></p><p><b><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgBXDYoSyxccxxE38Byo51aPWI-J2HlHqvxR8qiKqB3nVloHbqnsSsDcJXBU3Az9aUv2CwwkkEezB8zPD3FwdbdYPNv73KcudqSVI4z5N3r7ltD0ooyrfdEkfvNXfekWfF0BIDIhY_m1BmD/s813/level3bubble1.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="415" data-original-width="813" height="251" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgBXDYoSyxccxxE38Byo51aPWI-J2HlHqvxR8qiKqB3nVloHbqnsSsDcJXBU3Az9aUv2CwwkkEezB8zPD3FwdbdYPNv73KcudqSVI4z5N3r7ltD0ooyrfdEkfvNXfekWfF0BIDIhY_m1BmD/w640-h251/level3bubble1.png" width="640" /></a></b><span style="color: #04ff00;"><b><span style="color: black;"> <br /></span><br /></b></span></p><p><b><span style="color: #38761d;">You can see that even if no computation is needed it <span style="color: #351c75;">still does 66 </span>iterations <span style="color: #351c75;">for i value ranging from 0 to 11</span></span></b></p><p><b><span style="color: #38761d;"><span style="color: #351c75;"> </span></span><br /></b></p><p><span style="color: #04ff00;"><b><span style="color: black;">Level 3 Bubble Sort:</span></b></span></p><p><span style="color: #04ff00;"><b><span style="color: black;"><span style="color: #ffa400;">You
can see that here only 11 comparison is done and only i=0 is run
because the array is sorted Thus the time is reduced to run the
program.</span><br /></span></b></span></p><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg8NAl_RJddle7EIxj3ieYDhtMXWy5Sn6b1_MlAitoTmcUF-gge5vu1AmABOnPKL-2fr_6OKqS5bDLnkbon-rGsqF2UOjmz_c7x-lApsN8RTUQR4D6WssBZKqjQddY-Rnf2NfaH67dAf1cm/s903/level3bubble2.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="163" data-original-width="903" height="78" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg8NAl_RJddle7EIxj3ieYDhtMXWy5Sn6b1_MlAitoTmcUF-gge5vu1AmABOnPKL-2fr_6OKqS5bDLnkbon-rGsqF2UOjmz_c7x-lApsN8RTUQR4D6WssBZKqjQddY-Rnf2NfaH67dAf1cm/w640-h78/level3bubble2.png" width="640" /></a> <p><span style="color: #04ff00;"></span></p><div><div style="text-align: left;"><b> </b></div><div style="text-align: left;"><b>Related Posts:</b></div><div style="text-align: left;"><b> </b></div><div style="text-align: left;"><b><a href="https://codeityweb.blogspot.com/2020/12/bubble-sort-python.html" target="_blank">Bubble Sort In Python</a> </b></div><div style="text-align: left;"><b> <br /></b></div><div style="text-align: left;"><b>Conclusion:</b></div><div style="text-align: left;"><span style="color: red;"> </span></div><div style="text-align: left;"><span style="color: red;">Thus we can use any Sorting level for sorting if the search space that is the array in this case is small.But if the space is large enough then it is good to use level2 or level 3 sorting technique </span><br /></div><div style="text-align: left;"><br /></div></div></div><div class="blogger-post-footer">Contact Us By Email Or Get in touch by Commenting on posts</div>CodeityWebhttp://www.blogger.com/profile/01377516264362368650noreply@blogger.com0tag:blogger.com,1999:blog-7236090768681897809.post-15747758692922975832020-10-14T18:18:00.005+05:302021-01-21T11:10:59.476+05:30Making Anagrams -Hackerrank Solutions<h1 style="text-align: left;"> Making Two Strings Anagrams -Hackerrank Solutions</h1><p style="text-align: left;"><span style="font-size: medium;"> </span></p><p style="text-align: left;"><span style="font-size: medium;">Question:</span></p><p style="text-align: left;"><span style="font-size: medium;">Given two strings <b>a</b> and string <b>b ,</b> you have to delete/add characters from the strings to make <b>them anagram</b> of each other.</span></p><p style="text-align: left;"><span style="font-size: medium;">So before starting lets see what anagrams are. Consider a string<b> "abc" </b>the anagram of the string are <b>"acb","bca","cab" etc</b>.. That is that number of string that can be formed by rearranging the characters of the string without any addition or deletion of character.</span></p><p style="text-align: left;"><span style="font-size: medium;">In short if the string has <b>{a=2,b=4,c=3</b>} characters then the anagram will also have the same number of character i.e <b>{a=2,b=4,c=3</b>} but they can be in other order.</span></p><p style="text-align: left;"><span style="font-size: medium;">So in this post we have discussed </span>Making Anagrams Hackerrank Solution<br /></p><p style="text-align: left;"><span style="font-size: medium;">So lets start the algorithm for this:</span></p><p style="text-align: left;"><span style="font-size: medium;"> </span></p><p style="text-align: left;"><span style="font-size: medium;"><b>Explanation:</b></span></p><ul style="text-align: left;"><li><span style="font-size: medium;">As we have seen we need the count of the characters , so we will make a frequency array to store the count of each character in the array.</span></li><li><span style="font-size: medium;">This will be for both the string since we have to make them anagram of each other.</span></li><li><span style="font-size: medium;">Now the next step will be to find the total number of characters that are in string a and not in string b and vice versa</span></li><li><span style="font-size: medium;">For that since we have the count of each character we simply need to take difference of the characters in the strings to count the total number of characters that are missing in either of the string.</span></li><li><span style="font-size: medium;">Finally return the count of the numbers</span></li></ul><h4 style="text-align: left;"><span style="color: #3d85c6; font-size: medium;">We will understand it through an example:</span></h4><p style="text-align: left;"><span style="font-size: medium;"> </span><span style="font-size: medium;">String x="abc"</span></p><p style="text-align: left;"><span style="font-size: medium;"> </span><span style="font-size: medium;">String y="cde"</span></p><ul style="text-align: left;"><li><span style="font-size: medium;">So when we take the count of each character we get x_count={a=1,b=1,c=1}, y_count={c=1,d=1,e=1}</span></li></ul><p style="text-align: left;"><span style="font-size: medium;">Here we have completed first step now we need to <span style="color: #274e13;"><b>count the total missing characters</b></span> in the strings . </span></p><div style="text-align: left;"><ul style="text-align: left;"><li><span style="font-size: medium;"> a is 1 in string x but 0 in string y so the count becomes 1.</span></li><li><span style="font-size: medium;"> b is 1 in string x but 0 in string y so the count becomes 2.</span></li><li><span style="font-size: medium;">c is 1 in string x and 1 in string y so no need to make changes count remains 2.</span></li><li><span style="font-size: medium;">d is 1 in string y but 0 in string x so count becomes 3.</span></li><li><span style="font-size: medium;">finally e is 1 in y but 0 in x so count becomes 4.</span></li></ul><span style="font-size: medium;"> </span></div><div style="text-align: left;"><span style="font-size: medium;">Therefore their is total 4 characters difference in both the string thus we need 4 characters addition or deletion to make the string anagram</span></div><div style="text-align: left;"><ul style="text-align: left;"><li><span style="font-size: medium;">If we add 4 characters the string becomes x= "abcde" y="abcde" here we add d,e to x and a,b to y So that they becomes anagram</span></li><li><span style="font-size: medium;">if we delete 4 characters the string becomes x="c" , y="c" here we delete 2 characters i.e a,b from x and 2 characters d,e from y So they becomes anagram.</span></li></ul><span style="font-size: medium;"> </span></div><div style="text-align: left;"><h3><span style="color: #38761d;"><span style="font-size: medium;">Making Anagram Code:</span></span><br /></h3></div><div style="text-align: left;"><h3><span style="font-size: medium;"><b> </b></span></h3></div><div style="text-align: left;"><div style="background-color: #fffffe; color: black; font-family: "SourceCodePro, monospace", "Droid Sans Mono", "monospace", monospace, "Droid Sans Fallback"; font-weight: normal; line-height: 20px; white-space: pre;"><div><span style="color: blue; font-size: medium;">import</span><span style="color: black; font-size: medium;"> java.util.*;</span></div><span style="font-size: medium;"><br /></span><div><span style="color: blue; font-size: medium;">public</span><span style="color: black; font-size: medium;"> </span><span style="color: blue; font-size: medium;">class</span><span style="color: black; font-size: medium;"> Solution {</span></div><span style="font-size: medium;"><br /></span><div><span style="color: blue; font-size: medium;">static</span><span style="color: black; font-size: medium;"> </span><span style="color: blue; font-size: medium;">int</span><span style="color: black; font-size: medium;"> makeAnagram(String a, String b) {</span></div><div><span style="color: blue; font-size: medium;">int</span><span style="color: black; font-size: medium;"> frequency_of_a[]=</span><span style="color: blue; font-size: medium;">new</span><span style="color: black; font-size: medium;"> </span><span style="color: blue; font-size: medium;">int</span><span style="color: black; font-size: medium;">[</span><span style="color: #09885a; font-size: medium;">26</span><span style="color: black; font-size: medium;">];</span></div><div><span style="color: blue; font-size: medium;">int</span><span style="color: black; font-size: medium;"> frequency_of_b[]=</span><span style="color: blue; font-size: medium;">new</span><span style="color: black; font-size: medium;"> </span><span style="color: blue; font-size: medium;">int</span><span style="color: black; font-size: medium;">[</span><span style="color: #09885a; font-size: medium;">26</span><span style="color: black; font-size: medium;">];</span></div><div><span style="color: blue; font-size: medium;">int</span><span style="color: black; font-size: medium;"> count=</span><span style="color: #09885a; font-size: medium;">0</span><span style="color: black; font-size: medium;">;</span></div><span style="font-size: medium;"><br /></span><div><span style="color: blue; font-size: medium;">for</span><span style="color: black; font-size: medium;">(</span><span style="color: blue; font-size: medium;">int</span><span style="color: black; font-size: medium;"> i=</span><span style="color: #09885a; font-size: medium;">0</span><span style="color: black; font-size: medium;">;i<a.length();i++)</span></div><div><span style="color: black; font-size: medium;">{</span></div><div><span style="color: black; font-size: medium;"> frequency_of_a[a.charAt(i)-</span><span style="color: #a31515; font-size: medium;">'a'</span><span style="color: black; font-size: medium;">]++;</span></div><div><span style="color: black; font-size: medium;">}</span></div><div><span style="color: blue; font-size: medium;">for</span><span style="color: black; font-size: medium;">(</span><span style="color: blue; font-size: medium;">int</span><span style="color: black; font-size: medium;"> i=</span><span style="color: #09885a; font-size: medium;">0</span><span style="color: black; font-size: medium;">;i<b.length();i++)</span></div><div><span style="color: black; font-size: medium;">{</span></div><div><span style="color: black; font-size: medium;"> frequency_of_b[b.charAt(i)-</span><span style="color: #a31515; font-size: medium;">'a'</span><span style="color: black; font-size: medium;">]++;</span></div><div><span style="color: black; font-size: medium;">}</span></div><span style="font-size: medium;"><br /></span><div><span style="color: blue; font-size: medium;">for</span><span style="color: black; font-size: medium;">(</span><span style="color: blue; font-size: medium;">int</span><span style="color: black; font-size: medium;"> i=</span><span style="color: #09885a; font-size: medium;">0</span><span style="color: black; font-size: medium;">;i<</span><span style="color: #09885a; font-size: medium;">26</span><span style="color: black; font-size: medium;">;i++)</span></div><div><span style="color: black; font-size: medium;">{</span></div><div><span style="color: black; font-size: medium;"> </span><span style="color: blue; font-size: medium;">if</span><span style="color: black; font-size: medium;">(Math.abs(frequency_of_a[i]-frequency_of_b[i])></span><span style="color: #09885a; font-size: medium;">0</span><span style="color: black; font-size: medium;">)</span></div><div><span style="color: black; font-size: medium;"> {</span></div><div><span style="color: black; font-size: medium;"> count+=Math.abs(frequency_of_a[i]-frequency_of_b[i]);</span></div><div><span style="color: black; font-size: medium;"> }</span></div><div><span style="color: black; font-size: medium;">}</span></div><div><span style="color: blue; font-size: medium;">return</span><span style="color: black; font-size: medium;"> count;</span></div><div><span style="color: black; font-size: medium;"> }</span></div><span style="font-size: medium;"><br /></span><div><span style="color: black; font-size: medium;"> </span><span style="color: blue; font-size: medium;">public</span><span style="color: black; font-size: medium;"> </span><span style="color: blue; font-size: medium;">static</span><span style="color: black; font-size: medium;"> </span><span style="color: blue; font-size: medium;">void</span><span style="color: black; font-size: medium;"> main(String []args)</span></div><div><span style="color: black; font-size: medium;"> {</span></div><div><span style="color: black; font-size: medium;"> Scanner sc=</span><span style="color: blue; font-size: medium;">new</span><span style="color: black; font-size: medium;"> Scanner(System.in);</span></div><div><span style="color: black; font-size: medium;"> String a=sc.next();</span></div><div><span style="color: black; font-size: medium;"> String b=sc.next();</span></div><div><span style="color: black; font-size: medium;"> </span><span style="color: blue; font-size: medium;">int</span><span style="color: black; font-size: medium;"> result=makeAnagram(a,b);</span></div><div><span style="color: black; font-size: medium;"> System.out.println(result);</span></div><span style="font-size: medium;"><br /></span><div><span style="color: black; font-size: medium;"> }</span></div><div><span style="color: black; font-size: medium;"> </span></div><div><span style="color: black; font-size: medium;">}</span></div><span style="font-size: medium;"><br /><br /></span></div></div><div class="blogger-post-footer">Contact Us By Email Or Get in touch by Commenting on posts</div>CodeityWebhttp://www.blogger.com/profile/01377516264362368650noreply@blogger.com0tag:blogger.com,1999:blog-7236090768681897809.post-34650699921654075322020-10-13T14:36:00.005+05:302021-01-21T11:12:48.348+05:30Birthday Cake Candles<h1>Birthday Cake Candles<br /></h1><p> </p><p>The question is inspired from hackerrank.</p><p>You are in charge of the cake for a child's birthday. You have decided
the cake will have one candle for each year of their total age. They
will only be able to blow out the tallest of the candles. Count how many
candles are tallest. </p><p>Ex:</p><p>array=[5,6,7,4,5,7]<br /></p><p>The maximum height of the candle is 7 and their are 2 highest candles so the answer will be 2.<br /></p><p><b>Explanation:</b><br /></p><p><b>1.Find the maximum in the array</b></p><p><b>2.Find the count of the maximum</b></p><p><b>Code:</b></p><div style="text-align: left;"><b> </b><div style="background-color: #fffffe; color: black; font-family: "SourceCodePro, monospace", "Droid Sans Mono", "monospace", monospace, "Droid Sans Fallback"; font-size: 15px; font-weight: normal; line-height: 20px; white-space: pre;"><div><span style="color: black;"> </span><span style="color: blue;">static</span><span style="color: black;"> </span><span style="color: blue;">int</span><span style="color: black;"> birthdayCakeCandles(</span><span style="color: blue;">int</span><span style="color: black;">[] ar) {</span></div><div><span style="color: black;"> </span></div><div><span style="color: black;"> </span><span style="color: blue;">if</span><span style="color: black;">(ar.length==</span><span style="color: #09885a;">0</span><span style="color: black;">)</span><span style="color: blue;">return</span><span style="color: black;"> </span><span style="color: #09885a;">0</span><span style="color: black;">;</span></div><div><span style="color: black;"> </span><span style="color: blue;">int</span><span style="color: black;"> n=ar.length;</span></div><div><span style="color: black;"> </span><span style="color: blue;">int</span><span style="color: black;"> candle=</span><span style="color: #09885a;">0</span><span style="color: black;">;</span></div><div><span style="color: black;"> </span><span style="color: blue;">int</span><span style="color: black;"> max=-</span><span style="color: #09885a;">1</span><span style="color: black;">;</span></div><div><span style="color: black;"> </span><span style="color: blue;">for</span><span style="color: black;">(</span><span style="color: blue;">int</span><span style="color: black;"> i=</span><span style="color: #09885a;">0</span><span style="color: black;">;i<n;i++)</span></div><div><span style="color: black;"> {</span></div><div><span style="color: black;"> max=Math.max(max,ar[i]);</span></div><div><span style="color: black;"> }</span></div><br /><div><span style="color: black;"> </span><span style="color: blue;">for</span><span style="color: black;">(</span><span style="color: blue;">int</span><span style="color: black;"> i=</span><span style="color: #09885a;">0</span><span style="color: black;">;i<n;i++)</span></div><div><span style="color: black;"> {</span></div><div><span style="color: black;"> </span><span style="color: blue;">if</span><span style="color: black;">(ar[i]==max)</span></div><div><span style="color: black;"> {</span></div><div><span style="color: black;"> candle++;</span></div><div><span style="color: black;"> }</span></div><div><span style="color: black;"> }</span></div><br /><div><span style="color: blue;">return</span><span style="color: black;"> candle;</span></div><div><span style="color: black;"> }</span></div><div><span style="color: black;"> </span></div><div><span style="color: black;"><b>Analysis:</b> <br /></span></div><div><ul style="text-align: left;"><li><span style="color: black;">If you look into the asymptotic analysis , the time complexity will be O(n) </span></li></ul></div><div><ul style="text-align: left;"><li><span style="color: black;">where n is the number of elements in the array.</span></li></ul></div><div><ul style="text-align: left;"><li><span style="color: black;">We traverse the n elements in the array 2 times thus it is <b>2*n </b>but asymptotically it will be O(2) </span></li></ul></div><div><ul style="text-align: left;"><li><span style="color: black;">since we don't consider constants while dealing with higher values. </span></li></ul></div><div><ul style="text-align: left;"><li><span style="color: black;">The space complexity will be O(n), since we are storing the n elements in the array. </span></li></ul></div><div><span style="color: black;"> </span></div><div><span style="color: black;"> </span></div></div> </div><div class="blogger-post-footer">Contact Us By Email Or Get in touch by Commenting on posts</div>CodeityWebhttp://www.blogger.com/profile/01377516264362368650noreply@blogger.com0tag:blogger.com,1999:blog-7236090768681897809.post-13629685370410033742020-10-13T14:25:00.005+05:302021-01-03T15:48:55.407+05:30Diagonal Difference<div style="text-align: left;"> <h1 style="text-align: left;">Diagonal Difference</h1><h1 style="text-align: left;"> <br /></h1><p>Given a square matrix, calculate the absolute difference between the sums of its diagonals. </p>
<p>For example, the square matrix <b>arr </b>is shown below: </p>
<pre><code><b>1 2 3
4 5 6
9 8 9 </b>
</code></pre>
<p>The left-to-right diagonal = <b>1+5+9=15</b>. The right to left diagonal = 3+5+9=17. Their absolute difference is |15-17|=2<span class="MathJax_SVG" id="MathJax-Element-4-Frame" style="display: inline-block; font-size: 100%;"><svg height="2.843ex" style="vertical-align: -0.838ex;" viewbox="0 -863.1 5616.5 1223.9" width="13.045ex"><g fill="currentColor" stroke-width="0" stroke="currentColor" transform="matrix(1 0 0 -1 0 0)"></g></svg></span><span class="MathJax_SVG" id="MathJax-Element-4-Frame" style="display: inline-block; font-size: 100%;"></span><span class="MathJax_SVG" id="MathJax-Element-4-Frame" style="display: inline-block; font-size: 100%;"></span><span class="MathJax_SVG" id="MathJax-Element-4-Frame" style="display: inline-block; font-size: 100%;"></span><span class="MathJax_SVG" id="MathJax-Element-4-Frame" style="display: inline-block; font-size: 100%;"></span></p><div class="challenge_output_format"><div class="msB challenge_output_format_body"><div class="hackdown-content"><p></p></div></div></div><div class="challenge_sample_input"><div class="msB challenge_sample_input_title"><p><b>Sample Input</b></p></div><div class="msB challenge_sample_input_body"><div class="hackdown-content"><pre><code>3<b>
11 2 4
4 5 6
10 8 -12</b>
</code></pre></div></div></div><div class="challenge_sample_output"><div class="msB challenge_sample_output_title"><p><b>Sample Output</b></p></div><div class="msB challenge_sample_output_body"><div class="hackdown-content"><pre><code><b>15</b>
</code></pre></div></div></div><div class="challenge_explanation"><div class="msB challenge_explanation_title"><p><b>Explanation</b></p></div><div class="msB challenge_explanation_body"><div class="hackdown-content"><p>The primary diagonal is: </p>
<pre><code><b>11
5
-12</b>
</code></pre>
<p>Sum across the primary diagonal:<b> 11 + 5 - 12 = 4</b> </p>
<p>The secondary diagonal is:<br /></p>
<pre><code> <b> 4
5
10</b>
</code></pre>
<p>Sum across the secondary diagonal: <b>4 + 5 + 10 = 19</b> <br />
Difference: <b>|4 - 19| = 15</b></p>
<p><b></b><br /></p><p><b>Algo:</b></p><div style="text-align: left;">1.We take two pointer i.e variable i,j to traverse the matrix.To sum the left diagonal assign i=0,j=0 .</div><div style="text-align: left;"><b> </b><br /></div><div style="text-align: left;"><b>2. </b>To sum the right diagonal, make j as "n-1" and i=0, it will point to the last element in the first row , and now decrement j and increment i to add all diagonal values.</div><div style="text-align: left;"> </div><div style="text-align: left;">3.Finally return the difference between them<br /></div><p><b>Code:</b></p><div style="text-align: left;"><div style="background-color: #fffffe; color: black; font-family: "SourceCodePro, monospace", "Droid Sans Mono", "monospace", monospace, "Droid Sans Fallback"; font-size: 15px; font-weight: normal; line-height: 20px; white-space: pre;"><div><span style="color: blue;">public</span><span style="color: black;"> </span><span style="color: blue;">static</span><span style="color: black;"> </span><span style="color: blue;">int</span><span style="color: black;"> diagonalDifference(List<List<Integer>> arr) {</span></div><div><span style="color: black;"> </span><span style="color: blue;">int</span><span style="color: black;"> sum1=</span><span style="color: #09885a;">0</span><span style="color: black;">;</span></div><div><span style="color: black;"> </span><span style="color: blue;">int</span><span style="color: black;"> sum2=</span><span style="color: #09885a;">0</span><span style="color: black;">;</span></div><div><span style="color: black;"> </span><span style="color: blue;">int</span><span style="color: black;"> n=arr.size();</span></div><div><span style="color: black;"> </span><span style="color: blue;">int</span><span style="color: black;"> i=</span><span style="color: #09885a;">0</span><span style="color: black;">,j=</span><span style="color: #09885a;">0</span><span style="color: black;">;</span></div><br /><div><span style="color: black;"> </span><span style="color: blue;">while</span><span style="color: black;">(i<n && j<n)</span></div><div><span style="color: black;"> {</span></div><div><span style="color: black;"> </span><span style="color: green;">//positive diagonal sum "top left to bottom right"</span></div><div><span style="color: black;"> sum1+=arr.get(i++).get(j++);</span></div><div><span style="color: black;"> }</span></div><br /><div><span style="color: black;"> i=</span><span style="color: #09885a;">0</span><span style="color: black;">;</span></div><div><span style="color: black;"> j=n-</span><span style="color: #09885a;">1</span><span style="color: black;">;</span></div><div><span style="color: black;"> </span></div><div><span style="color: black;"> </span><span style="color: blue;">while</span><span style="color: black;">(i<n && j>=</span><span style="color: #09885a;">0</span><span style="color: black;">)</span></div><div><span style="color: black;"> {</span></div><div><span style="color: black;"> </span><span style="color: green;">//"top right to bottom left"</span></div><div><span style="color: black;"> sum2+=arr.get(i++).get(j--);</span></div><div><span style="color: black;"> }</span></div><div><span style="color: black;"> </span></div><div><span style="color: black;"> </span><span style="color: blue;">return</span><span style="color: black;"> Math.abs(sum1-sum2);</span></div><div><span style="color: black;"> }</span></div><div><span style="color: black;">}</span></div></div><b> </b></div></div></div></div></div><div class="blogger-post-footer">Contact Us By Email Or Get in touch by Commenting on posts</div>CodeityWebhttp://www.blogger.com/profile/01377516264362368650noreply@blogger.com0tag:blogger.com,1999:blog-7236090768681897809.post-35470240394007218092020-09-19T11:43:00.008+05:302021-01-03T15:49:36.335+05:30Delete without head pointer<h1 style="text-align: left;">Delete without head pointer<br /></h1><p><span style="font-size: 18px;">You are given a reference to the node which is to be deleted from the linked list of <b>N </b>nodes. The task is to delete the node.</span></p><p><span style="font-size: 18px;"> </span><br />
<span style="font-size: 18px;"><b>Note:</b> No head reference is given to you.</span></p><p style="text-align: left;"><b>Ex: 1->3->2->4->6 </b>you are given a pointer to node 2 you have to delete that node and you dont have the head node pointer.</p><p style="text-align: left;"><b><span> </span>Output will be </b><i>1->3->4->6</i></p><p style="text-align: left;"><b>Explanation:</b><br /></p><ul style="text-align: left;"><li>let the list be 1->3-><b> 2 </b>->4->6<b> </b> we have a pointer at node 2<b> </b></li></ul><div style="margin-left: 80px; text-align: left;"><b> Node class looks like </b></div><div style="margin-left: 80px; text-align: left;"><b> <span style="color: #3d85c6;"> </span></b><span style="color: #3d85c6;">class Node{</span></div><div style="margin-left: 80px; text-align: left;"><span style="color: #3d85c6;"><span> </span><span> </span><span style="color: #274e13;">int data;</span></span></div><div style="margin-left: 80px; text-align: left;"><span style="color: #274e13;"><span> </span><span> </span>Node next;</span></div><div style="margin-left: 80px; text-align: left;"><span style="color: #3d85c6;"><span> </span><span> </span><span> </span><span style="color: #4c1130;">Node(int data)</span></span></div><div style="margin-left: 80px; text-align: left;"><span style="color: #4c1130;"><span> </span><span> </span><span> </span>{</span></div><div style="margin-left: 80px; text-align: left;"><span style="color: #4c1130;"><span> </span><span> </span><span> </span><span> </span>this.data=data;</span></div><div style="margin-left: 80px; text-align: left;"><span style="color: #4c1130;"><span> </span><span> </span><span> </span><span> </span>this.next=NULL;</span></div><div style="margin-left: 80px; text-align: left;"><span style="color: #4c1130;"><span> </span><span> </span><span> </span>}</span></div><div style="margin-left: 80px; text-align: left;"><span style="color: #3d85c6;"><span> </span>}</span></div><div style="margin-left: 80px; text-align: left;"><b> </b><i></i><br /></div><div style="text-align: left;"><ul style="text-align: left;"><li> So what we can do is copy the data of the next node to the current node and make the pointer of the current node pointing to the next of the next node.</li><li>Means say list is 1->3-<b>> 2</b> -<b>></b>4->6 , and we have pointer to node <b>2, </b>now if we copy the data of the next node to the current node .</li><li>The list will be 1->3<b>-> 4 -></b>4->6 we still have pointer to that node just we replace its data with the neighbours data.</li><li>Now we make the next pointer to point to the node with data 6</li><li>Thus the list will be 1->3->4->6 .</li></ul>Code:</div><div style="text-align: left;"> </div><div style="text-align: left;"><span style="color: #3d85c6;">void</span> <span style="color: #274e13;">deleteNode</span>(Node node)<br /> {<br /> if(node.next!=null)<br /> {<span style="color: #20124d;"><br /> <span> </span> node.data=node.next.data;<br /> <span> </span> node.next=node.next.next;</span><br /> } <br /> <br /> } </div><div style="text-align: left;"><br /></div><div style="text-align: left;"><b>lets evaluate the code:</b></div><div style="text-align: left;">list<b>: 1->3->2->4->6</b></div><div style="text-align: left;"><ul style="text-align: left;"><li><b> </b>we have pointer to node 2, we check if it is last node or not. Now we copy the data to the our current node , and make its next to point next of nodes next.</li></ul></div><div style="text-align: left;"><br /></div><div style="text-align: left;"><ul style="text-align: left;"><li>node 2: data becomes 4 , next pointer will be node.next.next i.e next of 2 and its next which is 6 so the duplicate value that will be formed by coping data from the neighbours will not be considered as list uses pointer or references .</li></ul></div><div></div><div></div><div><br /><i> <span> </span><span> </span><span> </span><span> </span><span> </span><span> </span><span> </span><span> </span><span> </span><span> </span><span> </span></i></div><div class="blogger-post-footer">Contact Us By Email Or Get in touch by Commenting on posts</div>CodeityWebhttp://www.blogger.com/profile/01377516264362368650noreply@blogger.com0tag:blogger.com,1999:blog-7236090768681897809.post-50953989862489445842020-09-19T11:19:00.003+05:302021-01-03T15:50:53.294+05:30Count Set Bits<h2 style="text-align: left;"></h2><h1 style="text-align: left;"><span style="font-size: 18px;"><span style="font-size: x-large;">Count Set Bits </span><br /></span></h1><h1 style="text-align: left;"><span style="font-size: 18px;">Given a positive integer N, print count of set bits in it. </span></h1><div style="text-align: left;"><span style="font-size: 18px;">The Question is what is set bit?</span></div><div style="text-align: left;"><span style="font-size: 18px;"> </span></div><div style="text-align: left;"><span style="font-size: 18px;">Ans: The total number of ones "1" in the binary representation of a number are the set bits<br /></span></div><div style="text-align: left;"><span style="font-size: 18px;">Ex: let the number be <b>10 </b>its binary equivalent is <b>1010 </b>so total set bits are <b>2.</b></span></div><div style="text-align: left;"><span style="font-size: 18px;">similarly let the number be <b>21 </b>its binary equivalent is <b>10101 </b>total set bits are <b>3.</b></span></div><div style="text-align: left;"><span style="font-size: 18px;"><b> </b> <br /></span></div><p style="text-align: left;"><b><span style="font-size: 18px;">To count the set bits we will use 2 methods .</span></b></p><p style="text-align: left;"><span style="color: #3d85c6;"><span style="font-size: 18px;">1.First one is by converting number to its Binary equivalent and counting the number of ones.</span></span></p><p style="text-align: left;"><span style="color: #3d85c6;"><span style="font-size: 18px;">2.Second will be using bit manipulation.</span></span></p><p style="text-align: left;"><span style="color: #3d85c6;"><span style="font-size: 18px;"> </span></span></p><p style="text-align: left;"><span style="font-size: 18px;"><b>Method 1:Convert the number to binary string and count ones.</b></span></p><p style="text-align: left;"><span style="font-size: 18px;"><b>Code</b>:</span></p><div style="text-align: left;"><span style="font-size: 18px;"><span style="color: #073763;">public</span> <span style="color: #073763;">class</span> <span style="color: #38761d;">SetBits</span> {<br /> <br /> <span style="color: #073763;">static</span> <span style="color: #073763;">int</span> <span style="color: #38761d;">getBits</span>(int N)<br /> {<br /> int count=0;<br /> String <span style="color: #38761d;">binary</span>=Integer.<span style="color: #38761d;">toBinaryString</span>(N);<br /> for(int i=0;i<binary.length();i++)<br /> {<br /> if(binary.<span style="color: #38761d;">charAt</span>(i)=='1')<br /> {<br /> count++;<br /> }<br /> }<br /> return count;<br /> }<br /> <span style="color: #073763;">public</span> <span style="color: #073763;">static</span> <span style="color: #073763;">void</span> <span style="color: #38761d;">main</span>(String[] args) {<br /> // TODO Auto-generated method stub<br /> int N=11;<br /> int count=<span style="color: #38761d;">getBits</span>(N);<br /> System.out.println("Set Bits are : "+count);<br /><br /> }<br /><br />}</span></div><div style="text-align: left;"><span style="font-size: 18px;"><b></b><br /></span></div><div style="text-align: left;"><span style="font-size: 18px;"><b>Method 2: Using Bit Manipulation</b></span></div><div style="text-align: left;"><span style="font-size: 18px;"><b>Algo:</b></span></div><div style="text-align: left;"><ul style="text-align: left;"><li><span style="font-size: 18px;">we use bitwise & to get the set bits to recall what <b>& </b>operator does lets see.</span></li></ul><div style="margin-left: 160px; text-align: left;"><span style="font-size: 18px;">{1,0}->0</span><br /></div></div><div style="margin-left: 160px; text-align: left;"><span style="font-size: 18px;">{0,1}->0</span><br /></div><div style="margin-left: 160px; text-align: left;"><span style="font-size: 18px;">{1,1}->1</span><span style="font-size: 18px;"> </span></div><div style="text-align: left;"><div style="margin-left: 160px; text-align: left;"><span style="font-size: 18px;">{0,0}->0 </span></div><div style="margin-left: 160px; text-align: left;"><span style="font-size: 18px;"> </span></div><div style="text-align: left;"><ul style="text-align: left;"><li><span style="font-size: 18px;">now lets see what <b>n&(n-1)</b> does , let <b>n=10 </b></span></li></ul><div style="margin-left: 120px; text-align: left;"><span style="font-size: 18px;"><b>10&(10-1)=10&9=1 0 1 0 & 1 0 0 1 = 1 0 0 0 </b></span><br /></div><div style="margin-left: 40px;"><span style="font-size: 18px;"></span></div></div><div style="text-align: left;"><div style="margin-left: 80px; text-align: left;"><span style="font-size: 18px;"> <span> </span><span> </span><span> </span><span> </span><span> </span><span> </span><span> </span><span> </span><span> </span><span> </span><span> </span>^ <br /></span></div></div><div style="text-align: left;"><span style="font-size: 18px;"> As you can see this removes the set bit from last , so if we run through the number till the number becomes <b>0 </b>we can count the number of set bits using the above <b>& </b>operator.<br /></span></div><div style="margin-left: 160px; text-align: left;"><span style="font-size: 18px;"> </span></div><span style="font-size: 18px;"></span></div><div style="text-align: left;"><span style="font-size: 18px;"> <br /></span></div><div style="text-align: left;"><span style="font-size: 18px;"><b>Code:</b></span></div><div style="text-align: left;"><span style="font-size: 18px;"><b> </b><br /><span style="color: #0b5394;">public</span> <span style="color: #0b5394;">class</span> <span style="color: #38761d;">SetBits</span> {<br /><br /> <span style="color: #0b5394;">static</span> <span style="color: #0b5394;">int</span> <span style="color: #38761d;">getSetBits</span>(int N)<br /> {<br /> int count=0;<br /> while(N>0)<br /> {<br /> count++;<br /> N=N&(N-1);<br /> }<br /> return <span style="color: #38761d;">count</span>;<br /> <br /> }<br /> <br /> <span style="color: #0b5394;">public</span> <span style="color: #0b5394;">static</span> <span style="color: #0b5394;">void</span> <span style="color: #38761d;">main</span>(String[] args) {<br /> // TODO Auto-generated method stub<br /> <span style="color: #38761d;">int</span> <span style="color: #38761d;">N=11</span>;<br /> int count=<span style="color: #38761d;">getSetBits</span>(N);<br /> System.out.println("Set Bits are : "+count);<br /><br /> }<br /><br />}</span></div><div style="text-align: left;"><span style="font-size: 18px;"></span></div><br /><div style="text-align: left;"><span style="font-size: 18px;"><b> lets evaluate the code:</b></span></div><div style="text-align: left;"><ol style="text-align: left;"><li><span style="font-size: 18px;">N=11 is passed to the function , it initailizes the count=0</span></li><li><span style="font-size: 18px;">We run the while loop till the N=0</span></li><li><span style="font-size: 18px;">After iteration N will be addressed in the binary form N=11 means --><b>1011</b><br /></span></li></ol><div style="margin-left: 80px; text-align: left;">1.Count=1 --> N= <b>1010</b></div><div style="margin-left: 80px; text-align: left;">2. Count =2 -> N=<b>1000</b></div><div style="margin-left: 80px; text-align: left;">3.Count =3 ->N=<b>0000</b></div><div style="margin-left: 80px; text-align: left;"><b> </b></div><div style="text-align: left;"> <span> </span>now we come out of the loop and return count.</div><div style="text-align: left;"><b> </b></div><div style="text-align: left;"><b> </b></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEimOIecnAfwfrS2n465fPXreGlSHicXkp_i-pS4LA4ycdUhP2a7Yf8g6H7Bf8iUbxocJ-QuNF96SKOtE-Iq_89X5NjvdACSkR6Y6AJ_g7l4adB7uaPzCRWjG8fonwuIg9ub9MJo6KRQQT9D/s521/Setbits.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="94" data-original-width="521" height="116" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEimOIecnAfwfrS2n465fPXreGlSHicXkp_i-pS4LA4ycdUhP2a7Yf8g6H7Bf8iUbxocJ-QuNF96SKOtE-Iq_89X5NjvdACSkR6Y6AJ_g7l4adB7uaPzCRWjG8fonwuIg9ub9MJo6KRQQT9D/w640-h116/Setbits.png" width="640" /></a></div><br /><div style="text-align: left;"><br /><b><br /></b></div></div><div style="text-align: left;"><br /><span style="font-size: 18px;"><br /></span></div><div class="blogger-post-footer">Contact Us By Email Or Get in touch by Commenting on posts</div>CodeityWebhttp://www.blogger.com/profile/01377516264362368650noreply@blogger.com0