From 8e7cfcdc82066d85c0977f6315e0d11ddab54376 Mon Sep 17 00:00:00 2001 From: Ishita Mukherjee Date: Wed, 7 Aug 2024 21:11:27 +0530 Subject: [PATCH] Binary Search Problem from gfg is addded --- .../Easy problems/BinarySearch.md | 178 ++++++++++++++++++ 1 file changed, 178 insertions(+) create mode 100644 dsa-solutions/gfg-solutions/Easy problems/BinarySearch.md diff --git a/dsa-solutions/gfg-solutions/Easy problems/BinarySearch.md b/dsa-solutions/gfg-solutions/Easy problems/BinarySearch.md new file mode 100644 index 000000000..1f267dc07 --- /dev/null +++ b/dsa-solutions/gfg-solutions/Easy problems/BinarySearch.md @@ -0,0 +1,178 @@ +--- +id: binary-search +title: Binary Search +sidebar_label: Binary-Search +tags: + - Modular Arithmetic + - Algorithms +description: "This tutorial covers the solution to the Count Digits problem from the GeeksforGeeks." +--- +## Problem Description +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. + +## Examples + +**Example 1:** + +``` +Input: k = 4, arr= [1, 2, 3, 4, 5] +Output: 3 +Explanation: 4 appears at index 3. +``` + +**Example 2:** + +``` +Input: k = 445, arr= [11, 22, 33, 44, 55] +Output: -1 +Explanation: 445 is not present. +``` + + +Expected Time Complexity: O(logn) + +Expected Auxiliary Space: O(logn) + +## Constraints + +* `1 ≤ N ≤ 10^5` + +## Problem Explanation + +The task is to traverse the array and search the number. + +## Code Implementation + +### C++ Solution + +```cpp + +#include +#include + +int binarySearch(const std::vector& arr, int k) { + int low = 0; + int high = arr.size() - 1; + while (low <= high) { + int mid = low + (high - low) / 2; + if (arr[mid] == k) { + return mid; + } else if (arr[mid] < k) { + low = mid + 1; + } else { + high = mid - 1; + } + } + return -1; // return -1 if k is not found in the array +} +int main() { + std::vector arr = {1, 2, 3, 4, 5, 6, 7, 8, 9}; + int k = 5; + int position = binarySearch(arr, k); + if (position != -1) { + std::cout << "Element " << k << " is present at position " << position << std::endl; + } else { + std::cout << "Element " << k << " is not present in the array" << std::endl; + } + return 0; +} + + + + +``` + +```java +import java.util.*; + +public class Main { + public static int binarySearch(int[] arr, int k) { + int low = 0; + int high = arr.length - 1; + while (low <= high) { + int mid = low + (high - low) / 2; + if (arr[mid] == k) { + return mid; + } else if (arr[mid] < k) { + low = mid + 1; + } else { + high = mid - 1; + } + } + return -1; // return -1 if k is not found in the array + } + public static void main(String[] args) { + int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9}; + int k = 5; + int position = binarySearch(arr, k); + if (position != -1) { + System.out.println("Element " + k + " is present at position " + position); + } else { + System.out.println("Element " + k + " is not present in the array"); + } + } +} + + +``` + +```python + +def binary_search(arr, k): + low = 0 + high = len(arr) - 1 + while low <= high: + mid = low + (high - low) // 2 + if arr[mid] == k: + return mid + elif arr[mid] < k: + low = mid + 1 + else: + high = mid - 1 + return -1 # return -1 if k is not found in the array + +arr = [1, 2, 3, 4, 5, 6, 7, 8, 9] +k = 5 +position = binary_search(arr, k) +if position != -1: + print("Element {} is present at position {}".format(k, position)) +else: + print("Element {} is not present in the array".format(k)) + + +``` + +```javascript +function binarySearch(arr, k) { + let low = 0; + let high = arr.length - 1; + while (low <= high) { + let mid = Math.floor(low + (high - low) / 2); + if (arr[mid] === k) { + return mid; + } else if (arr[mid] < k) { + low = mid + 1; + } else { + high = mid - 1; + } + } + return -1; // return -1 if k is not found in the array +} + + +``` + +## Solution Logic: +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. + + + + +## Time Complexity + +* The time complexity is $O(log(n))$ where n is the input number. + + +## Space Complexity + +* The auxiliary space complexity is $O(1)$ due to the only extra memory used is for temporary variables while swapping two values in Array. \ No newline at end of file