[Hackerrank] – Between Two Sets Solution

    by nikoo28

    Question: You will be given two arrays of integers and asked to determine all integers between two sets that satisfy the following two conditions:
    – The elements of the first array are all factors of the integer being considered
    – The integer being considered is a factor of all elements of the second array

    Input:
    a = { 2, 6 }
    b = { 24, 36 }

    Output:
    2

    I really want to simplify this really confusing problem statement first.

    The hardest part about this problem is to understand what is it actually saying. To put it in really simple words, you are provided with 2 arrays. Now you need to find a count of integers that satisfy the conditions:

    • The integers should be multiples of each element of first array.
    • The integers should be factors of each element of second array.

    That is all you need to find out in the problem. Just return the count and your solution would have a successful submission.

    example test case to find integers between two sets
    Fig: Example test case

    For the above example, you can find 2 such integers. 6 and 12. You can verify that by:

    First condition (multiple of each element of array 'a')
    2 * 3 = 6
    6 * 1 = 6
    2 * 3 = 12
    6 * 2 = 12
    
    Second condition (factor of each element of array 'b')
    24 % 6 = 0
    24 % 12 = 0
    36 % 6 = 0
    36 % 12 = 0Code language: PHP (php)

    So you could find 2 such integers between two sets, and hence that is the answer.

    Brute Force Method:

    A brute force method to solve this problem would be:

    1. Find all the multiples of each element of first array.
    2. Get all the factors of each element of second array.
    3. Make an intersection of all the above integers.
    4. Count the number of common integers you could find.
    Brute Force Method to solve Between two sets
    Fig: Brute Force Method to solve Between two sets

    The only problem with this approach is that you spend a lot of time just to compute the multiples and finding the common elements. This would result in a very high time complexity. Surely, we need to find an optimized way.

    Efficient Way:

    You need to stop and think, why do you even need to find out all the multiples of array a. We are interested in integers that are a multiple of each of the element of array a. Thus, if we find the lowest common multiple, that can give us a starting point.

    Similarly, we do not need all the factors of all the elements of array b. We are just interested in integers that are factors of every element. So, finding the greatest common divisor can give you a good starting point.

    Next, we are sure that the integers are in the range of LCM (Lowest common multiple) and GCD (Greatest common divisor). The algorithm will be:

    1. Find the LCM of all integers of array a.
    2. Next, find the GCD of all integers of array b.
    3. Count the number of multiples of LCM that are divisible by the GCD.

    Code:

      int getGCD(int n1, int n2) {
        if (n2 == 0) {
          return n1;
        }
        return getGCD(n2, n1 % n2);
      }
    
      int getLCM(int n1, int n2) {
        if (n1 == 0 || n2 == 0)
          return 0;
        else {
          int gcd = getGCD(n1, n2);
          return Math.abs(n1 * n2) / gcd;
        }
      }
    
      public int getTotalX(List<Integer> a, List<Integer> b) {
        int result = 0;
    
        // Get LCM of all elements of a
        int lcm = a.get(0);
        for (Integer integer : a) {
          lcm = getLCM(lcm, integer);
        }
    
        // Get GCD of all elements of b
        int gcd = b.get(0);
        for (Integer integer : b) {
          gcd = getGCD(gcd, integer);
        }
    
        // Count multiples of lcm that divide the gcd
        int multiple = 0;
        while (multiple <= gcd) {
          multiple += lcm;
    
          if (gcd % multiple == 0)
            result++;
        }
    
        return result;
      }
    Code language: Java (java)

    Time Complexity: O(n \log n)
    Space Complexity: O(\log n)
    You can also find the code and testcases on Github as well.

    Video Explanation:

    YouTube player
    0 comments
    1 FacebookTwitterLinkedinWhatsappEmail
  • Arrays

    [Hackerrank] – Pairs Solution

    by nikoo28
    5 minutes read

    Question: Given an array of integers, find the number of pairs of array elements that have a difference equal to the target value. Example: Input:arr[ ] = {1, 2, 3, 4}k = 1 Output: 3 Problem Statement Let us try to simplify the problem statement first and understand the sample test case. You are given an array of unique integers which is in any random order. Along with the array, you are also given a target value k. If you pick up any 2 integers from the array, they would…

    0 FacebookTwitterLinkedinWhatsappEmail
  • Arrays

    [Hackerrank] – Missing Numbers Solution

    by nikoo28
    4 minutes read

    Question: You are required to find missing numbers that are left out while an artist transports numbers from one array to other. The output array should be sorted. Input:arr [ ] = {7, 2, 5, 3, 5, 3}brr [ ] = {7, 2, 5, 4, 6, 3, 5, 3} Output:Missing numbers: {4, 6} Problem Statement: Let me try to simplify the problem a little first. The problem description is quite verbose and we narrow down it to quite an extent. To summarize, the artist has an original array brr, and…

    0 FacebookTwitterLinkedinWhatsappEmail
  • Arrays

    [Hackerrank] – Equal Stacks Solution

    by nikoo28
    6 minutes read

    Question: Given 3 arrays, where each element represent the height of a cylinder. Find the maximum possible height of equal stacks by removing one or more cylinders from the original stack. Example: Input: n1[] = {3, 2, 1, 1, 1}n2[] = {4, 3, 2}n3[] = {1, 1, 4, 1} Output: 5 Problem Statement Let us try to simplify the problem statement first. The problem states that you are provided with 3 sets of arrays. Each element in the array represents the height of a cylinder. Now, all these cylinders are…

    0 FacebookTwitterLinkedinWhatsappEmail
  • Arrays

    [Hackerrank] – Left Rotation Solution

    by nikoo28
    7 minutes read

    Question: You are given an array of integers. After a left rotation of k times, find the resultant array. Example 1:Input: arr [ ] = {1, 2, 3, 4, 5}, size = 5, k = 2Output: {3, 4, 5, 1, 2} Example 2:Input: arr [ ] = {4, 8, 15, 16, 23, 42}, size = 6, k = 12Output: {4, 8, 15, 16, 23, 42} Terminology: To understand rotation of an array, you can assume that the array is kind of on an infinite conveyor belt, that keeps on looping.…

    0 FacebookTwitterLinkedinWhatsappEmail
  • Theory

    Analysis of Algorithms

    by nikoo28
    3 minutes read

    There can be several aspects when you think about analysis of algorithms. You may be writing a large scale application where you have to decide between two choices. You may be trying to submit a solution to some online competition and you want a higher score, or probably you may just want to write fewer lines of code. Analysis during an Interview Let’s say you are appearing for an interview. The person who is interviewing you gives you a problem to solve. He or she is not just interested in…

    0 FacebookTwitterLinkedinWhatsappEmail
  • Theory

    What is the Big O notation? [Simplified]

    by nikoo28
    8 minutes read

    As soon as you hear the word ‘Big O’, does your mind go “Oh my god! Oh no! This again?” The next thing following it would be time complexity and what not. Suddenly, your brain starts getting this confusing signals and you lose track of what is happening. If you have felt this at some point of time while learning programming, then I want to tell you that you are not alone. In fact, you are proceeding in the right direction. But all these notations can start to get overwhelming…

    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