Having some trouble with solving for runtime - runtime

In Big-Θ notation, analyze the running time of the following three pieces of code/pseudo-code, describing it as a function of the input, n.
1.
void f1(int n)
{
int t = sqrt(n);
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
// do something O(1)
}
n -= t;
}
}
2.
Assume A is an array of size n+1.
void f2(int* A, int n)
{
for(int i=1; i <= n; i++){
for(int k=1; k <= n; k++){
if( A[k] == i){
for(int m=1; m <= n; m=m+m){
// do something that takes O(1) time
// Assume the contents of the A[] array are not changed
}
}
}
}
}
3.
void f3(int* A, int n)
{
if(n <= 1) return;
else {
f3(A, n-2);
// do something that takes O(1) time
f3(A, n-2);
}
}
Any help would be appreciated.

Related

Time complexity of this algorithm? How to analysis?

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.

Why is the time complexity of the following code O(n)?

The Time Complexity of the below code is O(n) , But I'm not convinced , my answer is O(n^2).
public static void fun(int n, int arr[]) {
int i = 0, j = 0;
for (; i < n; i++) {
while (j < n && arr[i] < arr[j])
j++;
}
}
can somebody explain why the time complexity is O(n)?
As the comments state I also think it is O(n).
An algorithm similar to yours with O(n^2) would be:
public static void fun(int n, int arr[]) {
int i = 0;
for (; i < n; i++) {
int j = 0;
while (j < n && arr[i] < arr[j]) {
j++;
}
}
}
That can be simplified to:
public static void fun(int n, int arr[]) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
j++;
}
}
}
The main difference is that the j is instantiated only once in your example.
Maybe this already helps in seeing why that makes your example O(n).

What is the Big O notation of a loop in loop

I have this code and cannot understand the Big-O of this... Thanks
for(i = 0; i<n; i++){
for(j = i; j<n; j++){
if (arr[j]%2!=0){
if (minodd > arr[j]){
}
}
}
}
One of the best ways to approach this problem is to break it down into smaller parts.
First, lets look at your inner loop:
for(j = i; j<n; j++){
if (arr[j]%2!=0){ // O(1)
if (minodd > arr[j]){ // O(1)
}
}
}
The if-statements are O(1) or constant time so we can ignore those and we get just the inner for loop:
for(j = i; j<n; j++){
... // O(1) + O(1)
}
Since the worst case scenario is it loops n times we have O(n) + O(1) + O(1) which can be simplified to O(n) which is called linear time.
Next, lets zoom out and replace the inner loop with our new info:
for(i = 0; i<n; i++){
for(j = i; j<n; j++){
if (arr[j]%2!=0){
if (minodd > arr[j]){
}
}
}
}
becomes:
for(i = 0; i<n; i++){
O(n)
}
Since we know the outside for loop will cycle n times in the worst case, and the inside for loop will cycle n times in the worst case: We get O(n x n) or O(n²) which is also know as polynomial time.
Doesn't this just go on for forever?
You have i < n in your inner loop, so I think it's O(inf).
Now that you've updated the loop, I think #e2-e4 is right:
#include <stdio.h>
int eqn(int n)
{
return n > 0 ? n + eqn(n - 1) : 0;
}
int main(int argc, char **argv)
{
int i, j, n, v, a;
v = 0;
n = 5;
for (i = 0; i < n; i++) {
for (j = i; j < n; j++) {
v++;
}
}
// v = 15 ? 15
printf("v = %d ? %d\n", v, eqn(n));
return 0;
}

How to calculate time complexity for below function?

What is the time complexity for the below function?
void function(int n){
for(int i=0; i<n; i++){. //n times
for(int j=i ; j< i*i; j++){ // n*n times
if(j%i == 0){
for(int k=0; k<j; k++){ // j times = n * n times.
++counter;
}
}
}
}
}
Book says O(n^5). But I couldn't understand why book didn't take j%i into account. The k loop determines based on that. Can you clarify?
Let's consider the part of code in which u have doubt :
for(int j=i ; j< i*i; j++)
{
if(j%i == 0)
{
for(int p=0; p<j; p++){
++counter;
}
}
Let's just analyse this part of code .
For every i, j%i==0 will be true whenever j is a multiple of i, i.e.
i, 2i, 3i, .... i*i // upto i*i only,since j<i*i
Now, for every j, Inside complexity is O(j).
So Complexity for this part of code is :
So, Overall Complexity = O(n*n^3) = O(n^4)
In fact the function runs in time O(n^4), since the if condition happens only once every approximately n loops. See below for precise analysis.
void function (int n) {
for(int i=0; i<n; i++) {
// Runs a total of n times
for(int j=i; j< i*i; j++) {
// Runs a total of (n^3 - n)/3 = O(n^3) times
if(j%i == 0) {
// Runs a total of (n^2 - n)/2 = O(n^2) times
for(int k=0; k<j; k++) {
// Runs a total of (3n^4 - 10n^3 + 9n^2 - 2n)/24 = O(n^4) times
++counter;
}
}
}
}
}

How to calculate number of partitions of n?

How can I calculate number of partitions of n mod 1e9+7, where n<=50000.
See http://oeis.org/A000041 .
Here is the source problem http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1259 (In Chinese)
Simply applying the formula: a(n) = (1/n) * Sum_{k=0..n-1} d(n-k)*a(k) gave me an O(n^2) solution.
typedef long long ll;
ll MOD=1e9+7;
ll qp(ll a,ll b)
{
ll ans=1;
while(b)
{
if(b&1) ans=ans*a%MOD;
a=a*a%MOD;
b>>=1;
}
return ans;
}
ll a[50003],d[50003];
#define S 1000
int main()
{
for(int i=1; i<=S; i++)
{
for(int j=1; j<=S; j++)
{
if(i%j==0) d[i]+=j;
}
d[i]%=MOD;
}
a[0]=1;
for(int i=1; i<=S; i++)
{
ll qwq=0;
for(int j=0; j<i; j++) qwq=qwq+d[i-j]*a[j]%MOD;
qwq%=MOD;
a[i]=qwq*qp(i,MOD-2)%MOD;
}
int n;
cin>>n;
cout<<a[n]<<"\n";
}
I would solve it with a different approach.
Dynamic Programming:
DP[N,K] = number of partitions of N using only numbers 1..K
DP[0,k] = 1
DP[n,0] = 0
DP[n,k] when n<0 = 0
DP[n,k] when n>0 = DP[n-k,k] + DP[n,k-1]
Recursive implementation using memoization:
ll partition(ll n, ll max){
if (max == 0)
return 0;
if (n == 0)
return 1;
if (n < 0)
return 0;
if (memo[n][max] != 0)
return memo[n][max];
else
return (memo[n][max] = (partition(n, max-1) + partition(n-max,max)));
}
Live-Example

Resources