DDSA Solutions
POTD Solutions

GeeksforGeeks Java Solutions

Java solutions for GeeksforGeeks problems with step-by-step explanations on 230+ problems, plus approach notes and complexity analysis across the full archive.

740
Solved
230
Explained

740 problemsFull explanation available

#Title
10 - 1 Knapsack Problem
21s Surrounded by 0s
32D Difference Array
42D Submatrix Sum Queries
5Activity Selection
6Add 1 to a Linked List Number
7Add Binary Strings
8Add Number Linked Lists
9Adding Ones
10Adventure in a Maze
11Aggressive Cows
12Alien Dictionary
13All numbers with specific difference
14All Subsets Xor Sum
15All Unique Permutations of an array
16Allocate Minimum Pages
17Alternate positive and negative numbers
18Alternative Sorting
19Anagram
20Anagram Palindrome
21Ancestors in Binary Tree
22AND In Range
23Array Duplicates
24Array to BST
25Articulation Point - II
26ASCII Range Sum
27Assign Mice Holes
28Balancing Consonants and Vowels Ratio
29Balancing with Distinct Powers
30Bellman-Ford
31BFS of graph
32Binary Searchable Count
33Binary Strings with Equal Sum of Two Halves
34Binary Tree to DLL
35Bird and Max Fruit Gathering
36Bitonic Point
37Boolean Parenthesization
38Bottom View of Binary Tree
39Box Stacking
40Brackets in Matrix Chain Multiplication
41Bridge edge in a graph
42BST to greater sum tree
43BST with Dead End
44Buildings with Sunlight
45Burning Tree
46Bus Conductor
47Bus Ticket Change
48Buy Stock with Transaction Fee
49Candy
50Case-specific Sorting of Strings
51Check for BST
52Check for Power
53Check if a String is Subsequence of Other
54Check if All Bits Set
55Check if an Array is Max Heap
56Check if frequencies can be equal
57Check if subtree
58Check If two Line segments Intersect
59Check Level Anagrams in Binary Trees
60Check Preorder of BST
61Check Repeated Substring with K Replacements
62Check Subset sum divisible by k
63Chocolate Distribution Problem
64Chocolate Pickup II
65Chocolates Pickup
66Choose and Swap
67Circle of strings
68Clone a linked list with next and random pointer
69Clone an Undirected Graph
70Clone List with Next and Random
71Closest Neighbour in BST
72Closest Three Sum
73Coils in Matrix
74Coin Change (Count Ways)
75Coin Change (Minimum Coins)
76Coin Piles
77Common in 3 Sorted Arrays
78Compare two fractions
79Consecutive 1's not allowed
80Construct a Full Binary Tree
81Construct an array from its pair-sum array
82Construct Binary Tree from Parent Array
83Construct List using XOR Queries
84Construct Tree from Inorder & Preorder
85Construct Tree from Preorder & Postorder
86Container With Most Water
87Convert One String to Another with Constraints
88Count all triplets with given sum in sorted array
89Count Derangements
90Count distinct elements in every window
91Count elements less than or equal to k in a sorted rotated array
92Count increasing Subarrays
93Count Indices That Escape Array
94Count Indices to Balance Even and Odd Sums
95Count Inversions
96Count Linked List Nodes
97Count Matching Subsequences
98Count Numbers Containing Specific Digits
99Count of distinct substrings
100Count Pairs Divisible By K
101Count pairs Sum in matrices
102Count Pairs Violating BST Property
103Count Pairs whose sum is less than target
104Count pairs with given sum
105Count Palindromic Strings with Constraints
106Count Prefix-Balanced Binary Strings
107Count Reverse Pairs
108Count set bits
109Count Smaller elements
110Count Sorted Digit Groupings
111Count Spanning Trees in a Graph
112Count Subarray with k odds
113Count Subarrays with given XOR
114Count Subsequences Divisible by n
115Count Subset With Target Sum II
116Count the Coprimes
117Count the number of possible triangles
118Count the paths
119Count Tree Paths With Limited Max Edge Weight
120Count Unique Vowel Strings
121Count ways to N'th Stair(Order does not matter)
122Count X in Range of a Sorted Array
123Counting elements in two arrays
124Course Schedule I
125Course Schedule II
126Coverage of all Zeros in a Binary Matrix
127Cut Matrix
128Cut rope to maximise product
129Cutting Binary String
130Decode the string
131Delete Alternate Nodes
132Delete node in Doubly Linked List
133Delete Nodes with Greater on Right
134Deletion and Reverse in Circular Linked List
135Design MinMax Queue
136Detect Loop in linked list
137DFS of Graph
138Diameter of a Binary Tree
139Dice throw
140Difference Check
141Dijkstra Algorithm
142Directed Graph Cycle
143Distance of nearest cell having 1
144Distribute Candies
145Divisible by 13
146Dominant Pairs
147Duplicate Subtrees
148Edit Distance
149Elements in range [a, b]
150Elements in the Range
151Equal Point in Brackets
152Equalize All Prefix Sums
153Equalize the Towers
154Equilibrium Point
155Evaluation of Postfix Expression
156Exactly one swap
157Exit Point in a Matrix
158Express as Consecutive Number Sum
159Expression Add Operators
160Expression contains redundant bracket or not
161Extract the Number from the String
162Facing the sun
163Farthest Smaller Right
164Find all possible palindromic partitions of a String
165Find All Triplets with Zero Sum
166Find H-Index
167Find K Smallest Sum Pairs
168Find Kth Rotation
169Find length of Loop
170Find maximum volume of a cuboid
171Find median in a stream
172Find Only Repetitive Element from 1 to n-1
173Find rectangle with corners as 1
174Find the closest pair from two arrays
175Find the first node of loop in linked list
176Find the longest string
177Find the number of islands
178Find the Sum of Last N nodes of the Linked List
179Finding Profession
180Fixing Two nodes of a BST
181Flattening a Linked List
182Flip to Maximize 1s
183Flood fill Algorithm
184Floyd Warshall
185Form a palindrome
186Form the Largest Number
187Friends Pairing Problem
188Frog Jump
189Game of XOR
190Game with String
191Gas Station
192Geek in a Maze
193Generate all binary strings
194Generate Binary Numbers
195Generate IP Addresses
196Generate Permutations of an array
197Get Min from Stack
198Get Minimum Squares
199Gold Mine Problem
200Graph Diameter
201Gray Code
202Group Balls by Sequence
203Happiest Triplet
204Height of Binary Tree
205High Effort vs Low Effort
206Histogram Max Rectangular Area
207Huffman Encoding
208Identical Linked Lists
209Implement Atoi
210Implement k Queues in a Single Array
211Implement Pow
212Implement Trie
213Indexes of Subarray Sum
214Inorder Traversal
215Insert in Sorted Circular Linked List
216Insert in Sorted way in a Sorted DLL
217Insert Interval
218Integral Points Inside Triangle
219Interleave the First Half of the Queue with Second Half
220Interleaved Strings
221Intersection in Y Shaped Lists
222Intersection of Two arrays with Duplicate Elements
223Intersection of Two Sorted Arrays
224Intersection Point in Y Shaped Linked Lists
225Is Binary Tree Heap
226Is Linked List Length Even
227Job Sequencing Problem
228Josephus problem
229K closest elements
230K Closest Points to Origin
231K closest Values
232k largest elements
233K Sized Subarray Maximum
234K Sum Paths
235k Times Appearing Adjacent Two 1's
236K-Pangrams
237K-th element of two Arrays
238K-th Largest Sum Contiguous Subarray
239k-th Smallest in BST
240Kadane's Algorithm
241Koko Eating Bananas
242Kth distance
243Kth element in Matrix
244Kth Largest in a Stream
245Kth Missing Positive Number in a Sorted Array
246Kth Smallest
247Kth Smallest Number in Multiplication Table
248Largest BST
249Largest Divisible Subset
250Largest number in K swaps
251Largest number in one swap
252Largest Odd Squares with Limited 1s
253Largest Pair Sum
254Largest Rectangle with Column Swaps
255Largest square formed in a matrix
256Largest subarray of 0's and 1's
257Largest Subsquare Surrounded by X
258Largest Sum Cycle
259Largest Unblocked Submatrix
260Largest with Same Parity Adjacent Swaps
261Largest Zigzag Sequence
262Last Coin in a Game of Alternates
263Last Digit of a^b
264Last Moment Before All Ants Fall Out
265LCM Triplet
266LCS of three strings
267Left Rotate Matrix K times
268Left View of Binary Tree
269Length of Longest Cycle in a Graph
270Level Order in spiral form
271Level order traversal
272Lexicographically Largest String After Deleting K Characters
273Lexicographically smallest after removing k
274Lexicographically Smallest Rotation
275Linked List Group Reverse
276Linked List Matrix
277Linked list of strings forms a palindrome
278Longest alternating subsequence
279Longest Bitonic Subarray
280Longest Bounded-Difference Subarray
281Longest Colored Path
282Longest Common Increasing Subsequence
283Longest Common Prefix of Strings
284Longest Common Subsequence
285Longest Common Substring
286Longest Consecutive Subsequence
287Longest Increasing Path in Matrix
288Longest Increasing Subsequence
289Longest Matching in Dictionary with Removals
290Longest Palindrome in a String
291Longest Palindromic Subsequence
292Longest Path in a Directed Acyclic Graph
293Longest Periodic Proper Prefix
294Longest Possible Route in a Matrix with Hurdles
295Longest Prefix Suffix
296Longest Repeating Character Replacement
297Longest Span in two Binary Arrays
298Longest String Chain
299Longest Subarray Length
300Longest subarray with Atmost two distinct integers
301Longest Subarray with Majority Greater than K
302Longest Subarray with Sum K
303Longest Subsequence with Adjacent Diff as 1
304Longest substring with distinct characters
305Longest Substring with K Uniques
306Longest valid Parentheses
307Look and Say Pattern
308Lowest Common Ancestor in a BST
309LRU Cache
310Majority Element
311Majority Element II
312Majority Vote
313Make array elements unique
314Make Binary Tree From Linked List
315Make Matrix Beautiful
316Make Strings Equal
317Make the array beautiful
318Marks from Ranks
319Matrix Chain Multiplication
320Max Absolute Diff of Two Subarrays
321Max Adjacent Diffs Sum with 1 Replacements
322Max After m Range Increments
323Max Amount by Selling K Tickets
324Max Circular Subarray Sum
325Max DAG Edges
326Max Digit Sum Number in 1 to n
327Max distance between same elements
328Max Dot Product with 0 Insertions
329Max Gap Between Two Same
330Max min Height
331Max of min for every window size
332Max Path Sum Between Two Leaves
333Max Product Subsequence of Size K
334Max Product Subset
335Max Profit from Two Machines
336Max rectangle
337Max Score from Subarray Mins
338Max Subarray Sum by Removing At Most One
339Max sum in the configuration
340Max Sum Increasing Subsequence
341Max sum path in two arrays
342Max Sum Subarray of Size at least K
343Max Sum Subarray of size K
344Max Xor Subarray of size K
345Maximise String Score
346Maximize Array Value After Rearrangement
347Maximize median after doing k addition operation
348Maximize Number of 1's
349Maximize partitions in a String
350Maximize The Cut Segments
351Maximize the minimum difference between k elements
352Maximum Area Between Bars
353Maximum Connected group
354Maximum Difference
355Maximum Frequency with K Increments
356Maximum Height Disc Stack
357Maximum Non-Adjacent Nodes Sum
358Maximum Non-Overlapping Odd Palindrome Sum
359Maximum number of overlapping Intervals
360Maximum Number of People Defeated
361Maximum of minimum for every window size
362Maximum Packages within Time
363Maximum path sum
364Maximum path sum from any node
365Maximum People Visible in a Line
366Maximum Product Subarray
367Maximum product subset of an array
368Maximum Reachable Index Difference
369Maximum Stone Removal
370Maximum subarray sum 2
371Maximum Subarray Sum with One Square Allowed
372Maximum Subset XOR
373Maximum Sum Combination
374Maximum sum of elements not part of LIS
375Maximum sum of Non-adjacent nodes
376Maximum Sum Problem
377Maximum sum Rectangle
378Maximum Weight Transformation of String
379Maximum Without 3 Consecutive Set Bits
380Maximum XOR of two numbers in an array
381Mean of range in array
382Median in a row-wise sorted Matrix
383Median of BST
384Median of two sorted arrays
385Meeting Rooms
386Meeting Rooms III
387Merge K sorted linked lists
388Merge Sort for Linked List
389Merge two BST 's
390Merge two sorted linked lists
391Merge Without Extra Space
392Middle of a Linked List
393Min Add to Make Parentheses Valid
394Min Chars to Add for Palindrome
395Min Cost Climbing Stairs
396Min Cost To Make Two Strings Identical
397Min Edge Movements to Connect a Graph
398Min Edge Reversals for Path
399Min Product Subset
400Min Steps by Knight
401Min Swaps to Group 1s
402Minimal Cost
403Minimize Max Min Diff with Subarray Removal
404Minimize the Heights I
405Minimize the Heights II
406Minimum Absolute Difference In BST
407Minimum Cost for n Characters
408Minimum Cost of ropes
409Minimum Cost Path
410Minimum Cost Pizza Selection
411Minimum Cost Selection
412Minimum cost to connect all houses in a city
413Minimum Cost to cut a board into squares
414Minimum Cost to Cut a Stick of length N
415Minimum Cost to Fill Given Weight
416Minimum Cost to Merge Stones
417Minimum days to make M bouquets
418Minimum Deletions
419Minimum Deletions to Make Sorted
420Minimum Elements Outside Subsequences
421Minimum height roots
422Minimum Increment or Double Operations to Convert
423Minimum Insert and Delete to Convert
424Minimum Jumps
425Minimum K Consecutive Bit Flips
426Minimum Moves to Sort Permutation
427Minimum Multiplications to reach End
428Minimum number of deletions and insertions
429Minimum Number of Workers
430Minimum Operations to Connect Hospitals
431Minimum Operations to Reach n
432Minimum Platforms
433Minimum repeat to make substring
434Minimum Squares with Full Cuts
435Minimum Steps to Halve Sum
436Minimum sum
437Minimum Sum Subarray
438Minimum Time to Finish Project
439Minimum Toggle to Partition
440Minimum Weight Cycle
441Minimum Window Subsequence
442Mirror Tree
443Missing And Repeating
444Missing Element in Range
445Missing element of AP
446Missing in Array
447Mobile numeric keypad
448Modify the Array
449Mother Vertex
450Mountain Subarray Queries
451Move All Zeroes to End
452Multiply two linked lists
453Multiply two strings
454N meetings in one room
455N-Digit Numbers with Increasing Digits
456N-Queen Problem
457nCr
458Nearest multiple of 10
459Nearly sorted
460Negative Weight Cycle
461Next element with greater frequency
462Next Greater Element
463Next Greater Element in Circular Array
464Next Permutation
465Next Smallest Palindrome
466Nine Divisors
467Node and Ancestor Max Diff
468Non Repeating Character
469Non-Attacking Black and White Knights
470Non-overlapping Intervals
471Not a subset sum
472Nth in Concatenated Decimal String
473Nth Natural Number
474Number of BST From Array
475Number of distinct subsequences
476Number of occurrence
477Number of pairs
478Number of paths in a matrix with k coins
479Number of Rectangles in a Circle
480Number of Turns in Binary Tree
481Number of Ways to Arrive at Destination
482Numbers with Given Digit Sum
483Numbers Without d as Digit
484Occurence of an integer in a Linked List
485Opposite Sign Pair Reduction
486Optimal binary search tree
487Optimal File Merge
488Optimal Strategy For A Game
489Overlapping Intervals
490Painting the Fence
491Pair Sum in BST
492Pair with given sum in a sorted array
493Pairs with certain difference
494Pairs with difference k
495Pairs with Given GCD and LCM
496Pairs with Less Than K Diff
497Palindrome Linked List
498Palindrome Pairs
499Palindrome Sentence
500Palindrome SubStrings
501Palindrome with Replacement
502Parenthesis Checker
503Partition Equal Subset Sum
504Partitions with Given Difference
505Party in Town
506Pascal Triangle
507Path With Minimum Effort
508Peak element
509Perimeter of Shapes in Binary Matrix
510Permutations of a String
511Police and Thieves
512Populate Inorder Successor for all nodes
513Possible Words From Phone Digits
514Postfix Evaluation
515Postorder Traversal
516Power of k in factorial of n
517Powerful Integer
518Predecessor and Successor
519Prime List
520Prime Pair with Target Sum
521Print Anagrams Together
522Print Bracket Number
523Print Diagonally
524Print leaf nodes from preorder traversal of BST
525Product array puzzle
526Product Pair
527Pyramid Array with Reduce Operations
528Pythagorean Triplet
529Queue Reversal
530Quick Sort on Linked List
531Range GCD Queries
532Range LCM Queries
533Rat in a Maze
534Rat in a Maze Problem - I
535Rat Maze With Multiple Jumps
536Rearrange the Array
537Remaining String
538Remove all occurences of duplicates in a linked list
539Remove BST keys outside given range
540Remove Duplicates
541Remove duplicates in array
542Remove Half Nodes
543Remove Invalid Parentheses
544Remove loop in Linked List
545Remove Spaces
546Remove the balls
547Reorganize The Array
548Replace O's with X's
549Replace with XOR of Adjacent
550Reverse a Doubly Linked List
551Reverse a linked list
552Reverse an Array
553Reverse Words
554Roll Characters of String
555Roman Number to Integer
556Roof Top
557Root to leaf path sum
558Root to Leaf Paths
559Root to leaf paths sum
560Rotate a Linked List
561Rotate and delete
562Rotate Array
563Rotate by 90 degree
564Rotate Deque By K
565Rotten Oranges
566Row with max 1s
567Safe States
568Search for Subarray
569Search in a Row-Column sorted matrix
570Search in a row-wise sorted matrix
571Search in a sorted Matrix
572Search in an almost Sorted Array
573Search in fully rotated sorted 2D matrix
574Search in Rotated Sorted Array
575Search insert position of K in a sorted array
576Search Pattern (KMP-Algorithm)
577Search Pattern (Rabin-Karp Algorithm)
578Seating Arrangement
579Second Best Minimum Spanning Tree
580Second Largest
581Secret Cipher
582Segregate 0s and 1s
583Sequences where Adjacent Divide
584Serialize and deserialize a binary tree
585Set Matrix Zeroes
586Set Matrix Zeros
587Shop in Candy Store
588Shortest Common Supersequence
589Shortest Cycle
590Shortest Path From 1 To n
591Shortest Path in 1-2 Graph
592Shortest path in Undirected Graph
593Shortest Path in Weighted undirected graph
594Shortest Path Using Atmost One Curved Edge
595Shortest Safe Route in Grid
596Shortest Unique Prefix for Every Word
597Single Number
598Size of Binary Tree
599Smallest distinct window
600Smallest Divisor
601Smallest Non-Zero Number
602Smallest number
603Smallest Positive Missing
604Smallest Positive Missing Number
605Smallest range in K lists
606Smallest window containing 0, 1 and 2
607Smallest window containing all characters
608Smallest window in a string containing all the characters of another string
609Snake and Ladder Problem
610Solve the Sudoku
611Sort 0s, 1s and 2s
612Sort a k sorted doubly linked list
613Sort a linked list of 0s, 1s and 2s
614Sort by Absolute Difference
615Sort in specific order
616Sort the given array after applying the given equation
617Sort Two Parts Sorted
618Sorted and Rotated Minimum
619Sorted subsequence of size 3
620Sorting Elements of an Array by Frequency
621Spirally traversing a matrix
622Split an array into two equal Sum subarrays
623Split array in three equal sum subarrays
624Split Array into Minimum Subsets
625Split Array Largest Sum
626Split Array Subsequences
627Split Linked List Alternatingly
628Split the Array
629Square root of a number
630Stable Marriage Problem
631Stickler Thief
632Stickler Thief II
633Stock Buy and Sell – Max 2 Transactions Allowed
634Stock Buy and Sell – Max K Transactions Allowed
635Stock Buy and Sell – Max one Transaction Allowed
636Stock Buy and Sell – Multiple Transaction Allowed
637Stock Buy and Sell with Cooldown
638Stock span problem
639Stream First Non-repeating
640String stack
641Strings Rotations of Each Other
642Sub-arrays with equal number of occurences
643Subarray Frequency Count Queries
644Subarray range with given sum
645Subarrays With At Most K Distinct Integers
646Subarrays with First Element Minimum
647Subarrays with Sum in Range
648Subarrays with sum K
649Subset Sum on Generated Sequence
650Subset Sum Problem
651Subset XOR
652Subsets with MEX in Range
653Subsets with Products of Distinct Primes
654Substring with Max Zero-One Diff
655Substrings of length k with k-1 distinct elements
656Substrings with K Distinct
657Substrings with more 1's than 0's
658Substrings with same first and last characters
659Successful Binary Searches Irrespective of Pivot
660Sum of all substrings of a number
661Sum of Beauty of All Substrings
662Sum of Mode
663Sum of Nodes in BST Range
664Sum of nodes on the longest path
665Sum of Pairwise ANDs
666Sum of subarray minimum
667Sum of subarray minimums
668Sum of subarray ranges
669Sum of Subarrays
670Sum of XOR of all pairs
671Sum Pair closest to target
672Sum Tree
673Sum-string
674Summed Matrix
675Swap and Maximize
676Swap diagonals
677Swap Kth nodes from ends
678Symmetric Tree
679Target Sum
680The Celebrity Problem
681The Knight's tour problem
682The Painter's Partition Problem-II
683The Palindrome Pattern
684Toeplitz matrix
685Top K Frequent in Array
686Top View of Binary Tree
687Topological sort
688Total count
689Total Decoding Messages
690Towers Reaching Both Stations
691Trace Path
692Trail of ones
693Transform String
694Transform to Sum Tree
695Transpose of Matrix
696Trapping Rain Water
697Travelling Salesman Problem
698Tree Boundary Traversal
699Triplet Family
700Triplets with Sum in Range
701Two Equal Sum Subarrays
702Two Smallests in Every Subarray
703Two Sum - Pair with Given Sum
704Two Swaps
705Two water Jug problem
706Tywin's War Strategy
707Undirected Graph Cycle
708Union of Arrays with Duplicates
709Union of Two Sorted Arrays with Distinct Elements
710Unique K-Number Sum
711Unique Number I
712Unique Number II
713Unique Number III
714Unique Paths in a Grid
715Unoccupied Computers
716URLify a given string
717Validate an IP Address
718Values with Equal Array Remainders
719Vertical Sum
720Vertical Tree Traversal
721Vertical Width of a Binary Tree
722Visit Leaves with Budget
723Walls Coloring II
724Ways to Express as Sum of Consecutives
725Ways to Increase LCS by One
726Ways to Reach Origin
727Ways to Reach the n'th Stair
728Ways To Tile A Floor
729Ways to Tile the Floor
730Weighted Job Scheduling
731Wifi Range
732Wildcard Pattern Matching
733Word Break
734Word in Grid - All Occurrences
735Word Search
736XOR Linked List
737XOR of All Subarray XORs
738XOR Pairs less than K
739Your Social Network
740ZigZag Tree Traversal