Skip to content

Commit b5e3b4e

Browse files
committed
Complete TC & SC Class
1 parent 16ae324 commit b5e3b4e

1 file changed

Lines changed: 35 additions & 3 deletions

File tree

17-Time_&_Space_Complexity/TC_SC.java

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,42 @@ public static void main(String[] args) {
9696
// SC = O(n)
9797

9898
// Power function 1 Analysis --------------------------------
99-
//
99+
// powerFunction(int a, int n){
100+
// if(n==0){
101+
// return 1;
102+
// }
103+
// return a * powerFunction(a,n-1);
104+
// }
105+
// TC = O(n) -> Work done = number of calls * time in each call => n * k => n
106+
// SC = O(n) -> Max depth(stack calls) * memory in each call => n * 1 => n
107+
100108
// Power function 2 Analysis --------------------------------
101-
//
109+
// powerFunction2(int a, int n){
110+
// if(n==0){
111+
// return 1;
112+
// }
113+
// int halfPowerSq = powerFunction2(a,n/2) * powerFunction2(a,n/2);
114+
// if(n%2!= 0){
115+
// return a * halfPowerSq;
116+
// }
117+
// return halfPowerSq;
118+
// }
119+
// TC = O(n) -> two times recursion call inside a function
120+
// SC = O(n)
121+
102122
// Power function 3 Analysis --------------------------------
103-
//
123+
// powerFunction3(int a, int n){
124+
// if(n==0){
125+
// return 1;
126+
// }
127+
// int halfPower = powerFunction3(a,n/2);
128+
// int halfPowerSq = halfPower * halfPower;
129+
// if(n%2!= 0){
130+
// return a * halfPowerSq;
131+
// }
132+
// return halfPowerSq;
133+
// }
134+
// TC = O(logn) -> only one time recursion call inside a function
135+
// SC = O(logn) -> total stack calls * memory in each call => logn * k => logn
104136
}
105137
}

0 commit comments

Comments
 (0)