Suppose we have one array of digits, we have to find the largest multiple of three that can be formed by concatenating some of the given digits in any order as we want. The answer may be very large so make it as string. If there is no answer return an empty string.So, if the input is like [7, 2, 8], then the output will be 87To solve this, we will follow these steps −Define one 2D array d, there will be three rowssort the array digitssum := 0for initialize i := 0, when i < size of digits, update ... Read More
Suppose we have a list of n orders, in each order there are pickup and delivery services. We have to count all valid pickup/delivery possible sequences such that delivery[i] is always after the pickup[i]. As the answer may very large, we will return it modulo 10^9 + 7.So, if the input is like 2, then the output will be 6, as All possible orders are (P1, P2, D1, D2), (P1, P2, D2, D1), (P1, D1, P2, D2), (P2, P1, D1, D2), (P2, P1, D2, D1) and (P2, D2, P1, D1). And the order (P1, D2, P2, D1) is not valid ... Read More
Suppose we have an array of integers target. From a starting array A consisting of all 1's, we can perform the following procedure −Consider x be the sum of all elements currently in our array.Choose index i, in range 0 to n, where n is the size of the array and set the value of A at index i to x.We can repeat this procedure as many times as we need.We have to check whether it is possible to make the target array from A otherwise return False.So, if the input is like [3, 9, 5], then the output will ... Read More
Suppose we have an array of integers called arr. We are initially at index 0. In one step we can jump from index i to i + x where: i + x < n. i - x where: i - x >= 0. j where: arr[i] and arr[j] are same and i and j are not same. Here n is the size of array. We have to find the minimum number of steps to reach the last index of the array.So, if the input is like, then the output will be 3, We need three jumps from index 0 to ... Read More
Suppose we have an array of integers called arr and an integer d. In one step we can jump from index i to −i + x where: i + x < n and x in range 1 to d.i - x where: i - x >= 0 and x in range 1 to d.Here n is the size of array. In addition, we can only jump from index i to index j when arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j. We can choose any index of the array and start the jumping. ... Read More
Suppose we want to schedule a list of tasks in d days. The tasks are dependent so, to work on the i-th task, we have to finish all the tasks j where 0 0, then −return 1^6if dp[idx, k] is not equal to -1, then −return dp[idx, k]maxVal := 0ret := inffor initialize i := idx, when i < size of v, update (increase i by 1), do −maxVal := maximum of v[i] and maxValret := minimum of ret and maxVal + solve(v, i + 1, k - 1, dp)dp[idx, k] := retreturn retFrom the main method do the ... Read More
Suppose we have one integer array called nums. The value of this array is defined as the sum of |nums[i]-nums[i+1]| for all i in range 0 to n - 1. Where n is the size of the array. We can select any subarray of the given array and reverse it. We can perform this operation only once. Then we have to find the maximum possible value of the final array.So, if the input is like [1, 5, 4, 2, 3], then the output will be 10.To solve this, we will follow these steps −ret := 0, extra := 0n := ... Read More
Suppose there is a one-dimensional garden on the x-axis. The starting position of the garden is 0, and ending position is n. There are n + 1 taps located at points [0, 1, ..., n] in the garden. If we have an integer n and an integer array ranges of length n + 1 where ranges[i] is the i-th tap can water the area [i - ranges[i], i + ranges[i]] when that area is open.We have to find the minimum number of taps that should be open to water the whole garden, if there is no possible solution, then return ... Read More
Suppose we have a keyboard layout like below −ABCDEFGHIJKLMNOPQRSTUVWXYZWhere each English uppercase letter is located at some coordinate, as an example, the letter A is placed at (0, 0), the letter B is placed at (0, 1), the letter P is placed at (2, 3) and the letter Z is placed at (4, 1). Now if we have a word, we have to find the minimum total distance to type such string using only two fingers. The distance between two places (x1, y1) and (x2, y2) is |x1 - x2| + |y1 - y2|. And we can start from any ... Read More
Suppose we have a string S; we have to find the number of distinct non-empty substrings of S that can be written as the concatenation of some string with itself.So, if the input is like "elloelloello", then the output will be 5, as there are some substrings like "ello", "lloe", "loel", "oell".To solve this, we will follow these steps −prime := 31m := 1^9 + 7Define a function fastPow(), this will take base, power, res := 1while power > 0, do −if power & 1 is non-zero, then −res := res * baseres := res mod mbase := base * ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP