Suppose we have two strings s and t. we have to check whether s is rotation of t or not, in other words, can we get t after rotating s?So, if the input is like s = "helloworld" and t = "worldhello", then the output will be True.To solve this, we will follow these steps −if size of s0 is not equal to size of s1, then −return falses := s0 concatenate s0return true when s1 is in s, otherwise 0Let us see the following implementation to get better understanding −Example Live Demo#include using namespace std; class Solution { ... Read More
Suppose we have two strings s and t of same length, and both are in lowercase letters. Consider we have rearranged s at first into any order, then count the minimum number of changes needed to turn s into t.So, if the input is like s = "eccynue", t = "science", then the output will be 2 as if we rearrange "eccynue" to "yccence", then replace y with s and second c with i, it will be "science".To solve this, we will follow these steps −ret := 0Define two arrays cnt1 to hold frequency of s and cnt2 to hold ... Read More
Suppose we have a list of TV shows, and another list of duration, and an integer k, here shows[i] and duration[i] shows the name and duration watched by the ith person, we have to find the total duration watched of the k most watched shows.So, if the input is like shows: ["Castle Play", "Fairy Tale Series", "Castle Play", "Jerry Mouse", "Rich Boy"], duration: [6, 4, 6, 14, 5] and k = 2, then the output will be 26.To solve this, we will follow these steps −Define one map mn := size of vfor initialize i := 0, when i < ... Read More
Suppose we have a list of fractions where each fraction contains [numerator, denominator] (numerator / denominator). We have ti find a new list of fractions such that the numbers in fractions are −In their most reduced terms. (20 / 14 becomes 10 / 7).Any duplicate fractions (after reducing) will be removed.Sorted in ascending order by their actual value.If the number is negative, the '-' sign will be with the numerator.So, if the input is like {{16, 8}, {4, 2}, {7, 3}, {14, 6}, {20, 4}, {-6, 12}}, then the output will be [[-1, 2], [2, 1], [7, 3], [5, 1]]To ... Read More
Suppose we have a list of requests, where requests[i] contains [t, d] indicating at time t, a person arrived at the door and either wanted to go inside (inside is indicating using 1) or go outside (outside is indicating using 0).So if there is only one door and it takes one time unit to use the door, there are few rules that we have to follow −The door starts with 'in' position and then is set to the position used by the last participant.If there's only one participant at the door at given time t, they can use the door.If ... Read More
Suppose we have a list of lists of integers intervals where each element has interval like [start, end]. We have to find the most frequently occurred number in the intervals. If there are ties, then return the smallest number.So, if the input is like [[2, 5], [4, 6], [7, 10], [8, 10]], then the output will be 4To solve this, we will follow these steps −Define one map mcnt := 0, val := 0for each value it in x −(increase m[it[0]] by 1)decrease m[it[1] + 1] by 1last := 0for each key it in mlast := last + value of ... Read More
Suppose we have one number n. Here n indicates n full beer bottles. If we can exchange 3 empty beer bottles for 1 full beer bottle, we have to find the number of beer bottles we can drink.So, if the input is like 10, then the output will be 14.To solve this, we will follow these steps −Define a function solve(), this will take n, ret := 0while n >= 3, do −q := n / 3ret := ret + q * 3n := n - q * 3n := n + qret := ret + nreturn retLet us see ... Read More
Suppose we have a list of integers nums, we have to sort the list in this manner −First element is maximumSecond element is minimumThird element is 2nd maximumFourth element is 2nd minimumAnd so on.So, if the input is like [6,3,10,4], then the output will be [10, 3, 6, 4]To solve this, we will follow these steps −Define an array retsort the array numsj := size of nums - 1i := 0while i
Suppose we have a list of unique integers called nums. We have to find the number of integers that could still be successfully found using a standard binary search.So, if the input is like [2,6,4,3,10], then the output will be 3, as if we use binary search to look for 4, we can find it at first iteration. We can also find 2 and 10 after two iterations.To solve this, we will follow these steps −Define a function help(), this will take target, an array & nums,low := 0high := size of nums - 1while low
Suppose we have a non-negative integer called num, we have to check whether it is a palindrome or not, but not using a string.So, if the input is like 1331, then the output will be true.To solve this, we will follow these steps −ret := 0x := numwhile num > 0, do −d := num mod 10ret := ret * 10ret := ret + dnum := num / 10return true when x is same as retLet us see the following implementation to get better understanding −Example Live Demo#include using namespace std; class Solution { public: bool solve(int num) ... Read More