From 4108a242e4b240679466a7ecf16897ae8a2f4a61 Mon Sep 17 00:00:00 2001 From: PRASHANTSWAROOP001 Date: Mon, 7 Oct 2024 12:08:33 +0530 Subject: [PATCH 1/3] Adding The Article On Binray Seach Questions --- docs/binary-search/binary_search_questions.md | 150 ++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 docs/binary-search/binary_search_questions.md diff --git a/docs/binary-search/binary_search_questions.md b/docs/binary-search/binary_search_questions.md new file mode 100644 index 000000000..360302ece --- /dev/null +++ b/docs/binary-search/binary_search_questions.md @@ -0,0 +1,150 @@ +--- +id: binary-search-dsa +sidebar_position: 10 +title: Binary Search +sidebar_label: Binary Search +description: "In this blog post, we'll dive into the binary search algorithm, a fundamental technique in computer science for efficiently finding an element in a sorted array." +tags: [dsa, algorithms, binary search] +--- + +# Binary Search Patterns: A Comprehensive Guide + +Welcome to this in-depth exploration of binary search patterns! This guide is designed to help you master the art of binary search through a curated collection of problems, explanations, and real-world applications. + +## Table of Contents + +- [Binary Search Patterns: A Comprehensive Guide](#binary-search-patterns-a-comprehensive-guide) + - [Table of Contents](#table-of-contents) + - [Introduction to Binary Search](#introduction-to-binary-search) + - [Why Master Binary Search?](#why-master-binary-search) + - [Binary Search Patterns](#binary-search-patterns) + - [Pattern 1: Binary Search on 1D Arrays](#pattern-1-binary-search-on-1d-arrays) + - [Pattern 2: Binary Search on Answer Space](#pattern-2-binary-search-on-answer-space) + - [Pattern 3: Binary Search on 2D Arrays](#pattern-3-binary-search-on-2d-arrays) + - [Problem Collections](#problem-collections) + - [Pattern 1: Binary Search on 1D Arrays](#pattern-1-binary-search-on-1d-arrays-1) + - [Pattern 2: Binary Search on Answer Space](#pattern-2-binary-search-on-answer-space-1) + - [Pattern 3: Binary Search on 2D Arrays](#pattern-3-binary-search-on-2d-arrays-1) + - [Real-World Applications](#real-world-applications) + - [Benefits of Mastering Binary Search](#benefits-of-mastering-binary-search) + - [How to Use This Guide](#how-to-use-this-guide) + +## Introduction to Binary Search + +Binary search is a fundamental algorithm in computer science that efficiently locates an element in a sorted array. By repeatedly dividing the search interval in half, binary search achieves a time complexity of O(log n), making it significantly faster than linear search for large datasets. + +## Why Master Binary Search? + +Understanding binary search is crucial for several reasons: + +1. **Efficiency**: Binary search reduces time complexity from O(n) to O(log n), essential for working with large datasets. +2. **Versatility**: The core concept of dividing the search space applies to various problem types, from simple searches to complex optimization problems. +3. **Problem-Solving Skills**: Mastering binary search enhances your ability to think algorithmically and approach problems systematically. +4. **Interview Preparation**: Binary search is a popular topic in technical interviews, appearing in questions from leading tech companies. +5. **Foundation for Advanced Algorithms**: Many advanced algorithms and data structures build upon the principles of binary search. + +## Binary Search Patterns + +### Pattern 1: Binary Search on 1D Arrays + +This pattern focuses on applying binary search to one-dimensional sorted arrays. It covers: + +- Basic binary search implementation +- Finding boundaries (first/last occurrences) +- Searching in rotated sorted arrays +- Finding peak elements + +**Key Techniques**: +- Modifying search conditions +- Handling duplicate elements +- Identifying search spaces in modified arrays + +### Pattern 2: Binary Search on Answer Space + +This pattern applies binary search to a range of possible answers rather than a specific array. It's useful for: + +- Optimization problems +- Finding roots of equations +- Minimizing/maximizing values subject to constraints + +**Key Techniques**: +- Defining a feasible answer range +- Creating a condition to check answer validity +- Adjusting the search space based on the condition + +### Pattern 3: Binary Search on 2D Arrays + +This pattern extends binary search to two-dimensional arrays or matrices. It covers: + +- Searching in row-wise and column-wise sorted matrices +- Finding peak elements in 2D arrays +- Calculating matrix medians + +**Key Techniques**: +- Treating 2D arrays as flattened 1D arrays +- Utilizing properties of sorted rows/columns +- Combining binary search with other techniques (e.g., merge) + +## Problem Collections + +### Pattern 1: Binary Search on 1D Arrays + +| Problem | Difficulty | Practice Link | Explanation | +|---------|------------|---------------|-------------| +| Binary Search | Easy | [LeetCode](https://leetcode.com/problems/binary-search/) | [Explanation](https://takeuforward.org/data-structure/binary-search-explained/) | +| Implement Lower Bound | Easy | [GeeksforGeeks](https://www.geeksforgeeks.org/problems/floor-in-a-sorted-array-1587115620/1) | [Explanation](https://takeuforward.org/arrays/implement-lower-bound-bs-2/) | +| Search in Rotated Sorted Array | Medium | [LeetCode](https://leetcode.com/problems/search-in-rotated-sorted-array/) | [Explanation](https://takeuforward.org/data-structure/search-element-in-a-rotated-sorted-array/) | +| Find Minimum in Rotated Sorted Array | Medium | [LeetCode](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/) | [Explanation](https://takeuforward.org/data-structure/minimum-in-rotated-sorted-arrayy) | + +### Pattern 2: Binary Search on Answer Space + +| Problem | Difficulty | Practice Link | Explanation | +|---------|------------|---------------|-------------| +| Koko Eating Bananas | Medium | [LeetCode](https://leetcode.com/problems/koko-eating-bananas/) | [Explanation](https://takeuforward.org/binary-search/koko-eating-bananas/) | +| Find the Smallest Divisor | Medium | [LeetCode](https://leetcode.com/problems/find-the-smallest-divisor-given-a-threshold/) | [Explanation](https://takeuforward.org/arrays/find-the-smallest-divisor-given-a-threshold/) | +| Aggressive Cows | Hard | [GeeksforGeeks](https://www.geeksforgeeks.org/problems/aggressive-cows/0) | [Explanation](https://takeuforward.org/data-structure/aggressive-cows-detailed-solution/) | +| Median of Two Sorted Arrays | Hard | [LeetCode](https://leetcode.com/problems/median-of-two-sorted-arrays/) | [Explanation](https://takeuforward.org/data-structure/median-of-two-sorted-arrays-of-different-sizes) | + +### Pattern 3: Binary Search on 2D Arrays + +| Problem | Difficulty | Practice Link | Explanation | +|---------|------------|---------------|-------------| +| Search a 2D Matrix | Medium | [LeetCode](https://leetcode.com/problems/search-a-2d-matrix/) | [Explanation](https://takeuforward.org/data-structure/search-in-a-sorted-2d-matrix/) | +| Find Peak Element II | Hard | [LeetCode](https://leetcode.com/problems/find-a-peak-element-ii) | Not Available | +| Matrix Median | Hard | [InterviewBit](https://www.interviewbit.com/problems/matrix-median/) | [Explanation](https://takeuforward.org/data-structure/median-of-row-wise-sorted-matrix/) | + +## Real-World Applications + +Binary search and its patterns find applications in various real-world scenarios: + +1. **Database Systems**: Efficient data retrieval in sorted indexes. +2. **Machine Learning**: Hyperparameter tuning and model selection. +3. **Computer Graphics**: Collision detection in video games and simulations. +4. **Network Routing**: Finding optimal paths in network topologies. +5. **Version Control**: Identifying commits in large repositories (e.g., git bisect). +6. **Resource Allocation**: Optimizing resource distribution in cloud computing. +7. **Financial Analysis**: Finding breakeven points or optimal investment strategies. + +## Benefits of Mastering Binary Search + +By working through these problems and understanding the patterns, you'll gain: + +1. **Improved Problem-Solving Skills**: Develop a systematic approach to breaking down complex problems. +2. **Enhanced Algorithmic Thinking**: Learn to identify opportunities for optimization in various scenarios. +3. **Interview Readiness**: Build confidence in tackling a wide range of coding challenges. +4. **Efficiency Mindset**: Cultivate an intuition for designing efficient algorithms in your daily coding tasks. +5. **Foundation for Advanced Topics**: Prepare yourself for more complex algorithmic concepts and data structures. + +## How to Use This Guide + +1. Start with the basic binary search implementation in Pattern 1. +2. Progress through each pattern, solving problems of increasing difficulty. +3. For each problem: + - Attempt to solve it independently. + - If stuck, refer to the provided explanation. + - After solving, compare your solution with the optimal approach. +4. Reflect on the patterns and techniques used in each problem. +5. Try to apply these patterns to new, unseen problems to reinforce your learning. + +--- + From 2ba3091d2eb640d2849743749d881e55875f70e9 Mon Sep 17 00:00:00 2001 From: PRASHANTSWAROOP001 Date: Mon, 7 Oct 2024 17:52:27 +0530 Subject: [PATCH 2/3] Removed table of content files. --- docs/binary-search/binary_search_questions.md | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/docs/binary-search/binary_search_questions.md b/docs/binary-search/binary_search_questions.md index 360302ece..34846419a 100644 --- a/docs/binary-search/binary_search_questions.md +++ b/docs/binary-search/binary_search_questions.md @@ -11,23 +11,6 @@ tags: [dsa, algorithms, binary search] Welcome to this in-depth exploration of binary search patterns! This guide is designed to help you master the art of binary search through a curated collection of problems, explanations, and real-world applications. -## Table of Contents - -- [Binary Search Patterns: A Comprehensive Guide](#binary-search-patterns-a-comprehensive-guide) - - [Table of Contents](#table-of-contents) - - [Introduction to Binary Search](#introduction-to-binary-search) - - [Why Master Binary Search?](#why-master-binary-search) - - [Binary Search Patterns](#binary-search-patterns) - - [Pattern 1: Binary Search on 1D Arrays](#pattern-1-binary-search-on-1d-arrays) - - [Pattern 2: Binary Search on Answer Space](#pattern-2-binary-search-on-answer-space) - - [Pattern 3: Binary Search on 2D Arrays](#pattern-3-binary-search-on-2d-arrays) - - [Problem Collections](#problem-collections) - - [Pattern 1: Binary Search on 1D Arrays](#pattern-1-binary-search-on-1d-arrays-1) - - [Pattern 2: Binary Search on Answer Space](#pattern-2-binary-search-on-answer-space-1) - - [Pattern 3: Binary Search on 2D Arrays](#pattern-3-binary-search-on-2d-arrays-1) - - [Real-World Applications](#real-world-applications) - - [Benefits of Mastering Binary Search](#benefits-of-mastering-binary-search) - - [How to Use This Guide](#how-to-use-this-guide) ## Introduction to Binary Search From 55b215be5b7baa3216af51546c5f909b10182c9b Mon Sep 17 00:00:00 2001 From: PRASHANTSWAROOP001 Date: Mon, 7 Oct 2024 17:55:26 +0530 Subject: [PATCH 3/3] deleted --- line at the bottom --- docs/binary-search/binary_search_questions.md | 1 - 1 file changed, 1 deletion(-) diff --git a/docs/binary-search/binary_search_questions.md b/docs/binary-search/binary_search_questions.md index 34846419a..3841afa16 100644 --- a/docs/binary-search/binary_search_questions.md +++ b/docs/binary-search/binary_search_questions.md @@ -129,5 +129,4 @@ By working through these problems and understanding the patterns, you'll gain: 4. Reflect on the patterns and techniques used in each problem. 5. Try to apply these patterns to new, unseen problems to reinforce your learning. ----