Skip to main content

Posts

Making a Set in Java to avoid duplicates for complex data types

For simple data types, Set will work to avoid the duplicates using the Object class equals() and hashcode() methods, but to utilize the same behavior for the complex data types, we have to override the hashcode()  and equals() methods as shown in the below example:
Recent posts

Solution Repeated String Hacker Rank

Challenge: Lilah has a string,  , of lowercase English letters that she repeated infinitely many times. Given an integer,  , find and print the number of letter  a 's in the first   letters of Lilah's infinite string. For example, if the string   and  , the substring we consider is  , the first   characters of her infinite string. There are   occurrences of  a  in the substring. Function Description Complete the  repeatedString  function in the editor below. It should return an integer representing the number of occurrences of  a  in the prefix of length   in the infinitely repeating string. repeatedString has the following parameter(s): s : a string to repeat n : the number of characters to consider Input Format The first line contains a single string,  . The second line contains an integer,  . Constraints For   of ...

Tricks to follow while programming

1. Before start coding resolve the algorithm, The better way is to trace the logic that your mind followed already. Because on looking into the problem, your mind will give the solution. You have to know how the mind got that solution, in which way ? 2. We have to remember any time you are not going with Standard increment or decrements, you will loose the control on the flow then you have take it to your hand by using a if condition and break. 3. Here we need to talk about logical AND operator. It will not validate the second condition if the first condition fails. We effectively utilized that feature here. Suppose A && B is there. assume expression A is false, then it will not validate for B, it will go with the false result. 4. I feel we should always be strict to the boundaries, of the problem while developing the solution. 

Solved Jumping on the Clouds

Challenge: Emma is playing a new mobile game that starts with consecutively numbered clouds. Some of the clouds are thunderheads and others are cumulus. She can jump on any cumulus cloud having a number that is equal to the number of the current cloud plus   or  . She must avoid the thunderheads. Determine the minimum number of jumps it will take Emma to jump from her starting postion to the last cloud. It is always possible to win the game. For each game, Emma will get an array of clouds numbered   if they are safe or   if they must be avoided. For example,   indexed from  . The number on each cloud is its index in the list so she must avoid the clouds at indexes   and  . She could follow the following two paths:   or  . The first path takes  jumps while the second takes  . Function Description Complete the  jumpingOnClouds  function in the editor below. It should return th...