Number of Integers less than x in a subarray [closed] - arrays

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
Given an array A[1..n] and Q queries on that array.
Each query is of the format (x a b), and for every query I need to find out, how many elements in the sub-array (a,b) of main array denoted by A[a..b] are less than x.
How to perform this job efficiently ?
OR If required what kind of data structures to build on top of array, to perform the job efficiently ?
Limits :
n <= 10^6
Q <= 10^4

HINTS
Use a Fenwick tree to store a cumulative histogram of which values you have seen.
Turn your queries (x a b) into a F(x,a)-F(x,b-1) where F(x,a) counts numbers less than x in the range 0..a.
Sort these F(x,a) queries by a.
Then iterate over the array index i, inserting element A[i] into the Fenwick tree, and answering any F(x,a) queries that have a==i.

Related

How to find time complexity of a simultaneous for loop? [closed]

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
I am trying to find the time complexity of simultaneous loops, and I am stuck up on this question.
Find the time complexity of this simultaneous loop
for(int i=1,int j=0; i*i<=n && j<=n ;j=j*2 ,i++);
Can someone explain how to approach these types of questions?
Variable "i" is increased by 1 and stops at sqrt(n). Variable "j" is increased by two multiplied every step, but its initial value is zero, so it can not break the loop. So "i" variable reached to the end when n limits to a big number.
For complexity calculations, we focus the power of n or logarithmic of n values. Scalar values are not important. We discard 1, 2 or 10 steps. So the complexity of your statement is O(sqrt(n)). Initial values do not affect the complexity of statements.

Generation of random numbers but controlling the count of those numbers [closed]

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 5 years ago.
Improve this question
If i want to generate 0,1,2 randomly in a 2D matrix but i want to control the count of 0,1,2. Is this can be done by any inbuilt c function??(I am using C and new to programming.)
Let's suppose that you have a 3x3 matrix and you want three each of 0, 1, 2. The most practical way to do this is to fill in the matrix with the right number of numbers in a non-random way, like
0 0 0
1 1 1
2 2 2
and then use a Fisher-Yates shuffle to put them in a random order. You will have to implement the shuffle yourself, but you can use rand() in the normal way in your implementation.
This works for any situation where a fixed number of fixed values need to be put into random positions within a list.

C How can I calculate an average of a list without loops? [closed]

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
So I created a linked List in C using structs and it stores ints. My mission is to calculate the average of the values in the list without using recursion or loops.
I already have the list's item count I just need the sum.
Any ideas?
Simply have two varibles - count, total; Update them in Add and Delete.
When u want avg, just return total/count.
The list is not length bounded, but i found a solution.
I create a variable in the list's ateuct to save the sum of the list, and i change the sum of the list each time I add or remove a cell. When I want to calculate the sums I'll just divide the sum by the count.
Thank you anyway for your help :)

Calculate elements in a array that are linear [closed]

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 have a large array of a tuple (data, time)
Part of the array contains data that has a linear slope.
Other data in the array is non-linear.
What algorithm can I use to determine
Where does the linear slope start?
Where does the liner slope end?
Seems straightforward:
Sort tuples by time
Let (d1, t1), (d2, t2), (d3, t3), (d4, t4), etc be consecutive tuples
Calculate the gradient (slope) between each tuple: (d2 - d1) / (t2 - t1), (d3 - d2) / (t3 - t2), etc.
If the gradient is the same between multiple consecutive tuples (within a margin of error depending on your data), then those consecutive tuples must be on a linear line.
Look up RANSAC algorithm for linear regression robust to outliers. Using a randomized approach, RANSAC will essentially find you an (approximately) largest set of points that are within a prescribed distance to a common line.

C programming: logarithmic index array [closed]

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 9 years ago.
Improve this question
I'm trying to decimate a given vector with a logarithmic rule instead of a linear one.
e.g.:
The given vector has 100 elements. I want to reduce it to a vector of 10 elements that are the same elements of the starting vector taken with a logarithmic rule on the int interval [0,99].
I hope to be clear enough. Any help is appreciated !
Have a nice day !
In pseudo-code :
int n; // Length of subsampled array
for i:
new_array[i] = array[(int)(log(i)/log(n)*length(array))]
Is that what you are trying to do ?

Resources