Skip to content

Commit 7a63139

Browse files
committed
find time complexity of given code
1 parent 392928b commit 7a63139

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package part1_TimeComplexity;
2+
3+
public class testingCode {
4+
5+
private static int searchNumOccurrence(int[] V, int k, int start, int end) {
6+
7+
if (start > end)
8+
return 0;
9+
10+
int mid = (start + end) / 2;
11+
12+
if (V[mid] < k)
13+
return searchNumOccurrence(V, k, mid + 1, end);
14+
15+
if (V[mid] > k)
16+
return searchNumOccurrence(V, k, start, mid - 1);
17+
18+
return searchNumOccurrence(V, k, start, mid - 1) + 1 + searchNumOccurrence(V, k, mid + 1, end);
19+
}
20+
21+
}
22+
23+
/*
24+
* Time complexity: O(N) in worst case
25+
*/

0 commit comments

Comments
Β (0)