Found 7197 Articles for C++

Remove Covered Intervals in C++

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

308 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

554 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

272 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

316 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

Count Number of Nice Subarrays in C++

Arnab Chakraborty
Updated on 30-Apr-2020 09:43:00

857 Views

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

Minimum Swaps to Make Strings Equal in C++

Arnab Chakraborty
Updated on 30-Apr-2020 09:40:37

518 Views

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

Tree Diameter in C++

Arnab Chakraborty
Updated on 30-Apr-2020 09:38:13

1K+ Views

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

Design A Leaderboard in C++

Arnab Chakraborty
Updated on 30-Apr-2020 06:59:12

1K+ Views

Suppose we have to design a Leaderboard class, there are three different functions −addScore(playerId, score) − This will update the leaderboard by adding score to the given player's score. When there is no such player with given id in the leaderboard, add him to the leaderboard with the given score.top(K) − This will return the score sum of the top K players.reset(playerId) − This will reset the score of the player with the given id to 0. It is guaranteed that the player was added to the leaderboard before calling this function.Initially, the leaderboard should empty.If we perform the operations ... Read More

Longest Arithmetic Subsequence of Given Difference in C++

Arnab Chakraborty
Updated on 30-Apr-2020 06:49:02

443 Views

Suppose we have an integer array arr and an integer difference, we have to find the length of the longest subsequence in arr which is an arithmetic sequence such that the difference between adjacent elements in the subsequence is same as the difference. So if the input is like [1, 5, 7, 8, 5, 3, 4, 2, 1] and difference is -2, then the output will be − 4, as the longest arithmetic sequence is [7, 5, 3, 1]To solve this, we will follow these steps −Define a map mn := size of array arr, set ans := 0for i ... Read More

Two Sum BSTs in C++

Arnab Chakraborty
Updated on 30-Apr-2020 06:45:28

189 Views

Suppose we have two binary search trees, we have to return True iff there is a node in the first tree and a node in the second tree and sum of these nodes is a given integer target. So if the tree is like −and target is 5, then the result is true.To solve this, we will follow these steps −Define a map sdefine a method called check(), this will take node, target and nodeNumber, this will work as follows −if node is valid, then return falsecurr := value of node, req := target – currif req is present in ... Read More

Advertisements