Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by sudhir sharma
Page 94 of 98
Print all sequences starting with n and consecutive difference limited to k in C++
In this problem, we are given three variables n, s, and k and we have to print all the possible sequences that start with the number n and length s having the absolute difference between consecutive elements less than k. Let's take an example to understand the topic better − Input: n = 3, s = 3 , k = 2 Output: 3 3 3 3 3 4 3 3 2 3 4 4 3 4 5 3 4 3 3 2 2 3 2 3 3 2 1 In this problem, we need to obtain the ...
Read MorePrint all subsequences of a string using ArrayList in C++
In this problem, we are given a string and we have to print all subsequences of the string. The substring is formed by deleting elements. Also, the order of string should not be altered.Let’s take an example to understand the problem better −Input: string = “xyz” Output: x y xy z xz yz xyzTo solve this problem, we will find all substring starting from freezing the first character of the string and find subsequence accordingly, then going for the next character in string and subsequence.Examplepublic class Main { public static void printSubString(String sub, String subSeq){ if ...
Read MorePrint all subsequences of a string in C++
In this problem, we are given a string and we have to print all the subsequences of the string. The substring generated is created by deleting the elements of the string but the order remains the same(i.e. Order cannot be changed).Let’s take an example to understand the topic better −Input: xyz Output: x, y, z, xy, yz, xz, xyzExplanation − In the above example, we can see the only characters are deleted to create substring. No, rearranging takes place.There can be multiple methods to solve this problem, here we will discuss a few of them to understand methods.One is by ...
Read MorePrint all the cycles in an undirected graph in C++
In this problem, we are given an undirected graph and we have to print all the cycles that are formed in the graph. Undirected Graph is a graph that is connected together. All the edges of the unidirectional graph are bidirectional. It is also known as an undirected network. Cycle in a graph data structure is a graph in which all vertices form a cycle. Let's see an example to understand the problem better − Graph- Output- Cycle 1: 2 3 4 5 Cycle 2: 6 7 8 For this, we will make use of a few properties of ...
Read MorePrint all the sum pairs which occur maximum number of times in C++
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 MorePrint all ways to break a string in bracket form in C++
In this problem, we are given a string and we have to break it into substrings and print them enclosing brackets.Let’s take a few examples to understand the problem better, Input : wxyz Output : (w) (x) (y) (z) (w) (x) (yz) (w) (xy) (z) (w) (xyz) (wx) (y) (z) (wx) (yz) (wxy) (z) (wxyz)Explanation − We will break the string into all possible substrings. And enclose each substring with brackets.Now, since we have understood the problem, let’s create a solution to the problem.Here, we will use recursion to solve the problem. ...
Read MorePrint BST keys in the given range in C++
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 MorePrint common nodes on path from root (or common ancestors) in C++
In this problem, we are given a binary tree and two nodes of the binary tree are defined. And we have to print all the common ancestors of the node i.e. common nodes that occur in the path from traversal from root to the node.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, Ancestor node is a node that is connected to lower-level nodes in a tree.The common ancestor node of two nodes is a node that is an ...
Read MorePrint cousins of a given node in Binary Treein C++
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, In this problem, we are given a binary tree and we have a node of the tree and we have to find the cousin nodes of the node. No sibling nodes are to be printed for the binary tree.Let’s take an example, For the above binary tree, the cousin node is 5.To make the concept more clear let’s describe cousin node. In a binary tree, two nodes are said to ...
Read MoreBinomial Coefficient in C++
Binomial coefficient denoted as c(n, k) or ncr is defined as coefficient of xk in the binomial expansion of (1+X)n.The Binomial coefficient also gives the value of the number of ways in which k items are chosen from among n objects i.e. k-combinations of n-element set. The order of selection of items not considered.Here, we are given two parameters n and k and we have to return the value of binomial coefficient nck .ExampleInput : n = 8 and k = 3 Output : 56There can be multiple solutions to this problem, General SolutionThere is a method to calculate the value ...
Read More