DDSA Solutions
POTD Solutions

GeeksforGeeks Java Solutions

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

714
Solved
204
Explained

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