Suppose we have a tree, this tree is rooted at node 0, this is given as follows −Number of nodes is nodesValue of ith node is value[i]Parent of ith node is parent[i]We have to remove every subtree whose sum of values of nodes is 0, after doing that return the number of nodes remaining in the tree. So if the tree is like −There are 7 nodes, the output will be 2To solve this, we will follow these steps −Create a map called childrendefine a method called dfs(), this will take node, an array value and graphtemp := a pair ... Read More
Suppose we have a sorted list of disjoint intervals, each interval intervals[i] = [a, b] represents the set of numbers x such that a b[0], then swap a and bif a and b are intersecting, then call manipulate2(ans, t)return ansExample(C++)Let us see the following implementation to get a better understanding − Live Demo#include using namespace std; void print_vector(vector v){ cout
Suppose we have an unsorted array nums, we have to rearrange it such that nums[0] < nums[1] > nums[2] < nums[3]. So if the input is like [1, 5, 1, 1, 6, 4], then the output will be [1, 4, 1, 5, 1, 6].To solve this, we will follow these steps −make one array x, that has same elements as numssort x arrayi := size of x – 1, j := (size of x – 1) / 2 and n := size of nums arrayfor l in range 0 to n – 1, increase l by 2 in each stepnums[l] ... Read More
Suppose we have an array of integers nums and an integer k. A subarray is known as nice subarray if there are k odd numbers on it. We have to find the number of nice sub-arrays. So if the array is [1, 1, 2, 1, 1], and k = 3, then the output will be 2, as the subarrays are [1, 1, 2, 1], and [1, 2, 1, 1]To solve this, we will follow these steps −ans := 0, n := size of nums arrayleft := 0 and right := 0, and count := 0define an array odd, fill this ... Read More
Suppose we have a 2D binary matrix filled with 0's and 1's. We have to find the largest square containing only 1's and return its area. So if the matrix is like −10100101101111110010Then the output will be 4To solve this, we will follow these steps −ans := 0, n := number of rows, c := number of rowsif n is 0, then return 0Create another matrix of order (n x c)for i in range 0 to n – 1for j in range 0 to c – 1m[i, j] := matrix[i, j]ans := maximum of m[i, j] and ansfor j in ... Read More
Suppose we have two strings s1 and s2 of equal length consisting of letters only "x" and "y". Our task is to make these two strings equal to each other. We can swap any two characters that belong to different strings, which means − swap s1[i] and s2[j]. We have to find the minimum number of swaps required to make s1 and s2 equal, or return -1 if it is impossible to do so. So if the strings are s1 = “xy” and s2 = “yx”, then the output will be 2. If we swap s1[0] and s2[0], s1 = ... Read More
Suppose we have an array of n elements, and a positive integer s. We have to find the minimal length of a contiguous subarray, of which the sum is greater or equal to s. If there isn’t one, then return 0 instead. So if the array is like [2, 3, 1, 2, 3, 4] and sum is 7, then the output will be 2. This is the subarray [4, 3] has the minimum length for this case.To solve this, we will follow these steps −ans := 0, n := size of array A, j := 0 and sum := 0for ... Read More
Suppose we have an undirected tree; we have to find its diameter − the number of edges in the longest path in that tree is the diameter of that tree. Here tree is given as an edge list where edges[i] = [u, v] is a bidirectional edge between nodes u and v. Each node has labels in the set {0, 1, ..., edges.length}. So if the graph is like −The output will be 4.To solve this, we will follow these steps −Define a map ldefine a method called dfs(). this will take v, an array called visited, the graph and ... Read More
Suppose we have two integers representing the numerator and denominator of a fraction, we have to find fraction in string format. If the fractional part is repeating, enclose the repeating part in parentheses. So if the numerator is 2 and denominator is 3, then the output will be “0.(6)”To solve this, we will follow these steps −if numerator is 0, then return 0define one array ansif the numerator < 0 and denominator > 0 or numerator 0 and denominator < 0, then insert negative symbol ‘-’ into the ans arraydivisor := |numerator| and dividend := |denominator|, remainder := divisor mod ... Read More
Suppose we have a linked list like l1 -> l2 -> l3 -> l4 -> … -> l(n-1) -> ln. We have to rearrange this list into the form l1 -> ln -> l2 -> l(n - 1) -> … and so on. Here the constraint is that, we cannot modify the values in the list nodes, only the node itself may be changed. So for example, if the list is like [1, 2, 3, 4, 5], then the output will be [1, 5, 2, 4, 3]To solve this, we will follow these steps −Define a method called reverse to ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP