Suppose we have a list of numbers called candies and someone is playing a game against his/her friend. In each round, a player can remove any two consecutive candies with the same value. And whoever can not pick up a candy loses and that player1 start first, we have to check whether player1 will win or not.So, if the input is like nums = [2, 2, 5], then the output will be True, as if player1 picks the 2s then the other player cannot pick any candies.To solve this, we will follow these steps:stack := a new stackturns := 0for ... Read More
Suppose we have a string s containing a boolean expression with operators "and" and "or", evaluate it and return the result. Here the expressions may have parentheses, which should be evaluated first.So, if the input is like s = "T and (F or T)", then the output will be TrueTo solve this, we will follow these steps:stack := a new listt = list of elements of s split by blank spacesfor each v in t, doif v[0] is same as "(", thenpush true when v[from index of "(" to end] is same as "T", into stackotherwise when ")" is found, ... Read More
Suppose we have a list of sorted numbers called days, where we must take the bus for on each day. We have to find the lowest cost it takes to travel for all the days. There are 3 types of bus tickets. 1-day pass for 2 bucks 7-day pass for 7 bucks 30-day pass for 25 bucksSo, if the input is like days = [1, 3, 5, 6, 28], then the output will be 9, as the lowest cost can be achieved by purchasing a 7-day pass in the beginning and then a 1-day pass on the 29th day.To solve ... Read More
Suppose we have two lists of numbers L1 and L2, the length of each list is n and each value is unique to its list, and values are in range 1 to n, we have to find the minimum number of adjacent swaps required to transform L1 to L2.So, if the input is like L1 = [0, 1, 2, 3] L2 = [2, 0, 1, 3], then the output will be 2, as we can swap 1 and 2, L1 will be [0, 2, 1, 3], and then 0 and 2, L1 will be [2, 0, 1, 3], this is ... Read More
Suppose we have a list of stock prices of a company in chronological sequence, and also have the transaction fee for one sell transaction. We have to find the maximum profit we could have made from buying and selling that stock any number of times. We must buy before we can sell it.So, if the input is like prices = [2, 10, 4, 8] fee = 3, then the output will be 6, as we can buy at 2 and sell at 10 and incur a fee of 3, so profit is 5. Then we buy at 4 and sell ... Read More
Suppose we have a 2D matrix of three different values, 2s, 1s, and 0s, where a 2 represents an enemy, 1 represents a wall and 0 represents an empty cell. We have to find the maximum enemies we can kill using one bomb. The bomb kills all the enemies in the same row and column from the planted point until it hits the wall. And we can put bombs only on blank spaces.So, if the input is likethen the output will be 3, as we can place the bomb at the green box to kill maximum 3 enemies.ret := 0n ... Read More
Suppose we have a 2d binary matrix, where 1 represents a bomb and 0 represents an empty cell. When a bomb explodes, all the spaces along on the same row and column are damaged. We have to find the number of spaces we can stand in to not get damaged.So, if the input is like110000000then the output will be 2, as there are two spaces the bottom right cell and the middle right cell are safe.To solve this, we will follow these steps:r := a list of size same as row count of matrix and fill with falsec := a ... Read More
Suppose we have a list of numbers called nums and another value k. We have to following operation k times: Select any number on the list. In the binary representation of that number, select a bit that is 0 and make it 1. Finally, we have to return the minimum possible sum of all the numbers after performing k operations. If the answer is too high, return result mode 10^9+7.So, if the input is like nums = [4, 7, 3] k = 2, then the output will be 17, as the binary representation of 4 is 100, 3 is 011, ... Read More
Suppose we have n children standing in a circle, and they are waiting to get a balloon. The distribution is carried out starting with the kth child (first at index 0), and giving them a balloon they left the circle. Now every kth child gets a balloon going clockwise until there is only one child left that gets a balloon. So if we have n and k, we have to find the starting index of the child that receives the last balloon.So, if the input is like n = 3 k = 2, then the output will be 1, in ... Read More
Suppose we have a number n; we have to find the smallest next higher number with the same number of 1s as n in binary form.So, if the input is like n = 7, then the output will be 11, as 7 in binary is 0111 and next higher from 7 with three ones would be 11 which is 1011 in binary.To solve this, we will follow these steps:copy := n, zeros := 0, ones := 0while copy is not 0 and copy is even, dozeros := zeros + 1copy = copy / 2while copy is odd, doones := ones ... Read More