Suppose we have a string s. We have to find the sum of beauty of all of its substrings. The beauty of a string is actually the difference in frequencies between the most frequent and least frequent characters. So if the string is "abaacc", then its frequency is 3 - 1 = 2.So, if the input is like s = "xxyzy", then the output will be 5 because the substrings with non-zero beauty are ["xxy", "xxyz", "xxyzy", "xyzy", "yzy"], each has beauty value 1.To solve this, we will follow these steps −res:= 0for i in range 0 to size of ... Read More
Suppose we have an array called stones where stones[i] represents the value of the ith stone from the left. Two friends Amal and Bimal re playing a turn based game with these stones and Amal starts first always. There are n stones arranged in a row. Each player can remove either the leftmost stone or the rightmost stone from the row and get points equal to the sum of the remaining stones' values in the row. Who will get the higher score will win. Now, Bimal found that he will always lose this game, so he decided to minimize the ... Read More
Suppose we have a number n, we have to check whether it is possible to represent n as the sum of distinct powers of three or not. An integer y is said to be power of three if there exists an integer x such that y = 3^x.So, if the input is like n = 117, then the output will be True because 117 = 3^4 + 3^3 + 3^2 + = 81 + 27 + 9.To solve this, we will follow these steps −for i in range 16 to 0, decrease by 1, doif n >= 3^i , thenn ... Read More
Suppose we have a number n in string format. We have to find minimum deci-binary numbers are required, so that whose sum is equal to n. A deci-binary number is a decimal number whose digits are either 0 or 1.So, if the input is like n = "132", then the output will be 3 because 132 is sum of three decibinary number (10 + 11 + 111).To solve this, we will follow these steps −result := 1for each i in n, doif i is not in {0, 1}, thenresult := maximum of result and ireturn resultExampleLet us see the following ... Read More
Suppose we have an array nums, a ramp is a tuple (i, j) for which i < j and nums[i]
Suppose Amal and Bimal are playing a game and Amal's turn is first. The game is like below −There are n stones in a pile. Each player can take a stone from the pile and receive points based on the position of that stone. Amal and Bimal may value the stones in different manner.We have two arrays of same length, A_Values and B_Values. Each A_Values[i] and B_Values[i] represents how Amal and Bimal, respectively, value the ith stone. Here whose score is maximum, he will be winner after all the stones are taken out. If there is a tie, the game ... Read More
Suppose we have an array nums and that is sorted in non-decreasing order. We have to make an array called result with the same length as nums such that result[i] is the summation of absolute differences between nums[i] and all the other elements in the array.So, if the input is like nums = [5, 7, 12], then the output will be [9, 7, 12] because|5-5| + |5-7| + |5-12| = 0+2+7 = 9|7-5| + |7-7| + |7-12| = 2+0+5 = 7|5-12| + |7-12| + |12-12| = 7+5+0 = 12To solve this, we will follow these steps −res := a new ... Read More
Suppose we have two arrays of called nums1 and nums2. The values in the arrays are between 1 and 6(inclusive). In one operation, we can update any value in any of the arrays to any value between 1 and 6. We have to find the minimum number of operations needed to make the sum of values in nums1 equal to the sum of values in nums2. We have to return -1 if it is not possible.So, if the input is like nums1 = [1, 5, 6], nums2 = [4, 1, 1], then the output will be 2 because we can ... Read More
Suppose we have a number n, we have to find the decimal value of the binary string by concatenating the binary representations of 1 to n one by one in order, if the answer is too large then return answer modulo 10^9 + 7.So, if the input is like n = 4, then the output will be 220 because, by concatenating binary representation from 1 to 4 will be "1" + "10" + "11" + "100" = 110111000, this is binary representation of 220.To solve this, we will follow these steps −ans := 1m := 10^9+7for i in range 2 ... Read More
Suppose we have two arrays called baseCosts with n items from them we can select base and toppingCosts with m items from them we can select toppings and also have a target value. We have to follow these rules to make dessert.There must be exactly one base.We can add one or more topping or have no toppings at all.There are at most two of each type of topping.Here baseCosts[i] represents the price of the ith ice cream base. The toppingCosts[i] represents price of one of the ith topping. The target value is representing the target price for dessert. We have ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP