In this problem, we are given two numbers A and B. Our task is to create a program to calculate the Fast average of two numbers without division. Let’s take an example to understand the problem, Input: A = 34 B = 54Output: 44Solution Approach: Normally, the average is calculated by adding two numbers and then divide it by 2. This requires division but we need to find the average without using division. This can be done using right shift operator >> and shift the binary expansion instead of using division operator.Program to illustrate the working of our solution, ExampleLive Demo#include ... Read More
Midy’s Theorem is a statement used for decimal expansion of numbers denoted by n/p, where n is any number and p is a prime number and a/p has a repeating decimal with even period.In Extended Midy’s Theorem, the repeating portion is divided into m digits, then their sum is a multiple of 10m - 1. Program to illustrate Extended Midy’s Theorem: ExampleLive Demo#include using namespace std; string findDecimalValue(int num, int den) { string res; unordered_map mp; int rem = num % den; while ((rem != 0) && (mp.find(rem) == mp.end())) { ... Read More
An expression tree is a special type of binary tree in which each node of the tree either consists of an operator or operand.Leaf nodes of the tree represent an operand.Non-leaf nodes of the tree represent an operator.Example:To get the infix expression which can be easily solved, we need to traverse the tree using inorder traversal.
In this problem, we are given an odd number N. Our task is to express an odd number as the sum of prime numbers.There can be at max three prime numbers while expressing the number.Let’s take an example to understand the problem, Input: N = 55Output: 53 + 2Solution Approach: The odd number can be represented as a sum of primes. Taking these primes into consideration we have three cases.Case 1: If n is a prime number, it is represented as the sum of one prime number n.Case 2: If (n - 2) is a prime number, it is represented as the sum of two ... Read More
In this problem, we are given an array a number N. Our task is to check whether the number is an evil Number or Odious Number. Evil Number: It is a positive number which has an even number of 1’s in its binary expansion.Example: 5, 17 Odious Number: It is a positive number which has an odd number of 1’s in its binary expansion.example : 4, 6Let’s take an example to understand the problem, Input: N = 65Output: Evil NumberExplanation: Binary Expansion of 65 : 1000001Solution Approach: A simple solution to the problem is by finding the binary expansion of the number and then counting the number of 1’s ... Read More
In this problem, we are given two arrays each denoting an investment plan. Our task is to perform the evaluation of Risk in Investment and find which of the two investments is more promising.Both the investments I1[][] and I2[][] has a set of outcomes and the probability of that investment outcome.Using these values, we need to find the risk in each investment and then print the better investment out of the two investments.For this, we will be using statistical mathematics and find some values that will help us conclude to the better investment.We will find these values, Mean or average amount ... Read More
There are some rules in programming that govern how an operation is performed.The order of evaluation of operation and the associativity of operations (which is left to right is defined).Here is a program to show the evaluation order of operands,ExampleLive Demo#include using namespace std; int x = 2; int changeVal() { x *= x; return x; } int main() { int p = changeVal() + changeVal(); cout
In this problem, we are given three integer values A , B and T. Our task is to create a program to play Even-odd turn game with two integers. The two integer value are :T, that denotes the number of turns in the game.A denotes the value for player1B denotes the value for player2If the value of T is odd, the value of A is multiplied by 2.If the value of T is even, the value of B is multiplied by 2.We need to find and return value of max(A, B) / min(A, B) at the end.Let’s take an example to understand ... Read More
In this problem, we are given an array arr[] of size n consisting of n/2 even values and n/2 odd values. Our task is to create a program to place even numbers at even index and odd numbers at odd index. Let’s take an example to understand the problem, Input: arr[] = {5, 1, 6, 4, 3, 8}Output: arr[] = {6, 1, 5, 4, 3, 8}Solution Approach −A solution would be traversing the array and then find the end number which is not at even position and replacing it with the next off place value. This is a promising solution but the solution ... Read More
In this problem, we are given an adjacency list that denotes an n-ary tree. Our task is to find the number of even size subtree in n-ary tree. N-ary tree is defined as a collection of nodes normally represented hierarchically in the following manner.The tree is started at the root node.Each node of the tree maintains a list of pointers to its child nodes.The number of child nodes is less than or equal to m.Let’s take an example to understand the problem, Input: Output: 4Explanation: Tree rooted with 7 has an even size.Tree rooted with 2 has an even size.Tree rooted with 0 has an ... Read More