Suppose we have one m * n matrix called mat and an integer K, we have to find another matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K
Suppose we have the array arr of positive integers and the array queries where queries[i] = [Li, Ri], for each query the i compute the XOR of elements from Li to Ri (that is, arr[Li] XOR arr[Li+1] xor ... xor arr[Ri] ). We have to find the array containing the result for the given queries. So if the input is like − [1, 3, 4, 8], and queries are like [[0, 1], [1, 2], [0, 3], [3, 3]], then the result will be [2, 7, 14, 8]. This is because the binary representation of the elements in the array are ... Read More
Suppose we have an array of non-negative integers arr, we are initially positioned at start index of the array. When we are present at index i, we can jump to i + arr[i] or i - arr[i], check if we can reach to any index with value 0. We have to keep in mind that we cannot jump outside of the array at any time. So if the input is like: arr = [4, 2, 3, 0, 3, 1, 2] and start from 5, then output will be true, as move 5 → 4 → 1 → 3, or 5 ... Read More
Suppose we have two binary search trees, we have to return a list of values, that has all elements present in these trees, and the list elements will be in ascending order. So if the trees are like −Then the output will be [0, 1, 1, 2, 3, 4].To solve this, we will follow these steps −Define an array called ans, define two stacks st1 and st2curr1 := root1 and curr2 := root2insert node root1 and all left nodes into st1, insert node root2 and all left nodes into st2while st1 is not empty or st2 is not emptyif st1 ... Read More
Suppose there are N cities numbered from 1 to N. We have connections, where each connection [i] is [city1, city2, cost] this represents the cost to connect city1 and city2 together. We have to find the minimum cost so that for every pair of cities, there exists a path of connections (possibly of length 1) that connects those two cities together. The cost is the sum of the connection costs used. If the task is impossible, return -1.So if the graph is like −Then the output will be 6, Choosing any two cities will connect all cities, so we choose ... Read More
Suppose we have directed graph, with nodes labelled 0, 1, ..., n-1. In this graph, each edge is colored with either red or blue colors, and there could be self-edges or parallel edges. Each [i, j] in red_edges indicates a red directed edge from node i to node j. Similarly, each [i, j] in blue_edges indicates a blue directed edge from node i to node j. We have to find an array answer of length n, where each answer[X] is the length of the shortest path from node 0 to node X such that the edge colors alternate along the ... Read More
Suppose we have a rooted binary tree, we have to return the lowest common ancestor of its deepest leaves. We have to keep in mind that −The node of a binary tree is a leaf node if and only if it has no childrenThe depth of the root of the tree is 0, and when the depth of a node is d, the depth of each of its children is d+1.The lowest common ancestor of a set S of nodes in the node A with the largest depth such that every node in S is in the subtree with root ... Read More
Suppose we have the root of a binary tree; we have to find the maximum average value of any subtree of that tree. So if the tree is like −The output will be 6, this is because, for node 5, it will be (5 + 6 + 1)/ 3 = 4, then for node 6 it will be 6 / 1 = 6, and for node 1, it will be 1 / 1 = 1, so the max is 6.To solve this, we will follow these steps −res := 0Define a method called solve(), this will take rootif root is ... Read More
Suppose we have a binary tree, we have to find the sum of values of its deepest leaves. So if the tree is like −Then the output will be 15.To solve this, we will follow these steps −Define a map m, and maxDepthDefine a recursive method solve(), this will take node and level, initially level is 0if node is not present, then returnmaxDepth := max of level and maxDepthincrease m[level] by value of nodesolve(left of node, level + 1)solve(right of node, level + 1)In the main method, setup maxDepth := 0, then solve(root, 0)return m[maxDepth]Example(C++)Let us see the following implementation ... Read More
Suppose we have a string s, we have to find the maximum number of occurrences of any substring that satisfies the following rules −The number of distinct characters in the substring must be less than or equal to maxLetters.The substring size must be in range minSize and maxSize inclusive.So if the input is like − “aababcaab”, maxLetters = 2, minSize = 3 and maxSize = 4, then the output will be 2. The substring "aab" has 2 occurrences in the original string. This satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).To solve this, we will ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP