Found 7347 Articles for C++

Print all triplets with given sum in C++

sudhir sharma
Updated on 17-Jan-2020 09:48:03

193 Views

In this problem, we are given an array of unique integers and a sum. And we have to find the triplets which can form the same sum.Let's take an example to solve the problem −Input : array = {0 , 2 , -1 , 1, -2} Sum = 1 Output : 1 2 -2 0 2 -1To solve this problem, we will be finding all the triplets that provide the sum. A simple approach will be using three loops and finding the sum of the elements and return the adequate triplet.Example Live Demo#include using namespace std; void Triplets(int arr[], int ... Read More

Print all pairs with given sum in C++

sudhir sharma
Updated on 14-Jul-2020 07:33:46

341 Views

In this problem, we are given an array of integers and an integer sum and we have to print all pairs of integers whose sum is equal to the sum value.Let’s take an example to understand the problem :Input − array = {1, 6, -2, 3} sum = 4Output − (1, 3) , (6, -2)Here, we need pairs with the given sum value.A simple solution to the problem will be checking pairs of elements that generate the sum. This can be done by traversing array and find the number in array that sums up to sum value.ExampleThis program will illustrate ... Read More

Print all palindrome permutations of a string in C++

sudhir sharma
Updated on 14-Jul-2020 07:34:45

307 Views

In this problem, we are given a string and we have to print all the palindromic permutations that are possible from the characters of that string.Let’s take an example to understand the problem −Input − string = ‘aabb’Output − abba baabTo solve this problem we have to take the characters of the string and one by one generate all palindrome strings using these characters.Step 1 − Check if the string is a palindrome or not, print ‘Not Possible’ if not.Step 2 − If it can make palindrome, then make it into half and lexicographically select each letter of string.Step 3 ... Read More

Print all palindromic partitions of a string in C++

sudhir sharma
Updated on 14-Jul-2020 07:35:42

114 Views

In this problem, we are given a palindromic string. And we have to print all the partitions of this string. In this problem, we will find all possible palindrome partitions of the string by cutting it.Let’s take an example to understand the problem -Input − string = ‘ababa’Output − ababa , a bab a, a b a b a ….The solution, to this problem, is to check if a substring is a palindrome or not. And print the substring if it is substring.ExampleThe below program will illustrate the solution − Live Demo#include using namespace std; bool isPalindrome(string str, int low, int ... Read More

Print all palindromic paths from top left to bottom right in a matrix in C++

sudhir sharma
Updated on 14-Jul-2020 07:36:48

131 Views

In this problem, we are given a matix which contains aplhabets (lowercase only) and we have to print all palidromic paths in the given matrix from top left to bottom right of the matrix.Allowed moves in this problem are right and down. Diagonal moves are not allowed.Let’s take an example to understand the problem −Input: matrix[][] ={    {"xxxy",    "yxxx",    "xyyx"} Output: xxxxxx , xxxxxx , xyxxyxExplainationLets see all valid moves from top left to bottom right using the position wrt to ith position.i00 -> i01 -> i02 -> i03 -> i13 -> i23 = xxxyxx i00 -> ... Read More

Print all paths from a given source to a destination using BFS in C++

sudhir sharma
Updated on 14-Jul-2020 07:28:01

963 Views

In this problem we are given a directed graph and we have to print all paths from the source to the destination of the graph using Breadth first Search (BFS).Directed graph is a graph in with edges that are directed from vertex a to b.Let’s take an example to understand the problem -Source = K destination = POutputK -> T -> Y -> A -> P K -> T -> Y -> P K -> A -> PHere, we have found paths from K to P. We have traversed paths and printed all paths from K that direct us to P.To ... Read More

Print all paths from a given source to a destination in C++

sudhir sharma
Updated on 16-Jan-2020 12:44:35

272 Views

In this problem we are given a directed graph and we have to print all paths from the source to the destination of the graph.Directed graph is a graph in with edges that are directed from vertex a to b.Let’s take an example to understand the problem Source = K destination = POutput:K -> T -> Y -> A -> P K -> T -> Y -> P K -> A -> PHere, we have found paths from K to P. We have traversed paths and printed all paths from K that direct us to P.To solve this problem, we will ... Read More

Print all paths from top left to bottom right in a matrix with four moves allowed in C++

sudhir sharma
Updated on 16-Jan-2020 12:39:11

179 Views

In this problem, we are given an mXn 2D matrix and we have to print all possible paths from top left to bottom right of matrix. For traversal, we can move in all four directions i.e. left, right, top, bottom.Thought the moves right and top are rarely used but these can be beneficial sometimes.Let’s take an example to understand the topic better :Input:1 3 5 2 8 9Output:1 -> 3 -> 5 -> 9 1 -> 3 -> 8 -> 9 1 -> 2 -> 8 -> 9To solve this problem, we will move from one cell to other ... Read More

Print all permutations in sorted (lexicographic) order in C++

sudhir sharma
Updated on 16-Jan-2020 12:29:24

436 Views

In this problem, we are given a string of length n and we have to print all permutations of the characters of the string in sorted order.Let’s take an example to understand the problem :Input: ‘XYZ’Output: XYZ, XZY, YXZ, YZX, ZXY, ZYX.Here we have to print all permutations in lexicographical order (alphabetically increasing order).To solve this problem, we have to first sort the array in alphabetically increasing order, the sorted array is the first element of the permutation. And then generate the next higher order permutation of the string.The below code will make the solution more clear to you :Example Live ... Read More

Print all permutations with repetition of characters in C++

sudhir sharma
Updated on 14-Jul-2020 07:25:19

791 Views

In this problem, we are given a string of n characters and we have to print all permutations of characters of the string. Repeating of characters of the string is allowed. The printing of permutation should be done in alphabetical order (lexicographically sorted order).Let’s take an example to understand the topic better :Input − XYOutput − XX, XY, YX, YYTo solve this problem, we need to use fix and recur logic. Here, we will fix one element at first index of the array and then recursively call for the next elements in the sequence.Let’s see an implementation example which will ... Read More

Advertisements