What does cost mean in context of time complexity? [closed] - c++

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
Consider the following pseudo code:
Pseudocode:
list_Sum(A,n){//A->array and n->number of elements in the array
sum =0 // cost=1 no of times=1
for i=0 to n-1 // cost=2 no of times=n+1 (+1 for the end false condition)
sum = sum + A[i] // cost=2 no of times=n
return sum // cost=1 no of times=1
}
What does cost mean in context of time complexity?

Nothing, actually. Theoretically, this would be the number of instructions required on a fictional architecture made up by the author. Practically, it doesn't map to any existing, relevant architecture. No real architecture has exactly these costs.
The only relevant part is the times 1 or times n part, as that is applicable regardless of architecture.
There is also vectorization and super-scalarity, which easily slice the cost of e.g. the loop by factor 15-50 (so imagine that as a "real" cost of 0.02 for the body of the loop), but even then it stays times n.

Related

Suppose you have an array of N elements. You need to find for how many i, Ai + A(i+1) is a square number. Is this question trivial? If so how? [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 just wanted to know if the above question is trivial or not. More importantly, how can you recognize if an algorithm is trivial?
It depends what do you mean by trivial. If you talk about complexity, it is O(n*M(N)) where M(N) is the complexity of the underlying multiplication algorithm with N maximum of the array's values and n is the length of the array.
If you talk about implementation, it is one loop with one check that the sum of the neighbors is a perfect square. If the elements fit into int, double etc. you have sqrt function in the standard library. If your elements are arbitrary length integers or float point numbers, you either need to use an appropriate library or implement the handling of these numbers on your own, which might be not trivial.
This understanding should help you to answer your last question

Solving system of equation modulo 2 [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 3 years ago.
Improve this question
I make a program in C++, that factorizes natural numbers. The only problem is to create a function that does the following:
input: it receives a matrix vector< vector< int> > M.
output: it gives a vector v so that a result of multiplying v and M is a vector that all its coordinates are equal to 0.
Everything must be modulo 2, so coefficients of M and v consists only 0s and 1s
Schould I use a Gauss elimination method? If so, how do this? The problem that implementations I saw on the net don't use vectors and the vectors are necessary in my main program
I would be grateful if someone helped me.
Regards
This is an interesting problem. The steps to be taken can be found in this exercise. link.
The tricky part is to understand that there are always only a finite number of solutions, i.e, only the trivial solution exists or non-trivial solutions exist.
Once you finish the row reduction steps and if non-trivial solutions exist, there is always going to be at least one independent variable (it can take any value 0/1) and the rest of the variables depend on the independent variables.

how can i get periodicity of random function in c++ [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 4 years ago.
Improve this question
I want to calculate pattern at which stage random numbers repeat
string a="abcd";
for(int i=0;i<10000;i++)
{
a=swap(a[i%4],a[rand()]%4)
cout<<a<<"\n";
}
I want to calculate the stage at which, string pattern will same
Most random number generators' sequences do not repeat on successive calls to rand, and can pass tests for randomness.
The actual algorithm used in your C++ implementation can vary, and can be one of the algorithms listed here:
https://en.cppreference.com/w/cpp/numeric/random
Each has its own theory and implementation.
For example, the generation of one of the most common ones the Linear Congruential , is explained here:
https://en.wikipedia.org/wiki/Linear_congruential_generator
It is important to note that most C++ random number generators will generate the same sequence of numbers on different runs of the application, when seeded with the same seed.

Most frequent value in dataset (with variation) [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 7 years ago.
Improve this question
This is a part programming, part statistical math question.
I have a dataset where I want to get the most frequent number (mode), the problem is that I am dealing with values with slight variation.
So normally {1,2,50,50,90} the most frequent number would be 50
But in my case the numbers look like this:
{1,2,49,50,51,90} but the result is still 50
So my question is how can I efficiently calculate this number and is there a statistical term for this number?
Some pseudo code:
Float items.val[] = {1,2,49,50,51,90};
Float threshold = 4;
For (item in items) {
For (subitem in items){
Float dist=Distance(time,subitem)
If (dist < threshold){
item.dist += dist
}
}
}
Output=Sort(item.dist)[0]
There are various ways to go about this.
(1) the most careful, exact way is to assume a probabilistic model for the observed values, and look for the mode (as the expected value or most probable or some other criterion) of the inferred values. I am going to guess this is far too much work in this case, although given unlimited time I would certainly want to approach it that way.
(2) construct a histogram, and look for the bin which has the greatest density (with density = (#items in bin)/(width of bin)). This doesn't necessarily yield a single value.
(3) fit a parametric distribution to the observed values, and report the mode of the fitted distribution.
You might get more traction for this question at stats.stackexchange.com. Good luck and have fun.
EDIT: After looking at your example code, I see it is not too different from (2) above. It seems like a reasonable and workable approach.

A computing trick to calculate for e.g number of boxes required to place N objects given each box can hold M objects? [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
As a part of a recent topcoder SRM problem we had to compute number of buses "B" required to carry "N" people given that each bus has "S" seats.
What is the smartest way to compute this in C++?
The obvious way is to do:
if(N%S==0){B=N/S;}
else{ B=N/S + 1;}
^ ALL VARIABLES ARE INTEGERS, N AND S ASSIGNED APPROPRIATE VALUES
However I cant understand the logic behind the following code which is one particular topcoder user's solution which I was checking out;
B = (N + (S-1))/S;
How does this work?
The code
B = (N + (S-1))/S;
is a common rounding trick. We know that in integer division, the remainder is cut-off, essentially what floor does. In this case, we enforce a ceil operation by adding S-1 first.
This is similiar to the common way of rounding floating point numbers:
n = floor(n + 0.5);