Found 26504 Articles for Server Side Programming

Number of Valid Words for Each Puzzle in C++

Arnab Chakraborty
Updated on 08-Jun-2020 10:58:15

218 Views

Suppose there is a puzzle string, a word is valid if both the following conditions valid −word contains the first letter of puzzle.For each letter in word, that letter is in puzzle.Suppose if we consider an example that, if the puzzle is like "abcdefg", then valid words are "face", "cabbage" etc; but some invalid words are "beefed" as there is no "a" and "based" as there is "s" which is not present in the puzzle.We have to find the list of answers, where answer[i] is the number of words in the given word list words that are valid with respect ... Read More

Recover a Tree From Preorder Traversal in C++

Arnab Chakraborty
Updated on 08-Jun-2020 10:55:14

509 Views

Suppose there is a binary tree. We will run a preorder depth first search on the root of a binary tree.At each node in this traversal, the output will be D number of dashes (Here D is the depth of this node), after that we display the value of this node. As we know if the depth of a node is D, the depth of its immediate child is D+1 and the depth of the root node is 0.Another thing we have to keep in mind that if a node has only one child, that child is guaranteed to be ... Read More

Unique Paths III in C++

Arnab Chakraborty
Updated on 08-Jun-2020 10:52:23

250 Views

Suppose we have one 2-dimensional grid, there are 4 types of squares −In a square 1 is for the starting point. There will be exactly one starting square.In a square 2 is for the ending point. There will be exactly one ending square.In a square 0 is for the empty squares and we can walk over.In a square -1 if for the obstacles that we cannot walk over.We have to find the number of 4-directional walks from the starting square to the ending square, that walk over every non-obstacle square exactly once.So, if the input is like −10000000102-1then the output ... Read More

Odd Even Jump in C++

Arnab Chakraborty
Updated on 08-Jun-2020 10:48:34

614 Views

Suppose we have an array A. From some starting index, we can make a series of jumps. The position (1, 3, 5, ...) jumps in the series are called odd numbered jumps, and position (2, 4, 6, ...) jumps in the series are called even numbered jumps.Now we may from index i jump forward to index j (with i < j) in this way −During odd numbered jumps, we can jump to the index j such that A[i] = A[j] and A[j] is the largest possible value. When there are multiple such indexes j, we can only jump to the ... Read More

Program to find the minimum (or maximum) element of an array in C++

Ayush Gupta
Updated on 15-Sep-2020 14:21:28

14K+ Views

In this problem, we are given an array arr[] of n integers. Our task is to create a program to find the minimum and maximum element of an array in C++.Problem Description − Here, we have an array arr[]. The contains n integer values. We have to find the maximum value and minimum value out of all values of the array.Let’s take an example to understand the problem, Inputarr[] = {2, 1, 6, 9, 4, 10, 15, 21}Outputmax = 21 , min = 1Solution ApproachThere can be multiple solutions to the problem, One solution would be directly comparing elements of ... Read More

Program to find the mid-point of a line in C++

Ayush Gupta
Updated on 15-Sep-2020 15:24:12

1K+ Views

In this problem, we are given two points A and B, starting and ending point of a line. Our task is to create a program to find the mid-point of a line in C++.Problem Description − Here, we have a line with starting and ending points A(x1, y1) and B(x2, y2). And we need to find the mid-point of the line.Let’s take an example to understand the problem, Inputa(x1, y1) = (4, -5) b(x2, y2) = (-2, 6)Output(1, 0.5)Explanation(x1 + x2)/2 = 4 - 2 / 2 = 1 (y1 + y2)/2 = -5 + 6 / 2 = 0.5Solution ... Read More

Binary Tree Cameras in C++

Arnab Chakraborty
Updated on 08-Jun-2020 10:46:26

259 Views

Suppose we have a binary tree; we place cameras on the nodes of the tree. Now each camera at a node can monitor its parent, itself, and its children. We have to find the minimum number of cameras needed to monitor all nodes of the tree.So, if the input is like −then the output will be 1, because only one camera is enough to track all.To solve this, we will follow these steps −Define one set called covered, of type TreeNode (Tree node has left, right and data field)Define a function solve(), this will take node, parent, if node is ... Read More

Program to find the maximum difference between the index of any two different numbers in C++

Ayush Gupta
Updated on 15-Sep-2020 15:12:47

381 Views

In this problem, we are given an array arr[] consisting of n integers. Our task is to create a program to find the maximum difference between the index of any two different numbers in C++.Code Description − Here, we need to find the maximum difference between the index of integer values of the array, given that the two integers are different.Let’s take an example to understand the problem, Inputarr[] = {4, 1, 3, 2, 1, 2, 4}Output5ExplanationThe difference between index 0, element 4, and index 5, element 2.Solution ApproachWe will try to find the maximum possible difference between the index ... Read More

Program to find the maximum element in a Matrix in C++

Ayush Gupta
Updated on 15-Sep-2020 15:14:05

1K+ Views

In this problem, we are given a matrix of size nXm. Our task is to create a program to find the maximum element in a Matrix in C++.Problem Description − Here, we need to simply find the largest element of matrix.Let’s take an example to understand the problem, Inputmat[3][3] = {{4, 1, 6}, {5, 2, 9}, {7, 3, 0}}Output9Solution ApproachThe solution to the problem is by simply traversing the matrix. This is done by using two nested loops, and checking whether each element of the matrix is greater than maxVal. And return the maxVal at the end.Program to illustrate the ... Read More

Maximum of four numbers without using conditional or bitwise operator in C++

Ayush Gupta
Updated on 15-Sep-2020 15:15:42

645 Views

In this problem, we are given four integer numbers. Our task is to create a program to find the maximum of four numbers without using conditional or bitwise operator in C++.Code Description − Here, we have four integer values. And we need to find the maximum value out of these numbers without using any conditional or bitwise operator.Let’s take an example to understand the problem, Inputa = 4, b = 7, c = 1, d = 9Output9Solution ApproachTo solve the problem, we will take two elements first, then take the greater element with pair. For each pair, we will create ... Read More

Advertisements