Found 26504 Articles for Server Side Programming

Can Make Palindrome from Substring in Python

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

186 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

391 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

Maximum Level Sum of a Binary Tree in C++

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

278 Views

Suppose we have the root of a binary tree, the level of its root is 1, the level of its children is 2, and so on. We have to return the smallest level X such that the sum of all the values of nodes at level X is maximal. So if the tree is like −Then the output will be 2, The level 1 sum = 1, level 2 sum is 7 + 0 = 7, level 2 sum is 7 + (-8) = -1, so max is of level 2, so output is 2.To solve this, we will follow ... Read More

Number of Dice Rolls With Target Sum in Python

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

858 Views

Suppose we have d dice, and each die has f number of faces numbered 1, 2, ..., f. We have to find the number of possible ways (out of fd total ways) modulo 10^9 + 7 to roll the dice so the sum of the face up numbers equal to the target. So if the input is like d = 2, f = 6, target = 7, then the output will be 6. So if we throw each dice with 6 faces, then there are 6 ways to get sum 6, as 1 + 6, 2 + 5, 3 + ... Read More

Minimum Swaps to Group All 1's Together in Python

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

343 Views

Suppose we have a binary array data, we have to find the minimum number of swaps required to group all 1’s stored in the array together in any place in the array. So if the array is like [1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1], then the output will be 3, as possible solution is [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1]To solve this, we will follow these steps −set one := 0, n:= length of data arraymake an array summ of size n, and fill this with 0, set summ[0] := ... Read More

Snapshot Array in Python

Arnab Chakraborty
Updated on 30-Apr-2020 05:56:46

599 Views

Suppose we have to Implement a SnapshotArray that supports the following interfaces −SnapshotArray(int length) this will initialize the array-like data structure with the given length. Initially, each element equals 0.set(index, val) this will sets the element at the given index to be equal to val.snap() will take a snapshot of the array and returns the snap_id: the total number of times we called snap() minus 1.get(index, snap_id) this will return the value at the given index, at the time we took the snapshot with the given snap_idSo if the array size is 2, it is set using [0, 5], after ... Read More

Binary Tree Coloring Game in Python

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

523 Views

Suppose there are two players play a turn-based game on a binary tree. We have the root of this binary tree and the number of nodes n in the tree. Here n is odd, and each node has a distinct value from 1 to n. At first, the first player names a value x with 1

Decrease Elements To Make Array Zigzag in Python

Arnab Chakraborty
Updated on 30-Apr-2020 11:43:20

400 Views

Suppose we have an array nums of integers, a move operation is actually choosing any element and decreasing it by 1. An array A is a zigzag array if either 1 or 2 is satisfied −Every even-indexed element is greater than adjacent elements, So. A[0] > A[1] < A[2] > A[3] < A[4] > ... and so on.Every odd-indexed element is greater than adjacent elements, So. A[0] < A[1] > A[2] < A[3] > A[4] < ... and so on.We have to find the minimum number of moves to transform the given array nums into a zigzag array.So if the ... Read More

Connecting Cities With Minimum Cost in Python

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

416 Views

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

Advertisements