Candy LeetCode Solution

Difficulty Level Hard
Frequently asked in Adobe Amazon Apple Bloomberg Flipkart Goldman Sachs Google IBM Infosys Microsoft UberViews 1

Problem Statement:

Candy LeetCode Solution: There are n children standing in a line. Each child is assigned a rating value given in the integer array ratings.

You are giving candies to these children subjected to the following requirements:

  • Each child must have at least one candy.
  • Children with a higher rating get more candies than their neighbours.

Return the minimum number of candies you need to have to distribute the candies to the children.

Examples:

Example 1:

Input:

 ratings = [1,0,2]

Output:

 5

Explanation:

 You can allocate to the first, second and third child with 2, 1, 2 candies respectively.

Example 2:

Input:

 ratings = [1,2,2]

Output:

 4

Explanation:

You can allocate to the first, second and third child with 1, 2, 1 candies respectively.
The third child gets 1 candy because it satisfies the above two conditions.

Approach:

Idea:

We will solve the problem with a greedy approach. First, we will be iterating from left to right and will assign candies to a child based on its rating and the number of candies assigned to its previous child i.e., distribution relative to the left neighbours only. Similarly, we will iterate from right to left and will do the assignment based on the rating and the candies assigned to the previous child, i.e., distribution relative to the right neighbours only. Finally, the number of candies assigned to a child will be the maximum of the candies assigned from left to right and from right to left distribution.

So questions says distribute candies between kids in such a way that number of candies should be minimum. Now, what does that mean
We need to distribute candies to every kid in such a way that we will satisfy both of the below conditions but number of candies distributed should be minimum because to satisfy below conditions we can simply distribute 1000 1000 or 1000000 candies to each kid according to their rating, but we want to reduce number of candies as much as possible. So the conditions are:

  1. Each student should get atleast one candy
  2. The kid with higher rating should get more candies than it’s neighbours.

Solution approach

So we are using two loops here, first time we are moving from left to right, second time we are moving right to left. But why? why not only one loop we could have simply checked if rating[currentKid] > rating[currentKid – 1] && rating[currentKid] > rating[currentKid + 1]
This is because okay currentKid have greater rating than his previous and next kid but what if next kid has higher rating than his next kid or previous kid has higher rating than his previous kid ? than looping over array only ones will give wrong answers. That’s why we are looping over array of ratings twice and at a perticular time we are checking only one thing, if currentKid rating > previousKid rating and in 2nd loop we are checking if currentKid rating > nextKid rating.

Code:

Candy C++ Solution:

class Solution {
public:
    int candy(vector<int>& ratings) {
        int n = ratings.size();
        vector<int> left(n,1);
        vector<int> right(n,1);
        
        for(int i=1;i<n;i++){
            if(ratings[i]>ratings[i-1])
                left[i] = left[i-1]+1;
        }
        for(int i=n-2;i>=0;i--){
            if(ratings[i]>ratings[i+1])
                right[i] = right[i+1]+1;
        }
        int ans=0;
        for(int i=0;i<n;i++)
            ans += max(left[i],right[i]);
        return ans;
    }
};

 

Complexity Analysis of  Candy LeetCode Solution:

Top Interview Questions

S.No Interview Question Number of times has been asked
1Delete a node in doubly linked list2885
2Java program to find the number of Nodes in a Binary Tree2540
3Reverse a string without affecting special characters2522
4Palindrome using Recursion2487
5Delete a node of a linked list at given position2022
6Quick Sort1767
7Insert nodes in a linked list in a sorted way (Ascending Order)1708
8Find elements pair from array whose sum equal to number1686
9Sort Elements by Frequency of Occurrences1671
10Write a program to print all permutations of a given string1654
11Find Minimum Distance Between Two Numbers in an Array1516
12Create a Doubly Linked List1483
13Reverse an Array1461
14Smallest window in a string containing all characters of another string1435
15Recursively remove all adjacent duplicates1403
16Find a Triplet That Sum to a Given Value1391
17First Repeating Element1384
18Sum of numbers in String1361
19Arrange Even and Odd number such that Odd comes after Even1357
20Smallest Positive Number Missing in an Unsorted Array1325
21Check if the Elements of an Array are Consecutive1290
22Detect a loop in the Linked List1270
23Largest Sum Contiguous Subarray1260
24Quick Sort on SIngly Linked List1256
25Subarray with Given Sum1247
26Recursive function to do substring search1245
27Print all Possible Combinations of R Elements in a given Array of size N1245
28Find the Maximum Repeating Number in Array1203
29Find the First and Second Smallest Elements1155
30Binary Tree Level order traversal in Java1154
31Check if two linked lists are identical1140
32Maximum Subarray Sum using Divide and Conquer1139
33Remove characters from first string which are in second1112
34Find Leaders in an Array1092
35Swap nodes in the linked list1081
36Find the second most frequent character1034
37Find the Number Occurring Odd Number of Times in an Array1033
38Arrange given Numbers to Form the Biggest Number II1026
39Given a string find its first non-repeating character1014
40Find Triplet in Array With a Given Sum1002
41Given a sorted array and a number x, find the pair in array whose sum is closest to x993
42Total number of occurrences of a given item in the linked list990
43A Program to check if strings are rotations of each other or not987
44Print all possible words from phone digits970
45Find the Missing Number962
46Rearrange Positive and Negative Numbers Alternatively in Array950
47Longest Palindromic Substring944
48Segregate even and odd nodes in a linked list933
49Print Longest common subsequence924
50Transform one string to another using minimum number of given operations910
51Union and Intersection of Two Linked Lists905
52Check rearranged string can form a palindrome895
53Rearrange given Array in Maximum Minimum Form875
54Iterative Implementation of Quick Sort867
55Count Possible Triangles855
56Insertion Sort855
57Multiplication of Two Matrices840
58Count of Triplets With Sum Less than Given Value829
59Stock Buy Sell to Maximize Profit827
60Rotate a Linked List825
61Check if the linked list is palindrome823
62Concatenation of two strings803
63Tug of War795
64Print all duplicates in the input string793
65Count Number of Substrings with K Distinct Character’s793
66Find Nearest Greater and Smaller Element782
67Reverse String Without Temporary Variable775
68The Celebrity Problem771
69Remove ‘b’ and ‘ac’ from a given string768
70Find Pythagorean Triplets from Array767
71Find all Common Elements in Given Three Sorted Arrays763
72Remove all duplicates in an unsorted linked list738
73Find the Row with Maximum Number of 1’s737
74Remove Minimum Characters so that Two Strings Become Anagrams735
75Find the subarray whose sum is equal to a given number X735
76Find the Peak Element from an Array732
77Addition of Two Matrices727
78Find Smallest Missing Number in a Sorted Array724
79Generate all Binary Strings Without Consecutive 1’s722
80A Product Array Puzzle720
81Maximum Sum of Non Consecutive Elements709
82Implement Two Stacks in an Array702
83Lexicographic rank of string695
84Check if Two given Matrices are Identical689
85Maximum Product Subarray II686
86Multiplication of Previous and Next680
87Subtraction of Two Matrices672
88Merge K Sorted Arrays and Print Sorted Output667
89Move All the Zeros to the End of the Given Array664
90Online Algorithm for Checking Palindrome in a Stream663
91Form Minimum Number from Given Sequence of D’s and I’s663
92Divide a string in N equal parts660
93Check whether two strings are anagram of each other655
94Remove recurring digits in a given number652
95Sort a stack using a temporary stack651
96Maximum Circular Subarray Sum647
97Sort a linked list that is sorted alternating ascending and descending642
98Subarray and Subsequence638
99Find the Minimum Element in a Sorted and Rotated Array636
100First Circular Tour to Visit all the Petrol Bunks632
101Move last element of the Linked List at first place631
1023Sum Leetcode Solution627
103Largest Subarray with Equal Number of 0’s and 1’s626
104Compare two strings(linked lists)626
105Maximum Element in an Array which is Increasing and then Decreasing626
106Palindrome Permutations of a String624
107Flattening a linked list621
108Palindromes in a given range619
109Run length encoding615
110Majority Element615
111Pangram Checking615
112Minimum insertions to form a shortest palindrome614
113Print all permutations with repetition612
114Elements Appear more than N/K times in Array610
115Minimum Characters to be Added at Front to Make String Palindrome606
116Most repeating character in a string604
117Two Sum Leetcode Solution601
118Rotate string to get lexicographically minimum string599
119Repeated Subsequence of Length Two or More596
120Remove all duplicates in a sorted linked list593
121Rearrange a given linked list in-place593
122Minimum number of Merge Operations to make an Array Palindrome592
123Merge a linked list into another at alternate positions591
124Print all anagrams together in a sequence of words585
125Reorder an Array According to the Given Indexes579
126Pancake Sorting Problem576
127Merge Overlapping Intervals II566
128Clone a Linked List with next and random pointer565
129Transpose of a Matrix564
130Remove Extra Spaces from a String563
131Remove duplicates from a string562
132Smallest Palindrome after Replacement562
133Longest Palindrome can be Formed by Removing or Rearranging Characters561
134Maximum Sum Increasing Subsequence559
135Removing Spaces from a String using stringstream557
136Size of The Subarray With Maximum Sum555
137Check if a given string is a rotation of a palindrome555
138Partition Problem551
139Check whether Strings are K Distance Apart or Not544
140Generate all Binary Strings from Given Pattern543
141Length of Longest valid Substring540
142Delete Last Occurrence536
143Check if Two given Strings are Isomorphic to each other535
144Find Zeros to be Flipped so that Number of Consecutive 1’s is Maximized533
145Insert Node in the Sorted Linked List531
146Maximum difference between two elements such as larger element comes after smaller525
147Program to Toggle all Characters in a String524
148Given string is interleaving of two other strings or not521
149Count Minimum Steps to Get the given Array514
150Number of Smaller Elements on Right Side512
151Check length of a String is Equal to the Number Appended at its Last511
152Check if all Rows of a Matrix are Circular Rotations of Each Other507
153Find Pair with Given Difference505
154Longest Common Prefix using Divide and Conquer504
155Merge sort better than quick sort for linked lists503
156Compare Two Version Numbers496
157Print all interleavings of given two strings495
158Print Reverse of a string (Recursion)495
159Find nth node of the Linked list from the end494
160Median of Two Sorted Arrays LeetCode Solution494
161Find a Fixed Point in a Given Array493
162Sort 0s 1s and 2s in an Array493
163Reorder Array Using Given Indexes491
164Reverse words in a given string489
165Merge two sorted linked lists such that merged list is in reverse order482
166Find the Subarray of given length with Least Average481
167Split linked list using alternate nodes480
168Print all Palindromic Partitions of a String473
169Print string of odd length in ‘X’ format470
170Find Element Using Binary Search in Sorted Array468
171Find K Length Subarray of Maximum Average464
172Swap Kth Node from beginning with Kth Node from End461
173Find Duplicates in an Array in Most Efficient Way457
174Shortest Superstring Problem452
175print all palindromic partitions451
176Check if String Follows Order of Characters by a Pattern or not450
177Maximum Length of Chain Pairs448
178Sort a String According to Another String446
179Flatten a multilevel linked list444
180Sorting a K Sorted Array438
181Program to add two binary digits431
182Find a Sorted Subsequence of size 3426
183Caesar Cipher425
184Recursively print all the sentences that can be formed from list of word lists425
185Longest Span with same Sum in two Binary Arrays II424
186Longest Common Prefix Using Binary Search II423
187Reverse a Linked List in groups422
188Find the two Numbers with Odd Occurrences in an Unsorted Array422
189Kth Non-repeating Character420
190Reverse a Singly Linked List (Iterative/Non-Recursive)416
191Check if String can Become Empty by Recursively Deleting given Substring413
192Longest Common Prefix Word by Word Matching407
193Rearrange a linked list in Zig-Zag407
194Rotate Image by 90 degrees405
195Perfect Reversible String403
196Permutations of a Given String Using STL403
197Pancake Sorting401
1981`s and 2`s complement of binary number398
199Merging Two Sorted Arrays398
200Find First non-repeating character in a string398
201Increasing Subsequence of Length three with Maximum Product394
202Maximum occurring character in a string391
203List items containing all characters of a given word391
204Find the point where a monotonically increasing function becomes positive first time390
205Four Elements that Sum to Given388
206Sort a linked list with 0s, 1s and 2s388
207Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes386
208Longest Common Prefix using Character by Character Matching385
209Palindrome string (number)382
210Count Number of Occurrences in a Sorted Array382
211Valid Parentheses LeetCode Solution379
212Delete N nodes after M376
213Minimum Characters to be Removed to Make a Binary String Alternate376
214Split a string373
215Even Substring Count372
216Convert a String that is Repetition of a Substring of Length K369
217Recursive Implementation of atoi()369
218Sorting the array of strings369
219Check if a Linked list of Strings form a Palindrome363
220Print Shortest Path to Print a String on Screen363
221Maximum Subarray Leetcode Solution361
222Convert string1 to string2 in one edit359
223Reverse a String using Stack358
224Print All Distinct Elements of the Array358
225Reverse a singly linked list recursively356
226Matrix Chain Multiplication using Dynamic Programming356
227Find the first Repeating Number in a Given Array356
228wildcard character matching355
229Nth Character in Concatenated Decimal String355
230Count the number of words352
231Lower Case To Upper Case352
232Can we reverse a linked list in less than O(n) time ?349
233Binary Tree to Doubly linked list345
234Sort Elements by Frequency II344
235Find the Lost Element From a Duplicated Array343
236Longest Common Subsequence with Permutations343
237Merge Two Sorted Arrays339
238Split Four Distinct Strings339
239Find middle of the Linked List335
240Count the Pairs at Same Distance as in English Alphabets334
241Roman to Integer Leetcode Solution334
242Toeplitz Matrix327
243Palindrome Permutation325
244Word Search Leetcode Solution324
245Next Greater Element in an Array323
246Move all negative elements to one side of array322
247N queen problem321
248Sudoku Solver315
249Find All Pairs With a Given Difference315
250Find Nth Node314
251First non Repeating Element313
252Number of Islands LeetCode Solution313
253Count Pairs With Given Sum312
254Reverse Bits312
255Print all Possible Ways to Break a String in Bracket Form312
256String(represents an integer) to value312
257Searching a node in a Binary Search Tree312
258Reverse a String311
259Triplet from three linked lists with given sum311
260Types of Binary Tree310
261Repeated Substring Pattern309
262How to Efficiently Implement k Stacks in a Single Array?308
263Change Gender of a given String308
264Delete a Tree308
265Meeting Rooms II LeetCode Solution307
266Sort an array of strings305
267Number of sub-strings which recursively add up to 9305
268Fibonacci numbers304
269Longest Palindromic Substring LeetCode Solution304
270Reverse a linked list303
271Delete a node under given conditions303
272Min Stack303
273Most Frequent Element in an Array302
274Cuckoo sequence program301
275Dijkstra Algorithm300
276Longest Common Extension297
277House Robber Leetcode Solution296
278Binary Tree296
279Max stack294
280Remove spaces from a string294
281Best Time to Buy and Sell Stock  II Leetcode Solution293
282Subset Leetcode290
283Shuffle a given Array289
284Word Search289
285KMP Algorithm287
286Expression Evaluation286
287Remove middle points in a linked list of line segments286
288Plus One Leetcode Solution286
289Minimize the maximum difference between the heights286
290Find, second, frequent, character286
291Combination Sum Leetcode Solution284
292Number Of 1 bits282
293Search Insert Position Leetcode Solution281
294Reverse words in a string281
295Evaluation of Postfix Expression280
296Pair of Positive Negative Values in an Array280
297Set Matrix Zeroes278
298Sliding Window Technique276
299Valid Palindrome Leetcode Solution276
300Min Stack Leetcode Solution276
301Subarray with 0 sum273
302Backspace String Compare273
303Rabin Karp Algorithm273
304Sort linked which is sorted on absolute values272
305Common elements in all rows of a given matrix271
306Clone a linked list with next and random pointer (Hashing)270
307Merge Sorted Arrays Leetcode Solution268
308Sqrt(x) Leetcode Solution268
309Delete middle element of a stack267
310Reversing a Queue266
311Implementation of Deque using Doubly Linked List264
312Contains Duplicate II Leetcode Solution264
313Count of index pairs with equal elements in an array263
314Product of array except self263
315Contains Duplicate262
316Tower Of Hanoi262
317Combination Sum262
318Intersection of Two Arrays II Leetcode Solution261
319String Compression261
320Pascal Triangle Leetcode261
321Count subarrays with equal number of 1’s and 0’s261
322Find Top K (or Most Frequent) Numbers in a Stream260
323Minimum swaps required to bring all elements less than or equal to k together260
324Integer to Roman Leetcode Solution259
325Reverse individual words259
326Count Odd Numbers in an Interval Range Leetcode Solution259
327Page Replacement Algorithms in Operating Systems258
328How to Delete a Linked List258
329Postfix to Infix Conversion256
330Group Words With Same Set of Characters256
331Segregate even and odd numbers256
332Single Number Leetcode Solution255
333Find sum of non-repeating elements (distinct) elements in an array255
334Sort elements by frequency255
335Minimum Value to Get Positive Step by Step Sum Leetcode Solution255
336Second Most Repeated Word in a Sequence255
337Bellman Ford Algorithm254
338Arithmetic Expression Evaluation254
339Sum of minimum and maximum elements of all subarrays of size k254
340Sorting array using Stacks254
341Smallest Subarray with k Distinct Numbers254
342Add Binary Leetcode Solution253
343Top K Frequent Words252
344K-th Smallest Element in a Sorted Matrix252
345Next Permutation252
346Construct Binary Tree from Given Inorder and Preorder Traversals251
347Minimum operation to make all elements equal in array251
348Pow(x, n) Leetcode Solution251
349Maximum Number of Balloons Leetcode Solution250
350Longest Common Prefix Leetcode Solution249
351Palindrome Linked List Leetcode Solution249
352Given two unsorted arrays find all pairs whose sum is x248
353First element occurring k times in an array248
354Count subarrays having total distinct elements same as original array248
355Convex Hull Algorithm248
356Design a stack that supports getMin() in O(1) time and O(1) extra space247
357Scramble String247
358Kruskal Algorithm247
359Special Number246
360Longest Substring Without Repeating Characters LeetCode Solution246
361Majority Element Leetcode Solution245
362Find Lucky Integer in an Array Leetcode Solution245
363Evaluate Division245
364Third Maximum Number Leetcode Solution244
365Permutations Leetcode Solution244
366Reversing the First K elements of a Queue243
367Reverse a Number Using Stack242
368Running Sum of 1d Array Leetcode Solution242
369Number of Good Pairs Leetcode Solution242
370Find duplicates in a given array when elements are not limited to a range242
371Spiral Matrix LeetCode Solution242
372Prefix to Infix Conversion241
373Fizz Buzz Leetcode241
374Maximum possible difference of two subsets of an array241
375Minimum Path Sum240
376Find Numbers with Even Number of Digits Leetcode Solution240
377Huffman Coding240
378Check if two arrays are equal or not240
379Find the Town Judge Leetcode Solution240
380Maximum Subarray240
381Swap Nodes in Pairs Leetcode Solutions239
382Maximal Square239
383Group Anagrams239
384Count Primes Leetcode Solutions238
385Unique Paths238
386Single Number238
387Maximum Distance Between two Occurrences of Same Element in Array238
388Find the Closest Palindrome number238
389Minimum Absolute Difference Leetcode Solution238
390Minimum Steps to reach target by a Knight238
391Sort Array by Increasing Frequency Leetcode Solution237
392Smallest Element Repeated Exactly K Times236
393Find top three repeated in array236
394Palindrome Substring Queries236
395Range Sum Query 2D – Immutable Leetcode Solution236
396Convert String To Int235
397Find the first repeating element in an array of integers235
398Pascal’s Triangle II Leetcode Solution235
399Zigzag Conversion235
400Sorting using trivial hash function235
401Remove Minimum Number of Elements Such That no Common Element Exist in both Array234
402Implement Stack and Queue using Deque234
403Leetcode Permutations233
404Cumulative Frequency of Count of Each Element in an Unsorted Array233
405Power of Two Leetcode Solution233
406Find Number of Employees Under every Employee233
407Matrix Diagonal Sum Leetcode Solution233
408Merge Two Sorted Lists Leetcode Solutions233
409Missing Number Leetcode Solution233
410Letter Case Permutation232
411Length of the largest subarray with contiguous elements232
412Check if Array Contains Contiguous Integers With Duplicates Allowed232
413Find All Numbers Disappeared in an Array Leetcode Solution232
414Sort Integers by The Number of 1 Bit Leetcode Solution232
415How to Implement Stack Using Priority Queue or Heap?232
416Expression Contains Redundant Bracket or Not232
417Print All Distinct Elements of a Given Integer Array231
418Count Substrings with equal number of 0s, 1s and 2s231
419Bipartite Graph231
420LRU Cache Implementation231
421Happy Number Leetcode Solution231
422Print all subarrays with 0 sum231
423Top K Frequent Elements231
424House Robber II Leetcode Solution231
425Fizz Buzz230
426Maximum Depth of Binary Tree Leetcode Solution230
427Decode String230
428Difference between highest and least frequencies in an array230
429Subset sum problem229
430Find all pairs (a, b) in an array such that a % b = k229
431Search in Rotated Sorted Array Leetcode Solution229
432Unique Paths Leetcode Solution229
433Prim’s Algorithm229
434Sort a stack using recursion229
435Subarray Sum Equals k229
436Count pairs from two linked lists whose sum is equal to a given value228
437Average Salary Excluding the Minimum and Maximum Salary Leetcode Solution228
438Coin Change 2 Leetcode Solution228
439Find Winner on a Tic Tac Toe Game Leetcode Solution228
440Reverse Integer227
441Capacity To Ship Packages Within D Days Leetcode Solution227
442Check If N and Its Double Exist Leetcode Solution227
443Nth Catalan Number227
444Max Consecutive Ones Leetcode Solution227
445Find Median from data Stream227
446How Many Numbers Are Smaller Than the Current Number Leetcode Solution226
447Trapping Rain Water Leetcode Solution225
448Monotonic Array LeetCode Solution225
449Integer to English words225
450Find elements which are present in first array and not in second225
451Reverse a Stack Using Recursion225
452Find The Duplicate Number224
453Find the Difference Leetcode Solution224
454Text Justification LeetCode Solution224
455Subarrays with distinct elements223
456Pair with given product223
457Priority Queue Using Singly Linked List223
458Fibonacci Number LeetCode Solution223
459Subarray Sum Equals K LeetCode Solution223
460Subtract the Product and Sum of Digits of an Integer Leetcode Solution223
461Edit Distance223
462Reverse Vowels of a String Leetcode Solution223
463Sum of Subarray Ranges Leetcode Solution223
464Valid Parenthesis String223
465Target Sum223
466Best Time to Buy and Sell Stock III Leetcode Solution222
467Floyd Warshall Algorithm222
468Find Index of Closing Bracket for a Given Opening Bracket in an Expression222
469Count and Say221
470Reverse a String221
471Find distinct elements common to all rows of a matrix221
472Longest subarray not having more than K distinct elements221
473Remove Duplicates from Sorted Array Leetcode Solution221
474Kth largest element in an Array Leetcode Solutions221
475Generate a String With Characters That Have Odd Counts Leetcode Solution220
476Design Parking System Leetcode Solution220
477The K Weakest Rows in a Matrix Leetcode Solution220
478Longest Common Prefix using Trie220
479Jump Game Leetcode Solution219
480Find Common Characters Leetcode Solution219
481Delete a Node from linked list without head pointer219
482Find four elements that sum to a given value (Hashmap)219
483Intersection of Two Arrays219
484Find Minimum In Rotated Sorted Array219
485Multiply Strings Leetcode Solution218
486Count number of triplets with product equal to given number218
487Find any one of the multiple repeating elements in read only array218
488Rearrange a binary string as alternate x and y occurrences218
489Shuffle String Leetcode Solution218
490Check if a given array contains duplicate elements within k distance from each other218
491Top View of Binary Tree218
492Prefix to Postfix Conversion218
493MiniMax Algorithm218
494Flood Fill LeetCode218
495Shortest Palindrome217
496Find missing elements of a range217
497The Stock Span Problem217
498Word Ladder LeetCode Solution217
499Substring With Concatenation Of All Words217
500Shuffle the Array Leetcode Solution217
501Excel Sheet Column Number Leetcode Solution216
502Balanced Binary Tree Leetcode Solution216
503Iterative Inorder Traversal of a Binary Tree216
504Next Greater Element I Leetcode Solution216
505Iterative Tower of Hanoi216
506Priority Queue in C++215
507Best Time to Buy and Sell Stock LeetCode Solution215
508Number of Steps to Reduce a Number to Zero Leetcode Solution215
509Find subarray with given sum (Handles Negative Numbers)215
510Sorting a Queue without Extra Space215
511Merge Two Sorted Linked Lists215
512Move Zeroes LeetCode Solution215
513Degree of an array215
514Concatenation of Array LeetCode Solution214
515Longest Common Subsequence214
516Kids With the Greatest Number of Candies Leetcode Solution214
517Longest Common Prefix using Sorting214
518Check for Balanced Parentheses in an Expression214
519Valid Sudoku214
520Count and Say Leetcode Solution214
521Reverse Words in a String III LeetCode Solution214
522Recursion214
523Peak Index in a Mountain Array214
524Slowest Key Leetcode Solution214
525K-th Distinct Element in an Array214
526Implement a stack using single queue213
527Minimum Knight Moves LeetCode Solution213
528Kth Largest Element in a Stream Leetcode Solution213
529Implement Stack using Queues213
530Find First and Last Position of Element in Sorted Array Leetcode Solution213
531Max Area of Island213
532Merge Overlapping Intervals213
533Find the Duplicate Element213
534Minimum Delete Operations to make all Elements of Array Same212
535Minimum Moves to Equal Array Elements Leetcode Solution212
536Largest Sum Contiguous Subarray212
537Sum of Left Leaves Leetcode Solutions212
538Jewels and Stones Leetcode Solution212
539Gold Mine Problem211
540Container with Most Water211
541Postfix to Prefix Conversion211
542Largest Perimeter Triangle Leetcode Solution211
543Convert array into Zig-Zag fashion211
544Next Greater Frequency Element210
545Change the Array into Permutation of Numbers From 1 to N210
546Excel Sheet Column Title Leetcode Solution210
547Find N Unique Integers Sum up to Zero Leetcode Solution210
548Is Subsequence Leetcode Solution210
549Smallest Subarray With all Occurrences of a Most Frequent Element210
550Isomorphic Strings Leetcode Solution210
551Delete Node in a Linked List Leetcode Solution209
552Last Stone Weight209
553Minimum Bracket Reversals209
554Arrange given numbers to form the biggest number209
555Hamming Distance209
556Contiguous Array Leetcode209
557Linked List Cycle II LeetCode Solution209
558Zigzag Conversion LeetCode Solution209
559Distribute Candies to People Leetcode Solution208
560Summary Ranges Leetcode Solution208
561Check if a queue can be sorted into another queue using a stack208
562Mobile Numeric Keypad Problem208
563How to check if two given sets are disjoint?208
564Koko Eating Bananas Leetcode Solution208
565Group Multiple Occurrence of Array Elements Ordered by first Occurrence208
566Shuffle an Array208
567Maximum sum rectangle in a 2D matrix208
568Find Sum of all unique sub-array sum for a given array208
569Count the number of nodes at given level in a tree using BFS207
570Convert a normal BST to Balanced BST207
571Minimum Cost to Hire K Workers207
572Length of Last Word Leetcode Solution206
5733Sum Closest LeetCode Solution206
574Best Time to Buy and Sell Stock206
575Shortest Path in a Grid with Obstacles Elimination LeetCode Solution206
576Implementation of Deque using circular array206
577Maximum Distance in Array205
578Find Words That Can Be Formed by Characters Leetcode Solution205
579Valid Anagrams205
580Integer to Roman205
581Rotate Image LeetCode Solution205
582Number of Provinces Leetcode Solution205
583First negative integer in every window of size k205
584Maximum path sum in a triangle205
585Trapping Rain Water LeetCode Solution205
586Frog Jump Leetcode Solution204
587Minimum number of subsets with distinct elements204
588N-th Tribonacci Number Leetcode Solution204
589LRU Cache LeetCode Solution204
590Find if an Expression has Duplicate Parenthesis or Not204
591Decode Ways204
59201 Matrix LeetCode Solution204
593Remove All Occurrences of a Substring LeetCode Solution204
594Relative Sort Array Leetcode Solution204
595Word Pattern203
596Combinations Leetcode Solution203
597Build Array From Permutation Leetcode Solution203
598Longest Increasing Subsequence203
599Delete consecutive same words in a sequence203
600Island Perimeter Leetcode Solution203
601Minimum Operations to convert X to Y203
602Maximum Number of Occurrences of a Substring Leetcode Solution203
603Sieve of Eratosthenes202
604Sort Characters By Frequency LeetCode Solution202
605Iterative Method to find Height of Binary Tree202
606Minimum number of distinct elements after removing m items202
607Maximum difference between first and last indexes of an element in array202
608Smallest Good Base201
609GCD Of Two Numbers201
610Insert Interval Leetcode Solution201
611Non-overlapping sum of two sets201
612Remove Linked List Elements Leetcode Solution201
613Assign Cookies Leetcode Solution201
614Convert Sorted Array to Binary Search Tree Leetcode Solution201
615Find the smallest positive integer value that cannot be represented as sum of any subset of a given array200
616Tracking current Maximum Element in a Stack200
617Minimum insertions to form a palindrome with permutations allowed200
618Rotate List Leetcode Solution200
619XOR Operation in an Array Leetcode Solution200
620The Knapsack Problem200
621Find minimum difference between any two elements200
622Same Tree LeetCode Solution200
623Count all subsequences having product less than K200
624Permutation in String Leetcode Solution199
625Find the Duplicate Number LeetCode Solution199
626Reverse a stack without using extra space in O(n)199
627Sum of f(a[i], a[j]) over all pairs in an array of n integers199
628Bubble sort using two Stacks199
629Count Good Nodes in Binary Tree Leetcode Solution198
630Stone Game LeetCode198
631Printing brackets in Matrix Chain Multiplication Problem198
632Applications of Breadth First Search and Depth First Search198
633Unique Binary Search Trees198
634Isomorphic Strings198
635Optimal Account Balancing LeetCode Solution198
636Distance Between Bus Stops Leetcode Solution197
637Letter Combinations of a Phone Number197
638Defanging an IP Address Leetcode Solution197
639Maximum Number of Chocolates to be Distributed Equally Among k Students197
640Reorganize String197
641Longest Increasing Path in a Matrix LeetCode Solution197
642How to Create Mergable Stack?197
643Find Largest d in Array such that a + b + c = d196
644Employee Free Time LeetCode Solution196
645First Unique Character in a String LeetCode Solution196
646K Empty Slots196
647Replace Elements with Greatest Element on Right Side Leetcode Solution196
648Minimum Number of Steps to Make Two Strings Anagram Leetcode Solutions196
649Robot Room Cleaner Leetcode Solution195
650Minimum Depth of Binary Tree Leetcode Solution195
651Reducing Dishes LeetCode Solution195
652Strobogrammatic Number LeetCode Solution195
653House Robber195
654Convert an array to reduced form195
655Find pairs with given sum such that elements of pair are in different rows195
656Word Wrap Problem195
657License Key Formatting Leetcode Solution195
658Form minimum number from given sequence195
659Check If It Is a Straight Line Leetcode Solution195
660Sort Array By Parity LeetCode Solution195
661Painting Fence Algorithm194
662Find Pair with Greatest Product in Array194
663Kth Missing Positive Number Leetcode Solution194
664Merge Two Balanced Binary Search Trees194
665Longest Substring with At Least K Repeating Characters LeetCode Solution193
666Partition Labels LeetCode Solution193
667Valid Palindrome II Leetcode Solution193
668K Empty Slots LeetCode193
669Maximum Product of Two Elements in an Array Leetcode Solution193
670Convert a Number to Hexadecimal Leetcode Solution192
671Power of Four Leetcode Solution192
672Longest Substring with At Most K Distinct Characters LeetCode Solution192
673Moving Average from Data Stream Leetcode Solution192
674String to Integer (atoi) LeetCode Solution192
675Segregate 0s and 1s in an Array192
676Maximum Consecutive Numbers Present in an Array192
677Print the Fibonacci numbers in reverse order192
678Valid Palindrome191
679Reservoir Sampling191
680Find Maximum Level sum in Binary Tree191
681Dynamic Programming Basics191
682Coin Change Problem191
683Queries for counts of array elements with values in given range191
684Distance of nearest cell having 1 in a binary matrix191
685Remove Invalid Parentheses Leetcode Solution191
686Largest subarray with equal number of 0s and 1s191
687Partition Array Into Three Parts With Equal Sum Leetcode Solution191
688Find the Distance Value Between Two Arrays Leetcode Solution191
689Best Time to Buy and Sell Stock with Cooldown Leetcode Solution191
690Check If Two String Arrays are Equivalent Leetcode Solution191
691Binary Tree Maximum Path Sum LeetCode Solution190
692Sum of All Odd Length Subarrays Leetcode Solution190
693Longest Span with same Sum in two Binary arrays190
694To Lower Case Leetcode Solution190
695Wiggle Sort190
696Inorder Successor of a node in Binary Tree190
697Find the largest multiple of 3190
698Find unique character in a string190
699Path With Maximum Minimum Value LeetCode Solution190
700String Compression LeetCode Solution190
701Find Leaves of Binary Tree LeetCode Solution190
702Find the node with minimum value in a Binary Search Tree190
703Flipping an Image LeetCode Solution190
704Maximum Number of Coins You Can Get Leetcode Solution189
705Find the Smallest Divisor given a Threshold Leetcode Solution189
706Number of Dice Rolls With Target Sum LeetCode Solution189
707Increasing Decreasing String Leetcode Solution189
708Numbers with prime frequencies greater than or equal to k189
709Unique Paths II Leetcode Solution189
710Reverse Only Letters LeetCode Solution188
711Sort Array by Increasing Frequency Leetcode Solution188
712Non-decreasing Array LeetCode Solution188
713Race Car LeetCode Solution188
714Generate all possible sorted arrays from alternate elements of two given sorted arrays188
715Rearrange an array in order – smallest, largest, 2nd smallest, 2nd largest188
716Permutation Sequence LeetCode Solution188
717Edit Distance LeetCode Solution188
718Valid Perfect Square Leetcode Solution187
719Lucky Numbers in a Matrix Leetcode Solution187
720Majority Element II Leetcode Solution187
721Find Maximum Depth of Nested Parenthesis in a String187
722Reversing a Queue using Recursion187
723Delete Nth node from the end of the given linked list187
724Subset Sum Leetcode187
725BFS vs DFS for Binary Tree187
726Bulb Switcher LeetCode Solution187
727Factorial Trailing Zeroes Leetcode Solution187
728Count quadruples from four sorted arrays whose sum is equal to a given value x186
729Keyboard Row Leetcode Solution186
730Merge Two Binary Trees LeetCode Solution186
731Find whether an array is subset of another array186
732Maximum Length of a Concatenated String with Unique Characters Leetcode Solution186
733Snakes and Ladders LeetCode Solution186
734Binary Tree Zigzag Level Order Traversal LeetCode Solution186
735Recover Binary Search Tree186
736Ugly Number Leetcode Solution186
737Stack Permutations (Check if an array is stack permutation of other)186
738Valid Number185
739Number of NGEs to the Right185
740Elements to be added so that all elements of a range are present in array185
741Print a Binary Tree in Vertical Order185
742Finding K closest element185
743Rank Transform of an Array Leetcode Solution185
744Best Meeting Point LeetCode Solution185
745Maximum 69 Number Leetcode Solution185
746Populating Next Right Pointers in Each Node185
747Morris Traversal185
748Base 7 Leetcode Solution185
749Perform String Shifts Leetcode185
750Tiling Problem185
751Find if Path Exists in Graph Leetcode Solution185
752Restore IP Addresses Leetcode Solution185
753k-th missing element in increasing sequence which is not present in a given sequence185
754Find Median from Data Stream LeetCode Solution185
755Remove brackets from an algebraic string containing + and – operators185
756Minimum Cost For Tickets Leetcode Solution185
757Word Pattern LeetCode Solution184
758Check If Array Pairs Are Divisible by k LeetCode Solution184
759Partition to K Equal Sum Subsets Leetcode Solution184
760Check if Two Expressions With Brackets are Same184
761String comparison containing wildcards184
762Merge Sorted Array LeetCode Solution184
763Evaluate Reverse Polish Notation LeetCode Solution184
764Student Attendance Record I Leetcode Solution184
765Find All Duplicates in an Array LeetCode Solution184
766Maximum Profit in Job Scheduling Leetcode Solution184
767Binary Tree zigzag level order Traversal184
768N-Queens LeetCode Solution184
769Split a String in Balanced Strings Leetcode Solution184
770Construct BST from given Preorder Traversal183
771Special Array With X Elements Greater Than or Equal X Leetcode Solution183
772Find All Possible Recipes from Given Supplies LeetCode Solution183
773Symmetric Tree Leetcode Solution183
774Minimum Distance Between BST Nodes Leetcode Solution183
775Regular Expression Matching183
776Maximum Product of Three Numbers LeetCode Solution183
777Brick Wall LeetCode Solution183
778Add and Search Word – Data structure design LeetCode183
779Program for Bridge and Torch problem183
780Binomial Coefficient183
781Decompress Run-Length Encoded List Leetcode Solution182
782Largest Rectangle in Histogram LeetCode Solution182
783Minimize Maximum Pair Sum in Array LeetCode Solution182
784Minimum Height Trees182
785Hamming Distance Leetcode Solution182
786Merge K Sorted Linked Lists182
787Growable array based stack182
788Maximum Depth of N-ary Tree Leetcode Solution182
789Total Numbers With no Repeated Digits in a Range181
790Search in a Binary Search Tree Leetcode Solution181
791Jump Game181
792Final Prices With a Special Discount in a Shop Leetcode Solution181
793Sorted Linked List to Balanced BST181
794Balanced Expression with Replacement181
795Kth ancestor of a node in binary tree181
796Top K Frequent Words LeetCode Solution181
797Priority Queue using doubly linked list181
798Form Minimum Number From Given Sequence181
799Invert Binary Tree LeetCode Solution181
800Minimum Time Visiting All Points Leetcode Solution180
801Diagonal Traversal of Binary Tree180
802Identify and Mark Unmatched Parenthesis in an Expression180
803One Edit Distance LeetCode Solution180
804Minimum sum of multiplications of n numbers180
805Maximum Nesting Depth of the Parentheses Leetcode Solution180
806Number of Days Between Two Dates LeetCode Solution180
807Maximum Difference Between Increasing Elements LeetCode Solution180
808Largest rectangular sub-matrix whose sum is 0180
809Check for Palindrome after every character replacement Query180
810Sort an array according to the order defined by another array180
811Nested List Weight Sum II LeetCode Solution180
812Repeated Substring Pattern LeetCode Solution180
813Queue based approach for first non-repeating character in a stream179
814Count items common to both the lists but with different prices179
815Nearest Exit from Entrance in Maze LeetCode Solution179
816Relative Ranks Leetcode Solution179
817Minimum Swaps to Make Strings Equal Leetcode Solution179
818Linked List Cycle179
819Transpose Graph179
820Crawler Log Folder Leetcode Solution179
821Number of Islands II LeetCode Solution179
822Deletion in a Binary Tree179
823Search a 2D Matrix II Leetcode Solution179
824Circular Queue179
825Remove Nth Node From End of List Leetcode Solution179
826Longest Subarray Having Count of 1s One More than Count of 0s179
827Minimum Number of Taps to Open to Water a Garden LeetCode Solution178
828Subarray Product Less Than K LeetCode Solution178
829Given an Array of Pairs Find all Symmetric Pairs in it178
830Clone Graph LeetCode Solution178
831Course Schedule II – LeetCode178
832LCS (Longest Common Subsequence) of three strings177
833Distinct adjacent elements in an array177
834Minimum time required to rot all oranges177
835Binary Search Tree Search and Insertion177
836Check if an Array is Stack Sortable177
837Number Complement Leetcode Solution177
838Permutation Coefficient177
839A Space Optimized DP solution for 0-1 Knapsack Problem177
840Make Two Arrays Equal by Reversing Sub-arrays Leetcode Solution177
841Check if stack elements are pairwise consecutive177
842Sort Array By Parity II Leetcode Solution177
843Minimum Jumps to Reach Home LeetCode Solution177
844Minimum number of jumps to reach end176
845Reorder Data in Log Files LeetCode Solution176
846Missing Element in Sorted Array LeetCode Solution176
847Last Stone Weight II LeetCode Solution176
848Split Array Into Consecutive Subsequences176
849Design Browser History LeetCode Solution176
850Check if a given array can represent Preorder Traversal of Binary Search Tree176
851Sliding Window Maximum176
852Find Maximum Sum Possible Equal Sum of Three Stacks175
853Lemonade Change Leetcode Solution175
854Remove Duplicates from Sorted List LeetCode Solution175
855Insert Delete GetRandom175
856Interval Tree175
857Alien Dictionary LeetCode Solution175
858Iterative Preorder Traversal175
859Partition List Leetcode Solution175
860Valid Parentheses Leetcode Solution175
861Remove Duplicates from Sorted List II175
862Tree Traversal (Preorder, Inorder & Postorder)175
863Minimum Sum Path in a Triangle175
864Design a Stack With Increment Operation Leetcode Solution174
865Dividing Array into Pairs With Sum Divisible by K174
866Subsequence of Size K With the Largest Even Sum LeetCode Solution174
867Count Distinct Elements in Every Window of Size K174
868Union and Intersection of two Linked Lists174
869Can Place Flowers LeetCode Solution174
870Daily Temperatures Leetcode Solution174
871Spiral Matrix II Leetcode Solution174
872Water Bottles Leetcode Solution173
873Brightest Position on Street LeetCode Solution173
874Level order Traversal in Spiral Form173
875Sum of nearest smaller and greater number173
876Maximum size subarray sum equals k173
877Get Maximum in Generated Array Leetcode Solution173
878Least Number of Unique Integers after K Removals Leetcode Solution173
879Minimum swaps to make sequences increasing173
880Minesweeper LeetCode Solution173
881Infix to Postfix173
882Find the Only Repetitive Element Between 1 to N-1173
883Diagonal Traverse LeetCode Solution173
884Guess Number Higher or Lower LeetCode Solution173
885An Interesting Method to generate Binary Numbers from 1 to n173
886Find all permuted rows of a given row in a matrix173
887Sorted Array to Balanced BST173
888Subset with sum divisible by m173
889Convert BST to Min Heap173
890Count ways to reach the nth stair using step 1, 2 or 3172
891Lexicographical Numbers Leetcode Solution172
892Find all triplets with zero sum172
893GCDs of given index ranges in an array172
894Guess Number Higher or Lower II172
895Sequences of given length where every element is more than or equal to twice of previous172
896Rearrange Array such that arr[i] >= arr[j] if i is even and arr[i] <= arr[j] if i is odd and j < i172
897Pattern Occurrences using Stack172
898Sort Colors172
899Friends Pairing Problem172
900Check whether a given Binary Tree is Complete or not172
901Find the subarray with least average172
902Delete And Earn172
903Best Time to Buy and Sell Stock with Transaction Fee Leetcode Solution171
904Insert into a Binary Search Tree Leetcode Solution171
905Averages of Levels in Binary Tree171
906Number of Distinct Islands Leetcode Solution171
907Maximum Score After Splitting a String Leetcode Solution171
908Shortest Word Distance Leetcode Solution171
909Set Matrix Zeroes Leetcode Solution170
910Three way partitioning of an array around a given range170
911Time Based Key-Value Store LeetCode Solution170
912Range sum queries without updates170
913Find distance between two nodes of a Binary Tree170
914Check if any two intervals overlap among a given set of intervals170
915All Unique Triplets that Sum up to a Given Value170
916My Calendar I LeetCode Solution170
917Count Negative Numbers in a Sorted Matrix LeetCode Solution170
918Rearrange Spaces Between Words Leetcode Solution170
919Range LCM Queries170
920Matrix Chain Multiplication170
921Diagonal Traversal LeetCode Solution170
922A program to check if a binary tree is BST or not170
923Path with maximum average value170
924Check if the given array can represent Level Order Traversal of Binary Search Tree170
925Rearrange an Array Such that arr[i] is equal to i169
926Alien Dictionary169
927Destination City Leetcode Solution169
928Construct Complete Binary Tree from its Linked List Representation169
929Robot Bounded In Circle LeetCode Solution169
930Median of Two Sorted Arrays169
931Construct the Rectangle Leetcode Solution169
932Trim a Binary Search Tree169
933Binary Tree Longest Consecutive Sequence LeetCode Solution169
934Count Submatrices With All Ones LeetCode Solution169
935Difference Array | Range update query in O(1)169
936Iterative Postorder Traversal Using Two Stacks169
937Queue using Stacks169
938Spiral Matrix III LeetCode Solution168
939Binary array after M range toggle operations168
940Find Common Characters Leetcode Solution168
941Number of Equivalent Domino Pairs Leetcode Solution168
942Intersection of Two Linked Lists LeetCode Solution168
943Largest area rectangular sub-matrix with equal number of 1’s and 0’s168
944Product of Array Except Self LeetCode Solution168
945Strongly Connected Component168
946Maximum Frequency Stack Leetcode Solution168
947Convert Sorted List to Binary Search Tree168
948Construct BST from its given Level Order Traversal167
949Climbing stairs167
950Create Maximum Number167
951Word Break167
952Palindrome Partitioning Leetcode Solution167
9533 Sum167
954Remove Duplicates from Sorted List II LeetCode Solution167
955Kth Smallest Element in a BST Leetcode Solution167
956Three Consecutive Odds Leetcode Solution167
957Analyze User Website Visit Pattern LeetCode Solution167
958Sign of the Product of an Array LeetCode Solution166
959BFS for Disconnected Graph166
960K’th Largest Element in BST when modification to BST is not allowed166
961Count Subarrays with Same Even and Odd Elements166
962Vertical sum in a given binary tree166
963Merge Two Sorted Lists Leetcode166
964Queries for GCD of all numbers of an array except elements in a given range166
965Special Positions in a Binary Matrix Leetcode Solution166
966Longest subsequence such that difference between adjacents is one166
967Combination Sum IV LeetCode Solution166
968Merging Intervals165
969Design Hit Counter LeetCode Solution165
970Breadth First Search (BFS) for a Graph165
971Minimum Height Trees LeetCode Solution165
972Reverse Nodes in K-Group165
9734Sum165
974Count pair with Given Sum165
975Longest Palindromic Subsequence165
976Unique Paths II165
977Valid Boomerang Leetcode Solution164
978Asteroid Collision LeetCode Solution164
979Maximum Product Subarray164
980Maximum Product of Indexes of Next Greater on Left and Right164
981Find Maximum of Minimum for Every Window Size in a Given Array164
982Largest Number Leetcode Solution164
983Stone Game II Leetcode164
984Depth First Search (DFS) for a Graph164
985Count subarrays where second highest lie before highest164
986Maximize Sum of Array after K Negations Leetcode Solution164
987Count Primes in Ranges164
988The Maze III LeetCode Solution164
989Number of palindromic paths in a matrix163
990Can Make Arithmetic Progression From Sequence Leetcode Solution163
991Print Fibonacci sequence using 2 variables163
992Consecutive Characters LeetCode Solution163
993Next Permutation Leetcode Solution163
994Symmetric Tree163
995Serialize and Deserialize Binary Tree163
996Longest Nice Substring LeetCode Solution163
997Level Order Traversal of Binary Tree162
998Palindrome Partitioning162
999K Closest Points to Origin Leetcode Solution162
1000Minimum Index Sum of Two Lists162
1001Binary Search Tree Delete Operation162
1002String Matching in an Array Leetcode Solution162
1003Kth Smallest Element in a Sorted Matrix LeetCode Solution162
1004Find number of pairs in an array such that their XOR is 0162
1005Maximum Length of Repeated Subarray162
1006Maximum Subarray Sum Excluding Certain Elements162
1007Validate Binary Search Tree162
1008Scramble String LeetCode Solution161
1009Largest divisible pairs subset161
1010Balanced Binary Tree161
1011Merge Sort161
1012Length of Longest Fibonacci Subsequence161
1013Maximum Sum of 3 Non-Overlapping Subarrays161
1014Find the Difference Leetcode Solution161
1015Decrypt String from Alphabet to Integer Mapping Leetcode Solution161
1016Newman-Conway Sequence161
1017Collect maximum points in a grid using two traversals161
1018Find Duplicate Subtrees161
1019Graph Valid Tree LeetCode Solution161
1020Maximum difference between frequency of two elements such that element having greater frequency is also greater161
1021Height of a generic tree from parent array161
1022Count pairs from two sorted arrays whose sum is equal to a given value x161
1023Segment Tree161
1024Shuffle 2n integers as a1-b1-a2-b2-a3-b3-..bn without using extra space161
1025Insertion in a Binary Tree160
1026Number Of Longest Increasing Subsequence160
1027Generate Parentheses Leetcode Solution160
1028Find the First Circular Tour that visits all the Petrol Pumps160
1029Find postorder traversal of BST from preorder traversal160
1030K maximum sums of overlapping contiguous sub-arrays160
1031Maximum Number of Ways to Partition an Array LeetCode Solution160
1032First missing positive160
1033Binary Search Tree160
1034Print Next Greater Number of Q queries160
1035The Painter’s Partition Problem160
1036Rearrange an array such that ‘arr[j]’ becomes ‘i’ if ‘arr[i]’ is ‘j’159
1037Next Greater Element III LeetCode Solution159
1038Compute nCr % p159
1039Friends Of Appropriate Ages LeetCode Solution159
1040Construction of Longest Increasing Subsequence (N log N)159
1041Custom Sort String Leetcode Solution159
1042Construct Binary Tree from given Parent Array representation159
1043Invalid Transactions LeetCode Solution159
1044Advantages of BST over Hash Table159
1045Make The String Great Leetcode Solution159
1046Largest Substring Between Two Equal Characters Leetcode Solution159
1047Shortest Completing Word Leetcode Solution159
1048Available Captures for Rook Leetcode Solution159
1049Move all negative elements to end in order with extra space allowed159
1050Check If a Word Occurs As a Prefix of Any Word in a Sentence Leetcode Solution158
1051Rearrange array such that even positioned are greater than odd158
1052Kill Process LeetCode Solution158
1053Boundary Traversal of binary tree158
1054Count minimum steps to get the given desired array158
1055Path Sum II LeetCode Solution158
1056Boolean Parenthesization Problem158
1057Maximum length subsequence with difference between adjacent elements as either 0 or 1158
1058Iterative Depth First Traversal of Graph158
1059Maximum subsequence sum such that no three are consecutive157
1060Build an Array With Stack Operations Leetcode Solution157
1061Rearrange array such that even index elements are smaller and odd index elements are greater157
1062Find a Peak Element II LeetCode Solution157
1063Remove Duplicates from Sorted Array II Leetcode Solution157
1064Level order traversal using two Queues157
1065Recover Binary Search Tree Leetcode Solution157
1066Double the first element and move zero to end157
1067Determine Whether Matrix Can Be Obtained By Rotation LeetCode Solution157
1068Defanging an IP Address LeetCode Solution157
1069Bitwise AND of Numbers Range LeetCode Solution157
1070Smallest Common Region Leetcode Solution156
1071Palindrome Permutation LeetCode Solution156
1072Subarrays with K Different Integers Leetcode Solution156
1073Super Ugly Number155
1074Morris Inorder Traversal155
1075Partition Equal Subset Sum155
1076Remove Palindromic Subsequences Leetcode Solution155
1077Regular Expression Matching Regular Expression Matching LeetCode Solution155
1078Binary Tree Right Side View LeetCode Solution155
1079Ugly Numbers155
1080Employee Importance LeetCode Solution155
1081Number of Closed Islands Leetcode Solution155
1082Find Peak Element155
1083Lowest Common Ancestor155
1084Maximum weight transformation of a given string155
1085Print Right View of a Binary Tree154
1086Populating Next Right Pointers in Each Node Leetcode Solution154
1087Ugly Number II LeetCode Solution154
1088Minimum Size Subarray Sum154
1089Largest BST Subtree LeetCode Solution154
1090Bus Routes Leetcode Solution154
1091Find maximum difference between nearest left and right smaller elements154
1092Maximize Distance to Closest Person LeetCode Solution154
1093Palindromic Substrings Leetcode Solution154
1094Valid Tic-Tac-Toe State LeetCode Solution154
1095Implement Trie (Prefix Tree) Leetcode Solution154
1096Remove duplicates from sorted array154
1097Add two numbers154
1098Construct Binary Tree from Preorder and Postorder Traversal LeetCode Solution153
1099Binary Tree Data Structure153
1100Increasing Triplet Subsequence LeetCode Solution153
1101Divisible Pairs Counting153
1102Path Sum152
1103Find the minimum distance between two numbers152
1104Search in Sorted Rotated Array152
1105Reformat The String Leetcode Solution152
1106Number of siblings of a given Node in n-ary Tree152
1107Print all triplets in sorted array that form AP152
1108Find whether a subarray is in form of a mountain or not152
1109Search Insert Position151
1110Find Three Element From Different Three Arrays Such That a + b + c = sum151
1111Root to Leaf path with target sum Leetcode Solutions151
1112Count Largest Group Leetcode Solution151
1113Iterative method to find ancestors of a given binary tree151
1114Number of Students Doing Homework at a Given Time Leetcode Solution151
1115Kth Smallest Product of Two Sorted Arrays LeetCode Solution151
1116Cells with Odd Values in a Matrix LeetCode Solution151
1117Distinct Subsequences150
1118Products of ranges in an array150
1119Longest Bitonic Subsequence150
1120Level of Each node in a Tree from source node150
1121Prime Palindrome LeetCode Solution150
1122Moser-de Bruijn Sequence150
1123Serialize and Deserialize Binary Tree LeetCode Solution150
1124Find Smallest Range Containing Elements from k Lists150
1125Day of the Year Leetcode Solution150
1126Find Two Non-overlapping Sub-arrays Each With Target Sum LeetCode Solution150
1127Longest Repeated Subsequence149
1128Thousand Separator Leetcode Solution149
1129Maximum Product Subarray149
1130Longest Increasing Consecutive Subsequence149
1131Construct K Palindrome Strings LeetCode Solution149
1132Convert BST into a Min-Heap without using array149
1133Bottom View of a Binary Tree149
1134Minimum Absolute Difference in BST Leetcode Solution149
1135Rotate Array148
1136Clone a Binary Tree with Random Pointers148
1137Constant time range add operation on an array148
1138Mean of Array After Removing Some Elements Leetcode Solution148
1139Sum of Even Numbers After Queries148
1140Topological Sorting148
1141Maximum Depth Of Binary Tree148
1142Queries for Number of Distinct Elements in a Subarray147
1143Cutting a Rod147
1144Priority Queue147
1145Find maximum length Snake sequence147
1146Minimum Score Triangulation of Polygon Leetcode Solution147
1147Closest Binary Search Tree Value Leetcode Solution147
1148Web Crawler LeetCode Solution147
1149Longest Subarray of 1’s After Deleting One Element LeetCode Solution147
1150Subset Sum Problem in O(sum) space147
1151Print modified array after executing the commands of addition and subtraction146
1152Search Suggestions System LeetCode Solution146
1153Diameter of N-Ary Tree LeetCode Solution146
1154Minimum Cost to Move Chips to The Same Position LeetCode Solution146
1155Delete Nodes and Return Forest Leetcode Solution146
1156Swap Nodes In Pairs146
1157Write Code to Determine if Two Trees are Identical146
1158Compare Strings by Frequency of the Smallest Character Leetcode Solution146
1159Search an Element in Sorted Rotated Array146
1160Possible Bipartition LeetCode Solution145
1161Maximum Product of Splitted Binary Tree LeetCode Solution145
1162Minimum sum of squares of character counts in a given string after removing k characters145
1163Count even length binary sequences with same sum of first and second half bits145
1164Find a sorted subsequence of size 3 in linear time145
1165Minimum Moves to Equal Array Elements LeetCode Solution145
1166Third Maximum Number Leetcode Solution145
1167Decision Tree145
1168Golomb sequence144
1169Binary Tree to Binary Search Tree Conversion144
1170Reformat Date LeetCode Solution144
1171Kth Smallest Number in Multiplication Table Leetcode Solution144
1172Count Pairs Whose Products Exist in Array144
1173Minimum Swaps To Make Sequences Increasing LeetCode Solution144
1174Find Largest Value in Each Tree Row LeetCode Solution144
1175Red-Black Tree Introduction144
1176Find the smallest binary digit multiple of given number144
1177Transform a BST to Greater sum Tree144
1178Remove Max Number of Edges to Keep Graph Fully Traversable Leetcode Solution144
1179Minimum Sideway Jumps LeetCode Solution144
1180Closest Leaf in a Binary Tree LeetCode Solution144
1181Convert Integer to the Sum of Two No-Zero Integers Leetcode Solution143
1182Maximum Value at a Given Index in a Bounded Array LeetCode Solution143
1183Replace two consecutive equal values with one greater143
1184K’th Largest element in BST using constant extra space143
1185Merge two BSTs with limited extra space143
1186Reverse a Path in BST using Queue143
1187Matchsticks to Square Leetcode Solution143
1188Contiguous Array LeetCode Solution143
1189Perfect Squares LeetCode Solution143
1190Maximize sum of consecutive differences in a circular array143
1191Sum of Left Leaves LeetCode Solution143
1192Divide Two Integers Leetcode Solution142
1193Path Crossing Leetcode Solution142
1194Decrease Elements To Make Array Zigzag LeetCode Solution142
1195Factorial Trailing Zeroes LeetCode Solution142
1196Convert Sorted Array to Binary Search Tree LeetCode Solutions142
1197First Bad Version142
1198Given a binary tree, how do you remove all the half nodes?142
1199Check Array Formation Through Concatenation Leetcode Solution142
1200Queue Reconstruction by Height142
1201Maximum number of segments of lengths a, b and c142
1202Concatenation of Array LeetCode Solution141
1203Number of Orders in the Backlog Leetcode Solution141
1204Maximum Binary Tree141
1205Valid Perfect Square LeetCode Solution141
1206Swapping Nodes in a Linked List Leetcode Solution141
1207Valid Triangle Number141
1208New 21 Game140
1209Maximum sum bitonic subarray140
1210Maximum sum of pairs with specific difference140
1211Lowest Common Ancestor of a Binary Tree Leetcode Solution140
1212Sliding Window Median Leetcode Solution140
1213Champagne Tower LeetCode Solution140
1214Kth Smallest Element in a BST140
1215Flatten 2D Vector LeetCode Solution139
1216Guess The Word139
1217Integer Break LeetCode Solution139
1218How to print maximum number of A’s using given four keys139
1219Print n terms of Newman-Conway Sequence139
1220Graph and its representation138
1221Lowest Common Ancestor in Binary Search Tree138
1222Find minimum number of merge operations to make an array palindrome138
1223Array Queries for multiply replacements and product138
1224Merge k Sorted Lists Leetcode Solution138
1225Queries on Probability of Even or Odd Number in given Ranges138
1226Longest Common Prefix Using Word by Word Matching138
1227Maximum Sum Increasing Subsequence137
1228Check if all levels of two Binary Tree are anagrams or not137
1229Find Minimum in Rotated Sorted Array II LeetCode Solution137
1230Missing Number137
1231Moving Stones Until Consecutive Leetcode Solution137
1232LRU Cache Leetcode Solution137
1233Filter Restaurants by Vegan-Friendly, Price and Distance Leetcode Solution137
1234Design Skiplist LeetCode Solution137
1235Maximum sum of a path in a Right Number Triangle137
1236Largest Plus Sign Leetcode Solution136
1237Range Sum Query using Sparse Table136
1238Maximum Array from Two given Arrays Keeping Order Same136
1239Maximum Product Subarray136
1240Maximize Elements Using Another Array136
1241Range Queries for Longest Correct Bracket Subsequence136
1242Mean of range in array136
1243Contiguous Array135
1244BST to a Tree with Sum of all Smaller Keys135
1245Count Subsets Having Distinct Even Numbers135
1246The kth Factor of n Leetcode Solution135
1247Print modified array after multiple array range increment operations135
1248Find k-th smallest element in BST (Order Statistics in BST)134
1249Min Cost Climbing Stairs LeetCode Solution134
1250Power of Two134
1251Write a function to get the intersection point of two Linked Lists134
1252Arithmetic Slices II – Subsequence LeetCode Solution134
1253Graph Cloning134
1254Check if each internal node of a BST has exactly one child133
1255Check in binary array the number represented by a subarray is odd or even133
1256Bold Words in String LeetCode Solution133
1257Different Ways to Add Parentheses Leetcode Solution133
1258Minimum Remove to Make Valid Parentheses LeetCode Solution133
1259Parallel Courses II LeetCode Solution133
1260Minimum Time to Collect All Apples in a Tree LeetCode Solution132
1261Check if two nodes are on the same path in a Tree132
1262Check Completeness of a Binary Tree LeetCode Solution132
1263Check for Identical BSTs without building the trees132
1264Image Overlap LeetCode Solution132
1265Symmetric Tree LeetCode Solution Leetcode Solution131
1266Smallest Range II Leetcode Solution131
1267Print Ancestors of a Given Binary Tree Node Without Recursion131
1268Verify Preorder Serialization of a Binary Tree131
1269Koko Eating Bananas LeetCode Solution130
1270Orderly Queue LeetCode Solution129
1271Excel Sheet Column Title LeetCode Solution129
1272Arranging Coins Leetcode Solution129
1273Merge Sorted Array129
1274Next greater element128
1275Vertical Order Traversal of Binary Tree LeetCode Solution128
1276Longest Substring Without Repeating Characters Leetcode Solution127
1277Array Nesting Leetcode Solution127
1278Number of elements less than or equal to a given number in a given subarray127
1279Palindrome Partitioning126
1280Count and Toggle Queries on a Binary Array126
1281Check If a String Can Break Another String Leetcode Solution126
1282Add Two Numbers II Leetcode Solution125
1283Insert Delete GetRandom O(1) Leetcode Solution125
1284Palindrome Number LeetCode Solution125
1285Largest Submatrix With Rearrangements LeetCode Solution124
1286Peeking Iterator LeetCode Solution124
1287Check if X can give change to every person in the Queue124
1288Newman–Shanks–Williams prime124
1289Find the Winner of the Circular Game LeetCode Solution123
1290Find maximum average subarray of k length123
1291Maximum product of an increasing subsequence123
1292Queries for Decimal Values of Subarrays of a Binary Array123
1293Number of Subsequences That Satisfy the Given Sum Condition LeetCode solution122
1294Range Minimum Query (Square Root Decomposition and Sparse Table)122
1295Encoded String With Shortest Length LeetCode Solution121
1296Check given array of size n can represent BST of n levels or not120
1297Continuous Subarray Sum LeetCode Solution120
1298Reach a Number LeetCode Solution119
1299Jump Game IV LeetCode Solution119
1300Random Pick Index LeetCode Solution118
1301Minimum Possible Integer After at Most K Adjacent Swaps On Digits LeetCode Solution118
1302Binary Tree to Binary Search Tree Conversion using STL set118
1303Convert a BST to a Binary Tree such that sum of all greater keys is added to every key117
1304Number of indexes with equal elements in given range117
1305Convert to Base -2 LeetCode Solution116
1306Minimum Number of People to Teach LeetCode Solution116
1307Minimum Total Space Wasted With K Resizing Operations LeetCode Solution115
1308Design A Leaderboard Leetcode Solution115
1309Design Underground System Leetcode Solution114
1310Shifting Letters LeetCode Solution112
1311Queries on XOR of greatest odd divisor of the range112
1312Minimum Swaps to Group All 1’s Together Leetcode Solution110
1313Print Maximum Length Chain of Pairs110
1314Detect Capital Leetcode Solution108
1315Top K Frequent Elements LeetCode Solution108
1316Minimum Path Sum Leetcode Solution107
1317Count Sub Islands LeetCode Solution106
1318Monotonic Array Leetcode Solution105
1319Odd Even Linked List Leetcode Solution104
1320Substring with Concatenation of All Words Leetcode Solution104
1321Longest Common Subsequence LeetCode Solution102
1322Binary Tree Inorder Traversal LeetCode Solution101
1323Decode String Leetcode Solution101
1324Maximum Population Year LeetCode Solution99
1325Find the Town Judge LeetCode Solution99
1326Shortest Unsorted Continuous Subarray LeetCode Solution94
1327Best Meeting Point LeetCode Solution94
1328Rectangle Overlap LeetCode Solution93
1329Maximum Population Year LeetCode Solution92
1330Find the Town Judge LeetCode Solution91
1331Flatten Binary Tree to Linked List LeetCode Solution89
1332Design Add and Search Words Data Structure LeetCode Solution89
1333Sum Root to Leaf Numbers LeetCode Solution88
1334Valid Triangle Number LeetCode Solution86
1335Stone Game IV LeetCode Solution86
1336Is Graph Bipartite? LeetCode Solution86
1337Insert into a Sorted Circular Linked List LeetCode Solution86
1338Range Sum Query 2D – Immutable LeetCode Solution86
1339Reveal Cards In Increasing Order Leetcode Solution85
1340Score of Parenthesis LeetCode Solution84
1341Divide Chocolate LeetCode Solution80
1342Step-By-Step Directions From a Binary Tree Node to Another LeetCode Solution75
1343Range Sum of BST LeetCode Solution68
1344Reverse Integer Leetcode Solution66
1345Find K Closest Elements LeetCode Solution64
1346Rotate String LeetCode Solution64
1347Sort Colors LeetCode Solution63
1348Maximum Side Length of a Square with Sum Less than or Equal to Threshold LeetCode Solution61
1349Excel Sheet Column Number LeetCode Solution56
1350Maximum Size Subarray Sum Equals k Leetcode Solution49
1351Camelcase Matching Leetcode Solution45
1352Valid Anagram Leetcode Solution44
1353H-Index Leetcode Solution44
1354Most Stones Removed with Same Row or Column LeetCode Solution43
1355Sliding Window Maximum LeetCode Solution42
1356Container With Most Water LeetCode Solution42
1357Group Anagrams LeetCode Solution41
1358High Five LeetCode Solution41
1359Next Permutation LeetCode Solution41
1360Binary Search LeetCode Solution40
1361Find Peak Element LeetCode Solution40
1362Pairs of Songs With Total Durations Divisible by 60 LeetCode Solution37
1363Paint House LeetCode Solution37
1364Valid Triangle Number LeetCode Solution35
1365Minimum Number of Arrows to Burst Balloons LeetCode Solution34
1366Unique Binary Search Trees LeetCode Solution34
1367Next Greater Element I Leetcode Solution34
1368Group Shifted Strings Leetcode Solution34
1369Flatten Binary Tree to Linked List LeetCode Solution34
1370Sentence Screen Fitting LeetCode Solution33
1371Isomorphic Strings LeetCode Solution32
1372Count Good Nodes in Binary Tree LeetCode Solution32
1373Closest Binary Search Tree Value II LeetCode Solution31
1374Swim in Rising Water LeetCode Solution31
1375Insert Delete GetRandom O(1) – Duplicates allowed LeetCode Solution30
1376The Number of Weak Characters in the Game LeetCode Solution29
1377Peak Index in a Mountain Array LeetCode Solution29
1378Split Linked List in Parts Leetcode Solution27
1379Validate Stack Sequences LeetCode Solution26
1380Greatest Sum Divisible by Three LeetCode Solution24
1381Best Time to Buy and Sell Stock IV LeetCode Solution24
1382Implement strStr() LeetCode Solution23
1383All Possible Full Binary Trees LeetCode Solution23
1384Single Element in a Sorted Array LeetCode Solution23
1385Implement Rand10() Using Rand7() Leetcode Solution22
1386Lowest Common Ancestor of a Binary Search Tree Leetcode Solution22
1387Trapping Rain Water II LeetCode Solution21
1388Break a Palindrome LeetCode Solution21
1389Max Sum of Two Non-Overlapping Subarrays LeetCode Solution21
1390Contains Duplicate LeetCode Solution20
1391Minimum Increment to Make Array Unique Leetcode Solution20
1392Stone Game IV LeetCode Solution20
1393Remove All Ones With Row and Column Flips Leetcode Solution19
1394Find First and Last Position of Element in Sorted Array LeetCode Solution19
1395Detect Squares LeetCode Solution18
1396Reverse Nodes in k-Group LeetCode Solution17
1397Minimum Number of Operations to Move All Balls to Each Box LeetCode Solution16
1398Fibonacci Number LeetCode Solution16
1399Design Bounded Blocking Queue LeetCode Solution15
1400Total Hamming Distance LeetCode Solution14
1401Invert Binary Tree LeetCode Solution12
1402Unique Paths III LeetCode Solution2
1403Candy LeetCode Solution1
Translate »