Print Common Nodes on Path from Root or Common Ancestors in C++

sudhir sharma
Updated on 03-Jan-2020 07:56:25

207 Views

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 More

Print Concatenation of Zig Zag String in n Rows in C++

sudhir sharma
Updated on 03-Jan-2020 07:53:34

435 Views

In this problem, we are given a string that is a sequence of characters. And we are given the length of the zig-zag pattern and we have to print the concatenation string of this zig-zag string in n rows.Let’s see a few examples to understand the concept better, EXAMPLEInput : string = ‘STUVWXYZ’ n = 2. Output : SUWYTVXZExplanation − the zig-zag pattern for the string for a 2-row pattern is −S    U    W    Y    T    V    X    ZThe concatenation of this zig-zag pattern is - SUWYTVXZ.ExampleInput : string = ABCDEFGH n = ... Read More

Print Concentric Rectangular Pattern in a 2D Matrix in C++

sudhir sharma
Updated on 03-Jan-2020 07:50:37

980 Views

In this problem, we have to print a rectangular pattern in a 2D matrix in such a way that they are concentric to each other.Let’s take an example to understand this problem better, For n=4 is :    4 4 4 4 4 4 4    4 3 3 3 3 3 4    4 3 2 2 2 3 4    4 3 2 1 2 3 4    4 3 2 2 2 3 4    4 3 3 3 3 3 4    4 4 4 4 4 4 4Here, we have to print the pattern as ... Read More

Print Consecutive Characters Together in C++

sudhir sharma
Updated on 03-Jan-2020 07:47:02

342 Views

In this problem, we are given a string of characters and we have to print the same string in such a way that if two or more characters are consecutive then print them together in a single line otherwise print them in different lines i.e. with a line break.Let’s take an example to understand the concept better, Input : abcxstk Output : abc x st kExplanation − Since abc are in a sequence they are printed in a line. Then comes x which is not in sequence, so we add a linebreak here. Next is s which is not a ... Read More

Print Cousins of a Given Node in Binary Tree in C++

sudhir sharma
Updated on 03-Jan-2020 07:42:50

286 Views

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 More

Print Digit's Position to Make a Number Divisible by 6 in C++

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

301 Views

In this problem, we are given number and we have to remove more digit from the number. So that the new number formed after removal is divisible by 6.Let’s take an example to learn the concept better −Input : 1324 Output : 4Explanation − on removing the 4th number we will get 132 which is divisible by 6.Here, we are given a number and we have to return the position from where the number is removed to make it divisible by 6.For solving this problem, we will try to create a logic that solves the problem. For this, we will ... Read More

Print Direction of Moves Within Boundary in C++

sudhir sharma
Updated on 03-Jan-2020 07:27:50

236 Views

In this problem, we have to find a valid way to move positive direction or negative direction in such a way that we stay within a certain limit that is provided by the user.Here, We are given a certain maximum limit K, which is the maximum value to which we can move and an array of n positive values to move. We have to return the sequence i.e. positive or negative directions to move so that it never crosses K value.Let’s take an example to understand the topic better, Input : K = 56 and the array is [25 , ... Read More

Print Distinct Sorted Permutations with Duplicates in C++

sudhir sharma
Updated on 03-Jan-2020 07:24:47

816 Views

In this programming problem, we are given a string and we have to print the distinct sorted permutations of the string elements that can be formed. The condition with this problem is that the string may contain a character that will arise more than one time. Also, the given string is inputted in sorted order.Let’s take an example to understand the concept better, Input : ABD Output : ABD , ADB , BAD , BDA , DAB , DBA INPUT : RSTU OUTPUT : RSTU , RSUT , RTSU , RTUS , RUST , RUTS , SRTU , SRUT , ... Read More

Avoid Using eval in Bash: Alternatives and Best Practices

Pradeep Elance
Updated on 03-Jan-2020 07:23:05

2K+ Views

eval is a builtin command of the Bash shell which concatenates its arguments into a single string. Then it joins the arguments with spaces, then executes that string as a bash command. Below is an example of how it works.eval exampleIn the below example we take a string which has some Unix commands built into it and then apply eval to it.$ var="echo n" $ echo $var $ eval $varRunning the above code gives us the following result −echo n nAs you can see, when eval is applied the variable expands it gets executed as a command and no longer ... Read More

Looping Through the Content of a File in Bash

Pradeep Elance
Updated on 03-Jan-2020 07:15:52

7K+ Views

Often it is a requirement to read each of the line from a file using a bash script. There are various approaches to read the lines form a file. In the below example we have first described how to create a sample file and then run a script reading that sample file.Create a file to Read# Open vi Editor vi a_file.txt # Input the below lines Monday Tuesday Wednesday Thursday Friday Saturday Sunday # cat the file cat a_file.txtRunning the above code gives us the following result −Monday Tuesday Wednesday Thursday Friday Saturday SundayUsing Do-WhileIn this approach we use a ... Read More

Advertisements