sudhir sharma

sudhir sharma

975 Articles Published

Articles by sudhir sharma

Page 26 of 98

Find perimeter of shapes formed with 1s in binary matrix in C++

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

In this problem, we are given a binary matrix bin[][] of size nXm consisting of 0’s and 1’s only. Our task is to Find perimeter of shapes formed with 1s in a binary matrix.The perimeter taken will cover the figure from all sides, i.e.For 1 single value, the perimeter is 4.Let’s take an example to understand the problem, Inputbin[][] = [1, 0] [1, 0]Output6ExplanationThe cells (0, 0) and (1, 0) are connected making a rectangle of sides 2 and 1. The perimeter is 6.Solution ApproachA simple solution to the problem is simply finding all one and their ...

Read More

Find politeness of a number in C++

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

In this problem, we are given a positive integer N. Our task is to find the politeness of a number.Polite Number is a number which can be expressed as a sum of two or more consecutive numbers.Politeness of a number is defined as the number of ways the number can be expressed as sum of consecutive integers.Let’s take an example to understand the problem, Inputn = 5Output1Explanation2 + 3 = 5, is the only consecutive sum.Solution ApproachA simple solution to the problem is to check all consecutive numbers till N and if their sum is equal to N, increase count ...

Read More

Find position of an element in a sorted array of infinite numbers in C++

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

In this problem we are given an array consisting of infinite sorted numbers. Our task is to Find position of an element in a sorted array of infinite numbers.Let’s take an example to understand the problem, Inputarr[] = {2, 4, 6, 8, 9, 12, 14, 17, ….}, ele = 9Output4ExplanationSolution ApproachFor searching elements from a sorted array efficiently, we will be using the binary searching method. Here, single the end point is not known, we will modify the algorithm a bit.We will fix the start pointer to first position, then take the end pointer to second position. After this, we ...

Read More

Find position of the given number among the numbers made of 4 and 7 in C++

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

In this problem we are given a number N. Our task is to Find position of the given number among the numbers made of 4 and 7. The series consisting of 4 and 7 only is 4, 7, 44, 47, 74, 77, 444….Let’s take an example to understand the problem, InputN = 5Output74ExplanationSeries upto 5 terms is 4, 7, 44, 47, 74…Solution ApproachA simple solution to the problem is based on finding the pattern in the series.Here, every even position contains 7 in the end.And every odd position contains 4 in the end.So, we can find the series by going ...

Read More

Find position of the only set bit in C++

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

In this problem we are given a number N which has only one set bit in its binary representation. Our task is to find the position of the only set bit. If the number has only one set bit return the position of the number otherwise print invalid number.Let’s take an example to understand the problem, InputN = 32Output6ExplanationBinary representation of the number is 10000.Solution ApproachOne fact to know before we proceed further is the number will have only 1 set bit if it is a power of 2. Otherwise it will have more number of set bits.A simple solution ...

Read More

Find postorder traversal of BST from preorder traversal in C++

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

In this problem we are given an array preOrder[] that represents the preorder traversal of the binary search tree. Our task is to Find postorder traversal of BST from preorder traversal.Let’s take an example to understand the problem, InputpreOrder[] = {5, 2, 4, 7, 12}Output{4, 2, 12, 7, 5}Solution ApproachA simple solution to the problem is to create a BST from the given preorder traversal. And then do postorder traversal of the tree. This solution is Ok but a more effective solution is, We will traverse the preorder array with a limit on values to separate values of left and ...

Read More

Find power of power under mod of a prime in C++

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

In this problem we are given four values A, B, C, M(a prime number). Our task is to Find power of power under mod of a prime.We simply need to find the value of (A ^ (B ^ C)) (mod M).Let’s take an example to understand the problem, InputA = 3, B = 6, C = 2, M = 11Output3Explanation(A ^ (B ^ C)) = (3 ^ (6 ^ 2)) = (3 ^ (36))(mod 11) = 3Solution ApproachA simple solution to the problem is by directly calculating the values of the (A ^ (B ^ C)) , which is done ...

Read More

Find probability that a player wins when probabilities of hitting the target are given in C++

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

In this problem we are given four values p, q, r, s. Our task is to Find probability that a player wins when probabilities of hitting the target are given.Here, we have two players who are playing a game of archery. And the probability of player 1 hitting the target is defined as p/q. The probability of player 2 hitting the target is defined as r/s. We need to find the probability of player one winning the game.Let’s take an example to understand the problem, Inputp = 3, q = 5, r = 2, s = 5Output0.789Solution Approach*This approach requires ...

Read More

Find Pth term of a GP if Mth and Nth terms are given in C++

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

In this problem we are given five values m, n, mth term, nth term, p. Our task is to Find Pth term of a GP if Mth and Nth terms are given.For a GP, we are given the values of mth term and nth term. Using these values, we need to find the Pth term of the series.Let’s take an example to understand the problem, Inputm = 7, mthTerm = 1458, n = 10, nthterm = 39366, p = 3Output18Solution ApproachHere, we are given a GP. let's suppose the GP is, GP = a , a*r , a*(r2), a*(r3) ….The ...

Read More

Find right sibling of a binary tree with parent pointers in C++

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

In this problem we are given a binary tree and parent pointers. Our task is to Find right sibling of a binary tree with parent pointers.Let’s take an example to understand the problem, InputNode = 3Output7Solution ApproachA simple solution to the problem is finding the leaf node of the nearest ancestor (which is neither the current node nor the parest of the current node) which is at the same level as the current node. This is done by counting the levels while going up and then when coming down counting them down. And then finding the node.Program to illustrate the ...

Read More
Showing 251–260 of 975 articles
« Prev 1 24 25 26 27 28 98 Next »
Advertisements