Server Side Programming Articles - Page 1939 of 2650

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

196 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

177 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

238 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

Print all triplets with given sum in C++

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

288 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

457 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

401 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

217 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

224 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

Prime Arrangements in Python

Arnab Chakraborty
Updated on 29-Apr-2020 07:57:12

336 Views

We have to find the number of permutations of 1 to n, so the prime numbers are placed at prime indices. The answers may be large, return the answer modulo 10^9 + 7. So if n = 5, then output will be 12. So there will be 12 permutations. one possible permutation will be [1, 2, 5, 4, 3], one invalid permutation is [5, 2, 3, 4, 1] because 5 is placed at index 1, that is not prime.To solve this, we will follow these steps −Define one method called getNum, as follows −prime := list of all primes from ... 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

1K+ 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

Advertisements