Question

num of times needed to run each line, please explain in detail. for(j=1;j<n;j=j*3) sum = sum...

num of times needed to run each line, please explain in detail.

for(j=1;j<n;j=j*3)

sum = sum + j;

for (i = 0; i <n; i++)

for (j=i; j<n;j++)

sum+=i*j;

0 0
Add a comment Improve this question Transcribed image text
Answer #1

FIRST ONE

for(j=1;j<n;j=j*3) // ceil(log3 n) times

sum = sum + j; // ceil(log3 n) times

Possible numbers of j that can be inside the for loop are
1 3 9 27 81 243 . . .

They are are in powers of three
So, answer is ceil of log3 n

O(ceil(log3 n))

SECOND ONE

for (i = 0; i <n; i++) // n times

for (j=i; j<n;j++) // n(n+1)/2 times

sum+=i*j; // n(n+1)/2 times

First for loop runs for n times [0, 1, 2, 3, ... n-1]
Second for loop runs for

n times, n-1 times, n-2 times, ... 2 times, 1 time
n + (n-1) + (n-2) + ... + 2 + 1 = n(n+1)/2

Time complexity = O(n2)

-- Please up vote or comment if you have any doubts. Happy Learning!

Add a comment
Know the answer?
Add Answer to:
num of times needed to run each line, please explain in detail. for(j=1;j<n;j=j*3) sum = sum...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please explain how these code run for each line #include<stdio.h> #include<string.h> /** * Part A */...

    Please explain how these code run for each line #include<stdio.h> #include<string.h> /** * Part A */ struct myWord{    char Word[21];    int Length; }; int tokenizeLine(char line[], struct myWord wordList[]); void printList(struct myWord wordList[], int size); void sortList(struct myWord wordList[], int size); /** * main function */ int main() {    struct myWord wordList[20];    char line[100];    printf("Enter an English Sentence:\n");    gets(line);    int size = tokenizeLine(line, wordList);    printf("\n");    printf("Unsorted word list.\n");    printList(wordList, size);...

  • Explain the following code, line by line. As well as the is going on over all....

    Explain the following code, line by line. As well as the is going on over all. #include <stdio.h> int main() {   int a[30];   int i,j,lasti;   int num;      lasti=0;   // scanf the number   scanf("%d",&a[0]);   while (1)   {   // sacnf the new number   printf("Enter another Number \n");   scanf("%d",&num);   for(i=0;i<=lasti;i=i+1)   {   printf("%d \n",a[i]);   // we check if the num that we eneterd is greter than i   if(a[i] > num)   {   for(j=lasti; j>= i;j--)   {   a[j+1]=a[j];   }      a[i]=num;   lasti++;   break;   }   }...

  • Explain the code and analyze the performance of algorithm #include<stdio.h> #include<string.h> #define NUM 1...

    Explain the code and analyze the performance of algorithm #include<stdio.h> #include<string.h> #define NUM 100 #define maxint 10000 void dijkstra(int n,int v,int dist[],int prev[],int c[][NUM]) {    int i,j;    bool s[NUM];    for(i=1; i<=n; i++)    {        dist[i] = c[v][i];        s[i] = false;        if (dist[i]>maxint) prev[i] = 0;        else prev[i] = v;    }    dist[v] = 0;    s[v] = true;    for(i=1; i<n; i++)    {        int tmp = maxint;        int u = v;        for(j=1; j<=n; j++)            if(!(s[j]) && (dist[j]<tmp))            {                u = j;                tmp = dist[j];           ...

  • (c) int sum(int n) un { int sum=0; for (int i=0; i<n; i++) for(int j=0; j<i/2;...

    (c) int sum(int n) un { int sum=0; for (int i=0; i<n; i++) for(int j=0; j<i/2; j++) for(int k=0; k<min(j,5); k++) { sum=sum+1; } return sum; }

  • 1.4.6 Give the order of growth (as a function of n) of the running times of...

    1.4.6 Give the order of growth (as a function of n) of the running times of each of the following code fragments: a, int sum=0; for (int k n: k > 0; k /= 2) for (int i 0; ǐ < k; İ++) sum++; b.int sum 0; for (int i = 1; i < n; i *= 2) for (int j = 0; j < i; j++) sum++; int sum = 0; for (int í = 1; i < n;...

  • CONSIDER THE FOLLOWING CODE: double SumString(string x, int n) {       double sum = 0;      ...

    CONSIDER THE FOLLOWING CODE: double SumString(string x, int n) {       double sum = 0;       for (int i = 0; i < n; i += 2)             sum += x.length();       return sum; } ///////////////////////////////////////////////////////// void Question() {       string a = "string";       int num = a.length();       double total = SumString(a, num);       cout << "The total is: " << total << endl; } Select one: a. The total is: 14.0 b. The total is: 20.0...

  • [Recursive Cost] [ALGORITHM] Improving Efficiency PLEASE explain in DETAIL the following question in detail. The algorithm...

    [Recursive Cost] [ALGORITHM] Improving Efficiency PLEASE explain in DETAIL the following question in detail. The algorithm is also given below. Thank You! 1.a) Define recursively the worst case cost Kn of the Knapsack function for n items. Remember that you need to provide both the base case and the recurrence relation. Also do not forget to include the cost of the function Worth in your cost. Justify your answer (i.e. explain what each component of the formula represents). [5points] 1.b)  Use...

  • Prove the below statement for n>=2 and 1 <= j <=n 2^n >= (n(n-1)...(n-j+1))/j! Please explain...

    Prove the below statement for n>=2 and 1 <= j <=n 2^n >= (n(n-1)...(n-j+1))/j! Please explain with a detailed proof, thanks

  • Please DONOT attempt this Big O question if you don't know the exact answer. Algorithms question...

    Please DONOT attempt this Big O question if you don't know the exact answer. Algorithms question (Big O): Please explain me in details the order of growth (as a function of N) of the running times of each of the following code fragments: a)         int sum = 0; for (int n = N; n > 0; n /= 2)    for(int i = 0; i < n; i++)      sum++; b)         int sum = 0; for (int i =...

  • Please try to explain the answers as well. It will be highly appreciated. Output of the...

    Please try to explain the answers as well. It will be highly appreciated. Output of the following expression: 5 * 4 % 2 = ? Output of a loop: Example: What will be the output of the Java code (Assume all variables are properly declared.) num = 10; while (num <= 32)                 num = num + 5; System.out.println(num); What will be the output of the Java code (Assume all variables are properly declared.) public class test {   ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT