Skip to main content

Posts

Showing posts from August, 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 ) t...

Consecutive Prime Sum | Codevita |Python

Problem Description Question – : Some prime numbers can be expressed as a sum of other consecutive prime numbers. For example 5 = 2 + 3, 17 = 2 + 3 + 5 + 7, 41 = 2 + 3 + 5 + 7 + 11 + 13. Your task is to find out how many prime numbers which satisfy this property are present in the range 3 to N subject to a constraint that summation should always start with number 2. Write code to find out the number of prime numbers that satisfy the above-mentioned property in a given range. Input Format: First line contains a number N Output Format: Print the total number of all such prime numbers which are less than or equal to N. Constraints: 2<N<=12,000,000,000 Input 20   Output: 2 (Below 20 there are two such members; 5 and 17) 5=2+3 17=2+3+65+7

Dining Table Seating Arrangement | Codevita | Python Solution

Problem Statemen t In a Conference ,attendees are invited for a dinner after the conference.The Co-ordinator,Sagar arranged around round tables for dinner and want to have an impactful seating experience for the attendees.Before finalizing the seating arrangement,he wants to analyze all the possible arrangements.These are R round tables and N attendees.In case where N is an exact multiple of R,the number of attendees must be exactly N//R,,If N is not an exact multiple of R, then the distribution of attendees must be as equal as possible.Please refer to the example section before for better understanding. For example, R = 2 and N = 3 All possible seating arrangements are (1,2) & (3) (1,3) & (2) (2,3) & (1) Attendees are numbered from 1 to N. Input Format: The first line contains T denoting the number of test cases. Each test case contains two space separated integers R and N, Where R denotes the number of round tables and N denotes the number of attendees. Output Format: Sin...

Smallest Multiple in Permuted digits | Codevita Solution |Python

Problem:  Given two integers N,d,find the smallest number that number that is a multiple of d that could be formed by permuting the digits of N .You must use all the digits of N.and if the smallest multiple of d has leading zeros,they can be dropped .If no such number exists,output -1. Input: A line containing two space separated integers.representing N and d. Output: A single line giving the permutation of N that is the smallest multiple of d,without any leading zeros,if any.If not such permutation exists, the output should be -1. Constraints: 1<=N<=10^12 1<=d<=1000000 Example 1: Input: 210 2 Output: 12 Example 2: Input: 531 2 Output: 1

Base_6 | Codevita Problem | Python Solution

Problem Description Given a sequence of distinct numbers a1, a2, ….. an, an inversion occurs if there are indices i<j such that ai > aj . For example, in the sequence 2 1 4 3 there are 2 inversions (2 1) and (4 3). The input will be a main sequence of N positive integers. From this sequence, a Derived Sequence will be obtained using the following rule. The output is the number of inversions in the derived sequence. Rule for forming derived sequence An integer may be represented in base 6 notation. In base 6, 10305 is 1x64 + 3x62 + 5 =1409. Note that none of the digits in that representation will be more than 5. The sum of digits in a base 6 representation is the sum of all the digits at the various positions in the representation. Thus for the number 1409, the representation is 10305, and the sum of digits is 1+0+3+0+5=9. The sum of digits may be done in the decimal system, and does not need to be in base 6 The derived sequence is the sum of the digits when the corresponding inte...

Bank Compare | Codevita solution | Python

Bank Compare There are two banks – Bank A and Bank B. Their interest rates vary. You have received offers from both banks in terms of the annual rate of interest, tenure, and variations of the rate of interest over the entire tenure.You have to choose the offer which costs you least interest and reject the other. Do the computation and make a wise choice. The loan repayment happens at a monthly frequency and Equated Monthly Installment (EMI) is calculated using the formula given below :EMI = loanAmount * monthlyInterestRate / ( 1 – 1 / (1 + monthlyInterestRate)^(numberOfYears * 12)) Input Format : First line: P principal (Loan Amount) Second line: T Total Tenure (in years). Third Line: N1 is the number of slabs of interest rates for a given period by Bank A. First slab starts from the first year and the second slab starts from the end of the first slab and so on. Next N1 line will contain the interest rate and their period. After N1 lines we will receive N2 viz. the number of slabs off...

Distribute Books | codevita Solution | Python

For enhancing the book reading, school distributed story books to students as part of the Children’s day celebrations. To increase the reading habit, the class teacher decided to exchange the books every weeks so that everyone will have a different book to read. She wants to know how many possible exchanges are possible. If they have 4 books and students, the possible exchanges are 9. Bi is the book of i-th student and after the exchange he should get a different book, other than Bi. B1 B2 B3 B4 – first state, before exchange of the books B2 B1 B4 B3 B2 B3 B4 B1 B2 B4 B1 B3 B3 B1 B4 B2 B3 B4 B1 B2 B3 B4 B2 B1 B4 B1 B2 B3 B4 B3 B1 B2 B4 B3 B2 B1 Find the number of possible exchanges, if the books are exchanged so that every student will receive a different book. Constraints 1<= N <= 1000000 Input Format Input contains one line with N, indicates the number of books and number of students. Output Output the answer modulo 1000000007. Test Case Example 1 Input 4 Output 9

Zombie World | Codevita Solution | Python Solution

One day Bob is playing Zombie World video game. In Zombie World game each round will contain N zombie's and each zombie's energy is Zi (where 1<=i<=N). Bob will start the round with B energy. In order to move to the next level Bob need to kill all the N zombie's but Bob can select any one among N Zombie's. If energy of Bob (B) is less than Zombie energy (Zi) then Bob will die and lose the round else Bob will won, during the fighting with zombie, Bob will lose his energy by (Zi%2)+(Zi/2). At any point of game Bob will play optimally. Now your task is to find out whether Bob can reach to the next level or not. Input Format: First line will contains B and N, separated by space, where B is the energy of Bob and N is the number of Zombie. Next line will contain N spaced integers each will represent the energy of zombie. Line 1 B N, where B is the energy of Bob and N is the number of Zombie Line 2 Zi, where Zi is a list containing energy of zombies separated by space. C...

String Rotation | codevita | Python Solution

Problem Description Rotate a given String in the specified direction by specified magnitude. After each rotation make a note of the first character of the rotated String, After all rotation are performed the accumulated first character as noted previously will form another string, say FIRSTCHARSTRING. Check If FIRSTCHARSTRING is an Anagram of any substring of the Original string. If yes print "YES" otherwise "NO". Input The first line contains the original string s. The second line contains a single integer q. The ith of the next q lines contains character d[i] denoting direction and integer r[i] denoting the magnitude. Constraints 1 <= Length of original string <= 30 1<= q <= 10 Input carrace  3  L 2  R 2 L 3 Output: NO Explanation: After applying all the rotations the FIRSTCHARSTRING string will be "rcr" which is not anagram of any sub string of original string "carrace". Logic:     Input the string and no.of.rotations in integer ...

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

Chakravyuha | CodeVita Problem | Python

Chakravyuha :         During the battle of Mahabharat, when Arjuna was far away in the battlefield, Guru Drona made a Chakravyuha formation of the Kaurava army to capture Yudhisthir Maharaj. Abhimanyu, young son of Arjuna was the only one amongst the remaining Pandava army who knew how to crack the Chakravyuha. He took it upon himself to take the battle to the enemies. Abhimanyu knew how to get power points when cracking the Chakravyuha. So great was his prowess that rest of the Pandava army could not keep pace with his advances. Worried at the rest of the army falling behind, Yudhisthir Maharaj needs your help to track of Abhimanyu's advances. Write a program that tracks how many power points Abhimanyu has collected and also uncover his trail  A Chakravyuha is a wheel-like formation. Pictorially it is depicted as below  A Chakravyuha has a very well-defined co-ordinate system. Each point on the co-ordinate system is manned by a certain unit of th...

FOREST FIRE | CODEVITA SOLUTION |PYTHON

Problem Statement: Roco is an island near Africa which is very prone to forest fire. Forest fire is such that it destroys the complete forest. Not a single tree is left.This island has been cursed by God , and the curse is that whenever a tree catches fire, it passes the fire to all its adjacent tree in all 8 directions,North, South, East, West, North-East, North-West, South-East, and South-West.And it is given that the fire is spreading every minute in the given manner, i.e every tree is passing fire to its adjacent tree.Suppose that the forest layout is as follows where T denotes tree and W denotes water. Your task is that given the location of the first tree that catches fire, determine how long would it take for the entire forest to be on fire. You may assume that the lay out of the forest is such that the whole forest will catch fire for sure and that there will be at least one tree in the forest Input Format: First line contains two integers, M, N, space separated, giving the siz...

Minimum cyclic (forward or backward) shifts to be performed to the string to make it a palindrome | CodeVita | Python Solution

Problem Statement: A string is said to be palindrome, if it reads the same from both the ends. Given a string  S , you are allowed to perform cyclic shifts. More formally, you can pick any one character from any end (head or tail) and you can append that character at the other end. For example, if the string is  "abc" , then if we do a shift using the character at head position then the string becomes  "bca" . Similarly, if we do the shift using the character at the tail then the input string becomes  "cab" . Your task is to find out the minimum number of shifts needed to make the given string, a palindrome. In case, we can't convert the string to palindrome then print  -1 . Input Format: First line starts with  T  i.e. number of test cases, and then  T  lines will follow each containing a string  S . Output Format: Print the minimum number of cyclic shifts for each string if it can be made a palindrome, else  -1 . Constraints:...

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

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