Skip to content

Commit e3ed041

Browse files
authored
Merge pull request #3950 from Ishitamukherjee2004/main
Solution of Binary Search from gfg is added
2 parents 8344c14 + e73cf03 commit e3ed041

File tree

1 file changed

+178
-0
lines changed

1 file changed

+178
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
---
2+
id: binary-search
3+
title: Binary Search
4+
sidebar_label: Binary-Search
5+
tags:
6+
- Modular Arithmetic
7+
- Algorithms
8+
description: "This tutorial covers the solution to the Count Digits problem from the GeeksforGeeks."
9+
---
10+
## Problem Description
11+
Given a sorted array `arr` and an integer `k`, find the position(0-based indexing) at which `k` is present in the array using binary search. when divided.
12+
13+
## Examples
14+
15+
**Example 1:**
16+
17+
```
18+
Input: k = 4, arr= [1, 2, 3, 4, 5]
19+
Output: 3
20+
Explanation: 4 appears at index 3.
21+
```
22+
23+
**Example 2:**
24+
25+
```
26+
Input: k = 445, arr= [11, 22, 33, 44, 55]
27+
Output: -1
28+
Explanation: 445 is not present.
29+
```
30+
31+
32+
Expected Time Complexity: O(logn)
33+
34+
Expected Auxiliary Space: O(logn)
35+
36+
## Constraints
37+
38+
* `1 ≤ N ≤ 10^5`
39+
40+
## Problem Explanation
41+
42+
The task is to traverse the array and search the number.
43+
44+
## Code Implementation
45+
46+
### C++ Solution
47+
48+
```cpp
49+
50+
#include <iostream>
51+
#include <vector>
52+
53+
int binarySearch(const std::vector<int>& arr, int k) {
54+
int low = 0;
55+
int high = arr.size() - 1;
56+
while (low <= high) {
57+
int mid = low + (high - low) / 2;
58+
if (arr[mid] == k) {
59+
return mid;
60+
} else if (arr[mid] < k) {
61+
low = mid + 1;
62+
} else {
63+
high = mid - 1;
64+
}
65+
}
66+
return -1; // return -1 if k is not found in the array
67+
}
68+
int main() {
69+
std::vector<int> arr = {1, 2, 3, 4, 5, 6, 7, 8, 9};
70+
int k = 5;
71+
int position = binarySearch(arr, k);
72+
if (position != -1) {
73+
std::cout << "Element " << k << " is present at position " << position << std::endl;
74+
} else {
75+
std::cout << "Element " << k << " is not present in the array" << std::endl;
76+
}
77+
return 0;
78+
}
79+
80+
81+
82+
83+
```
84+
85+
```java
86+
import java.util.*;
87+
88+
public class Main {
89+
public static int binarySearch(int[] arr, int k) {
90+
int low = 0;
91+
int high = arr.length - 1;
92+
while (low <= high) {
93+
int mid = low + (high - low) / 2;
94+
if (arr[mid] == k) {
95+
return mid;
96+
} else if (arr[mid] < k) {
97+
low = mid + 1;
98+
} else {
99+
high = mid - 1;
100+
}
101+
}
102+
return -1; // return -1 if k is not found in the array
103+
}
104+
public static void main(String[] args) {
105+
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9};
106+
int k = 5;
107+
int position = binarySearch(arr, k);
108+
if (position != -1) {
109+
System.out.println("Element " + k + " is present at position " + position);
110+
} else {
111+
System.out.println("Element " + k + " is not present in the array");
112+
}
113+
}
114+
}
115+
116+
117+
```
118+
119+
```python
120+
121+
def binary_search(arr, k):
122+
low = 0
123+
high = len(arr) - 1
124+
while low <= high:
125+
mid = low + (high - low) // 2
126+
if arr[mid] == k:
127+
return mid
128+
elif arr[mid] < k:
129+
low = mid + 1
130+
else:
131+
high = mid - 1
132+
return -1 # return -1 if k is not found in the array
133+
134+
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
135+
k = 5
136+
position = binary_search(arr, k)
137+
if position != -1:
138+
print("Element {} is present at position {}".format(k, position))
139+
else:
140+
print("Element {} is not present in the array".format(k))
141+
142+
143+
```
144+
145+
```javascript
146+
function binarySearch(arr, k) {
147+
let low = 0;
148+
let high = arr.length - 1;
149+
while (low <= high) {
150+
let mid = Math.floor(low + (high - low) / 2);
151+
if (arr[mid] === k) {
152+
return mid;
153+
} else if (arr[mid] < k) {
154+
low = mid + 1;
155+
} else {
156+
high = mid - 1;
157+
}
158+
}
159+
return -1; // return -1 if k is not found in the array
160+
}
161+
162+
163+
```
164+
165+
## Solution Logic:
166+
This solution uses binary search to find the position of k in the sorted array. It starts by considering the middle element of the array. If the middle element is equal to k, it returns the position of the middle element. If the middle element is less than k, it repeats the process for the right half of the array. If the middle element is greater than k, it repeats the process for the left half of the array.
167+
168+
169+
170+
171+
## Time Complexity
172+
173+
* The time complexity is $O(log(n))$ where n is the input number.
174+
175+
176+
## Space Complexity
177+
178+
* The auxiliary space complexity is $O(1)$ due to the only extra memory used is for temporary variables while swapping two values in Array.

0 commit comments

Comments
 (0)