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 2 years ago.
Improve this question
Given array A[] and B[], to find how frequently each possible sums A[i] + B[j] appears we can use FFT
For example
A = [1,2,3], B = [2,4].
The sum 3 can be obtained in 1 way, sum 4 : 1 way, sum 5 : 2 ways, sum 6 : 1 way, sum 7 : 1 way
The way we can do this is to construct two polynomials, P and Q with their power corresponding to the element of the array. And apply the regular FFT.
So is there an efficient way of backtracking the numbers that forms the above. To elaborate, we know 3 can be formed in 1 way. But how do we know which two numbers form it?
One way to do it would be the classic two sum algorithm, i.e given an array and a target sum. Find the pairs that create the sum. Which is O(n)
Given we can have N different targets, the resulting algorithm is O(n^2). But I want to keep it under O(nlogn).
Related
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
You have an Array of size n=10;
arr[]={1,2,3,4,5,6,7,8,9,10}
Operation 1:- Delete every odd index
:- 1,3,5,7,9
Now arr[]={2,4,6,8,10}
Repeat Operation 1 Unless size of array become one
in this case Answer is :-8
Is there any formula threw which i can directly fine out this.
1) Assuming indices are starting from 1.
then,
Mathematical interpretation:
Index of the element that will stay up to the end = 2^(⌊log2 n⌋)
where ⌊n⌋ is: floor of log of n base 2.
And, n is the size of the array.
Index of the element at the end will always be the highest 2^k <= n possible, where k is a positive integer.
For example: If n=20 then the index of the element which will make up to the last will be 16.
that is, 2^4
If n=40, the answer will be 32 ==> 2^5
2) If Indices starts from zero, then the 0th element will stay until the end.
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
How many unique arrays of m elements exist such that they contain numbers in the range [1,n] and there exists atleast one subsequence {1,2,3,4....n}?
Constraints: m > n
I thought of combinations approach. But there will be repetitions.
In my approach, I first lay out all the numbers from 1 to n.
For example, if m=n+1, answer is n^2. (n spots available, each number in range [1,n])
Now, I think there might be a DP relation for further calculation, but I am not being able to figure it out.
Here's an example for n=3 and m=5. The green squares are the subsequence. The subsequence consists of the first 1 in the array, the first 2 that's after the first 1, etc. Squares that aren't part of the subsequence can either take n values if they are after the end of the subsequence, or n-1 values otherwise.
So the answer to this example is 1*9 + 3*6 + 6*4 = 51, which is easily verified by brute force. The coefficients 1,3,6 appear to be related to Pascal's triangle. The rest is left to the reader.
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
Wondering if anyone have worked on similar problem before? And my question is, why {8, 6} are peaks? I think 8 is peak, but since 6 is smaller than 8, it should not be a peak? Thanks.
In an array of integers, a "peak" is an element which is greater than or equal to the adjacent integers and a "valley" us an element which is less than or equal to the adjacent integers. For example, in the array {5,8,6,2,3,4,6} {8,6} are peaks and {5,2} are valleys. Given an array of integers, sort the array into an alternating sequence of peaks and valleys.
Example,
Input: {5,3,1,2,3}
Output: {5,1,3,2,3}
thanks in advance,
Lin
The 6 referred to is the 2nd 6 at the end of the sequence. This fits the description well (if not made very clear) and is backed up by 5 being a valley.
An alternating sequence of peaks and valleys is a sequence such that either all of the elements in odd-numbered positions are peaks and those in even-numbered positions are valleys, or vice-versa. The output sequence in the example demonstrates the elements of the input sequence sorted in such a way.
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
hi to all
I want to know how to find the solution for this problem. consider in a Jar there are 10 papers numbered 1 to 10. we have to take 2 papers from that. what is the probability that 2 numbers are consecutive numbers.
It's a probability to choose one of 9 consecutive pairs from C(10, 2) possible pairs. Thus its 9 / (10*9 / 2) = 1 /5
The probability of getting 2-8 is 8/10. Once we have a given number in that range, the probability of getting one of the two adjacent numbers is 2/9. 8/10 * 2/9 = 16/90.
The probability of getting either a 1 or a 10 is 2/10. If we have one of those two endpoint numbers, the probability of getting the one adjacent number is 1/9. 2/10 * 1/9 = 2/90.
Adding these together, we have a total probability of 18/90 or 1/5.
Your first choice has ten possible numbers. 8 of those 10 will have two consecutive numbers, while the other two only have 1. Your second choice will be out of nine numbers. So
(8/10 * 2/9) + (2/10 * 1/9) = 9/45 = 1/5