Given a decimal number, convert that number to its binary representation. Example: Number: 5, binary representation: 101 Number: 8, binary representation: 1000 Number: 105, binary representation: 1101001 Solution: Initialize a result string and while the given number is greater than 0, keep dividing it by 2 and append the remainder of division to the result … Read more Decimal to Binary
Write a program to add the digits of the given number until the number becomes a single digit number. Example: Input: 256, Output: 4 256 -> 13 -> 4 Input: 15, Output: 6 15 -> 6 Input: 82, Output: 1 82 -> 10 -> 1 Input: 242239, Output: 4 242239 -> 22 -> 4 Approach: … Read more Add digits until number becomes a single digit
Write a program to add the digits of the given number until the number becomes a single digit number. Example: Input: 256, Output: 4 256 -> 13 -> 4 Input: 15, Output: 6 15 -> 6 Input: 82, Output: 1 82 -> 10 -> 1 Input: 242239, Output: 4 242239 -> 22 -> 4 Approach: … Read more Add digits until the number becomes a single digit
Given a list of tax brackets (different tax on different incomes). Write a program to calculate the tax on given income. Tax bracket is a pair consisting of amount and percentage tax on that amount. If amount is null means, it’s a highest bracket and percentage tax is mentioned will be applied to all the … Read more Calculate tax on income as per given tax brackets.
Given N orders, each order consists of pickup and delivery services, means delivery of a particular service will after the pick up of the same service so the sequence pickup/delivery such that delivery(i) is after pickup(i). Write a program to count all valid pickup/delivery possible sequences Since the answer may be too large, return it … Read more Valid Pickup and Delivery options
Given a number write an algorithm to construct the largest number possible by using the digits of given number. Given number could be a negative number as well. Example: Given Input: 34277765 Output: 77765432 Given Input: -342765 Output: -234567 Given Input: 0 Output: 0 Given Input: 2034 Output: 4320 Approach: Sorting Check if the given … Read more The largest number can be formed from the given number
Given a set of digits, write an algorithm to construct the largest number possible by appending the given digits. Example: Given Digits: [9, 1, 9, 2, 8, 4, 2] largest number: 9984221 Given Digits: [1, 2, 5, 6, 7] largest number: 76521 Approach: Sorting Sort the given array in descending order. Initialize result = 0. … Read more Construct the Largest number from the given digits
Objective: Given a range, find all the numbers in the range which has prime set bits using Sieve of Eratosthenes Algorithm. Earlier we had seen a similar problem -Numbers with prime set bits in a given range solved in the naive approach. in this algorithm, we will improve the time complexity using the Sieve of … Read more Numbers with prime set bits in a given range using Sieve of Eratosthenes Algorithm
Objective: Given two integers, write an algorithm to calculate the hamming distance between the integers. Hamming Distance: Hamming distance between two integers is the number of positions at which the bits are different. Example: X = 2, Y = 4 Hamming distance: 2 2 = 0 1 0 4 = 1 0 0 There are … Read more Hamming Distance between two given integers
Objective: Given a range of integers, wrote a program to find all the Armstrong numbers in that range. What is Armstrong number: Determine the number of digits in the number. Call that n. Then take every digit in the number and raise it to the n power. Add all those together, and if your answer … Read more Find all the Armstrong numbers in the given range
Objective: Given a number N, write an algorithm to find the maximum number of steps it takes to transform (1, N) to 1 using Collatz Conjecture. The Collatz conjecture is a conjecture in mathematics which states that no matter what value of Positive Number N, If the below sequence is followed then the sequence will … Read more Collatz Conjecture – Maximum Steps takes to transform (1, N) to 1.
Objective: Given two integers N and K. Write an algorithm to find all the combinations of k numbers which sum to N. Conditions: All K numbers must be between 1 and 9 and unique. All numbers in K are positive. Example: N= 5 K=2 Output: [1, 4] [2, 3] N =12 K=3 Output: [1, 2, … Read more Find all unique combinations of exact K numbers (from 1 to 9 ) with sum to N
Objective: Given a number, write a program to check if the number is divisible by all of its digits separately. Example: N = 15 Output: true (15 is divisible by 1 and 5) N = 123 Output: true (123 is divisible by 1, 2 and 3) N = 143 Output: false (143 is not divisible … Read more Check if Number is divisible by its digits
Objective: Given a number, write a program to find out whether the number is Armstrong number or not What is Armstrong number: Determine the number of digits in the number. Call that n. Then take every digit in the number and raise it to the n power. Add all those together, and if your answer … Read more Check if the given number is Armstrong number or not
Prefix notation is a notation for writing arithmetic expressions in which the operands appear after their operators. There are no precedence rules to learn. Let’s assume the below Operands are real numbers in real digits. (Later we will Enhance the solution for any number) Permitted operators: +,-, *, /, ^(exponentiation) Blanks are NOT permitted in … Read more Evaluation of Prefix Expressions (Polish Notation) | Set 1