Author: devangini123
π Problem Statement: Given a sorted array, this algorithm returns the k elements closest to a target value x. The output is sorted in ascending order. Approach: We apply binary search to find the best starting index of the k closest elements window. We compare arr[m + k] – x and x – arr[m] to decide whether to shift the window left or right. Once we find the optimal window, return the subarray from index l to l + k – 1. Time & Space Complexity: Time Complexity: O(log(n β k) + k) Space Complexity: O(k) Dry Run Input (assumed):…
π Problem Statement: This algorithm finds the single non-duplicate element in a sorted array where every other element appears exactly twice. Approach: Use binary search between left (l) and right (r). At each mid (m): If arr[m] === arr[m – 1], count elements on the left. If that count is odd β single lies left β r = m – 2. If count is even β single lies right β l = m + 1. Same logic applies if arr[m] === arr[m + 1]. If neither left nor right match β return arr[m]. Time & Space Complexity: Time Complexity: O(logn)…
π Problem Statement: This algorithm finds the peak element in a mountain array using binary search. A mountain array increases to a peak and then decreases. Approach: Initialize l = 0 and r = arr.length – 1. Use binary search: If arr[m + 1] > arr[m], peak is to the right β l = m + 1. Else peak is at m or to the left β r = m. When loop ends, l (or r) is the peak index. Time & Space Complexity: Time Complexity: O(logn) Space Complexity: O(1) Dry Run Input: arr = [1, 3, 5, 6, 4,…
π Problem Statement: This version improves clarity by separating the two binary searches more cleanly. We use one binary search to find the first index, and another to find the last index of the target. Approach: Binary search for the **first index** (on match, shift right side). Binary search for the **last index** (on match, shift left side). Update ans[0] and ans[1] accordingly. Time & Space Complexity: Time Complexity: O(logn) Space Complexity: O(1) Dry Run Input: arr = [5, 7, 7, 8, 8, 10], target = 8 Initial: l = 0, r = 5 ans = [-1, -1] — First…
π Problem Statement: This problem asks us to find the first and last positions of a given target in a sorted array. If the target is not found, return [-1, -1]. Approach: Use binary search twice. Once to find the **first occurrence** (left bound). Once to find the **last occurrence** (right bound). Store results in ans[0] and ans[1]. Time & Space Complexity: Time Complexity: O(logn) Space Complexity: O(1) Dry Run Input: arr = [5, 7, 7, 8, 8, 10], target = 8 Initial: l = 0, r = 5 ans = [-1, -1] — First While Loop (find first index)…
π Problem Statement: This problem asks us to find the smallest element in an array that was originally sorted in ascending order and then rotated. The array has no duplicates. Approach: We use binary search to locate the minimum element efficiently. Initialize l = 0 and r = a.length – 1. While l β€ r. If a[l] β€ a[r] β subarray is sorted β return a[l]. Find mid: m = l + floor((r – l) / 2). If a[m] < a[m - 1] β pivot found β return a[m]. If a[l] > a[m] β rotation point is left β r…
π Problem Statement: This problem involves finding any **peak element** in an array. A peak element is an element that is strictly greater than its neighbors. The array is unsorted, but thereβs guaranteed to be at least one peak. Approach: We use binary search to find a peak efficiently. Initialize l = 0 and r = arr.length – 1. While l < r: Find middle: m = l + floor((r - l) / 2) If arr[m] < arr[m + 1] β we are in ascending slope β shift l = m + 1. Else β we are in descending slope…
π Problem Statement: This problem finds the first bad version in a sequence of versions from 1 to n. We have an API isBadVersion(version) that returns whether a version is bad. We use binary search to pinpoint the earliest bad version. Approach: Initialize pointers: l = 1 and r = n. While l < r: Compute mid: m = l + floor((r - l) / 2). If isBadVersion(m) is false β move right (l = m + 1). Else (true) β move left or stay (r = m). Loop ends when l == r, which is the first bad version.…
π Problem Statement: This problem requires finding a target value in a rotated sorted array nums. Using a modified binary search, we determine which half of the array is sorted in each iteration and narrow the search range accordingly. Approach: Initialize two pointers: l = 0 and r = nums.length – 1. While l β€ r: Compute mid: m = l + floor((r – l) / 2). If nums[m] === target, return m. Check which side is sorted: If nums[l] β€ nums[m], left side is sorted: If target β [nums[l], nums[m]], move left: r = m – 1. Else, move…
π Problem Statement: This problem uses binary search to find a hidden number between 1 and n using a feedback API guess(num), which tells us whether our guess is too high, too low, or correct. Approach: Initialize two pointers: l = 1 and r = n. Use binary search to guess the middle number m. If guess(m) returns: 0 β m is the correct number. -1 β the picked number is smaller β move left. 1 β the picked number is larger β move right. Repeat until the number is found. Edge Case Input: n= 1, pick = 1 Output:…
Contact Us
Subscribe to Stay Updated
Stay ahead in the world of tech with our exclusive newsletter! Subscribe now for regular updates on the latest trends, valuable coding resources, and tips to boost your frontend development skills.
