Find the Duplicate Number — Binary Search on Count
Find the duplicate in array of n+1 integers in [1,n] using binary search on value with count-of-smaller logic.
webcoderspeed.com
7 articles
Find the duplicate in array of n+1 integers in [1,n] using binary search on value with count-of-smaller logic.
Calculate the perimeter of an island in a binary grid. Each land cell contributes 4 edges minus 2 for each land neighbour.
Count pairs (i,j) where j-i != nums[j]-nums[i] by counting good pairs via frequency map of nums[i]-i.
Count tuples (a,b,c,d) where a*b=c*d by counting pairs with each product using a frequency map.
Apply the inclusion-exclusion principle to count elements satisfying union of conditions. Solves divisibility, coverage, and derangement problems.
Count the number of shortest paths from source to destination using modified Dijkstra that tracks both minimum distance and path count simultaneously.
Count the number of distinct substrings using the suffix array and LCP array. Total substrings minus sum of LCP values gives distinct count in O(n log n).