Found 7347 Articles for C++

Print all the combinations of a string in lexicographical order in C++

sudhir sharma
Updated on 17-Jan-2020 10:15:46

367 Views

In this problem, we are given string str, and we have to print all the combinations of the characters in a lexicographical order.Let’s take an example to understand the problem better −Input: str = ‘XYZ’ Output : X XY XYZ XZ XZY Y YX YXZ YZ YZX Z ZX ZXY ZY ZYXTo solve this problem, we will print all the combinations of characters in the string. For this, we need a map data structure to store the characters of the string. For the implementation, we will need to use backtracking to keep track of all combinations.Example Live Demo#include using namespace ... Read More

Print all the combinations of N elements by changing sign such that their sum is divisible by M in C++

sudhir sharma
Updated on 17-Jan-2020 10:14:04

139 Views

In this problem, we are given an array of N elements. And need to return all the sums of the elements are divisible by an integer M.Input : array = {4, 7, 3} ; M = 3 Output : 5+4+3 ; 5+4-3To solve this problem, we need to know the concept of a power set that can be used to find all the possible sums obtained. From this sum, print all those which are divisible by M.AlgorithmStep 1: Iterate overall combinations of ‘+’ and ‘-’ using power set. Step 2: If the sum combination is divisible by M, print them ... Read More

Print all the cycles in an undirected graph in C++

sudhir sharma
Updated on 17-Jan-2020 10:11:49

2K+ Views

In this problem, we are given an undirected graph and we have to print all the cycles that are formed in the graph.Undirected Graph is a graph that is connected together. All the edges of the unidirectional graph are bidirectional. It is also known as an undirected network.Cycle in a graph data structure is a graph in which all vertices form a cycle.Let’s see an example to understand the problem better −Graph-Output-Cycle 1: 2 3 4 5 Cycle 2: 6 7 8For this, we will make use of a few properties of the graph. You need to use graph coloring ... Read More

Print all the duplicates in the input string in C++

sudhir sharma
Updated on 17-Jan-2020 10:05:08

339 Views

In this problem, we are given a string and we have to find all the characters that are duplicated along with their number of occurrences in the string.Let’s take an example to understand the problem −Input: TutorialsPoint Output: t (3) o (2) i (2)Explanation− The frequencies of occurrence of each character are t → 3; u → 1; o → 2; r → 1; i → 2; a → 1; s → 1; n → 1.Now, to solve this problem we will find the character count and store it in an array from the string. And then print the characters ... Read More

Print all the levels with odd and even number of nodes in it in C++

sudhir sharma
Updated on 17-Jan-2020 10:09:56

130 Views

In this problem, we are given a tree. And we have to print all the levels with even number of nodes and odd number of nodes in it.Let’s take an example to understand the concept betterOutput −Levels with odd number of nodes: 1, 3, 4 Levels with even number of nodes: 2Explanation − The first level has only 1 element(odd), 2nd level contains two elements(even), 3rd level contains 3 elements(odd) and 4th level contains 1 element(even).Now, to solve this problem. We need to find the count of nodes at each level and print the even-odd levels accordingly.We will follow the ... Read More

Print all the pairs that contains the positive and negative values of an element in C++

sudhir sharma
Updated on 17-Jan-2020 09:58:19

123 Views

In this problem, we are given an array of unique integers. And we have to return all pairs of integers(positive and negative integers) that are present in the array.Let’s take an example to understand the problem better −Input: array = {1 , 4 , 7 , -1, 2, 5, -7} Output: -11 -33An easy way to solve the problem is by using two loops and find the positive-negative pairs. But this solution will be a complex one and will have time complexity of the order n2 where n is the size of the array.But, we have to find a more ... Read More

Print all the palindromic permutations of given string in alphabetic order in C++

sudhir sharma
Updated on 17-Jan-2020 09:57:09

198 Views

In this problem, we are given a string of size n. And we have to print all possible palindromic permutation that can be generated using the characters of the string in alphabetical order. If palindrome is not created using the string print ‘-1’.Let’s take an example to understand the topic better −Input: string = “abcba” Output : abcba bacbaNow, to solve this we need to find all the palindromes possible and then arrange them in alphabetical order(lexicographical order). Or another way could be finding the lexicographically first palindrome that is made from the string. Then find the sequentially next palindrome ... Read More

Print all the paths from root, with a specified sum in Binary tree in C++

sudhir sharma
Updated on 17-Jan-2020 09:53:32

140 Views

In this problem, we are given a Binary tree and a sum S. And we have to find the path starting from root to any node of the tree, which gives the sum equal to the given sum.InputSum = 14 Output : path : 4 10 4 3 7To find the solution to this problem, we need to find the preorder traversal of the binary tree. And then find the path that adds up to the given sum.Example Live Demo#include using namespace std; struct Node{    int key;    struct Node *left, *right; }; Node* insertNode(int key){    Node* temp = ... Read More

Print all the sum pairs which occur maximum number of times in C++

sudhir sharma
Updated on 17-Jan-2020 09:42:13

97 Views

In this problem, we are given an array of n unique integers. And we have to find the sum of two integers of the array which has the maximum frequency. The problem has multiple solutions and you need to find them all.Input : array = { 1, 12, 5, 7, 9, 11} Output : 16 12Explanation − sum 16 and 12 occur two times.5 + 11 = 16 & 7 + 9 = 16 1 + 11 = 12 & 5 + 7 = 12Now to solve this problem, our approach to the problem is checking the occurrence every sum ... Read More

Print all triplets in sorted array that form AP in C++

sudhir sharma
Updated on 17-Jan-2020 09:39:52

156 Views

In this problem, we are given a sorted array of numbers and we need to find the triplets with are in the form of arithmetic progression.An arithmetic progression is a series of numbers in which the difference between consecutive terms is the same.Let’s take an example to understand the problem better −Input : array = {2 , 5 , 7, 8 , 9 , 10} Output : 2 5 8 5 7 9 7 8 9 8 9 10To solve this problem, a simple solution would be running three loops and checking all triplets if they are in AP. but ... Read More

Advertisements