Assuming we have a list containing integers and we need to find min & max. What is the best way to do it ? I can think of following : If number of reads are much higher than writes; keep the list in sorted manner. Whenever a number is added; add it in sorted manner. Now to get min and
Tag: algorithm
Logarithm Algorithm
I need to evaluate a logarithm of any base, it does not matter, to some precision. Is there an algorithm for this? I program in Java, so I’m fine with Java code. How to find a binary logarithm very fast? (O(1) at best) might be able to answer my question, but I don’t understand it. Can it be clarified? Answer
Mergesort in java
I am new to Java and have tried to implement mergesort in Java. However, even after running the program several times, instead of the desired sorted output, I am getting the same user given input as …
Find all numbers in the String [closed]
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 10 years ago.
Is there a simpler solution for Codingbat fix45?
I am trying to solve this CodingBat problem: (This is a slightly harder version of the fix34 problem.) Return an array that contains exactly the same numbers as the given array, but rearranged so that every 4 is immediately followed by a 5. Do not move the 4’s, but every other number may move. The array contains the same number
Why does QuickSort use O(log(n)) extra space?
I have implemented the below quicksort algorithm. Online I’ve read that it has a space requirement of O(log(n)). Why is this the case? I’m not creating any extra data structures. Is it because my recursion will use some extra space on the stack? If this is the case, is it possible to do it with less memory by not having
What’s the most efficient algorithm to calculate the LCM of a range of numbers?
I looked around and found other questions that had answers but none of them address the scope of this particular question., including this question, and also this one. I have to compute the LCM of large ranges of numbers in an efficient way. I didn’t look too in-depth at those other questions because they don’t deal with ranges of numbers
How to find the index of an element in a TreeSet?
I’m using a TreeSet<Integer> and I’d quite simply like to find the index of a number in the set. Is there a nice way to do this that actually makes use of the O(log(n)) complexity of binary trees? (If not, what should I do, and does anyone know why not? I’m curious why such a class would be included in
Does a hasDuplicate array method without using java.util exit? Is O(n) achievable with it?
I had an interview today which involved this very question and in order to widen my algorithmic knowledge. I am trying to see if there are any better suggestions. I was trying to find duplicates in an …
First occurrence in a binary search
I’m tinkering with some code and I realized something I never knew. A normal binary search will return a random index in a data set for a key that occurs more than once. How can I modify this code below to return the first occurrence? Is this something people do? Answer Having found a matching value, you basically need to