Learn it from developers who built it.
Tutorials, how-tos and deep dives on AI, .NET, C#, cloud and developer tools, written by the community.
Find the shortest path in a 1-2 weighted graph efficiently using BFS after transforming weight-2 edges.
Find the longest consecutive path in a binary tree where child nodes are exactly 1 greater than their parent. Uses DFS.
Find the maximum index difference by jumping between alphabets in a string, starting from 'a'. Optimized O(N) solution.
Discover the shortest unique prefix for every word in an array using a Trie. Efficiently identify distinct word beginnings with this optimized algorithm.
Solve the max absolute difference of two non-overlapping subarrays in O(n) using Kadane's Algorithm and four helper arrays.
This article explains how to solve the Numbers with Given Digit Sum problem using Dynamic Programming (DP). Starting from the brute-force approach, it gradually builds the DP solution with state definition, transition formula, initialization, dry run, Java implementation, and time and space complexity analysis. The explanation is beginner-friendly and suitable for coding interview preparation.
Learn how to solve the Longest Bitonic Subarray problem in Java using an efficient O(n) approach. This tutorial includes the problem statement, algorithm, step-by-step explanation, dry run, Java code, complexity analysis, and interview tips.
Learn how to count representations of a positive integer as a sum of two or more consecutive natural numbers using arithmetic progression and an O(sqrt n) Java algorithm.
Find the largest unblocked rectangular area in a grid with blocked rows/columns by identifying max gaps between blocked lines.
Find the maximum sum path in two sorted arrays with distinct integers, allowing switches at common elements.
Master dynamic programming to efficiently count LCS increases. Learn prefix/suffix DP for O(nm) solutions, avoiding brute-force recomputation.
Master the Maximum Subarray Sum problem with at most one deletion using dynamic programming. Optimize with O(N) time and O(1) space.
Find the maximum number of people that can be defeated using the sum-of-squares formula and binary search in O(log n) time.
Optimize array transformations by finding the minimum operations to equalize prefix sums using median and prefix sum techniques for O(n) efficiency.
A greedy stack-based problem where we adjust k based on string length and remove characters to form the lexicographically smallest string possible. The solution uses monotonic stack technique for optimal character removal.
A linked list problem where nodes are removed if a greater value exists on their right side. The optimal solution uses list reversal and a greedy traversal with a running maximum to achieve O(n) time complexity.
Learn how to count valid balanced-parentheses expressions of length n using Catalan numbers. Odd lengths return 0, while even lengths are computed in O(n) time and O(1) space.
Master the Josephus Problem! Learn the elegant recurrence relation, its 0-based and 1-based solutions, and why iterative is superior.
Solve the Seating Arrangement Problem: Determine if k people can be seated in empty seats without adjacent occupancy using a greedy approach.
Find the maximum number of people visible in a line. This problem is solved efficiently using a monotonic stack to find previous and next greater elements.
A matrix search problem solved efficiently using binary search on value range and counting elements less than or equal to mid in O(n) time per step.
Learn how to efficiently count array elements within given ranges using sorting and binary search. Includes intuition, lower bound and upper bound concepts, complexity analysis, and Java solution.
This problem involves finding the number of elements in an unsorted array that lie within a given range [a, b] for multiple queries. A naive approach would check each element for every query, resulting in high time complexity. A more efficient solution uses sorting and binary search: Sort the array to enable fast searching. For each query [a, b]: Use a lower bound search to find the first element = a. Use an upper bound search to find the first element > b. The difference between these indices gives the count of elements in the range. This approach significantly reduces time complexity to O(n log n + q log n) while keeping space usage minimal. It’s a classic example of combining sorting with binary search to handle range-based queries efficiently.
Find the position of the single set bit in an integer's binary representation. Learn the bit manipulation trick using n & (n-1) for efficient problem-solving.
Find buildings visible to sunlight using a greedy algorithm! This problem focuses on array traversal, tracking maximums, and handling edge cases like equal heights. A common interview question!
Maximize thieves caught by policemen within distance 'k' using a greedy two-pointer approach. Optimal O(n) solution ensures the closest valid pairs are matched first.
A detailed guide to solving the Minimum Window Subsequence problem using a greedy two-pointer approach. The article explains how to find the smallest substring in a string that contains another string as a subsequence, using forward scanning to match characters and backward scanning to minimize the window. It includes step-by-step intuition, dry run, and an optimized O(n × m) solution suitable for coding interviews.
Learn how to efficiently solve the Substrings With Exactly K Distinct Characters problem using the sliding window technique. This guide explains the key insight of converting the problem into “at most K” subproblems, includes step-by-step intuition, dry run examples, and an optimized Java solution with O(n) time complexity and constant space.
A detailed guide to solving the Subarrays With At Most K Distinct Integers problem using the sliding window technique. Learn the intuition, step-by-step approach, dry run examples, and optimized Java implementation with O(n) time complexity. Perfect for coding interviews and mastering array-based problems.
Master the 'Count Subarrays with K Odd Numbers' problem using the sliding window and 'atMost(k) - atMost(k-1)' technique. Optimize your coding interview prep with this O(n) solution!
Everything on Data Structures and Algorithms (DSA), not only its articles: the Data Structures and Algorithms (DSA) page →
Write an article and earn +1,000 SHARP when it is approved.
Write an article