What algorithm can be used here [closed] - algorithm

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
This is an interview question, not a homework.
N friends are playing a game. Each of them has a list of numbers in front of himself.
Each of N friends chooses a number from his list and reports it to the game administrator. Then the game administrator sorts the reported numbers and shouts the K-th largest number.
I must the count all possible numbers that the game administrator can shout.
For example, if N = 3 and K = 3, and the lists for the 3 friends are 2 5 3, 8 1 6, 7 4 9. The output is 6, since the possible values are 4 5 6 7 8 9.
Can anybody suggest a decent algorithm for this problem? What I am doing is to create all possible permutations taking one number from each list, then sorting the resultant and printing the kth-largest. But that takes too much time.

To know if a number is present in the result or not, you need to know for each other list if there are numbers above and if there are numbers below. List where there are both numbers above and below are not a problem as you can choose a number in them as it suits you. The problem are lists where there are only numbers above or only numbers below. The first ones must be at most N-K, the second ones must be at most K. If this is not true, your number cannot be picked. If this is true, you can always choose numbers in the lists where there are both number above and below so that your number is picked.
This can be checked in linear time, or even better if your first sort your lists, thus giving an overall complexity of O(n.log(n)) where n is the number of numbers in total. Without sorting you got a n² complexity.
In your example with lists :
{2 5 3}, {8 1 6}, {7 4 9}
say we are looking for the 2-greatest number. For each number we ask if it can be shout by the administator. For each of them we look if in the other list there are both numbers below and numbers above. Let's look further for some of them
For 5 : there is numbers above and below in both other lists. So "5" can be shout by the administrator.
For "2" : there is numbers above and below in the second list so I can freely choose a number above or below in this list. But there are only numbers above in the third list, so the picked number in this list will always be greater. Since i can freely choose a number below in the second list, thus making my "2" the 2nd greatest number.
For "1" : there is only numbers above in the second list, so "1" will always be the smallest element.
For "9" : this is the other way round, it is always the greatest.

take the smallest number from each set. find the K-th -largest of these. This is the smallest number that is in the result. Similarly, find the largest number in the result. Prove that Each number between the two is in the result.

Related

Why binary search algorithm runs in O(log n) time for a sorted array with n elements? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Here is my thought:
You have N elements to begin with.
But you aren't going through each one though?
Say I have
{1, 2, 3, 4, 5, 6, 7, 8, 9} and I want to find 4.
The first step is looking at 5, we see 4 < arr[5]. Then, we have
{1, 2, 3, 4, 5}, the middle is 3, we see 4 > arr[2], thus we are left with {3, 4, 5}.
Now we will get 4.
But that was only 3 steps! I am not understanding why the first search takes N elements, when we are looking at the (N-1)/2th element, which is one step?
EDIT!!!
Here is what I am taught:
search 1: n elements in search space
search 2: n/2 elements in search space
search 3: n/4 elements in search space
...
search i: 1 element in search space.
search i has n/(2^[i-1])elements, thus you solve for i then you get
i = log(n) + 1.
What I don't understand:
You have n elements, I agree, but you aren't searching through all of them, you are only searching 1 element, then why do you count all n?
The main reason why binary search (which requires sorted data in a data structure with O(1) random access reads) is O(log N) is that for any given data set, we start by looking at the middle-most element.
If that is large than the element we're looking for, we know that we can ignore anything from that point to the end. If it is smaller, we can ignore anything from the beginning to that element. This means that for every step, we're effectively cutting the size of the remaining in half.
By cutting the problem set in half at every step, we can (relatively) easily see that to get from N elements to a single element is O(log N) steps.
The reason why we're not terminating earlier in your example is that even though it seems as if we're scanning the first elements, the only things we actually do are "get length of array" and "access the middle-most element" (so, we never know if the element we're looking for is contained in the array).

Big - O notation linear and binary search [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
In terms of Big - O notation the linear search is a x^n, but what is the binary search? I am not 100% that the linear search is correct.
Linear search means that you will have to iterate through the list of elements until you find the element that you were looking for.
For instance, if you have a list with elements [1, 3, 5, 7, 9, 11] and you are looking for 11 you will start by the first element, then the second element, and so on, which in this case will take 6 iterations.
Generally, we could say that in the worst case you will have to traverse the whole list; so it will take n iterations, where n is the number of elements on the list.
So we say that the linear search algorithm is O(n).
In the case of binary search, you start on the middle element of the list:
Case 1: the number we are searching is the same as the number on the middle element: we are done!
Case 2: the number we are searching is smaller: we will only search the elements that precedes the middle element.
Case 3: the number we are searching is bigger: we will only search on the subsequent elements.
In our example, the number we are searching is 11 and the middle element is 5; since 11 > 5, we will only search on the sublist of the elements bigger than 5, namely [7, 9, 11].
Now, we will keep doing the same until we find the element that we are searching, in this case it takes only three iterations to get to the last element.
In general this approach takes log(n) iterations; therefore, the algorithm is O(log(n)).
Note that the latter only works for sorted lists.

How to check if number is summation of powers of 5 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
How to check if a number is a power of 5?
I could think of below algorithm. Is there way to improve it? Any mathematical trick?
First check if last digit is 5.
If last digit is 5; divide it by 5.
If result of division is 1, then number is power of 5.
Else check if division result itself is power of 5 (i.e. go to step 1 with result as number).
You don't need to look at individual digits, you can just do it like this:
n = (int)(log(x) / log(5)); // get n = log5(x), truncated to integer
if (pow(5, n) == x) // test to see whether x == 5^n
// x is a power of 5
LIVE DEMO
There are only few power of five that fit in int/long range, so you just need to generate all of them and check one by one (less than 60 numbers), using a HashSet will have O(1) time complexity
Successive division until you reach the number undivided by 5 and check whether the result is equal to 1, isn't bad solution. It take log_5(n) operations, so it's O(lg n), it's very good time. For 9094947017729282379150390625 it's only 40 operations!
What I would do is first create an array of numbers that are powers of 5. You could use a range, and then say, for each value in the range, take that value to the fifth power, and push the new value into the array.
You would then find if n, the number you are looking for is included in the array.

Pseudocode - Largest to smallest integer [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Write a program that will read in five positive integers (one at a time) and print out the largest and smallest number of the five numbers. The program should read the numbers one at a time.
Mind you this is pseudocode and not to be done in any language.
My question is how would I go about setting this up so that the 5 integers save as values so I can display them.
Don't want the answer, just a start.
How would you solve the same problem if you had to only report the largest number? The pseudocode would be something like the following
consider the first number to be largest
for each of the rest of the number
if it is larger then the current largest
assign to largest
How would you do it if there were two?
consider the first number to be largest
if second number is larger then the largest
consider the second number to be largest, first to be 2nd largest
else
consider the first number to be largest, second to be 2nd largest
for each of the rest of the numbers
if it is larger then the largest
consider current largest to be 2nd largest and this number to be largest
else if it is larger then the 2nd largest
consider it to be 2nd largest
But if there are three or more this can get ugly. How do we keep N largest number? Clearly, we need a list of N sorted number. I will leave it to you how to maintain that list, but here's a pseudocode using that approach
populate the top-list with first N numbers from input, ensure the top-list is sorted
for each of the rest of the numbers
if the number is larger then any number in the top-list
insert it at the right place in top list, pushing out the smallest element of the top list
The question now is: is this better than sorting the list and picking up the top N and bottom N elements?
The answer is that "it depends". Can you figure out some circumstances where one approach is better then the other?
As you read the numbers, keep track of the currently largest and smallest numbers, and update the values as the input is coming in. This has the advantage that it works for even long sequences of numbers. I mean something like this:
min = 0
max = 0
while input:
read number from input
if number < min:
min = number
if number > max:
max = number

Looking for New Ideas on An Old Interview Trick [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
The original question is:
Describe an algorithm to output a die roll(a random number from 1 to 6), given a function that outputs a coin toss(a random number from 1 to 2). Each possible outcome should be equally likely.
The most popular answer to this question is:
Flip the coin three times, and use the three coin flips as the bits of a three-bit number. If the number is in the range 1 to 6, output the number. Otherwise, repeat.
My question is:
Most of the discussions on Stack Overflow come in the above flavour. I've also searched the Internet, finding that there exist many answers of other flavours, which they don't explicitly dig into. Could someone please share one or two different ideas on this problem?
There is a small improvement to the "toss 3 times and discard if 110 or 111" algorithm. Discarding 110 or 111 is wasteful, since you are wasting one perfectly good bit of entropy that you could reuse. After one of these values pops up, you only need to toss twice and get the value of the third toss from the mapping {110->tails, 111->heads}. Both of 110 and 111 are equally probable, so you're not introducing any bias this way.
In pseudocode:
bit0 = toss()
while True:
bit1 = toss()
bit2 = toss()
if bit1,bit2,bit3 give i such that 0<=i<=5 then
return i+1
else
bit0 = bit3 // the reuse happens here
The expected number of tosses here is 1 + 2 * expected_number_of_loop_executions = 1+2*8/6 = 11/3
If you just want other options, not necessarily good ones then how about this:
Flip a coin for each possible output value.
If there is one or more heads then discard all the possible values that got tails.
If you have only one value left then stop. Else goto 1.
I would suspect it is going to have a higher expected number of coin tosses than the method you've described and have no advantages at all really.
In general I assume this is why there is not much on the other possible ways of using random numbers. They are just not as good.
Flip 5 coins. If they're all heads or all tails, your answer is 1. If there's only one head or one tail, continue to the next step. If there's more than one head and more than one tail, repeat this step (including reflipping the coins).
Flip 4 coins. If they're all heads or all tails, your answer is 2. If there's only one head or one tail, continue to the next step. If there's two heads and two tails, repeat this step (including reflipping the coins).
Flip 3 coins. If they're all heads or all tails, your answer is 3. Otherwise, continue to the next step
Flip 2 coins. If they're both heads, your answer is 4. If they're both tails, repeat this step (including reflipping the coins). Otherwise, continue to the next step.
Flip 1 coin. If it's heads, your answer is 5. If it's tails, your answer is 6.

Resources