Ways to Use Where-Object in PowerShell

Chirag Nagrekar
Updated on 25-Jan-2021 07:11:07

2K+ Views

Where−Object or (alias: Where) in PowerShell is used to filter the data output provided through the Pipeline.There are two methods we can use the Where−Object for the pipeline inputs.a. ScriptMethod −In this method, we use ScriptBlock to filter the output with the Property name, value, and the comparison operator.Get−Service | Where−Object{($_.StartType −eq 'Automatic') −and ($_.Status −eq 'Stopped')}You can also use Alias: Where instead of Where−Object.Get−Service | Where{($_.StartType −eq 'Automatic') −and ($_.Status −eq 'Stopped')} Other Syntax ‘?’ (Question Mark) can also be used Instead of the Where−Object command.Get−Service | ?{($_.StartType −eq 'Automatic') −and ($_.Status −eq 'Stopped')}The above commands will get the ... Read More

Convert One String into Another String in C++

sudhir sharma
Updated on 25-Jan-2021 05:29:05

274 Views

In this problem, we are given two strings str1 and str2. Our task is to create a program to Print all possible ways to convert one string into another string. Problem Description: Here, we need to find all possible ways using which we can convert str1 to str2. While converting, we can perform any of the three operations, Insert RemoveReplaceLet’s take an example to understand the problem,  Input: str1 = “kfeod” str2 = “kfcadq”OutputWay1:Insert q, after d. Replace c by e. Replace o by a.Solution ApproachWe will find the minimum number of edits first and then create a DP matrix. Then check if the character ... Read More

Print BST Keys in Given Range in O(1) Space using C++

sudhir sharma
Updated on 25-Jan-2021 05:28:31

139 Views

In this problem, we are given two values k1 and k2 (k1 < k2), and the root of the binary search tree. Our task is to create a program to Print BST keys in given Range. Problem Description: We will print all the keys of the tree from n1 to n2 in increasing order.Let’s take an example to understand the problem,  Input: k1 = 4, k2 = 12Output: 6, 7, 9Solution ApproachSimple we can solve the problem using inorder traversal but the space complexity there is 0(n) but the need of the hour is to solve in O(1) complexity. So, for this, we will use a ... Read More

Print Cells with Same Rectangular Sums in a Matrix in C++

sudhir sharma
Updated on 25-Jan-2021 05:28:12

144 Views

In this problem, we are given a matrix mat of size mXn of integer values. Our task is to create a program to Print cells with same rectangular sums in a matrix.Problem description: We will be finding a cell in the matrix in such a way that the sum of sub-matrices that are starting and ending with the cell is equal to the sum of all the remaining elements. For a cell the sum of matrix (a, b) sub-matrix mat[0][0] to mat[a][b] and mat[a][b] to mat[m][n] is equal to the sum of all remaining elements.Let’s take an example to understand the problem, ... Read More

Find Maximum Average Subarray of K Length in C++

sudhir sharma
Updated on 25-Jan-2021 05:25:01

181 Views

In this problem, we are given an array arr[] of size n consisting of positive and negative values and an integer k. Our task is to find the maximum average subarray of k length. Let’s take an example to understand the problem,  Input: arr[] = {4, -1, 5, 6, -2, 4} k = 3Output: 10Explanation: The subarray of size 3 with max sum is -1, 5, 6 = 10Solution ApproachA solution to the problem is done by using an auxiliary array to store cumulative sum till the current index in the array.To find the sum of subarrays, we need to compute the difference between the indices ... Read More

Find Maximum in an Array Without Using Relational Operators in C++

sudhir sharma
Updated on 25-Jan-2021 05:24:40

283 Views

In this problem, we are given an array arr[] of size n consisting of positive values. Our task is to find maximum in an array without using Relational Operators. Let’s take an example to understand the problem, Input: arr[] = {5, 1, 6, 7 , 8, 2}Output: 8Solution ApproachSince we need to compare values without using logical operators. For this we need to perform repeated subtraction, the number which will last longer will be the larger one.We will decrement all values by one till they become zero. We will start with the first two values of the array and find the greatest ... Read More

Find Maximum among x^2 and y^2 in C++

sudhir sharma
Updated on 25-Jan-2021 05:22:01

137 Views

In this problem, we are given two values x and y. Our task is to find maximum among x^(y^2) or y^(x^2) where x and y are given. Let’s take an example to understand the problem,  Input: x = 4, y = 3Output: 3^(4^2)Explanation: x^(y^2) = 4^(3^2) = 4^9 = 262144y^(x^2) = 3^(4^2) = 3^16 = 43046721Solution approachOne approach can be to calculate both values and then print the maximum of both. But this method does not work when the values are large.A simple and easy approach is using natural log (ln) which will be the solution easier.ln(x^(y^2)) = (y^2) * ln(x)ln(y^(x^2)) = (x^2) * ... Read More

Find Maximum Among All Right Nodes in Binary Tree in C++

sudhir sharma
Updated on 25-Jan-2021 05:21:35

111 Views

In this problem, we are given a Binary Tree. Our task is to find maximum among all right nodes in Binary Tree. Problem Description: Here, we need to find the maximum value amongst all right child nodes of the binary Tree.Let’s take an example to understand the problem,  Input: Output: 9Explanation: All right nodes are: {2, 8, 9}. Maximum of them is 9.Solution ApproachTo solve the problem, we need to traverse the tree and check if its right child exists. If it exists, compare with maxRight element and replace if it is greater.Program to illustrate the working of our solution, ExampleLive Demo#include using namespace ... Read More

Find Maximum or Minimum Sum of a Subarray of Size K in C++

sudhir sharma
Updated on 25-Jan-2021 05:21:14

432 Views

In this problem, we are given an  array arr[] and a number k. Our task is to Find the maximum (or minimum) sum of a subarray of size k. Let’s take an example to understand the problem,  Input: arr[] = {55, 43, 12, 76, 89, 25, 99} , k = 2Output: 165Explanation:The subarray of size 2 has sum = 76 + 89 = 165Solution ApproachA simple approach to solve the problem is by finding all k sized subarrays and then return the sum with maximum value.Another Approach is using the sliding window,  we will find the sum of k sized subarrayes. For this, the ... Read More

Find Maximum or Minimum in Binary Tree in C++

sudhir sharma
Updated on 25-Jan-2021 05:20:42

1K+ Views

In this problem, we are given a binary tree. Our task is to Find maximum (or minimum) in Binary Tree. Problem Description: We need to find the nodes of the binary tree that have maximum and minimum value in the binary tree.Let’s take an example to understand the problem,  Input: Output: max = 9 , min = 1Solution ApproachWe need to find the max node of the binary tree. We will do this by traversing the pointer until we reach the leaf node and then find the maximum node of the tree.Program to illustrate the working of our solution, ExampleLive Demo#include using namespace ... Read More

Advertisements