├── Shortest Path Using Atmost One Curved Edge └── README.md ├── Divisor Game └── Divisor Game.java ├── 319. Bulb Switcher └── 319. Bulb Switcher.java ├── Make Array Elements Equal └── Make Array Elements Equal.java ├── Matchsticks game └── Matchsticks game.java ├── README.md ├── String's Count └── String's Count.java ├── 258. Add Digits ├── 258. Add Digits.java └── README.md ├── Check whether K-th bit is set or not └── Check whether K-th bit is set or not.java ├── 342. Power of Four ├── 342. Power of Four.java └── Readme.md ├── Find Transition Point └── Find Transition Point.java ├── Reverse Coding └── Reverse Coding.java ├── Power of 2 └── Power of 2.java ├── Maximum Depth Of Binary Tree └── Maximum Depth Of Binary Tree.java ├── 100. Same Tree └── Same Tree.java ├── Binary Modulo └── Binary Modulo.java ├── Check if strings are rotations of each other or not └── Check if strings are rotations of each other or not.java ├── Count Total Setbits └── Count Total Setbits.java ├── 1512. Number of Good Pairs └── 1512. Number of Good Pairs.java ├── 1523. Count Odd Numbers in an Interval Range ├── Count Odd Numbers in an Interval Range.java └── README.md ├── 191. Number of 1 Bits └── 191. Number of 1 Bits.java ├── 338. Counting Bits └── 338. Counting Bits.java ├── 1822. Sign of the Product of an Array └── 1822. Sign of the Product of an Array.java ├── 70. Climbing Stairs └── 70. Climbing Stairs.java ├── Form a number divisible by 3 using array digits └── Form a number divisible by 3 using array digits.java ├── Lucky Numbers └── Lucky Numbers.java ├── Missing in Array └── Missing in Array.java ├── 1688. Count of Matches in Tournament └── 1688. Count of Matches in Tournament.java ├── 3110. Score of a String └── 3110. Score of a String.java ├── 104. Maximum Depth of Binary Tree ├── Maximum Depth of Binary Tree.java └── README.md ├── Number of 1 Bits └── Number of 1 Bits.java ├── Shortest path from 1 to n └── Shortest path from 1 to n.java ├── Min operations └── Min operations.java ├── 344. Reverse String ├── 344. Reverse String.java └── Readme.md ├── Copy Set Bits in Range └── Copy Set Bits in Range.java ├── Last modified ball └── Last modified ball.java ├── 744. Find Smallest Letter Greater Than Target └── 744. Find Smallest Letter Greater Than Target.java ├── Game of XOR └── Game of XOR.java ├── 1823. Find the Winner of the Circular Game └── 1823. Find the Winner of the Circular Game.java ├── Index of an Extra Element └── Index of an Extra Element.java ├── Minimum Steps Required └── Minimum Steps Required.java ├── Modified Game of Nim └── Modified Game of Nim.java ├── Reverse a String ├── Reverse a String.java └── Readme.md ├── Queue Reversal └── Queue Reversal.java ├── Stock buy and sell II └── Stock buy and sell II.java ├── Sum of dependencies in a graph └── Sum of dependencies in a graph.java ├── First element to occur k times └── First element to occur k times.java ├── 1572. Matrix Diagonal Sum └── 1572. Matrix Diagonal Sum.java ├── Rotate Bits └── Rotate Bits.java ├── 58. Length of Last Word └── 58. Length of Last Word.java ├── Column name from a given column number └── Column name from a given column number.java ├── Total count └── Total count.java ├── 912. Sort an Array └── Sort an Array.java ├── 1351. Count Negative Numbers in a Sorted Matrix └── 1351. Count Negative Numbers in a Sorted Matrix.java ├── 55. Jump Game └── 55. Jump Game.java ├── Inorder Traversal and BST └── Inorder Traversal and BST.java ├── Minimum Number └── Minimum Number.java ├── 1732. Find the Highest Altitude └── 1732. Find the Highest Altitude.java ├── 779. K-th Symbol in Grammar └── 779. K-th Symbol in Grammar.java ├── wave array └── wave array.java ├── 1359. Count All Valid Pickup and Delivery Options └── 1359. Count All Valid Pickup and Delivery Options.java ├── 2439. Minimize Maximum of Array └── 2439. Minimize Maximum of Array.java ├── Minimum Operations └── Minimum Operations.java ├── Toeplitz matrix └── Toeplitz matrix.java ├── 201. Bitwise AND of Numbers Range ├── 201. Bitwise AND of Numbers Range.java └── Readme.md ├── 455. Assign Cookies └── 455. Assign Cookies.java ├── Bus Conductor └── Bus Conductor.java ├── Maximum Identical Bowls └── Maximum Identical Bowls.java ├── 2433. Find The Original Array of Prefix Xor └── 2433. Find The Original Array of Prefix Xor.java ├── 389. Find the Difference ├── 389. Find the Difference.java └── Readme.md ├── 458. Poor Pigs └── 458. Poor Pigs.java ├── 59. Spiral Matrix II └── Readme.md ├── Is it Fibonacci └── Is it Fibonacci.java ├── Maximum Sum Problem └── Maximum Sum Problem.java ├── Number of Subarrays of 0's └── Number of Subarrays of 0's.java ├── 226. Invert Binary Tree ├── Invert Binary Tree.java └── README.md ├── Arithmetic Number └── Arithmetic Number.java ├── Play With OR └── Play With OR.java ├── Remainder on dividing by 11 └── Remainder on dividing by 11.java ├── Taxi Booking └── Taxi Booking.java ├── 242. Valid Anagram └── 242. Valid Anagram.java ├── 2849. Determine if a Cell Is Reachable at a Given Time └── 2849. Determine if a Cell Is Reachable at a Given Time.java ├── Count all Possible Path └── Count all Possible Path.java ├── Find first set bit └── Find first set bit.java ├── 237. Delete Node in a Linked List └── 237. Delete Node in a Linked List.java ├── 2678. Number of Senior Citizens └── 2678. Number of Senior Citizens.java ├── Euler circuit and Path └── Euler circuit and Path.java ├── 518. Coin Change II └── 518. Coin Change II.java ├── Number of Rectangles in a Circle ├── Number of Rectangles in a Circle.java └── Readme.md ├── Reverse Bits └── Reverse Bits.java ├── Max Sum without Adjacents └── Max Sum without Adjacents.java ├── Swap the array elements └── Swap the array elements.java ├── 119. Pascal's Triangle II └── 119. Pascal's Triangle II.java ├── 1877. Minimize Maximum Pair Sum in Array └── 1877. Minimize Maximum Pair Sum in Array.java ├── 867. Transpose Matrix └── 867. Transpose Matrix.java ├── 997. Find the Town Judge ├── 997. Find the Town Judge.java └── Find the Town Judge leetcode java.java ├── Height of Binary Tree └── Height of Binary Tree.java ├── Nth Fibonacci Number └── Nth Fibonacci Number.java ├── Is Binary Number Multiple of 3 └── Is Binary Number Multiple of 3.java ├── 1071. Greatest Common Divisor of Strings └── Greatest Common Divisor of Strings.java ├── 387. First Unique Character in a String ├── 387. First Unique Character in a String.java └── Readme.md ├── nCr └── nCr.java ├── 141. Linked List Cycle └── 141. Linked List Cycle.java ├── 1791. Find Center of Star Graph └── 1791. Find Center of Star Graph.java ├── 746. Min Cost Climbing Stairs └── 746. Min Cost Climbing Stairs.java ├── Insert an Element at the Bottom of a Stack └── Insert an Element at the Bottom of a Stack.java ├── 1518. Water Bottles └── 1518. Water Bottles.java ├── Fibonacci series up to Nth term └── Fibonacci series up to Nth term.java ├── Find Total Time Taken └── Find Total Time Taken.java ├── Largest Number formed from an Array └── Largest Number formed from an Array.java ├── k largest elements └── k largest elements.java ├── 1614. Maximum Nesting Depth of the Parentheses └── 1614. Maximum Nesting Depth of the Parentheses.java ├── 1833. Maximum Ice Cream Bars └── Maximum Ice Cream Bars.java ├── 2706. Buy Two Chocolates └── 2706. Buy Two Chocolates.java ├── 392. Is Subsequence └── 392. Is Subsequence.java ├── Absolute difference divisible by K └── Absolute difference divisible by K.java ├── Armstrong Numbers └── Armstrong Numbers.java ├── Nth catalan number └── Nth catalan number.java ├── 1502. Can Make Arithmetic Progression From Sequence └── 1502. Can Make Arithmetic Progression From Sequence.java ├── 168. Excel Sheet Column Title ├── 168. Excel Sheet Column Title.java └── Readme.md ├── 260. Single Number III └── 260. Single Number III.java ├── 1266. Minimum Time Visiting All Points └── 1266. Minimum Time Visiting All Points.java ├── 2390. Removing Stars From a String └── 2390. Removing Stars From a String.java ├── 2441. Largest Positive Integer That Exists With Its Negative └── 2441. Largest Positive Integer That Exists With Its Negative.java ├── How Many X's? └── How Many X's?.java ├── Reach the Nth point └── Reach the Nth point.java ├── 1561. Maximum Number of Coins You Can Get └── 1561. Maximum Number of Coins You Can Get.java ├── 205. Isomorphic Strings └── 205. Isomorphic Strings.java ├── Minimum Integer └── Minimum Integer.java ├── Transform to Sum Tree └── Transform to Sum Tree.java ├── Transpose of Matrix └── Transpose of Matrix.java ├── 1402. Reducing Dishes └── 1402. Reducing Dishes.java ├── 1701. Average Waiting Time └── 1701. Average Waiting Time.java ├── 62. Unique Paths └── 62. Unique Paths.java ├── Lowest Common Ancestor in a BST └── Lowest Common Ancestor in a BST.java ├── Maximize dot product └── Maximize dot product.java ├── Maximum Diamonds └── Maximum Diamonds.java ├── Remove Duplicates ├── Remove Duplicates.java └── Readme.md ├── 977. Squares of a Sorted Array └── 977. Squares of a Sorted Array.java ├── Delete without head pointer └── Delete without head pointer.java ├── Queue Operations └── Queue Operations.java ├── Type it!.java └── Type it!.java ├── 2348. Number of Zero-Filled Subarrays └── 2348. Number of Zero-Filled Subarrays.java ├── 2485. Find the Pivot Integer └── 2485. Find the Pivot Integer.java ├── 35. Search Insert Position └── Search Insert Position.java ├── Reach a given score └── Reach a given score.java ├── Remove duplicate element from sorted Linked List └── Remove duplicate element from sorted Linked List.java ├── 67. Add Binary └── README.md ├── Find the Highest number └── Find the Highest number.java ├── Print Pattern └── Print Pattern.java ├── Rohan's Love for Matrix └── Rohan's Love for Matrix.java ├── 2540. Minimum Common Value └── 2540. Minimum Common Value.java ├── 3075. Maximize Happiness of Selected Children └── 3075. Maximize Happiness of Selected Children.java ├── 118. Pascal's Triangle ├── Readme.md └── 118. Pascal's Triangle.java ├── 1436. Destination City └── 1436. Destination City.java ├── 215. Kth Largest Element in an Array ├── 215. Kth Largest Element in an Array.java └── Readme.md ├── 409. Longest Palindrome └── 409. Longest Palindrome.java ├── Facing the sun └── Facing the sun.java ├── 1470. Shuffle the Array └── Shuffle the Array.java ├── 343. Integer Break ├── Readme.md └── 343. Integer Break.java ├── 5. Longest Palindromic Substring └── Readme.md ├── 513. Find Bottom Left Tree Value └── Readme.md ├── 930. Binary Subarrays With Sum └── 930. Binary Subarrays With Sum.java ├── Make array elements unique └── Make array elements unique.java ├── 145. Binary Tree Postorder Traversal ├── 145. Binary Tree Postorder Traversal.java └── Readme.md ├── 198. House Robber └── 198. House Robber.java ├── 767. Reorganize String └── Readme.md ├── Count possible ways to construct buildings └── Count possible ways to construct buildings.java ├── Minimize the sum └── Minimize the sum.java ├── Stickler Thief └── Stickler Thief.java ├── 111. Minimum Depth of Binary Tree └── 111. Minimum Depth of Binary Tree.java ├── 1491. Average Salary Excluding the Minimum and Maximum Salary └── 1491. Average Salary Excluding the Minimum and Maximum Salary.java ├── 896. Monotonic Array └── 896. Monotonic Array.java ├── Cutting Rectangles └── Cutting Rectangles.java ├── Sum of all substrings of a number └── Sum of all substrings of a number.java ├── 101. Symmetric Tree ├── 101. Symmetric Tree.java └── README.md ├── 2073. Time Needed to Buy Tickets └── 2073. Time Needed to Buy Tickets.java ├── Does array represent Heap └── Does array represent Heap.java ├── Non Repeating Character └── Non Repeating Character.java ├── You and your books └── You and your books.java ├── Bit Magic └── Bit Magic.java ├── Chocolate Distribution Problem └── Chocolate Distribution Problem.java ├── Implement Atoi └── Implement Atoi.java ├── Make the array beautiful └── Make the array beautiful.java ├── Print first n Fibonacci Numbers └── Print first n Fibonacci Numbers.java ├── Unique Number of Occurrences └── Unique Number of Occurrences.java ├── 137. Single Number II └── 137. Single Number II.java ├── 714. Best Time to Buy and Sell Stock with Transaction Fee └── 714. Best Time to Buy and Sell Stock with Transaction Fee.java ├── 946. Validate Stack Sequences └── 946. Validate Stack Sequences.java ├── Array to BST └── Array to BST.java ├── Count More than n by k Occurences └── Count More than n by k Occurences.java ├── Middle of a Linked List └── Middle of a Linked List.java ├── 1207. Unique Number of Occurrences └── 1207. Unique Number of Occurrences.java ├── 1662. Check If Two String Arrays are Equivalent └── 1662. Check If Two String Arrays are Equivalent.java ├── 2000. Reverse Prefix of Word └── 2000. Reverse Prefix of Word.java ├── 2864. Maximum Odd Binary Number └── 2864. Maximum Odd Binary Number.java ├── 989. Add to Array-Form of Integer └── Add to Array-Form of Integer .java ├── 1347. Minimum Number of Steps to Make Two Strings Anagram └── 1347. Minimum Number of Steps to Make Two Strings Anagram.java ├── 2971. Find Polygon With the Largest Perimeter └── 2971. Find Polygon With the Largest Perimeter.java ├── 628. Maximum Product of Three Numbers └── README.md ├── 646. Maximum Length of Pair Chain └── 646. Maximum Length of Pair Chain.java ├── 881. Boats to Save People └── 881. Boats to Save People.java ├── Floor in BST └── Floor in BST.java ├── Least Prime Factor └── Least Prime Factor.java ├── Rotate a Linked List └── Rotate a Linked List.java ├── Segregate 0s and 1s └── Segregate 0s and 1s.java ├── 1750. Minimum Length of String After Deleting Similar Ends └── 1750. Minimum Length of String After Deleting Similar Ends.java ├── 1897. Redistribute Characters to Make All Strings Equal └── 1897. Redistribute Characters to Make All Strings Equal.java ├── 95. Unique Binary Search Trees II └── Readme.md ├── Consecutive 1's not allowed └── Consecutive 1's not allowed.java ├── Subarray with 0 sum └── Subarray with 0 sum.java ├── 121. Best Time to Buy and Sell Stock └── Best Time to Buy and Sell Stock.java ├── 1642. Furthest Building You Can Reach └── 1642. Furthest Building You Can Reach.java ├── 231. Power of Two ├── Readme.md └── 231. Power of Two.java ├── 515. Find Largest Value in Each Tree Row └── Readme.md ├── Determine if Two Trees are Identical └── Determine if Two Trees are Identical.java ├── Distinct Coloring └── Distinct Coloring.java ├── Predict the Column └── Predict the Column.java ├── Recursive sequence └── Recursive sequence │ └── Recursive sequence.java ├── Seating Arrangement └── Seating Arrangement.java ├── 1464. Maximum Product of Two Elements in an Array └── 1464. Maximum Product of Two Elements in an Array.java ├── 28. Find the Index of the First Occurrence in a String └── Find the Index of the First Occurrence in a String.java ├── Array Operations └── Array Operations.java ├── Walls Coloring └── Walls Coloring.java ├── 1137. N-th Tribonacci Number ├── N-th Tribonacci Number .java └── README.md ├── 234. Palindrome Linked List └── Readme.md ├── Count the elements └── Count the elements.java ├── Count ways to N'th Stair └── Count ways to N'th Stair.java ├── Equilibrium Point └── Equilibrium Point.java ├── Find missing in second array └── Find missing in second array.java ├── Max Sum Subarray of size K └── Max Sum Subarray of size K.java ├── Perfect Numbers └── Perfect Numbers.java ├── 1441. Build an Array With Stack Operations └── 1441. Build an Array With Stack Operations.java ├── 1481. Least Number of Unique Integers after K Removals └── 1481. Least Number of Unique Integers after K Removals.java ├── 2870. Minimum Number of Operations to Make Array Empty └── 2870. Minimum Number of Operations to Make Array Empty.java ├── Row with minimum number of 1's └── Row with minimum number of 1's.java ├── 1598. Crawler Log Folder └── 1598. Crawler Log Folder.java ├── 1700. Number of Students Unable to Eat Lunch └── 1700. Number of Students Unable to Eat Lunch.java ├── 316. Remove Duplicate Letters └── Readme.md ├── 645. Set Mismatch └── 645. Set Mismatch.java ├── 783. Minimum Distance Between BST Nodes └── README.md ├── Delete middle element of a stack └── Delete middle element of a stack.java ├── Frogs and Jumps └── Frogs and Jumps.java ├── Sort 0s, 1s and 2s └── Readme.md ├── 229. Majority Element II └── Readme.md ├── 1291. Sequential Digits └── Readme.md ├── 144. Binary Tree Preorder Traversal └── Binary Tree Preorder Traversal.java ├── 1550. Three Consecutive Odds ├── Readme.md └── 1550. Three Consecutive Odds.java ├── 1768. Merge Strings Alternately └── 1768. Merge Strings Alternately.java ├── 46. Permutations └── Readme.md ├── 704. Binary Search └── 704. Binary Search.java ├── Arranging the array └── Arranging the array.java ├── Maximum sum of hour glass └── Maximum sum of hour glass.java ├── Print adjacency list └── Print adjacency list.java ├── Count numbers containing 4 ├── Readme.md └── Count numbers containing 4.java ├── Find Number └── Find Number.java ├── Juggler Sequence └── Juggler Sequence.java ├── Kth largest element in BST └── Kth largest element in BST.java ├── Maximum sum increasing subsequence └── Maximum sum increasing subsequence.java ├── Move all zeroes to end of array └── Move all zeroes to end of array.java ├── Rearrange an array with O(1) extra space └── Rearrange an array with O(1) extra space.java ├── Remaining String └── Remaining String.java ├── Remove duplicates from an unsorted linked list └── Remove duplicates from an unsorted linked list.java ├── Sum of Products └── Sum of Products.java ├── Xoring and Clearing └── Xoring and Clearing.java ├── 1431. Kids With the Greatest Number of Candies └── 1431. Kids With the Greatest Number of Candies.java ├── 1535. Find the Winner of an Array Game └── 1535. Find the Winner of an Array Game.java ├── 1758. Minimum Changes To Make Alternating Binary String └── 1758. Minimum Changes To Make Alternating Binary String.java ├── Sum of leaf nodes in BST └── Sum of leaf nodes in BST.java ├── 1557. Minimum Number of Vertices to Reach All Nodes └── 1557. Minimum Number of Vertices to Reach All Nodes.java ├── 1759. Count Number of Homogenous Substrings └── 1759. Count Number of Homogenous Substrings.java ├── Minimize the Heights II └── Minimize the Heights II.java ├── Prefix match with other strings └── Prefix match with other strings.java ├── 225. Implement Stack using Queues └── 225. Implement Stack using Queues.java ├── 2551. Put Marbles in Bags └── 2551. Put Marbles in Bags.java ├── 905. Sort Array By Parity └── Readme.md ├── Count Cyclic Paths └── Count Cyclic Paths.java ├── DFS of Graph └── DFS of Graph.java ├── Mirror Tree └── Mirror Tree.java ├── 2181. Merge Nodes in Between Zeros └── 2181. Merge Nodes in Between Zeros.java ├── 443. String Compression └── String Compression .java ├── 629. K Inverse Pairs Array └── 629. K Inverse Pairs Array.java ├── Count the Substrings └── Count the Substrings.java ├── 2149. Rearrange Array Elements by Sign └── 2149. Rearrange Array Elements by Sign.java ├── 852. Peak Index in a Mountain Array └── 852. Peak Index in a Mountain Array.java ├── Minimum times A has to be repeated such that B is a substring of it └── Minimum times A has to be repeated such that B is a substring of it.java ├── String Mirror └── String Mirror.java ├── 134. Gas Station └── Gas Station.java ├── 287. Find the Duplicate Number └── 287. Find the Duplicate Number.java ├── 525. Contiguous Array ├── 525. Contiguous Array.java └── Readme.md ├── Form a palindrome └── Readme.md ├── Minimum distance between two numbers └── Minimum distance between two numbers.java ├── Remove all duplicates from a given string └── Remove all duplicates from a given string.java ├── Remove the balls └── Remove the balls.java ├── Word Break └── Word Break.java ├── 1716. Calculate Money in Leetcode Bank └── 1716. Calculate Money in Leetcode Bank.java ├── 349. Intersection of Two Arrays └── Readme.md ├── 45. Jump Game II └── Jump Game II .java ├── 1539. Kth Missing Positive Number └── Kth Missing Positive Number.java ├── 1544. Make The String Great └── 1544. Make The String Great.java ├── 799. Champagne Tower └── 799. Champagne Tower.java ├── 879. Profitable Schemes └── 879. Profitable Schemes.java ├── Check if string is rotated by two places └── Check if string is rotated by two places.java ├── 1887. Reduction Operations to Make the Array Elements Equal └── 1887. Reduction Operations to Make the Array Elements Equal.java ├── 209. Minimum Size Subarray Sum └── 209. Minimum Size Subarray Sum.java ├── 542. 01 Matrix └── Readme.md ├── 75. Sort Colors └── 75. Sort Colors.java ├── 94. Binary Tree Inorder Traversal └── Readme.md ├── Reverse First K elements of Queue └── Reverse First K elements of Queue.java ├── Tree Transformation └── Tree Transformation.java ├── 165. Compare Version Numbers └── 165. Compare Version Numbers.java ├── 459. Repeated Substring Pattern └── 459. Repeated Substring Pattern.java ├── Power Set └── Power Set.java ├── 169. Majority Element └── Readme.md ├── 567. Permutation in String └── README.md ├── Even Swap └── Even Swap.java ├── K-Pangrams └── K-Pangrams.java ├── Maximum Possible Value └── Maximum Possible Value.java ├── Transform to prime └── Transform to prime.java ├── 1424. Diagonal Traverse II └── Readme.md ├── 1603. Design Parking System └── 1603. Design Parking System.java ├── 1838. Frequency of the Most Frequent Element └── 1838. Frequency of the Most Frequent Element.java ├── 452. Minimum Number of Arrows to Burst Balloons └── Minimum Number of Arrows to Burst Balloons.java ├── 785. Is Graph Bipartite? └── 785. Is Graph Bipartite?.java ├── 86. Partition List └── Readme.md ├── 206. Reverse Linked List └── Readme.md ├── 2444. Count Subarrays With Fixed Bounds └── Count Subarrays With Fixed Bounds.java ├── 92. Reverse Linked List II └── Readme.md ├── Check if a number is divisible by 8 └── Check if a number is divisible by 8.java ├── Convert an array to reduced form └── Convert an array to reduced form.java └── 1046. Last Stone Weight └── 1046. Last Stone Weight.java /Shortest Path Using Atmost One Curved Edge/README.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Divisor Game/Divisor Game.java: -------------------------------------------------------------------------------- 1 | 2 | class Solution { 3 | public static boolean divisorGame(int n) { 4 | return n%2 == 0 ; 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /319. Bulb Switcher/319. Bulb Switcher.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int bulbSwitch(int n) { 3 | return (int)Math.sqrt(n); 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /Make Array Elements Equal/Make Array Elements Equal.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public long minOperations(int N) { 3 | return ((long )N*N)/4; 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /Matchsticks game/Matchsticks game.java: -------------------------------------------------------------------------------- 1 | 2 | class Solution { 3 | static int matchGame(Long N) { 4 | 5 | return (N%5!=0)?(int)(N%5):-1; 6 | } 7 | }; 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Problem-of-the-day-of-Leetcode-and-GeeksForGeeks 2 | Here the Daily problem Solution will appear of Leetcode as well as GFG . 3 | Please Leave star to motivate me . 4 | -------------------------------------------------------------------------------- /String's Count/String's Count.java: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | static long countStr(long n) 4 | { 5 | long ans = 1 + n + n + n*(n-1) + n*((n-1)*(n-2))/2 + (n*(n-1))/2 ; 6 | 7 | return ans; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /258. Add Digits/258. Add Digits.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int addDigits(int num) { 3 | if(num ==0) 4 | return 0 ; 5 | if(num%9==0)return 9 ; 6 | 7 | return num%9; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /Check whether K-th bit is set or not/Check whether K-th bit is set or not.java: -------------------------------------------------------------------------------- 1 | 2 | class CheckBit 3 | { 4 | 5 | static boolean checkKthBit(int n, int k) 6 | { 7 | return ((n>>k)&1) == 1 ; 8 | } 9 | 10 | } 11 | -------------------------------------------------------------------------------- /342. Power of Four/342. Power of Four.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isPowerOfFour(int n) { 3 | double no = Math.log(n)/Math.log(4) ; 4 | double res = no - (int)no ; 5 | 6 | return res==0.0; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /Find Transition Point/Find Transition Point.java: -------------------------------------------------------------------------------- 1 | 2 | class Solution { 3 | int transitionPoint(int arr[], int n) { 4 | for(int i = 0 ;i=0 ; 8 | } 9 | 10 | } 11 | -------------------------------------------------------------------------------- /Count Total Setbits/Count Total Setbits.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public static long countBits(long N) { 3 | long count=0; 4 | for(long i=1;i<=N;i++) 5 | { 6 | count += Long.bitCount(i); 7 | } 8 | return count; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /1512. Number of Good Pairs/1512. Number of Good Pairs.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int numIdenticalPairs(int[] A) { 3 | int ans = 0, cnt[] = new int[101]; 4 | for (int a: A) { 5 | ans += cnt[a]++; 6 | } 7 | return ans; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /1523. Count Odd Numbers in an Interval Range/Count Odd Numbers in an Interval Range.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int countOdds(int low, int high) { 3 | if(low%2!=0 || high%2!=0 ) 4 | return ((int)(high-low)/2+1) ; 5 | else 6 | return (int)(high-low)/2; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /191. Number of 1 Bits/191. Number of 1 Bits.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int hammingWeight(int n) { 3 | int cnt = 0 ; 4 | 5 | for(int i = 0 ;i<32;i++) 6 | { 7 | if(((1<>1]+(i%2) ; 7 | } 8 | return ans ; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /1822. Sign of the Product of an Array/1822. Sign of the Product of an Array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int arraySign(int[] nums) { 3 | var sign = 1; 4 | 5 | for (var n : nums) { 6 | if (n == 0) return 0; 7 | sign *= n > 0 ? 1 : -1; 8 | } 9 | return sign; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /70. Climbing Stairs/70. Climbing Stairs.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int climbStairs(int n) { 3 | int dp[] = new int[n+1] ; 4 | 5 | dp[0] = 1 ; 6 | dp[1] = 1 ; 7 | 8 | for(int i = 2;i<=n;i++) 9 | dp[i] = dp[i-1]+dp[i-2] ; 10 | 11 | return dp[n]; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Form a number divisible by 3 using array digits/Form a number divisible by 3 using array digits.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | static int isPossible(int N, int arr[]) { 3 | long sum = 0 ; 4 | for(int i : arr) 5 | sum += i ; 6 | 7 | return (sum%3==0) ? 1 : 0 ; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /Lucky Numbers/Lucky Numbers.java: -------------------------------------------------------------------------------- 1 | 2 | 3 | class Solution 4 | { 5 | public static boolean isLucky(int n) 6 | { 7 | for(int i = 2;i<=n;i++){ 8 | if(n%i == 0) 9 | return false ; 10 | 11 | n = n - n/i ; 12 | } 13 | return true; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Missing in Array/Missing in Array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | 3 | 4 | int missingNumber(int n, int arr[]) { 5 | 6 | int xor = 0 ; 7 | 8 | for(int i = 1;i<=n;i++)xor^=i; 9 | 10 | for(int i : arr) 11 | xor ^= i ; 12 | 13 | return xor ; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /1688. Count of Matches in Tournament/1688. Count of Matches in Tournament.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int numberOfMatches(int n) { 3 | int ans = 0 ; 4 | 5 | while(n>1){ 6 | ans+=(n/2) ; 7 | n = n - n/2 ; 8 | } 9 | 10 | return ans; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /3110. Score of a String/3110. Score of a String.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int scoreOfString(String s) { 3 | int sum = 0 ; 4 | 5 | for(int i = 0 ;i0) 8 | { 9 | cnt++ ; 10 | N = (N)&(N-1) ; 11 | } 12 | return cnt; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Shortest path from 1 to n/Shortest path from 1 to n.java: -------------------------------------------------------------------------------- 1 | 2 | class Solution{ 3 | public int minStep(int n) 4 | { 5 | int res = 0 ; 6 | 7 | while(n>=3) 8 | { 9 | res += n%3 + 1 ; 10 | n /=3 ; 11 | } 12 | 13 | return res+(n-1); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Min operations/Min operations.java: -------------------------------------------------------------------------------- 1 | 2 | class Solution { 3 | public static int solve(int a, int b) { 4 | if(a==b) 5 | return 0 ; 6 | if((a&b)==Math.min(a,b)) 7 | return 1; 8 | 9 | if((a&b)>=0 && (a&b)=0;i--) 5 | { 6 | if(A[i]<9) 7 | return i+1 ; 8 | } 9 | return 0; 10 | } 11 | }; 12 | 13 | 14 | 15 | Time Complexity -> O(n) 16 | Space Complexity -> O(n) 17 | -------------------------------------------------------------------------------- /744. Find Smallest Letter Greater Than Target/744. Find Smallest Letter Greater Than Target.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public char nextGreatestLetter(char[] letters, char target) { 3 | 4 | for(char ch : letters){ 5 | if(ch>target) 6 | return ch ; 7 | } 8 | return letters[0]; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Game of XOR/Game of XOR.java: -------------------------------------------------------------------------------- 1 | 2 | class Solution { 3 | static int gameOfXor(int N , int[] A) { 4 | int xor = 0 ; 5 | 6 | if(N%2==0)return 0 ; 7 | 8 | for(int i =0 ;i=0;i--) 8 | sb.append(str.charAt(i)) ; 9 | 10 | 11 | return sb.toString(); 12 | } 13 | 14 | 15 | } 16 | -------------------------------------------------------------------------------- /Queue Reversal/Queue Reversal.java: -------------------------------------------------------------------------------- 1 | class GfG{ 2 | 3 | public Queue rev(Queue q){ 4 | help(q); 5 | return q; 6 | } 7 | void help(Queueq) 8 | { 9 | if(q.size()==0) 10 | return ; 11 | 12 | int x = q.poll() ; 13 | help(q) ; 14 | q.add(x); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Stock buy and sell II/Stock buy and sell II.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public static int stockBuyAndSell(int n, int[] prices) { 3 | int ans = 0 ; 4 | for(int i = 0 ;iprices[i]) 7 | ans += (prices[i+1]-prices[i]) ; 8 | } 9 | return ans; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Sum of dependencies in a graph/Sum of dependencies in a graph.java: -------------------------------------------------------------------------------- 1 | 2 | class Solution { 3 | int sumOfDependencies(ArrayList> adj, int V) { 4 | int sum = 0 ; 5 | 6 | for(int i = 0 ;i=k)return i; 10 | } 11 | 12 | 13 | return -1; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /1572. Matrix Diagonal Sum/1572. Matrix Diagonal Sum.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int diagonalSum(int[][] mat) { 3 | int n = mat.length; 4 | int sum = 0; 5 | for (int i = 0; i < n; i++) { 6 | sum += mat[i][i]; 7 | if (i != n - i - 1) { 8 | sum += mat[i][n - i - 1]; 9 | } 10 | } 11 | return sum; 12 | }} 13 | -------------------------------------------------------------------------------- /Rotate Bits/Rotate Bits.java: -------------------------------------------------------------------------------- 1 | 2 | class Solution 3 | { 4 | 5 | ArrayList rotate(int N, int D) 6 | { 7 | ArrayListans = new ArrayList<>() ; 8 | 9 | D = D%16 ; 10 | ans.add(((N<>(16-D))) & 65535 ) ; 11 | ans.add(((N>>D)|(N<<(16-D)))&65535 ) ; 12 | 13 | return ans; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /58. Length of Last Word/58. Length of Last Word.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int lengthOfLastWord(String s) { 3 | s = s.trim() ; 4 | 5 | int cnt = 0 ; 6 | 7 | for(int i = s.length()-1;i>=0;i--){ 8 | if(s.charAt(i)!= ' ') 9 | cnt++; 10 | else break; 11 | } 12 | 13 | return cnt; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Column name from a given column number/Column name from a given column number.java: -------------------------------------------------------------------------------- 1 | 2 | class Solution 3 | { 4 | String colName (long n) 5 | { 6 | String str=""; 7 | int i=0; 8 | while(n>0){ 9 | char r=(char)('A'+(n-1)%26); 10 | str=r+str; 11 | n=(n-1)/26; 12 | } 13 | return str; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Total count/Total count.java: -------------------------------------------------------------------------------- 1 | 2 | class Solution { 3 | int totalCount(int k, int[] arr) { 4 | int ans = 0 ; 5 | 6 | for(int i : arr) 7 | { 8 | int div = i / k ; 9 | if((i%k)!=0) 10 | div++; 11 | 12 | ans+= div ; 13 | } 14 | 15 | return ans; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /912. Sort an Array/Sort an Array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] sortArray(int[] nums) { 3 | PriorityQueuepq = new PriorityQueue<>() ; 4 | for(int i:nums) 5 | pq.add(i) ; 6 | 7 | int []ans = new int[nums.length] ; 8 | int c = 0 ; 9 | while(!pq.isEmpty()) 10 | ans[c++] = pq.poll(); 11 | 12 | return ans ; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /1351. Count Negative Numbers in a Sorted Matrix/1351. Count Negative Numbers in a Sorted Matrix.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int countNegatives(int[][] grid) { 3 | int cnt = 0 ; 4 | for(int []i :grid) 5 | { 6 | for(int n = 0 ;narr[i]) 10 | return 0 ; 11 | 12 | temp = arr[i] ; 13 | } 14 | return 1; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Minimum Number/Minimum Number.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public static int minimumNumber(int n, int[] arr) { 3 | int ans =0 ; 4 | for(int i = 0 ;i0){ 9 | if(n %2==0) 10 | n/=2 ; 11 | else 12 | n-- ; 13 | 14 | ans++; 15 | } 16 | 17 | return ans ; 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /Toeplitz matrix/Toeplitz matrix.java: -------------------------------------------------------------------------------- 1 | 2 | class Solution { 3 | 4 | boolean isToeplitz(int mat[][]) { 5 | int n = mat.length, m = mat[0].length ; 6 | for(int i = 0 ;i>= 1; 10 | right >>=1 ; 11 | } 12 | 13 | return right<0) 9 | { 10 | if(sum%n==0) 11 | return n; 12 | 13 | n--; 14 | } 15 | return 0; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /2433. Find The Original Array of Prefix Xor/2433. Find The Original Array of Prefix Xor.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] findArray(int[] pref) { 3 | int []ans = new int[pref.length] ; 4 | 5 | ans[0] = pref[0] ; 6 | 7 | for(int i = 1;i 2 | -------------------- 3 | 4 | 5 | Given a positive integer n, generate an n x n matrix filled with elements from 1 to n2 in spiral order. 6 | 7 | 8 | 9 | Example 1: 10 | 11 | 12 | Input: n = 3 13 | Output: [[1,2,3],[8,9,4],[7,6,5]] 14 | Example 2: 15 | 16 | Input: n = 1 17 | Output: [[1]] 18 | 19 | 20 | Constraints: 21 | 22 | 1 <= n <= 20 23 | -------------------------------------------------------------------------------- /Is it Fibonacci/Is it Fibonacci.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | static long solve(int N, int K, ArrayList GeekNum) { 3 | long sum = 0 ; 4 | for(int i = K;iB) && C<0) || ((A0)) 6 | { 7 | int dif = B- A ; 8 | if(dif%C==0) 9 | return 1 ; 10 | else 11 | return 0 ; 12 | } 13 | else 14 | return 0 ; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Play With OR/Play With OR.java: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | 3 | 4 | 5 | public static int[] game_with_number (int arr[], int n) { 6 | int []ans = new int[n] ; 7 | 8 | for(int i = 0 ;i0){ 11 | cnt++; 12 | if((n&1)==1){ 13 | return cnt ; 14 | } 15 | n>>=1 ; 16 | } 17 | return 0 ; 18 | 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /237. Delete Node in a Linked List/237. Delete Node in a Linked List.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * public class ListNode { 4 | * int val; 5 | * ListNode next; 6 | * ListNode(int x) { val = x; } 7 | * } 8 | */ 9 | class Solution { 10 | public void deleteNode(ListNode node) { 11 | 12 | node.val = node.next.val ; 13 | node.next = node.next.next ; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /2678. Number of Senior Citizens/2678. Number of Senior Citizens.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int countSeniors(String[] details) { 3 | int cnt = 0 ; 4 | 5 | for(String str : details) 6 | { 7 | if((str.charAt(11)-'0' ) > 6)cnt++; 8 | else 9 | if((str.charAt(11)-'0' ) >= 6 && (str.charAt(12)-'0' )>0)cnt++; 10 | } 11 | 12 | return cnt; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Euler circuit and Path/Euler circuit and Path.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int isEulerCircuit(int v, List[] adj) { 3 | int even = 0, odd = 0; 4 | for(int i = 0; i < v; i++) { 5 | if(adj[i].size() % 2 == 0) 6 | even++; 7 | else 8 | odd++; 9 | } 10 | 11 | return even == v ? 2 : odd > 0 && odd == 2 ? 1 : 0; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /518. Coin Change II/518. Coin Change II.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int change(int amount, int[] coins) { 3 | int dp[] = new int[amount+1] ; 4 | dp[0] = 1 ; 5 | for(int i =0 ;i0){ 8 | if((x & 1 ) != 0) 9 | ans += (long) (Math.pow(2,ind)) ; 10 | 11 | x >>= 1 ; 12 | ind--; 13 | 14 | } 15 | 16 | return ans; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /Max Sum without Adjacents/Max Sum without Adjacents.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | int findMaxSum(int arr[], int n) { 3 | int inc = arr[0] ; 4 | int exc = 0 ; 5 | for(int i = 1;i getRow(int rowIndex) { 3 | List ans = new ArrayList<>() ; 4 | for(int i = 0 ;i0 ;j--) 9 | ans.set(j,ans.get(j)+ans.get(j-1) ) ; 10 | } 11 | 12 | return ans; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /1877. Minimize Maximum Pair Sum in Array/1877. Minimize Maximum Pair Sum in Array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minPairSum(int[] nums) { 3 | Arrays.sort(nums) ; 4 | 5 | int l = 0 , h = nums.length -1 ; 6 | 7 | int ans = Integer.MIN_VALUE ; 8 | 9 | while(ln) 6 | return 0; 7 | int mod=1000000007; 8 | int d[]=new int[r+1]; 9 | for(int i=1;i<=n;i++) 10 | { 11 | 12 | d[0]=1; 13 | for(int j=r;j>0;j--) 14 | { 15 | d[j]=(d[j]+d[j-1])%mod; 16 | } 17 | } 18 | return d[r]; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /141. Linked List Cycle/141. Linked List Cycle.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public boolean hasCycle(ListNode head) { 3 | ListNode slow = head ; 4 | ListNode fast = head ; 5 | while(fast != null && fast.next!=null) 6 | { 7 | slow = slow.next ; 8 | fast = fast.next.next ; 9 | if(slow == fast ) 10 | return true ; 11 | } 12 | return false ; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /1791. Find Center of Star Graph/1791. Find Center of Star Graph.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findCenter(int[][] edges) { 3 | int n = edges.length; 4 | int[] freq = new int[edges.length+2]; 5 | for(int []ed : edges){ 6 | freq[ed[0]]++ ; 7 | freq[ed[1]]++; 8 | } 9 | 10 | for(int i = 0 ;i< freq.length;i++) 11 | if( freq[i] == n)return i; 12 | 13 | return -1; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /746. Min Cost Climbing Stairs/746. Min Cost Climbing Stairs.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minCostClimbingStairs(int[] cost) { 3 | int []dp = new int[1001] ; 4 | dp[0] = cost[0] ; 5 | dp[1] = cost[1] ; 6 | 7 | int n = cost.length ; 8 | 9 | 10 | for(int i = 2;i insertAtBottom(Stack st, int x) { 3 | Stack ans = new Stack<>() ; 4 | while(!st.isEmpty()) 5 | ans.push(st.pop()) ; 6 | 7 | st.push(x) ; 8 | 9 | while(!ans.isEmpty()) 10 | st.push(ans.pop()); 11 | 12 | return st; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /1518. Water Bottles/1518. Water Bottles.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int numWaterBottles(int numBottles, int numExchange) { 3 | int ans = numBottles ; 4 | 5 | int emptyBot = numBottles ; 6 | 7 | while(emptyBot>=numExchange){ 8 | ans += emptyBot/numExchange; 9 | int r = emptyBot%numExchange ; 10 | 11 | emptyBot = emptyBot/numExchange + r ; 12 | } 13 | return ans; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Fibonacci series up to Nth term/Fibonacci series up to Nth term.java: -------------------------------------------------------------------------------- 1 | 2 | class Solution { 3 | 4 | int[] Series(int n) { 5 | int ans[] = new int[n+1] ; 6 | int mod = (int)(1e9+7) ; 7 | int a = 0 , b = 1 ; 8 | 9 | for(int i = 0 ;ist = new HashSet<>() ; 6 | int ans = 0; 7 | for(int i = 0 ;i (y+x).compareTo(x+y)) ; 4 | 5 | StringBuilder sb = new StringBuilder() ; 6 | 7 | // for(String s : arr) 8 | // sb.append(s) ; 9 | 10 | // return sb.toString() ; 11 | 12 | 13 | return String.join("",arr); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /k largest elements/k largest elements.java: -------------------------------------------------------------------------------- 1 | 2 | class Solution { 3 | int[] kLargest(int[] arr, int n, int k) { 4 | 5 | PriorityQueuepq=new PriorityQueue<>(); 6 | int ans[]=new int[k]; 7 | for(int i=0;i=i) 9 | { 10 | ans++ ; 11 | coins-=i ; 12 | } 13 | } 14 | return ans ; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /2706. Buy Two Chocolates/2706. Buy Two Chocolates.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int buyChoco(int[] prices, int money) { 3 | Arrays.sort(prices) ; 4 | 5 | for(int i = 1 ;i< prices.length ;i++) 6 | { 7 | if((prices[i]+prices[i-1])<=money) 8 | { 9 | money -= (prices[i]+prices[i-1]) ; 10 | return money ; 11 | } 12 | 13 | } 14 | return money ; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /392. Is Subsequence/392. Is Subsequence.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isSubsequence(String s, String t) { 3 | if(s.length() ==0)return true ; 4 | 5 | int i = 0 , j = 0 ; 6 | 7 | while(i0){ 7 | int digit = temp%10 ; 8 | 9 | arm += ( digit * digit * digit) ; 10 | temp /=10 ; 11 | } 12 | 13 | if(arm == n)return "true"; 14 | 15 | return "false"; 16 | 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Nth catalan number/Nth catalan number.java: -------------------------------------------------------------------------------- 1 | 2 | class Solution { 3 | public static int findCatalan(int n) { 4 | long []dp = new long[n+1] ; 5 | int mod = 1000000007; 6 | dp[0] =1 ; 7 | dp[1] =1 ; 8 | 9 | for(int i = 2;i<=n;i++) 10 | { 11 | for(int j = 0 ;j0) 6 | { 7 | int r = (columnNumber-1)%26 ; 8 | 9 | sb.append((char)(r+'A')) ; 10 | 11 | columnNumber = (columnNumber-1)/26 ; 12 | } 13 | 14 | return sb.reverse().toString(); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /260. Single Number III/260. Single Number III.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] singleNumber(int[] nums) { 3 | int xor = 0 ; 4 | 5 | for(int i : nums) 6 | xor^= i ; 7 | 8 | int mask = xor&(-xor); 9 | int []ans = new int[2] ; 10 | 11 | 12 | for(int i : nums) 13 | if((mask & i) != 0) 14 | ans[0] ^= i ; 15 | else 16 | ans[1] ^= i ; 17 | 18 | return ans; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /1266. Minimum Time Visiting All Points/1266. Minimum Time Visiting All Points.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minTimeToVisitAllPoints(int[][] points) { 3 | int ans = 0 ; 4 | 5 | for(int i = 1 ;ist = new HashSet<>() ; 4 | 5 | for(int i : nums)st.add(i); 6 | 7 | int ans = -1 ; 8 | 9 | for(int i : nums) 10 | { 11 | if(i>ans && st.contains(-i))ans = i; 12 | 13 | } 14 | 15 | return ans; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /How Many X's?/How Many X's?.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | int countX(int L, int R, int X) { 3 | int sum = 0 ; 4 | 5 | for(int i = L+1;i0){ 10 | rem = ( temp%10) ; 11 | if(rem == X) 12 | sum++ ; 13 | temp/=10 ; 14 | } 15 | } 16 | return sum; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /Reach the Nth point/Reach the Nth point.java: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public int nthPoint(int n) 4 | { 5 | if(n<=2)return n ; 6 | 7 | int mod = 1000000007 ; 8 | long a = 1, b = 2 ; 9 | long c = a+b ; 10 | 11 | for(int i = 3;i= 0; i--) { 7 | pansum += satisfaction[i]; 8 | if (pansum < 0) { 9 | break; 10 | } 11 | ans += pansum; 12 | } 13 | return ans; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /1701. Average Waiting Time/1701. Average Waiting Time.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public double averageWaitingTime(int[][] cus) { 3 | int st = 1 ; 4 | double sum = 0 ; 5 | for(int i = 0 ;ist) 8 | st = cus[i][0]; 9 | 10 | st += cus[i][1]; 11 | 12 | sum += (st-cus[i][0]); 13 | } 14 | 15 | return (double)sum / cus.length; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /62. Unique Paths/62. Unique Paths.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int uniquePaths(int m, int n) { 3 | int [][]dp = new int[m][n]; 4 | 5 | for(int i = 0 ;i=0 ;r--){ 12 | for(int c = n-2;c>=0;c--) 13 | dp[r][c] = dp[r][c+1] + dp[r+1][c]; 14 | } 15 | 16 | return dp[0][0]; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Lowest Common Ancestor in a BST/Lowest Common Ancestor in a BST.java: -------------------------------------------------------------------------------- 1 | 2 | class BST 3 | { 4 | 5 | Node LCA(Node root, int n1, int n2) 6 | { 7 | while(root!= null) 8 | { 9 | if(root.data>n1 && root.data>n2) 10 | root = root.left ; 11 | else if(root.datapq = new PriorityQueue<>(Collections.reverseOrder()) ; 5 | for(int i :A) 6 | pq.offer(i) ; 7 | 8 | long ans = 0 ; 9 | for(int i = 0 ;ipq = new PriorityQueue<>() ; 5 | 6 | for(int i : nums) 7 | pq.offer(i*i) ; 8 | 9 | int ind = 0 ; 10 | while(!pq.isEmpty()) 11 | { 12 | ans[ind++] = pq.poll() ; 13 | } 14 | 15 | return ans; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Delete without head pointer/Delete without head pointer.java: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | class Node 4 | { 5 | int data ; 6 | Node next; 7 | Node(int d) 8 | { 9 | data = d; 10 | next = null; 11 | } 12 | } 13 | */ 14 | 15 | //Function to delete a node without any reference to head pointer. 16 | class Solution 17 | { 18 | void deleteNode(Node del_node) 19 | { 20 | del_node.data = del_node.next.data ; 21 | del_node.next = del_node.next.next ; 22 | 23 | } 24 | } 25 | 26 | -------------------------------------------------------------------------------- /Queue Operations/Queue Operations.java: -------------------------------------------------------------------------------- 1 | class Geeks{ 2 | 3 | 4 | static void insert(Queue q, int k){ 5 | 6 | 7 | q.add(k); 8 | 9 | } 10 | 11 | 12 | static int findFrequency(Qu // Your code here 13 | int count=0; 14 | for(Integer e:q) 15 | { 16 | if(e==k) 17 | { 18 | count++; 19 | } 20 | } 21 | return count; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /Type it!.java/Type it!.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | int minOperation(String s) { 3 | int len = s.length() ; 4 | int low = 0 , high = len ; 5 | if(len==1) 6 | return 1 ; 7 | while(low<=high) 8 | { 9 | int mid = low+high/2 -1 ; 10 | if((s.substring(0,mid+1)).equals(s.substring(mid+1,2*(mid+1)))) 11 | return (mid+1+1+(len-2*(mid+1))) ; 12 | high--; 13 | } 14 | return len ; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /2348. Number of Zero-Filled Subarrays/2348. Number of Zero-Filled Subarrays.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public long zeroFilledSubarray(int[] nums) { 3 | long count = 0, zeroSubarraysEndingAtCurrentIndex = 0; 4 | for (int n : nums) { 5 | if (n == 0) { 6 | count += ++zeroSubarraysEndingAtCurrentIndex; 7 | }else { 8 | zeroSubarraysEndingAtCurrentIndex = 0; 9 | } 10 | } 11 | return count; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /2485. Find the Pivot Integer/2485. Find the Pivot Integer.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int pivotInteger(int n) { 3 | int sum = 0 ; 4 | 5 | for(int i = 1;i<=n;i++) 6 | sum += i ; 7 | 8 | int temp = 0 ; 9 | 10 | for(int i = 1;i<=n;i++) 11 | { 12 | 13 | 14 | temp += i-1 ; 15 | sum -= i; 16 | 17 | if(temp == sum )return i ; 18 | } 19 | 20 | return -1; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /35. Search Insert Position/Search Insert Position.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int searchInsert(int[] nums, int target) { 3 | int low = 0 , high = nums.length-1 ; 4 | int mid = 0; 5 | while(low<=high){ 6 | mid = low + (high-low)/2 ; 7 | if(nums[mid]== target) 8 | return mid ; 9 | else if(target>nums[mid]) 10 | low = ++mid ; 11 | else 12 | high = --mid ; 13 | } 14 | return low ; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Reach a given score/Reach a given score.java: -------------------------------------------------------------------------------- 1 | class Geeks { 2 | public long count(int n) { 3 | 4 | int []dp = new int[n+1] ; 5 | dp[0] = 1 ; 6 | 7 | for(int i = 3 ;i<=n;i++){ 8 | dp[i] += dp[i-3]; 9 | } 10 | for(int i = 5 ;i<=n;i++){ 11 | dp[i] += dp[i-5]; 12 | } 13 | for(int i = 10 ;i<=n;i++){ 14 | dp[i] += dp[i-10]; 15 | } 16 | 17 | return dp[n]; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Remove duplicate element from sorted Linked List/Remove duplicate element from sorted Linked List.java: -------------------------------------------------------------------------------- 1 | 2 | class GfG 3 | { 4 | 5 | Node removeDuplicates(Node head) 6 | { 7 | Node temp = head; 8 | 9 | while(temp.next!=null){ 10 | if(temp.data == temp.next.data){ 11 | temp.next = temp.next.next; 12 | 13 | } 14 | else{ 15 | temp = temp.next; 16 | 17 | } 18 | } 19 | return head; 20 | 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /67. Add Binary/README.md: -------------------------------------------------------------------------------- 1 | 67. Add Binary => 2 | -------------- 3 | 4 | 5 | Given two binary strings a and b, return their sum as a binary string. 6 | 7 | 8 | 9 | Example 1: 10 | 11 | Input: a = "11", b = "1" 12 | Output: "100" 13 | Example 2: 14 | 15 | Input: a = "1010", b = "1011" 16 | Output: "10101" 17 | 18 | 19 | Constraints: 20 | 21 | 1 <= a.length, b.length <= 104 22 | a and b consist only of '0' or '1' characters. 23 | Each string does not contain leading zeros except for the zero itself. 24 | -------------------------------------------------------------------------------- /Find the Highest number/Find the Highest number.java: -------------------------------------------------------------------------------- 1 | 2 | class Solution { 3 | public int findPeakElement(List a) { 4 | int left = 0; 5 | int right = a.size() - 1; 6 | while (left < right) { 7 | int mid = left + (right - left) / 2; 8 | if (a.get(mid) > a.get(mid + 1)) { 9 | right = mid; 10 | } else { 11 | left = mid + 1; 12 | } 13 | } 14 | return a.get(left); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Print Pattern/Print Pattern.java: -------------------------------------------------------------------------------- 1 | 2 | class Solution{ 3 | public List pattern(int N){ 4 | Listans = new ArrayList<>() ; 5 | int t = N ; 6 | 7 | 8 | while(t>0 ) 9 | { 10 | ans.add(t) ; 11 | t-=5 ; 12 | } 13 | 14 | while(t!=N) 15 | { 16 | ans.add(t) ; 17 | t+=5 ; 18 | } 19 | 20 | ans.add(N); 21 | return ans; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Rohan's Love for Matrix/Rohan's Love for Matrix.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | static int firstElement(int n) { 3 | if(n<=2) 4 | return 1; 5 | 6 | if(n==3) 7 | return 2; 8 | 9 | int a=1,b=2,c=0; 10 | int mod=1000000007; 11 | while(n>=4){ 12 | 13 | c=(a+b)%mod; 14 | a=b; 15 | b=c%mod; 16 | n--; 17 | } 18 | 19 | return c%mod; 20 | 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /2540. Minimum Common Value/2540. Minimum Common Value.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int getCommon(int[] nums1, int[] nums2) { 3 | Arrays.sort(nums1); 4 | Arrays.sort(nums2); 5 | 6 | int i = 0 , j = 0 ; 7 | 8 | while(i= 0 ;i--) 8 | { 9 | sum += (happ[i]-cnt > 0)?(long)(happ[i]-cnt) : 0 ; 10 | cnt++; 11 | if(cnt == k)break; 12 | } 13 | return sum; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /118. Pascal's Triangle/Readme.md: -------------------------------------------------------------------------------- 1 | 118. Pascal's Triangle => 2 | ---------------------- 3 | 4 | 5 | Given an integer numRows, return the first numRows of Pascal's triangle. 6 | 7 | In Pascal's triangle, each number is the sum of the two numbers directly above it as shown: 8 | 9 | 10 | 11 | 12 | Example 1: 13 | 14 | Input: numRows = 5 15 | Output: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]] 16 | Example 2: 17 | 18 | Input: numRows = 1 19 | Output: [[1]] 20 | 21 | 22 | Constraints: 23 | 24 | 1 <= numRows <= 30 25 | -------------------------------------------------------------------------------- /1436. Destination City/1436. Destination City.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String destCity(List> paths) { 3 | Setst = new HashSet<>() ; 4 | 5 | for(int i = 0 ;ipq = new PriorityQueue<>(Collections.reverseOrder()) ; 4 | 5 | for(int i : nums) 6 | pq.add(i); 7 | 8 | int ans = 0 ; 9 | 10 | while(k-- >0) 11 | { 12 | 13 | ans = pq.poll() ; 14 | 15 | } 16 | return ans; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /409. Longest Palindrome/409. Longest Palindrome.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int longestPalindrome(String s) { 3 | Setst =new HashSet<>() ; 4 | int ans = 0; 5 | for(char ch : s.toCharArray()) 6 | { 7 | if(st.contains(ch)) 8 | { 9 | ans += 2 ; 10 | st.remove(ch); 11 | } 12 | else 13 | st.add(ch); 14 | } 15 | 16 | return st.size()>0 ? ans+1:ans; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Facing the sun/Facing the sun.java: -------------------------------------------------------------------------------- 1 | 2 | class Solution { 3 | // Returns count buildings that can see sunlight 4 | public int countBuildings(int[] height) { 5 | if(height.length ==1)return 1 ; 6 | int cnt = 1; 7 | int prevGrt = height[0]; 8 | 9 | for(int i= 1;iprevGrt) 11 | { 12 | cnt++; 13 | prevGrt = height[i]; 14 | } 15 | } 16 | return cnt; 17 | 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /1470. Shuffle the Array/Shuffle the Array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] shuffle(int[] nums, int n) { 3 | if(nums.length==1) 4 | return nums ; 5 | int[] ans = new int[2*n] ; 6 | Listtemp =new ArrayList<>() ; 7 | int c = 0 ; 8 | for(int i = 0 ;i 2 | ------------------ 3 | 4 | Given an integer n, break it into the sum of k positive integers, where k >= 2, and maximize the product of those integers. 5 | 6 | Return the maximum product you can get. 7 | 8 | 9 | 10 | Example 1: 11 | 12 | Input: n = 2 13 | Output: 1 14 | Explanation: 2 = 1 + 1, 1 × 1 = 1. 15 | Example 2: 16 | 17 | Input: n = 10 18 | Output: 36 19 | Explanation: 10 = 3 + 3 + 4, 3 × 3 × 4 = 36. 20 | 21 | 22 | Constraints: 23 | 24 | 2 <= n <= 58 25 | -------------------------------------------------------------------------------- /5. Longest Palindromic Substring/Readme.md: -------------------------------------------------------------------------------- 1 | 5. Longest Palindromic Substring => 2 | -------------------------------- 3 | 4 | 5 | Given a string s, return the longest 6 | palindromic 7 | 8 | substring 9 | in s. 10 | 11 | 12 | 13 | Example 1: 14 | 15 | Input: s = "babad" 16 | Output: "bab" 17 | Explanation: "aba" is also a valid answer. 18 | Example 2: 19 | 20 | Input: s = "cbbd" 21 | Output: "bb" 22 | 23 | 24 | Constraints: 25 | 26 | 1 <= s.length <= 1000 27 | s consist of only digits and English letters. 28 | -------------------------------------------------------------------------------- /513. Find Bottom Left Tree Value/Readme.md: -------------------------------------------------------------------------------- 1 | 513. Find Bottom Left Tree Value => 2 | -------------------------------- 3 | 4 | 5 | Given the root of a binary tree, return the leftmost value in the last row of the tree. 6 | 7 | 8 | 9 | Example 1: 10 | 11 | 12 | Input: root = [2,1,3] 13 | Output: 1 14 | Example 2: 15 | 16 | 17 | Input: root = [1,2,3,4,null,5,6,null,null,7] 18 | Output: 7 19 | 20 | 21 | Constraints: 22 | 23 | The number of nodes in the tree is in the range [1, 104]. 24 | -231 <= Node.val <= 231 - 1 25 | -------------------------------------------------------------------------------- /930. Binary Subarrays With Sum/930. Binary Subarrays With Sum.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int numSubarraysWithSum(int[] nums, int goal) { 3 | Mapmap = new HashMap<>() ; 4 | int ans = 0 ; 5 | int ps = 0 ; 6 | map.put(0,1); 7 | for(int i : nums) 8 | { 9 | ps += i ; 10 | 11 | ans += map.getOrDefault(ps-goal,0); 12 | map.put(ps,map.getOrDefault(ps,0)+1) ; 13 | } 14 | return ans; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /997. Find the Town Judge/Find the Town Judge leetcode java.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findJudge(int n, int[][] trust) { 3 | int []cnt = new int[n+1] ; 4 | for(int []i: trust) 5 | { 6 | cnt[i[0]]-- ; 7 | cnt[i[1]]++ ; 8 | } 9 | for(int i = 1;i<=n;i++) 10 | { 11 | if(cnt[i]==n-1) 12 | return i ; 13 | } 14 | return -1; 15 | } 16 | } 17 | 18 | Time complexity > O(n) 19 | Space Complexity -> O(n) 20 | -------------------------------------------------------------------------------- /Make array elements unique/Make array elements unique.java: -------------------------------------------------------------------------------- 1 | 2 | class Solution { 3 | public long minIncrements(int[] arr, int N) { 4 | // Code here 5 | long res = 0 ; 6 | Arrays.sort(arr); 7 | for(int i = 1 ;i postorderTraversal(TreeNode root) { 3 | Listans = new ArrayList<>() ; 4 | 5 | postOrder(root,ans); 6 | return ans; 7 | } 8 | 9 | void postOrder(TreeNode root,Listans){ 10 | if(root != null){ 11 | postOrder(root.left,ans); 12 | postOrder(root.right,ans); 13 | ans.add(root.val) ; 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /198. House Robber/198. House Robber.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int rob(int[] nums) { 3 | int []dp = new int[nums.length] ; 4 | 5 | if(nums.length == 1)return nums[0] ; 6 | if(nums.length == 2)return Math.max(nums[0],nums[1]) ; 7 | 8 | dp[0] = nums[0] ; 9 | dp[1] = Math.max(nums[0],nums[1]) ; 10 | 11 | for(int i = 2;i 2 | ---------------------- 3 | 4 | 5 | Given a string s, rearrange the characters of s so that any two adjacent characters are not the same. 6 | 7 | Return any possible rearrangement of s or return "" if not possible. 8 | 9 | 10 | 11 | Example 1: 12 | 13 | Input: s = "aab" 14 | Output: "aba" 15 | Example 2: 16 | 17 | Input: s = "aaab" 18 | Output: "" 19 | 20 | 21 | Constraints: 22 | 23 | 1 <= s.length <= 500 24 | s consists of lowercase English letters. 25 | -------------------------------------------------------------------------------- /Count possible ways to construct buildings/Count possible ways to construct buildings.java: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public int TotalWays(int N) 4 | { 5 | long mod = 1000000007 ; 6 | 7 | long a = 1, b = 1 , c = 0 ; 8 | 9 | for(int i = 1;i<=N;i++) 10 | { 11 | c = (a%mod+b%mod)%mod ; 12 | a = b%mod ; 13 | b = c %mod ; 14 | 15 | } 16 | 17 | return (int) ((c*c)%mod) ; 18 | 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Minimize the sum/Minimize the sum.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | long minimizeSum(int N, int arr[]) { 3 | PriorityQueueq=new PriorityQueue<>(); 4 | for(int i:arr) 5 | { 6 | q.add(i); 7 | } 8 | if(N==1) 9 | return 0; 10 | else if(N==2) 11 | return arr[0]+arr[1]; 12 | int ans=0; 13 | while (q.size()>1) 14 | {int a =q.poll(); 15 | int b =q.poll(); 16 | ans += (a+b); 17 | q.add(a+b); 18 | } 19 | return ans; 20 | 21 | } 22 | } 23 | 24 | Expected Time Complexity: O(N * log(N)) 25 | Expected Auxiliary Space: O(N) 26 | -------------------------------------------------------------------------------- /Stickler Thief/Stickler Thief.java: -------------------------------------------------------------------------------- 1 | 2 | class Solution 3 | { 4 | 5 | public int FindMaxSum(int arr[], int n) 6 | { 7 | int dp[] = new int[n] ; 8 | 9 | if(n == 1)return arr[0] ; 10 | if(n==2)return (int)Math.max(arr[0],arr[1]) ; 11 | 12 | dp[0] = arr[0] ; 13 | dp[1] = Math.max(arr[0],arr[1]) ; 14 | 15 | for(int i = 2;i 0) 8 | dec = false ; 9 | 10 | if(nums[i+1]- nums[i] < 0) 11 | in = false ; 12 | 13 | if(in == false && dec == false)return false ; 14 | } 15 | 16 | return in || dec ; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Cutting Rectangles/Cutting Rectangles.java: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | static List minimumSquares(long L, long B) 3 | { 4 | List ans = new ArrayList<>() ; 5 | long k = gcd(L,B) ; 6 | long no_of_sq = (L*B)/ (k*k) ; 7 | 8 | ans.add(no_of_sq ) ; 9 | ans.add(k) ; 10 | 11 | return ans ; 12 | } 13 | static long gcd(long a, long b) 14 | { 15 | if(b==0) 16 | return a ; 17 | 18 | return gcd(b,a%b) ; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Sum of all substrings of a number/Sum of all substrings of a number.java: -------------------------------------------------------------------------------- 1 | 2 | class Solution 3 | { 4 | 5 | public static long sumSubstrings(String s) 6 | { 7 | long prev = 0 , cur = 0 , ans = 0 ; 8 | long mod = (long)(1e9+7); 9 | 10 | for(int i = 0 ;iarr[i])return false ; 14 | if(rightarr[i])return false ; 15 | 16 | } 17 | 18 | return true ; 19 | 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Non Repeating Character/Non Repeating Character.java: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | 4 | static char nonrepeatingCharacter(String S) 5 | { 6 | Mapmap = new HashMap<>() ; 7 | for(char ch : S.toCharArray()) 8 | { 9 | map.put(ch,map.getOrDefault(ch,0)+1) ; 10 | } 11 | 12 | for(int i = 0;i 2 | ----------------------- 3 | 4 | 5 | Given the root of a binary tree, invert the tree, and return its root. 6 | 7 | 8 | 9 | Example 1: 10 | 11 | 12 | Input: root = [4,2,7,1,3,6,9] 13 | Output: [4,7,2,9,6,3,1] 14 | Example 2: 15 | 16 | 17 | Input: root = [2,1,3] 18 | Output: [2,3,1] 19 | Example 3: 20 | 21 | Input: root = [] 22 | Output: [] 23 | 24 | 25 | Constraints: 26 | 27 | The number of nodes in the tree is in the range [0, 100]. 28 | -100 <= Node.val <= 100 29 | -------------------------------------------------------------------------------- /Bit Magic/Bit Magic.java: -------------------------------------------------------------------------------- 1 | 2 | class Solution { 3 | public static int bitMagic(int n, int[] arr) { 4 | 5 | int i = 0; 6 | int j = n-1; 7 | int count = 0; 8 | while(i a, int n, int m) 5 | { 6 | Collections.sort(a) ; 7 | long ans = Long.MAX_VALUE; 8 | for(int i = 0;i= '0' && ch <='9') 14 | ans = ans*10+(ch-'0'); 15 | else 16 | return -1 ; 17 | } 18 | 19 | if(s.charAt(0)=='-') 20 | ans *= -1 ; 21 | 22 | return (int)ans; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Make the array beautiful/Make the array beautiful.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public static ArrayList makeBeautiful(int[] arr) { 3 | 4 | ArrayListans = new ArrayList<>() ; 5 | 6 | for(int i = 0 ;i=0) || ( ans.get(ans.size()-1)>=0 && arr[i]<0))) 8 | ans.remove(ans.size()-1) ; 9 | else 10 | ans.add(arr[i]) ; 11 | } 12 | return ans ; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Print first n Fibonacci Numbers/Print first n Fibonacci Numbers.java: -------------------------------------------------------------------------------- 1 | 2 | class Solution 3 | { 4 | 5 | public static long[] printFibb(int n) 6 | { 7 | long [] ans = new long[n] ; 8 | if(n ==1) 9 | { 10 | ans[0] = 1 ; return ans ; 11 | } 12 | 13 | ans[0] = 1 ; 14 | ans[1] = 1 ; 15 | 16 | for(int i = 2;i 2 | ----------------- 3 | 4 | 5 | 6 | You are given a string s. You need to reverse the string. 7 | 8 | Example 1: 9 | 10 | Input: 11 | s = Geeks 12 | Output: skeeG 13 | Example 2: 14 | 15 | Input: 16 | s = for 17 | Output: rof 18 | Your Task: 19 | 20 | You only need to complete the function reverseWord() that takes s as parameter and returns the reversed string. 21 | 22 | Expected Time Complexity: O(|S|). 23 | Expected Auxiliary Space: O(1). 24 | 25 | Constraints: 26 | 1 <= |s| <= 10000 27 | -------------------------------------------------------------------------------- /Unique Number of Occurrences/Unique Number of Occurrences.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public static boolean isFrequencyUnique(int n, int[] arr) { 3 | Mapmap = new HashMap<>() ; 4 | for(int i : arr) 5 | { 6 | map.put(i,map.getOrDefault(i,0)+1); 7 | } 8 | Setst = new HashSet<>() ; 9 | for(Integer i : map.values()) 10 | { 11 | st.add(i) ; 12 | } 13 | return (st.size()== map.size())?true:false; 14 | } 15 | } 16 | 17 | -------------------------------------------------------------------------------- /137. Single Number II/137. Single Number II.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int singleNumber(int[] nums) { 3 | if(nums.length == 1) 4 | return nums[0]; 5 | Mapmap = new HashMap<>() ; 6 | for(int i :nums) 7 | map.put(i,map.getOrDefault(i,0)+1); 8 | 9 | int ans = 0 ; 10 | for(Map.Entryentry : map.entrySet()) 11 | { 12 | if(entry.getValue()==1) 13 | {ans = entry.getKey();break;} 14 | } 15 | return ans; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /714. Best Time to Buy and Sell Stock with Transaction Fee/714. Best Time to Buy and Sell Stock with Transaction Fee.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxProfit(int[] prices, int fee) { 3 | int effectiveBuyPrice = prices[0]; 4 | int profit = 0 ; 5 | for(int i = 0 ;ist = new Stack<>() ; 4 | int j =0 ; 5 | for(int p : pushed){ 6 | st.push(p) ; 7 | while(!st.isEmpty() && jhigh)return null; 8 | 9 | if(low == high)return new Node(ar[low]); 10 | 11 | int mid = (low+high)/2 ; 12 | Node root = new Node(ar[mid]); 13 | root.left = help(ar,low,mid-1); 14 | root.right = help(ar,mid+1,high); 15 | 16 | return root; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Count More than n by k Occurences/Count More than n by k Occurences.java: -------------------------------------------------------------------------------- 1 | 2 | class Solution 3 | { 4 | public int countOccurence(int[] arr, int n, int k) 5 | { 6 | Mapmap = new HashMap<>() ; 7 | 8 | for(int i : arr) 9 | map.put(i,map.getOrDefault(i,0)+1) ; 10 | 11 | int ans = 0 ; 12 | for(Map.Entryentry : map.entrySet()){ 13 | if(entry.getValue()>(n/k)) 14 | ans++; 15 | } 16 | return ans ; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Middle of a Linked List/Middle of a Linked List.java: -------------------------------------------------------------------------------- 1 | 2 | /* Node of a linked list 3 | class Node { 4 | int data; 5 | Node next; 6 | Node(int d) { data = d; next = null; } 7 | } 8 | */ 9 | 10 | class Solution { 11 | int getMiddle(Node head) { 12 | Node slow = head; 13 | Node fast = head; 14 | 15 | while(fast != null && fast.next != null) 16 | { 17 | slow = slow.next ; 18 | fast = fast.next.next ; 19 | } 20 | 21 | return slow.data; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /1207. Unique Number of Occurrences/1207. Unique Number of Occurrences.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean uniqueOccurrences(int[] arr) { 3 | Mapmap = new HashMap<>() ; 4 | 5 | for(int i : arr) 6 | map.put(i,map.getOrDefault(i,0)+1) ; 7 | 8 | Setst = new HashSet<>() ; 9 | 10 | for(Integer i : map.values()) 11 | if(st.size()>0 && st.contains(i)) 12 | return false; 13 | else st.add(i) ; 14 | 15 | return st.size() == map.size(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /1662. Check If Two String Arrays are Equivalent/1662. Check If Two String Arrays are Equivalent.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean arrayStringsAreEqual(String[] word1, String[] word2) { 3 | String s1 = makeString(word1) ; 4 | String s2 = makeString(word2) ; 5 | return s1.equals(s2) ; 6 | } 7 | public String makeString(String []word) 8 | { 9 | StringBuilder sb = new StringBuilder() ; 10 | for(String s:word) 11 | sb.append(s) ; 12 | return sb.toString() ; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /2000. Reverse Prefix of Word/2000. Reverse Prefix of Word.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String reversePrefix(String word, char ch) { 3 | int ind = word.indexOf(ch) ; 4 | if(ind == -1) 5 | { 6 | return word; 7 | } 8 | 9 | StringBuilder sb = new StringBuilder() ; 10 | 11 | for(int i = 0 ;i<=ind;i++) 12 | sb.append(word.charAt(i)); 13 | 14 | sb.reverse() ; 15 | sb.append(word.substring(ind+1)); 16 | return sb.toString() ; 17 | } 18 | 19 | 20 | } 21 | -------------------------------------------------------------------------------- /2864. Maximum Odd Binary Number/2864. Maximum Odd Binary Number.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String maximumOddBinaryNumber(String s) { 3 | int ones = 0 ; 4 | 5 | for(char ch : s.toCharArray()) if(ch == '1')ones++ ; 6 | 7 | int i = 0 ; 8 | StringBuilder sb = new StringBuilder() ; 9 | 10 | for( ;i addToArrayForm(int[] num, int k) { 3 | Listans = new ArrayList<>() ; 4 | int carray = k ; 5 | int i = num.length-1 ; 6 | while(i>=0 || carray>0) 7 | { 8 | if(i>=0 ) 9 | carray += num[i] ; 10 | ans.add(carray%10) ; 11 | carray/=10 ; 12 | i-- ; 13 | } 14 | Collections.reverse(ans) ; 15 | return ans ; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /1347. Minimum Number of Steps to Make Two Strings Anagram/1347. Minimum Number of Steps to Make Two Strings Anagram.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minSteps(String s, String t) { 3 | int []countS = new int[26] ; 4 | int ans = 0 ; 5 | 6 | for(char ch : s.toCharArray()) 7 | countS[ch-'a']++; 8 | 9 | for(char ch : t.toCharArray()) 10 | if(countS[ch-'a']>0) 11 | countS[ch-'a']--; 12 | 13 | for(int i : countS) 14 | if(i>0) ans+=i ; 15 | 16 | return ans; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /2971. Find Polygon With the Largest Perimeter/2971. Find Polygon With the Largest Perimeter.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public long largestPerimeter(int[] nums) { 3 | Arrays.sort(nums) ; 4 | 5 | long sum = nums[0]+nums[1] ; 6 | long ans = -1 ; 7 | 8 | for(int i = 2;inums[i] ){ 11 | ans = Math.max(ans,sum + nums[i]); 12 | } 13 | 14 | sum += nums[i] ; 15 | 16 | } 17 | 18 | return ans; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /628. Maximum Product of Three Numbers/README.md: -------------------------------------------------------------------------------- 1 | 628. Maximum Product of Three Numbers => 2 | ------------------------------------- 3 | 4 | 5 | Given an integer array nums, find three numbers whose product is maximum and return the maximum product. 6 | 7 | 8 | 9 | Example 1: 10 | 11 | Input: nums = [1,2,3] 12 | Output: 6 13 | Example 2: 14 | 15 | Input: nums = [1,2,3,4] 16 | Output: 24 17 | Example 3: 18 | 19 | Input: nums = [-1,-2,-3] 20 | Output: -6 21 | 22 | 23 | Constraints: 24 | 25 | 3 <= nums.length <= 104 26 | -1000 <= nums[i] <= 1000 27 | -------------------------------------------------------------------------------- /646. Maximum Length of Pair Chain/646. Maximum Length of Pair Chain.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findLongestChain(int[][] pairs) { 3 | int current = Integer.MIN_VALUE ; 4 | int ans = 0 ; 5 | Arrays.sort(pairs, (a,b)->{ 6 | return a[1]-b[1] ; 7 | }); 8 | 9 | for(int []pair: pairs) 10 | { 11 | if(current0) 14 | { 15 | tail.next = new Node(head.data) ; 16 | head = head.next ; 17 | tail= tail.next ; 18 | k--; 19 | } 20 | 21 | return head ; 22 | 23 | 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Segregate 0s and 1s/Segregate 0s and 1s.java: -------------------------------------------------------------------------------- 1 | 2 | class Solution { 3 | void segregate0and1(int[] arr) { 4 | int st = 0 ; 5 | 6 | for(int i = 0 ;ij?0:j-i+1 ; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /1897. Redistribute Characters to Make All Strings Equal/1897. Redistribute Characters to Make All Strings Equal.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean makeEqual(String[] words) { 3 | int []count = new int[26] ; 4 | 5 | for(String word : words){ 6 | for(int i = 0 ;i 2 | ---------------------------------- 3 | 4 | 5 | Given an integer n, return all the structurally unique BST's (binary search trees), which has exactly n nodes of unique values from 1 to n. Return the answer in any order. 6 | 7 | 8 | 9 | Example 1: 10 | 11 | 12 | Input: n = 3 13 | Output: [[1,null,2,null,3],[1,null,3,2],[2,1,3],[3,1,null,null,2],[3,2,null,1]] 14 | Example 2: 15 | 16 | Input: n = 1 17 | Output: [[1]] 18 | 19 | 20 | Constraints: 21 | 22 | 1 <= n <= 8 23 | -------------------------------------------------------------------------------- /Consecutive 1's not allowed/Consecutive 1's not allowed.java: -------------------------------------------------------------------------------- 1 | 2 | class Solution { 3 | long countStrings(int n) { 4 | long mod = 1000000007 ; 5 | long zeroend = 1 ; 6 | long oneend = 1 ; 7 | long ans = zeroend+oneend ; 8 | 9 | if(n == 1)return ans ; 10 | int i = 2 ; 11 | while(i<= n){ 12 | oneend = zeroend ; 13 | zeroend = ans ; 14 | ans = (oneend+zeroend) %mod ; 15 | i++; 16 | } 17 | 18 | return ans ; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Subarray with 0 sum/Subarray with 0 sum.java: -------------------------------------------------------------------------------- 1 | 2 | class Solution{ 3 | static boolean findsum(int arr[],int n) 4 | { 5 | Setst = new HashSet<>() ; 6 | 7 | int sum = 0 ; 8 | for(int i = 0 ;ipq = new PriorityQueue<>() ; 4 | 5 | for(int i = 0 ;i0) 10 | pq.add(dif) ; 11 | 12 | if(pq.size()>l) 13 | b -= pq.poll() ; 14 | 15 | if(b<0)return i ; 16 | } 17 | 18 | return h.length-1; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /201. Bitwise AND of Numbers Range/Readme.md: -------------------------------------------------------------------------------- 1 | 201. Bitwise AND of Numbers Range => 2 | --------------------------------- 3 | 4 | 5 | Given two integers left and right that represent the range [left, right], return the bitwise AND of all numbers in this range, inclusive. 6 | 7 | 8 | 9 | Example 1: 10 | 11 | Input: left = 5, right = 7 12 | Output: 4 13 | Example 2: 14 | 15 | Input: left = 0, right = 0 16 | Output: 0 17 | Example 3: 18 | 19 | Input: left = 1, right = 2147483647 20 | Output: 0 21 | 22 | 23 | Constraints: 24 | 25 | 0 <= left <= right <= 231 - 1 26 | -------------------------------------------------------------------------------- /231. Power of Two/Readme.md: -------------------------------------------------------------------------------- 1 | 231. Power of Two => 2 | ----------------- 3 | 4 | 5 | Given an integer n, return true if it is a power of two. Otherwise, return false. 6 | 7 | An integer n is a power of two, if there exists an integer x such that n == 2x. 8 | 9 | 10 | 11 | Example 1: 12 | 13 | Input: n = 1 14 | Output: true 15 | Explanation: 20 = 1 16 | Example 2: 17 | 18 | Input: n = 16 19 | Output: true 20 | Explanation: 24 = 16 21 | Example 3: 22 | 23 | Input: n = 3 24 | Output: false 25 | 26 | 27 | Constraints: 28 | 29 | -231 <= n <= 231 - 1 30 | 31 | -------------------------------------------------------------------------------- /515. Find Largest Value in Each Tree Row/Readme.md: -------------------------------------------------------------------------------- 1 | 515. Find Largest Value in Each Tree Row => 2 | ----------------------------------------- 3 | 4 | 5 | Given the root of a binary tree, return an array of the largest value in each row of the tree (0-indexed). 6 | 7 | 8 | 9 | Example 1: 10 | 11 | 12 | Input: root = [1,3,2,5,3,null,9] 13 | Output: [1,3,9] 14 | Example 2: 15 | 16 | Input: root = [1,2,3] 17 | Output: [1,3] 18 | 19 | 20 | Constraints: 21 | 22 | The number of nodes in the tree will be in the range [0, 104]. 23 | -231 <= Node.val <= 231 - 1 24 | -------------------------------------------------------------------------------- /Determine if Two Trees are Identical/Determine if Two Trees are Identical.java: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | 4 | class Node{ 5 | int data; 6 | Node left,right; 7 | Node(int d){ 8 | data=d; 9 | left=right=null; 10 | } 11 | }*/ 12 | 13 | 14 | class Solution 15 | { 16 | boolean isIdentical(Node root1, Node root2) 17 | { 18 | if(root1 == null || root2 == null) 19 | return root1 == root2 ; 20 | 21 | return (root1.data == root2.data)&& isIdentical(root1.left,root2.left) && isIdentical(root1.right,root2.right) ; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /Distinct Coloring/Distinct Coloring.java: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public static long distinctColoring(int N, int[]r, int[]g, int[]b){ 3 | long [][]dp = new long [N][3] ; 4 | dp[0][0] = r[0] ; dp[0][1] = g[0] ; dp[0][2] = b[0] ; 5 | for(int i =1;izeroCnt) 15 | { 16 | zeroCnt = cnt ; 17 | col = i; 18 | } 19 | } 20 | 21 | return col; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Recursive sequence/Recursive sequence/Recursive sequence.java: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | static long sequence(int n){ 3 | long mod = (long) (1e9+7); 4 | long ans = 0 ; 5 | int cnt = 1; 6 | 7 | for(int i = 1;i<=n;i++){ 8 | 9 | long temp = 1 ; 10 | for(int j =1;j<=i;j++) 11 | { 12 | temp = (temp%mod * cnt%mod)%mod ; 13 | cnt++; 14 | } 15 | ans= (ans % mod+ temp%mod)%mod; 16 | } 17 | 18 | return ans; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Seating Arrangement/Seating Arrangement.java: -------------------------------------------------------------------------------- 1 | 2 | class Solution { 3 | public static boolean is_possible_to_get_seats(int n, int m, int[] seats) { 4 | int res = 0 ; 5 | int i = 0 ; 6 | while(i=n; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /1464. Maximum Product of Two Elements in an Array/1464. Maximum Product of Two Elements in an Array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxProduct(int[] nums) { 3 | int mx1 = Integer.MIN_VALUE ; 4 | int mx2 = Integer.MIN_VALUE ; 5 | 6 | for(int i : nums){ 7 | if(i>=mx1) 8 | { 9 | mx2 = mx1 ; 10 | mx1 = i ; 11 | } 12 | else if(i>mx2) 13 | { 14 | mx2 = i ; 15 | } 16 | 17 | } 18 | 19 | return (mx1-1)*(mx2-1); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /28. Find the Index of the First Occurrence in a String/Find the Index of the First Occurrence in a String.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int strStr(String haystack, String needle) { 3 | if(needle.equals(haystack)) 4 | return 0 ; 5 | 6 | if(needle.length()>haystack.length()) 7 | return -1 ; 8 | for(int i = 0 , j = needle.length() ;j<=haystack.length() ;i++,j++) 9 | { 10 | if(needle.equals(haystack.substring(i,j))) 11 | return i ; 12 | } 13 | return -1 ; 14 | } 15 | } 16 | 17 | -------------------------------------------------------------------------------- /Array Operations/Array Operations.java: -------------------------------------------------------------------------------- 1 | 2 | class Solution { 3 | public static int arrayOperations(int n, int[] arr) { 4 | int res = 0; 5 | int cnt = 0; 6 | int zero = 0; 7 | for (int i = 0; i < n; i++) { 8 | if (arr[i] > 0)cnt++; 9 | if (arr[i] == 0) { 10 | zero= 1; 11 | if (cnt > 0)res++; 12 | cnt = 0; 13 | } 14 | } 15 | if (cnt > 0)res++; 16 | if (zero == 0)return -1; 17 | return res; 18 | } 19 | } 20 | 21 | -------------------------------------------------------------------------------- /Walls Coloring/Walls Coloring.java: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | int minCost(int [][] colors, int N){ 3 | int [][]dp = new int[N][3] ; 4 | dp[0][0] = colors[0][0] ; dp[0][1] = colors[0][1] ; dp[0][2] = colors[0][2] ; 5 | for(int i =1;i0) 7 | { 8 | cnt++ ; 9 | n= n&(n-1) ; 10 | } 11 | 12 | return cnt == 1; 13 | } 14 | } 15 | 16 | 17 | 18 | 19 | // Method - 2 20 | class Solution { 21 | public boolean isPowerOfTwo(int n) { 22 | if(n ==0)return false; 23 | if(n ==Integer.MIN_VALUE)return false; 24 | return (n&(-n))==n; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /234. Palindrome Linked List/Readme.md: -------------------------------------------------------------------------------- 1 | 234. Palindrome Linked List => 2 | --------------------------- 3 | 4 | 5 | Given the head of a singly linked list, return true if it is a 6 | palindrome 7 | or false otherwise. 8 | 9 | 10 | 11 | Example 1: 12 | 13 | 14 | Input: head = [1,2,2,1] 15 | Output: true 16 | Example 2: 17 | 18 | 19 | Input: head = [1,2] 20 | Output: false 21 | 22 | 23 | Constraints: 24 | 25 | The number of nodes in the list is in the range [1, 105]. 26 | 0 <= Node.val <= 9 27 | 28 | 29 | Follow up: Could you do it in O(n) time and O(1) space? 30 | -------------------------------------------------------------------------------- /Count the elements/Count the elements.java: -------------------------------------------------------------------------------- 1 | 2 | class Solution 3 | { 4 | public static int[] countElements(int a[], int b[], int n, int query[], int q) 5 | { 6 | int []ans = new int[query.length] ; 7 | int ind = 0 ; 8 | 9 | for(int que : query){ 10 | int ele = a[que] ; 11 | int cnt = 0 ; 12 | for(int i = 0 ;i findMissing(int a[], int b[], int n, int m) 6 | { 7 | ArrayList ans = new ArrayList<>(); 8 | Setst = new HashSet<>() ; 9 | 10 | for(int i : b) 11 | st.add(i); 12 | 13 | for(int i :a ) 14 | { 15 | if(!st.contains(i)) 16 | { 17 | ans.add(i); 18 | } 19 | 20 | } 21 | return ans; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Max Sum Subarray of size K/Max Sum Subarray of size K.java: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | static long maximumSumSubarray(int k, ArrayList arr,int N){ 3 | long sum = 0; 4 | long value = Integer.MIN_VALUE; 5 | for(int i=0;i buildArray(int[] target, int n) { 3 | Listans = new ArrayList<>() ; 4 | 5 | int cnt = 1 ; 6 | 7 | for(int i : target){ 8 | while(cnt < i) 9 | { 10 | ans.add("Push"); 11 | ans.add("Pop"); 12 | cnt++; 13 | } 14 | cnt++; 15 | ans.add("Push"); 16 | } 17 | 18 | return ans; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /1481. Least Number of Unique Integers after K Removals/1481. Least Number of Unique Integers after K Removals.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findLeastNumOfUniqueInts(int[] arr, int k) { 3 | 4 | Mapmap = new HashMap<>() ; 5 | 6 | for(int i : arr) 7 | map.put(i,map.getOrDefault(i,0)+1) ; 8 | 9 | PriorityQueuepq = new PriorityQueue<>(map.values()) ; 10 | 11 | while(k>0) 12 | { 13 | k-= pq.poll() ; 14 | } 15 | 16 | 17 | return k<0?pq.size()+1:pq.size(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /2870. Minimum Number of Operations to Make Array Empty/2870. Minimum Number of Operations to Make Array Empty.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minOperations(int[] nums) { 3 | Mapmap = new HashMap<>() ; 4 | 5 | for(int i : nums) 6 | map.put(i,map.getOrDefault(i,0)+1) ; 7 | 8 | int ans = 0 ; 9 | for(Integer i : map.values()) 10 | { 11 | 12 | if(i == 1)return -1 ; 13 | 14 | ans += i/3 ; 15 | if( i%3 != 0)ans++; 16 | } 17 | 18 | return ans; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Row with minimum number of 1's/Row with minimum number of 1's.java: -------------------------------------------------------------------------------- 1 | 2 | class Solution { 3 | int minRow(int n, int m, int a[][]) { 4 | int ans = 0 ; 5 | int one = 1001 ; 6 | for(int i = 0 ;i=1) 10 | { 11 | cnt++ ; 12 | freq[i]--; 13 | } 14 | else break; 15 | } 16 | 17 | return cnt == students.length ? 0 : students.length-cnt; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /258. Add Digits/README.md: -------------------------------------------------------------------------------- 1 | 258. Add Digits => 2 | --------------- 3 | 4 | 5 | Given an integer num, repeatedly add all its digits until the result has only one digit, and return it. 6 | 7 | 8 | 9 | Example 1: 10 | 11 | Input: num = 38 12 | Output: 2 13 | Explanation: The process is 14 | 38 --> 3 + 8 --> 11 15 | 11 --> 1 + 1 --> 2 16 | Since 2 has only one digit, return it. 17 | Example 2: 18 | 19 | Input: num = 0 20 | Output: 0 21 | 22 | 23 | Constraints: 24 | 25 | 0 <= num <= 231 - 1 26 | 27 | 28 | Follow up: Could you do it without any loop/recursion in O(1) runtime? 29 | -------------------------------------------------------------------------------- /316. Remove Duplicate Letters/Readme.md: -------------------------------------------------------------------------------- 1 | 316. Remove Duplicate Letters => 2 | ----------------------------- 3 | 4 | 5 | Given a string s, remove duplicate letters so that every letter appears once and only once. You must make sure your result is 6 | the smallest in lexicographical order 7 | among all possible results. 8 | 9 | 10 | 11 | Example 1: 12 | 13 | Input: s = "bcabc" 14 | Output: "abc" 15 | Example 2: 16 | 17 | Input: s = "cbacdcbc" 18 | Output: "acdb" 19 | 20 | 21 | Constraints: 22 | 23 | 1 <= s.length <= 104 24 | s consists of lowercase English letters. 25 | 26 | -------------------------------------------------------------------------------- /342. Power of Four/Readme.md: -------------------------------------------------------------------------------- 1 | 342. Power of Four => 2 | ------------------ 3 | 4 | 5 | Given an integer n, return true if it is a power of four. Otherwise, return false. 6 | 7 | An integer n is a power of four, if there exists an integer x such that n == 4x. 8 | 9 | 10 | 11 | Example 1: 12 | 13 | Input: n = 16 14 | Output: true 15 | Example 2: 16 | 17 | Input: n = 5 18 | Output: false 19 | Example 3: 20 | 21 | Input: n = 1 22 | Output: true 23 | 24 | 25 | Constraints: 26 | 27 | -231 <= n <= 231 - 1 28 | 29 | 30 | Follow up: Could you solve it without loops/recursion? 31 | -------------------------------------------------------------------------------- /387. First Unique Character in a String/Readme.md: -------------------------------------------------------------------------------- 1 | 387. First Unique Character in a String => 2 | --------------------------------------- 3 | 4 | 5 | Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1. 6 | 7 | 8 | 9 | Example 1: 10 | 11 | Input: s = "leetcode" 12 | Output: 0 13 | Example 2: 14 | 15 | Input: s = "loveleetcode" 16 | Output: 2 17 | Example 3: 18 | 19 | Input: s = "aabb" 20 | Output: -1 21 | 22 | 23 | Constraints: 24 | 25 | 1 <= s.length <= 105 26 | s consists of only lowercase English letters. 27 | -------------------------------------------------------------------------------- /645. Set Mismatch/645. Set Mismatch.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] findErrorNums(int[] nums) { 3 | int []freq = new int[nums.length+1] ; 4 | 5 | for(int i : nums) 6 | freq[i]++ ; 7 | 8 | int []ans = new int[2] ; 9 | 10 | boolean flag =true ; 11 | int ind = 0 ; 12 | 13 | for(int i : nums) 14 | if(freq[i]==2) 15 | {ans[ind++] = i ;break; } 16 | 17 | for(int i = 1;i<=nums.length;i++) 18 | if(freq[i] == 0) 19 | ans[ind] = i ; 20 | 21 | return ans; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /783. Minimum Distance Between BST Nodes/README.md: -------------------------------------------------------------------------------- 1 | 783. Minimum Distance Between BST Nodes => 2 | --------------------------------------- 3 | 4 | 5 | Given the root of a Binary Search Tree (BST), return the minimum difference between the values of any two different nodes in the tree. 6 | 7 | 8 | 9 | Example 1: 10 | 11 | 12 | Input: root = [4,2,6,1,3] 13 | Output: 1 14 | Example 2: 15 | 16 | 17 | Input: root = [1,0,48,null,null,12,49] 18 | Output: 1 19 | 20 | 21 | Constraints: 22 | 23 | The number of nodes in the tree is in the range [2, 100]. 24 | 0 <= Node.val <= 105 25 | 26 | -------------------------------------------------------------------------------- /Delete middle element of a stack/Delete middle element of a stack.java: -------------------------------------------------------------------------------- 1 | 2 | class Solution 3 | { 4 | 5 | public void deleteMid(Stackst,int sizeOfStack){ 6 | ArrayListal = new ArrayList<>() ; 7 | int cnt = 0; 8 | int mid = (sizeOfStack)/2 ; 9 | while(!st.isEmpty()) 10 | { 11 | int temp = st.pop() ; 12 | if(cnt != mid) 13 | al.add(temp) ; 14 | cnt++; 15 | } 16 | 17 | for(int i = al.size()-1;i>=0;i--) 18 | st.add(al.get(i)) ; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Frogs and Jumps/Frogs and Jumps.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int unvisitedLeaves(int N, int leaves, int frogs[]) { 3 | 4 | BitSet visited = new BitSet(leaves + 1); 5 | 6 | 7 | for (int frog : frogs) { 8 | 9 | if (frog <= leaves && !visited.get(frog)) { 10 | 11 | for (int j = frog; j <= leaves; j += frog) { 12 | visited.set(j); 13 | } 14 | } 15 | } 16 | 17 | 18 | int unvisited = leaves- visited.cardinality(); 19 | 20 | 21 | return unvisited; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Sort 0s, 1s and 2s/Readme.md: -------------------------------------------------------------------------------- 1 | Sort 0s, 1s and 2s => 2 | ------------------ 3 | 4 | 5 | Given an array arr containing only 0s, 1s, and 2s. Sort the array in ascending order. 6 | 7 | Examples: 8 | 9 | Input: arr[]= [0, 2, 1, 2, 0] 10 | Output: 0 0 1 2 2 11 | Explanation: 0s 1s and 2s are segregated into ascending order. 12 | Input: arr[] = [0, 1, 0] 13 | Output: 0 0 1 14 | Explanation: 0s 1s and 2s are segregated into ascending order. 15 | Expected Time Complexity: O(n) 16 | Expected Auxiliary Space: O(1) 17 | 18 | Constraints: 19 | 1 <= arr.size() <= 106 20 | 0 <= arr[i] <= 2 21 | 22 | -------------------------------------------------------------------------------- /101. Symmetric Tree/README.md: -------------------------------------------------------------------------------- 1 | 101. Symmetric Tree => 2 | --------------------- 3 | 4 | 5 | Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center). 6 | 7 | 8 | 9 | Example 1: 10 | 11 | 12 | Input: root = [1,2,2,3,4,4,3] 13 | Output: true 14 | Example 2: 15 | 16 | 17 | Input: root = [1,2,2,null,3,null,3] 18 | Output: false 19 | 20 | 21 | Constraints: 22 | 23 | The number of nodes in the tree is in the range [1, 1000]. 24 | -100 <= Node.val <= 100 25 | 26 | 27 | Follow up: Could you solve it both recursively and iteratively? 28 | -------------------------------------------------------------------------------- /229. Majority Element II/Readme.md: -------------------------------------------------------------------------------- 1 | 229. Majority Element II => 2 | ------------------------ 3 | 4 | 5 | Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. 6 | 7 | 8 | 9 | Example 1: 10 | 11 | Input: nums = [3,2,3] 12 | Output: [3] 13 | Example 2: 14 | 15 | Input: nums = [1] 16 | Output: [1] 17 | Example 3: 18 | 19 | Input: nums = [1,2] 20 | Output: [1,2] 21 | 22 | 23 | Constraints: 24 | 25 | 1 <= nums.length <= 5 * 104 26 | -109 <= nums[i] <= 109 27 | 28 | 29 | Follow up: Could you solve the problem in linear time and in O(1) space? 30 | 31 | -------------------------------------------------------------------------------- /1291. Sequential Digits/Readme.md: -------------------------------------------------------------------------------- 1 | 1291. Sequential Digits => 2 | ----------------------- 3 | 4 | 5 | An integer has sequential digits if and only if each digit in the number is one more than the previous digit. 6 | 7 | Return a sorted list of all the integers in the range [low, high] inclusive that have sequential digits. 8 | 9 | 10 | 11 | Example 1: 12 | 13 | Input: low = 100, high = 300 14 | Output: [123,234] 15 | Example 2: 16 | 17 | Input: low = 1000, high = 13000 18 | Output: [1234,2345,3456,4567,5678,6789,12345] 19 | 20 | 21 | Constraints: 22 | 23 | 10 <= low <= high <= 10^9 24 | -------------------------------------------------------------------------------- /144. Binary Tree Preorder Traversal/Binary Tree Preorder Traversal.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List preorderTraversal(TreeNode root) { 3 | Listans = new ArrayList<>() ; 4 | solve(root,ans ); 5 | return ans ; 6 | 7 | } 8 | TreeNode solve(TreeNode root , Listans) 9 | { 10 | if(root == null) 11 | { 12 | return null ; 13 | }ans.add(root.val) ; 14 | root.left = solve(root.left, ans) ; 15 | 16 | root.right = solve(root.right, ans) ; 17 | return null ; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /1550. Three Consecutive Odds/Readme.md: -------------------------------------------------------------------------------- 1 | 1550. Three Consecutive Odds => 2 | ---------------------------- 3 | 4 | 5 | Given an integer array arr, return true if there are three consecutive odd numbers in the array. Otherwise, return false. 6 | 7 | 8 | Example 1: 9 | 10 | Input: arr = [2,6,4,1] 11 | Output: false 12 | Explanation: There are no three consecutive odds. 13 | Example 2: 14 | 15 | Input: arr = [1,2,34,3,4,5,7,23,12] 16 | Output: true 17 | Explanation: [5,7,23] are three consecutive odds. 18 | 19 | 20 | Constraints: 21 | 22 | 1 <= arr.length <= 1000 23 | 1 <= arr[i] <= 1000 24 | -------------------------------------------------------------------------------- /1768. Merge Strings Alternately/1768. Merge Strings Alternately.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String mergeAlternately(String word1, String word2) { 3 | StringBuilder ans = new StringBuilder(); 4 | int i = 0; 5 | while (i < word1.length() || i < word2.length()) { 6 | if (i < word1.length()) { 7 | ans.append(word1.charAt(i)); 8 | } 9 | if (i < word2.length()) { 10 | ans.append(word2.charAt(i)); 11 | } 12 | i++; 13 | } 14 | return ans.toString(); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /343. Integer Break/343. Integer Break.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int integerBreak(int n) { 3 | int []dp = new int[60] ; 4 | 5 | dp[1] = 1 ; 6 | dp[2] = 1 ; 7 | dp[3] = 2 ; 8 | 9 | for(int i = 4;i<=n;i++){ 10 | int num = i ; 11 | for(int j = 1;j 3 | ------------------- 4 | 5 | 6 | Write a function that reverses a string. The input string is given as an array of characters s. 7 | 8 | You must do this by modifying the input array in-place with O(1) extra memory. 9 | 10 | 11 | 12 | Example 1: 13 | 14 | Input: s = ["h","e","l","l","o"] 15 | Output: ["o","l","l","e","h"] 16 | Example 2: 17 | 18 | Input: s = ["H","a","n","n","a","h"] 19 | Output: ["h","a","n","n","a","H"] 20 | 21 | 22 | Constraints: 23 | 24 | 1 <= s.length <= 105 25 | s[i] is a printable ascii character. 26 | -------------------------------------------------------------------------------- /46. Permutations/Readme.md: -------------------------------------------------------------------------------- 1 | 46. Permutations => 2 | ----------------- 3 | 4 | 5 | Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order. 6 | 7 | 8 | 9 | Example 1: 10 | 11 | Input: nums = [1,2,3] 12 | Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]] 13 | Example 2: 14 | 15 | Input: nums = [0,1] 16 | Output: [[0,1],[1,0]] 17 | Example 3: 18 | 19 | Input: nums = [1] 20 | Output: [[1]] 21 | 22 | 23 | Constraints: 24 | 25 | 1 <= nums.length <= 6 26 | -10 <= nums[i] <= 10 27 | All the integers of nums are unique. 28 | -------------------------------------------------------------------------------- /704. Binary Search/704. Binary Search.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int search(int[] nums, int target) { 3 | int left = 0; 4 | int right = nums.length - 1; 5 | 6 | while (left <= right) { 7 | int mid = left + (right - left) / 2; 8 | if (nums[mid] == target) { 9 | return mid; 10 | } else if (nums[mid] < target) { 11 | left = mid + 1; 12 | } else { 13 | right = mid - 1; 14 | } 15 | } 16 | 17 | return -1; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Arranging the array/Arranging the array.java: -------------------------------------------------------------------------------- 1 | // Brute Force solution 2 | class Solution { 3 | 4 | public void Rearrange(int a[], int n) 5 | { 6 | ArrayListal = new ArrayList<>(); 7 | 8 | for(int i: a){ 9 | if(i<0) 10 | al.add(i); 11 | } 12 | for(int i: a){ 13 | if(i>=0) 14 | al.add(i); 15 | } 16 | 17 | 18 | for(int i = 0 ;i> printGraph(int V, int edges[][]) { 3 | List> ans = new ArrayList<>() ; 4 | 5 | 6 | for(int i = 0 ;i() ) ; 8 | 9 | 10 | 11 | for(int i = 0 ;i 2 | -------------------------- 3 | 4 | 5 | You are given a number n, Return the count of total numbers from 1 to n containing 4 as a digit. 6 | 7 | Examples: 8 | 9 | Input: n = 9 10 | Output: 1 11 | Explanation: 4 is the only number between 1 to 9 which contains 4 as a digit. 12 | Input: n = 44 13 | Output: 9 14 | Explanation: 4, 14, 24, 34, 40, 41, 42, 43 & 44, there are total 9 numbers containing 4 as a digit. 15 | Expected Time Complexity: O(nlogn) 16 | Expected Auxiliary Space: O(1) 17 | 18 | Constraints: 19 | 1 <= n <= 105 20 | -------------------------------------------------------------------------------- /Find Number/Find Number.java: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public long findNumber(long N) 4 | { 5 | 6 | String s=""; 7 | long ans=0; 8 | int a[] = {1,3,5,7,9}; 9 | while(N>0) 10 | { 11 | 12 | long curr = N%5; 13 | if(curr==0) s=a[4]+s; 14 | else s=a[(int)curr-1]+s; 15 | 16 | 17 | if(N%5==0 && N>0) 18 | N=N/5-1; 19 | 20 | else 21 | N=N/5; 22 | } 23 | return Long.parseLong(s); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Juggler Sequence/Juggler Sequence.java: -------------------------------------------------------------------------------- 1 | 2 | class Solution { 3 | static List jugglerSequence(long n) { 4 | Listans = new ArrayList<>() ; 5 | ans.add(n) ; 6 | while(n>1) 7 | { 8 | 9 | if(n % 2== 0 ) 10 | { 11 | n = (long)Math.sqrt(n) ; 12 | } 13 | else 14 | { 15 | n = (long)(Math.sqrt(n ) *n ) ; 16 | 17 | } 18 | 19 | ans.add(n); 20 | } 21 | 22 | return ans; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Kth largest element in BST/Kth largest element in BST.java: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | 4 | public static void reverseInorder(Node root,ArrayList arr){ 5 | if(root==null){ 6 | return; 7 | } 8 | reverseInorder(root.right,arr); 9 | arr.add(root.data); 10 | reverseInorder(root.left,arr); 11 | } 12 | public int kthLargest(Node root,int K) 13 | { 14 | ArrayList arr=new ArrayList<>(); 15 | reverseInorder(root,arr); 16 | 17 | return arr.get(K-1); 18 | } 19 | } 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /Maximum sum increasing subsequence/Maximum sum increasing subsequence.java: -------------------------------------------------------------------------------- 1 | 2 | class Solution 3 | { 4 | public int maxSumIS(int arr[], int n) 5 | { 6 | int []msis = new int[n] ; 7 | msis[0] = arr[0] ; 8 | int res = arr[0] ; 9 | 10 | for(int i= 1 ;ist = new HashSet<>() ; 7 | Node cur = head , prev = null ; 8 | while(cur!=null) { 9 | if(st.contains(cur.data)) 10 | { 11 | prev.next = cur.next ; 12 | } 13 | else 14 | { 15 | st.add(cur.data) ; 16 | prev = cur ; 17 | } 18 | cur = cur.next ; 19 | } 20 | return head; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Sum of Products/Sum of Products.java: -------------------------------------------------------------------------------- 1 | 2 | class Solution { 3 | static long pairAndSum(int n, long arr[]) { 4 | long ans = 0; 5 | for (int i = 0; i < 32; i++) { 6 | long c = 0; 7 | long t = 1 << i; 8 | for (int j = 0; j < n; j++) { 9 | if ((t & arr[j]) != 0) { 10 | c++; // no.of elements that have 1's on the same bit 11 | } 12 | } 13 | ans += (c * (c - 1) / 2) * (t); // nC2 and converting into decimal also 14 | } 15 | return ans; 16 | 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Xoring and Clearing/Xoring and Clearing.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public void printArr(int n, int arr[]) { 3 | for(int i : arr) 4 | { 5 | System.out.print(i +" ") ; 6 | 7 | } 8 | System.out.println() ; 9 | } 10 | 11 | public void setToZero(int n, int arr[]) { 12 | Arrays.fill(arr,0) ; 13 | } 14 | 15 | public void xor1ToN(int n, int arr[]) { 16 | int ind = 0 ; 17 | for(int i : arr) 18 | { 19 | arr[ind] = ind^i; 20 | ind++; 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /1431. Kids With the Greatest Number of Candies/1431. Kids With the Greatest Number of Candies.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List kidsWithCandies(int[] candies, int extraCandies) { 3 | int maxCandi = Integer.MIN_VALUE ; 4 | for(int i : candies){ 5 | maxCandi = Math.max(i,maxCandi) ; 6 | } 7 | List ans = new ArrayList<>(); 8 | for(int i : candies){ 9 | if((i+extraCandies)>=maxCandi) 10 | ans.add(true) ; 11 | else 12 | ans.add(false); 13 | } 14 | return ans; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /1535. Find the Winner of an Array Game/1535. Find the Winner of an Array Game.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int getWinner(int[] arr, int k) { 3 | int winner = arr[0] ; 4 | int cnt = 0 ; 5 | 6 | for(int i = 1;i arr[i])cnt++; 9 | else 10 | { 11 | winner = arr[i] ; 12 | cnt = 1; 13 | } 14 | 15 | if(cnt == k)return winner; 16 | } 17 | 18 | return winner; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /1758. Minimum Changes To Make Alternating Binary String/1758. Minimum Changes To Make Alternating Binary String.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | int check(String s, char ch){ 3 | int total = 0 ; 4 | 5 | for(int i = 0 ;i 2 | -------------------------------------------- 3 | 4 | 5 | Given two non-negative integers low and high. Return the count of odd numbers between low and high (inclusive). 6 | 7 | 8 | 9 | Example 1: 10 | 11 | Input: low = 3, high = 7 12 | Output: 3 13 | Explanation: The odd numbers between 3 and 7 are [3,5,7]. 14 | Example 2: 15 | 16 | Input: low = 8, high = 10 17 | Output: 1 18 | Explanation: The odd numbers between 8 and 10 are [9]. 19 | 20 | 21 | Constraints: 22 | 23 | 0 <= low <= high <= 10^9 24 | -------------------------------------------------------------------------------- /1557. Minimum Number of Vertices to Reach All Nodes/1557. Minimum Number of Vertices to Reach All Nodes.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List findSmallestSetOfVertices(int n, List> edges) { 3 | int[] inDegree = new int[n]; 4 | for (List e : edges) { 5 | inDegree[e.get(1)]++; 6 | } 7 | 8 | List result = new ArrayList(); 9 | for (int i = 0; i < n; i++) 10 | if (inDegree[i] == 0) 11 | result.add(i); 12 | 13 | return result; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /1759. Count Number of Homogenous Substrings/1759. Count Number of Homogenous Substrings.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int countHomogenous(String s) { 3 | int mod = 1000000007; 4 | int n = s.length() ; 5 | long ans = 0 ; 6 | 7 | for(int i = 0 ;i 0) 14 | { 15 | int d = n%10 ; 16 | if(d == 4) 17 | { return true ; 18 | 19 | } 20 | n /= 10 ; 21 | } 22 | 23 | return false; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Minimize the Heights II/Minimize the Heights II.java: -------------------------------------------------------------------------------- 1 | 2 | class Solution { 3 | int getMinDiff(int[] arr, int n, int k) { 4 | if(n == 1)return 0 ; 5 | 6 | int min ,max ; 7 | 8 | Arrays.sort(arr) ; 9 | int dif = arr[n-1]-arr[0] ; 10 | 11 | for(int i = 1;istr.length()) 8 | return 0 ; 9 | String temp = str.substring(0,k); 10 | for(int i = 0 ;iarr[i].length()) 13 | continue; 14 | 15 | String s = arr[i].substring(0,k) ; 16 | if(s.equals(temp)) 17 | cnt++; 18 | } 19 | return cnt; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /225. Implement Stack using Queues/225. Implement Stack using Queues.java: -------------------------------------------------------------------------------- 1 | public class MyStack { 2 | private Queue q; 3 | 4 | public MyStack() { 5 | q = new LinkedList<>(); 6 | } 7 | 8 | public void push(int x) { 9 | q.add(x); 10 | for (int i = 1; i < q.size(); i++) { 11 | q.add(q.remove()); 12 | } 13 | } 14 | 15 | public int pop() { 16 | return q.remove(); 17 | } 18 | 19 | public int top() { 20 | return q.peek(); 21 | } 22 | 23 | public boolean empty() { 24 | return q.isEmpty(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /2551. Put Marbles in Bags/2551. Put Marbles in Bags.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public long putMarbles(int[] weights, int k) { 3 | int n = weights.length; 4 | int[] pairs = new int[n - 1]; 5 | for (int i = 1; i < n; i++) { 6 | pairs[i - 1] = weights[i] + weights[i - 1]; 7 | } 8 | Arrays.sort(pairs); 9 | long minScore = 0; 10 | long maxScore = 0; 11 | for (int i = 0; i < k - 1; i++) { 12 | minScore += pairs[i]; 13 | maxScore += pairs[n - i - 2]; 14 | } 15 | return maxScore - minScore; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /905. Sort Array By Parity/Readme.md: -------------------------------------------------------------------------------- 1 | 905. Sort Array By Parity => 2 | -------------------------- 3 | 4 | 5 | Given an integer array nums, move all the even integers at the beginning of the array followed by all the odd integers. 6 | 7 | Return any array that satisfies this condition. 8 | 9 | 10 | 11 | Example 1: 12 | 13 | Input: nums = [3,1,2,4] 14 | Output: [2,4,3,1] 15 | Explanation: The outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepted. 16 | Example 2: 17 | 18 | Input: nums = [0] 19 | Output: [0] 20 | 21 | 22 | Constraints: 23 | 24 | 1 <= nums.length <= 5000 25 | 0 <= nums[i] <= 5000 26 | -------------------------------------------------------------------------------- /Count Cyclic Paths/Count Cyclic Paths.java: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | static int mod = 1000000007; 3 | public static int countPaths(int n){ 4 | //code here 5 | 6 | int dp[][] = new int[4][n+1]; 7 | dp[0][0] = 1; 8 | for (int i = 1; i <= n; i++) { 9 | for (int j = 0; j <= 3; j++) { 10 | dp[j][i] = (dp[j][i] + (dp[(j + 1) % 4][i - 1])) % mod; 11 | dp[j][i] = (dp[j][i] + (dp[(j + 2) % 4][i - 1])) % mod; 12 | dp[j][i] = (dp[j][i] + (dp[(j + 3) % 4][i - 1])) % mod; 13 | } 14 | } 15 | return dp[0][n]; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /DFS of Graph/DFS of Graph.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | 3 | public ArrayList dfsOfGraph(int V, ArrayList> adj) { 4 | ArrayListal = new ArrayList<>() ; 5 | boolean []vis = new boolean[V]; 6 | 7 | dfs(0,vis,adj,al) ; 8 | return al ; 9 | } 10 | private void dfs(int v,boolean []vis ,ArrayList> adj , ArrayListal ){ 11 | vis[v] = true ; 12 | al.add(v) ; 13 | for(int nei : adj.get(v)){ 14 | if(!vis[nei]) 15 | dfs(nei,vis,adj,al); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Mirror Tree/Mirror Tree.java: -------------------------------------------------------------------------------- 1 | 2 | // FUNCTION CODE 3 | /* A Binary Tree node 4 | class Node 5 | { 6 | int data; 7 | Node left, right; 8 | Node(int item) 9 | { 10 | data = item; 11 | left = right = null; 12 | } 13 | } */ 14 | 15 | class Solution { 16 | // Function to convert a binary tree into its mirror tree. 17 | void mirror(Node node) { 18 | if(node == null)return ; 19 | 20 | mirror(node.left); 21 | mirror(node.right); 22 | 23 | Node t = node.left; 24 | node.left = node.right; 25 | node.right = t; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /104. Maximum Depth of Binary Tree/README.md: -------------------------------------------------------------------------------- 1 | 104. Maximum Depth of Binary Tree => 2 | --------------------------------- 3 | 4 | 5 | Given the root of a binary tree, return its maximum depth. 6 | 7 | A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 8 | 9 | 10 | 11 | Example 1: 12 | 13 | 14 | Input: root = [3,9,20,null,null,15,7] 15 | Output: 3 16 | Example 2: 17 | 18 | Input: root = [1,null,2] 19 | Output: 2 20 | 21 | 22 | Constraints: 23 | 24 | The number of nodes in the tree is in the range [0, 104]. 25 | -100 <= Node.val <= 100 26 | -------------------------------------------------------------------------------- /1550. Three Consecutive Odds/1550. Three Consecutive Odds.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean threeConsecutiveOdds(int[] arr) { 3 | if(arr.length<3)return false; 4 | 5 | int odd = 0 ; 6 | for(int i = 0 ;i<3;i++) 7 | if(arr[i]%2!=0) odd++; 8 | 9 | if(odd==3)return true; 10 | 11 | int st = 0 ; 12 | 13 | for(int i = 3;i 1) 11 | { 12 | String count = j-i+"" ; 13 | for(char ch : count.toCharArray()) 14 | chars[ind++] = ch ; 15 | } 16 | i= j ; 17 | } 18 | return ind ; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /629. K Inverse Pairs Array/629. K Inverse Pairs Array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int kInversePairs(int n, int k) { 3 | int[][] dp = new int[1001][1001]; 4 | dp[0][0] = 1; 5 | 6 | for (int i = 1; i <= n; i++) { 7 | for (int j = 0; j <= k; j++) { 8 | for (int x = 0; x <= Math.min(j, i - 1); x++) { 9 | if (j - x >= 0) { 10 | dp[i][j] = (dp[i][j] + dp[i - 1][j - x]) % 1000000007; 11 | } 12 | } 13 | } 14 | } 15 | 16 | return dp[n][k]; 17 | } 18 | } 19 | 20 | -------------------------------------------------------------------------------- /Count the Substrings/Count the Substrings.java: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | int countSubstring(String S) 4 | { 5 | int cnt ; 6 | int ans = 0 ; 7 | for(int i = 0 ;i= 'a' && ch<='z') 12 | cnt++ ; 13 | else 14 | cnt--; 15 | 16 | if(cnt == 0 ) 17 | ans++; 18 | } 19 | } 20 | return ans ; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /1137. N-th Tribonacci Number/README.md: -------------------------------------------------------------------------------- 1 | 1137. N-th Tribonacci Number => 2 | ---------------------------- 3 | 4 | 5 | The Tribonacci sequence Tn is defined as follows: 6 | 7 | T0 = 0, T1 = 1, T2 = 1, and Tn+3 = Tn + Tn+1 + Tn+2 for n >= 0. 8 | 9 | Given n, return the value of Tn. 10 | 11 | 12 | 13 | Example 1: 14 | 15 | Input: n = 4 16 | Output: 4 17 | Explanation: 18 | T_3 = 0 + 1 + 1 = 2 19 | T_4 = 1 + 1 + 2 = 4 20 | Example 2: 21 | 22 | Input: n = 25 23 | Output: 1389537 24 | 25 | 26 | Constraints: 27 | 28 | 0 <= n <= 37 29 | The answer is guaranteed to fit within a 32-bit integer, ie. answer <= 2^31 - 1. 30 | -------------------------------------------------------------------------------- /2149. Rearrange Array Elements by Sign/2149. Rearrange Array Elements by Sign.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] rearrangeArray(int[] nums) { 3 | int[] ans = new int[nums.length] ; 4 | Listal = new ArrayList<>() ; 5 | 6 | for(int i : nums) 7 | if(i<0) 8 | al.add(i) ; 9 | 10 | int ind = 0 , cnt = 0 ; 11 | 12 | for(int i: nums) 13 | { 14 | if(i>=1) 15 | { 16 | ans[ind++] = i; 17 | ans[ind++] = al.get(cnt++) ; 18 | } 19 | } 20 | 21 | return ans; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /852. Peak Index in a Mountain Array/852. Peak Index in a Mountain Array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int peakIndexInMountainArray(int[] arr) { 3 | int ind = 0 ; 4 | if(arr.length == 0 ) 5 | return 0; 6 | int l = 0 , h = arr.length - 1; 7 | while(l<= h){ 8 | int m = (l+h)/2 ; 9 | if(m != 0 && arr[m-1]=0)?pos:-1 ; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /168. Excel Sheet Column Title/Readme.md: -------------------------------------------------------------------------------- 1 | 168. Excel Sheet Column Title => 2 | ------------------------------ 3 | 4 | 5 | Given an integer columnNumber, return its corresponding column title as it appears in an Excel sheet. 6 | 7 | For example: 8 | 9 | A -> 1 10 | B -> 2 11 | C -> 3 12 | ... 13 | Z -> 26 14 | AA -> 27 15 | AB -> 28 16 | ... 17 | 18 | 19 | Example 1: 20 | 21 | Input: columnNumber = 1 22 | Output: "A" 23 | Example 2: 24 | 25 | Input: columnNumber = 28 26 | Output: "AB" 27 | Example 3: 28 | 29 | Input: columnNumber = 701 30 | Output: "ZY" 31 | 32 | 33 | Constraints: 34 | 35 | 1 <= columnNumber <= 231 - 1 36 | -------------------------------------------------------------------------------- /287. Find the Duplicate Number/287. Find the Duplicate Number.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findDuplicate(int[] nums) { 3 | int n = nums.length ; 4 | 5 | int max = 0 ; 6 | int maxInd = 0 ; 7 | 8 | for(int i = 0 ;imax){ 15 | max = nums[i] ; 16 | maxInd = i ; 17 | } 18 | nums[i] %= n ; 19 | } 20 | 21 | return maxInd ; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /525. Contiguous Array/525. Contiguous Array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findMaxLength(int[] nums) { 3 | Mapmap = new HashMap<>() ; 4 | 5 | int length = 0 , sum = 0 ; 6 | for(int i = 0 ;i 2 | --------------------- 3 | 4 | 5 | Given a binary array nums, return the maximum length of a contiguous subarray with an equal number of 0 and 1. 6 | 7 | 8 | 9 | Example 1: 10 | 11 | Input: nums = [0,1] 12 | Output: 2 13 | Explanation: [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. 14 | Example 2: 15 | 16 | Input: nums = [0,1,0] 17 | Output: 2 18 | Explanation: [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. 19 | 20 | 21 | Constraints: 22 | 23 | 1 <= nums.length <= 105 24 | nums[i] is either 0 or 1. 25 | -------------------------------------------------------------------------------- /Form a palindrome/Readme.md: -------------------------------------------------------------------------------- 1 | Form a palindrome => 2 | ------------------ 3 | 4 | 5 | Given a string, find the minimum number of characters to be inserted to convert it to a palindrome. 6 | 7 | Examples : 8 | 9 | Input: str = "abcd" 10 | Output: 3 11 | Explanation: Inserted character marked with bold characters in dcbabcd, here we need minimum three characters to make it palindrome. 12 | Input: str = "aa" 13 | Output: 0 14 | Explanation: "aa" is already a palindrome. 15 | Expected Time Complexity: O(n2) 16 | Expected Auxiliary Space: O(n2) 17 | 18 | Constraints: 19 | 1 ≤ |str| ≤ 500 20 | str contains only lowercase alphabets. 21 | -------------------------------------------------------------------------------- /Minimum distance between two numbers/Minimum distance between two numbers.java: -------------------------------------------------------------------------------- 1 | 2 | class Solution { 3 | int minDist(int a[], int n, int x, int y) { 4 | int min = Integer.MAX_VALUE ; 5 | int indx = -1 ,indy = -1 ; 6 | 7 | for(int i = 0 ;ist = new HashSet<>() ; 8 | st.add(' '); 9 | 10 | for(int i= 0 ;ist = new Stack<>() ; 11 | for(int i = 0 ;i dictionary ) 5 | { 6 | Set ws = new HashSet<>(dictionary); 7 | int N = s.length(); 8 | boolean[] dp = new boolean[N + 1]; 9 | dp[0] = true; 10 | 11 | for (int i=1;i<=N;i++) { 12 | for (int j=0;j 2 | ------------------------------- 3 | 4 | 5 | Given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must be unique and you may return the result in any order. 6 | 7 | 8 | 9 | Example 1: 10 | 11 | Input: nums1 = [1,2,2,1], nums2 = [2,2] 12 | Output: [2] 13 | Example 2: 14 | 15 | Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4] 16 | Output: [9,4] 17 | Explanation: [4,9] is also accepted. 18 | 19 | 20 | Constraints: 21 | 22 | 1 <= nums1.length, nums2.length <= 1000 23 | 0 <= nums1[i], nums2[i] <= 1000 24 | -------------------------------------------------------------------------------- /45. Jump Game II/Jump Game II .java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int jump(int[] nums) { 3 | Integer []dp = new Integer[nums.length] ; 4 | dp[nums.length-1]= 0 ; 5 | 6 | for(int i = nums.length-2 ;i>=0 ;i--){ 7 | int min = Integer.MAX_VALUE ; 8 | int steps = nums[i] ; 9 | for(int j = 1;j<=steps && (j+i)st =new Stack<>() ; 4 | 5 | StringBuilder sb = new StringBuilder() ; 6 | for(int i = 0 ;i= g; i--) { 10 | for (int j = minProfit; j >= 0; j--) { 11 | dp[i][j] = (dp[i][j] + dp[i - g][Math.max(0, j - p)])%mod; 12 | } 13 | } 14 | } 15 | int sum = 0; 16 | for(int i = 0; i <= n; i++){ 17 | sum = (sum + dp[i][minProfit])%mod; 18 | } 19 | return sum; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Check if string is rotated by two places/Check if string is rotated by two places.java: -------------------------------------------------------------------------------- 1 | 2 | class Solution 3 | { 4 | public static boolean isRotated(String str1, String str2) 5 | { 6 | int n1 = str1.length(); 7 | 8 | int n2 = str2.length(); 9 | 10 | if(n1 != n2) { 11 | 12 | return false; 13 | 14 | } 15 | 16 | 17 | 18 | String rotatedLeft = str1.substring(2) + str1.substring(0, 2); 19 | 20 | String rotatedRight = str1.substring(n1 - 2) + str1.substring(0, n1 - 2); 21 | 22 | 23 | 24 | return rotatedLeft.equals(str2) || rotatedRight.equals(str2); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /1887. Reduction Operations to Make the Array Elements Equal/1887. Reduction Operations to Make the Array Elements Equal.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int reductionOperations(int[] nums) { 3 | int n = nums.length; 4 | int[] freq = new int[50001]; 5 | for (int i = 0; i < n; i++) { 6 | freq[nums[i]]++; 7 | } 8 | int res = 0, operations = 0; 9 | for (int i = 50000; i >= 1; i--) { 10 | if (freq[i] > 0) { 11 | operations += freq[i]; 12 | res += operations - freq[i]; 13 | } 14 | } 15 | return res; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /209. Minimum Size Subarray Sum/209. Minimum Size Subarray Sum.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minSubArrayLen(int target, int[] nums) { 3 | int i = 0 , j; 4 | int ans = Integer.MAX_VALUE ; 5 | int curSum = 0 ; 6 | for( j = 0 ;j=target) 12 | { 13 | ans = Math.min(ans,j-i+1); 14 | curSum -= nums[i] ; i++ ; 15 | } 16 | } 17 | 18 | 19 | return (ans!=Integer.MAX_VALUE )?ans:0; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /215. Kth Largest Element in an Array/Readme.md: -------------------------------------------------------------------------------- 1 | 215. Kth Largest Element in an Array => 2 | ------------------------------------ 3 | 4 | 5 | Given an integer array nums and an integer k, return the kth largest element in the array. 6 | 7 | Note that it is the kth largest element in the sorted order, not the kth distinct element. 8 | 9 | Can you solve it without sorting? 10 | 11 | 12 | 13 | Example 1: 14 | 15 | Input: nums = [3,2,1,5,6,4], k = 2 16 | Output: 5 17 | Example 2: 18 | 19 | Input: nums = [3,2,3,1,2,4,5,5,6], k = 4 20 | Output: 4 21 | 22 | 23 | Constraints: 24 | 25 | 1 <= k <= nums.length <= 105 26 | -104 <= nums[i] <= 104 27 | -------------------------------------------------------------------------------- /542. 01 Matrix/Readme.md: -------------------------------------------------------------------------------- 1 | 542. 01 Matrix => 2 | -------------- 3 | 4 | 5 | Given an m x n binary matrix mat, return the distance of the nearest 0 for each cell. 6 | 7 | The distance between two adjacent cells is 1. 8 | 9 | 10 | 11 | Example 1: 12 | 13 | 14 | Input: mat = [[0,0,0],[0,1,0],[0,0,0]] 15 | Output: [[0,0,0],[0,1,0],[0,0,0]] 16 | Example 2: 17 | 18 | 19 | Input: mat = [[0,0,0],[0,1,0],[1,1,1]] 20 | Output: [[0,0,0],[0,1,0],[1,2,1]] 21 | 22 | 23 | Constraints: 24 | 25 | m == mat.length 26 | n == mat[i].length 27 | 1 <= m, n <= 104 28 | 1 <= m * n <= 104 29 | mat[i][j] is either 0 or 1. 30 | There is at least one 0 in mat. 31 | -------------------------------------------------------------------------------- /75. Sort Colors/75. Sort Colors.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public void sortColors(int[] nums) { 3 | int mid = 0 ,st = 0, end = nums.length-1; 4 | 5 | while(mid<=end){ 6 | if(nums[mid]==0 ) 7 | {swap(nums,mid,st) ; 8 | st++; 9 | mid++; 10 | } 11 | else if(nums[mid]==1)mid++; 12 | else { 13 | swap(nums,mid,end); 14 | end--; 15 | } 16 | } 17 | } 18 | void swap(int []a,int i,int j){ 19 | int t = a[i]; 20 | a[i] = a[j] ; 21 | a[j] = t ; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /94. Binary Tree Inorder Traversal/Readme.md: -------------------------------------------------------------------------------- 1 | 94. Binary Tree Inorder Traversal => 2 | ---------------------------------- 3 | 4 | 5 | Given the root of a binary tree, return the inorder traversal of its nodes' values. 6 | 7 | 8 | 9 | Example 1: 10 | 11 | 12 | Input: root = [1,null,2,3] 13 | Output: [1,3,2] 14 | Example 2: 15 | 16 | Input: root = [] 17 | Output: [] 18 | Example 3: 19 | 20 | Input: root = [1] 21 | Output: [1] 22 | 23 | 24 | Constraints: 25 | 26 | The number of nodes in the tree is in the range [0, 100]. 27 | -100 <= Node.val <= 100 28 | 29 | 30 | Follow up: Recursive solution is trivial, could you do it iteratively? 31 | -------------------------------------------------------------------------------- /Reverse First K elements of Queue/Reverse First K elements of Queue.java: -------------------------------------------------------------------------------- 1 | 2 | class GfG { 3 | public Queue modifyQueue(Queue q, int k) { 4 | Stackst = new Stack<>(); 5 | int n = q.size(); 6 | for(int i=0;i 2 | ------------------------ 3 | 4 | 5 | You are given two strings s and t. 6 | 7 | String t is generated by random shuffling string s and then add one more letter at a random position. 8 | 9 | Return the letter that was added to t. 10 | 11 | 12 | 13 | Example 1: 14 | 15 | Input: s = "abcd", t = "abcde" 16 | Output: "e" 17 | Explanation: 'e' is the letter that was added. 18 | Example 2: 19 | 20 | Input: s = "", t = "y" 21 | Output: "y" 22 | 23 | 24 | Constraints: 25 | 26 | 0 <= s.length <= 1000 27 | t.length == s.length + 1 28 | s and t consist of lowercase English letters. 29 | -------------------------------------------------------------------------------- /459. Repeated Substring Pattern/459. Repeated Substring Pattern.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean repeatedSubstringPattern(String s) { 3 | int n = s.length(); 4 | for (int i = 1; i <= n / 2; i++) { 5 | if (n % i == 0) { 6 | StringBuilder pattern = new StringBuilder(); 7 | for (int j = 0; j < n / i; j++) { 8 | pattern.append(s.substring(0, i)); 9 | } 10 | if (s.equals(pattern.toString())) { 11 | return true; 12 | } 13 | } 14 | } 15 | return false; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Power Set/Power Set.java: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public List AllPossibleStrings(String s) 4 | { 5 | List list = new ArrayList<>(); 6 | list.add(""); 7 | for (char ch : s.toCharArray()) { 8 | int n = list.size(); 9 | for (int i = 0; i < n; i++) { 10 | String str = list.get(i); 11 | StringBuilder sb = new StringBuilder(str); 12 | sb.append(ch); 13 | list.add(sb.toString()); 14 | } 15 | } 16 | list.remove(0); 17 | Collections.sort(list); 18 | return list; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /169. Majority Element/Readme.md: -------------------------------------------------------------------------------- 1 | 169. Majority Element => 2 | --------------------- 3 | 4 | 5 | Given an array nums of size n, return the majority element. 6 | 7 | The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array. 8 | 9 | 10 | 11 | Example 1: 12 | 13 | Input: nums = [3,2,3] 14 | Output: 3 15 | Example 2: 16 | 17 | Input: nums = [2,2,1,1,1,2,2] 18 | Output: 2 19 | 20 | 21 | Constraints: 22 | 23 | n == nums.length 24 | 1 <= n <= 5 * 104 25 | -109 <= nums[i] <= 109 26 | 27 | 28 | Follow-up: Could you solve the problem in linear time and in O(1) space? 29 | -------------------------------------------------------------------------------- /567. Permutation in String/README.md: -------------------------------------------------------------------------------- 1 | 567. Permutation in String => 2 | -------------------------- 3 | 4 | 5 | Given two strings s1 and s2, return true if s2 contains a permutation of s1, or false otherwise. 6 | 7 | In other words, return true if one of s1's permutations is the substring of s2. 8 | 9 | 10 | 11 | Example 1: 12 | 13 | Input: s1 = "ab", s2 = "eidbaooo" 14 | Output: true 15 | Explanation: s2 contains one permutation of s1 ("ba"). 16 | Example 2: 17 | 18 | Input: s1 = "ab", s2 = "eidboaoo" 19 | Output: false 20 | 21 | 22 | Constraints: 23 | 24 | 1 <= s1.length, s2.length <= 104 25 | s1 and s2 consist of lowercase English letters. 26 | -------------------------------------------------------------------------------- /Even Swap/Even Swap.java: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | int [] lexicographicallyLargest(int [] arr, int n) { 3 | int i = 0 ; 4 | 5 | while(i=26 ; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Maximum Possible Value/Maximum Possible Value.java: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | long maxPossibleValue(int N, int A[] ,int B[]) { 4 | long total_stick = 0 , total_peri = 0 ; 5 | long min_len = Integer.MAX_VALUE ; 6 | for(int i = 0 ;i=2) 10 | min_len = Math.min(min_len,A[i]) ; 11 | 12 | total_stick += B[i] ; 13 | total_peri += A[i]*B[i] ; 14 | } 15 | 16 | if(total_stick %4 != 0 ) 17 | total_peri -= 2*min_len ; 18 | 19 | return total_peri ; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Remove Duplicates/Readme.md: -------------------------------------------------------------------------------- 1 | Remove Duplicates => 2 | ----------------- 3 | 4 | 5 | Given a string str without spaces, the task is to remove all duplicate characters from it, keeping only the first occurrence. 6 | 7 | Note: The original order of characters must be kept the same. 8 | 9 | Examples : 10 | 11 | Input: str = "zvvo" 12 | Output: "zvo" 13 | Explanation: Only keep the first occurrence 14 | Input: str = "gfg" 15 | Output: "gf" 16 | Explanation: Only keep the first occurrence 17 | Expected Time Complexity: O(n) 18 | Expected Auxiliary Space: O(1) 19 | 20 | Constraints: 21 | 1 <= |str| <= 105 22 | str contains lowercase English alphabets 23 | 24 | -------------------------------------------------------------------------------- /Transform to prime/Transform to prime.java: -------------------------------------------------------------------------------- 1 | 2 | class Solution 3 | { 4 | public int minNumber(int arr[], int N) 5 | { 6 | int sum = 0 ; 7 | for(int i : arr)sum += i ; 8 | if(isPrime(sum))return 0; 9 | 10 | for(int i = sum + 1;i<=sum *2;i++) 11 | if(isPrime(i)) 12 | return i - sum ; 13 | 14 | return 0 ; 15 | } 16 | public boolean isPrime(int x){ 17 | if(x == 1)return false ; 18 | if(x == 2)return true ; 19 | for(int i = 2;i<= Math.sqrt(x);i++) 20 | if(x%i ==0)return false ; 21 | 22 | return true; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /118. Pascal's Triangle/118. Pascal's Triangle.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List> generate(int numRows) { 3 | List> ans = new ArrayList<>() ; 4 | List row,pre = null ; 5 | for(int i = 0 ;i() ; 7 | 8 | for(int j = 0 ;j<=i;j++){ 9 | if(j == 0 || j == i) 10 | row.add(1) ; 11 | else 12 | row.add(pre.get(j-1)+pre.get(j)) ; 13 | } 14 | 15 | pre = row ; 16 | ans.add(row); 17 | } 18 | 19 | return ans; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /1424. Diagonal Traverse II/Readme.md: -------------------------------------------------------------------------------- 1 | 1424. Diagonal Traverse II -> 2 | -------------------------- 3 | 4 | 5 | 6 | 7 | Given a 2D integer array nums, return all elements of nums in diagonal order as shown in the below images. 8 | 9 | 10 | 11 | Example 1: 12 | 13 | 14 | Input: nums = [[1,2,3],[4,5,6],[7,8,9]] 15 | Output: [1,4,2,7,5,3,8,6,9] 16 | Example 2: 17 | 18 | 19 | Input: nums = [[1,2,3,4,5],[6,7],[8],[9,10,11],[12,13,14,15,16]] 20 | Output: [1,6,2,8,7,3,9,4,12,10,5,13,11,14,15,16] 21 | 22 | 23 | Constraints: 24 | 25 | 1 <= nums.length <= 105 26 | 1 <= nums[i].length <= 105 27 | 1 <= sum(nums[i].length) <= 105 28 | 1 <= nums[i][j] <= 105 29 | -------------------------------------------------------------------------------- /145. Binary Tree Postorder Traversal/Readme.md: -------------------------------------------------------------------------------- 1 | 145. Binary Tree Postorder Traversal => 2 | ------------------------------------ 3 | 4 | 5 | Given the root of a binary tree, return the postorder traversal of its nodes' values. 6 | 7 | 8 | 9 | Example 1: 10 | 11 | 12 | Input: root = [1,null,2,3] 13 | Output: [3,2,1] 14 | Example 2: 15 | 16 | Input: root = [] 17 | Output: [] 18 | Example 3: 19 | 20 | Input: root = [1] 21 | Output: [1] 22 | 23 | 24 | Constraints: 25 | 26 | The number of the nodes in the tree is in the range [0, 100]. 27 | -100 <= Node.val <= 100 28 | 29 | 30 | Follow up: Recursive solution is trivial, could you do it iteratively? 31 | -------------------------------------------------------------------------------- /1603. Design Parking System/1603. Design Parking System.java: -------------------------------------------------------------------------------- 1 | class ParkingSystem { 2 | int big,medium,small ; 3 | public ParkingSystem(int big, int medium, int small) { 4 | this.big = big ; 5 | this.medium = medium ; 6 | this.small = small ; 7 | } 8 | 9 | public boolean addCar(int carType) { 10 | if(carType == 1 && big ==0 || carType == 2 && medium ==0 || carType == 3 && small ==0 ) 11 | return false ; 12 | if(carType == 1) 13 | big--; 14 | if(carType == 2) 15 | medium--; 16 | if(carType == 3) 17 | small--; 18 | return true ; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /1838. Frequency of the Most Frequent Element/1838. Frequency of the Most Frequent Element.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxFrequency(int[] nums, int k) { 3 | Arrays.sort(nums); 4 | int i = 0; 5 | int j = 0; 6 | int sum = 0; 7 | int maxLength = 0; 8 | 9 | for (i = 0; i < nums.length; i++) { 10 | sum += nums[i]; 11 | 12 | while ((i - j + 1) * nums[i] - sum > k) { 13 | sum -= nums[j]; 14 | j++; 15 | } 16 | 17 | maxLength = Math.max(maxLength, i - j + 1); 18 | } 19 | 20 | return maxLength; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /452. Minimum Number of Arrows to Burst Balloons/Minimum Number of Arrows to Burst Balloons.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findMinArrowShots(int[][] points) { 3 | int arrows = 1 ; 4 | Arrays.sort(points,(a,b)-> Integer.compare(a[1],b[1])) ; 5 | int end = points[0][1] ; 6 | for(int i = 0;i end) 9 | { 10 | arrows++ ; 11 | end = points[i][1] ; 12 | } 13 | 14 | } 15 | return arrows; 16 | } 17 | } 18 | 19 | 20 | Time Complexity -> O(nlogn) 21 | Space Complexity -> O(1) 22 | -------------------------------------------------------------------------------- /785. Is Graph Bipartite?/785. Is Graph Bipartite?.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isBipartite(int[][] graph) { 3 | int color[] = new int[graph.length] ; 4 | for(int i = 0 ;i 2 | ------------------ 3 | 4 | 5 | 6 | Given the head of a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. 7 | 8 | You should preserve the original relative order of the nodes in each of the two partitions. 9 | 10 | 11 | 12 | Example 1: 13 | 14 | 15 | Input: head = [1,4,3,2,5,2], x = 3 16 | Output: [1,2,2,4,3,5] 17 | Example 2: 18 | 19 | Input: head = [2,1], x = 2 20 | Output: [1,2] 21 | 22 | 23 | Constraints: 24 | 25 | The number of nodes in the list is in the range [0, 200]. 26 | -100 <= Node.val <= 100 27 | -200 <= x <= 200 28 | -------------------------------------------------------------------------------- /206. Reverse Linked List/Readme.md: -------------------------------------------------------------------------------- 1 | 206. Reverse Linked List => 2 | ------------------------ 3 | 4 | 5 | Given the head of a singly linked list, reverse the list, and return the reversed list. 6 | 7 | 8 | 9 | Example 1: 10 | 11 | 12 | Input: head = [1,2,3,4,5] 13 | Output: [5,4,3,2,1] 14 | Example 2: 15 | 16 | 17 | Input: head = [1,2] 18 | Output: [2,1] 19 | Example 3: 20 | 21 | Input: head = [] 22 | Output: [] 23 | 24 | 25 | Constraints: 26 | 27 | The number of nodes in the list is the range [0, 5000]. 28 | -5000 <= Node.val <= 5000 29 | 30 | 31 | Follow up: A linked list can be reversed either iteratively or recursively. Could you implement both 32 | -------------------------------------------------------------------------------- /2444. Count Subarrays With Fixed Bounds/Count Subarrays With Fixed Bounds.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public long countSubarrays(int[] nums, int minK, int maxK) { 3 | long res = 0; 4 | int j = -1; 5 | int prevMinKIndex = -1; 6 | int prevMaxKIndex = -1; 7 | 8 | for (int i = 0; i < nums.length; ++i) { 9 | if (nums[i] < minK || nums[i] > maxK) 10 | j = i; 11 | if (nums[i] == minK) 12 | prevMinKIndex = i; 13 | if (nums[i] == maxK) 14 | prevMaxKIndex = i; 15 | 16 | res += Math.max(0, Math.min(prevMinKIndex, prevMaxKIndex) - j); 17 | } 18 | 19 | return res; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /92. Reverse Linked List II/Readme.md: -------------------------------------------------------------------------------- 1 | 92. Reverse Linked List II > 2 | -------------------------- 3 | 4 | 5 | Given the head of a singly linked list and two integers left and right where left <= right, reverse the nodes of the list from position left to position right, and return the reversed list. 6 | 7 | 8 | 9 | Example 1: 10 | 11 | 12 | Input: head = [1,2,3,4,5], left = 2, right = 4 13 | Output: [1,4,3,2,5] 14 | Example 2: 15 | 16 | Input: head = [5], left = 1, right = 1 17 | Output: [5] 18 | 19 | 20 | Constraints: 21 | 22 | The number of nodes in the list is n. 23 | 1 <= n <= 500 24 | -500 <= Node.val <= 500 25 | 1 <= left <= right <= n 26 | 27 | 28 | -------------------------------------------------------------------------------- /Check if a number is divisible by 8/Check if a number is divisible by 8.java: -------------------------------------------------------------------------------- 1 | 2 | class Solution{ 3 | int DivisibleByEight(String s){ 4 | 5 | if(s.length()<=3) 6 | { 7 | int no = Integer.parseInt(s) ; 8 | return (no%8)==0?1:-1; 9 | } 10 | 11 | String temp = "" ; 12 | 13 | int cnt = 0 ; 14 | for(int i= s.length()-1 ;i>=0;i--) 15 | { 16 | cnt++ ; 17 | temp = s.charAt(i)+temp ; 18 | if(cnt==3)break ; 19 | } 20 | int no = Integer.parseInt(temp); 21 | 22 | return (no%8)==0?1:-1 ; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Convert an array to reduced form/Convert an array to reduced form.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | void convert(int[] arr, int n) { 3 | 4 | Mapmap = new HashMap<>() ; 5 | int []ans = new int[n] ; 6 | for(int i =0;i 2 | -------------------------------- 3 | 4 | 5 | Given a circular sheet of radius, r. Find the total number of rectangles with integral length and width that can be cut from the sheet that can fit on the circle, one at a time. 6 | 7 | Examples : 8 | 9 | Input: r=1 10 | Output: 1 11 | Explanation: Only 1 rectangle of dimensions 1x1. 12 | Input: r=2 13 | Output: 8 14 | Explanation: The 8 possible rectangles are 15 | (1x1)(1x2)(1x3)(2x1)(2x2)(2x3)(3x1)(3x2). 16 | Expected Time Complexity: O(r2) 17 | Expected Auxillary Space: O(1) 18 | 19 | 20 | Constraints: 21 | 1<=r<=1000 22 | 23 | -------------------------------------------------------------------------------- /1046. Last Stone Weight/1046. Last Stone Weight.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int lastStoneWeight(int[] stones) { 3 | PriorityQueuepq = new PriorityQueue<>(Collections.reverseOrder()) ; 4 | for(int i :stones) 5 | pq.offer(i) ; 6 | 7 | while(pq.size()>1){ 8 | int x = pq.poll() ; 9 | int y = pq.poll(); 10 | if(x==y) 11 | continue ; 12 | else 13 | { 14 | int dif = x-y; 15 | pq.offer(dif); 16 | } 17 | } 18 | if(pq.size()==0) 19 | return 0; 20 | return pq.poll(); 21 | } 22 | } 23 | --------------------------------------------------------------------------------