Home Misc
Category:

Misc

View Miscellaneous problems

  • Misc

    [Hackerrank] – Number Line Jumps Solution

    by nikoo28
    4 minutes read

    Let us try to understand this problem statement first. We are in charge of a fancy circus and 2 kangaroos have to jump on a number line. The interesting part is that both the kangaroos have a different start position, and different jump distances. It is a little tricky to understand, the problems asks to calculate the number line jumps such that both the kangaroos are at the same position. We will try to simplify this using some diagrams, and see how the kangaroos actually jump. In the given test …

    1 FacebookTwitterLinkedinWhatsappEmail
  • Question: Given an integer N, find all the prime factors of the number. Input: N = 45 Output: 3 5 Finding prime factors of any numbers is a very important concept in number theory. One sample problem is available here in which we have to find the largest prime factor. The mathematical concept behind this problem is simple. We start with ‘2’ and keep dividing the original number to get rid of composite factors. Continue this process until the number is no longer divisible by 2. This means that at …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Given an integer N, find the prime numbers in that range from 1 to N. Input: N = 25Output: 2, 3, 5, 7, 11, 13, 17, 19, 23 We have several ways of finding prime numbers. Some of the methods are discussed in the these posts. Method 1 Method 2 Method 3 In this post we will find the first N prime numbers using the Sieve of Eratosthenes. This technique is helpful in scenarios when we have to give immediate results and we need to query the prime numbers …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Misc

    Find the sum of even Fibonacci numbers.

    by nikoo28
    3 minutes read

    Question: Find the sum of even fibonacci numbers upto a limit N. Input:100Output: 44 Fibonacci numbers are a miracle of Math and are defined as:- Thus the Fibonacci series can be given as0, 1, 1, 2, 3, 5, 8, 13, 21… Let us try to understand the question. We need to find all the Fibonacci numbers till a limit ‘N’ and then sum only the even numbers in them.Example:N = 10 Fibonacci numbers: 0, 1, 1, 2, 3, 5, 8 Sum of even numbers: 2 + 8 = 10 N …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Misc

    [Hackerrank] – Multiples of 3 and 5

    by nikoo28
    3 minutes read

    Question: If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below ‘N’ Input: N = 100 Output: 2318 The most naive approach to solve this problem will be Iterate over each number till N If the number is divisible by 3 or 5, add it to the sum Print the sum This approach can work well to numbers …

    0 FacebookTwitterLinkedinWhatsappEmail
  • ArraysMisc

    Convert a number into a string of words

    by nikoo28
    4 minutes read

    Question: Given an integer N, convert it into a string of words. Input: N = 345 Output: three hundred forty five This is one of the most common problems that we come across in out daily lives. We need to input a number from the user and print it in words. First, we perform a number of checks on the input string. If it is in any way invalid, the result is the empty string (“”). Next, we take note of whether or not the string begins with a ‘-‘. …

    0 FacebookTwitterLinkedinWhatsappEmail
  • MiscStrings

    Common String Algorithms

    by nikoo28
    2 minutes read

    In this post, I would try to implement the following string-related algorithm questions. I would like to implement these using Java and try to make additional comments which I think that are useful for understanding the implementations of some type of data structures in Java programming language. Non Repeated Characters in String : Return the unique characters in a given letter. Anagram Strings : Given two strings, check if they’re anagrams or not. Two strings are anagrams if they are written using the same exact letters, ignoring space, punctuation and …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Misc

    Find the first N prime numbers. (Method 3)

    by nikoo28
    3 minutes read

    Question: Given an integer N, find the prime numbers in that range from 1 to N. Input: N = 25Output: 2, 3, 5, 7, 11, 13, 17, 19, 23 We discussed the basic approaches to find the first N prime numbers in these posts Find the first N prime numbers (Method 1) Find the first N prime numbers (Method 2) It is advised that you go through the above posts to get a basic understanding of the approach that we have used to determine the prime numbers. Now lets analyze …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Misc

    Find the first N prime numbers. (Method 2)

    by nikoo28
    2 minutes read

    Question: Given an integer N, find the prime numbers in that range from 1 to N. Input: N = 25 Output: 2, 3, 5, 7, 11, 13, 17, 19, 23 We discussed the most basic approach to find the first N prime numbers in this post. Find the first N prime numbers. (Method 1) Please go through the post if you are not familiar with the naive methods. They are necessary to learn how the code can be optimized further. So let us try to further optimize the previously discussed …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Misc

    Find the first N prime numbers. (Method 1)

    by nikoo28
    2 minutes read

    Question: Given an integer N, find the prime numbers in that range from 1 to N. Input: N = 25 Output: 2, 3, 5, 7, 11, 13, 17, 19, 23 Today let us discuss about a very common but very interesting problem “To find prime numbers in first N Natural numbers “. I will be taking here a very specific approach of first giving definition of prime numbers , using that definition to derive the algorithm, and then using different properties or results that can be further applied to our …

    0 FacebookTwitterLinkedinWhatsappEmail

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More