Binary Search Techniques and Applications
This week's practice focuses on mastering binary search algorithms through LeetCode problems.
The first problem is Binary Search. Below is the implementation:
int search(int* nums, int numsSize, int target) {
int left = 0;
int right = numsSize - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if ( ...
Posted on Mon, 08 Jun 2026 18:45:49 +0000 by canabatz
Binary Search and Memoized DFS for Optimization Problems
Maximizing Minimum Distance Between ElementsGiven an array of unique positions and a number of items to place, the goal is to position the items such that the minimum absolute difference between any two items' positions is maximized.Example 1:Input: positions = [1,2,3,4,7], items = 3Output: 3Explanation: Placing items at positions 1, 4, and 7 y ...
Posted on Thu, 04 Jun 2026 16:01:15 +0000 by cash09
Programming Contest Solutions and Algorithm Explanations
A. Programming Contest
This problem is a straightforward implementation task. The idea is to count the number of valid years betweeen two given years, excluding specific invalid years provided in the input.
void solve() {
int n, m;
std::cin >> n;
std::vector<int> invalidYearFlag(10000, 0); // Assuming a safe upper bound ...
Posted on Mon, 01 Jun 2026 17:35:16 +0000 by czs
Simultaneous Binary Search for Multiple Queries
When numerous queries each admit a binary search solution, but performing binary search individually for every query is too slow, we can process all queries together using a technique known as simultaneous binary search (also called "parallel binary search" or "global binary search").
This approach requires:
Queries to be h ...
Posted on Wed, 20 May 2026 03:05:55 +0000 by digitalmustache
Binary Search Algorithm Implementation and Performance Analysis in Java
Binary search operates with O(log n) time complexity on a sorted array of n elements. The algorithm repeatedly divides the search interval in half, achieving logarithmic performance.
Algorithm Fundamentals
Binary search, also known as half-interval search, is an efficient algorithm for locating a target value within a sorted sequence. It compar ...
Posted on Sat, 16 May 2026 09:08:13 +0000 by XPertMailer
Minimum Spanning Tree Algorithmic Practice Problems
Problem A: Road Construction
Description
There are n initially isolated cities. In each round, every city connects to its nearest neighbor. If a cycle formss during a round, the shortest edge in that cycle is removed. Once cities are connected, they form a "union" and act as a single entity in subsequent rounds. The process continues ...
Posted on Fri, 15 May 2026 17:29:52 +0000 by peter.t
Locating Target Boundaries in a Sorted Array via Binary Search
Problem StatementGiven an array of integers sorted in non-decreasing order, identify the starting and ending index of a specified target value. If the target is absent from the array, return [-1, -1]. The solution must operate with a logarithmic time complexity of O(log n).Expected OutcomesFor data = [5, 7, 7, 8, 8, 10], target = 8, the output ...
Posted on Fri, 15 May 2026 09:02:31 +0000 by heavenly
Implementing Saddle Point Search and String Operations in C
Finding Saddle Points in a 2D Array
A saddle point in an m×n matrix is an element that is both the maximum in its row and the minimum in its column.
#include <stdio.h>
void locateSaddlePoint(int matrix[100][100], int rows, int cols) {
for (int row = 0; row < rows; row++) {
int rowMax = matrix[row][0];
int maxCol = ...
Posted on Thu, 14 May 2026 11:36:24 +0000 by HairyArse
Binary Search Strategy for Grader-Based Interactive Number Guessing
Interactive programming challenges require a different approach than standard I/O problems. Instead of reading from standard input and writing to standard output, the solution communicates directly with a judging system through predefined function calls. This architecture is commonly referred to as a grader-based interaction model.
In a typical ...
Posted on Wed, 13 May 2026 22:09:09 +0000 by indigo2k
Minimum Swaps to Sort an Array Using Adjacent Exchanges
This problem requires finding the minimum number of adjacent swaps to sort an array containing a permutation of numbers from 1 to n. The cost of each adjacent swap is 1.
The key insight is that each adjacent swap changes the number of inversions in the array by exactly one. To sort the array in ascending order, we aim to eliminate all inversion ...
Posted on Wed, 13 May 2026 00:05:42 +0000 by ole968