DDSA Solutions
POTD Solutions

GeeksforGeeks Java Solutions

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

648
Solved
139
Explained

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