Suppose a=1, b=2,......, z=26. Given a string Str, which carries the number series. The task is to figure out all the ways to convert it into a letter series.
Example 1:
Input:
421 a Value of Str
Output:
2
Explanation:
Here, Str=421.
First possibility is 4 = d and 21 = u, so du.
Second possibility is 4 = d, 2 = b and 1 = a, so dba.
Hence the output is 2.
Note: Possibility of {42,1} will not take into consideration because 42 is greater than 26 and not valid digit for conversion.
Example 2:
Input:
101 a Value of Str
Output:
1
Ria likes to play a number games always. Today she decided to find the largest number that can be made using all of the digits of the given input (Non-negative integer) value N.
Example 1:
Input
3675092 a Value of N
Output
9765320
Example 2:
Input:
02856 a Value of N
Output:
8652
Note: Since input value, N accepts integer value only, hence digit 0 that comes before the first nonzero digit should not taken into consideration of input.
Constraints
1 < N < 10000000
The Input format for testing
The candidate has to write the code to accept a positive integer number.
Given a number N, the task is to split the Num into multiple parts in such a fashion as to find the highest product. Print the highest product value.
Consider N= 4.
Best: 4 = 2 + 2 and 2 * 2 = 4
Similarly for N = 6, (3*3) = 9
For N = 8, (233) = 18
For N = 15, (33333) = 243
Constraints:
1 <= N <= 100
Example 1
Input
4 a Value of N
Output
4
Example 2
Input
6 a Value of N
Output
9
Given a non-negative integer array Arr having size N. Each element of the array will carry a different value. This means no two elements can have same values. the candidate has to do with minimal changes in the original value of elements, making every element as least as much as it originally had.
Find the minimum sum of all elements that can be set the array for.
Example 1:
Input
3 a Value of N, represents size of Arr
2 a Value of Arr[0]
2 a Value of Arr[1]
4 a Value of Arr[2]
Output
9
Explanation
As of two elements have the same value, max value for one of them needs to be incremented to 3.
He can set the array with 2+3+4=9.
A group of friends are playing cards. Only numeric cards are in consideration along with the joker card(equivalent to 0) and the symbols in the cards are not taken into account. Each player will receive a set of cards. The rule of the game is to rearrange the set of cards to the possible lowest sequence. Player with the set of cards forming the smallest number wins the game. The sequence of cards should not start with a joker card. Task is to write a program for developing the logic of the game considering the set of cards as a number.
Example 1:
Input
3421 --- Value of CardSet
Output
1234 --- Prints the lowest number matching the set criteria
Explanation:
The lowest number formation by rearranging the digits of the number 3421 and not starting with a 0 will1234 Hence, the output is a '1234'.
Aman is in a science lab and in today's experiment he is given a task to bring z ml of the starch solution into a test tube by his teammates. He is given three test tubes a, B and C of capacities x, y and N ml respectively, where (N=x+y). His task is to fill test tube C with z ml with the help of test tubes A and B from a large beaker.
Aman is not good at handling test tubes. While handling any test tube A or B, he either empties it or fully fills it but does not waste the starch solution while filling any test tube A or B. You can fill the test tubes A or B either from the beaker or from test tube C. Your task is to determine, will he be able to get z ml starch solution in test tube C or not.
Example 1:
Input
3 4 ---- Value of x and y respectively
2 ---- Value of z
Output
YES ---- Denotes whether Aman can get the required solution for experiment
Explanation:
He can first fill test tube by 6ml by filling A test tube two times, (3+3)=6
Then he can fill test tube B completely from test tube C once, (6-4)=2
2 ml remains in the test tube.
In the question, we are given an integer array consisting of only non-negative integers, and we can increase some elements (since each element should be greater than or equal to what it was originally) so that all elements of the array are unique, and they have their sum as low as possible.
The main idea for this would be to start from the smallest element in the array, and check its freq. If it has a frequency of 1, we move on to the next. Otherwise, apart from 1 occurrence of this element, for all other occurrences we add 1 to the element. This is done because the frequency of each element can only be 1. Now we move on to the next element, and add 1 to all occurrences except one of them. We keep on doing this till we get to the last element, so that each element has frequency equal to 1.
Now while implementing, finding all the occurrences of each element in the array can take O(n) time making the overall time complexity O(n^2) which is not desired. So we find another method to do this. We can keep a set, which consists of all the unique elements in the array currently, and a map which stores the frequency of all these elements. For the initial array, this can be done in O(nlogn) time.
Now we take the smallest element from the set, and check its frequency. If the number is say num and its frequency is greater than 1, say val, then we increase the frequency of num+1 by val-1 and decrease the frequency of num to 1. So now, num has frequency 1 and so it is unique. All other occurrences of num have been increased to num+1. We also add num+1 to the original set which consisted of all unique elements of the array if it is not present already. Now, for moving on to the next element, we binary search on the unique elements in the set, to find the next greater element present in the array. This can be implemented in C++ using upper_bound function. We end the loop when the upper bound gives us the end of the set.
set < int > unique;
map < int, int > freq;
// Initialize unique and freq
int curr = smallest_element - 1;
while(true)
{
auto it = unique.upper_bound(curr);
if(it==unique.end())
break;
int val = *it;
if(freq[val]==1)
{
curr = val;
continue;
}
freq[val+1] += (freq[val]-1);
unique.insert(val+1);
curr = val;
}
Question 2
Overview
Solution
O(NlogN)
that may interview needed from you.0 to 9
.Question 3
Overview
a1 * a2 * a3 …. * aK such that n = a1 + a2 + a3 … + aK and a1, a2, … ak > 0
.PreRequiste
Solution
n into (n/k)
k’s then their product will be k(n/k)
, now if we take the derivative of this product and make that equal to 0 for maxima, we will get to know that value of k should be e (base of the natural logarithm) for the maximum product.6 = 3 + 3 = 2 + 2 + 2, but 3 * 3 > 2 * 2 * 2,
that is every triplet of 2 can be replaced with a tuple of 3 for the maximum product,2*2 (2*2 > 3*1)
and 2 respectively and we will get our maximum product.We can solve the given problem by using dynamic programming. Let dp[i]
denote the number of ways of decoding the prefix of length i
of the given string. Now, we have two choices :
i
th character alone gets mapped to a valid character and thus our problem reduces to finding the number of ways to decode the prefix of length i-1
.i-1
th and i
th characters combined map to a valid character and thus our problem reduces to finding the number of ways to decode the prefix of length i-2
.Hence, by using the above transitions, we can find the number of ways of decoding the entire string, i.e. dp[n]
in O(N)
.
We can implement the given dynamic programming either using tabulation or memoization. The pseudoCode below implements the solution using memoization.
valid(T): //helper function that returns true if the given string is a valid character code and false otherwise.
if(T[0] == '0' or int(T) > 26):
return false
return true
solve(S,index):
if(index<0): //base case, if we have 0 characters there is exactly one way to decode it
return 1
if(dp[index]!=-1): //If we have already calculated the ans for this index, we return it
return dp[index]
if(valid(S[index])):
dp[index] = solve(S,index-1) // Calculating the dp for this prefix using the transitions explained.
if(valid(S[index-1:index])):
dp[index] += solve(S,index-2)