Program asked in a online hiring challenge - math

Given N integers in the form of Ai where 1≤i≤N, the goal is to find the M that minimizes the sum of |M-Ai| and then report that sum.
For example,
Sample Input: 1 2 4 5
Sample Output: 6
Explanation: One of the best M′s you could choose in this case is 3.
So the answer = |1−3|+|2−3|+|4−3|+|5−3| = 6.
The approach I used is sort the given input and take the middle number as M.
But I was not able to solve all the test cases. I am unable to find any other approach for this question. Where did I go wrong?(Please help me this question has been bugging me from the past 2 days.Thanks)

Can M be any real number or must it be an integer?
If there are no constraints on M your algorithm must work fine.
If M must be an integer then you have to choose M among floor(The Middle Number) and ceiling(The Middle Number).
In which language did you code up the algorithm?

Related

Design an algorithm that minimises the load on the most heavily loaded server

Reading the book of Aziz & Prakash 2021 I am a bit stuck on problem 3.7 and the associated solution for which I am trying to implement.
The problem says :
You have n users with unique hashes h1 through hn and
m servers, numbered 1 to m. User i has Bi bytes to store. You need to
find numbers K1 through Km such that all users with hashes between
Kj and Kj+1 get assigned to server j. Design an algorithm to find the
numbers K 1 through Km that minimizes the load on the most heavily
loaded server.
The solution says:
Let L(a,b) be the maximum load on a server when
users with hash h1 through ha are assigned to servers S1 through Sb in
an optimal way so that the max load is minimised. We observe the
following recurrence:
In other words, we find the right value of x such that if we pack the
first x users in b - 1 servers and the remaining in the last servers the max
load on a given server is minimized.
Using this relationship, we can tabulate the values of L till we get
L(n,m). While computing L(a,b) when the values of L is tabulated
for all lower values of a and b we need to find the right value of x to
minimize the load. As we increase x, L(x,b-1) in the above expression increases the the sum term decreases. We can do binary search for x to find x that minimises their max.
I know that we can probably use some sort of dynamic programming, but how could we possibly implement this idea into a code?
The dynamic programming algorithm is defined fairly well given that formula: Implementing a top-down DP algorithm just needs you to loop from x = 1 to a and record which one minimizes that max(L(x,b-1), sum(B_i)) expression.
There is, however, a simpler (and faster) greedy/binary search algorithm for this problem that you should consider, which goes like this:
Compute prefix sums for B
Find the minimum value of L such that we can partition B into m contiguous subarrays whose maximum sum is equal to L.
We know 1 <= L <= sum(B). So, perform a binary search to find L, with a helper function canSplit(v) that tests whether we can split B into such subarrays of sum <= v.
canSplit(v) works greedily: Remove as many elements from the start of B as possible so that our sum does not exceed v. Repeat this a total of m times; return True if we've used all of B.
You can use the prefix sums to run canSplit in O(m log n) time, with an additional inner binary search.
Given L, use the same strategy as the canSplit function to determine the m-1 partition points; find the m partition boundaries from there.

Calculate the number of trips in graph traversal

Hello Stack Overflow Community,
I'm attempting to solve this problem:
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1040
The problem is to find the best path based on capacity between edges. I get that this can be solved using Dynamic Programming, I'm confused by the example they provide:
According to the problem description, if someone is trying to get 99 people from city 1 to 7, the route should be 1-2-4-7 which I get since the weight of each edge represents the maximum amount of passengers that can go at once. What I don't get is that the description says that it takes at least 5 trips. Where does the 5 come from? 1-2-4-7 is 3 hops, If I take this trip I calculate 4 trips, since 25 is the most limited hop in the route, I would say you need 99/25 or at least 4 trips. Is this a typo, or am I missing something?
Given the first line of the problem statement:
Mr. G. works as a tourist guide.
It is likely that Mr. G must always be present on the bus, thus the equation for the number of trips is:
x = (ceil(x) + number_of_passengers) / best_route
rather than simply:
x = number_of_passengers / best_route
or, for your numbers:
x = (ceil(x) + 99) / 25
Which can be solved with:
x == 4.16 (trips)

How to calculate the expected cost?

I am not good at probability and I know it's not a coding problem directly. But I wish you would help me with this. While I was solving a computation problem I found this difficulty:
Problem definition:
The Little Elephant from the Zoo of Lviv is going to the Birthday
Party of the Big Hippo tomorrow. Now he wants to prepare a gift for
the Big Hippo. He has N balloons, numbered from 1 to N. The i-th
balloon has the color Ci and it costs Pi dollars. The gift for the Big
Hippo will be any subset (chosen randomly, possibly empty) of the
balloons such that the number of different colors in that subset is at
least M. Help Little Elephant to find the expected cost of the gift.
Input
The first line of the input contains a single integer T - the number
of test cases. T test cases follow. The first line of each test case
contains a pair of integers N and M. The next N lines contain N pairs
of integers Ci and Pi, one pair per line.
Output
In T lines print T real numbers - the answers for the corresponding test cases. Your answer will considered correct if it has at most 10^-6 absolute or relative error.
Example
Input:
2
2 2
1 4
2 7
2 1
1 4
2 7
Output:
11.000000000
7.333333333
So, Here I don't understand why the expected cost of the gift for the second case is 7.333333333, because the expected cost equals Summation[xP(x)] and according to this formula it should be 33/2?
Yes, it is a codechef question. But, I am not asking for the solution or the algorithm( because if I take the algo from other than it would not increase my coding potentiality). I just don't understand their example. And hence, I am not being able to start thinking about the algo.
Please help. Thanks in advance!
There are three possible choices, 1, 2, 1+2, with costs 4, 7 and 11. Each is equally likely, so the expected cost is (4 + 7 + 11) / 3 = 22 / 3 = 7.33333.

Geometrical progression with any number row

I can have any number row which consists from 2 to 10 numbers. And from this row, I have to get geometrical progression.
For example:
Given number row: 125 5 625 I have to get answer 5. Row: 128 8 512 I have to get answer 4.
Can you give me a hand? I don't ask for a program, just a hint, I want to understand it by myself and write a code by myself, but damn, I have been thinking the whole day and couldn't figure this out.
Thank you.
DON'T WRITE THE WHOLE PROGRAM!
Guys, you don't get it, I can't just simple make a division. I actually have to get geometrical progression + show all numbers. In 128 8 512 row all numbers would be: 8 32 128 512
Seth's answer is the right one. I'm leaving this answer here to help elaborate on why the answer to 128 8 512 is 4 because people seem to be having trouble with that.
A geometric progression's elements can be written in the form c*b^n where b is the number you're looking for (b is also necessarily greater than 1), c is a constant and n is some arbritrary number.
So the best bet is to start with the smallest number, factorize it and look at all possible solutions to writing it in the c*b^n form, then using that b on the remaining numbers. Return the largest result that works.
So for your examples:
125 5 625
Start with 5. 5 is prime, so it can be written in only one way: 5 = 1*5^1. So your b is 5. You can stop now, assuming you know the row is in fact geometric. If you need to determine whether it's geometric then test that b on the remaining numbers.
128 8 512
8 can be written in more than one way: 8 = 1*8^1, 8 = 2*2^2, 8 = 2*4^1, 8 = 4*2^1. So you have three possible values for b, with a few different options for c. Try the biggest first. 8 doesn't work. Try 4. It works! 128 = 2*4^3 and 512 = 2*4^4. So b is 4 and c is 2.
3 15 375
This one is a bit mean because the first number is prime but isn't b, it's c. So you'll need to make sure that if your first b-candidate doesn't work on the remaining numbers you have to look at the next smallest number and decompose it. So here you'd decompose 15: 15 = 15*?^0 (degenerate case), 15 = 3*5^1, 15 = 5*3^1, 15 = 1*15^1. The answer is 5, and 3 = 3*5^0, so it works out.
Edit: I think this should be correct now.
This algorithm does not rely on factoring, only on the Euclidean Algorithm, and a close variant thereof. This makes it slightly more mathematically sophisticated then a solution that uses factoring, but it will be MUCH faster. If you understand the Euclidean Algorithm and logarithms, the math should not be a problem.
(1) Sort the set of numbers. You have numbers of the form ab^{n1} < .. < ab^{nk}.
Example: (3 * 2, 3*2^5, 3*2^7, 3*2^13)
(2) Form a new list whose nth element of the (n+1)st element of the sorted list divided by the (n)th. You now have b^{n2 - n1}, b^{n3 - n2}, ..., b^{nk - n(k-1)}.
(Continued) Example: (2^4, 2^2, 2^6)
Define d_i = n_(i+1) - n_i (do not program this -- you couldn't even if you wanted to, since the n_i are unknown -- this is just to explain how the program works).
(Continued) Example: d_1 = 4, d_2 = 2, d_3 = 6
Note that in our example problem, we're free to take either (a = 3, b = 2) or (a = 3/2, b = 4). The bottom line is any power of the "real" b that divides all entries in the list from step (2) is a correct answer. It follows that we can raise b to any power that divides all the d_i (in this case any power that divides 4, 2, and 6). The problem is we know neither b nor the d_i. But if we let m = gcd(d_1, ... d_(k-1)), then we CAN find b^m, which is sufficient.
NOTE: Given b^i and b^j, we can find b^gcd(i, j) using:
log(b^i) / log(b^j) = (i log b) / (j log b) = i/j
This permits us to use a modified version of the Euclidean Algorithm to find b^gcd(i, j). The "action" is all in the exponents: addition has been replaced by multiplication, multiplication with exponentiation, and (consequently) quotients with logarithms:
import math
def power_remainder(a, b):
q = int(math.log(a) / math.log(b))
return a / (b ** q)
def power_gcd(a, b):
while b != 1:
a, b = b, power_remainder(a, b)
return a
(3) Since all the elements of the original set differ by powers of r = b^gcd(d_1, ..., d_(k-1)), they are all of the form cr^n, as desired. However, c may not be an integer. Let me know if this is a problem.
The simplest approach would be to factorize the numbers and find the greatest number they have in common. But be careful, factorization has an exponential complexity so it might stop working if you get big numbers in the row.
What you want is to know the Greatest Common Divisor of all numbers in a row.
One method is to check if they all can be divided by the smaller number in the row.
If not, try half the smaller number in the row.
Then keep going down until you find a number that divides them all or your divisor equals 1.
Seth Answer is not correct, applyin that solution does not solves 128 8 2048 row for example (2*4^x), you get:
8 128 2048 =>
16 16 =>
GCD = 16
It is true that the solution is a factor of this result but you will need to factor it and check one by one what is the correct answer, in this case you will need to check the solutions factors in reverse order 16, 8, 4, 2 until you see 4 matches all the conditions.

Number of combinations

Given the following letters in a license plate, how many combinations of them can you create
AAAA1234
Please note that this is not a homework question (I am too old for college :)
I am only trying to understand permutations and combinations. I always get lost when I see questions like this. Do I use n! or nPr or nCr.
Any book on this subject in addition to the logic used to arrive at the answer will also be greatly appreciated.
I have faith in exactly one method to remember such formulas: Rethink through the reasoning to justify it as needed. Then, each time you need the formula, remembering it becomes a mental exercise that makes it easier to remember it the next time. It also allows you to know the math on your own authority, instead of someone else's authority.
If the letters are all different, then there are n choices for the first letter, n-1 choices for the second letter, and so on. That makes n! However, in your problem the letters are not all different. One trick is to tag them to make them different so that you are overcounting, then divide by the amount that you are overcounting. If a of the symbols are A, then you can tag them in a! ways. They are then all different, so that the answer to the modified question is n!. So the answer to the original question is n!/a! (This is assuming that the symbols other than the A are fixed, distinct numbers.)
Another argument is to count the positions for the numbers. There are n positions for the 1, n-1 positions for the 2, etc., so you get n(n-1)...(n-r+1) = n!/a!, where r = n-a.
In fact the answer is the same as the permutation formula nPr. And your arrangements are much the same as partial permutations, which is what the formula is for. But you'll learn it better if you reason through it before looking at the formula.
As for books, I might suggest Brualdi, Introductory Combinatorics.
One strategy that you can use (there will be many) is to get all the permutations possible, then divide out the repeats.
Permutations of 8 elements = 8!
But for each unique arrangement of these, there are a bunch more with the same positions of the A's. So, how many ways can you arrange four A's in one particular set of positions?
Permutations of 4 A's = 4!
So the total unique arrangements should be 8! / 4!
If I'm totally wrong just someone say so and I'll delete this answer...
If you mean 3 letters A-Z and 4 digits 0...9 in that order, then you have
26 letters x
26 letters x
26 letters x
26 letters x
10 digits x
10 digits x
10 digits x
10 digits
= 26^4 * 10^4
= 4569760000
If no leading "0" is allowed, you get a few less.
Edit1: Miscounted the "A"
Edit2: I reread the question - originally I thought it was just four letters at the beginning followed by 4 numbers. If it's just a permutation thing, then the answer is obviously different: 8! permutations at all, but 4! permutations for the A are the same, so 8! / 4! = 1680.
Answer is 8!/4!
Let's try to explain with a simpler question: Combinations of 112 ?
There are 112, 121 and 211. If all digits would be unique, we could just find the answer by 3! But there is a repeating digit. So we should extract repeating digits by 3!/2! = 3
Another example is 1122. We have two repeating digit here. So we should extract twice. 4!/2!.2! = 6
I think this is a good explanation of permutations and combinations:
Easy Permutations and Combinations Better explained.
It goes step by step until you discover how to make the calculations.
No need for permutations, because all letters can be repeated, even the number
since the given example is [AAAA1234],then we have 4-Letters and 4-Digits.
for each letter we have 26 {A-Z} possible combinations
Thats why for 4 letters we will have 26^4
For each Number we have 10 {0-9} possible combinations, except the last digit we 9 possible combinations {case 1}, if it not allowed to be 0 otherwise it is 10 {case 2}
Thats why for 4 letters we will have 9*10^3 {case 1} or 10^4 {case 2}
The total number of combinations is {case 1} 9*(26^4)***(10^3) or {case 2} (26^4)*(10^4)
But if your question about permutations for the set{A,A,A,A,1,2,3,4}, then consider the the equivalent set {1,2,3,4,5,6,7,8} and try avoid the repeated sequence by divide over the permutations of {5,6,7,8} and the answer is 8!/4!=5*6*7*8=1680. the{5,6,7,8} represent {A,A,A,A} See #Tesserex & #erkangur
How many distinct sets of positions can the A's occupy? Given this value, multiply by the number of distinct arrangements of 1234 and you have your answer. You'll need to choose the positions for the A's and then ! will help with the arrangements of 1234.
Consider a simpler example. Let's say you had asked the question:
How many arrangements are there of the symbols: ABCD1234?
Now, since every symbol is distinct, there are 8! ways to arrange them.
Now let's build up to your problem. If we change the letter B to an A, we have AACD1234.
This destroys the uniqueness of exactly half the possible combinations, since any combination where we could have previously switched the A and the B is now non-unique. Therefore, we now have 8!/2 combinations.
Similarly, replacing the C with another A would result in half of the remaining combinations losing their uniqueness, and so on.
So, if only one symbol is duplicated, the generalized formula is (number of symbols total)!/2^(number of duplications)
In your case, the number of possible arrangements is 8!/2^4

Resources