Server Side Programming Articles - Page 1897 of 2646

Prefixes with more a than b in C++

sudhir sharma
Updated on 03-Feb-2020 12:08:14

214 Views

In this problem, we are given string str containing only a and b and an integer N such that a string is created by appending str n times. Our task is to print the total number of substring in which the count of a’s is more than the count of b.Let’s take an example to understand the problemInput: aab 2 Output: 9 Explanation: created string is aabaab. Substrings with count(a) > count(b) : ‘a’ , ‘aa’, ‘aab’, ‘aaba’, ‘aabaa’, ‘aabaab’, ‘aba’, ‘baa’, ‘abaa’.To solve this problem, we will have to check if the string contains the required prefix subsets. Here, ... Read More

Preorder from Inorder and Postorder traversals in C++

sudhir sharma
Updated on 03-Feb-2020 12:04:00

2K+ Views

In this problem, we are given the inorder and postorder traversal of a binary tree. Our task is to print the postorder traversal of the tree.Let’s take an example to understand the problemInput:inorder: 16 7 21 12 1 5 9 postorder: 16 21 7 1 9 5 12 Output: preorder: 12 7 16 21 5 1 9 Explanation: the binary tree is :To solve this problem, a simple solution could be creating a tree using the given traversals and then finding the preorder traversal of the tree. But this method will be more complex for the system.A more effective solution ... Read More

Preorder predecessor of a Node in Binary Tree in C++

sudhir sharma
Updated on 03-Feb-2020 12:00:04

494 Views

In this problem, we are given a binary tree and a node value. Our task is to print the preorder predecessor of the node.Binary Tree is a special type of tree in which each root node can have maximum 2 child nodes.Preorder Traversal is a way to traverse nodes of the tree. In this we will first traverse root node then left child and then the right child.Preorder predecessor node is the node that comes before the node in the preorder traversal of the node.Let’s take an example to understand the problemInput: 1 Output: 9To solve this problem, a navie approach will ... Read More

Preorder Successor of a Node in Binary Tree in C++

sudhir sharma
Updated on 03-Feb-2020 11:56:18

688 Views

In this problem, we are given a binary tree and a node value. Our task is to print the preorder successor of the node.Binary Tree is a special type of tree in which each root node can have maximum 2 child nodes.Preorder Traversal is a way to traverse nodes of the tree. In this we will first traverse root node then left child and then the right child.Preorder successor node is the node that comes next to the node in the preorder traversal of the node.Let’s take an example to understand the problemInput: 9 Output 0 Explanation: the preorder traversal ... Read More

Preorder Traversal of N-ary Tree Without Recursion in C++

sudhir sharma
Updated on 03-Feb-2020 11:52:50

275 Views

In this problem, we are given an N-ary Tree. Our task is to print the preorder traversal of the tree.First, let’s learn some basic terminologies, N-ary Tree is a tree in which all nodes can have at max N child nodes. Example 2-ary (binary) tree has max 2 child nodes.Preorder Traversal is a way to traverse nodes of the tree. In this we will first traverse root node then left child and then the right child.Let’s take an example to understand our problemPreorder traversal : 12151499411719To solve this problem, we have to use the stack data structure. We will first ... Read More

Previous greater element in C++

sudhir sharma
Updated on 03-Feb-2020 11:48:23

1K+ Views

In this problem, we are given an array. Our task is to return the greatest element that precedes the current element in the array otherwise print -1.Let’s take an example to understand the problemInput: {6, 2, 7, 1, 5, 3} Output: -1, 6, -1, 7, 7, 7To solve this problem, an easy and obvious solution will be using nested loops that will check greater element in the preceding part of the array.Program to show the implementation of our solutionExample Live Demo#include using namespace std; void preceddingGreatestElement(int arr[], int n){    cout = 0; j--) {          if (arr[i]

Previous number same as 1’s complement in C++

sudhir sharma
Updated on 03-Feb-2020 11:40:21

397 Views

In this problem, we are given an integer n. Our task is to check weather the preceding number is equal to 1’s complement of the number.Let’s take a few examples to understand our problemInput: 12 Output: No Explanation: (12)10 = (1100)2 Preceding number 11 = (1011)2 1’s complement of 12 = (0011)2 Input: 4 Output: Yes Explanation: 4 = (100)2 Preceding number 3 = (011)2 1’s complement of 12 = (011)2To solve this problem, we can use a simple approach which is by comparing the previous number and the 1’s complement of the number.This approach is simple but consumes space ... Read More

Previous smaller integer having one less number of set bits in C++

sudhir sharma
Updated on 03-Feb-2020 11:36:41

180 Views

In this problem, we are given an integer n. Our task is to print the largest number less than n which can be formed by changing one set bit of the binary representation of the number.Let’s take an example to understand the problemInput: n = 3 Output: 2 Explanation: (3)10 = (011)2 Flipping one set bit gives 001 and 010. 010 is greater i.e. 2.To solve this problem, we will have to flip the rightmost set bit and make it zero which will create the number the greatest possible number less than n that is found by flipping one bit ... Read More

Prim’s Algorithm (Simple Implementation for Adjacency Matrix Representation) in C++

sudhir sharma
Updated on 03-Feb-2020 11:34:13

4K+ Views

Prim’s Algorithm is a greedy method that is used to find minimum spanning tree for a given weighted undirected graph.Weighted graph is a graph that has all edges with weight values.Undirected graph is a special type of graph in which all edges are bidirectional.Minimum spanning tree is a subset that contains all edges and vertices but no cycle and has the least possible total edge weight.In this article, we will learn about prim’s algorithm to find minimum spanning tree. Usually, the algorithm uses two arrays but in this solution, we will use only one.Program to show the implementation of prim’s ... Read More

Primality test for the sum of digits at odd places of a number in C++

sudhir sharma
Updated on 03-Feb-2020 11:14:32

140 Views

In this problem, we are given a number N. our task is to check whether the sum of digits at odd place of the number gives a prime number or not.Primality Test is the algorithm that is used to check whether the given number is prime or not.Let’s take an example to understand the problem, Input: 3425 Output: No Explanation: sum digits at odd place = 5 + 4 = 9, which is not a prime number.To solve this problem an easy approach would be adding all digits that are at odd places in the number and then checking whether ... Read More

Advertisements