Skip to main content

Posts

Showing posts from July, 2020

Java Loops | HackerRank solutions -codewithyasar

  Objective In this challenge, we're going to use loops to help us do some simple math. Task Given an integer,  , print its first   multiples. Each multiple   (where  ) should be printed on a new line in the form:  N x i = result . Input Format A single integer,  . Constraints Output Format Print   lines of output; each line   (where  ) contains the   of   in the form: N x i = result . Sample Input 2 Sample Output 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10 2 x 6 = 12 2 x 7 = 14 2 x 8 = 16 2 x 9 = 18 2 x 10 = 20 Explanation:           Here, we just need to use for loops to achieve the result Solution : import java.io.* ; import java.math.* ; import java.security.* ; import java.text.* ; import java.util.* ; import java.util.concurrent.* ; import java.util.regex.* ; public class Solution { public static void main ( String [] args ) throws IOException { BufferedReader bufferedReader = new BufferedReader ( new InputStreamReader ( Syste

Dynamic Programming:Memoization| Program to find Fibonaaci number in that postion using Memoization Technique

Idea Behind Memoize technique: In the Recursion there are so many repetitive calls in that,in order to overcome that we need to declare an array to store the intermediate results and use that. Logic: Declare the Memo array and store the result and call them by position later. Code Logic: input to find fibonacci number at the position function declaration fib(n,memo) if memo[n]!=null:return memo[n] base condition n==1 or n==2 return 1 else return fib(n-1)+fib(n-2) Program:

Dynamic Programming:Recursion | Program to find Fibonaaci number in that postion using Recursion

Idea Behind Recursion:                Function call itself is called Recursion Like a binary tree it grows up to the end of the tree and start computation from the base case Logic: we need to define base case Then we set the recursive calls Code Logic: input the position we need to find fibonacci 'p' function for fibRecur() basecase if n==1 or n==2 return 1 else return fibRecur(n-1) +fibRecur(n-2) Program:

Sieve of Eratosthenes:Simple Way To Find Prime Numbers

Explanation:          We can simply define what is the range, we need to find prime numbers in between.          Starting from min prime we need to eliminate the tables of min prime ..so on and so forth          Repeat for next number. By SKopp at German Wikipedia - Own work , Original image at Image:Animation_Sieve_of_Eratosth.gif , CC BY-SA 3.0 , Link Program: Output:              Enter upper limit of range: 10             {2, 3, 4, 5, 6, 7, 8, 9, 10}             2    3   5  7     

Haunted Hotel

Haunted Hotel Follow the instructions to live 6th June 1906, there was a big fire accident at a 5 star hotel in which more than 160 people died, from that time no one even dared to go near that burnt place, because it was believed that the place became haunted. This fear among people went increasing due to the suspicious deaths of the people who tried entering the hotel. More than 100 years have passed, but that has not reduced thee fear among the people even a bit. Dhoni is a person who likes adventures, so wanted to see what actually is the mystery of the hotel. So on a Friday evening he went inside the hotel with a camera which was live streamed live on his Facebook page.   The live stream lasted not even for an hour, all the electronic equipment he had began malfunctioning. He might be brave, but being alone in a place which is believed to be haunted and that too in late evening, his nerves got him. It was so quite so that, he could hear his own heart beat increasing as a drum

Mountain Peak Sequence

Mountain Peak Sequence Consider the first three natural numbers 1, 2, 3. These can be arranged in the following ways: 2, 3, 1 and 1, 3, 2. Inboth of these arrangements, the numbers increase to a certain point and then decrease. A sequence with this property is called a “mountain peak sequence”. Given an integer N, write a program to find the remainder of mountain peak arrangements that can be obtained by rearranging the numbers 1, 2, …, N. Input Format : One line containing the integer N Input Constraints : Mod = 10^9 +7 3 ≤ N ≤ 10^9 Output Format : An integer m, giving the remainder of the number of mountain peak arrangements that could be obtained from 1, 2, …, N is divide by Mod Sample Input : 3 Sample Output : 2 Explanation:      If mountain like sequence are formed using the number then count it. Understand the logic: For input 3, 6 combinations can be formed,out of these 2 is a mountain sequence. For input 4, 24 combinations can be formed,out of these,6   Mounta

Even Sum

Even Sum A number is even if it is divisible by 2, but in this case a number is even if the active bits (1s in its binary representation) of a given number are 2. So your task is to find the sum of first N even numbers. Input Format : The first line of the input contains an integer T denoting the number of test cases Each of the next T lines contains a single integer N. Input Constraints : 0 < N <=10^5 Output Format : For each test case, print a single integer denoting sum of first N even numbers mod 1000000007. Sample Input : 2 15 20 Sample Output : 315 666 Explanation of Question: They are asking for sum of even numbers but here the even numbers are those having even number of active bits Active bits are nothing but one. Logic:         Active even bits come in the format like            3,5,6,9,10,12,17,18,20,24...             There are two patterns we find out               1.2**i+1 [2+1,4+1,8+1,...]                2.The number + 2**j [5+2**0=6

Square Free Number

Square Free Number In the theory of numbers, square free numbers have a special place. A square free number is one that is not divisible by a perfect square (other than 1). Thus 72 is divisible by 36 (a perfect square), and is not a square free number, but70 has factors 1, 2, 5, 7, 10, 14, 35 and 70. As none of these are perfect squares (other than 1), 70 is a squarefree number.  For some algorithms, it is important to find out the square free numbers that divide a number. Note that1 is not considered a square free number. In this problem, you are asked to write a program to find the number of square free numbers that divide a given number.  Input Format : The only line of the input is a single integer N which is divisible by no prime number larger than 19 Input Constraints : N < 10^9 Output Format : One line containing an integer that gives the number of square-free numbers (not including 1) Sample Input : 20 Sample Output : 3 Explanation: Find out the number which is square free

GCD Matrix

GCD Matrix In mathematics, the greatest common divisor (gcd) of two or more integers, which are not all zero, is the largest positive integer that divides each of the integers. For example, the gcd of 8 and 12 is 4. You are given a 2D array, find the GCD of the elements in the diagonals of the matrix. Input Format : The first line of input has one single integer N The next N lines will have N spaced integer inputs Input Constraints : 1<=N<=100 1<=a[i]<=100 Output Format : One single integer output denoting the GCD of the diagonal elements Sample Input : 2 1 2 3 4 Sample Output : 1 Explanation: We need to find the gcd among the diagonal of the matrix Program:

Accico Equi Pair

Accico Equi Pair: Ron Wesley has been bit by a three headed snake and Harry Potter is searching for a potion. The Witch promises to tell the ingredients of the medicine if Harry can find equi pair of an array. Listen to the conversation between Harry The witch to know more about equi pairs. Conversation:- The Witch : To find the equi pair, you must know how to find the slices first. Harry : What is a slice? The Witch : If Z is an array with N elements, a slice of indices (X, Y) is Z[X] + Z[X+1]...Z[Y] Harry : How can I use it to find equi pair? The Witch : (a, b) is an equi pair if slice of (0, a-1) = slice of (a+1, b-1) = slice of (b+1, N-1) and b&gt;a+1 and size of array &gt; 4 Input Format : The first line of input has one single integer N where N will be the size of the Array An array of N integers delimited by white space Input Constraints : Zi &gt;= 0 and 1&lt;= i &lt;=N size of array (N) &gt; 4 b &gt; (a+1) Output Format : Print equi

Least Number

Least Number You are given an array of positive numbers, find the minimum number which is not present in the list, which also cannot be represented as a combination of numbers in the array. Input Format : The first line of input has one single integer input N The second line of input has N spaced integers Input Constraints : 1<=N<=10^3 1<=a[i]<=10^3 Output Format : One single integer output Sample Input :4 1 2 5 6 Sample Output : 4 Logic: To find first least number.we need to sort the array. And we need to identify the least number is not in combination of elements so,add the elements and compare to find first least number which is not in list and combination. Code logic: Get input from the user. Get input array Sort the given array Intialize as ans=1 for I in arr: check ans >=i: if greater add with I else:break Program:

Search Engine Optimization

What is SEO? SEO stands for search engine optimization it ranks the webpage which webpage to display first and second major companies pay to the seo to push thier pages up in the google search  How SEO works You might think of a search engine as a website you visit to type (or speak) a question into a box and Google, Yahoo!, Bing, or whatever search engine you're using magically replies with a long list of links to webpages that could potentially answer your question. That's true. But have you ever stopped to consider what's behind those magical lists of links? Here's how it works: Google (or any search engine you're using) has a crawler that goes out and gathers information about all the content they can find on the Internet. The crawlers bring all those 1s and 0s back to the search engine to build an index. That index is then fed through an algorithm that tries to match all that data with your query. There are a lot of factors that go into a search engine's

Highlights

Super ASCII String Checker | CodeVita | PYTHON CODE

Problem: In the Byteland country a string "S" is said to super ascii string if and only if count of each character in the string is equal to its ascii value. In the Byteland country ascii code of 'a' is 1, 'b' is 2 ...'z' is 26. Your task is to find out whether the given string is a super ascii string or not. Input Format: First line contains number of test cases T, followed by T lines, each containing a string "S". Output Format: For each test case print "Yes" if the String "S" is super ascii, else print "No" Constraints: 1<=T<=100 1<=|S|<=400, S will contains only lower case alphabets ('a'-'z'). Explanation: For eg1 - String "bba" is super ascii string Explanation:- .String "bba" The count of character 'b' is 2. Ascii value of 'b' is also 2. The count of character 'a' is 1. Ascii value of 'a' is also 1. Hence string "bba"

Lexi String | codevita |Python Solution

Lexi String Little Jill jumbled up the order of the letters in our dictionary. Now, Jack uses this list to find the smallest lexicographical string that can be made out of this new order. Can you help him? You are given a string P that denotes the new order of letters in the English dictionary. You need to print the smallest lexicographic string made from the given string S. Constraints: 1 <= T <= 1000 Length (P) = 261 <= length (S) <= 100 All characters in the string S, P are in lowercase Input Format The first line contains number of test cases T The second line has the string P The third line has the string S Output Print a single string in a new line for every test case giving the result Test Case Example 1 Input 2 polikujmnhytgbvfredcxswqaz abcd qwryupcsfoghjkldezxvbintma ativedoc Output bdca codevita Explanation: The transformed smallest lexicographical strings are in order they would be if order of letters are changed to string P

Haunted Hotel

Haunted Hotel Follow the instructions to live 6th June 1906, there was a big fire accident at a 5 star hotel in which more than 160 people died, from that time no one even dared to go near that burnt place, because it was believed that the place became haunted. This fear among people went increasing due to the suspicious deaths of the people who tried entering the hotel. More than 100 years have passed, but that has not reduced thee fear among the people even a bit. Dhoni is a person who likes adventures, so wanted to see what actually is the mystery of the hotel. So on a Friday evening he went inside the hotel with a camera which was live streamed live on his Facebook page.   The live stream lasted not even for an hour, all the electronic equipment he had began malfunctioning. He might be brave, but being alone in a place which is believed to be haunted and that too in late evening, his nerves got him. It was so quite so that, he could hear his own heart beat increasing as a drum