Construct the Largest number from the given digits

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

Numbers with prime set bits in a given range using Sieve of Eratosthenes Algorithm

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

Hamming Distance between two given integers

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

Find all the Armstrong numbers in the given range

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

Collatz Conjecture – Maximum Steps takes to transform (1, N) to 1.

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.

Find all unique combinations of exact K numbers (from 1 to 9 ) with sum to N

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

Check if the given number is Armstrong number or not

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

Evaluation of Prefix Expressions (Polish Notation) | Set 1

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

Evaluation of Postfix Expressions (Polish Postfix notation) | Set 2

Earlier we had discussed how to evaluate postfix expressions where operands are of single-digit. In this article, we will discuss how to evaluate postfix expressions for any number ( not necessarily single digit.) Postfix notation is a notation for writing arithmetic expressions in which the operands appear before their operators. Let’s assume the below Operands … Read more Evaluation of Postfix Expressions (Polish Postfix notation) | Set 2

Evaluation of Postfix Expressions (Polish Postfix notation) | Set 1

Postfix notation is a notation for writing arithmetic expressions in which the operands appear before their operators. There are no precedence rules to learn, and parentheses are never needed. Because of this simplicity. Let’s assume the below Operands are real numbers in single digits. (Read: Evaluation of Postfix Expressions for any Number ) Permitted operators: … Read more Evaluation of Postfix Expressions (Polish Postfix notation) | Set 1

Find all the numbers in the range which has prime set bits.

Objective: Given a range, find all the numbers in the range which has prime set bits. Example: L = 4, R = 10 Output: 5 Explanation: 4 = 1 0 0 ( not prime) 5 = 1 0 1 (prime) 6 = 1 1 0 (prime) 7 = 1 1 1 (prime) 8 = 1 … Read more Find all the numbers in the range which has prime set bits.

Smallest Number after Removing K digits

Objective: Given a number with N digits, write a program to get the smallest number possible after removing k digits from number N. OR Implement a method that returns the lowest possible number that could be generated after removing n characters from a string of digits. Example: N = 1453287, k = 3 Output: 1287 … Read more Smallest Number after Removing K digits

Find the maximum number present in a String

Given a string with alphabets and numbers. Write an algorithm to find the maximum number in the string. If any number is greater than equal to Max_Value of integer then return the Max_Value. Example: Input: “abcd 45, 54 and 50 are the numbers” Output: Maximum Number- 54 Input: aabb123ccdefgh54319hij Output: 54319 Input: 213123123and544455523412423423424234234234 Output: 2147483647 … Read more Find the maximum number present in a String

Convert Prefix to Postfix Expression

Objective: Given a Prefix expression, write an algorithm to convert it into Postfix expression. Example: Input: Prefix expression:  + A B  Output: Postfix expression: A B + Input: Prefix expression:  *-A/BC-/AKL Output: Postfix expression: ABC/-AK/L-* Approach: Use Stacks Algorithm: Iterate the given expression from right to left, one character at a time If the character … Read more Convert Prefix to Postfix Expression