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
Suppose we want to make a target string of lowercase letters.At first, we have the sequence as n '?' marks (n is the length of target string). We also have a stamp of lowercase letters.On each turn, we can place the stamp over the sequence, and replace every letter in the with the corresponding letter from that stamp. You can make up to 10 * n turns. As an example consider the initial sequence is "?????", and the stamp is "abc", then we may make strings like "abc??", "?abc?", "??abc" in the first turn.If the sequence is possible to stamp, ... Read More
Suppose we have a grid. There are few symbols. "." is indicating empty cell, "#" is for wall, "@" is for starting point, ("a", "b", ...) all are keys, and ("A", "B", ...) all are locks. We will start from the starting point, and one move consists of walking one space in one of the 4 directions (left, right, top, bottom). We will not go outside the grid, and there are walls to block our way. If we walk over a key, we pick it up. We can't walk over a lock unless we have the corresponding key.For each lock ... Read More
In this tutorial, we will be discussing a program to find the kth character after decrypting a string.For this, we will be provided with a string that will consist of characters and numbers and integer K. Our task is to decrypt the given string and find the character at Kth position.Example Live Demo#include #include using namespace std; //finding decrypted Kth character char findKthChar(string s, int k) { int len = s.length(); int i = 0; int total_len = 0; while (i < len) { if (isalpha(s[i])) { total_len++; ... Read More
Suppose we have a list of (axis-aligned) rectangles. Here each rectangle[i] = {x1, y1, x2, y2}, where (x1, y1) is the point of the bottom-left corner, and (x2, y2) are the point of the top-right corner of the ith rectangle.We have to find the total area covered by all rectangles in the plane. The answer may be very , so we can use modulo 10^9 + 7.So, if the input is likethen the output will be 6.To solve this, we will follow these steps −m = 10^9 + 7Define a function add(), this will take a, b, return ((a mod ... Read More
Suppose we want to define a function called countUniqueChars(s) that will return the number of unique characters on s, so if s = "HELLOWORLD" then "H", "E", "W", "R", "D" are the unique characters since they appear only once in s, therefore countUniqueChars(s) = 5.Now on this problem given a string s we have to find the sum of countUniqueChars(t) where t is a substring of s. (Here some substrings can be repeated so on this case we have to count the repeated ones too.)As the answer can be very large, we can return answer modulo 10^9+7.So, if the input ... Read More
Suppose we have a 2D grid of binary values (0s and 1s), we change at most one 0 to a 1. After that we have to find what is the size of the largest island? Here an island is a 4-directionally (top, bottom, left, right) connected group of 1s.So, if the input is like [[1, 0], [0, 1]], then the output will be 3, this is because if we change one 0 to 1 and connect two 1s, then we will get an island with area = 3.To solve this, we will follow these steps −Define an array dir of ... Read More
Suppose we have a grid of binary values (0s and 1s) the 1s in a cell represent the bricks. A brick will not drop when that satisfies these conditions −Either brick is directly connected to the top of the gridor at least one of its adjacent (top, bottom, left, right) bricks will not drop.We will do some erasures sequentially. In each case we want to do the erasure at the location (i, j), the brick (if that is present) on that location will disappear, and then some other bricks may drop because of that erasure. We have to find the ... Read More
Suppose we have one 2x3 board, there are 5 tiles those are represented by the numbers 1 through 5, and one empty square is there, that is represented by 0.Here a move means 0 and one adjacent number (top, bottom, left or right) and swapping it. This will be solved when the elements are arranged in this manner: [[1, 2, 3], [4, 5, 0]].We have the puzzle board; we have to find the least number of moves required so that the state of the board is solved. If this is not possible to solve, then return -1.So, if the input ... Read More
Suppose we have an array arr of integers, we have to split the array into some number of partitions, and individually sort each partition. Now after concatenating them we will get one sorted array. We have to find the maximum number of partitions we could have made?So, if the input is like [3, 2, 4, 5, 5], then the output will be 4, as we can make partitions like [3, 2], [4], [5], [5].To solve this, we will follow these steps −cnt := 1n := size of arrDefine an array maxOfLeft of size nDefine an array minOfRight of size nmaxOfLeft[0] ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP