Remove Sub-Folders from the Filesystem in C++

Arnab Chakraborty
Updated on 30-Apr-2020 13:10:36

285 Views

Suppose we have a list of folders, we have to remove all sub-folders in those folders and return in any order the folders after removing. Here if a folder[i] is located within another folder[j], it is denoted as subfolder of it. The paths will be like folder1/subfolder2/… etc.Suppose the input is like["/myfolder", "/myfolder/secondfolder", "/another/document", "/another/document/extrafolder", "/another/final"], then the output will be: ["/myfolder", "/another/final", "/another/document"]To solve this, we will follow these steps −sort the folder array based on the length of the pathscreate one map m, and another array ansfor i in range 0 to size of path array – 1s ... Read More

Minimum Increment to Make Array Unique in C++

Arnab Chakraborty
Updated on 30-Apr-2020 13:08:33

371 Views

Suppose we have an array of integers A, here a move consists of choosing any A[i], and incrementing it by 1. We have to find the least number of moves to make every value in A unique. So if the input is like [3, 2, 1, 2, 1, 7], then the output will be 6, as after 6 moves, the array could be [3, 4, 1, 2, 5, 7], it can be shown with 5 or less moves that it is impossible for the array to have all distinct values.To solve this, we will follow these steps −ret:= 0sort array ... Read More

Toss Strange Coins in C++

Arnab Chakraborty
Updated on 30-Apr-2020 13:05:59

679 Views

Suppose we have some coins. The i-th coin has a probability prob[i] of facing heads when tossed. We have to show the probability that the number of coins facing heads equals target if you toss every coin exactly once. So if the prob array is like [0.5, 0.5, 0.5, 0.5, 0.5] and target is 0, then the output will be 0.03125.To solve this, we will follow these steps −n := size of prob arraycreate one 2d array of size n x (target + 5)set dp[0, 0] = 1 – prob[0] and dp[0, 1] := prob[0]for i in range 1 to ... Read More

Beautiful Array in C++

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

1K+ Views

Suppose for some fixed value of N, an array A is beautiful when it is a permutation of the integers 1, 2, ..., N, such that −For every i < j, there is no such k with i < k < j such that A[k] * 2 = A[i] + A[j].Suppose we have N, we have to find any beautiful array A.So if the input is like 5, then the output will be [3, 1, 2, 5, 4]To solve this, we will follow these steps −Create one array called ret, insert 1 into retwhile size of ret < Ncreate an ... Read More

Meeting Scheduler in C++

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

1K+ Views

Suppose we have the availability time slots lists slots1 and slots2 of two people and a meeting duration d, we have to find the earliest time slot that works for both of them and is of duration d. If there is no common time slot that satisfies the requirements, then show an empty array. Here the format of a time slot is an array of two elements [start, end] representing an inclusive time range from start to end. we can assume that no two availability slots of the same person intersect with each other. That is, for any two time ... Read More

Binary Subarrays with Sum in C++

Arnab Chakraborty
Updated on 30-Apr-2020 13:01:18

538 Views

Suppose an array A of 0s and 1s is given, we have to find how many non-empty subarrays have sum S? So if the input is like [1, 0, 1, 0, 1], and S = 2, then the result will be 4, as the subarrays are [1, 0, 1, 0, 1], [1, 0, 1, 0, 1], [1, 0, 1, 0, 1], [1, 0, 1, 0, 1].To solve this, we will follow these steps −Define a method called atMost(), this will take array A and integer xif x < 0, then return 0, set j := 0 and set ret := ... Read More

Dice Roll Simulation in C++

Arnab Chakraborty
Updated on 30-Apr-2020 12:58:42

688 Views

Suppose a die simulator generates a random number from 1 to 6 for each roll. We want to introduced a constraint to the generator such that it cannot roll the number i more than rollMax[i] (1-indexed) consecutive times. Consider we have an array of integers rollMax and an integer n, we have to return the number of distinct sequences that can be obtained with exact n rolls. The two sequences are considered different if at least one element differs from each other. So if n is 2, then rollMax = [1, 1, 2, 2, 2, 3], then the output will ... Read More

Flip String to Monotone Increasing in C++

Arnab Chakraborty
Updated on 30-Apr-2020 12:55:51

371 Views

Suppose a string of '0's and '1's is given. That string will be monotonic increasing if it consists of some number of '0's (possibly 0), followed by some number of '1's (also possibly 0.). We have a string S of '0's and '1's, and we may flip any '0' to a '1' or a '1' to a '0'. Find the minimum number of flips to make S monotone increasing. So if the input is like “010110”, then the output will be 2. By flipping we can get “011111” or “000111”.To solve this, we will follow these steps −n := size ... Read More

Path with Maximum Gold in C++

Arnab Chakraborty
Updated on 30-Apr-2020 12:55:21

540 Views

Suppose in a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 means that is empty. We have to find the maximum amount of gold that you can collect under the conditions −Every time we are pointing a cell we will collect all the gold in that cell.From our position we can walk one step to the left, right, up or down.We cannot visit the same cell more than once.Never visit a cell with 0 gold.So if the input is like [[0, 6, 0], ... Read More

Partition Array into Disjoint Intervals in C++

Arnab Chakraborty
Updated on 30-Apr-2020 12:52:20

454 Views

Suppose we have an array A, we have to partition it into two subarrays left and right such that −Every element in left subarray is less than or equal to every element in right subarray.left and right subarrays are non-empty.left subarray has the smallest possible size.We have to find the length of left after such a partitioning. It is guaranteed that such a partitioning exists.So if the input is like [5, 0, 3, 8, 6], then the output will be 3, as left array will be [5, 0, 3] and right subarray will be [8, 6].To solve this, we will ... Read More

Advertisements