Skip to main content

Posts

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 ) t...

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 d...

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 th...

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 ou...

Highlights

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

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" ...

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 d...