# 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

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

(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.

1. sourabh sharma says:

awesome !!!!! thank you for the elaborated explanation :):)

2. vineet kishor says:

thanks body for give a full explanation to show how we can solve the complexity in very easy way…………..

3. Ni2l says:

Thanx a lot, tht ws realy helpfull..

4. srija says:

can u give me explanation about program which is having labels at different levels

5. xyz says:

make web comfortable to all so that they can get ans

6. andy says:

Really helpful….but can u give an explanation for nested loops where d 2nd loop depends on the value of the outer loop variable?

7. 007 says:

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

8. opposite says:

Come on, this is for a particular algorithm…but how can you write a program which calculates the time complexity of a given algoriythm??

9. sandy says:

can u give answr 4r dis..time complexity..

i=n;

while(i>=0)

{

x=x+2;

i=i/2;

}

10. raza says:

@Sandy since your coder halves the input every time the complexity is O(log n)

11. raza says:

@Sandy since your code halves the input every time so the complexity is O(log n)

12. Atul Rai says:

i=n;

while(i>=0)

{

x=x+2;

i=i/2;

}

logn is the complexity

13. priyanka says:

thanks for giving such array type example.

14. Tejashree says:

amazing n easy 2 understand explanation..thnk:)

15. Rakesh Menon says:

Whats the complexity for nested 'for' loops??!??!?!?

16. aixa says:

O(N2) is the complexity for two nested for loops…2 is for square!

17. konda says:

what is the time complexity of tower honoe prablem

18. ABCD says:

SDEGFFJDHSGFDHGSFDHSFGDHGSFDHS

DJGHKDJFGHDF

DFJKHGKJDFGH

DKJFGHKFDJG

19. abcdefgh says:

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

20. g says:

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

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

ase

sa

as

as

sas

sa

sas

as

sa

ss

sa

as

asas

dd

df

1

1

1

1

1

1

1

3e 2

2

2

xdasd

sd

ds

dds

a

3 w

d

sd s

s

a

as

ds

s

s

s

s

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

22. fools says:

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

23. GOOOOOOOOOOD says:

Good yara………………………………Thank

24. laa says:

#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=w*30+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("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;

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);

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 8:

int mo;

printf("ENTER MOBILE NUMBER");

scanf("%d",&mo);

if(s[i].ph==mo)

{

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;

getch();

}}}

25. laa says:

caculate time complexcity for above program

26. gaandu says:

maa chudao bhosadi k…..chpta program to solve hota nahi..ye hoga

27. kau says:

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

28. sumit says:

awesome what a explanation of for loops……………….

29. Ali says:

Wow..wonderful explanation………………

30. Vimal says:

Now that you have given the time complexity of "removing an invalid character", can you please show me how the hell the time complexity of a Heap sort is O(n log n)?

31. deeeppak says:

dsdssssassssssssmmammnnmnanmnamnmmammakdkanannd

Thanx a lot…this will definitely work for me…..JUG JUG GEO

33. sam says:

thankx this is a really simple ans………:P

34. Student says:

Thanks for posting this. It help a lot

35. nav says:

what is the complexity of my code.?????

try{

test.setInputFile("C:/Users/PRIYA/Documents/NetBeansProjects/JavaApplication2/src/dbscan/Input.xls");

System.out.println(">>>1");

if(corrdinatMap.containsKey("xCordinat"))

xAxis=(ArrayList)corrdinatMap.get("xCordinat");

if(corrdinatMap.containsKey("yCordinat"))

yAxis=(ArrayList)corrdinatMap.get("yCordinat");

System.out.println(">>>2");

xX = (Object[]) xAxis.toArray();

yY = (Object[]) yAxis.toArray();

System.out.println(">>>3");

if(xX.length > yY.length)

loopCount=xX.length;

else

loopCount=yY.length;

Class.forName("oracle.jdbc.driver.OracleDriver");

user="system";

URL="jdbc:oracle:thin:@localhost:1521:XE";

}

catch(Exception e){System.out.println("Exception>>1>>>"+e);}

for(int loop=0;loop< loopCount;loop++)

{

try{

tfx.setText(xX[loop].toString());

tfy.setText(yY[loop].toString());

x1 = Integer.parseInt(tfx.getText());

y1 = Integer.parseInt(tfy.getText());

36. moh kalid aayar noole says:

thnz for ur giving full expntion of time complxty

37. Aysel says:

The most understandable answer i have ever got for complexity.. well done! ðŸ™‚

38. poppy says:

Is the complexity for the foll algorithm O(n)???

if(n<=2)

return 1;

else

return (A/n^0.5);

39. Pro says:

@Poppy..your program has a constant time complexity…represented as O(1).

40. msj says:

how to decide that the following answer is asymtotic..tnx

41. Mohd haseen says:

Good but simple you should give another complex after this

42. Marry says:

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

43. Shashikant Mitkari says:

@Laa:

Time complexity of your program is O(n)…

pretty simple.. ðŸ™‚

44. Blu says:

Thanks..  Today is my exam and this really helped

45. Rana Nayab says:

i want to learn from starting to end any platform where i can learn algo and find conplexity

46. Civilized Gamer says:

How did you go from 10N + 8 to O(N)?

47. Phezi says:

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;

}

}

48. Phezi says:

And:

for (int i = 0; i <= n-1; i += 2)

{

for (int j = n-1;  j > 0;  j–)

{

loop body;

}

}

49. Dee says:

I appreciate the explanation. It's very helpful

50. handa says:

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++

51. Raj says:

Good explanation!! Appreciate it!! But

What will b the running time fa

Sum= a[i];

52. Softek Information Services says:

How did you go from 10N + 8 to O(N)?

53. pruthviraj says:

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…………….

54. Shahbaz Hassan says:

Asalam o alikum. Your defining method was very good. Very good explanation