Share via


How to calculate Time Complexity for a given algorithm

One of my friends wanted to know "How to calculate the time complexity of a given algorithm". My obvious answer to him was... "Why do YOU want to calculate it?. There are tools available that do it for you!!" (E.g. Analyze menu in VS Team Suite, NDepend are a few). Well... I don't want to say what his answer was, but he wanted to know :-)

In my personal view, it's a good to know "cool" thing, but not really required for ALL.

With that note, let me write a small program and calculate the time complexity for it.

Here is a sample code to remove an invalid character from an array.

    1: namespace TimeComplexity
    2: {
    3:     class Program
    4:     {
    5:         static void Main(string[] args)
    6:         {
    7:             char[] arr = { 'a', 'b', 'b', 'd', 'e' };
    8:             char invalidChar = 'b';
    9:             int ptr = 0, N = arr.Length;
   10:             for (int i = 0; i < n; i++)
   11:             {
   12:                 if (arr[i] != invalidChar)
   13:                 {
   14:                     arr[ptr] = arr[i];
   15:                     ptr++;
   16:                 }
   17:             }
   18:  
   19:             for (int i = 0; i < ptr; i++)
   20:             {
   21:                 Console.Write(arr[i]);
   22:                 Console.Write(' ');
   23:             }
   24:             Console.ReadLine();
   25:         }
   26:     }
   27: }

Output for the above code will look like

 a d e

Let's look at each loop one at a time. That's the key; you calculate the time complexity for each loop

    1: for (int i = 0; i < N; i++)
    2: {
    3:     if (arr[i] != invalidChar)
    4:     {
    5:         arr[ptr] = arr[i];
    6:         ptr++;
    7:     }
    8: }

The above Code snippet contains a lot of basic operations which will be repeated. (That's why it's called a loop.. duh !!). The basic operations it contains are

 

int i=0; This will be executed only once. The time is actually calculated to i=0 and not the declaration.
i<N; This will be executed N+1 times
i++ ; This will be executed N times
if(arr[i]!=invalidChar)  This will be executed N times
arr[ptr]=arr[i]; This will be executed N times (in worst case scenario)
ptr++; This will be executed N times (in worst case scenario)

 

Note: I considered the worst case scenario and am calculating the Worst Case Time Complexity for the above code

So the number of operations required by this loop are

 

{1+(N+1)+N}+N+N+N = 5N+2

 

The part inside the curly braces is the time consumed by Loop alone (i.e.. for(int i =0;i<N;i++)), it is 2N+2. Keep this mind; it is usually the same (unless you have a non-default FOR loop)

 

Now for the second loop

    1: for (int i = 0; i < ptr; i++)
    2: {
    3:     Console.Write(arr[i]);
    4:     Console.Write(' ');
    5: }

 

Remember, a loop takes 2N+2 iterations. So, here it will take 2ptr+2 operations. Again, considering the worst case scenario ptr will be N so the above expression evaluates to (again) 2N+2. Then there are these additional 2 operations of Console.Write with will be executed N times each (Again, worst case scenario).

So the above code snippet will take

{1+(N+1)+N}+N+N = 4N+2

 

Oh.. I almost forgot the other statements

 

char[] arr = { 'a', 'b', 'b', 'd', 'e' }; This will be executed N times
char invalidChar = 'b'; This will be executed 1 time
int ptr = 0; This will be executed 1 time
N = arr.Length; This will be executed 1 time
Console.ReadLine(); This will be executed 1 time

 

Note: The character array initialization will actually execute N times. This is because you are assigning one character at a time.

So the rest of the code requires

N+4

Adding everything up I get

(N+4)+(5N+2)+(4N+2) = 10N+8

So the asymptotic time complexity for the above code is O(N), which means that the above algorithm is a liner time complexity algorithm.

There you have it, now you know how to calculate the time complexity of a simple program.

Comments

  • Anonymous
    January 25, 2011
    awesome !!!!! thank you for the elaborated explanation :):)

  • Anonymous
    May 18, 2011
    thanks body for give a full explanation to show how we can solve the complexity in very easy way..............

  • Anonymous
    June 11, 2011
    Thanx a lot, tht ws realy helpfull..

  • Anonymous
    July 07, 2011
    can u give me explanation about program which is having labels at different levels

  • Anonymous
    July 19, 2011
    make web comfortable to all so that they can get ans

  • Anonymous
    July 30, 2011
    Really helpful....but can u give an explanation for nested loops where d 2nd loop depends on the value of the outer loop variable?

  • Anonymous
    August 30, 2011
    how the statement char[] arr = { 'a', 'b', 'b', 'd', 'e' }; executed N times? isn't it should be only one time ?

  • Anonymous
    August 31, 2011
    Come on, this is for a particular algorithm...but how can you write a program which calculates the time complexity of a given algoriythm??

  • Anonymous
    September 14, 2011
    can u give answr 4r dis..time complexity.. i=n; while(i>=0) { x=x+2; i=i/2; }

  • Anonymous
    September 14, 2011
    @Sandy since your coder halves the input every time the complexity is O(log n)

  • Anonymous
    September 14, 2011
    @Sandy since your code halves the input every time so the complexity is O(log n)

  • Anonymous
    September 28, 2011
    i=n; while(i>=0) { x=x+2; i=i/2; } logn is the complexity

  • Anonymous
    October 02, 2011
    thanks for giving such array type example.

  • Anonymous
    October 16, 2011
    amazing n easy 2 understand explanation..thnk:)

  • Anonymous
    October 20, 2011
    Whats the complexity for nested 'for' loops??!??!?!?

  • Anonymous
    October 21, 2011
    O(N2) is the complexity for two nested for loops...2 is for square!

  • Anonymous
    October 22, 2011
    what is the time complexity of tower honoe prablem

  • Anonymous
    November 01, 2011
    SDEGFFJDHSGFDHGSFDHSFGDHGSFDHS DJGHKDJFGHDF DFJKHGKJDFGH DKJFGHKFDJG

  • Anonymous
    November 01, 2011
    fdgdfklhgdf fglkhflkg fghfgh fgh gfh fg hf gh gf hfg h gfh gf hf gh fgh fg hfg hgf h fgh fg hfg h gfh fg hfg h gfh fg h fgh gf hgf h fg hf gh fg hfg h fg h fgh fg h gfh fg h fg hfg hfg h fgh gfh gf h fgh fgh fgh fgh fg h fgh gf hf gh fgh hjfgh hjkjljk jkl jkl jl jk ljk l jkl jk l jl jkl jk l jkl jk lkj ljk

  • Anonymous
    November 01, 2011
    1 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 111111111 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1

  • Anonymous
    December 06, 2011
    ase sa as as sas sa sas as sa ss sa as asas dd df ad 1 1 1 1 1 1 1 3e 2 2 2 xdasd sd ds dds ads a 3 w d sd s s wwad a as ds s s s s sadsa sa s ddfr f a a g aa f f gg ag a a a a a F F G SGFD FD FD DF DFSDFAGF F F FD DF DF F FD FD DF FD DF F DF DF DF DF FD FDDF DFDFASS SD SD D D D D F FD DF DF G G H D S S SD S S S DASDS DS DS Z C C C C CX CX C CX C CX CX XC C CX CX X ZZZ ZX X X X XZ VC C CVX Z XCZ XZ XZ XZ XZ X Z XZ X XZ XZ X X XZ C VC CV CV X C Z XC V CX Z Z Z CVCVVZ ZC XZ XC X C XC XZ CZ ZC ZC CZ C C Z

  • Anonymous
    January 16, 2012
    bgdgdg dfg sdfg sdf gsdfgsdfgsdfgsdg dfgsdfg dgsdf gsdf gsdf gsdfg sd gsdfg sdfg sd gsd gsdf gsd g sdfg dg sdfg sd gdg df gsd gsdf gsdf g db b dg sdfg dgwe rtw erter t wert wert wer twer twer t wert wet we te t wert wet wert

  • Anonymous
    April 05, 2012
    Good yara....................................Thank

  • Anonymous
    April 11, 2012
    #include<stdio.h> #include<conio.h> #include<stdlib.h> #include<string.h> struct { char name[20],dtype[10],ename[10],native[10],treatment[10],docname[10],med[10],bgroup[10]; int c,age,date,mon,year,ph,ward,a,m,n,o,q,w,e,total,i,nn,L; }s[10]; main() {int c,i;      printf("***********WELCOME TO HOSPITAL **** n");      A: ;       printf("choose:n");        printf("1.ENTER PATIENT DETAILS.n2.SHOW PATIENT DETAILS.n3.SEARCH PATIENT DETAILSn4.EXITn");        scanf("%d",&c);        switch(c)                 { case 1:                        struct hos { char name[20],dtype[10],ename[10],native[10],treatment[10],docname[10],med[10],bgroup[10]; int c,age,date,mon,year,ph,ward,a,m,n,o,q,w,e,total,i,nn,l; }s[10];                        int q,w,e,m,n,o,total,l;                        printf("ENTER NO PATIENT DETAILS TO BE ENTER:n");                        scanf("%d",&l);                        for(i=1;i<=l;i++)                        {                        printf("nENTER PATIENT NAME:         ");                        scanf("%s",&s[i].name);                         printf("nENTER AGE:                  ");                         scanf("%d",&s[i].age);                          printf("nENTER JOIN DATE ddmmyyyy:  ");                          scanf("%d%d%d",&s[i].date,&s[i].mon,&s[i].year);                           printf("ENTER CONTACT NUMBER:       ");                          scanf("%d",&s[i].ph);                           printf("nENTER BLOOD GROUP:        ");                           scanf("%s",&s[i].bgroup);                           printf("nENTER ESCORT NAME:        ");                           scanf("%s",&s[i].ename);                            printf("nENTER NATIVE PLACE:      ");                            scanf("%s",&s[i].native);                             printf("nENTER DISEASE TYPE:     ");                             scanf("%s",&s[i].dtype);                              printf("nENTER TREATMENT TYPE: ");                              scanf("%s",&s[i].treatment);                               printf("nENTER CONSULTENT DOCTOR: ");                               scanf("%s",&s[i].docname);                                printf("nENTER WARD NUMBER:       ");                                scanf("%s",&s[i].ward);                              printf("nENTER MEDICINES REQUIRED: ");                              scanf("%s",&s[i].med);                              printf("nMEDICINE COST:          ");                              scanf("%d",&s[i].a);                               printf("nTOKEN FEE 100");                              printf("nENTER DISCHARGE  DATE:  ");                              scanf("%d%d%d",&s[i].m,&s[i].n,&s[i].o);                             q=m-s[i].date;w=n-s[i].mon;e=o-s[i].year;                              if(w==0)                              total=q;                              else                              total=w30+q;                              }                              goto A;                              break;                              case 2:                                     B:                                    for(i=1;i<=l;i++)                        {                                   printf("PATIENT DETAILS");                          printf("nPATIENT NAME:      %sn",s[i].name);                          printf("AGE:                 %dn",s[i].age);                          printf("JOIN DATE :          %d-%d-%dn",s[i].date,s[i].mon,s[i].year);                          printf("CONTACT NUMBER:      %dn",s[i].ph);                          printf("BLOOD GROUP:         %s+n",s[i].bgroup);                          printf("NATIVE PLACE:        %sn",s[i].native);                          printf("DISEASE TYPE:        %sn",s[i].dtype);                          printf("TREATMENT TYPE:      %sn",s[i].treatment);                          printf("CONSULTENT DOCTOR:   %sn",s[i].docname);                          printf("WARD NUMBER:         %dn",s[i].ward);                          printf(" MEDICINES REQUIRED: %sn",s[i].med);                            printf("MEDICINES CHARGES: 1900n");                          printf("ACAMIDATION CHARGES:  5000n");                          printf("TOTAL FEE:6900n");                          printf("TOTAL DAYS :%d daysn",s[i].total);                          }                          goto A;                          break;                         case 3:                            //   for(i=0;i<=2;i++)                       // {                               int  war;                               int sa;                               printf("nfor searching choose your option");                               printf("n5.PATIENT NAME n6.WARD NUMBER n7.DATE OF ADMISSIONn8.CONTACT NUMBERn");                               scanf("%d",&sa);                              switch(sa)                              {                               case 5:                                    char p[10];                                    printf("enter patient name");                                    scanf("%s",&p);                                    if((strcmp(s[i].name,p)==0))                                    {                                     printf("PATIENT DETAILS");                          printf("nPATIENT NAME:      %sn",s[i].name);                          printf("AGE:                 %dn",s[i].age);                          printf("JOIN DATE :          %d-%d-%dn",s[i].date,s[i].mon,s[i].year);                          printf("CONTACT NUMBER:      %dn",s[i].ph);                          printf("BLOOD GROUP:         %s+n",s[i].bgroup);                          printf("NATIVE PLACE:        %sn",s[i].native);                          printf("DISEASE TYPE:        %sn",s[i].dtype);                          printf("TREATMENT TYPE:      %sn",s[i].treatment);                          printf("CONSULTENT DOCTOR:   %sn",s[i].docname);                          printf("WARD NUMBER:         %dn",s[i].ward);                          printf(" MEDICINES REQUIRED: %sn",s[i].med);                            printf("MEDICINES CHARGES: 1900n");                          printf("ACAMIDATION CHARGES:  5000n");                          printf("TOTAL FEE:6900n");                          printf("TOTAL DAYS :%d daysn",s[i].total);                          }                                    else                                    printf("data not exist");                                    break;                               case 6:                                    printf("enter ward number");                                    scanf("%d",&war);                                    if(s[i].ward==war)                                     {                                     printf("PATIENT DETAILS");                          printf("nPATIENT NAME:      %sn",s[i].name);                          printf("AGE:                 %dn",s[i].age);                          printf("JOIN DATE :          %d-%d-%dn",s[i].date,s[i].mon,s[i].year);                          printf("CONTACT NUMBER:      %dn",s[i].ph);                          printf("BLOOD GROUP:         %s+n",s[i].bgroup);                          printf("NATIVE PLACE:        %sn",s[i].native);                          printf("DISEASE TYPE:        %sn",s[i].dtype);                          printf("TREATMENT TYPE:      %sn",s[i].treatment);                          printf("CONSULTENT DOCTOR:   %sn",s[i].docname);                          printf("WARD NUMBER:         %dn",s[i].ward);                          printf(" MEDICINES REQUIRED: %sn",s[i].med);                            printf("MEDICINES CHARGES:  1900n");                          printf("ACAMIDATION CHARGES:  5000n");                          printf("TOTAL FEE:6900n");                          printf("TOTAL DAYS :%d daysn",s[i].total);                          }                                   else                                    printf("data not exist");                                    break;                              case 7:                                   int dd,mm,yy;                                   printf("ENTER DATE OF ADMISSION");                                   scanf("%d%d%d",&dd,&mm,&yy);                                   if(dd==s[i].date&&mm==s[i].mon&&yy==s[i].year)                                    {                                     printf("PATIENT DETAILS");                          printf("nPATIENT NAME:      %sn",s[i].name);                          printf("AGE:                 %dn",s[i].age);   &nb

  • Anonymous
    April 11, 2012
    caculate time complexcity for above program

  • Anonymous
    May 23, 2012
    maa chudao bhosadi k.....chpta program to solve hota nahi..ye hoga

  • Anonymous
    June 01, 2012
    how the statement char[] arr = { 'a', 'b', 'b', 'd', 'e' }; executed N times? isn't it should be only one time ?

  • Anonymous
    June 03, 2012
    awesome what a explanation of for loops...................

  • Anonymous
    July 29, 2012
    Wow..wonderful explanation..................

  • Anonymous
    September 11, 2012
    The comment has been removed

  • Anonymous
    September 19, 2012
    dsdssssassssssssmmammnnmnanmnamnmmammakdkanannd

  • Anonymous
    September 24, 2012
    Thanx a lot...this will definitely work for me.....JUG JUG GEO

  • Anonymous
    November 20, 2012
    thankx this is a really simple ans.........:P

  • Anonymous
    February 18, 2013
    Thanks for posting this. It help a lot

  • Anonymous
    April 17, 2013
    The comment has been removed

  • Anonymous
    September 15, 2013
    thnz for ur giving full expntion of time complxty

  • Anonymous
    September 29, 2013
    The most understandable answer i have ever got for complexity.. well done! :)

  • Anonymous
    November 28, 2013
    Is the complexity for the foll algorithm O(n)??? if(n<=2) return 1; else return (A/n^0.5);

  • Anonymous
    November 28, 2013
    @Poppy..your program has a constant time complexity...represented as O(1).

  • Anonymous
    December 10, 2013
    how to decide that the following answer is asymtotic..tnx

  • Anonymous
    May 06, 2014
    Good but simple you should give another complex after this

  • Anonymous
    December 21, 2014
    Anyone can help?

  1. Dim C(n) As Integer
  2. AA(C, 1, n)
  3. For  j  As Integer = 1 To n
  4.      Dim  i  As integer = Sqrt(j)
  5.      print C(i)
  6. Next
  7. Sub AA (C  As Integer(), m  As Integer, n  As Integer)
  8.     Dim  p  As Integer = (n-m+1)/3
  9.     If p > 5  Then
  10.         For  i  As Integer = 1 to 4
  11.              AA (C, m, m+p)
  12.         Next
  13.         For  i  As Integer = m To n
  14.               C(i)=C(i)+1
  15.         Next
  16.     End If
  17. End Sub
  • Anonymous
    January 09, 2015
    @Laa: Time complexity of your program is O(n)... pretty simple.. :)

  • Anonymous
    January 21, 2015
    Thanks..  Today is my exam and this really helped

  • Anonymous
    March 15, 2015
    i want to learn from starting to end any platform where i can learn algo and find conplexity

  • Anonymous
    May 16, 2015
    How did you go from 10N + 8 to O(N)?

  • Anonymous
    August 29, 2015
    a) What is an appropriate Big O expression for            for (int i = 0; i <= n -1; i++)            {                for (int j = i + 1; j <= n - 1; j++)                {                    loop body;                }            }

  • Anonymous
    August 29, 2015
    And: for (int i = 0; i <= n-1; i += 2)     {       for (int j = n-1;  j > 0;  j--)              {              loop body;             } }

  • Anonymous
    September 13, 2015
    I appreciate the explanation. It's very helpful

  • Anonymous
    November 05, 2015
    could you give the time complexity for this int k=0; for(int i=0;i<n;i++)    for(int j=i;j<n;j++)       k++

  • Anonymous
    December 04, 2015
    Good explanation!! Appreciate it!! But What will b the running time fa Sum= a[i];

  • Anonymous
    January 03, 2016
    How did you go from 10N + 8 to O(N)?

  • Anonymous
    January 26, 2016
    thanks for explanig in very easy langauge i really tell u i just understand little bit but i  will make the practices for this session so if u r reading my comment then plz just text me on my no. i would like to help from u my mob no. is 8793666371 thank you so much................

  • Anonymous
    February 19, 2016
    Asalam o alikum. Your defining method was very good. Very good explanation