Time complexity of this algorithm? How to analysis?
int fun(int n)
{
int i = 0, j = 0, m = 0;
for (i = n; i > 0; i /= 2)
{
for (j = 0; j < i; j++)
{
m += 1;
}
}
return m;
}
Your running time is Sum_i (n/2^{i}), for i in {0 to log(n)}. n is the leading term, thus O(n). The sum will not exceed 2n.
Related
Can anyone help me to make loop invarients of Eratosthenes Algorithm please?
Here is the peace of code:
algorithm Sieve of Eratosthenes is
input: an integer n > 1.
output: all prime numbers from 2 through n.
let A be an array of Boolean values, indexed by integers 2 to n,
initially all set to true.
for i = 2, 3, 4, ..., not exceeding √n do
if A[i] is true
for j = i2, i2+i, i2+2i, i2+3i, ..., not exceeding n do
A[j] := false
return all i such that A[i] is true.
After the ith iteration, A[x] = false for all x <= n where x is a multiple of any integer j such that 2 <= j <= i.
You can do as
vector<int> getPrimes(int n) {
vector<bool> A(n + 1, true);
for (int i = 2; i * i <= n; i++) {
for (int j = i * i; j <= n; j += i) {
A[j] = false;
}
}
vector<int> primes;
for (int i = 2; i <= n; i++) {
if (A[i]) {
primes.push_back(i);
}
}
return primes;
}
There are n numbers, ranging 1-100. n ranges in 1-1000.
Another number k. Its bounds are 1 <= k <= 10^6
How to check if its possible to divide the given n numbers in two sets such that the sum of both group numbers is <=k.
I am looking for a high level implementation approach or an algorithm which will return true if the division is possible.
A DP based solution with a complexity of O(n*sum)
for (int i = 0; i < n; i++) {
sum += a[i];
}
int[][] dp = new int[n + 1][sum + 1];
for (int i = 0; i <= n; i++) {
dp[i][0] = 1;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j <= sum; j++) {
dp[i + 1][j] = dp[i][j];
if (a[i] <= j) {
dp[i + 1][j] |= dp[i][j - a[i]];
}
}
}
for (int i = sum / 2; i >= 0; i--) {
if (dp[n][i] == 1) {
int x = sum - i;
if (x > k || i > k) {
System.out.println("NO");
} else {
System.out.println("YES");
}
break;
}
}
I have an assignment I am not sure with; I have to calculate the time complexity of the following code:
int a[][] = new int[m][n]; //O(1)
int w = 0; //O(1)
for (int i = 0; i < m; i++) //O(n)
for (int j = 0; j <n; j++) //O(n)
if (a[i] [j] % 2 == 0) //O(logn)
w++; //O(1)
So from my O estimations I add them up:
O(1) + O(1) + O(n) * ( O(n) * ( O(logn) + O(1) / 2 ) )
O(1) + O(1) + O(n) * ( O(nlogn) + O(n) / 2 )
O(1) + O(1) + (O(n2logn) + O(n2) / 2)
=O(n2logn)
I'm not sure if my train of thought is correct, could somebody help?
for (int i = 0; i < m; i++) //O(m)
for (int j = 0; j <n; j++) //O(n)
if (a[i] [j] % 2 == 0) //O(1)
w++; //O(1)
So the total complexity in terms of big-o is:
O(m)*(O(n) + O(1) + O(1)) = O(m)*O(n) = O(m*n).
for (int i = 0; i < m; i++) //O(m)
{
for (int j = 0; j <n; j++) //O(n)
{
// your code
}
}
So the i loop will go on m times, and for the j loop would run n times.
So in total the code will go on m*n times which would be its time complexity: O(m.n)
The final complexity is O(n^2)
Your logic is close except...
int a[][] = new int[m][n]; //O(1)
int w = 0; //O(1)
for (int i = 0; i < m; i++) //O(n)
for (int j = 0; j <n; j++) //O(n)
if (a[i] [j] % 2 == 0) //O(1)
w++; //O(1)
Your if statement embedded in your second for loop is simply referencing an element in an array and doing a basic comparison. This is of time complexity O(1). Also, typically you would not consider initializing variables in a time complexity problem.
How to find the time complexity {Big-Oh} of the following function?
function(int n) {
for (int i = 0; i < n; i++){
for (int j = i; j < i*i; j++){
if (j%i == 0){
for(int k = 0; k < j; k++){
printf(" * ");
}
}
}
}
}
Answer for this is O(n^5) but I don't know how to find that.
I think that complexity is O(n^5).
function(int n) {
for (int i = 0; i < n; i++){ // N, max value of i is n
for (int j = i; j < i*i; j++){ // N^2, max value of j is n*n
if (j%i == 0){
for(int k = 0; k < j; k++){ // N^2, max value of k is n*n
printf(" * ");
}
}
}
}
}
The first loop is N. Second loop the max value of j is i*i => n*n so it is n^2. Third loop, the max value of k is j, which is n*n so n^2. So n * n^2 * n^2 is n^5
The task is to analyze the following algorithm and calculate its time complexity.
I solved it as taking nested loops are 3 so O(n^3).
How do I solve this problem?
MSS (A[], N) //Where N is size of array A[]
{
int temp = 0, MS = 0;
For (int i = 0; i < N; i++)
{
for(int j = i; j < N; j++)
{
temp = 0;
for(int k = i; k <= j; k++)
temp = temp + A[k];
if(temp > MS)
MS = temp;
}
}
return(MS);
}
Well, you can proceed formally as such: