[Hackerrank] – Multiples of 3 and 5

    by nikoo28

    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 till N = 10000. But can take a lot of time for higher numbers. Thus we apply some trick to make our solution efficient.

    We know that multiples of 3 form an AP as

    3, 6, 9, 12, 15, 18…

    Similarly multiples of 5 form an AP as

    5, 10, 15, 20, 25, 30…

    Sum both and we get

    3, 5, 6, 9, 12, 15, 15, 18, 20…

    You’ll notice that 15 is repeated. In fact all the multiples of 15 or 5 * 3 are repeated because it got counted twice once in the series of 3 and again in the series of 5. Hence we’ll subtract the series of 15.

    Now we know that sum of AP is given by
    S=\frac{n\ *\ (a\ +\ l)}{2}
    here n is the number of terms, a is the starting term, and l is the last term.

    Our final answer will be
    S_3 + S_5 - S_{15}
    Go over the comments in the source code for a better understanding.

    import java.util.Scanner;
    
    /**
    * Created by studyalgorithms
    */
    class Problem1 {
    
        public static void main(String[] args) {
    
            Scanner sc = new Scanner(System.in);
            long t = sc.nextLong();
            for (long i = 0; i < t; i++) {
                long n = sc.nextLong();
                long a = 0, b = 0, d = 0;
    
                // Here a, b and d are the count of numbers divisible by 3, 5 and 15 respectively
                a = (n - 1) / 3;
                b = (n - 1) / 5;
                d = (n - 1) / 15;
    
                // To get the sum of all numbers divisible by 3 (sum3) i.e. 3+6+9+-----+3n = 3(1+2+3+---+n) = 3*n(n+1)/2
                // Similarly sum of all numbers divisible by 5 (sum5) is 5*n(n+1)/2
                // Sum of numbers divisible by 15 (sum15) is 15*n(n+1)/2.
                long sum3 = 3 * a * (a + 1) / 2;
                long sum5 = 5 * b * (b + 1) / 2;
                long sum15 = 15 * d * (d + 1) / 2;
                long c = sum3 + sum5 - sum15;
                System.out.println(c);
            }
        }
    }
    Code language: Java (java)

    A working implementation of the code can be found here.
    This problem is also available on HackerRank and Project Euler.

    0 comments
    0 FacebookTwitterLinkedinWhatsappEmail
  • Strings

    [Hackerrank] – Palindrome Index

    by nikoo28
    3 minutes read

    Question: Given a string, identify the index of character to be removed to change the string into a palindrome. If the string cannot be converted to palindrome or is already a palindrome just return -1. Input: abckdeedcbaOutput: 3 (0 based indexing) To start off with the problem, we must first understand that there can be two possible ways to make the string a palindrome. If the given string is “bcbc”. If we remove the first ‘b’, the string becomes “cbc” which is a palindrome. If we remove the last ‘c’,…

    0 FacebookTwitterLinkedinWhatsappEmail
  • Let us try to simplify the problem as much as we can. We are given with some of the terms. Weights:- According to the problem each character in the English alphabet has been assigned a weight. ‘a’ has been given a weight of 1, ‘b’ has a weight of 2, and so on. The weight of ‘z’ is 26. All the letters are assumed to be lowercase for simplicity. Weight of a string:- This is defined as the sum of all the characters in the string. Example if a string…

    0 FacebookTwitterLinkedinWhatsappEmail
  • Strings

    Pangrams

    by nikoo28
    2 minutes read

    Question: Given a string, identify if it is a pangram. Input: The quick brown fox jumps over the lazy little dog.Output: pangram To start with the problem statement we must first understand what is a pangram. A pangram is a string that contains all the characters in the English alphabet at-least once. This means that the string ‘the quick brown fox jumps over the lazy little dog’ will cover each of the letter from ‘a-z’. So how to identify if a string is a pangram?You need to keep a check…

    0 FacebookTwitterLinkedinWhatsappEmail
  • Strings

    [HackerRank] – Two Characters

    by nikoo28
    7 minutes read

    A string is said to be valid when it has only distinct characters and none of them repeat simultaneously. For example, if string ‘s two distinct characters are x and y, then valid examples could be xyxyx or yxyxy but not xxyy or xyyx.Question: Given a sample string, we need to determine what is the maximum length of valid string that can be made by deleting any of the characters.Input: beabeefeabOutput: 5 Explanation: Let us try to understand the output for the sample test case.If we delete e and f,…

    0 FacebookTwitterLinkedinWhatsappEmail
  • Strings

    Next lexicographic string permutation

    by nikoo28
    4 minutes read

    Question: Given a string, find the next lexicographic permutation. Input: abcOutput: acb First of all let us try to understand what do you mean by next lexicographic string. To get a hold of the concept try to remember an English dictionary and how words are arranged in it. We start with the letter ‘a’, write all the words and then move to letter ‘b’ all the way to letter ‘z’. This is called as a lexicographic order.This is possible when we have all the letters in English alphabet available to…

    0 FacebookTwitterLinkedinWhatsappEmail
  • Arrays

    Kth largest element in an array (Method 1)

    by nikoo28
    3 minutes read

    Question: Given an unsorted array of integers and a number ‘k’. Return the kth largest element in the array.Input: arr = {3,2,1,5,6,4}, k = 2Output: 5 Given an unsorted array of integers, you need to determine the kth largest element. By taking a first glance at the problem we can be sure of one thing. If the array is already sorted in ascending order, we can simply return the element at arr[size – k]. This would give us the kth largest element. This is because the 2nd largest element is…

    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