DDSA Solutions
POTD Solutions

GeeksforGeeks Java Solutions

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

749
Solved
239
Explained

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