Server Side Programming Articles - Page 1958 of 2646

Find a palindromic string B such that given String A is a subsequence of B in C++

Arnab Chakraborty
Updated on 03-Jan-2020 09:44:13

222 Views

Suppose we have a string A, we have to find another string B, that will be palindrome. And the given string A will be subsequence of B. The subsequence of a string is a string that can be formed by it by deleting some characters without changing the order of remaining characters. Suppose the string is “cotst”, then generated string will be “contest”. For the input of this program we have chosen A = “ab”, the generated string will be “abba”, this is palindrome.To solve this, we will follow this approach. This is very simple, we will reverse the A, ... Read More

Print array of strings in sorted order without copying one string into another in C++

sudhir sharma
Updated on 03-Jan-2020 09:42:54

550 Views

In the problem to print an array of strings in sorted order without copying one string into another, we need to sort the array of string. Here the programmer cannot copy a string into another string while sorting.Let’s take an example to understand the concept better :Example −Input : {“Delhi”, “Hyderabad”, “Indore”, “Mumbai”, “Banglore”} Output : Banglore, Delhi, Hyderabad, Indore, MumbaiExplanation − Lexicographically the ordering is done. So, Bangalore starting with B comes first and Mumbai starting with M comes last.Now, let's try to derive a solution to our problem.To solve the problem, we can create an array that stores the ... Read More

Print Binary Tree in 2-Dimensions in C++

sudhir sharma
Updated on 03-Jan-2020 09:39:06

1K+ Views

In this problem, we are given a binary tree and we have to print it two dimensional plane.Binary Tree is a special tree whose every node has at max two child nodes. So, every node is either a leaf node or has one or two child nodes.Example, Let’s take an example to understand the topic better −Output -      7    4 5       1    3       8Now as we have seen in the example, the nodes of the tree are printed in a 2-D output screen horizontally.Here, we have flipped the tree by ... Read More

Find a common element in all rows of a given row-wise sorted matrix in C++

Arnab Chakraborty
Updated on 03-Jan-2020 09:40:19

341 Views

Suppose we have a matrix where each row is sorted. We have to write a function that will find the common elements in each row. Suppose the matrix is like below −The result will be 5.To solve this, we will use hash based approach. This approach can also be used when the rows are not sorted. We have to follow some steps to do this −We will create one hash table with all keys as distinct elements of two 1. All values will be 0loop through each element in the matrix, if the number is present in the hash table ... Read More

Print Binary Tree levels in sorted order in C++

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

229 Views

In this problem, we are given a binary tree and we have to print all nodes at a level in sorted order of their values.Let’s take an example to understand the concept better, Input −Output −20 6 15 2 17 32 78To solve this problem, we need to print a sorted order of each level of the tree. For this, we need to create a queue and two priority queues. The NULL separator is used to separate two levels.ExampleProgram to illustrate the logic − Live Demo#include #include #include using namespace std; struct Node {    int data;    struct ... Read More

Print bitwise AND set of a number N in C++

sudhir sharma
Updated on 03-Jan-2020 09:30:29

158 Views

In this problem, we have to print all the numbers from 0 to n which are Bitwise AND of a binary of n.Let’s take an example to understand the concept better.Input : N = 4. Output : 0 4 Explanation :    0 & 4 = 0    1 & 4 = 0    2 & 4 = 0    3 & 4 = 0    4 & 4 = 4. Input : N = 6 Output : 0, 2, 4, 6To solve this problem, we need to use bitwise operators. Using these we will find the required subsets. We ... Read More

Print BST keys in the given range in C++

sudhir sharma
Updated on 03-Jan-2020 09:28:26

700 Views

In this problem, we are given two nodes of a binary search tree. And we have to print all the values in the range of k1 to k2 that occur in the tree. That is we have to print all the values that are greater than k1 and smaller than k2. And we have to print all these keys in increasing order of their values.Binary Search Tree is a tree which follows these 3 properties −The left subtree has nodes with values lesser than the node’s value.The right subtree has a node with values greater than the node’s value.A subtree’s ... Read More

Print Bracket Number in C++

sudhir sharma
Updated on 03-Jan-2020 09:23:18

392 Views

In this problem, we are given expression. And we have to print the bracket number sequence. Let’s look at an example to understand the problem better.Example, Input : ((()())()) Output : 1233442551Explanation − Here we have encountered 5 bracket pairs and we have printed them in a sequence of their[ occurrence.Now since we know about the problem, let’s create a solution to this solution.The solution to this problem requires a stack data structure. We will use one variable that keeps the count of the number of left brackets and stack keeps track of right brackets. We will count left brackets ... Read More

Print calendar for a given year in C++

sudhir sharma
Updated on 03-Jan-2020 09:06:07

4K+ Views

In this problem, we are given a year and we want to print the calendar for that year.The year calendar shows all days, months on every date of the month. And here we will create a program that will return the calendar of the current year.For this, we will need some calculations like, Number of days in a specific monthJanuary, March, May, July, August, October, December has 31 days.February has 28 days in a nonleap year and 29 days in a leap year.April, June, September, November has 30 days.Start Day (weekday) on the monthBased on the year and month, the ... Read More

Print characters and their frequencies in order of occurrence in C++

sudhir sharma
Updated on 03-Jan-2020 08:10:11

573 Views

This problem, We are given a string of lowercase characters. and we have to find the frequencies of each character that occurs in the string. the below example when explaining more about the problem.Input : “jskdk” Output : j 1 s 1 k 2 d 1Explanation − In the String, the characters j, s, d occur once and k occurs twice. Hence, the output printed gives the above result.Now let's create a logic to solve this problem. As stated we have to find the frequency of occurrence of each character in the string. One logical way is to traverse the ... Read More

Advertisements