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.
Related
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.
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 4 years ago.
Improve this question
Often, I come across the following terminology in coding interviews.
Given an array or string, find the
sub-array
sub-sequence
sub-string
What difference they have?
For example, I see an integer array can be split into
n*(n+1)/2
sub arrays. Do they become subsets as well? Should sub-arrays are contiguous?
For calculating the sub-sequences of a string, why to use
2^str_length - 1
After searching online, I ended up with this link
https://www.geeksforgeeks.org/subarraysubstring-vs-subsequence-and-programs-to-generate-them/
But I still feel ambiguous as what is the universal term for calling a part of an array/string? and how to compute them?
In general, arrays and strings are both sub-sequences. The "sequence" part indicates that the order of elements is important somehow. "substring" is usually contiguous; "sub-array" and "sub-sequence" are unclear. If you're in a job interview and not certain of the interpretation, your first job is to ask. Sometimes, part of the job interview is making sure you can spot and resolve ambiguities.
UPDATE after question update
I find the referenced page quite clear.
First, note that string and array are both specific types of a sequence.
subsequence is the generic term: elements of the original sequence
appearing in the same order as in the original, but not necessarily contiguous. For instance, given the sequence "abcdefg", we have sub-sequences "a", "ag", bce", etc.
Elements repeated or otherwise not in the original ordering would include "ga", "bb", bcfe", etc. None of these is a sub-sequence.
"Subset" is a separate type. In a set, repeated elements do not exist, and ordering does not matter.
Does that clear up your problems?
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 6 years ago.
Improve this question
I need simple hash function that takes two parameters, with the following parameters:
a,b,c,d are different strings (approximately 30 characters)
f(a,b) = f(b,a)
f(a,c) ≠ f(a,d)
f(c,b) ≠ f(d,b)
Sort and concatenate the two parameters (to ensure that f(a,b) equals f(b,a). Since there are only two items to sort the result will be either ab or ba.
If the strings have the property that ab may equal cd (for example strong + hearted and strongheart + ed) you may want to "salt" the string by prepending it with the length of the first string coded in a fixed number of bytes.
Then apply a string hash on the result. There are numerous examples online.
Note that there is no guarantee that two different strings won't result in the same hash value, but a good hash algorithm will reduce the probability.
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 8 years ago.
Improve this question
Call every subunitary ratio with its denominator a power of 2 a perplex.
Number 1 can be written in many ways as a sum of perplexes.
Call every sum of perplexes a zeta.
Two zetas are distinct if and only if one of the zeta has as least one perplex that the other does not have. In the image shown above, the last two zetas are considered to be the same.
Find all the numbers of ways 1 can be written as a zeta with N perplexes. Because this number can be big, calculate it modulo 100003.
Please don't post the code, but rather the algorithm. Be as precise as you can.
This problem was given at a contest and the official solution, written in the Romanian language, has been uploaded at https://www.dropbox.com/s/ulvp9of5b3bfgm0/1112_descr_P2_fractii2.docx?dl=0 , as a docx file. (you can use google translate)
I do not understand what the author of the solution meant to say there.
Well, this reminds me of BFS algorithms(Breadth first search), where you radiate out from a single point to find multiple solutions w/ different permutations.
Here you can use recursion, and set the base case as when N perplexes have been reached in that 1 call stack of the recursive function.
So you can say:
function(int N <-- perplexes, ArrayList<Double> currentNumbers, double dividedNum)
if N == 0, then you're done - enter the currentNumbers array into a hashtable
clone the currentNumbers ArrayList as cloneNumbers
remove dividedNum from cloneNumbers and add 2 dividedNum/2
iterate through index of cloneNumbers
for every number x in cloneNumbers, call function(N--, cloneNumbers, x)
This is a rough, very inefficient but short way to do it. There's obviously a lot of ways you can prune the algorithm(reduce the amount of duplicates going into the hashtable, prevent cloning as much as possible, etc), but because this shows the absolute permutation of every number, and then enters that sequence into a hashtable, the hashtable will use its equals() comparison to see that the sequence already exists(such as your last 2 zetas), and reject the duplicate. That way, you'll be left with the answer you want.
The efficiency of the current algorithm: O(|E|^(N)), where |E| is the absolute number of numbers you can have inside of the array at the end of all insertions, and N is the number of insertions(or as you said, # of perplexes). Obviously this isn't the most optimal speed, but it does definitely work.
Hope this helps!
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 8 years ago.
Improve this question
I need to propose an algorithm for the following: let us assume that we have an array consisting of zeros and ones. The array is filled with zeros from the beginning of the array to the index m, and all remaining indexes are filled with ones. I need to find this index m in O(logm) time. Here is what i thought: I think this is like binary search, first i look at the middle element of the array, if that is zero, then i forget about the left part of the array and do the same for the right part, and continue like this until i encounter a one. If the middle element is one, then i forget about the right part and do the same for left part of the array. Is this a correct O(logm) solution? Thanks
It is not "like" a binary search - it is a binary search. Unfortunately, it is O(logN), not O(logM).
To find the borderline in O(logM), start from the other end: try positions {1, 2, 4, 8, 16, ... 2^i} and so on, until you hit a 1. Then do a binary search on the interval between 2^i and 2^(i+1), where 2^i+1 is the first position where you discovered a 1.
Finding the first 1 takes O(logM), because the index is doubled on each iteration. After that, the binary search takes another O(logM), because the length of the interval 2^i..2^(i+1) is less than M as well.