Subsets with MEX in Range
Learn how to count array subsets whose MEX falls within a given range using frequencies, powers of two, and modular arithmetic in Java.
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.
Learn how to count array subsets whose MEX falls within a given range using frequencies, powers of two, and modular arithmetic in Java.
Learn how to minimize file-merge cost using a greedy min-heap strategy in Java with PriorityQueue, including examples, a dry run, and complexity analysis.
Learn a dynamic-programming approach to maximize a binary string's weight by forming non-overlapping alternating pairs while accounting for toggle costs.
Learn how to determine whether a movement-command path can fit in a grid by tracking its relative bounding rectangle in O(|s|) time.
Solve a wildcard palindrome problem by minimizing the total ASCII difference between adjacent characters, including impossibility checks and a Java implementation.
Learn how to count distinct paths from a grid coordinate to the origin using dynamic programming, base cases, and a recurrence relation in Java.
Learn how to solve the Box Stacking Problem using Dynamic Programming in Java with box rotations, normalized bases, stacking rules, and O(n²) complexity.
Learn how to solve Range GCD Queries using a Segment Tree in Java 21 with efficient range queries, point updates, and Euclidean Algorithm.
Learn how to solve the Minimum Cost Pizza Selection problem using Dynamic Programming. Given the area and cost of Small, Medium, and Large pizzas, the goal is to find the minimum cost required to achieve a total area of at least x. This article explains the DP approach, state definition, transition, handling of area greater than x, dry run, Java implementation, and complexity analysis.
Learn why recursion is essential before backtracking in C#, with recursion trees, take-or-skip decisions, permutations, and the choose-explore-undo approach.
Learn how to solve the maximum disc stack problem using dynamic programming and a Fenwick Tree for an efficient O(n log n) Java solution.
Learn how to solve the Pyramid Array with Reduce Operations problem using Dynamic Programming in Java. This article explains how to calculate the maximum possible pyramid height at every position using left and right DP arrays. It also covers the key observation that a pyramid with peak height x has a total sum of x², allowing us to calculate the minimum reduction cost efficiently in O(n) time and O(n) auxiliary space.
Learn how to solve the Longest Matching Word in Dictionary with Removals problem in Java. This article explains the subsequence concept, character position preprocessing, binary search, greedy matching, complete Java solution, dry run, and time and space complexity.
Learn how to check whether the nodes at every corresponding level of two binary trees are anagrams using Java. This article explains the Level Order Traversal (BFS) approach, how to use HashMap to compare node frequencies, step-by-step code implementation, dry run, and time and space complexity.
Find the minimum absolute difference between any two different nodes in a Binary Search Tree (BST) using inorder traversal. This article explains how the BST property helps generate sorted values and how comparing adjacent values gives an efficient O(n) solution in Java.
Learn how to maximize leaf nodes within a budget using BFS, level-based costs, frequency counting, and a greedy strategy in Java with O(n + k) time.
Learn how to solve the minimum unused elements problem using dynamic programming by splitting an array into strictly increasing and decreasing subsequences.
Learn how to count palindromic strings of length up to n using the first k lowercase English letters, where no character appears more than twice. This article explains the key palindrome observations, separates even and odd-length cases, derives the permutation formulas, and provides an efficient Java solution using modular arithmetic. The solution achieves O(k) time complexity and O(1) auxiliary space.
Count Subsequences Divisible by N is a Dynamic Programming problem where we need to count the number of non-empty subsequences of a numeric string whose numeric value is divisible by a given integer n. The solution uses remainder-based DP to efficiently track subsequences for each possible remainder instead of generating all subsequences. The approach runs in O(|s| × n) time and O(n) space and handles large inputs efficiently.
Learn how to solve the Minimum Cost Selection problem using Dynamic Programming in Java. Understand the recurrence, dry run, optimal substructure, and O(n) time with O(1) auxiliary space.
Learn how to detect negative weight cycles in a directed graph using the Bellman-Ford algorithm in Java, including relaxation, disconnected graphs, complexity, and examples.
Count the number of prefix-balanced binary strings of length 2 * n containing exactly n ones and n zeros, where every prefix has at least as many ones as zeros. This article explains the Dynamic Programming approach in Java, including the DP transition, base case, dry run, Catalan number connection, and time and space complexity.
Learn how to solve the Transform String problem in Java using a greedy approach. This article explains how to check character frequencies, compare both strings from right to left, and calculate the minimum number of operations required to transform one string into another. The solution runs in O(n) time and O(1) auxiliary space.
Find the maximum difference between an ancestor node and its descendant node in a binary tree using DFS and recursion. This article explains the approach, Java solution, dry run, time complexity, and space complexity.
The Snake and Ladder Problem is a classic shortest-path problem that can be efficiently solved using Breadth-First Search (BFS). The board is represented as a graph where each cell is a node, and each dice throw creates edges to the next 1–6 possible cells. Snakes and ladders act as immediate jumps between cells. The goal is to find the minimum number of dice throws required to reach the final cell. This article explains the BFS approach, Java implementation, step-by-step logic, and complexity analysis.
Subset Sum on Generated Sequence is a Greedy Algorithm problem where a sequence is dynamically generated from an initial value s and the given array arr[]. Each new value is calculated using the sum of all previously generated values plus the current array element. The generated sequence has a special property where each new value is greater than the sum of all previous values. This property allows us to determine whether a target x can be formed efficiently by processing the sequence from largest to smallest and greedily selecting suitable values. The solution runs in O(n) time and uses O(n) auxiliary space.
Learn how to find the Longest Path in a Directed Acyclic Graph (DAG) using Java. This solution uses Topological Sorting and Dynamic Programming to calculate the maximum distance from a given source vertex to every other vertex. The article explains adjacency lists, DFS-based topological sorting, distance initialization, edge relaxation using Math.max(), handling unreachable vertices with Integer.MIN_VALUE, and the overall time and space complexity
Learn how to count contiguous subarrays with sums in a given range using a sliding window for positive integer arrays in O(n) time.
Optimize range increment operations from O(nm) to O(n+m) using a difference array and prefix sum technique for finding the maximum element.
Solve subset product problems with distinct primes using Bitmask DP. Efficiently handles values up to 30 by mapping primes to bits.
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