Server Side Programming Articles - Page 1857 of 2650

Count Number of Nice Subarrays in C++

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

867 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

534 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

453 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

205 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

Maximum Subarray Sum with One Deletion in C++

Arnab Chakraborty
Updated on 30-Apr-2020 06:36:31

367 Views

Suppose we have an array of integers; we have to find the maximum sum for a non-empty subarray (contiguous elements) with at most one element deletion. In other words, we can say that we want to choose a subarray and optionally delete one element from it so that there is still at least one element left and the sum of the remaining elements is maximum possible. We have to keep in mind that the subarray needs to be non-empty after deleting one element. So if the input is like [1, -2, 0, 3], then the output will be 4. So ... Read More

Can Make Palindrome from Substring in Python

Arnab Chakraborty
Updated on 30-Apr-2020 06:32:37

193 Views

Suppose we have a string s, we have to make queries on substrings of s. For each query queries[i], there are three parts [left, right, k], we may rearrange the substring s[left], ..., s[right], and then choose up to k of them to replace with any lowercase English letter. If the substring is possible to be a palindrome after the operations mentioned above, the result of the query is true. Otherwise false. We have to find an array answer[], where answer[i] is the result of the i-th query queries[i].For example, if the input is “abcda”, queries is like [[3, 3, ... Read More

Minimum Cost to Connect Sticks in C++

Arnab Chakraborty
Updated on 30-Apr-2020 06:27:42

403 Views

Suppose we have some sticks with positive integer lengths. We can connect any two sticks of lengths X and Y into one stick by paying a cost of X + Y. This will be performed until there is one stick remaining. We have to find the minimum cost of connecting all the given sticks into one stick in this way. So if the stack is [2, 4, 3], then the output will be 14.To solve this, we will follow these steps −Define a max heap priority queue pqinsert all elements of s into pqans := 0while pq has more than ... Read More

Design File System in Python

Arnab Chakraborty
Updated on 30-Apr-2020 06:22:05

2K+ Views

Suppose we have to design a file system which provides these two functions −createPath(path, value) − This creates a new path and associates a value to it if possible and returns True. It returns False if the path already exists or its parent path doesn't exist.get(path) − This finds the value associated with a path or returns -1 if the path doesn't exist.The format of a path is one or more concatenated strings of the form − (forward slash) / followed by one or more lowercase English letters. For example, /programming and /programming/problems are valid paths while an empty string ... Read More

Advertisements