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 2 years ago.
Improve this question
Given a list of integers.
I wonder if it is possible to calculate bitwise OR on the segment for O(1) per query and O(n) of the premise? (Some prefix sums) (it is Easy to do this for O(log n) per query and O(n log n) of the premise, for example, using the segment tree, but what is faster?)
Yes, it is possible. Just build a prefix-sum-like array for each bit position and fill it with a running total of bits set from the start of your data. The initial value for each counter would be zero: counter[0][b]=0, and the n-th counter would store a number of bits set in data items 0 through n-1.
Then you can test if the bit no b is set anywhere in the given range [m,n] just by testing if b-th counters on both ends of the range differ (counter[n+1][b] not.eq. counter[m][b]).
Finally compose an answer bit by bit from results of all (8, 16, 32...) bit positions.
Be aware this solution requires an additional integer per each bit of original data, which means you need e.g. 32 times more memory if your int is 32 bits wide.
Related
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 6 years ago.
Improve this question
Given a list of 2n-1 numbers: all between 1 to n, all but one occur twice. Determine the number that occurs only once. Multiple ways preferred.
I think the problem is at fault, how can you determine which number without knowing the list of numbers?
[O(1) space, O(n) time]: Just take the XOR of all the numbers. Since all the numbers occur two times except one, XOR of those numbers will be zero and the single occurring number will be the result.
[O(1) space, O(n) time]: As said by user3386109 in comments, we can sum all the given numbers and compare that to the sum of numbers in the range [1, n] which will be n*(n+1) (since all numbers are supposed to occur twice). The difference of the two numbers is the answer.
[O(n) space, O(n) time]: Create an array of size n and keep the count of all the elements in the array at their corresponding positions. At the end, traverse the array, and find the number whose count is only 1.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have an array of elements A1,A2,...,An.
The probability of a user searching for each element are P1,P2,...,Pn.
If the elements are rearranged, will the average case of the algorithm change?
Edit : I have posted the question, which appeared in my exam.
The expected number of comparisons is sum_{i=1...n}(i * p_i).
Re-ordering the elements in descending order reduces the expectation. That's intuitive since by looking at the most probable choices first will reduce, on average, the number of elements looked at before you find a particular choice.
As an example, suppose there's three items k1, k2, k3 with match probabilities 10%, 30% and 60%.
Then in the order k1, k2, k3, the expected number of comparisons is 1*0.1 + 2*0.3 + 3*0.6 = 2.5
With the most likely first: k3, k2, k1, the expected number of comparisons is 1*0.6 + 2*0.3 + 3*0.1 = 1.5
No, because it takes O(1) time to access an element in array and it does not depend on a position of this element in array. So arr[0] and arr[10000] should take the same amount of time.
If you will have something like a linked list or a binary tree, then it makes sense to put elements that are accessed with higher probability closer to the beginning.
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
I'm looking for an efficient way to map a N integers to [1,N].
The N integers are actually entries of a sorted array A with no redundancies, and my goal is to be able to simply access the index of every entry of the array.
Example :
For a given array A of integers, sorted and without redundancies, but with gaps and possibly very large numbers (you could have 1000 integers ranging from 25 to 10^6), I need a way of finding the index of every entry in an efficient way. For example if A[15] = 1546, I need to be able to do index(1546) = 15.
My problem is that I need to do this in Fortran, and as far as I know, there are no real hash table libraries.
I think, you can use a binary search for solve your problem. It is simple for code.
Look this page [Binary search in array issue using Fortran
Using binary search you get the inverse index of the given number.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I am aware that computers use the shift and add method to multiply two numbers.
The bit wise shift multiplies and divides by the powers of two. This operation is faster than a multiply instruction.
Multiplication by a constant and division by a constant can be implemented using a sequence of shifts and adds or subtracts.
((x << 2) + x) << 1 // Here 10*x is computed as (x*2^2 + x)*2
(x << 3) + (x << 1) // Here 10*x is computed as x*2^3 + x*2
But is there a faster algorithm to do so?
Thx for the help :)
Compilers use whatever combination of native assembly instructions is most efficient.
When multiplying by a constant, compilers will choose direct multiplication or shifts based on the number of cycles and binary code size each strategy costs. It's typically a job for low level optimizer, that requires exact knowledge of the processor the code is running on.
You won't beat a compiler at this game.
For really large ns (>1000 digits roughly), there are specialised algorithms, in increasing order of strength they are: Karatsuba, Toom-Cook and finally the monster of 100000 digit multiplications: Schönhage-Strassen.
These perform poorly on smaller numbers due to their overheads but asymptotically they're far better than naive multiplication.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Imagine you are given a matrix of positive integer numbers (maximum 25*15, value of number does not exceed 3000000). When you do column sums and pick the smallest and the largest one, the difference between them must be the smallest possible.
You can swap numbers in every row (permute rows), not in column, how many times you want.
How would you solve this task?
I'm not asking for your code but your ideas.
Thanks in advance
I would make an attempt to solve the problem using Simulated Annealing. Here is a sketch of the plan:
Let the distance to optimize the difference between the max and min column sums.
Set the goal to be 0 (i.e., try to reach as close as possible to a matrix with no difference between sums)
Initialize the problem by calculating the array of sums of all columns to their current value.
Let a neighbor of the current matrix be the matrix that results from swapping two entries in the same row of the matrix.
Represent neighbors by their row index and two swapping column indexes.
When accepting a neighbor, do not compute all sums again. Just adjust the array of sums in the columns that have been swapped and by the difference of the swap (which you can deduce from the swapped row index)
Step 6 is essential for the sake of performance (large matrices).
The bad news is that this problem without the limits is NP-hard, and exact dynamic programming at scale seems out of the question. I think that my first approach would be large-neighborhood local search: repeatedly choose a random submatrix (rows and columns) small enough to be amenable to brute force and choose the optimal permutations while leaving the rest of the matrix undisturbed.