Found 7347 Articles for C++

Print all possible strings of length k that can be formed from a set of n characters in C++

sudhir sharma
Updated on 17-Jan-2020 11:35:01

2K+ Views

In this problem, we are given a set of characters and a positive integer k and we have to print all possible strings of length k that can be generated using the characters of the set.Let’s take an example to understand the problem better −Input: set = {‘x’, ‘y’, ‘z’} , k = 2 Output: xy, xz, yzTo solve this problem, we have to find all possible sequences that can be generated. For the set of size n, the total number of a possible string of length k will be nk (n^k). We will use a recursive call to generate ... Read More

Print all possible strings that can be made by placing spaces in C++

sudhir sharma
Updated on 17-Jan-2020 11:31:08

316 Views

In this problem, we are given a string and we have to print all those string that can be made using this string by placing space in between the characters of the string.Let’s take an example to understand the topic better −Input: string = ‘XYZ’ Output: XYZ, XY Z, X YZ, X Y ZTo solve this problem, we will have to find all the possible ways in which we can put space in the string. For this we will use recursion. In this, we will place spaces on by one and generate a new string.Example Live Demo#include #include using ... Read More

Print all possible sums of consecutive numbers with sum N in C++

sudhir sharma
Updated on 17-Jan-2020 11:30:24

412 Views

In this problem, we are given a positive integer N and we have to print the sequence of all possible consecutive numbers with a sum equal to N.Let’s take an example to understand the problem,Input: N = 15 Output: 1 2 3 4 5 7 8A simple solution to this problem is by adding up consecutive sequence combinations till N/2. And then print the sequence that sums up to N.Example Live Demo#include using namespace std; void printConsequtiveSum(int N){    int start = 1, end = (N+1)/2;    while (start < end){       int sum = 0;       for (int i = start; i

Print all possible words from phone digits in C++

sudhir sharma
Updated on 17-Jan-2020 11:23:51

707 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 all prime factors and their powers in C++

sudhir sharma
Updated on 17-Jan-2020 11:16:29

523 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 all prime numbers less than or equal to N in C++

sudhir sharma
Updated on 17-Jan-2020 11:13:55

837 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 all Prime Quadruplet of a number less than it in C++

sudhir sharma
Updated on 17-Jan-2020 11:12:58

110 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 Proth primes up to N in C++

sudhir sharma
Updated on 17-Jan-2020 11:09:28

100 Views

In this problem, we are given an integer N and we have to print all proth prime numbers less than or equal to N.Proth Prime NumberA proth prime number is a positive integer whose value can be represented as n = k* 2n + 1. where k is an odd positive integer and n is a positive integer and both satisfy the 2n > k.Examples − 3, 5, 13…..Let’s take an example to understand the topic better −Input: N = 23 Output: 3, 5, 13, 17.For this, we will find all the prime numbers less than N(for this we will ... Read More

Print all root to leaf paths with there relative positions in C++

sudhir sharma
Updated on 17-Jan-2020 11:07:39

97 Views

In this problem, we are given a binary tree. And we have to print all the paths from the root to the leaf of the tree. Also, add underscore “_” to show the relative positions of the nodes.Let’s take an example to understand the topic better −Input −Output −_ _ 3 _ 9 1 _3 9 _7 3 _ 4 _ _ 2 3 9 4 1 7 6 2 3 _ 4 6To solve this problem, we will use the concept of the vertical order of the elements of the tree.Based on this, we will print the path from ... Read More

Print all safe primes below N in C++

sudhir sharma
Updated on 17-Jan-2020 11:04:32

83 Views

In this problem, we are given an integer N and we have to print all safe prime number whose values are less than N.A safe prime number is a prime number which can be represented as [(2*p)- 1] where p is also a prime number.Examples − 5[(2*2) +1] , 7[(2*3)+1].Let’s take a few examples to understand the problem better −Input: N = 12 Output: 5 7 11.To solve this problem, we will find all the prime numbers less than N(for this we will use Sieve of Eratosthenes). And check if the prime number is a safe prime number or not ... Read More

Advertisements