Found 26504 Articles for Server Side Programming

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

207 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

211 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

297 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

192 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

171 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

233 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

277 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

447 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

391 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

206 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

Advertisements