sudhir sharma

sudhir sharma

975 Articles Published

Articles by sudhir sharma

Page 58 of 98

Print bitwise AND set of a number N in C++

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

In this problem, we have to print all the numbers from 0 to n which are Bitwise AND of a binary of n.Let’s take an example to understand the concept better.Input : N = 4. Output : 0 4 Explanation :    0 & 4 = 0    1 & 4 = 0    2 & 4 = 0    3 & 4 = 0    4 & 4 = 4. Input : N = 6 Output : 0, 2, 4, 6To solve this problem, we need to use bitwise operators. Using these we will find the required subsets. We ...

Read More

Print Binary Tree levels in sorted order in C++

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

In this problem, we are given a binary tree and we have to print all nodes at a level in sorted order of their values.Let’s take an example to understand the concept better, Input −Output −20 6 15 2 17 32 78To solve this problem, we need to print a sorted order of each level of the tree. For this, we need to create a queue and two priority queues. The NULL separator is used to separate two levels.ExampleProgram to illustrate the logic −#include #include #include using namespace std; struct Node {    int data;    struct Node ...

Read More

Print all Prime Quadruplet of a number less than it in C++

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

In this problem, we are given a positive integer N, and we have to print all prime quadruplet less than or equal to n.Prime quadruplets are the set of four prime numbers calculated as {p, p+2, p+6, p+8}. Example − 5 7 11 13.Let’s take an example to understand the problem −Input: N = 15 Output: 5 7 11 13.To solve this problem, a simple approach is to generate all quadruplets of prime number p and check if all p, p+2, p+6, p+8 are prime numbers. This solution is easy but is more complex for the compiler.Another efficient approach is ...

Read More

Print all prime numbers less than or equal to N in C++

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

In this problem, we are given a number N and we have to print all prime numbers less than or equal to N.Let’s take an example to understand the topic better −Input: 10 Output: 2 3 5 7A prime number is a number that can be divided by only one and the number itself. Example: 2, 3.A simple approach is to iterate from 2 to N and divide the number by it. If the number is not divisible, then it’s a prime number. Print the number. Do this till the number is equal to N. This approach is not that ...

Read More

Print Binary Tree in 2-Dimensions in C++

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

In this problem, we are given a binary tree and we have to print it two dimensional plane.Binary Tree is a special tree whose every node has at max two child nodes. So, every node is either a leaf node or has one or two child nodes.Example, Let’s take an example to understand the topic better −Output -      7    4 5       1    3       8Now as we have seen in the example, the nodes of the tree are printed in a 2-D output screen horizontally.Here, we have flipped the tree by ...

Read More

Print all prime factors and their powers in C++

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

In this problem, we are given a number N, and we have to find all unique prime factors and their powers that divide the number.Let’s take an example to understand the topic −Input: 55 Output: 5 power 1 11 power 1Explanation −55 is divisible by 5 and 11.To solve this problem, an easy approach to solving the problem is to find prime factors of N. And then find power of the prime number that divides the number N and print it.AlgorithmEfficient ApproachStep 1: Find an array s[N+1]. s[i] = prime factor of i dividing N. Step 2: Find all powers ...

Read More

Print array of strings in sorted order without copying one string into another in C++

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

In the problem to print an array of strings in sorted order without copying one string into another, we need to sort the array of string. Here the programmer cannot copy a string into another string while sorting.Let’s take an example to understand the concept better :Example −Input : {“Delhi”, “Hyderabad”, “Indore”, “Mumbai”, “Banglore”} Output : Banglore, Delhi, Hyderabad, Indore, MumbaiExplanation − Lexicographically the ordering is done. So, Bangalore starting with B comes first and Mumbai starting with M comes last.Now, let's try to derive a solution to our problem.To solve the problem, we can create an array that stores the ...

Read More

Print array elements that are divisible by at-least one other in C++

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

In this problem, we are given an array of integers and we have to print only those numbers that are divisible by at least one other element of the array.Let’s take an example to understand the concept better, Input : 3 12 16 21 Output : 12 21Explanation − 3 is the smallest so it can be divisible by any other number are 12 which is divisible by 3, 16 not divisible by 3 and then 21 which is divisible by 3. So, we will neglect 3 and 16.One easy way is to check if all elements are divisible ...

Read More

Print all possible words from phone digits in C++

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

In this problem, we are given a number and we have to print all words that can be formed by pressing those words in an old fashioned mobile keyboard.We are quite familiar with the QWERTY keyboard style that we use today. But before the invention of QWERTY keypad phones were fitted with keypads with 12 buttons and each button contains words and numbers both. Like they word 6 on the keypad will contain words “MNO” which will be typed by clicking one, twice or thrice the keys.The keypad looked like this −12ABC3DEF4GHI5JKL6MNO7PQRS8TUV9WXYZ*0#In these keywords also all words are present and ...

Read More

Print array elements in alternatively increasing and decreasing order in C++

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

In this problem, we are given an array of number and we have to print the elements of the array in alternatively increasing and decreasing order while printing. The alternative order will be in such a way that 1st two elements are in increasing order and then the next three elements are in decreasing order again next four are in ascending order.Let’s take an example to understand the problem better, Input : {1, 4, 0, 2, 7, 9, 3} Output : 0 1 9 7 4 2 3Explanation − the array in increasing order of elements is 0 1 ...

Read More
Showing 571–580 of 975 articles
« Prev 1 56 57 58 59 60 98 Next »
Advertisements