sudhir sharma

sudhir sharma

975 Articles Published

Articles by sudhir sharma

Page 39 of 98

Even size subtree in n-ary tree in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 259 Views

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

Even-odd turn game with two integers in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 337 Views

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

Evil Number with Example in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 1K+ Views

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

Express an odd number as sum of prime numbers in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 480 Views

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

Extended Midy's theorem in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 195 Views

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: Example#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

Fast average of two numbers without division in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 504 Views

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, Example#include ...

Read More

Fast inverse square root in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 2K+ Views

In this problem, we are given an integers x. Our task is to calculate Fast inverse square root () of a 32-bit floating point number.  The algorithm to find the inverse square root of the number is of great use in programming, such as vector normalization in video games,  in 3D graphics, etc.  Algorithm: Step 1: The algorithm converts the floating point value to integer. Step 2: Operate on the integer value and return approximate value of the inverse square root.Step 3: Convert the integer value back to floating point using the same method used in step 1.Step 4: The approximation is made for improving precision using ...

Read More

Fermat's Last Theorem in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 285 Views

Fermat’s last theorem in number theory also known as Fermet’s conjecture is a theorem that states that for power n greater than 2. No three values a, b, c satisfy −          an + bn = cni.e.     if n 32 + 42 = 9 + 16 = 25 = 52. 5, 12, 13 => 25 + 49 = 169 = 132.In this problem, we are given three values, L, R, pow denoting range [L, R] and power. Our task is to verify the fermat’s last theorem for the given range and power.Let’s take an example to understand the problem, ...

Read More

Fermat's little theorem in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 808 Views

Fermat’s little theorem −This theorem states that for any prime number p,           Ap - p is a multiple of p.This statement in modular arithmetic is denoted as,                  ap  ≡ a (mod p)If a is not divisible by p then,  ap - 1 ≡ 1 (mod p)In this problem, we are given two numbers a and p. Our task is to verify fermat’s little theorem on these values.We need to check if ap  ≡ a (mod p) or ap - 1 ≡ 1 (mod p)Holds true for the given values of a and p.Let’s take ...

Read More

Fifth root of a number in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 1K+ Views

In this problem, we are given a number N. Our task is to find the floor value of the fifth root of a number.Fifth Root of a number is the number which when multiplied to itself 5 times returns the number.If N1/5 = a then, a*a*a*a*a = N.Let’s take an example to understand the problem, Input: N = 325Output: 3Explanation:  The fifth root of 325 is 3.179 whose floor value is 3.Solution Approach: A simple solution to the problem is traversing from 1 to n. And finding the number which when multiplied to itself five times gives the number.Here, the exact value cannot be found ...

Read More
Showing 381–390 of 975 articles
« Prev 1 37 38 39 40 41 98 Next »
Advertisements