What is the algorithmic approach, given an array of integers, to remove a minimum number of elements from the array such that the largest and the smallest number does not differ by more than two times?
-
In other words if x is the minimum of the remaining elements in the array and y is the maximum than y<=2x Sample Test Case: Input: {4,5,3,8,3,7} Output: 2 Note: In the above sample you can remove the fourth and the sixth measurement results (values 8 and 7). Then the maximum of the remaining values will be 5, and the minimum one will be 3. Or else, you can remove the third and fifth results (both equal 3). After that the largest remaining result will be 8, and the smallest one will be 4.
-
Answer:
Sort the list.O(n*log(n)) The elements removed will either be smallest or the largest(prefixes or suffixes). If we remove i elements from beginning than the number of element that we need to remove(=j) from end can be found by searching(binary search) for 2*a[i]. Total elements removed = j+i. Find this number for all i and take minimum of this list. This will give the answer.O(n*log(n)) The second step can be reduced to O(n) but overall complexity will remain O(n*log(n)).
Nikhil Vyas at Quora Visit the source
Related Q & A:
- How can I find the minimum number of open rectangles in a grid?Best solution by Stack Overflow
- What is a contact number that I can call Yahoo at?Best solution by Yahoo! Answers
- If you lived alone with a minimum wage then what?Best solution by Yahoo! Answers
- What is the best approach for me to get a job?Best solution by techrepublic.com
- What is a minimum age to be an exchange student?Best solution by ChaCha
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.