Minimum Number of Operations to Move All Balls to Each Box LeetCode Solution


Frequently asked in Amazon GoogleViews 20

Problem Statement:

Minimum Number of Operations to Move All Balls to Each Box LeetCode Solution:

You have n boxes. You are given a binary string boxes of length n, where boxes[i] is '0' if the ith box is empty, and '1' if it contains one ball.

In one operation, you can move one ball from a box to an adjacent box. Box i is adjacent to box j if abs(i - j) == 1. Note that after doing so, there may be more than one ball in some boxes.

Return an array answer of size n, where answer[i] is the minimum number of operations needed to move all the balls to the ith box.

Each answer[i] is calculated considering the initial state of the boxes.

Example 1:

Input:

 boxes = "110"

Output:

 [1,1,3]

Explanation:

 The answer for each box is as follows:
1) First box: you will have to move one ball from the second box to the first box in one operation.
2) Second box: you will have to move one ball from the first box to the second box in one operation.
3) Third box: you will have to move one ball from the first box to the third box in two operations, and move one ball from the second box to the third box in one operation.

Example 2:

Input:

 boxes = "001011"

Output:

 [11,8,5,4,3,4]

Constraints:

  • n == boxes.length
  • 1 <= n <= 2000
  • boxes[i] is either '0' or '1'.

Approach:

1. The brute force solution is Simple to do a double iteration and for each ball found, compute the distance and add it to the cost.

2. Now look into the optimized solution.

3. Let pref[i] is the total distance after moving all the ball from the left side to ith box.

4. Let the sum is the number of balls after moving all the balls from the left side to ith box so far.

5. To calculate pref[i]  , we need to move some balls from (i-1)th box by one, so total distance is pref[i]=pref[i-1]+sum.

6. Do the same with suff[i] on the right side,so total distance is suff[i]=suff[i+1]+sum.

7. To calculate the total distance after moving all the balls from the left and the right side to the ith box: ans[i]=pref[i]+suff[i].

Code:

Minimum Number of Operations to Move All Balls to Each Box C++ code:

class Solution {
public:
    #define ll int
    vector<int> minOperations(string boxes) {
        ll n=boxes.size();
        vector<ll>pref(n+1,0),suff(n+1,0);
        ll i,sum=0,prev=0;
        for(i=0;i<n;i++)
        { 
            pref[i]=sum+prev;
            if(boxes[i]=='1')
                sum++;
            prev=pref[i];
           
        }
        sum=0,prev=0;
        for(i=n-1;i>=0;i--)
        { 
            suff[i]=sum+prev;
            if(boxes[i]=='1')
                sum++;
            prev=suff[i];
            
        }
        
        vector<ll>ans(n,0);
        for(i=0;i<n;i++)
            ans[i]=pref[i]+suff[i];
        return ans;
    }
};

Minimum Number of Operations to Move All Balls to Each Box Java code:

class Solution {
    public int[] minOperations(String boxes) {
        int n=boxes.length();
        int i;
        int[] ans=new int[n];
        int[] pref=new int[n];
        int[] suff=new int[n];
        for(i=0;i<n;i++)
        {
            pref[i]=0;
            suff[i]=0;
        }
        
         int sum=0,prev=0;
        for(i=0;i<n;i++)
        { 
            pref[i]=sum+prev;
            if(boxes.charAt(i)=='1')
                sum++;
            prev=pref[i];
           
        }
        sum=0;prev=0;
        for(i=n-1;i>=0;i--)
        { 
            suff[i]=sum+prev;
            if(boxes.charAt(i)=='1')
                sum++;
            prev=suff[i];
            
        }
        
        
        for(i=0;i<n;i++)
            ans[i]=pref[i]+suff[i];
        return ans;
        
    }
}

Complexity Analysis of Minimum Number of Operations to Move All Balls to Each Box Leetcode Solution:

Time Complexity:

Time Complexity is O(N). As we are traversing the whole array to calculate pref[i] and suff[i].

Space Complexity:

Space complexity is O(N). We are taking extra space to store the value.

Top Interview Questions

S.No Interview Question Number of times has been asked
1Delete a node in doubly linked list2887
2Palindrome using Recursion2619
3Java program to find the number of Nodes in a Binary Tree2543
4Reverse a string without affecting special characters2526
5Delete a node of a linked list at given position2022
6Quick Sort1769
7Insert nodes in a linked list in a sorted way (Ascending Order)1710
8Find elements pair from array whose sum equal to number1690
9Sort Elements by Frequency of Occurrences1672
10Write a program to print all permutations of a given string1656
11Find Minimum Distance Between Two Numbers in an Array1518
12Create a Doubly Linked List1484
13Reverse an Array1466
14Smallest window in a string containing all characters of another string1440
15Recursively remove all adjacent duplicates1409
16Find a Triplet That Sum to a Given Value1392
17First Repeating Element1387
18Sum of numbers in String1364
19Arrange Even and Odd number such that Odd comes after Even1357
20Smallest Positive Number Missing in an Unsorted Array1326
21Check if the Elements of an Array are Consecutive1294
22Detect a loop in the Linked List1273
23Largest Sum Contiguous Subarray1263
24Quick Sort on SIngly Linked List1259
25Recursive function to do substring search1250
26Print all Possible Combinations of R Elements in a given Array of size N1248
27Subarray with Given Sum1248
28Find the Maximum Repeating Number in Array1206
29Find the First and Second Smallest Elements1155
30Binary Tree Level order traversal in Java1155
31Check if two linked lists are identical1143
32Maximum Subarray Sum using Divide and Conquer1141
33Remove characters from first string which are in second1115
34Find Leaders in an Array1094
35Swap nodes in the linked list1086
36Find the second most frequent character1036
37Find the Number Occurring Odd Number of Times in an Array1036
38Arrange given Numbers to Form the Biggest Number II1027
39Given a string find its first non-repeating character1016
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 x995
42A Program to check if strings are rotations of each other or not992
43Total number of occurrences of a given item in the linked list991
44Print all possible words from phone digits974
45Find the Missing Number966
46Rearrange Positive and Negative Numbers Alternatively in Array953
47Longest Palindromic Substring946
48Segregate even and odd nodes in a linked list937
49Print Longest common subsequence926
50Transform one string to another using minimum number of given operations915
51Union and Intersection of Two Linked Lists905
52Check rearranged string can form a palindrome899
53Rearrange given Array in Maximum Minimum Form879
54Iterative Implementation of Quick Sort869
55Insertion Sort858
56Count Possible Triangles856
57Multiplication of Two Matrices843
58Count of Triplets With Sum Less than Given Value830
59Rotate a Linked List828
60Check if the linked list is palindrome827
61Stock Buy Sell to Maximize Profit827
62Concatenation of two strings807
63Tug of War799
64Print all duplicates in the input string798
65Count Number of Substrings with K Distinct Character’s796
66Find Nearest Greater and Smaller Element784
67Reverse String Without Temporary Variable778
68Remove ‘b’ and ‘ac’ from a given string771
69The Celebrity Problem771
70Find Pythagorean Triplets from Array770
71Find all Common Elements in Given Three Sorted Arrays763
72Remove all duplicates in an unsorted linked list741
73Find the Row with Maximum Number of 1’s739
74Find the subarray whose sum is equal to a given number X738
75Remove Minimum Characters so that Two Strings Become Anagrams738
76Find the Peak Element from an Array735
77Addition of Two Matrices730
78Generate all Binary Strings Without Consecutive 1’s727
79Find Smallest Missing Number in a Sorted Array726
80A Product Array Puzzle724
81Maximum Sum of Non Consecutive Elements710
82Implement Two Stacks in an Array707
83Lexicographic rank of string701
84Check if Two given Matrices are Identical692
85Maximum Product Subarray II687
86Multiplication of Previous and Next681
87Subtraction of Two Matrices674
88Merge K Sorted Arrays and Print Sorted Output668
89Divide a string in N equal parts667
90Move All the Zeros to the End of the Given Array667
91Form Minimum Number from Given Sequence of D’s and I’s665
92Online Algorithm for Checking Palindrome in a Stream665
93Check whether two strings are anagram of each other658
94Remove recurring digits in a given number657
95Sort a stack using a temporary stack652
96Maximum Circular Subarray Sum651
97Sort a linked list that is sorted alternating ascending and descending645
98Subarray and Subsequence640
99Find the Minimum Element in a Sorted and Rotated Array637
100Move last element of the Linked List at first place634
101First Circular Tour to Visit all the Petrol Bunks633
1023Sum Leetcode Solution629
103Compare two strings(linked lists)629
104Maximum Element in an Array which is Increasing and then Decreasing628
105Largest Subarray with Equal Number of 0’s and 1’s627
106Palindrome Permutations of a String627
107Flattening a linked list623
108Palindromes in a given range623
109Minimum insertions to form a shortest palindrome619
110Pangram Checking619
111Majority Element618
112Run length encoding616
113Print all permutations with repetition616
114Elements Appear more than N/K times in Array611
115Minimum Characters to be Added at Front to Make String Palindrome609
116Most repeating character in a string606
117Two Sum Leetcode Solution605
118Rotate string to get lexicographically minimum string603
119Repeated Subsequence of Length Two or More603
120Merge a linked list into another at alternate positions596
121Remove all duplicates in a sorted linked list595
122Rearrange a given linked list in-place594
123Minimum number of Merge Operations to make an Array Palindrome593
124Print all anagrams together in a sequence of words592
125Reorder an Array According to the Given Indexes582
126Pancake Sorting Problem581
127Merge Overlapping Intervals II568
128Longest Palindrome can be Formed by Removing or Rearranging Characters568
129Smallest Palindrome after Replacement568
130Transpose of a Matrix568
131Remove duplicates from a string567
132Clone a Linked List with next and random pointer566
133Remove Extra Spaces from a String565
134Removing Spaces from a String using stringstream561
135Maximum Sum Increasing Subsequence560
136Check if a given string is a rotation of a palindrome560
137Size of The Subarray With Maximum Sum558
138Partition Problem552
139Check whether Strings are K Distance Apart or Not549
140Generate all Binary Strings from Given Pattern547
141Length of Longest valid Substring542
142Delete Last Occurrence541
143Check if Two given Strings are Isomorphic to each other539
144Find Zeros to be Flipped so that Number of Consecutive 1’s is Maximized535
145Insert Node in the Sorted Linked List533
146Program to Toggle all Characters in a String531
147Given string is interleaving of two other strings or not526
148Maximum difference between two elements such as larger element comes after smaller525
149Check length of a String is Equal to the Number Appended at its Last517
150Count Minimum Steps to Get the given Array515
151Number of Smaller Elements on Right Side512
152Check if all Rows of a Matrix are Circular Rotations of Each Other509
153Longest Common Prefix using Divide and Conquer508
154Find Pair with Given Difference507
155Merge sort better than quick sort for linked lists505
156Compare Two Version Numbers503
157Find nth node of the Linked list from the end499
158Print all interleavings of given two strings499
159Find a Fixed Point in a Given Array496
160Sort 0s 1s and 2s in an Array496
161Print Reverse of a string (Recursion)496
162Median of Two Sorted Arrays LeetCode Solution495
163Reverse words in a given string494
164Reorder Array Using Given Indexes493
165Find the Subarray of given length with Least Average486
166Merge two sorted linked lists such that merged list is in reverse order486
167Split linked list using alternate nodes484
168Print all Palindromic Partitions of a String477
169Print string of odd length in ‘X’ format475
170Find Element Using Binary Search in Sorted Array469
171Find K Length Subarray of Maximum Average467
172Swap Kth Node from beginning with Kth Node from End465
173Find Duplicates in an Array in Most Efficient Way460
174Shortest Superstring Problem458
175print all palindromic partitions455
176Check if String Follows Order of Characters by a Pattern or not452
177Maximum Length of Chain Pairs449
178Sort a String According to Another String448
179Flatten a multilevel linked list445
180Sorting a K Sorted Array440
181Program to add two binary digits435
182Recursively print all the sentences that can be formed from list of word lists431
183Caesar Cipher430
184Find a Sorted Subsequence of size 3429
185Longest Span with same Sum in two Binary Arrays II427
186Longest Common Prefix Using Binary Search II427
187Reverse a Linked List in groups426
188Find the two Numbers with Odd Occurrences in an Unsorted Array425
189Kth Non-repeating Character422
190Reverse a Singly Linked List (Iterative/Non-Recursive)421
191Check if String can Become Empty by Recursively Deleting given Substring418
192Longest Common Prefix Word by Word Matching414
193Rearrange a linked list in Zig-Zag412
194Permutations of a Given String Using STL408
195Rotate Image by 90 degrees407
196Perfect Reversible String406
197Pancake Sorting405
198Merging Two Sorted Arrays405
199Find First non-repeating character in a string402
2001`s and 2`s complement of binary number401
201Maximum occurring character in a string396
202List items containing all characters of a given word396
203Increasing Subsequence of Length three with Maximum Product395
204Find the point where a monotonically increasing function becomes positive first time394
205Four Elements that Sum to Given391
206Sort a linked list with 0s, 1s and 2s390
207Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes388
208Longest Common Prefix using Character by Character Matching387
209Count Number of Occurrences in a Sorted Array384
210Palindrome string (number)384
211Valid Parentheses LeetCode Solution382
212Delete N nodes after M382
213Minimum Characters to be Removed to Make a Binary String Alternate379
214Even Substring Count377
215Split a string377
216Recursive Implementation of atoi()374
217Sorting the array of strings373
218Convert a String that is Repetition of a Substring of Length K370
219Print Shortest Path to Print a String on Screen368
220Check if a Linked list of Strings form a Palindrome367
221Maximum Subarray Leetcode Solution364
222Nth Character in Concatenated Decimal String364
223Convert string1 to string2 in one edit363
224Print All Distinct Elements of the Array360
225Count the number of words360
226Reverse a singly linked list recursively360
227Reverse a String using Stack359
228wildcard character matching358
229Matrix Chain Multiplication using Dynamic Programming358
230Find the first Repeating Number in a Given Array358
231Lower Case To Upper Case355
232Can we reverse a linked list in less than O(n) time ?352
233Binary Tree to Doubly linked list350
234Longest Common Subsequence with Permutations347
235Sort Elements by Frequency II346
236Find the Lost Element From a Duplicated Array344
237Split Four Distinct Strings343
238Merge Two Sorted Arrays341
239Find middle of the Linked List338
240Roman to Integer Leetcode Solution338
241Count the Pairs at Same Distance as in English Alphabets336
242Toeplitz Matrix333
243N queen problem329
244Palindrome Permutation328
245Word Search Leetcode Solution326
246Next Greater Element in an Array325
247Move all negative elements to one side of array324
248First non Repeating Element320
249Sudoku Solver319
250Find All Pairs With a Given Difference318
251Searching a node in a Binary Search Tree318
252Reverse Bits316
253Print all Possible Ways to Break a String in Bracket Form316
254Count Pairs With Given Sum316
255String(represents an integer) to value316
256Find Nth Node315
257Number of Islands LeetCode Solution314
258Reverse a String313
259Change Gender of a given String313
260Repeated Substring Pattern312
261Types of Binary Tree312
262Triplet from three linked lists with given sum312
263Delete a Tree311
264Sort an array of strings310
265Fibonacci numbers310
266How to Efficiently Implement k Stacks in a Single Array?308
267Meeting Rooms II LeetCode Solution308
268Reverse a linked list308
269Number of sub-strings which recursively add up to 9308
270Min Stack308
271Delete a node under given conditions307
272Most Frequent Element in an Array307
273Longest Palindromic Substring LeetCode Solution305
274Cuckoo sequence program304
275Dijkstra Algorithm304
276Longest Common Extension302
277Binary Tree299
278Max stack299
279Remove spaces from a string298
280House Robber Leetcode Solution297
281Subset Leetcode293
282Best Time to Buy and Sell Stock  II Leetcode Solution293
283Word Search292
284Shuffle a given Array290
285KMP Algorithm290
286Minimize the maximum difference between the heights289
287Expression Evaluation289
288Find, second, frequent, character288
289Remove middle points in a linked list of line segments288
290Plus One Leetcode Solution287
291Number Of 1 bits286
292Combination Sum Leetcode Solution285
293Reverse words in a string284
294Search Insert Position Leetcode Solution284
295Pair of Positive Negative Values in an Array282
296Sliding Window Technique282
297Evaluation of Postfix Expression282
298Subarray with 0 sum279
299Set Matrix Zeroes279
300Min Stack Leetcode Solution279
301Rabin Karp Algorithm278
302Valid Palindrome Leetcode Solution277
303Backspace String Compare277
304Common elements in all rows of a given matrix274
305Sort linked which is sorted on absolute values273
306Merge Sorted Arrays Leetcode Solution272
307Clone a linked list with next and random pointer (Hashing)272
308Sqrt(x) Leetcode Solution270
309Reversing a Queue269
310Delete middle element of a stack269
311Contains Duplicate267
312Tower Of Hanoi267
313Implementation of Deque using Doubly Linked List266
314String Compression266
315Contains Duplicate II Leetcode Solution265
316Count of index pairs with equal elements in an array265
317Count subarrays with equal number of 1’s and 0’s265
318Product of array except self265
319Page Replacement Algorithms in Operating Systems264
320Find Top K (or Most Frequent) Numbers in a Stream264
321Reverse individual words264
322Intersection of Two Arrays II Leetcode Solution264
323Count Odd Numbers in an Interval Range Leetcode Solution264
324How to Delete a Linked List264
325Pascal Triangle Leetcode263
326Combination Sum263
327Minimum swaps required to bring all elements less than or equal to k together263
328Segregate even and odd numbers260
329Group Words With Same Set of Characters260
330Integer to Roman Leetcode Solution260
331Find sum of non-repeating elements (distinct) elements in an array260
332Smallest Subarray with k Distinct Numbers259
333Second Most Repeated Word in a Sequence258
334Bellman Ford Algorithm258
335Postfix to Infix Conversion258
336Sorting array using Stacks258
337Sum of minimum and maximum elements of all subarrays of size k258
338Minimum Value to Get Positive Step by Step Sum Leetcode Solution258
339Single Number Leetcode Solution258
340Arithmetic Expression Evaluation257
341Sort elements by frequency257
342Next Permutation257
343Top K Frequent Words256
344Add Binary Leetcode Solution256
345Count subarrays having total distinct elements same as original array255
346Maximum Number of Balloons Leetcode Solution255
347K-th Smallest Element in a Sorted Matrix255
348Given two unsorted arrays find all pairs whose sum is x254
349Minimum operation to make all elements equal in array254
350Convex Hull Algorithm253
351Construct Binary Tree from Given Inorder and Preorder Traversals253
352Longest Common Prefix Leetcode Solution252
353Scramble String252
354First element occurring k times in an array252
355Pow(x, n) Leetcode Solution251
356Palindrome Linked List Leetcode Solution251
357Special Number251
358Find duplicates in a given array when elements are not limited to a range250
359Majority Element Leetcode Solution250
360Design a stack that supports getMin() in O(1) time and O(1) extra space250
361Longest Substring Without Repeating Characters LeetCode Solution250
362Find Lucky Integer in an Array Leetcode Solution250
363Permutations Leetcode Solution248
364Kruskal Algorithm248
365Evaluate Division248
366Third Maximum Number Leetcode Solution246
367Running Sum of 1d Array Leetcode Solution246
368Spiral Matrix LeetCode Solution244
369Reversing the First K elements of a Queue244
370Fizz Buzz Leetcode244
371Maximum possible difference of two subsets of an array244
372Check if two arrays are equal or not244
373Minimum Path Sum244
374Number of Good Pairs Leetcode Solution243
375Reverse a Number Using Stack243
376Maximum Subarray243
377Prefix to Infix Conversion243
378Maximum Distance Between two Occurrences of Same Element in Array243
379Huffman Coding242
380Range Sum Query 2D – Immutable Leetcode Solution242
381Smallest Element Repeated Exactly K Times242
382Maximal Square241
383Count Primes Leetcode Solutions241
384Minimum Steps to reach target by a Knight241
385Find Numbers with Even Number of Digits Leetcode Solution241
386Unique Paths241
387Palindrome Substring Queries240
388Minimum Absolute Difference Leetcode Solution240
389Find the Closest Palindrome number240
390Convert String To Int240
391Find the Town Judge Leetcode Solution240
392Single Number240
393Group Anagrams240
394Swap Nodes in Pairs Leetcode Solutions240
395Find Number of Employees Under every Employee239
396Sorting using trivial hash function239
397Find top three repeated in array239
398Find All Numbers Disappeared in an Array Leetcode Solution239
399Find the first repeating element in an array of integers239
400Remove Minimum Number of Elements Such That no Common Element Exist in both Array239
401Zigzag Conversion238
402Sort Array by Increasing Frequency Leetcode Solution238
403Cumulative Frequency of Count of Each Element in an Unsorted Array238
404Pascal’s Triangle II Leetcode Solution236
405Implement Stack and Queue using Deque236
406Leetcode Permutations236
407Power of Two Leetcode Solution236
408Check if Array Contains Contiguous Integers With Duplicates Allowed236
409Print all subarrays with 0 sum235
410Merge Two Sorted Lists Leetcode Solutions235
411Count Substrings with equal number of 0s, 1s and 2s235
412Missing Number Leetcode Solution235
413Expression Contains Redundant Bracket or Not235
414Print All Distinct Elements of a Given Integer Array235
415Decode String234
416Matrix Diagonal Sum Leetcode Solution234
417Length of the largest subarray with contiguous elements234
418House Robber II Leetcode Solution234
419Sort Integers by The Number of 1 Bit Leetcode Solution234
420Bipartite Graph234
421Fizz Buzz234
422How to Implement Stack Using Priority Queue or Heap?234
423Difference between highest and least frequencies in an array234
424Letter Case Permutation234
425Prim’s Algorithm233
426Happy Number Leetcode Solution233
427Sort a stack using recursion233
428Subarray Sum Equals k233
429Maximum Depth of Binary Tree Leetcode Solution232
430Top K Frequent Elements232
431LRU Cache Implementation232
432Subset sum problem232
433Find all pairs (a, b) in an array such that a % b = k231
434Subarrays with distinct elements231
435Coin Change 2 Leetcode Solution231
436Reverse Integer231
437Find Median from data Stream231
438Count pairs from two linked lists whose sum is equal to a given value231
439Unique Paths Leetcode Solution231
440Search in Rotated Sorted Array Leetcode Solution231
441Find Winner on a Tic Tac Toe Game Leetcode Solution230
442Average Salary Excluding the Minimum and Maximum Salary Leetcode Solution230
443Pair with given product229
444Check If N and Its Double Exist Leetcode Solution229
445Capacity To Ship Packages Within D Days Leetcode Solution228
446Max Consecutive Ones Leetcode Solution228
447Monotonic Array LeetCode Solution228
448Reverse a Stack Using Recursion228
449How Many Numbers Are Smaller Than the Current Number Leetcode Solution228
450Nth Catalan Number228
451Find elements which are present in first array and not in second228
452Find the Difference Leetcode Solution228
453Find The Duplicate Number227
454Count and Say226
455Integer to English words226
456Subarray Sum Equals K LeetCode Solution226
457Edit Distance226
458Find distinct elements common to all rows of a matrix226
459Text Justification LeetCode Solution226
460Priority Queue Using Singly Linked List225
461Find Minimum In Rotated Sorted Array225
462Reverse a String225
463Valid Parenthesis String225
464Longest subarray not having more than K distinct elements225
465Find Index of Closing Bracket for a Given Opening Bracket in an Expression225
466Sum of Subarray Ranges Leetcode Solution225
467Fibonacci Number LeetCode Solution225
468Trapping Rain Water Leetcode Solution225
469Find any one of the multiple repeating elements in read only array224
470Reverse Vowels of a String Leetcode Solution224
471Rearrange a binary string as alternate x and y occurrences224
472Best Time to Buy and Sell Stock III Leetcode Solution224
473Remove Duplicates from Sorted Array Leetcode Solution224
474Floyd Warshall Algorithm224
475Check if a given array contains duplicate elements within k distance from each other224
476Subtract the Product and Sum of Digits of an Integer Leetcode Solution224
477Find four elements that sum to a given value (Hashmap)224
478Longest Common Prefix using Trie223
479Kth largest element in an Array Leetcode Solutions223
480Target Sum223
481Design Parking System Leetcode Solution223
482The K Weakest Rows in a Matrix Leetcode Solution222
483MiniMax Algorithm222
484Intersection of Two Arrays222
485Generate a String With Characters That Have Odd Counts Leetcode Solution221
486Count number of triplets with product equal to given number221
487Jump Game Leetcode Solution221
488Find missing elements of a range221
489Prefix to Postfix Conversion221
490Delete a Node from linked list without head pointer221
491Sorting a Queue without Extra Space220
492Shuffle String Leetcode Solution220
493Word Ladder LeetCode Solution220
494Degree of an array220
495Find subarray with given sum (Handles Negative Numbers)220
496Shuffle the Array Leetcode Solution220
497Find Common Characters Leetcode Solution220
498The Stock Span Problem219
499Top View of Binary Tree219
500Multiply Strings Leetcode Solution219
501Substring With Concatenation Of All Words219
502Flood Fill LeetCode219
503Balanced Binary Tree Leetcode Solution218
504Recursion218
505Shortest Palindrome218
506K-th Distinct Element in an Array218
507Best Time to Buy and Sell Stock LeetCode Solution218
508Check for Balanced Parentheses in an Expression218
509Iterative Tower of Hanoi218
510Merge Two Sorted Linked Lists218
511Excel Sheet Column Number Leetcode Solution218
512Peak Index in a Mountain Array217
513Minimum Delete Operations to make all Elements of Array Same217
514Kids With the Greatest Number of Candies Leetcode Solution217
515Implement a stack using single queue217
516Priority Queue in C++217
517Slowest Key Leetcode Solution217
518Iterative Inorder Traversal of a Binary Tree217
519Postfix to Prefix Conversion217
520Number of Steps to Reduce a Number to Zero Leetcode Solution217
521Next Greater Element I Leetcode Solution217
522Longest Common Prefix using Sorting217
523Implement Stack using Queues216
524Minimum Knight Moves LeetCode Solution216
525Container with Most Water216
526Longest Common Subsequence216
527Count and Say Leetcode Solution216
528Valid Sudoku216
529Concatenation of Array LeetCode Solution216
530Move Zeroes LeetCode Solution216
531Sum of Left Leaves Leetcode Solutions216
532Find the Duplicate Element216
533Reverse Words in a String III LeetCode Solution216
534Linked List Cycle II LeetCode Solution215
535Max Area of Island215
536Convert array into Zig-Zag fashion215
537Find Sum of all unique sub-array sum for a given array215
538Hamming Distance215
539Arrange given numbers to form the biggest number215
540Minimum Moves to Equal Array Elements Leetcode Solution215
541Next Greater Frequency Element215
542Check if a queue can be sorted into another queue using a stack214
543Change the Array into Permutation of Numbers From 1 to N214
544Largest Sum Contiguous Subarray214
545Find First and Last Position of Element in Sorted Array Leetcode Solution214
546Jewels and Stones Leetcode Solution214
547Smallest Subarray With all Occurrences of a Most Frequent Element213
548How to check if two given sets are disjoint?213
549Gold Mine Problem213
550Group Multiple Occurrence of Array Elements Ordered by first Occurrence213
551Merge Overlapping Intervals213
552Kth Largest Element in a Stream Leetcode Solution213
553Find N Unique Integers Sum up to Zero Leetcode Solution212
554Zigzag Conversion LeetCode Solution212
555Largest Perimeter Triangle Leetcode Solution212
556Isomorphic Strings Leetcode Solution212
557Count the number of nodes at given level in a tree using BFS212
558Excel Sheet Column Title Leetcode Solution212
559Is Subsequence Leetcode Solution212
560Minimum number of subsets with distinct elements211
561Shuffle an Array211
562Contiguous Array Leetcode211
563Delete Node in a Linked List Leetcode Solution211
564Maximum sum rectangle in a 2D matrix210
565Minimum Cost to Hire K Workers210
566Last Stone Weight210
567Summary Ranges Leetcode Solution210
568N-th Tribonacci Number Leetcode Solution210
569Valid Anagrams210
570Koko Eating Bananas Leetcode Solution210
571Convert a normal BST to Balanced BST210
572Smallest Good Base210
573Minimum Bracket Reversals210
574Find if an Expression has Duplicate Parenthesis or Not209
575Integer to Roman209
576Maximum path sum in a triangle209
577Number of Provinces Leetcode Solution208
578Implementation of Deque using circular array208
5793Sum Closest LeetCode Solution208
580Mobile Numeric Keypad Problem208
581Distribute Candies to People Leetcode Solution208
582Shortest Path in a Grid with Obstacles Elimination LeetCode Solution208
583Best Time to Buy and Sell Stock208
584Length of Last Word Leetcode Solution208
585Build Array From Permutation Leetcode Solution207
586Longest Increasing Subsequence207
587Find Words That Can Be Formed by Characters Leetcode Solution207
588Relative Sort Array Leetcode Solution207
589Maximum Distance in Array207
590Decode Ways207
591Iterative Method to find Height of Binary Tree206
592Rotate Image LeetCode Solution206
593Frog Jump Leetcode Solution206
594Trapping Rain Water LeetCode Solution206
595Non-overlapping sum of two sets206
596Island Perimeter Leetcode Solution206
597Word Pattern206
598First negative integer in every window of size k206
599Remove All Occurrences of a Substring LeetCode Solution205
600Maximum Number of Occurrences of a Substring Leetcode Solution205
601Minimum Operations to convert X to Y205
602Sort Characters By Frequency LeetCode Solution205
603Maximum difference between first and last indexes of an element in array205
604Combinations Leetcode Solution205
605Remove Linked List Elements Leetcode Solution205
606Delete consecutive same words in a sequence205
607LRU Cache LeetCode Solution205
60801 Matrix LeetCode Solution205
609Minimum number of distinct elements after removing m items204
610Assign Cookies Leetcode Solution204
611Count all subsequences having product less than K204
612XOR Operation in an Array Leetcode Solution204
613Sieve of Eratosthenes204
614The Knapsack Problem204
615GCD Of Two Numbers203
616Find the smallest positive integer value that cannot be represented as sum of any subset of a given array203
617Reverse a stack without using extra space in O(n)203
618Minimum insertions to form a palindrome with permutations allowed203
619Bubble sort using two Stacks203
620Find Largest d in Array such that a + b + c = d202
621Maximum Number of Chocolates to be Distributed Equally Among k Students202
622Rotate List Leetcode Solution202
623Isomorphic Strings202
624Permutation in String Leetcode Solution202
625Find minimum difference between any two elements202
626Form minimum number from given sequence202
627Stone Game LeetCode201
628Insert Interval Leetcode Solution201
629Tracking current Maximum Element in a Stack201
630Convert Sorted Array to Binary Search Tree Leetcode Solution201
631Sum of f(a[i], a[j]) over all pairs in an array of n integers201
632Unique Binary Search Trees201
633Optimal Account Balancing LeetCode Solution200
634Longest Increasing Path in a Matrix LeetCode Solution200
635Same Tree LeetCode Solution200
636Applications of Breadth First Search and Depth First Search200
637K Empty Slots200
638Defanging an IP Address Leetcode Solution200
639How to Create Mergable Stack?200
640Convert an array to reduced form200
641Find the Duplicate Number LeetCode Solution200
642Reorganize String200
643Count Good Nodes in Binary Tree Leetcode Solution199
644Distance Between Bus Stops Leetcode Solution199
645Convert a Number to Hexadecimal Leetcode Solution198
646Find Pair with Greatest Product in Array198
647Employee Free Time LeetCode Solution198
648Minimum Depth of Binary Tree Leetcode Solution198
649Printing brackets in Matrix Chain Multiplication Problem198
650Find pairs with given sum such that elements of pair are in different rows198
651Word Wrap Problem198
652Merge Two Balanced Binary Search Trees198
653First Unique Character in a String LeetCode Solution198
654Largest subarray with equal number of 0s and 1s198
655License Key Formatting Leetcode Solution198
656Replace Elements with Greatest Element on Right Side Leetcode Solution197
657Longest Substring with At Most K Distinct Characters LeetCode Solution197
658Painting Fence Algorithm197
659Letter Combinations of a Phone Number197
660Flipping an Image LeetCode Solution196
661Robot Room Cleaner Leetcode Solution196
662Check If It Is a Straight Line Leetcode Solution196
663Sort Array By Parity LeetCode Solution196
664Coin Change Problem196
665House Robber196
666Segregate 0s and 1s in an Array196
667Minimum Number of Steps to Make Two Strings Anagram Leetcode Solutions196
668Find the Distance Value Between Two Arrays Leetcode Solution196
669Moving Average from Data Stream Leetcode Solution196
670Reducing Dishes LeetCode Solution196
671Valid Palindrome195
672Longest Span with same Sum in two Binary arrays195
673Strobogrammatic Number LeetCode Solution195
674Kth Missing Positive Number Leetcode Solution195
675Partition Labels LeetCode Solution195
676Print the Fibonacci numbers in reverse order195
677Maximum Consecutive Numbers Present in an Array195
678Find the node with minimum value in a Binary Search Tree195
679K Empty Slots LeetCode195
680Queries for counts of array elements with values in given range194
681Reservoir Sampling194
682Find Leaves of Binary Tree LeetCode Solution194
683k-th missing element in increasing sequence which is not present in a given sequence194
684String to Integer (atoi) LeetCode Solution194
685String Compression LeetCode Solution194
686Dynamic Programming Basics194
687Distance of nearest cell having 1 in a binary matrix194
688Best Time to Buy and Sell Stock with Cooldown Leetcode Solution193
689Power of Four Leetcode Solution193
690Partition Array Into Three Parts With Equal Sum Leetcode Solution193
691Find Maximum Level sum in Binary Tree193
692Sum of All Odd Length Subarrays Leetcode Solution193
693Maximum Product of Two Elements in an Array Leetcode Solution193
694Valid Palindrome II Leetcode Solution193
695Find unique character in a string193
696Longest Substring with At Least K Repeating Characters LeetCode Solution193
697Generate all possible sorted arrays from alternate elements of two given sorted arrays192
698Check If Two String Arrays are Equivalent Leetcode Solution192
699Reverse Only Letters LeetCode Solution192
700Numbers with prime frequencies greater than or equal to k192
701Binary Tree Maximum Path Sum LeetCode Solution192
702Find whether an array is subset of another array192
703Count quadruples from four sorted arrays whose sum is equal to a given value x191
704Find the largest multiple of 3191
705Rearrange an array in order – smallest, largest, 2nd smallest, 2nd largest191
706Remove brackets from an algebraic string containing + and – operators191
707Remove Invalid Parentheses Leetcode Solution191
708Increasing Decreasing String Leetcode Solution191
709Find the Smallest Divisor given a Threshold Leetcode Solution191
710Unique Paths II Leetcode Solution191
711To Lower Case Leetcode Solution191
712Edit Distance LeetCode Solution191
713Maximum Number of Coins You Can Get Leetcode Solution191
714Path With Maximum Minimum Value LeetCode Solution191
715Merge Two Binary Trees LeetCode Solution190
716Snakes and Ladders LeetCode Solution190
717Inorder Successor of a node in Binary Tree190
718Wiggle Sort190
719Sort Array by Increasing Frequency Leetcode Solution190
720Base 7 Leetcode Solution190
721Number of Dice Rolls With Target Sum LeetCode Solution190
722Delete Nth node from the end of the given linked list190
723Permutation Sequence LeetCode Solution190
724Bulb Switcher LeetCode Solution190
725Stack Permutations (Check if an array is stack permutation of other)190
726Valid Perfect Square Leetcode Solution190
727Subset Sum Leetcode190
728Elements to be added so that all elements of a range are present in array189
729Lucky Numbers in a Matrix Leetcode Solution189
730Race Car LeetCode Solution189
731Find if Path Exists in Graph Leetcode Solution189
732BFS vs DFS for Binary Tree189
733Non-decreasing Array LeetCode Solution189
734Check for Palindrome after every character replacement Query189
735Minimum Cost For Tickets Leetcode Solution189
736Populating Next Right Pointers in Each Node189
737Recover Binary Search Tree189
738Binary Tree zigzag level order Traversal189
739Factorial Trailing Zeroes Leetcode Solution189
740Valid Number189
741Maximum Length of a Concatenated String with Unique Characters Leetcode Solution188
742Keyboard Row Leetcode Solution188
743Number of NGEs to the Right188
744Perform String Shifts Leetcode188
745Ugly Number Leetcode Solution188
746Merge Sorted Array LeetCode Solution188
747Tiling Problem188
748Reversing a Queue using Recursion188
749Find Maximum Depth of Nested Parenthesis in a String188
750String comparison containing wildcards187
751Evaluate Reverse Polish Notation LeetCode Solution187
752Growable array based stack187
753Program for Bridge and Torch problem187
754Best Meeting Point LeetCode Solution187
755Construct BST from given Preorder Traversal187
756Maximum 69 Number Leetcode Solution187
757Binary Tree Zigzag Level Order Traversal LeetCode Solution187
758Morris Traversal187
759Restore IP Addresses Leetcode Solution187
760Student Attendance Record I Leetcode Solution187
761Minimum sum of multiplications of n numbers187
762Majority Element II Leetcode Solution187
763Add and Search Word – Data structure design LeetCode187
764Check If Array Pairs Are Divisible by k LeetCode Solution186
765Regular Expression Matching186
766Priority Queue using doubly linked list186
767Word Pattern LeetCode Solution186
768Rank Transform of an Array Leetcode Solution186
769Print a Binary Tree in Vertical Order186
770Maximum Profit in Job Scheduling Leetcode Solution186
771Check if Two Expressions With Brackets are Same186
772Split a String in Balanced Strings Leetcode Solution186
773N-Queens LeetCode Solution186
774Symmetric Tree Leetcode Solution185
775Count items common to both the lists but with different prices185
776Maximum Product of Three Numbers LeetCode Solution185
777Number of Days Between Two Dates LeetCode Solution185
778Hamming Distance Leetcode Solution185
779Find Median from Data Stream LeetCode Solution185
780Find All Possible Recipes from Given Supplies LeetCode Solution185
781Binomial Coefficient185
782Sorted Linked List to Balanced BST185
783Finding K closest element185
784Jump Game185
785Brick Wall LeetCode Solution185
786Special Array With X Elements Greater Than or Equal X Leetcode Solution184
787Minimum Height Trees184
788Find All Duplicates in an Array LeetCode Solution184
789Minimize Maximum Pair Sum in Array LeetCode Solution184
790Search in a Binary Search Tree Leetcode Solution184
791Longest Subarray Having Count of 1s One More than Count of 0s184
792Minimum Time Visiting All Points Leetcode Solution184
793Top K Frequent Words LeetCode Solution184
794Largest Rectangle in Histogram LeetCode Solution184
795Partition to K Equal Sum Subsets Leetcode Solution184
796Sort an array according to the order defined by another array184
797Maximum Nesting Depth of the Parentheses Leetcode Solution184
798Total Numbers With no Repeated Digits in a Range183
799Maximum Depth of N-ary Tree Leetcode Solution183
800Linked List Cycle183
801Kth ancestor of a node in binary tree183
802Queue based approach for first non-repeating character in a stream183
803Form Minimum Number From Given Sequence183
804Decompress Run-Length Encoded List Leetcode Solution183
805Merge K Sorted Linked Lists183
806Minimum Distance Between BST Nodes Leetcode Solution183
807Maximum Difference Between Increasing Elements LeetCode Solution182
808Largest rectangular sub-matrix whose sum is 0182
809Balanced Expression with Replacement182
810One Edit Distance LeetCode Solution182
811Circular Queue182
812Number of Islands II LeetCode Solution182
813Remove Nth Node From End of List Leetcode Solution182
814Nearest Exit from Entrance in Maze LeetCode Solution182
815Nested List Weight Sum II LeetCode Solution182
816Given an Array of Pairs Find all Symmetric Pairs in it182
817Invert Binary Tree LeetCode Solution182
818Relative Ranks Leetcode Solution181
819Repeated Substring Pattern LeetCode Solution181
820Identify and Mark Unmatched Parenthesis in an Expression181
821Final Prices With a Special Discount in a Shop Leetcode Solution181
822Diagonal Traversal of Binary Tree181
823Transpose Graph180
824A Space Optimized DP solution for 0-1 Knapsack Problem180
825Last Stone Weight II LeetCode Solution180
826Minimum time required to rot all oranges180
827Subarray Product Less Than K LeetCode Solution180
828Lemonade Change Leetcode Solution180
829Deletion in a Binary Tree179
830Find all permuted rows of a given row in a matrix179
831Design Browser History LeetCode Solution179
832Reorder Data in Log Files LeetCode Solution179
833Minimum Number of Taps to Open to Water a Garden LeetCode Solution179
834Make Two Arrays Equal by Reversing Sub-arrays Leetcode Solution179
835Search a 2D Matrix II Leetcode Solution179
836Missing Element in Sorted Array LeetCode Solution179
837Check if a given array can represent Preorder Traversal of Binary Search Tree179
838Clone Graph LeetCode Solution179
839Crawler Log Folder Leetcode Solution179
840Course Schedule II – LeetCode179
841Insert Delete GetRandom179
842Minimum Swaps to Make Strings Equal Leetcode Solution179
843Sliding Window Maximum179
844Minimum Jumps to Reach Home LeetCode Solution179
845Split Array Into Consecutive Subsequences179
846Permutation Coefficient179
847Daily Temperatures Leetcode Solution179
848Subsequence of Size K With the Largest Even Sum LeetCode Solution179
849LCS (Longest Common Subsequence) of three strings179
850Valid Parentheses Leetcode Solution178
851Guess Number Higher or Lower LeetCode Solution178
852Remove Duplicates from Sorted List LeetCode Solution178
853Sort Array By Parity II Leetcode Solution178
854Number Complement Leetcode Solution178
855Binary Search Tree Search and Insertion178
856Check if an Array is Stack Sortable178
857Distinct adjacent elements in an array178
858Remove Duplicates from Sorted List II178
859Find the Only Repetitive Element Between 1 to N-1178
860Find Maximum Sum Possible Equal Sum of Three Stacks178
861Iterative Preorder Traversal178
862Tree Traversal (Preorder, Inorder & Postorder)178
863Alien Dictionary LeetCode Solution178
864Minimum number of jumps to reach end177
865Spiral Matrix II Leetcode Solution177
866Count Distinct Elements in Every Window of Size K177
867An Interesting Method to generate Binary Numbers from 1 to n177
868Check if stack elements are pairwise consecutive177
869Get Maximum in Generated Array Leetcode Solution177
870Minimum Sum Path in a Triangle177
871Sum of nearest smaller and greater number177
872Interval Tree177
873Can Place Flowers LeetCode Solution176
874Subset with sum divisible by m176
875Union and Intersection of two Linked Lists176
876Dividing Array into Pairs With Sum Divisible by K176
877Minimum swaps to make sequences increasing176
878Find the subarray with least average176
879Count Negative Numbers in a Sorted Matrix LeetCode Solution176
880Path with maximum average value176
881Sort Colors176
882Matrix Chain Multiplication176
883Range LCM Queries176
884Iterative Postorder Traversal Using Two Stacks176
885Partition List Leetcode Solution176
886Maximum Score After Splitting a String Leetcode Solution175
887Rearrange Array such that arr[i] >= arr[j] if i is even and arr[i] <= arr[j] if i is odd and j < i175
888Diagonal Traversal LeetCode Solution175
889GCDs of given index ranges in an array175
890Design a Stack With Increment Operation Leetcode Solution175
891Friends Pairing Problem175
892Time Based Key-Value Store LeetCode Solution175
893Find all triplets with zero sum175
894Pattern Occurrences using Stack175
895Water Bottles Leetcode Solution175
896Minesweeper LeetCode Solution175
897Level order Traversal in Spiral Form175
898Sequences of given length where every element is more than or equal to twice of previous175
899Convert BST to Min Heap175
900Count ways to reach the nth stair using step 1, 2 or 3175
901Least Number of Unique Integers after K Removals Leetcode Solution175
902Sorted Array to Balanced BST175
903Infix to Postfix175
904Insert into a Binary Search Tree Leetcode Solution175
905Brightest Position on Street LeetCode Solution175
906Number of Distinct Islands Leetcode Solution174
907Find distance between two nodes of a Binary Tree174
908All Unique Triplets that Sum up to a Given Value174
909Diagonal Traverse LeetCode Solution174
910Maximum size subarray sum equals k174
911Best Time to Buy and Sell Stock with Transaction Fee Leetcode Solution174
912Check whether a given Binary Tree is Complete or not174
913Delete And Earn174
914Destination City Leetcode Solution173
915Median of Two Sorted Arrays173
916Shortest Word Distance Leetcode Solution173
917Guess Number Higher or Lower II173
918A program to check if a binary tree is BST or not173
919Range sum queries without updates173
920Set Matrix Zeroes Leetcode Solution173
921Check if any two intervals overlap among a given set of intervals172
922Binary array after M range toggle operations172
923Difference Array | Range update query in O(1)172
924Lexicographical Numbers Leetcode Solution172
925Count Submatrices With All Ones LeetCode Solution172
926Rearrange Spaces Between Words Leetcode Solution172
927Robot Bounded In Circle LeetCode Solution172
928Find Common Characters Leetcode Solution172
929Rearrange an Array Such that arr[i] is equal to i172
930Binary Tree Longest Consecutive Sequence LeetCode Solution172
931Three way partitioning of an array around a given range171
932Product of Array Except Self LeetCode Solution171
933Alien Dictionary171
934K’th Largest Element in BST when modification to BST is not allowed171
935Convert Sorted List to Binary Search Tree171
936Averages of Levels in Binary Tree171
937Queue using Stacks171
938Special Positions in a Binary Matrix Leetcode Solution171
9394Sum171
940Construct the Rectangle Leetcode Solution171
941My Calendar I LeetCode Solution171
942Check if the given array can represent Level Order Traversal of Binary Search Tree171
9433 Sum170
944Palindrome Partitioning Leetcode Solution170
945Analyze User Website Visit Pattern LeetCode Solution170
946Construct Complete Binary Tree from its Linked List Representation170
947Longest Palindromic Subsequence170
948Spiral Matrix III LeetCode Solution170
949Trim a Binary Search Tree170
950Three Consecutive Odds Leetcode Solution170
951Count Subarrays with Same Even and Odd Elements170
952Largest area rectangular sub-matrix with equal number of 1’s and 0’s169
953Merge Two Sorted Lists Leetcode169
954Remove Duplicates from Sorted List II LeetCode Solution169
955Minimum Height Trees LeetCode Solution169
956Vertical sum in a given binary tree169
957Maximum Frequency Stack Leetcode Solution169
958Intersection of Two Linked Lists LeetCode Solution169
959Number of Equivalent Domino Pairs Leetcode Solution169
960Word Break169
961BFS for Disconnected Graph169
962Create Maximum Number169
963Queries for GCD of all numbers of an array except elements in a given range168
964Print Fibonacci sequence using 2 variables168
965Count Primes in Ranges168
966Symmetric Tree168
967Maximize Sum of Array after K Negations Leetcode Solution168
968Strongly Connected Component168
969Climbing stairs168
970Reverse Nodes in K-Group167
971Sign of the Product of an Array LeetCode Solution167
972Asteroid Collision LeetCode Solution167
973Depth First Search (DFS) for a Graph167
974Serialize and Deserialize Binary Tree167
975Kth Smallest Element in a BST Leetcode Solution167
976Combination Sum IV LeetCode Solution167
977Design Hit Counter LeetCode Solution167
978Merging Intervals167
979Maximum Product Subarray167
980Count subarrays where second highest lie before highest167
981Longest subsequence such that difference between adjacents is one167
982Maximum Subarray Sum Excluding Certain Elements167
983Number of palindromic paths in a matrix167
984Construct BST from its given Level Order Traversal167
985Count pair with Given Sum167
986Unique Paths II167
987Valid Boomerang Leetcode Solution166
988K Closest Points to Origin Leetcode Solution166
989Longest Nice Substring LeetCode Solution166
990Breadth First Search (BFS) for a Graph166
991K maximum sums of overlapping contiguous sub-arrays166
992Stone Game II Leetcode166
993Maximum Length of Repeated Subarray166
994Largest Number Leetcode Solution166
995String Matching in an Array Leetcode Solution165
996Maximum length subsequence with difference between adjacent elements as either 0 or 1165
997Can Make Arithmetic Progression From Sequence Leetcode Solution165
998Height of a generic tree from parent array165
999Count pairs from two sorted arrays whose sum is equal to a given value x165
1000Find number of pairs in an array such that their XOR is 0165
1001Find Maximum of Minimum for Every Window Size in a Given Array165
1002Collect maximum points in a grid using two traversals165
1003Scramble String LeetCode Solution165
1004Find the First Circular Tour that visits all the Petrol Pumps165
1005Binary Search Tree Delete Operation165
1006The Maze III LeetCode Solution165
1007Next Permutation Leetcode Solution165
1008Remove Duplicates from Sorted Array II Leetcode Solution164
1009Shortest Completing Word Leetcode Solution164
1010Maximum Product of Indexes of Next Greater on Left and Right164
1011Maximum difference between frequency of two elements such that element having greater frequency is also greater164
1012Shuffle 2n integers as a1-b1-a2-b2-a3-b3-..bn without using extra space164
1013Segment Tree164
1014Kth Smallest Element in a Sorted Matrix LeetCode Solution164
1015Largest Substring Between Two Equal Characters Leetcode Solution164
1016Find the Difference Leetcode Solution164
1017Print Next Greater Number of Q queries164
1018Newman-Conway Sequence164
1019Palindrome Partitioning164
1020Invalid Transactions LeetCode Solution163
1021Graph Valid Tree LeetCode Solution163
1022Balanced Binary Tree163
1023Length of Longest Fibonacci Subsequence163
1024Generate Parentheses Leetcode Solution163
1025Friends Of Appropriate Ages LeetCode Solution163
1026Consecutive Characters LeetCode Solution163
1027The Painter’s Partition Problem163
1028Construct Binary Tree from given Parent Array representation163
1029Minimum Index Sum of Two Lists163
1030Find postorder traversal of BST from preorder traversal163
1031Level Order Traversal of Binary Tree163
1032Bitwise AND of Numbers Range LeetCode Solution162
1033Merge Sort162
1034Maximum Number of Ways to Partition an Array LeetCode Solution162
1035Largest divisible pairs subset162
1036Boolean Parenthesization Problem162
1037Validate Binary Search Tree162
1038Check If a Word Occurs As a Prefix of Any Word in a Sentence Leetcode Solution162
1039Boundary Traversal of binary tree162
1040Decrypt String from Alphabet to Integer Mapping Leetcode Solution162
1041Path Sum II LeetCode Solution162
1042Find a Peak Element II LeetCode Solution162
1043Maximum Sum of 3 Non-Overlapping Subarrays162
1044Find Duplicate Subtrees162
1045Rearrange array such that even positioned are greater than odd162
1046Move all negative elements to end in order with extra space allowed162
1047Number Of Longest Increasing Subsequence161
1048Next Greater Element III LeetCode Solution161
1049Custom Sort String Leetcode Solution161
1050First missing positive161
1051Build an Array With Stack Operations Leetcode Solution161
1052Available Captures for Rook Leetcode Solution161
1053Binary Search Tree161
1054Make The String Great Leetcode Solution161
1055Construction of Longest Increasing Subsequence (N log N)161
1056Rearrange an array such that ‘arr[j]’ becomes ‘i’ if ‘arr[i]’ is ‘j’161
1057Compute nCr % p160
1058Insertion in a Binary Tree160
1059Advantages of BST over Hash Table160
1060Iterative Depth First Traversal of Graph160
1061Kill Process LeetCode Solution160
1062Count minimum steps to get the given desired array159
1063Subarrays with K Different Integers Leetcode Solution159
1064Rearrange array such that even index elements are smaller and odd index elements are greater159
1065Determine Whether Matrix Can Be Obtained By Rotation LeetCode Solution159
1066Print Right View of a Binary Tree159
1067Ugly Numbers159
1068Double the first element and move zero to end159
1069Recover Binary Search Tree Leetcode Solution159
1070Add two numbers159
1071Minimum Size Subarray Sum159
1072Regular Expression Matching Regular Expression Matching LeetCode Solution158
1073Morris Inorder Traversal158
1074Remove Palindromic Subsequences Leetcode Solution158
1075Maximum subsequence sum such that no three are consecutive158
1076Defanging an IP Address LeetCode Solution158
1077Smallest Common Region Leetcode Solution158
1078Partition Equal Subset Sum158
1079Find Peak Element158
1080Largest BST Subtree LeetCode Solution158
1081Employee Importance LeetCode Solution158
1082Ugly Number II LeetCode Solution157
1083Remove duplicates from sorted array157
1084Maximum weight transformation of a given string157
1085Level order traversal using two Queues157
1086Find whether a subarray is in form of a mountain or not157
1087Cells with Odd Values in a Matrix LeetCode Solution157
1088Lowest Common Ancestor157
1089Find maximum difference between nearest left and right smaller elements157
1090Super Ugly Number156
1091Populating Next Right Pointers in Each Node Leetcode Solution156
1092Number of siblings of a given Node in n-ary Tree156
1093Palindrome Permutation LeetCode Solution156
1094Palindromic Substrings Leetcode Solution156
1095Valid Tic-Tac-Toe State LeetCode Solution156
1096Implement Trie (Prefix Tree) Leetcode Solution156
1097Find Three Element From Different Three Arrays Such That a + b + c = sum156
1098Maximize Distance to Closest Person LeetCode Solution156
1099Longest Bitonic Subsequence156
1100Binary Tree Right Side View LeetCode Solution156
1101Search in Sorted Rotated Array155
1102Construct Binary Tree from Preorder and Postorder Traversal LeetCode Solution155
1103Number of Closed Islands Leetcode Solution155
1104Bus Routes Leetcode Solution155
1105Find the minimum distance between two numbers155
1106Increasing Triplet Subsequence LeetCode Solution155
1107Print all triplets in sorted array that form AP155
1108Path Sum155
1109Kth Smallest Product of Two Sorted Arrays LeetCode Solution155
1110Clone a Binary Tree with Random Pointers154
1111Reformat The String Leetcode Solution154
1112Iterative method to find ancestors of a given binary tree154
1113Divisible Pairs Counting154
1114Sum of Even Numbers After Queries154
1115Serialize and Deserialize Binary Tree LeetCode Solution154
1116Binary Tree Data Structure154
1117Products of ranges in an array154
1118Prime Palindrome LeetCode Solution153
1119Moser-de Bruijn Sequence153
1120Distinct Subsequences153
1121Thousand Separator Leetcode Solution153
1122Find Smallest Range Containing Elements from k Lists153
1123Search Insert Position153
1124Day of the Year Leetcode Solution153
1125Number of Students Doing Homework at a Given Time Leetcode Solution153
1126Reformat Date LeetCode Solution152
1127Root to Leaf path with target sum Leetcode Solutions152
1128Minimum Score Triangulation of Polygon Leetcode Solution152
1129Longest Repeated Subsequence152
1130Bottom View of a Binary Tree152
1131Minimum sum of squares of character counts in a given string after removing k characters152
1132Queries for Number of Distinct Elements in a Subarray152
1133Mean of Array After Removing Some Elements Leetcode Solution152
1134Longest Subarray of 1’s After Deleting One Element LeetCode Solution152
1135Diameter of N-Ary Tree LeetCode Solution152
1136Count Largest Group Leetcode Solution152
1137Level of Each node in a Tree from source node152
1138Minimum Absolute Difference in BST Leetcode Solution152
1139Maximum Product Subarray151
1140Maximum Product of Splitted Binary Tree LeetCode Solution151
1141Construct K Palindrome Strings LeetCode Solution151
1142Third Maximum Number Leetcode Solution151
1143Find Two Non-overlapping Sub-arrays Each With Target Sum LeetCode Solution151
1144Write Code to Determine if Two Trees are Identical151
1145Longest Increasing Consecutive Subsequence151
1146Maximum Depth Of Binary Tree151
1147Search an Element in Sorted Rotated Array150
1148Minimum Moves to Equal Array Elements LeetCode Solution150
1149Closest Leaf in a Binary Tree LeetCode Solution150
1150Print modified array after executing the commands of addition and subtraction150
1151Find Largest Value in Each Tree Row LeetCode Solution150
1152Constant time range add operation on an array150
1153Compare Strings by Frequency of the Smallest Character Leetcode Solution150
1154Topological Sorting150
1155Priority Queue150
1156Find maximum length Snake sequence150
1157Golomb sequence150
1158Convert BST into a Min-Heap without using array150
1159Kth Smallest Number in Multiplication Table Leetcode Solution149
1160Rotate Array149
1161Contiguous Array LeetCode Solution149
1162Swap Nodes In Pairs149
1163Subset Sum Problem in O(sum) space149
1164Search Suggestions System LeetCode Solution149
1165Count even length binary sequences with same sum of first and second half bits148
1166Check Array Formation Through Concatenation Leetcode Solution148
1167Cutting a Rod148
1168Web Crawler LeetCode Solution148
1169Closest Binary Search Tree Value Leetcode Solution148
1170Factorial Trailing Zeroes LeetCode Solution148
1171Maximum Binary Tree148
1172Maximize sum of consecutive differences in a circular array147
1173Reverse a Path in BST using Queue147
1174Find a sorted subsequence of size 3 in linear time147
1175Remove Max Number of Edges to Keep Graph Fully Traversable Leetcode Solution147
1176Delete Nodes and Return Forest Leetcode Solution147
1177Perfect Squares LeetCode Solution147
1178Decision Tree147
1179Possible Bipartition LeetCode Solution147
1180Replace two consecutive equal values with one greater146
1181Count Pairs Whose Products Exist in Array146
1182Minimum Cost to Move Chips to The Same Position LeetCode Solution146
1183Given a binary tree, how do you remove all the half nodes?146
1184Minimum Sideway Jumps LeetCode Solution146
1185Valid Triangle Number146
1186Transform a BST to Greater sum Tree146
1187Red-Black Tree Introduction146
1188Binary Tree to Binary Search Tree Conversion146
1189Concatenation of Array LeetCode Solution146
1190Matchsticks to Square Leetcode Solution146
1191Merge two BSTs with limited extra space146
1192Minimum Swaps To Make Sequences Increasing LeetCode Solution146
1193Decrease Elements To Make Array Zigzag LeetCode Solution146
1194K’th Largest element in BST using constant extra space145
1195Queries on Probability of Even or Odd Number in given Ranges145
1196Sum of Left Leaves LeetCode Solution145
1197Maximum number of segments of lengths a, b and c145
1198Maximum sum of a path in a Right Number Triangle145
1199Path Crossing Leetcode Solution145
1200First Bad Version145
1201Find the smallest binary digit multiple of given number144
1202Maximum sum bitonic subarray144
1203Number of Orders in the Backlog Leetcode Solution144
1204Divide Two Integers Leetcode Solution144
1205Maximum Value at a Given Index in a Bounded Array LeetCode Solution144
1206Sliding Window Median Leetcode Solution143
1207Valid Perfect Square LeetCode Solution143
1208Maximum sum of pairs with specific difference143
1209Print n terms of Newman-Conway Sequence143
1210Queue Reconstruction by Height143
1211Convert Sorted Array to Binary Search Tree LeetCode Solutions143
1212Lowest Common Ancestor of a Binary Tree Leetcode Solution143
1213Convert Integer to the Sum of Two No-Zero Integers Leetcode Solution143
1214Swapping Nodes in a Linked List Leetcode Solution143
1215Guess The Word142
1216How to print maximum number of A’s using given four keys142
1217Integer Break LeetCode Solution142
1218Find minimum number of merge operations to make an array palindrome142
1219New 21 Game142
1220Moving Stones Until Consecutive Leetcode Solution141
1221Longest Common Prefix Using Word by Word Matching141
1222Graph and its representation141
1223Champagne Tower LeetCode Solution141
1224Array Queries for multiply replacements and product141
1225Range Sum Query using Sparse Table141
1226Find Minimum in Rotated Sorted Array II LeetCode Solution140
1227Range Queries for Longest Correct Bracket Subsequence140
1228Bold Words in String LeetCode Solution140
1229Lowest Common Ancestor in Binary Search Tree140
1230Kth Smallest Element in a BST140
1231Check if all levels of two Binary Tree are anagrams or not140
1232Flatten 2D Vector LeetCode Solution140
1233Contiguous Array139
1234Merge k Sorted Lists Leetcode Solution139
1235Maximum Array from Two given Arrays Keeping Order Same139
1236Maximum Sum Increasing Subsequence139
1237Filter Restaurants by Vegan-Friendly, Price and Distance Leetcode Solution139
1238LRU Cache Leetcode Solution139
1239Mean of range in array138
1240The kth Factor of n Leetcode Solution138
1241Maximize Elements Using Another Array138
1242Missing Number138
1243Maximum Product Subarray138
1244Print modified array after multiple array range increment operations138
1245Design Skiplist LeetCode Solution138
1246BST to a Tree with Sum of all Smaller Keys138
1247Largest Plus Sign Leetcode Solution138
1248Write a function to get the intersection point of two Linked Lists138
1249Minimum Remove to Make Valid Parentheses LeetCode Solution137
1250Count Subsets Having Distinct Even Numbers137
1251Find k-th smallest element in BST (Order Statistics in BST)137
1252Arithmetic Slices II – Subsequence LeetCode Solution137
1253Parallel Courses II LeetCode Solution137
1254Different Ways to Add Parentheses Leetcode Solution136
1255Min Cost Climbing Stairs LeetCode Solution136
1256Graph Cloning136
1257Power of Two135
1258Check if each internal node of a BST has exactly one child135
1259Check in binary array the number represented by a subarray is odd or even135
1260Image Overlap LeetCode Solution135
1261Check Completeness of a Binary Tree LeetCode Solution134
1262Symmetric Tree LeetCode Solution Leetcode Solution134
1263Verify Preorder Serialization of a Binary Tree134
1264Check if two nodes are on the same path in a Tree134
1265Minimum Time to Collect All Apples in a Tree LeetCode Solution134
1266Koko Eating Bananas LeetCode Solution134
1267Check for Identical BSTs without building the trees134
1268Print Ancestors of a Given Binary Tree Node Without Recursion133
1269Smallest Range II Leetcode Solution133
1270Next greater element132
1271Longest Substring Without Repeating Characters Leetcode Solution132
1272Orderly Queue LeetCode Solution132
1273Count and Toggle Queries on a Binary Array132
1274Excel Sheet Column Title LeetCode Solution132
1275Merge Sorted Array131
1276Arranging Coins Leetcode Solution130
1277Vertical Order Traversal of Binary Tree LeetCode Solution130
1278Array Nesting Leetcode Solution130
1279Number of elements less than or equal to a given number in a given subarray130
1280Palindrome Number LeetCode Solution129
1281Largest Submatrix With Rearrangements LeetCode Solution129
1282Palindrome Partitioning129
1283Check if X can give change to every person in the Queue127
1284Peeking Iterator LeetCode Solution127
1285Add Two Numbers II Leetcode Solution127
1286Newman–Shanks–Williams prime126
1287Check If a String Can Break Another String Leetcode Solution126
1288Find the Winner of the Circular Game LeetCode Solution126
1289Continuous Subarray Sum LeetCode Solution126
1290Maximum product of an increasing subsequence126
1291Find maximum average subarray of k length126
1292Range Minimum Query (Square Root Decomposition and Sparse Table)125
1293Insert Delete GetRandom O(1) Leetcode Solution125
1294Queries for Decimal Values of Subarrays of a Binary Array125
1295Number of Subsequences That Satisfy the Given Sum Condition LeetCode solution124
1296Check given array of size n can represent BST of n levels or not123
1297Encoded String With Shortest Length LeetCode Solution123
1298Jump Game IV LeetCode Solution122
1299Binary Tree to Binary Search Tree Conversion using STL set121
1300Number of indexes with equal elements in given range121
1301Convert to Base -2 LeetCode Solution121
1302Minimum Possible Integer After at Most K Adjacent Swaps On Digits LeetCode Solution121
1303Reach a Number LeetCode Solution121
1304Random Pick Index LeetCode Solution121
1305Minimum Number of People to Teach LeetCode Solution121
1306Convert a BST to a Binary Tree such that sum of all greater keys is added to every key119
1307Minimum Total Space Wasted With K Resizing Operations LeetCode Solution118
1308Design Underground System Leetcode Solution116
1309Design A Leaderboard Leetcode Solution116
1310Queries on XOR of greatest odd divisor of the range115
1311Shifting Letters LeetCode Solution115
1312Detect Capital Leetcode Solution112
1313Print Maximum Length Chain of Pairs111
1314Minimum Swaps to Group All 1’s Together Leetcode Solution110
1315Top K Frequent Elements LeetCode Solution110
1316Substring with Concatenation of All Words Leetcode Solution109
1317Count Sub Islands LeetCode Solution108
1318Minimum Path Sum Leetcode Solution108
1319Odd Even Linked List Leetcode Solution107
1320Monotonic Array Leetcode Solution106
1321Binary Tree Inorder Traversal LeetCode Solution104
1322Maximum Population Year LeetCode Solution104
1323Decode String Leetcode Solution103
1324Longest Common Subsequence LeetCode Solution103
1325Find the Town Judge LeetCode Solution102
1326Shortest Unsorted Continuous Subarray LeetCode Solution98
1327Rectangle Overlap LeetCode Solution96
1328Best Meeting Point LeetCode Solution96
1329Sum Root to Leaf Numbers LeetCode Solution93
1330Maximum Population Year LeetCode Solution93
1331Flatten Binary Tree to Linked List LeetCode Solution93
1332Design Add and Search Words Data Structure LeetCode Solution92
1333Insert into a Sorted Circular Linked List LeetCode Solution92
1334Find the Town Judge LeetCode Solution92
1335Stone Game IV LeetCode Solution90
1336Is Graph Bipartite? LeetCode Solution89
1337Score of Parenthesis LeetCode Solution89
1338Valid Triangle Number LeetCode Solution88
1339Range Sum Query 2D – Immutable LeetCode Solution88
1340Reveal Cards In Increasing Order Leetcode Solution88
1341Divide Chocolate LeetCode Solution82
1342Step-By-Step Directions From a Binary Tree Node to Another LeetCode Solution76
1343Range Sum of BST LeetCode Solution71
1344Reverse Integer Leetcode Solution69
1345Rotate String LeetCode Solution66
1346Find K Closest Elements LeetCode Solution66
1347Sort Colors LeetCode Solution65
1348Maximum Side Length of a Square with Sum Less than or Equal to Threshold LeetCode Solution63
1349Excel Sheet Column Number LeetCode Solution60
1350Maximum Size Subarray Sum Equals k Leetcode Solution52
1351H-Index Leetcode Solution47
1352Valid Anagram Leetcode Solution45
1353Camelcase Matching Leetcode Solution45
1354Most Stones Removed with Same Row or Column LeetCode Solution45
1355Sliding Window Maximum LeetCode Solution44
1356Container With Most Water LeetCode Solution43
1357High Five LeetCode Solution43
1358Find Peak Element LeetCode Solution43
1359Group Anagrams LeetCode Solution42
1360Next Permutation LeetCode Solution42
1361Binary Search LeetCode Solution41
1362Pairs of Songs With Total Durations Divisible by 60 LeetCode Solution41
1363Paint House LeetCode Solution40
1364Group Shifted Strings Leetcode Solution37
1365Flatten Binary Tree to Linked List LeetCode Solution37
1366Minimum Number of Arrows to Burst Balloons LeetCode Solution37
1367Sentence Screen Fitting LeetCode Solution37
1368Split Linked List in Parts Leetcode Solution36
1369Valid Triangle Number LeetCode Solution36
1370Next Greater Element I Leetcode Solution36
1371Isomorphic Strings LeetCode Solution35
1372Unique Binary Search Trees LeetCode Solution35
1373The Number of Weak Characters in the Game LeetCode Solution35
1374Count Good Nodes in Binary Tree LeetCode Solution34
1375Peak Index in a Mountain Array LeetCode Solution33
1376Insert Delete GetRandom O(1) – Duplicates allowed LeetCode Solution33
1377Closest Binary Search Tree Value II LeetCode Solution32
1378Swim in Rising Water LeetCode Solution32
1379Best Time to Buy and Sell Stock IV LeetCode Solution31
1380Single Element in a Sorted Array LeetCode Solution31
1381Find First and Last Position of Element in Sorted Array LeetCode Solution29
1382Max Sum of Two Non-Overlapping Subarrays LeetCode Solution29
1383Minimum Increment to Make Array Unique Leetcode Solution29
1384Lowest Common Ancestor of a Binary Search Tree Leetcode Solution28
1385All Possible Full Binary Trees LeetCode Solution28
1386Contains Duplicate LeetCode Solution28
1387Greatest Sum Divisible by Three LeetCode Solution27
1388Remove All Ones With Row and Column Flips Leetcode Solution27
1389Validate Stack Sequences LeetCode Solution27
1390Trapping Rain Water II LeetCode Solution26
1391Detect Squares LeetCode Solution26
1392Stone Game IV LeetCode Solution25
1393Break a Palindrome LeetCode Solution25
1394Implement Rand10() Using Rand7() Leetcode Solution25
1395Implement strStr() LeetCode Solution25
1396Reverse Nodes in k-Group LeetCode Solution25
1397Invert Binary Tree LeetCode Solution24
1398Fibonacci Number LeetCode Solution22
1399Design Bounded Blocking Queue LeetCode Solution21
1400Minimum Number of Operations to Move All Balls to Each Box LeetCode Solution20
1401Total Hamming Distance LeetCode Solution19
1402Unique Paths III LeetCode Solution18
1403Candy LeetCode Solution18
Translate »