Skip to main content

Posts

Showing posts with the label codevita

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

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 the army. The Commander-In-Chief is a

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

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"

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

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:

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