Dividing rocks into minimum number of groups - algorithm

Given a list of rocks' weights and an integer maxVariance, divide the rocks into groups such that within a group the difference in weight between any two rocks is less than or equal to maxVariance. Find the minimum number of groups to divide all the rocks.
Example:
weights = [5,3,2,4,6]
maxVariance = 2
Output: 2
Explanation: divide into two groups [3,2,4] and [5,6]
My intuition is to sort the weights in ascending order and divide greedily, but I can't prove that this will definitely work. What do you guys think?

Related

Given numbers a1,a2,...,an whose sum is positive. Find the minimal number s.t. the sum of numbers less than or equal to it is positive, in linear time

Problem: Given n different numbers a1,a2,...,an, whose sum is positive. Show how one can find the minimal number such that the sum of numbers less than or equal to it is positive, in time-complexity of O(n).
Note: the numbers aren't necessarily whole and they aren't necessarily sorted as given.
Some explanation of the problem: if the array was sorted, [x,x,x,y,x,...,x,x,x] and y is the first number such that summing all the numbers up-to it will give a positive/zero sum ( and summing less numbers up-to it will give negative sum ), then y will be returned. ( the x here is just a place holder for a number, all numbers in the array are different )
Attempt:
Define the parameters low , high = 0, n which will serve as boundaries for the summation of the elements within them and also as boundaries for choosing the pivot.
Chose a pivot randomly and partition the array ( for example, by Lomuto's partition ), denote this pivot's index as p'. The partitioning will cost O(n). Sum the numbers from low to p' and designate the sum of these numbers as s.
If s<0 define low=p', and repeat the process of choosing a random pivot ( whose index will be denoted as p' ) and parititoning between low and high and then summing the numbers between these two bounderies as s := s + the new summation value.
Else, define high=p' and repeat the process described in the 'If' condition above.
The process will end when low = high.
Besides a few logical gaps in my attempt, it's overall complexity is O(n) on average and not at worst-case.
Do you have any ideas as to how solve the problem in O(n) time?, I thought maybe using a manipulation of 'Median of Medians' algorithm but I have no idea.
Thanks in advance for any help!

Given a collection of integers and threshold value T, divide the collection into as many groups as possible whose sum >= T

Given a collection of integers and threshold value T, divide the collection into as many groups as possible whose sum >= T.
The remaining integers (whose sum < T, so another group cannot be formed) should be left outside of the groups.
Constraints:
length of the list <= 1,000
values and T <= 1,000,000
Is there an algorithm for this problem in polynomial time?
For example given [25,25,25,50,50,50,10] and a threshold T = 70 it should return:
[25,50]
[25,50]
[25,50]
Remaining: [10]
Selecting [25,25,25] as one of the groups would make it possible to only form one more group, [50,50] and the remaining values would be [50,10]. Two groups are not the optimal amount of groups, which is why this solution would be incorrect.
There is no polynomial time algorithm for this since it contains as a special case the np-complete Partition problem.

Constructing dodge ball team

A coach is trying to construct a dodge ball team. Each player is assigned a student ID, and if one player's ID divides by other player's ID, they fight. So the couch wants to make a team so that no one fights in the team. Given the number N (ID is assigned 1 to N), find out the minimum number K
where the couch is unable to make a team in which no one fights.
input (N): 3
output (K): 2
For example, N = 3,
K = 3, {1,2,3} --> Player 1 and 2 has a fight.
K = 2, {2,3} --> No one fights.
input (N): 4
output (K): 2
N = 4,
K = 4, {1,2,3,4} -> more than a pair of players (1,2), (1,3), etc, fights.
K = 3, {1,2,4}, {2,3,4}, {1,3,4} --> players fights in all teams.
K = 2, {2,3} --> No one fights.
So basically, given N, find out the minimum K that a couch can't make any combination of K players so that no one fights. (This is also the maximum number K'+1 where a couch can find at least one team of K' where no one fights.)
A greedy solution I and my friend came up with is try finding the maximum set from the given N. The optimal set must contain the big numbers because since if we start putting small numbers, 2, 3, ..., all the multipliers of these numbers can't be included. So we can start putting N to N/2 in a set as long as the new number is not a divisor of some number already included in the set. We are not entirely sure if this solution would be correct, so we would love to discuss the correctness of our solution and hear other people's ideas.
I was asked to solve this coding problem during an online coding test but couldn't figure it out how to solve.
The way I answered this was return the number of primes in n + 1.
The k is the minimum number that makes it impossible to have pairs that don't fight, as in, at least one pair of a number that divides the other evenly, yes? Based on that, the "safest" bet is prime numbers (since they can't divide each other). Once you add non-prime numbers, you'll be certain to have a "fighting" pair.
Case 1: all prime numbers in n and the number 1 (trivial).
Case 2: all prime numbers in n and any even number in n (the even number can be divided by 2, which eliminates this option).
Case 3: all prime numbers and an odd number (any composite -non prime - odd number can be divided by an odd prime number).
I'm not 100% sure on case 3 regarding mathematical proof, but it seems to be the case.
Disclaimer: I haven't yet received feedback from the interview, this could be totally wrong.

Number of binary search trees possible with nodes equal to or less than n

I know total number of distinct BST's possible with N distinct nodes are given by Catalan formula .
Which is described in this question at SO.
But how can we use it to find all the BSTs less then or equal to n nodes(N distinct nodes)?
for e.g.
For N=3
Possible numbers of BST's are 14.
9 Shown in Picture below for less than 3.
5 for N=3 which can be obtained from Catalan formula.
What is the approach to solve it? I need only algorithmic explanation.
The mathematical formula shall be:
summation ( C(n,i) * Catalan(i) ) {i = 1 to n}
Where C(n,i) = n! / ( i! * (n-i)! )
You can choose i nodes out of a possible n nodes in C(n,i) ways. After choosing those i nodes, form the BSTs in Catalan(i) number of ways.

What is the probability that all priorities are unique for Permute-By-Sorting algorithm?

I hope someone can help me answer the following question. Thanks!
Here is a pseudo code of Permute-By-Sorting algorithm:
Permute-By-Sorting (A)
n = A.length
let P[1..n] be a new array
for i = 1 to n
P[i] = Random (1,n^3)
sort A, using P as sort keys
In the above algorithm, the array P represents the priorities of the elements in array A. Line 4 chooses a random number between 1 and n^3.
The question is what is the probability that all priorities in P are unique? and how do I get the probability?
To reconcile the answers already given: for choice i = 0, ..., n - 1, given that no duplicates have been chosen yet, there are n^3 - i non-duplicate choices of n^3 total for the ith value. Thus the probability is the product for i = 0, ..., n - 1 of (1 - i/n^3).
sdcwc is using a union bound to lowerbound this probability by 1 - O(1/n). This estimate turns out to be basically right. The proof sketch is that (1 - i/n^3) is exp(-i/n^3 + O(i^2/n^6)), so the product is exp(-O(n^2)/n^3 + O(n^-3)), which is greater than or equal to 1 - O(n^2)/n^3 + O(n^-3) = 1 - O(1/n). I'm sure the fine folks on math.SE would be happy to do this derivation "properly" for you.
Others have given you the probability calculation, but I think you may be asking the wrong question.
I assume the reason you're asking about the probability of the priorities being unique, and the reason for choosing n^3 in the first place, is because you're hoping they will be unique, and choosing a large range relative to n seems to be a reasonable way of achieving uniqueness.
It is much easier to ensure that the values are unique. Simply populate the array of priorities with the numbers 1 .. n and then shuffle them with the Fisher-Yates algorithm (aka algorithm P from The Art of Computer Programming, volume 2, Seminumerical Algorithms, by Donald Knuth).
The sort would then be carried out with known unique priority values.
(There are also other ways of going about getting a random permutation. It is possible to generate the nth lexicographic permutation of a sequence using factoradic numbers (or, the factorial number system), and so generate the permutation for a randomly chosen value in [1 .. n!].)
You are choosing n numbers from 1...n^3 and asking what is the probability that they are all unique.
There are (n^3) P n = (n^3)!/(n^3-n)! ways to choose the n numbers uniquely, and (n^3)^n ways to choose the n-numbers total.
So the probability of the numbers being unique is just the first equation divided by the second, which gives
n3!
--------------
(n3-n)! n3n
Let Aij be the event: i-th and j-th elements collide. Obviously P(Aij)=1/n3.
There is at most n2 pairs, therefore probability of at least one collision is at most 1/n.
If you are interested in exact thing, see BlueRaja's answer, but in randomized algorithms it is usually enough to give this type of bound.
So the sort part is irrelevant
Assuming the "Random" is real random, the probability is just
n^3!
----------------
(n^3-n)!n^(3n)

Resources