|
| 1 | +--- |
| 2 | +id: Binary-Search |
| 3 | +title: Binary Search (Geeks for Geeks) |
| 4 | +sidebar_label: Binary Search |
| 5 | +tags: |
| 6 | + - Beginner |
| 7 | + - Search Algorithms |
| 8 | + - Geeks for Geeks |
| 9 | + - CPP |
| 10 | + - Python |
| 11 | + - Java |
| 12 | + - JavaScript |
| 13 | + - DSA |
| 14 | +description: "This is a solution to the Binary Search problem on Geeks for Geeks." |
| 15 | +--- |
| 16 | + |
| 17 | +## What is Binary Search? |
| 18 | + |
| 19 | +Binary Search is a highly efficient search algorithm used to find the position of a target element within a sorted list. It works by repeatedly dividing the search interval in half and comparing the target value to the middle element of the interval. |
| 20 | + |
| 21 | +## Algorithm for Binary Search |
| 22 | + |
| 23 | +1. Start with the left pointer at the beginning of the list and the right pointer at the end. |
| 24 | +2. Calculate the middle index of the current search interval. |
| 25 | +3. Compare the target value with the middle element: |
| 26 | + - If the target value equals the middle element, return the middle index. |
| 27 | + - If the target value is less than the middle element, move the right pointer to $middle - 1$. |
| 28 | + - If the target value is greater than the middle element, move the left pointer to $middle + 1$. |
| 29 | +4. Repeat steps 2-3 until the left pointer exceeds the right pointer. |
| 30 | +5. If the target value is not found, return -1. |
| 31 | + |
| 32 | +## How does Binary Search work? |
| 33 | + |
| 34 | +- It starts by comparing the target value to the middle element of the list. |
| 35 | +- If the target value matches the middle element, the search is complete. |
| 36 | +- If the target value is less than the middle element, the search continues in the left half of the list. |
| 37 | +- If the target value is greater than the middle element, the search continues in the right half of the list. |
| 38 | +- This process continues until the target value is found or the search interval is empty. |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | +## Problem Description |
| 43 | + |
| 44 | +Given a sorted list and a target element, implement the Binary Search algorithm to find the index of the target element in the list. If the element is not present, return -1. |
| 45 | + |
| 46 | +## Examples |
| 47 | + |
| 48 | +**Example 1:** |
| 49 | +``` |
| 50 | +Input: |
| 51 | +list = [1, 3, 5, 7, 9] |
| 52 | +target = 5 |
| 53 | +Output: 2 |
| 54 | +``` |
| 55 | + |
| 56 | +**Example 2:** |
| 57 | +``` |
| 58 | +Input: |
| 59 | +list = [2, 4, 6, 8, 10] |
| 60 | +target = 7 |
| 61 | +Output: -1 |
| 62 | +``` |
| 63 | + |
| 64 | +## Your Task: |
| 65 | + |
| 66 | +You dont need to read input or print anything. Complete the function binarysearch() which takes arr[], N and K as input parameters and returns the index of K in the array. If K is not present in the array, return -1. |
| 67 | + |
| 68 | + |
| 69 | +Expected Time Complexity: $O(LogN)$ |
| 70 | +Expected Auxiliary Space: $O(LogN)$ if solving recursively and O(1) otherwise. |
| 71 | + |
| 72 | +## Constraints |
| 73 | + |
| 74 | +- $1 <= N <= 10^5$ |
| 75 | +- $1 <= arr[i] <= 10^6$ |
| 76 | +- $1 <= K <= 10^6$ |
| 77 | + |
| 78 | +## Implementation |
| 79 | + |
| 80 | +<Tabs> |
| 81 | + <TabItem value="Python" label="Python" default> |
| 82 | + <SolutionAuthor name="@ngmuraqrdd"/> |
| 83 | + ```python |
| 84 | + def binary_search(lst, target): |
| 85 | + left, right = 0, len(lst) - 1 |
| 86 | + while left <= right: |
| 87 | + mid = left + (right - left) // 2 |
| 88 | + if lst[mid] == target: |
| 89 | + return mid |
| 90 | + elif lst[mid] < target: |
| 91 | + left = mid + 1 |
| 92 | + else: |
| 93 | + right = mid - 1 |
| 94 | + return -1 |
| 95 | + ``` |
| 96 | + </TabItem> |
| 97 | + |
| 98 | + <TabItem value="C++" label="C++"> |
| 99 | + <SolutionAuthor name="@ngmuraqrdd"/> |
| 100 | + ```cpp |
| 101 | + #include <iostream> |
| 102 | + #include <vector> |
| 103 | + |
| 104 | + int binary_search(const std::vector<int>& lst, int target) { |
| 105 | + int left = 0, right = lst.size() - 1; |
| 106 | + while (left <= right) { |
| 107 | + int mid = left + (right - left) / 2; |
| 108 | + if (lst[mid] == target) { |
| 109 | + return mid; |
| 110 | + } else if (lst[mid] < target) { |
| 111 | + left = mid + 1; |
| 112 | + } else { |
| 113 | + right = mid - 1; |
| 114 | + } |
| 115 | + } |
| 116 | + return -1; |
| 117 | + } |
| 118 | + |
| 119 | + int main() { |
| 120 | + std::vector<int> lst = {1, 3, 5, 7, 9}; |
| 121 | + int target = 5; |
| 122 | + std::cout << "Index: " << binary_search(lst, target) << std::endl; |
| 123 | + return 0; |
| 124 | + } |
| 125 | + ``` |
| 126 | + </TabItem> |
| 127 | +
|
| 128 | + <TabItem value="Java" label="Java"> |
| 129 | + <SolutionAuthor name="@ngmuraqrdd"/> |
| 130 | + ```java |
| 131 | + public class BinarySearch { |
| 132 | + public static int binarySearch(int[] lst, int target) { |
| 133 | + int left = 0, right = lst.length - 1; |
| 134 | + while (left <= right) { |
| 135 | + int mid = left + (right - left) / 2; |
| 136 | + if (lst[mid] == target) { |
| 137 | + return mid; |
| 138 | + } else if (lst[mid] < target) { |
| 139 | + left = mid + 1; |
| 140 | + } else { |
| 141 | + right = mid - 1; |
| 142 | + } |
| 143 | + } |
| 144 | + return -1; |
| 145 | + } |
| 146 | +
|
| 147 | + public static void main(String[] args) { |
| 148 | + int[] lst = {1, 3, 5, 7, 9}; |
| 149 | + int target = 5; |
| 150 | + System.out.println("Index: " + binarySearch(lst, target)); |
| 151 | + } |
| 152 | + } |
| 153 | + ``` |
| 154 | + </TabItem> |
| 155 | + |
| 156 | + <TabItem value="JavaScript" label="JavaScript"> |
| 157 | + <SolutionAuthor name="@ngmuraqrdd"/> |
| 158 | + ```javascript |
| 159 | + function binarySearch(lst, target) { |
| 160 | + let left = 0, right = lst.length - 1; |
| 161 | + while (left <= right) { |
| 162 | + const mid = left + Math.floor((right - left) / 2); |
| 163 | + if (lst[mid] === target) { |
| 164 | + return mid; |
| 165 | + } else if (lst[mid] < target) { |
| 166 | + left = mid + 1; |
| 167 | + } else { |
| 168 | + right = mid - 1; |
| 169 | + } |
| 170 | + } |
| 171 | + return -1; |
| 172 | + } |
| 173 | + |
| 174 | + const lst = [1, 3, 5, 7, 9]; |
| 175 | + const target = 5; |
| 176 | + console.log("Index:", binarySearch(lst, target)); |
| 177 | + ``` |
| 178 | + </TabItem> |
| 179 | +</Tabs> |
| 180 | + |
| 181 | +## Complexity Analysis |
| 182 | + |
| 183 | +- **Time Complexity**: $O(log n)$, where $n$ is the number of elements in the list. The list is divided in half at each step, leading to logarithmic time complexity. |
| 184 | +- **Space Complexity**: $O(1)$, as no extra space is required apart from the input list. |
| 185 | + |
| 186 | +## Advantages and Disadvantages |
| 187 | + |
| 188 | +**Advantages:** |
| 189 | +- Highly efficient for large sorted lists. |
| 190 | +- Fast search time due to logarithmic time complexity. |
| 191 | + |
| 192 | +**Disadvantages:** |
| 193 | +- Requires the list to be sorted. |
| 194 | +- Less efficient for small lists compared to linear search. |
| 195 | + |
| 196 | +## References |
| 197 | + |
| 198 | +- **GFG Problem:** [GFG Problem](https://www.geeksforgeeks.org/binary-search/) |
| 199 | +- **HackerRank Problem:** [HackerRank](https://www.hackerrank.com/challenges/binary-search/problem) |
| 200 | +- **Author's Geeks for Geeks Profile:** [MuraliDharan](https://www.geeksforgeeks.org/user/ngmuraqrdd/) |
0 commit comments