Server Side Programming Articles - Page 1856 of 2650

XOR Queries of a Subarray in C++

Arnab Chakraborty
Updated on 30-Apr-2020 11:30:17

276 Views

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

Jump Game III in C++

Arnab Chakraborty
Updated on 30-Apr-2020 11:26:25

447 Views

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

All Elements in Two Binary Search Trees in C++

Arnab Chakraborty
Updated on 30-Apr-2020 11:22:51

273 Views

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

Deepest Leaves Sum in C++

Arnab Chakraborty
Updated on 30-Apr-2020 11:13:24

249 Views

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

Maximum Number of Occurrences of a Substring in C++

Arnab Chakraborty
Updated on 30-Apr-2020 11:08:37

595 Views

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

Maximum Side Length of a Square with Sum Less than or Equal to Threshold in C++

Arnab Chakraborty
Updated on 30-Apr-2020 10:59:54

223 Views

Suppose we have a m x n matrix mat and an integer threshold. We have to the maximum side-length of a square with a sum less than or equal to the given threshold or return 0 if there is no such square. So if the input is like −113243211324321132432113243211324321132432And threshold is 4, then output will be 2, as there are two squares of side length 2, so max is 2To solve this, we will follow these steps −Define a method called ok, this will take x and matrix m and threshold thset curr := 0for i in range x – ... Read More

Remove Covered Intervals in C++

Arnab Chakraborty
Updated on 30-Apr-2020 10:51:38

320 Views

Suppose we have a list of intervals, we have to remove all intervals that are covered by another interval in the list. Here Interval [a,b) is covered by interval [c,d) if and only if c

Iterator for Combination in C++

Arnab Chakraborty
Updated on 30-Apr-2020 10:49:00

564 Views

Suppose we have to design an Iterator class, that consists of few operations −Define a constructor that takes a string characters of sorted distinct lowercase English letters and a number combinationLength as parameter.Define a function next() that returns the next combination of length combinationLength in alphabetic order.Define another function hasNext() that returns True if and only if there exists a next combination.So if the input is like −CombinationIterator iterator = new CombinationIterator("xyz", 2); iterator.next(); // returns "xy" iterator.hasNext(); // returns true iterator.next(); // returns "xz" iterator.hasNext(); // returns true iterator.next(); // returns "yz" iterator.hasNext(); // returns falseTo solve this, we ... Read More

Delete Tree Nodes in C++

Arnab Chakraborty
Updated on 30-Apr-2020 09:50:17

284 Views

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

Remove Interval in C++

Arnab Chakraborty
Updated on 30-Apr-2020 09:47:27

330 Views

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

Advertisements