├── Recursion ├── Easy │ ├── PartitionKequalSubsets2.java │ └── LongesUniversalPath.java └── Medium │ ├── KthGrammer.java │ └── FullBinaryTree.java ├── Divide and Conquer ├── Easy │ └── Questions ├── Hard │ └── ExpressionAddOperator.java └── Medium │ └── searchMatrix2.java ├── DP ├── Medium │ ├── CanIWin.class │ ├── EggDrop.class │ ├── canIWin.class │ ├── isSubseq.class │ ├── DeleteEarn.class │ ├── TargetSum.class │ ├── BoundaryPath.class │ ├── BuyingFruits.class │ ├── EggDrop$mover.class │ ├── GuessNumber2.class │ ├── IntegerBreak.class │ ├── Key2Keyboard.class │ ├── KnightDialer.class │ ├── OnesAndZeros.class │ ├── ArithmeticSlices.class │ ├── CombinationSum4.class │ ├── ContinuousSubSum.class │ ├── LargestPluSign.class │ ├── MinCostTickets.class │ ├── PredictTheWinner.class │ ├── SameConseqDiff.class │ ├── LargestSumAverages.class │ ├── LongestIncrSubSeq.class │ ├── WiggleSubsequence.class │ ├── sellStockCooldown.class │ ├── CountWithUniqueDigits.class │ ├── LongestArithmeticSeq.class │ ├── MinSwapsToIncreasing.class │ ├── CountLongestIncrSubSeq.class │ ├── LargestDivisibleSubset.class │ ├── LongestIncreasingSubSeq.class │ ├── PartitionEqualSubsetSum.class │ ├── BuySellStockTransactionFee.class │ ├── LongestPalindromicSubseq.class │ ├── MinSwapsToMakeIncreasing.class │ ├── sellStockCooldown.java │ ├── BuySellStockTransactionFee.java │ └── LongestIncrSubSeq.java ├── Hard │ └── LongestValidParenthesis.class └── Easy │ ├── SumQuery.java │ ├── UglyNumber.java │ └── CountBits.java ├── Arrays ├── Easy │ ├── FindAll.class │ ├── fibo.java │ ├── MissingNumber.java │ ├── Lgst_Cont_Inc_Sub.java │ ├── Sort_parity.java │ ├── MoveZeros.java │ ├── ContainsDuplicate.java │ ├── MaxOnes.java │ ├── bit_character.java │ ├── Sort_parity_2.java │ ├── RemoveDuplicates.java │ ├── Largest_number_twice.java │ ├── Mountain_Array.java │ ├── ContainsDuplicate2.java │ ├── RemoveElement.java │ ├── Transpose.java │ ├── Monotonic_Array.java │ ├── Non_decreasing_Array.java │ ├── Maximum_Subarray.java │ ├── SearchInsertPosition.java │ ├── FindAll.java │ ├── Rotate.java │ ├── TwoSum2.java │ ├── find_pivot.java │ ├── Fair_candy_swap.java │ ├── k_diff.java │ ├── TwoSum.java │ └── partition_1.java ├── Medium │ ├── GameOfLife.class │ ├── Mycalendar.class │ ├── TeemoAttacking.class │ ├── Mycalendar.java │ ├── PeakElement.java │ ├── DuplicateNumber.java │ ├── ProductLessThanK.java │ ├── TeemoAttacking.java │ ├── AllDuplicateArray.java │ ├── MaxBoundSubArray.java │ ├── SubarraySumK.java │ ├── ValidTriangle.java │ ├── MonotoneIncreasing.java │ ├── SortColors.java │ ├── RevealCards.java │ ├── MinInRotatedArray.java │ ├── MinimumIncrement.java │ ├── AppropriateAge.java │ ├── Conainted_Water.java │ ├── RemoveDuplicates2.java │ ├── MaxWidthRamp.java │ ├── Majority2.java │ └── Maxchunks.java └── Hard │ ├── MaxChunksToSorted2.java │ └── MinInRotatedSortedArray2.java ├── Bit Manipulation ├── Medium │ ├── subsets.class │ ├── SingleNumber2.java │ ├── subsets.java │ └── SlidingWindowMax.java └── Easy │ ├── isPower4 │ ├── NumberCompliment.java │ ├── isPower4.java │ ├── AlternateBits.java │ ├── PowerOfTwo.java │ ├── MissingNumber.java │ ├── HammingDistance.java │ ├── Number1bits.java │ ├── SumOfTwoIntegers.java │ ├── SingleNumber.java │ ├── ConvertNumToHex.java │ ├── ReverseBits.java │ ├── PrimeNumberOfSetBits.java │ ├── LetterCasePermutation.java │ └── FindTheDiff.java ├── Two Pointers ├── Medium │ ├── BoatsToSave.class │ ├── BoatsToSave.java │ ├── Multiplicity3Sum.java │ └── LongestMountain.java └── Easy │ ├── ImplementStrstr.java │ ├── PermutationInString.java │ └── LongPressedName.java ├── Tree ├── Medium │ ├── InsertIntoMax.java │ ├── CountNodes.java │ ├── MaximumBinaryTree2.java │ ├── LCA.java │ ├── MaxDiffBetweenNodes.java │ ├── PruneTree.java │ ├── CheckCompletness.java │ ├── ConstBSTPreorder.java │ ├── InsertIntoBST.java │ ├── SmallestStringForm.java │ ├── KthSmallest.java │ ├── ConstMaxBT.java │ ├── FlipBinaryTree.java │ ├── AddOneToRow.java │ ├── FindSubTreeSum.java │ ├── RecoverBT.java │ ├── SubTreeDeepestNode.java │ ├── BottomLeft.java │ └── DeleteNode.java ├── Easy │ ├── MaxDepth.java │ ├── MaxDepthGT.java │ ├── MinDepth.java │ ├── constructString.java │ ├── getMinDiff.java │ ├── LeftLeaves.java │ ├── UnivaluedTree.java │ ├── SeachBST.java │ ├── PreorderGT.java │ ├── LowestCommonAncestor.java │ ├── PostorderGT.java │ ├── SumTorootLeaf.java │ ├── BSTfromSortedArray.java │ ├── MinimumDistance.java │ ├── HasPath.java │ ├── RangeSumBst.java │ ├── SameTree.java │ ├── GreaterTree.java │ ├── RootToLeafPaths.java │ ├── isSubTree.java │ ├── RootToLeaf.java │ ├── IncreasingOrderSearch.java │ ├── LeafSimilar.java │ ├── TiltTree.java │ ├── LongestUniversalPath.java │ ├── cousinsTree.java │ ├── FlattenTree.java │ ├── PopulateNextRightPointer.java │ ├── InvertTree.java │ ├── FindSecondMin.java │ ├── LevelOrderGT.java │ └── AverageLevels.java └── Hard │ ├── MaximumPathSum.java │ ├── RecoverBT.java │ └── BinaryTreeCamera.java ├── HashMap ├── Easy │ ├── distributeCandies.java │ ├── MinimumIndexSum.java │ ├── findAllAnagrams.java │ ├── IntersectionOfTwoArrays2.java │ ├── SetMisMatch.java │ ├── Anagram.java │ ├── HappyNumber.java │ ├── UniqueCharacter.java │ ├── MyHashSet.java │ ├── SubarraySumDivbyK.java │ ├── LongestPalindrome.java │ ├── OccurenceAfterBigram.java │ ├── UncommonWords.java │ ├── nRepeatedElement.java │ ├── CommonCharacters.java │ ├── KeboardRow.java │ ├── islandParameter.java │ ├── IntersectionOfTwoArrays.java │ ├── numOfBoomerang.java │ ├── MyHashMap.java │ └── PowerfulInteger.java ├── Medium │ ├── LastStoneWt.java │ ├── SubarraySumDivbyK.java │ ├── ValidSudoku.java │ ├── FractionToRecurrDecimal.java │ └── TimeMap.java └── Hard │ ├── MaximalRectangle.java │ └── RandomPickWithBlackList.java ├── Greedy ├── Easy │ ├── AssignCookies.java │ ├── DeleteColsToMakeSorted.java │ ├── TwoCitySchedulling.java │ ├── K_Negotiations.java │ └── WalkingRobotSimulation.java └── Medium │ ├── RemoveKdigits.java │ ├── isSubsequence.java │ ├── NonOverLappingIntervals.java │ ├── GasStation.java │ ├── findMinArrowShots.java │ ├── TaskScheduler.java │ ├── SplitIntoConsecutiveSubseq.java │ └── reconstructQueue.java ├── Queue ├── Easy │ └── RecentCalls.java └── Hard │ └── SubarrayAtleastK.java ├── Stack ├── Hard │ ├── RecentCalls.java │ ├── FreqStack.java │ └── BasicCalculator.java ├── Medium │ ├── ValidateSequence.java │ ├── ScoreParenthesis.java │ ├── CarFleet.java │ ├── Nextgreater2.java │ ├── MinmumAddToMakeValid.java │ ├── VerifyPreorder.java │ ├── DailyTemperature.java │ ├── StockSpanner.java │ ├── DecodeAtIndex.java │ ├── ReversePolishNotation.java │ ├── AsteroidCollision.java │ ├── Kdigits.java │ ├── NextLargerNodes.java │ └── pattern132.java └── Easy │ ├── RemoveAdjacent.java │ ├── RemoveOuterParenthesis.java │ └── BaseBallGame.java ├── LinkedList ├── Easy │ ├── Intersection.java │ ├── Mid.java │ ├── CycleIndex.java │ ├── DeleteSpecificNode.java │ ├── RemoveDuplicates.java │ ├── RemoveElement.java │ └── MergeTwoSortedLists.java └── Medium │ ├── RemoveNthFromLast.java │ ├── fold.java │ ├── OddEvenList.java │ ├── SwapInPairs.java │ ├── Rotate.java │ ├── InsertionSort.java │ ├── ReverseLL2.java │ ├── DeleteDuplicate.java │ ├── partitionList.java │ └── ReorderList.java ├── Binary Search └── Easy │ └── GuessNumber.java ├── Heap ├── Medium │ ├── KClosestPoints.java │ ├── KFrequentElements.java │ ├── SortByFrequency.java │ ├── KpairsWithSmallestSum.java │ └── TopKfrequentWords.java └── Easy │ ├── KthLargest.java │ └── LastStoneWt.java ├── README.md ├── Backtracking ├── Medium │ ├── combine.java │ ├── WordDictionary.java │ └── RegularExpressionMatching.java ├── Hard │ ├── prmtSeq.java │ └── wordBreak2.java └── Easy │ └── BinaryWatch.java └── Extra └── wordBreak.java /Recursion/Easy/PartitionKequalSubsets2.java: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Divide and Conquer/Easy/Questions: -------------------------------------------------------------------------------- 1 | Easy Contain two Questions and both are covered in Arrays. -------------------------------------------------------------------------------- /DP/Medium/CanIWin.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frankenstein32/Leetcode-Solutions/HEAD/DP/Medium/CanIWin.class -------------------------------------------------------------------------------- /DP/Medium/EggDrop.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frankenstein32/Leetcode-Solutions/HEAD/DP/Medium/EggDrop.class -------------------------------------------------------------------------------- /DP/Medium/canIWin.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frankenstein32/Leetcode-Solutions/HEAD/DP/Medium/canIWin.class -------------------------------------------------------------------------------- /DP/Medium/isSubseq.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frankenstein32/Leetcode-Solutions/HEAD/DP/Medium/isSubseq.class -------------------------------------------------------------------------------- /Arrays/Easy/FindAll.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frankenstein32/Leetcode-Solutions/HEAD/Arrays/Easy/FindAll.class -------------------------------------------------------------------------------- /DP/Medium/DeleteEarn.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frankenstein32/Leetcode-Solutions/HEAD/DP/Medium/DeleteEarn.class -------------------------------------------------------------------------------- /DP/Medium/TargetSum.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frankenstein32/Leetcode-Solutions/HEAD/DP/Medium/TargetSum.class -------------------------------------------------------------------------------- /DP/Medium/BoundaryPath.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frankenstein32/Leetcode-Solutions/HEAD/DP/Medium/BoundaryPath.class -------------------------------------------------------------------------------- /DP/Medium/BuyingFruits.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frankenstein32/Leetcode-Solutions/HEAD/DP/Medium/BuyingFruits.class -------------------------------------------------------------------------------- /DP/Medium/EggDrop$mover.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frankenstein32/Leetcode-Solutions/HEAD/DP/Medium/EggDrop$mover.class -------------------------------------------------------------------------------- /DP/Medium/GuessNumber2.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frankenstein32/Leetcode-Solutions/HEAD/DP/Medium/GuessNumber2.class -------------------------------------------------------------------------------- /DP/Medium/IntegerBreak.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frankenstein32/Leetcode-Solutions/HEAD/DP/Medium/IntegerBreak.class -------------------------------------------------------------------------------- /DP/Medium/Key2Keyboard.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frankenstein32/Leetcode-Solutions/HEAD/DP/Medium/Key2Keyboard.class -------------------------------------------------------------------------------- /DP/Medium/KnightDialer.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frankenstein32/Leetcode-Solutions/HEAD/DP/Medium/KnightDialer.class -------------------------------------------------------------------------------- /DP/Medium/OnesAndZeros.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frankenstein32/Leetcode-Solutions/HEAD/DP/Medium/OnesAndZeros.class -------------------------------------------------------------------------------- /Arrays/Medium/GameOfLife.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frankenstein32/Leetcode-Solutions/HEAD/Arrays/Medium/GameOfLife.class -------------------------------------------------------------------------------- /Arrays/Medium/Mycalendar.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frankenstein32/Leetcode-Solutions/HEAD/Arrays/Medium/Mycalendar.class -------------------------------------------------------------------------------- /DP/Medium/ArithmeticSlices.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frankenstein32/Leetcode-Solutions/HEAD/DP/Medium/ArithmeticSlices.class -------------------------------------------------------------------------------- /DP/Medium/CombinationSum4.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frankenstein32/Leetcode-Solutions/HEAD/DP/Medium/CombinationSum4.class -------------------------------------------------------------------------------- /DP/Medium/ContinuousSubSum.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frankenstein32/Leetcode-Solutions/HEAD/DP/Medium/ContinuousSubSum.class -------------------------------------------------------------------------------- /DP/Medium/LargestPluSign.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frankenstein32/Leetcode-Solutions/HEAD/DP/Medium/LargestPluSign.class -------------------------------------------------------------------------------- /DP/Medium/MinCostTickets.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frankenstein32/Leetcode-Solutions/HEAD/DP/Medium/MinCostTickets.class -------------------------------------------------------------------------------- /DP/Medium/PredictTheWinner.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frankenstein32/Leetcode-Solutions/HEAD/DP/Medium/PredictTheWinner.class -------------------------------------------------------------------------------- /DP/Medium/SameConseqDiff.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frankenstein32/Leetcode-Solutions/HEAD/DP/Medium/SameConseqDiff.class -------------------------------------------------------------------------------- /Arrays/Medium/TeemoAttacking.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frankenstein32/Leetcode-Solutions/HEAD/Arrays/Medium/TeemoAttacking.class -------------------------------------------------------------------------------- /DP/Medium/LargestSumAverages.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frankenstein32/Leetcode-Solutions/HEAD/DP/Medium/LargestSumAverages.class -------------------------------------------------------------------------------- /DP/Medium/LongestIncrSubSeq.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frankenstein32/Leetcode-Solutions/HEAD/DP/Medium/LongestIncrSubSeq.class -------------------------------------------------------------------------------- /DP/Medium/WiggleSubsequence.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frankenstein32/Leetcode-Solutions/HEAD/DP/Medium/WiggleSubsequence.class -------------------------------------------------------------------------------- /DP/Medium/sellStockCooldown.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frankenstein32/Leetcode-Solutions/HEAD/DP/Medium/sellStockCooldown.class -------------------------------------------------------------------------------- /Bit Manipulation/Medium/subsets.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frankenstein32/Leetcode-Solutions/HEAD/Bit Manipulation/Medium/subsets.class -------------------------------------------------------------------------------- /DP/Hard/LongestValidParenthesis.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frankenstein32/Leetcode-Solutions/HEAD/DP/Hard/LongestValidParenthesis.class -------------------------------------------------------------------------------- /DP/Medium/CountWithUniqueDigits.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frankenstein32/Leetcode-Solutions/HEAD/DP/Medium/CountWithUniqueDigits.class -------------------------------------------------------------------------------- /DP/Medium/LongestArithmeticSeq.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frankenstein32/Leetcode-Solutions/HEAD/DP/Medium/LongestArithmeticSeq.class -------------------------------------------------------------------------------- /DP/Medium/MinSwapsToIncreasing.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frankenstein32/Leetcode-Solutions/HEAD/DP/Medium/MinSwapsToIncreasing.class -------------------------------------------------------------------------------- /Two Pointers/Medium/BoatsToSave.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frankenstein32/Leetcode-Solutions/HEAD/Two Pointers/Medium/BoatsToSave.class -------------------------------------------------------------------------------- /DP/Medium/CountLongestIncrSubSeq.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frankenstein32/Leetcode-Solutions/HEAD/DP/Medium/CountLongestIncrSubSeq.class -------------------------------------------------------------------------------- /DP/Medium/LargestDivisibleSubset.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frankenstein32/Leetcode-Solutions/HEAD/DP/Medium/LargestDivisibleSubset.class -------------------------------------------------------------------------------- /DP/Medium/LongestIncreasingSubSeq.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frankenstein32/Leetcode-Solutions/HEAD/DP/Medium/LongestIncreasingSubSeq.class -------------------------------------------------------------------------------- /DP/Medium/PartitionEqualSubsetSum.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frankenstein32/Leetcode-Solutions/HEAD/DP/Medium/PartitionEqualSubsetSum.class -------------------------------------------------------------------------------- /DP/Medium/BuySellStockTransactionFee.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frankenstein32/Leetcode-Solutions/HEAD/DP/Medium/BuySellStockTransactionFee.class -------------------------------------------------------------------------------- /DP/Medium/LongestPalindromicSubseq.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frankenstein32/Leetcode-Solutions/HEAD/DP/Medium/LongestPalindromicSubseq.class -------------------------------------------------------------------------------- /DP/Medium/MinSwapsToMakeIncreasing.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frankenstein32/Leetcode-Solutions/HEAD/DP/Medium/MinSwapsToMakeIncreasing.class -------------------------------------------------------------------------------- /Bit Manipulation/Easy/isPower4: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class isPower4 { 4 | 5 | public boolean isPowerOf4(int num){ 6 | 7 | return (num & (num - 1)) == 0 && num > 0 && (num - 1) % 3 == 0; 8 | } 9 | } 10 | 11 | -------------------------------------------------------------------------------- /Bit Manipulation/Easy/NumberCompliment.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class NumberCompliment { 4 | 5 | public int findComplement(int num){ 6 | 7 | return ~num & (Integer.highestOneBit(num) - 1); 8 | } 9 | } 10 | 11 | -------------------------------------------------------------------------------- /Bit Manipulation/Easy/isPower4.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class isPower4 { 4 | 5 | public boolean isPowerOf4(int num){ 6 | 7 | return (num & (num - 1)) == 0 && num > 0 && (num - 1) % 3 == 0; 8 | } 9 | } 10 | 11 | -------------------------------------------------------------------------------- /Bit Manipulation/Easy/AlternateBits.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class AlternateBits { 4 | 5 | public boolean hasAlternateBits(int n){ 6 | 7 | n = n ^ n >> 1; 8 | 9 | return (n & n + 1) == 0; 10 | } 11 | } 12 | 13 | -------------------------------------------------------------------------------- /Bit Manipulation/Easy/PowerOfTwo.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class PowerOfTwo { 4 | 5 | public boolean isPowerOfTwo(int n){ 6 | 7 | if(n <= 0){ 8 | return false; 9 | } 10 | 11 | return ((n & (n - 1)) == 0? true :false); 12 | } 13 | } 14 | 15 | -------------------------------------------------------------------------------- /Tree/Medium/InsertIntoMax.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class MaximumBinaryTree2{ 4 | 5 | class Node{ 6 | 7 | int val; 8 | Node right; 9 | Node left; 10 | Node next; 11 | 12 | public Node(int x){ val = x; } 13 | } 14 | 15 | public TreeNode insertIntoMax() 16 | } -------------------------------------------------------------------------------- /Bit Manipulation/Easy/MissingNumber.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class MissingNumber { 4 | 5 | public int missingNumber(int[] nums){ 6 | 7 | int missing = nums.length; 8 | for(int i = 0;i < nums.length;i++){ 9 | 10 | missing = missing ^ i ^ nums[i]; 11 | } 12 | 13 | return missing; 14 | 15 | } 16 | } 17 | 18 | -------------------------------------------------------------------------------- /DP/Medium/sellStockCooldown.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class sellStockCooldown{ 4 | 5 | public static void main(String[] args){ 6 | 7 | int[] arr = {1,2,3,0,2}; 8 | System.out.println(maxProfit(arr)); 9 | 10 | } 11 | 12 | public static int maxProfit(int[] arr){ 13 | 14 | 15 | } 16 | 17 | 18 | 19 | 20 | } -------------------------------------------------------------------------------- /Bit Manipulation/Easy/HammingDistance.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class HammingDistance { 4 | 5 | public int hammingDistance(int x, int y){ 6 | 7 | int xor = x ^ y; 8 | 9 | int count = 0; 10 | 11 | while(xor != 0){ 12 | 13 | count += (xor & 1); 14 | xor = xor >> 1; 15 | } 16 | 17 | return count; 18 | } 19 | } 20 | 21 | -------------------------------------------------------------------------------- /Bit Manipulation/Easy/Number1bits.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Number1bits { 4 | 5 | public int hammingWeight(int n){ 6 | 7 | if(n == 0) return 0; 8 | 9 | int cnt = 0; 10 | while(n != 0){ 11 | 12 | cnt = cnt + (n & 1); 13 | 14 | n >>= 1; 15 | 16 | } 17 | 18 | return cnt; 19 | } 20 | } 21 | 22 | -------------------------------------------------------------------------------- /HashMap/Easy/distributeCandies.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class distributeCandies{ 4 | 5 | public int distributeCandy(int[] candies){ 6 | 7 | Set s = new HashSet<>(); 8 | 9 | for(int candy : candies){ 10 | 11 | s.add(candy); 12 | } 13 | 14 | return Math.min(s.size(), candies.length/2); 15 | 16 | } 17 | } 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /Bit Manipulation/Easy/SumOfTwoIntegers.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class SumOfTwoIntegers { 4 | 5 | public int getSum(int a, int b){ 6 | 7 | 8 | if(a == 0) return b; 9 | if(b == 0) return a; 10 | 11 | int carry ; 12 | while(b != 0){ 13 | 14 | int c = (a & b); 15 | a = a ^ b; 16 | b = carry << 1; 17 | } 18 | 19 | return a; 20 | } 21 | } 22 | 23 | -------------------------------------------------------------------------------- /Bit Manipulation/Medium/SingleNumber2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class SingleNumber2 { 4 | 5 | public static void main(String[] args){ 6 | 7 | int[] arr = {1,2,3}; 8 | System.out.println(subsets(arr)); 9 | } 10 | 11 | public static int singleNumber(int[] nums){ 12 | 13 | int missing = 0; 14 | 15 | for(int i = 0;i < nums.length ) 16 | } 17 | } 18 | 19 | -------------------------------------------------------------------------------- /Divide and Conquer/Hard/ExpressionAddOperator.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class ExpressionAddOperator{ 4 | 5 | 6 | public static void main(String[] args){ 7 | 8 | 9 | String str = "123"; 10 | int target = 6; 11 | } 12 | 13 | public static List addOperator(String num, int target){ 14 | 15 | 16 | } 17 | 18 | public static int addOperator(String) 19 | 20 | 21 | } -------------------------------------------------------------------------------- /Tree/Medium/CountNodes.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class CountNodes{ 4 | 5 | class Node{ 6 | 7 | int val; 8 | Node right; 9 | Node left; 10 | Node next; 11 | 12 | public Node(int x){ val = x; } 13 | } 14 | 15 | public int count(Treenode root){ 16 | 17 | if(root == null){ 18 | return 0; 19 | } 20 | 21 | return count(root.left) + count(root.right) + 1; 22 | } 23 | 24 | } -------------------------------------------------------------------------------- /Tree/Easy/MaxDepth.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class MaxDepth{ 4 | 5 | class TreeNode{ 6 | 7 | int val; 8 | TreeNode right; 9 | TreeNode left; 10 | 11 | public TreeNode(int x){ val = x; } 12 | } 13 | 14 | public int height(TreeNode root){ 15 | 16 | if(root == null){ 17 | return 0; 18 | } 19 | 20 | return Math.max(height(root.left), height(root.right)) + 1; 21 | } 22 | 23 | 24 | } -------------------------------------------------------------------------------- /Bit Manipulation/Easy/SingleNumber.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class SingleNumber { 4 | 5 | public int singleNumber(int[] nums){ 6 | 7 | 8 | if(nums == null || nums.length <= 2){ 9 | return 0; 10 | } 11 | 12 | int val = nums[0]; 13 | 14 | for(int i = 1;i < nums.length;i++){ 15 | 16 | val = val ^ nums[i]; 17 | } 18 | 19 | return val; 20 | } 21 | } 22 | 23 | -------------------------------------------------------------------------------- /Greedy/Easy/AssignCookies.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class AssignCookies{ 4 | 5 | public int findContentChildren(int[] g, int[] s){ 6 | 7 | 8 | Arrays.sort(g); 9 | Arrays.sort(s); 10 | 11 | int i = 0, j = 0; 12 | int res = 0; 13 | while(i < g.length && j < s.length){ 14 | 15 | if(g[i] <= s[j]){ 16 | i++; 17 | res++; 18 | }else{ 19 | j++; 20 | } 21 | } 22 | 23 | return res; 24 | } 25 | } 26 | 27 | -------------------------------------------------------------------------------- /Bit Manipulation/Easy/ConvertNumToHex.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class ConvertNumToHex { 4 | 5 | public String toHex(int num){ 6 | 7 | if(num == 0){ 8 | return "0"; 9 | } 10 | 11 | char[] map = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'}; 12 | String str = ""; 13 | 14 | while(num != 0){ 15 | 16 | str += map[num & 15]; 17 | num = (num >>> 4); 18 | } 19 | 20 | return str; 21 | } 22 | } 23 | 24 | -------------------------------------------------------------------------------- /Bit Manipulation/Easy/ReverseBits.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class ReverseBits { 4 | 5 | public int reverseBits(int n){ 6 | 7 | if(n == 0) return 0; 8 | 9 | 10 | int result = 0; 11 | 12 | for(int i = 0;i < 32;i++){ 13 | 14 | result <<= 1; 15 | 16 | if(n & 1 == 1) result++; 17 | 18 | n >>= 1; 19 | } 20 | 21 | return result; 22 | } 23 | } 24 | 25 | -------------------------------------------------------------------------------- /Greedy/Easy/DeleteColsToMakeSorted.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class DeleteColsToMakeSorted{ 4 | 5 | public int minDeletionSize(String[] A){ 6 | 7 | int res = 0; 8 | 9 | for(int col = 0;col < A[0].length();col++){ 10 | for(int row = 0;row < A.length;j++){ 11 | if(A[row].get(col) > A[row].get(col + 1)){ 12 | res++; 13 | } 14 | } 15 | } 16 | 17 | 18 | return res; 19 | 20 | 21 | } 22 | } 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /HashMap/Easy/MinimumIndexSum.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class MinimumIndexSum{ 4 | 5 | public String[] findRestaurant(String[] list1, String[] list2){ 6 | 7 | HashMap map = new HashMap<>(); 8 | 9 | for(int i = 0;i < list1.length;i++){ 10 | 11 | map.put(list1[i], i); 12 | } 13 | 14 | int minIdx = list1.length() - 1; 15 | for (int i = 0; i < list2.length ;i++) { 16 | 17 | 18 | } 19 | } 20 | } 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /Queue/Easy/RecentCalls.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class RecentCalls{ 4 | 5 | 6 | Queue q; 7 | 8 | public RecentCalls(){ 9 | 10 | q = new LinkedList<>(); 11 | } 12 | 13 | public static int ping(int t){ 14 | 15 | queue.add(t); 16 | 17 | while(q.size() > 0){ 18 | 19 | int first = q.peek(); 20 | 21 | if(3000 < t - first){ 22 | q.poll(); 23 | }else{ 24 | break; 25 | } 26 | } 27 | 28 | return queue.size(); 29 | } 30 | } -------------------------------------------------------------------------------- /Stack/Hard/RecentCalls.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class RecentCalls{ 4 | 5 | 6 | Queue q; 7 | 8 | public RecentCalls(){ 9 | 10 | q = new LinkedList<>(); 11 | } 12 | 13 | public static int ping(int t){ 14 | 15 | queue.add(t); 16 | 17 | while(q.size() > 0){ 18 | 19 | int first = q.peek(); 20 | 21 | if(3000 < t - first){ 22 | q.poll(); 23 | }else{ 24 | break; 25 | } 26 | } 27 | 28 | return queue.size(); 29 | } 30 | } -------------------------------------------------------------------------------- /Tree/Easy/MaxDepthGT.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class MaxDepthGT{ 4 | 5 | class Node{ 6 | 7 | int val; 8 | List _children; 9 | 10 | public Node(){} 11 | public Node(int x){ this.val = x;} 12 | } 13 | 14 | public int MaxDepth(TreeNode root){ 15 | 16 | if(root == null){ 17 | return 0; 18 | } 19 | 20 | int max = 0; 21 | for(Node child : root.children){ 22 | 23 | max = Math.max(max, MaxDepth(child)); 24 | } 25 | 26 | return max + 1; 27 | } 28 | 29 | 30 | } -------------------------------------------------------------------------------- /HashMap/Easy/findAllAnagrams.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class findAllAnagrams{ 4 | 5 | public int longestPalindrome(String s){ 6 | 7 | HashSet hs = new HashSet<>(); 8 | 9 | int count = 0; 10 | for(char ch : s.toCharArray()){ 11 | 12 | if(hs.contains(ch)){ 13 | hs.remove(ch); 14 | count++; 15 | }else{ 16 | hs.add(ch); 17 | } 18 | } 19 | 20 | return !hs.isEmpty() ? 2 * count : 2 * count + 1; 21 | 22 | } 23 | } 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /Bit Manipulation/Easy/PrimeNumberOfSetBits.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class PrimeNumberOfSetBits { 4 | 5 | public int countPrimeSetBits(int l, int r){ 6 | 7 | Set primes = new HashSet<>(Arrays.asList( 2, 3, 5, 7, 11, 13, 17, 19)); 8 | int count = 0; 9 | for(int i = l;i <= r;i++){ 10 | int bits = 0; 11 | for(int n = i; n != 0; n >> 1){ 12 | bits += n & 1; 13 | } 14 | 15 | if(primes.contains(bits)){ 16 | count++; 17 | } 18 | } 19 | 20 | return count; 21 | } 22 | } 23 | 24 | -------------------------------------------------------------------------------- /DP/Easy/SumQuery.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class SumQuery{ 4 | 5 | int[] strg; 6 | 7 | public SumQuery(int[] nums) { 8 | 9 | this.strg = new int[nums.length + 1]; 10 | 11 | for(int i = 0;i < nums.length;i++){ 12 | 13 | this.strg[i + 1] = nums[i] + this.strg[i]; 14 | } 15 | 16 | } 17 | 18 | public int sumRange(int i, int j) { 19 | 20 | return this.strg[j + 1] - this.strg[i]; 21 | 22 | } 23 | } -------------------------------------------------------------------------------- /Greedy/Medium/RemoveKdigits.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class RemoveKdigits{ 4 | 5 | public boolean isSubsequence(String s, String t) { 6 | if (s.length() == 0) return true; 7 | int indexS = 0, indexT = 0; 8 | while (indexT < t.length()) { 9 | if (t.charAt(indexT) == s.charAt(indexS)) { 10 | indexS++; 11 | if (indexS == s.length()) return true; 12 | } 13 | indexT++; 14 | } 15 | return false; 16 | } 17 | } 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /Greedy/Medium/isSubsequence.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class isSubsequence{ 4 | 5 | public boolean isSubsequence(String s, String t) { 6 | if (s.length() == 0) return true; 7 | int indexS = 0, indexT = 0; 8 | while (indexT < t.length()) { 9 | if (t.charAt(indexT) == s.charAt(indexS)) { 10 | indexS++; 11 | if (indexS == s.length()) return true; 12 | } 13 | indexT++; 14 | } 15 | return false; 16 | } 17 | } 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /Tree/Medium/MaximumBinaryTree2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class MaximumBinaryTree2{ 4 | 5 | class Node{ 6 | 7 | int val; 8 | Node right; 9 | Node left; 10 | Node next; 11 | 12 | public Node(int x){ val = x; } 13 | } 14 | 15 | public TreeNode insertIntoMax(TreeNode root, int val){ 16 | 17 | if(root != null && root.val > val){ 18 | 19 | root.right = insertIntoMax(root.right, val); 20 | return root.right; 21 | } 22 | 23 | TreeNode nn = new TreeNode(val); 24 | nn.left = root; 25 | return nn; 26 | } 27 | } -------------------------------------------------------------------------------- /HashMap/Easy/IntersectionOfTwoArrays2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class IntersectionOfTwoArrays2{ 4 | 5 | public int firstUniqchar(String s){ 6 | 7 | 8 | HashMap map = new HashMap<>(); 9 | 10 | for(int i = 0;i < s.length();i++){ 11 | map.put(s.charAt(i), map.getOrDefault(s.charAt(i), 0) + 1); 12 | } 13 | 14 | for(int i = 0;i < s.length();i++){ 15 | 16 | if(map.get(s.charAt(i)) == 1){ 17 | return i; 18 | } 19 | } 20 | 21 | return -1; 22 | } 23 | } 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /LinkedList/Easy/Intersection.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Intersection{ 4 | 5 | public static void main(String[] args){ 6 | 7 | 8 | } 9 | 10 | static class ListNode{ 11 | 12 | int val; 13 | ListNode Next; 14 | 15 | public ListNode(int x){ this.val = x; } 16 | } 17 | 18 | public static ListNode getInter(ListNode A, ListNode B, int m, int n){ 19 | 20 | ListNode a = A; 21 | ListNode b = B; 22 | 23 | while(a != b){ 24 | 25 | a = (a == null ? B : a.next); 26 | b = (b == null ? A : b.next); 27 | } 28 | 29 | return a; 30 | 31 | } 32 | 33 | } -------------------------------------------------------------------------------- /Tree/Easy/MinDepth.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class MinDepth{ 4 | 5 | class TreeNode{ 6 | 7 | int val; 8 | TreeNode right; 9 | TreeNode left; 10 | 11 | public TreeNode(int x){ val = x; } 12 | } 13 | 14 | public int height(TreeNode root){ 15 | 16 | if(root == null){ 17 | return Integer.MAX_VALUE; 18 | } 19 | 20 | int lh = height(root.left); 21 | int rh = height(root.right); 22 | 23 | if(lh == Integer.MAX_VALUE && rh == Integer.MAX_VALUE){ 24 | 25 | return 1; 26 | }else { 27 | 28 | return Math.min(lh, rh) + 1; 29 | } 30 | } 31 | 32 | 33 | } -------------------------------------------------------------------------------- /Tree/Easy/constructString.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class constructString{ 4 | 5 | class TreeNode{ 6 | 7 | int val; 8 | TreeNode right; 9 | TreeNode left; 10 | 11 | public TreeNode(int x){ val = x; } 12 | } 13 | 14 | public String tree2str(TreeNode t){ 15 | 16 | if(t == null) 17 | return ""; 18 | 19 | if(t.left == null && t.right == null){ 20 | 21 | return t.val + ""; 22 | } 23 | 24 | if(t.right == null){ 25 | return "("+tree2str(t.left)+")"; 26 | } 27 | 28 | return "("+tree2str(t.left)+")"+"("+tree2str(t.right)+")"; 29 | 30 | } 31 | 32 | } -------------------------------------------------------------------------------- /Tree/Easy/getMinDiff.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class getMinDiff{ 4 | 5 | class TreeNode{ 6 | 7 | int val; 8 | TreeNode right; 9 | TreeNode left; 10 | 11 | public TreeNode(int x){ val = x; } 12 | } 13 | 14 | TreeNode prev = null; 15 | int minDiff = Integer.MAX_VALUE; 16 | 17 | public void getDiff(TreNode root){ 18 | 19 | if(root == null){ 20 | return; 21 | } 22 | 23 | getDiff(root.left); 24 | 25 | if(prev != null){ 26 | minDiff = Math.min(minDiff, root.val - prev); 27 | } 28 | 29 | prev = root; 30 | getDiff(root.right); 31 | } 32 | 33 | 34 | } -------------------------------------------------------------------------------- /Binary Search/Easy/GuessNumber.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class GuessNumber{ 4 | 5 | public static void main(String[] args){ 6 | 7 | // No Main 8 | 9 | } 10 | 11 | public static int find(int n){ 12 | 13 | 14 | int left = 0; 15 | int right = n; 16 | 17 | 18 | while(left <= right){ 19 | 20 | 21 | int mid = left + (right - left) / 2; 22 | int val = guess(mid); 23 | 24 | if(val == 0){ 25 | return mid; 26 | }else if(val == -1){ 27 | right = mid - 1; 28 | }else{ 29 | left = mid + 1; 30 | } 31 | } 32 | } 33 | 34 | 35 | 36 | 37 | 38 | 39 | } -------------------------------------------------------------------------------- /Greedy/Easy/TwoCitySchedulling.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class TwoCitySchedulling{ 4 | 5 | 6 | public int twoCitySchedCost(int[][] costs){ 7 | 8 | Arrays.sort(costs, new Comparator(){ 9 | 10 | @Override 11 | public int compare(final int[] entry1, final int[] entry2){ 12 | return (entry1[1] - entry1[0]) - (entry2[1] - entry2[0]); 13 | } 14 | }); 15 | 16 | int sum = 0; 17 | int n = costs.length; 18 | for(int i = 0;i < costs.length;i++){ 19 | 20 | sum += costs[i][1] + costs[n - i - 1][0]; 21 | } 22 | 23 | return sum; 24 | } 25 | 26 | } 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /HashMap/Easy/SetMisMatch.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class SetMisMatch{ 4 | 5 | public int[] findErrorNums(int[] nums){ 6 | 7 | int[] res = new int[2]; 8 | for(int i = 0;i < nums.length;i++){ 9 | 10 | int val = nums[i] - 1; 11 | 12 | if(nums[val] > 0){ 13 | nums[val] = -nums[val]; 14 | }else{ 15 | res[0] = val; 16 | } 17 | } 18 | 19 | for(int i = 0;i < nums.length;i++){ 20 | 21 | if(nums[i] > 0){ 22 | res[1] = i + 1; 23 | return res; 24 | } 25 | } 26 | 27 | return res; 28 | } 29 | } 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /Stack/Medium/ValidateSequence.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class ValidateSequence{ 4 | 5 | 6 | public static void main(String[] args){ 7 | 8 | String preorder = ""; 9 | 10 | 11 | } 12 | 13 | public static boolean validate(int[] pushed, int[] popped){ 14 | 15 | Stack stack = new Stack<>(); 16 | int j = 0; 17 | for(int i = 0;i < pushed.length;i++){ 18 | 19 | stack.push(pushed[i]); 20 | 21 | while(!stack.isEmpty() && stack.peek() == popped[j]){ 22 | stack.pop(); 23 | j++; 24 | } 25 | } 26 | 27 | return stack.size() == 0; 28 | 29 | 30 | 31 | } 32 | } -------------------------------------------------------------------------------- /Greedy/Easy/K_Negotiations.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class K_Negotiations{ 4 | 5 | 6 | public int largestSumAfterKNegotiations(int[] A, int k){ 7 | 8 | Arrays.sort(A); 9 | int minPos = Integer.MAX_VALUE; 10 | int sum = 0; 11 | int cnt = 0; 12 | 13 | for(int num : A){ 14 | 15 | if(num < 0 && ++cnt <= k){ 16 | 17 | num = -num; 18 | } 19 | 20 | sum += num; 21 | minPos = Math.min(minPos, num); 22 | } 23 | 24 | if(cnt > k || k - cnt % 2 == 0){ 25 | 26 | return sum; 27 | }else{ 28 | 29 | return sum - minPos * 2; 30 | } 31 | 32 | 33 | } 34 | } 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /Heap/Medium/KClosestPoints.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class KClosestPoints{ 4 | 5 | 6 | public int[][] kClosest(int[][] points, int K){ 7 | 8 | 9 | PriorityQueue pq = new PriorityQueue<>((a,b) -> dist(a) - dist(b)); 10 | 11 | for(int[] arr : points){ 12 | 13 | pq.offer(arr); 14 | } 15 | 16 | int[][] ans = new int[K][2]; 17 | 18 | for(int i = 0;i <= K;i++){ 19 | 20 | ans[i] = pq.poll(); 21 | } 22 | 23 | return ans; 24 | } 25 | 26 | public int dist(int[] points){ 27 | 28 | return points[0] * points[0] + points[1] * points[1]; 29 | } 30 | } -------------------------------------------------------------------------------- /Stack/Medium/ScoreParenthesis.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class ScoreParenthesis{ 4 | 5 | 6 | public static void main(String[] args){ 7 | 8 | String preorder = ""; 9 | 10 | 11 | } 12 | 13 | public static int score(String s){ 14 | 15 | 16 | Stack stack = new Stack<>(); 17 | stack.push(0); 18 | 19 | for(Character c : s.toCharArray()){ 20 | 21 | if(c == '('){ 22 | stack.push(0); 23 | }else{ 24 | 25 | int v = stack.pop(); 26 | int w = stack.pop(); 27 | 28 | w = w + Math.max(2 * v, 1); 29 | stack.push(w); 30 | } 31 | } 32 | 33 | return stack.pop(); 34 | } 35 | } -------------------------------------------------------------------------------- /HashMap/Easy/Anagram.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Anagram{ 4 | 5 | public boolean isHappy(int n){ 6 | 7 | Set inloop = new HashSet<>(); 8 | 9 | while(inloop.add(n)){ 10 | 11 | int squareSum = 0; 12 | int temp = n; 13 | while(temp != 0){ 14 | 15 | int rem = temp % 10; 16 | squareSum += rem * rem; 17 | temp = temp / 10; 18 | } 19 | 20 | if(squareSum == 1){ 21 | return true; 22 | }else{ 23 | n = squareSum; 24 | } 25 | } 26 | 27 | return false; 28 | } 29 | } 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /LinkedList/Easy/Mid.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Mid{ 4 | 5 | public static void main(String[] args){ 6 | 7 | 8 | } 9 | 10 | static class ListNode{ 11 | 12 | int val; 13 | ListNode Next; 14 | 15 | public ListNode(int x){ this.val = x; } 16 | } 17 | 18 | public static int mid(ListNode head){ 19 | 20 | if(head.next == null){ 21 | return head.val; 22 | } 23 | 24 | ListNode slow = head; 25 | ListNode fast = head.next; 26 | 27 | while(fast != null && fast.next != null){ 28 | 29 | slow = slow.next; 30 | fast = fast.next.next; 31 | } 32 | 33 | return slow; 34 | } 35 | 36 | 37 | } -------------------------------------------------------------------------------- /Tree/Easy/LeftLeaves.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class LeftLeaves{ 4 | 5 | class TreeNode{ 6 | 7 | int val; 8 | TreeNode right; 9 | TreeNode left; 10 | 11 | public TreeNode(int x){ val = x; } 12 | } 13 | 14 | public int SumLeaves(TreeNode root){ 15 | 16 | if(root == null){ 17 | return 0; 18 | } 19 | 20 | int ans = 0; 21 | 22 | if(root.left != null){ 23 | 24 | if(root.left.left == null && root.left.right == null) 25 | ans += root.left.val; 26 | else 27 | ans += SumLeaves(root.left); 28 | } 29 | 30 | return SumLeaves(root.right) + ans ; 31 | 32 | 33 | } 34 | 35 | 36 | } -------------------------------------------------------------------------------- /Tree/Medium/LCA.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class LCA{ 4 | 5 | class TreeNode{ 6 | 7 | int val; 8 | TreeNode right; 9 | TreeNode left; 10 | 11 | public TreeNode(int x){ val = x; } 12 | } 13 | 14 | public TreeNode LCA(TreeNode root, TreeNode p, TreeNode q){ 15 | 16 | if(root == null){ 17 | return null; 18 | } 19 | 20 | int n1 = p.val; 21 | int n2 = q.val; 22 | 23 | if(root.val > n1 && root.val > n2){ 24 | 25 | return LCA(root.left, p, q); 26 | }else if(root.val < n1 && root.val < n2){ 27 | 28 | return LCA(root.right, p , q); 29 | } 30 | 31 | 32 | return root; 33 | } 34 | 35 | 36 | } -------------------------------------------------------------------------------- /Tree/Medium/MaxDiffBetweenNodes.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class MaxDiffBetweenNodes{ 4 | 5 | class Node{ 6 | 7 | int val; 8 | Node right; 9 | Node left; 10 | Node next; 11 | 12 | public Node(int x){ val = x; } 13 | } 14 | 15 | public int maxAncestor(TreeNode node){ 16 | 17 | return dfs(root, 0, 0); 18 | } 19 | 20 | public int dfs(TreeNode node, int max, int min){ 21 | 22 | if(root == null){ 23 | return max - min; 24 | } 25 | 26 | max = Math.max(node.val, max); 27 | min = Math.min(node.val, min); 28 | 29 | return Math.max(dfs(node.left, max, min), dfs(node.right, max, min)); 30 | } 31 | } -------------------------------------------------------------------------------- /Greedy/Medium/NonOverLappingIntervals.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class NonOverLappingIntervals{ 4 | 5 | public int[][] eraseOverlapIntervals(int[][] intervals){ 6 | 7 | 8 | Arrays.sort(intervals, new Comparator(){ 9 | 10 | @Override 11 | public int compare(int[] a, int[] b){ 12 | 13 | return a[0] != b[0] ? a[0] - b[0] : b[1] - a[1]; 14 | } 15 | }); 16 | 17 | for(int[] arr : intervals){ 18 | 19 | for(int val : arr){ 20 | System.out.print(val+" "); 21 | } 22 | System.out.println(); 23 | } 24 | 25 | 26 | } 27 | } 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /HashMap/Easy/HappyNumber.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class HappyNumber{ 4 | 5 | public boolean isHappy(int n){ 6 | 7 | Set inloop = new HashSet<>(); 8 | 9 | while(inloop.add(n)){ 10 | 11 | int squareSum = 0; 12 | int temp = n; 13 | while(temp != 0){ 14 | 15 | int rem = temp % 10; 16 | squareSum += rem * rem; 17 | temp = temp / 10; 18 | } 19 | 20 | if(squareSum == 1){ 21 | return true; 22 | }else{ 23 | n = squareSum; 24 | } 25 | } 26 | 27 | return false; 28 | } 29 | } 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /Tree/Easy/UnivaluedTree.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class UnivaluedTree{ 4 | 5 | class TreeNode{ 6 | 7 | int val; 8 | TreeNode right; 9 | TreeNode left; 10 | 11 | public TreeNode(int x){ val = x; } 12 | } 13 | 14 | public boolean isUnivariant(TreeNode root){ 15 | 16 | if(root == null){ 17 | return true; 18 | } 19 | 20 | if(root.left != null && root.left.val != root.val){ 21 | return false; 22 | } 23 | 24 | if(root.right != null && root.right.val != root.val){ 25 | return false; 26 | } 27 | 28 | return isUnivariant(root.left) && isUnivariant(root.right); 29 | } 30 | } -------------------------------------------------------------------------------- /Tree/Medium/PruneTree.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class ConstFromPrePost{ 4 | 5 | class Node{ 6 | 7 | int val; 8 | Node right; 9 | Node left; 10 | Node next; 11 | 12 | public Node(int x){ val = x; } 13 | } 14 | 15 | public boolean prune(TreeNode root){ 16 | 17 | if(root == null){ 18 | return false; 19 | } 20 | 21 | 22 | boolean isLeft = prune(root.left); 23 | boolean isRight = prune(root.right); 24 | 25 | if(!isLeft){ 26 | 27 | root.left = null; 28 | } 29 | 30 | if(!isRight){ 31 | root.right = null; 32 | } 33 | 34 | return isLeft || isRight || root.val == 1; 35 | } 36 | 37 | 38 | } -------------------------------------------------------------------------------- /Greedy/Medium/GasStation.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class GasStation{ 4 | 5 | public int canCompleteCircuit(int[] gas, int[] cost){ 6 | 7 | int tank = 0; 8 | 9 | for(int i = 0;i < gas.length;i++) 10 | tank += gas[i] - cost[i]; 11 | 12 | if(tank < 0) 13 | return -1; 14 | 15 | int accumulate = 0; 16 | int start = 0; 17 | 18 | for(int i = 0;i < gas.length;i++){ 19 | 20 | int curGain = gas[i] - cost[i]; 21 | 22 | if(accumulate + curGain < 0){ 23 | accumulate = 0; 24 | curGain = 0; 25 | }else{ 26 | accumulate += curGain; 27 | } 28 | } 29 | 30 | return start; 31 | } 32 | } 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /Tree/Easy/SeachBST.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class SeachBST{ 4 | 5 | class TreeNode{ 6 | 7 | int val; 8 | TreeNode right; 9 | TreeNode left; 10 | 11 | public TreeNode(int x){ val = x; } 12 | } 13 | 14 | int ans = 0; 15 | 16 | public TreeNode search(TreeNode root, int item){ 17 | 18 | if(root == null){ 19 | return null; 20 | } 21 | 22 | if(item == root.val){ 23 | return root; 24 | }else if(item > root.val){ 25 | return search(root.right, item); 26 | }else{ 27 | return search(root.left, item); 28 | } 29 | } 30 | 31 | } -------------------------------------------------------------------------------- /Arrays/Easy/fibo.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class fibo{ 4 | 5 | public static void main(String[] args){ 6 | 7 | Scanner scn = new Scanner(System.in); 8 | 9 | int n = scn.nextInt(); 10 | System.out.println(fibo(n)); 11 | 12 | } 13 | 14 | public static void display(int[] arr){ 15 | 16 | for(int i = 0 ;i < arr.length;i++){ 17 | System.out.print(arr[i] +" "); 18 | } 19 | System.out.println(); 20 | } 21 | 22 | public static int fibo(int n){ 23 | 24 | int a = 0; 25 | int b = 1; 26 | 27 | for(int i = 0;i < n;i++){ 28 | int c = a + b; 29 | a = b; 30 | b = c; 31 | } 32 | 33 | return a; 34 | } 35 | } -------------------------------------------------------------------------------- /Tree/Easy/PreorderGT.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class PreorderGT{ 4 | 5 | class Node{ 6 | 7 | int val; 8 | List _children; 9 | 10 | public Node(){} 11 | public Node(int x){ this.val = x;} 12 | } 13 | 14 | public List preorder(Node root){ 15 | 16 | List res = new ArrayList<>(); 17 | 18 | helper(root, res); 19 | return res; 20 | } 21 | 22 | public void helper(Node root, List res){ 23 | 24 | if(root == null){ 25 | return; 26 | } 27 | 28 | res.add(root.val); 29 | for(int i = 0;i < root.children.size();i++){ 30 | helper(root.children.get(i), res); 31 | } 32 | } 33 | 34 | } -------------------------------------------------------------------------------- /HashMap/Easy/UniqueCharacter.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class UniqueCharacter{ 4 | 5 | public boolean isAnagram(String s, String t){ 6 | 7 | 8 | int[] freq = new int[256]; 9 | 10 | for(char chs : s.toCharArray()){ 11 | freq[chs - 'a']++; 12 | } 13 | 14 | for(char cht : t.toCharArray()){ 15 | freq[cht - 'a']--; 16 | 17 | if(freq[cht - 'a'] < 0){ 18 | return false; 19 | } 20 | } 21 | 22 | for(int i = 0;i < 256;i++){ 23 | 24 | if(freq[i] != 0) 25 | return false; 26 | } 27 | 28 | 29 | return true; 30 | } 31 | } 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /LinkedList/Easy/CycleIndex.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class CycleIndex{ 4 | 5 | public static void main(String[] args){ 6 | 7 | 8 | } 9 | 10 | static class ListNode{ 11 | 12 | int val; 13 | ListNode Next; 14 | 15 | public ListNode(int x){ this.val = x; } 16 | } 17 | 18 | public static boolean hasCycle(ListNode head){ 19 | 20 | ListNode slow = head; 21 | ListNode fast = head.next; 22 | 23 | while(fast != null && fast.next!= null){ 24 | 25 | if(slow == fast){ 26 | return true; 27 | } 28 | slow = slow.next; 29 | fast = fast.next.next; 30 | 31 | 32 | } 33 | 34 | return false; 35 | } 36 | 37 | 38 | } -------------------------------------------------------------------------------- /Tree/Easy/LowestCommonAncestor.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class LowestCommonAncestor{ 4 | 5 | class Node{ 6 | 7 | int val; 8 | Node right; 9 | Node left; 10 | Node next; 11 | 12 | public Node(int x){ val = x; } 13 | } 14 | 15 | public int find(TreeNode root){ 16 | 17 | if(root == null){ 18 | return -1; 19 | } 20 | 21 | List res = new ArrayList<>(); 22 | dfs(root, res); 23 | 24 | return res.get(i - 1); 25 | } 26 | 27 | public void dfs(TreeNode root, List res){ 28 | 29 | if(root == null) return null; 30 | 31 | dfs(root.left); 32 | res.add(root.val); 33 | dfs(root.right); 34 | } 35 | 36 | } -------------------------------------------------------------------------------- /Tree/Easy/PostorderGT.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class PostorderGT{ 4 | 5 | class Node{ 6 | 7 | int val; 8 | List _children; 9 | 10 | public Node(){} 11 | public Node(int x){ this.val = x;} 12 | } 13 | 14 | public List preorder(Node root){ 15 | 16 | List res = new ArrayList<>(); 17 | 18 | helper(root, res); 19 | return res; 20 | } 21 | 22 | public void helper(Node root, List res){ 23 | 24 | if(root == null){ 25 | return; 26 | } 27 | 28 | for(int i = 0;i < root.children.size();i++){ 29 | helper(root.children.get(i), res); 30 | } 31 | 32 | res.add(root.val); 33 | } 34 | 35 | } -------------------------------------------------------------------------------- /HashMap/Easy/MyHashSet.java: -------------------------------------------------------------------------------- 1 | class MyHashSet { 2 | 3 | /** Initialize your data structure here. */ 4 | public MyHashSet() { 5 | 6 | } 7 | 8 | public void add(int key) { 9 | 10 | } 11 | 12 | public void remove(int key) { 13 | 14 | } 15 | 16 | /** Returns true if this set contains the specified element */ 17 | public boolean contains(int key) { 18 | 19 | } 20 | } 21 | 22 | /** 23 | * Your MyHashSet object will be instantiated and called as such: 24 | * MyHashSet obj = new MyHashSet(); 25 | * obj.add(key); 26 | * obj.remove(key); 27 | * boolean param_3 = obj.contains(key); 28 | */ -------------------------------------------------------------------------------- /Tree/Medium/CheckCompletness.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class CheckCompletness{ 4 | 5 | class Node{ 6 | 7 | int val; 8 | Node right; 9 | Node left; 10 | Node next; 11 | 12 | public Node(int x){ val = x; } 13 | } 14 | 15 | public boolean isCompleteTree(TreeNode root){ 16 | 17 | Queue bfs = new LinkedList<>(); 18 | 19 | bfs.add(root); 20 | 21 | 22 | while(bfs.peek() != null){ 23 | 24 | TreeNode rp = bfs.poll(); 25 | 26 | bfs.offer(rp.left); 27 | bfs.offer(rp.right); 28 | } 29 | 30 | while(!bfs.isEmpty() && bfs.peek() == null){ 31 | bfs.poll(); 32 | } 33 | 34 | return bfs.isEmpty(); 35 | } 36 | 37 | } -------------------------------------------------------------------------------- /Recursion/Medium/KthGrammer.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class KthGrammer{ 4 | 5 | 6 | public static void main(String[] args){ 7 | 8 | 9 | int n = 1; 10 | int k = 1; 11 | 12 | System.out.println(kthGrammar(n,k)); 13 | 14 | } 15 | 16 | public static int kthGrammar(int n,int k){ 17 | 18 | return helper(n,k); 19 | } 20 | 21 | public static String helper(int n, int k){ 22 | 23 | if(n == 1){ 24 | return 0; 25 | } 26 | 27 | 28 | int parentk = (k / 2) + (k % 2); 29 | 30 | int parent = helper(n - 1,parentk); 31 | 32 | if(k % 2 == 1){ 33 | return parent; 34 | }else{ 35 | return 1 - parent; 36 | } 37 | 38 | } 39 | 40 | 41 | } -------------------------------------------------------------------------------- /Stack/Medium/CarFleet.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class CarFleet{ 4 | 5 | 6 | public static void main(String[] args){ 7 | 8 | String preorder = ""; 9 | 10 | 11 | } 12 | 13 | public static int[] calculate(int target, int[] position, int[] speed){ 14 | 15 | TreeMap map = new TreeMap<>(); 16 | 17 | for(int i = 0;i < position.length;i++){ 18 | 19 | map.put(position[i], (double)(target - position[i]) / speed[i]); 20 | } 21 | 22 | int curr = 0; 23 | int ans = 0; 24 | for(int time : map.values){ 25 | 26 | if(time > curr){ 27 | curr = time; 28 | ans++; 29 | } 30 | } 31 | 32 | return ans; 33 | 34 | } 35 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Leetcode-Solutions 2 | Containing Solution for LeetCode Questions TopicWise 3 | 4 | # Description 5 | - Each folder in the repo corresponds to each Each Topic on Leetcode. 6 | - Further Each Folder contains 3 Sub-Folders as Difficulty Level Viz. Easy, Med, Hard. 7 | 8 | ## Topics 9 | - Arrays 10 | - Hash Tables 11 | - Linked List 12 | - Math 13 | - Two Pointers 14 | - String 15 | - Searching and Sorting 16 | - Divide and Conquer 17 | - Dynamic Programming 18 | - BackTracking 19 | - Stack 20 | - Heap 21 | - Greedy 22 | - Tree 23 | - Graph 24 | - Recursion 25 | - Queue 26 | - Bit manipulation 27 | - Segment Trees 28 | - Topological Sort 29 | - Map 30 | - Binary Search 31 | -------------------------------------------------------------------------------- /Tree/Easy/SumTorootLeaf.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class SumTorootLeaf{ 4 | 5 | class TreeNode{ 6 | 7 | int val; 8 | TreeNode right; 9 | TreeNode left; 10 | 11 | public TreeNode(int x){ val = x; } 12 | } 13 | 14 | public int sumRootToLeaf(TreeNode root){ 15 | 16 | if(root == null){ 17 | return 0; 18 | } 19 | 20 | return dfs(root, 0); 21 | } 22 | 23 | public int dfs(TreeNode root, int val){ 24 | 25 | if(root == null){ 26 | return 0; 27 | } 28 | 29 | val = 2 * val + root.val; 30 | 31 | return (root.left == root.right ? val : dfs(root.left,val) + dfs(root.right, val)); 32 | } 33 | } -------------------------------------------------------------------------------- /Bit Manipulation/Medium/subsets.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class subsets { 4 | 5 | public static void main(String[] args){ 6 | 7 | int[] arr = {1,2,3}; 8 | System.out.println(subsets(arr)); 9 | } 10 | 11 | public static List> subsets(int[] arr){ 12 | 13 | int num = (int)Math.pow(2, arr.length); 14 | List> res = new ArrayList<>(); 15 | 16 | 17 | for(int j = 0;j < num;j++){ 18 | List set = new ArrayList<>(); 19 | for(int i = 0;i < arr.length;i++){ 20 | if(((j >> i) & 1 ) == 1){ 21 | set.add(arr[i]); 22 | } 23 | 24 | } 25 | 26 | res.add(set); 27 | } 28 | 29 | return res; 30 | } 31 | } 32 | 33 | -------------------------------------------------------------------------------- /Stack/Medium/Nextgreater2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Nextgreater2{ 4 | 5 | 6 | public static void main(String[] args){ 7 | 8 | String preorder = ""; 9 | 10 | 11 | } 12 | 13 | public static int[] nextGreater(int[] nums){ 14 | 15 | Stack stack = new Stack<>(); 16 | int[] res = new int[nums.length]; 17 | 18 | for(int i = 2 * nums.length - 1;i >= 0;i--){ 19 | 20 | 21 | while(!stack.isEmpty() && nums[i % nums.length] < nums[stack.peek()]){ 22 | 23 | stack.pop(); 24 | } 25 | 26 | res[i % nums.length] =(stack.isEmpty()? -1 : nums[stack.peek()]); 27 | stack.push(i % nums.length); 28 | } 29 | 30 | return res; 31 | } 32 | } -------------------------------------------------------------------------------- /Stack/Easy/RemoveAdjacent.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class RemoveAdjacent{ 4 | 5 | 6 | public static void main(String[] args){ 7 | 8 | 9 | } 10 | 11 | public static int RemoveDuplicates(String str){ 12 | 13 | Stack stack = new Stack<>(); 14 | 15 | for(int i = 0;i < str.length();i++){ 16 | 17 | char ch = str.charAt(i); 18 | 19 | while(!stack.isEmpty && stack.peek() == ch){ 20 | 21 | stack.pop() 22 | } 23 | 24 | stack.push(ch); 25 | } 26 | 27 | StringBuilder sb = new StringBuilder(); 28 | 29 | while(!stack.isEmpty()){ 30 | 31 | sb.append(0,stack.pop()); 32 | } 33 | 34 | return sb.toString(); 35 | 36 | } 37 | 38 | 39 | 40 | } -------------------------------------------------------------------------------- /Arrays/Medium/Mycalendar.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Mycalendar{ 4 | 5 | TreeSet list; 6 | public Mycalendar(){ 7 | 8 | this.list = new TreeSet<>((a, b) -> (a[0] - b[0])); 9 | 10 | } 11 | 12 | public boolean book(int start, int end){ 13 | 14 | if(start >= end) 15 | return false; 16 | 17 | int[] bk = new int[]{start, end}; 18 | 19 | int[] floor = this.list.floor(bk); 20 | 21 | if(floor != null && floor[1] > bk[0]){ 22 | return false; 23 | } 24 | 25 | int[] ceiling = this.list.ceiling(bk); 26 | 27 | if(ceiling != null && ceiling[0] < bk[1]){ 28 | return false; 29 | } 30 | 31 | this.list.add(bk); 32 | return true; 33 | } 34 | 35 | } -------------------------------------------------------------------------------- /Stack/Hard/FreqStack.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class FreqStack{ 4 | 5 | 6 | HashMap freq = new HashMap<>(); 7 | HashMap> m = new HashMap<>(); 8 | int maxFreq = 0; 9 | 10 | public void push(int x){ 11 | 12 | int f = freq.getOrDefault(x, 0) + 1; 13 | 14 | freq.put(x, f); 15 | maxFreq = Math.max(maxFreq, f); 16 | 17 | if(!m.containsKey(f)){ 18 | 19 | m.put(f, new Stack<>()); 20 | } 21 | 22 | m.get(f).add(x); 23 | 24 | } 25 | 26 | public int pop(){ 27 | 28 | int x = m.get(maxFreq).pop(); 29 | freq.put(x, maxFreq - 1); 30 | 31 | if(m.get(maxFreq).size() == 0) 32 | maxFreq--; 33 | 34 | return x; 35 | } 36 | } -------------------------------------------------------------------------------- /Stack/Medium/MinmumAddToMakeValid.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class MinmumAddToMakeValid{ 4 | 5 | 6 | public static void main(String[] args){ 7 | 8 | String preorder = ""; 9 | 10 | 11 | } 12 | 13 | public static int MinCount(String S){ 14 | 15 | int count = 0; 16 | 17 | Stack stack = new Stack<>(); 18 | 19 | for(int i =0;i < S.length();i++){ 20 | 21 | char ch = S.charAt(i); 22 | 23 | if(ch == '('){ 24 | stack.push(ch); 25 | }else{ 26 | 27 | 28 | if(!stack.isEmpty() && stack.peek() == '('){ 29 | stack.pop(); 30 | continue; 31 | } 32 | 33 | stack.push(ch); 34 | } 35 | } 36 | 37 | return stack.size(); 38 | } 39 | } -------------------------------------------------------------------------------- /Tree/Easy/BSTfromSortedArray.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class BSTfromSortedArray{ 4 | 5 | class TreeNode{ 6 | 7 | int val; 8 | TreeNode right; 9 | TreeNode left; 10 | 11 | public TreeNode(int x){ val = x; } 12 | } 13 | 14 | public TreeNode construct(int[] nums){ 15 | 16 | return helper(nums,0, nums.length - 1); 17 | } 18 | 19 | public TreeNode helper(int[] nums, int i, int j){ 20 | 21 | if(i > j){ 22 | return null; 23 | } 24 | 25 | int mid = (i + j) / 2; 26 | 27 | TreeNode nn = new TreeNode(nums[mid]); 28 | 29 | nn.left = helper(nums, i, mid - 1); 30 | nn.right = helper(nums, mid + 1, j); 31 | 32 | return nn; 33 | } 34 | 35 | 36 | 37 | } -------------------------------------------------------------------------------- /Tree/Easy/MinimumDistance.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class MinimumDistance{ 4 | 5 | class TreeNode{ 6 | 7 | int val; 8 | TreeNode right; 9 | TreeNode left; 10 | 11 | public TreeNode(int x){ val = x; } 12 | } 13 | 14 | TreeNode prev = null; 15 | int min = Integer.MAX_VALUE; 16 | 17 | public void minDiffBST(TreeNode root){ 18 | 19 | if(root == null){ 20 | return; 21 | } 22 | 23 | minDiffBST(root.left); 24 | 25 | if(prev != null){ 26 | 27 | min = Math.min(min, root.val - prev.val); 28 | } 29 | 30 | prev = root; 31 | 32 | 33 | 34 | minDiffBST(root.right); 35 | } 36 | 37 | } -------------------------------------------------------------------------------- /Tree/Easy/HasPath.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class HasPath{ 4 | 5 | class TreeNode{ 6 | 7 | int val; 8 | TreeNode right; 9 | TreeNode left; 10 | 11 | public TreeNode(int x){ val = x; } 12 | } 13 | 14 | public boolean Find(TreeNode root, int sum){ 15 | 16 | if(root == null || sum < 0){ 17 | return false; 18 | } 19 | 20 | if(sum == root.val){ 21 | return true; 22 | } 23 | 24 | if(root.left != null) 25 | boolean left = Find(root.left, sum - root.left.val); 26 | 27 | if(left) 28 | return true; 29 | 30 | if(root.right != null){ 31 | boolean right = Find(root.right, sum - root.right.val); 32 | } 33 | 34 | return right; 35 | } 36 | 37 | 38 | } -------------------------------------------------------------------------------- /Bit Manipulation/Easy/LetterCasePermutation.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class LetterCasePermutation { 4 | 5 | public List letterCasePermutation(String str){ 6 | 7 | List ans = new ArrayList<>(); 8 | 9 | 10 | return dfs(str, 0, ans); 11 | 12 | } 13 | 14 | public List dfs(char[] str, int vidx, List ans){ 15 | 16 | ans.add(String.valueOf(str)); 17 | 18 | if(vidx >= str.length()){ 19 | return ans; 20 | } 21 | 22 | for(int i = vidx;i < str.length();i++){ 23 | 24 | if(Character.isAlphabetic(str[i])){ 25 | 26 | str[i] ^= 32; 27 | dfs(str, i + 1, ans); 28 | str[i] ^= 32; 29 | } 30 | } 31 | 32 | return ans; 33 | } 34 | } 35 | 36 | -------------------------------------------------------------------------------- /HashMap/Easy/SubarraySumDivbyK.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class SubarraySumDivbyK{ 4 | 5 | 6 | public String[] findOccurences(String text, String first, String second){ 7 | 8 | 9 | StringBuilder sb = new StringBuilder(""); 10 | String[] tokens = text.split(" "); 11 | 12 | for(int i = 2;i < tokens.length();i++){ 13 | 14 | if(tokens[i - 2].equals(first) && tokens[i -1].equals(second)){ 15 | 16 | if(!"".equals(sb.toString())){ 17 | sb.append(" "); 18 | } 19 | 20 | sb.append(tokens[i]); 21 | } 22 | } 23 | 24 | if("".equals(sb)){ 25 | return new String[0]; 26 | } 27 | 28 | String[] arr = sb.toString.split(" "); 29 | 30 | return arr; 31 | } 32 | 33 | } -------------------------------------------------------------------------------- /HashMap/Easy/LongestPalindrome.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class LongestPalindrome{ 4 | 5 | public int[] intersect(int[] nums1, int[] nums2){ 6 | 7 | 8 | ArrayList ans = new ArrayList<>(); 9 | 10 | HashMap map = new HashMap<>(); 11 | 12 | for(int num : nums1) 13 | map.put(num, map.getOrDefault(num, 0) + 1); 14 | 15 | for(int num : nums2){ 16 | 17 | if(map.containsKey(num)){ 18 | 19 | ans.add(num); 20 | map.remove(num); 21 | } 22 | } 23 | 24 | int[] res = new int[ans.size()]; 25 | for(int i = 0;i < res.length;i++) 26 | res[i] = ans.get(i); 27 | return ans; 28 | 29 | } 30 | } 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /LinkedList/Easy/DeleteSpecificNode.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class DeleteSpecificNode{ 4 | 5 | public static void main(String[] args){ 6 | 7 | 8 | } 9 | 10 | static class ListNode{ 11 | 12 | int val; 13 | ListNode Next; 14 | 15 | public ListNode(int x){ this.val = x; } 16 | } 17 | 18 | public static void delete(ListNode node, int item){ 19 | 20 | 21 | if(node == null || this.head == null){ 22 | return ; 23 | } 24 | 25 | ListNode prev = head; 26 | ListNode curr = prev.next; 27 | 28 | while(curr != null){ 29 | 30 | if(curr.val == item){ 31 | prev.next = curr.next; 32 | break; 33 | } 34 | 35 | prev = curr; 36 | curr = curr.next; 37 | } 38 | } 39 | } -------------------------------------------------------------------------------- /LinkedList/Medium/RemoveNthFromLast.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class RemoveNthFromLast{ 4 | 5 | public static void main(String[] args){ 6 | 7 | 8 | } 9 | 10 | static class ListNode{ 11 | 12 | int val; 13 | ListNode Next; 14 | 15 | public ListNode(int x){ this.val = x; } 16 | } 17 | 18 | public static ListNode Find(ListNode head, int n){ 19 | 20 | 21 | ListNode slow = head; 22 | ListNode fast = head; 23 | 24 | for(int i = 0;i < n - 1 && fast != null;i++){ 25 | 26 | fast = fast.next; 27 | } 28 | 29 | while(fast != null){ 30 | 31 | fast = fast.next; 32 | slow = slow.next; 33 | 34 | } 35 | 36 | slow.next = slow.next; 37 | 38 | return head; 39 | 40 | } 41 | 42 | } -------------------------------------------------------------------------------- /Tree/Easy/RangeSumBst.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class RangeSumBst{ 4 | 5 | class TreeNode{ 6 | 7 | int val; 8 | TreeNode right; 9 | TreeNode left; 10 | 11 | public TreeNode(int x){ val = x; } 12 | } 13 | 14 | public int rangeSum(TreeNode root, int L, int R){ 15 | 16 | if(root == null){ 17 | return 0; 18 | } 19 | 20 | if(root.val < L){ 21 | return rangeSum(root.right, L, R); 22 | }else if(root.val > R){ 23 | 24 | return rangeSum(root.left, L, R); 25 | }else{ 26 | 27 | int lsum = rangeSum(root.left, L, R); 28 | int rsum = rangeSum(root.right, L, R); 29 | 30 | return lsum + rsum + root.val; 31 | 32 | } 33 | } 34 | } -------------------------------------------------------------------------------- /HashMap/Easy/OccurenceAfterBigram.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class OccurenceAfterBigram{ 4 | 5 | 6 | public String[] findOccurences(String text, String first, String second){ 7 | 8 | 9 | StringBuilder sb = new StringBuilder(""); 10 | String[] tokens = text.split(" "); 11 | 12 | for(int i = 2;i < tokens.length();i++){ 13 | 14 | if(tokens[i - 2].equals(first) && tokens[i -1].equals(second)){ 15 | 16 | if(!"".equals(sb.toString())){ 17 | sb.append(" "); 18 | } 19 | 20 | sb.append(tokens[i]); 21 | } 22 | } 23 | 24 | if("".equals(sb)){ 25 | return new String[0]; 26 | } 27 | 28 | String[] arr = sb.toString.split(" "); 29 | 30 | return arr; 31 | } 32 | 33 | } -------------------------------------------------------------------------------- /Two Pointers/Easy/ImplementStrstr.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class ReverseString{ 4 | 5 | public static void main(String[] args){ 6 | 7 | Scanner scn = new Scanner(System.in); 8 | 9 | char[] ch = {'h','e','l','l','o'}; 10 | } 11 | 12 | public static void display(char[] arr){ 13 | 14 | for(int i = 0 ;i < arr.length;i++){ 15 | System.out.print(arr[i] +" "); 16 | } 17 | System.out.println(); 18 | } 19 | 20 | public static int Reverse(char[] arr){ 21 | 22 | 23 | int i = 0; 24 | int j = arr.length - 1; 25 | while(i < j){ 26 | 27 | char temp = arr[i]; 28 | arr[i] = arr[j]; 29 | arr[j] = temp; 30 | 31 | i++; 32 | j--; 33 | 34 | } 35 | 36 | 37 | } 38 | } -------------------------------------------------------------------------------- /Tree/Medium/ConstBSTPreorder.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class ConstBSTPreorder{ 4 | 5 | class Node{ 6 | 7 | int val; 8 | Node right; 9 | Node left; 10 | Node next; 11 | 12 | public Node(int x){ val = x; } 13 | } 14 | 15 | int i = 0; 16 | 17 | public TreeNode bstFromPreorder(int[] preorder){ 18 | 19 | return dfs(preorder, Integer.MAX_VALUE); 20 | } 21 | 22 | public TreeNode dfs(int[] preorder, int bound){ 23 | 24 | if(i == preorder.length || preorder[i] > bound){ 25 | return null; 26 | } 27 | 28 | TreeNode nn = new TreeNode(preorder[i++]); 29 | 30 | nn.left = dfs(preorder, root.val); 31 | nn.right = dfs(preorder, bound); 32 | 33 | return nn; 34 | 35 | } 36 | 37 | } -------------------------------------------------------------------------------- /DP/Easy/UglyNumber.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class UglyNumber{ 4 | 5 | public static void main(String[] args){ 6 | 7 | int num = 7; 8 | System.out.println(isUgly(num)); 9 | 10 | } 11 | 12 | public static boolean isUgly(int num){ 13 | 14 | 15 | return helper(num); 16 | } 17 | 18 | public static boolean helper(int num){ 19 | 20 | 21 | if( num == 1){ 22 | return true; 23 | } 24 | 25 | if(num == 0){ 26 | return false; 27 | } 28 | 29 | if(num % 2 == 0){ 30 | 31 | return helper(num/2); 32 | }else if(num % 3 == 0){ 33 | return helper(num/3); 34 | }else if(num % 5 == 0){ 35 | return helper(num/5); 36 | }else{ 37 | return false; 38 | } 39 | } 40 | 41 | 42 | 43 | } -------------------------------------------------------------------------------- /HashMap/Easy/UncommonWords.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class UncommonWords{ 4 | 5 | public String[] uncommonFromSentences(String A, String B) { 6 | 7 | HashMap count = new HashMap<>(); 8 | 9 | for(String a : A.split(" ")) 10 | count.put(a,count.getOrDefault(a, 0) + 1); 11 | for(String b : B.split(" ")) 12 | count.put(b,count.getOrDefault(b, 0) + 1); 13 | 14 | List ans = new ArrayList<>(); 15 | 16 | for(String word : count.keySet()){ 17 | 18 | if(count.get(word) == 1){ 19 | ans.add(word); 20 | } 21 | } 22 | 23 | return ans.toArray(new String[ans.size()]); 24 | } 25 | } 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /LinkedList/Medium/fold.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class fold{ 4 | 5 | public static void main(String[] args){ 6 | 7 | 8 | } 9 | 10 | static class ListNode{ 11 | 12 | int val; 13 | ListNode Next; 14 | 15 | public ListNode(int x){ this.val = x; } 16 | } 17 | 18 | static class mover{ 19 | ListNode prev; 20 | } 21 | 22 | public static void helper(ListNode curr,mover m){ 23 | 24 | if(curr == null){ 25 | return; 26 | } 27 | 28 | 29 | helper(curr.next,m); 30 | 31 | if(mover.prev == curr || mover.prev.next == curr){ 32 | return; 33 | } 34 | 35 | Node save = mover.prev.next; 36 | mover.prev.next = curr; 37 | curr.next = save; 38 | 39 | mover.prev = save; 40 | } 41 | 42 | } -------------------------------------------------------------------------------- /HashMap/Easy/nRepeatedElement.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class nRepeatedElement{ 4 | 5 | public String[] uncommonFromSentences(String A, String B) { 6 | 7 | HashMap count = new HashMap<>(); 8 | 9 | for(String a : A.split(" ")) 10 | count.put(a,count.getOrDefault(a, 0) + 1); 11 | for(String b : B.split(" ")) 12 | count.put(b,count.getOrDefault(b, 0) + 1); 13 | 14 | List ans = new ArrayList<>(); 15 | 16 | for(String word : count.keySet()){ 17 | 18 | if(count.get(word) == 1){ 19 | ans.add(word); 20 | } 21 | } 22 | 23 | return ans.toArray(new String[ans.size()]); 24 | } 25 | } 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /Tree/Easy/SameTree.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class SameTree{ 4 | 5 | class TreeNode{ 6 | 7 | int val; 8 | TreeNode right; 9 | TreeNode left; 10 | 11 | public TreeNode(int x){ val = x; } 12 | } 13 | public boolean isSame(TreeNode p, TreeNode q){ 14 | 15 | if(p == null || q == null){ 16 | return true; 17 | } 18 | 19 | if(p.val != q.val){ 20 | return false; 21 | } 22 | 23 | if((p.left == null && q.left != null) || (p.left != null && q.left == null)){ 24 | return false; 25 | } 26 | 27 | if((p.right == null && q.right != null) || (p.right != null && q.right == null)){ 28 | return false; 29 | } 30 | 31 | return isSame(p.left,q.left) && isSame(p.right,q.right); 32 | 33 | } 34 | } -------------------------------------------------------------------------------- /Greedy/Medium/findMinArrowShots.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class findMinArrowShots{ 4 | 5 | public int[][] eraseOverlapIntervals(int[][] intervals){ 6 | 7 | 8 | Arrays.sort(intervals, new Comparator(){ 9 | 10 | @Override 11 | public int compare(int[] a, int[] b){ 12 | 13 | return a[0] != b[0] ? a[0] - b[0] : b[1] - a[1]; 14 | } 15 | }); 16 | 17 | int count = 0; 18 | int end = intervals[0][1]; 19 | 20 | for(int i = 1;i < intervals.length;i++){ 21 | 22 | if(intervals[i][0] >= end){ 23 | end = intervals[i][1]; 24 | count++; 25 | } 26 | } 27 | 28 | return intervals.length - count; 29 | 30 | } 31 | } 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /HashMap/Easy/CommonCharacters.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class CommonCharacters{ 4 | 5 | 6 | public List commonChars(String[] A){ 7 | 8 | int[] count = new int[26]; 9 | 10 | Arrays.fill(count, Integer.MAX_VALUE); 11 | 12 | for(String s : A){ 13 | 14 | int[] tempCnt = new int[26]; 15 | for(int i = 0;i < s.length();i++){ 16 | ++tempCnt[s.charAt(i) - 'a']; 17 | } 18 | 19 | for(int i = 0;i < 26;i++){ 20 | count[i] = Math.min(count[i], tempCnt[i]); 21 | } 22 | } 23 | 24 | ArrayList ans = new ArrayList<>(); 25 | 26 | for(char ch = 'a'; c <= 'z';c++){ 27 | 28 | while(count[c - 'a']-- > 0){ 29 | ans.add(ch); 30 | } 31 | } 32 | 33 | return ans; 34 | } 35 | 36 | } -------------------------------------------------------------------------------- /Tree/Hard/MaximumPathSum.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class MaximumPathSum{ 4 | 5 | class Node{ 6 | 7 | int val; 8 | Node right; 9 | Node left; 10 | Node next; 11 | 12 | public Node(int x){ val = x; } 13 | } 14 | 15 | int max = Integer.MIN_VALUE; 16 | 17 | public int maxPathSum(TreeNode root){ 18 | 19 | helper(root); 20 | return max; 21 | 22 | } 23 | 24 | public int helper(TreeNode root){ 25 | if(root == null){ 26 | return 0; 27 | } 28 | 29 | int LSum = Math.max(0, helper(root.left)); 30 | int RSum = Math.max(0, helper(root.right)); 31 | int selfSum = LSum + RSum + root.val; 32 | 33 | max = Math.max(selfSum, max); 34 | 35 | return Math.max(LSum, RSum) + root.val; 36 | } 37 | 38 | 39 | } -------------------------------------------------------------------------------- /Bit Manipulation/Easy/FindTheDiff.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class FindTheDiff { 4 | 5 | public char findTheDifference(String s, String t){ 6 | 7 | if(s.length() == 0 & t.length() == 0){ 8 | return '~'; 9 | } 10 | 11 | int ans = 0; 12 | 13 | for(int i = 0;i < s.length();i++){ 14 | 15 | ans = ans ^ s.charAt(i); 16 | } 17 | 18 | for(int i = 0;i < t.length();i++){ 19 | ans = ans ^ t.charAt(i); 20 | } 21 | 22 | return ans; 23 | } 24 | 25 | public char findTheDiff_elegant(String s, String t){ 26 | 27 | int n = t.length(); 28 | char c = t.charAt(n - 1); 29 | 30 | for(int i = 0;i < n - 1;i++){ 31 | 32 | c ^= s.charAt(i); 33 | c ^= t.charAt(i); 34 | } 35 | 36 | return c; 37 | } 38 | } 39 | 40 | -------------------------------------------------------------------------------- /Stack/Medium/VerifyPreorder.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class VerifyPreorder{ 4 | 5 | 6 | public static void main(String[] args){ 7 | 8 | String preorder = ""; 9 | 10 | 11 | } 12 | 13 | public static boolean isValid(String preorder){ 14 | 15 | Stack stack = new Stack<>(); 16 | 17 | String[] str = preorder.split(","); 18 | 19 | for(int i = 0;i < str.length;i++){ 20 | 21 | String curr = preorder[i]; 22 | 23 | while(curr.equals("#") && !stack.isEmpty() && stack.peek().equals(curr)){ 24 | 25 | if(stack.isEmpty()){ 26 | return false; 27 | } 28 | 29 | stack.pop(); 30 | 31 | } 32 | 33 | stack.push(curr); 34 | } 35 | 36 | return stack.size() == 1 && stack.peek().equals("#"); 37 | } 38 | } -------------------------------------------------------------------------------- /Tree/Medium/InsertIntoBST.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class InsertIntoBST{ 4 | 5 | class Node{ 6 | 7 | int val; 8 | Node right; 9 | Node left; 10 | Node next; 11 | 12 | public Node(int x){ val = x; } 13 | } 14 | 15 | public TreeNode insertInto(TreeNode Parent, TreeNode root,int val){ 16 | 17 | if(root == null){ 18 | 19 | TreeNode nn = new TreeNode(val); 20 | if(parent.val < val){ 21 | parent.right = nn; 22 | }else{ 23 | parent.left = nn; 24 | } 25 | return nn; 26 | } 27 | 28 | if(root.val > val){ 29 | 30 | root.left = insertInto(root, root.left, val); 31 | }else if(root.val < val){ 32 | root.right = insertInto(root, root.right, val); 33 | } 34 | 35 | return root; 36 | } 37 | 38 | } -------------------------------------------------------------------------------- /HashMap/Easy/KeboardRow.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class KeboardRow{ 4 | 5 | public int islandPerimeter(int[][] grid){ 6 | 7 | int sum = 0; 8 | if(grid == null || grid.length == 0 || grid[0].length == 0) 9 | return sum; 10 | 11 | for(int i = 0;i < grid.length;i++){ 12 | for (int j = 0; j < grid[0].length ; j++) { 13 | 14 | if(grid[i][j] == 0) 15 | continue; 16 | 17 | if(i == 0 || grid[i - 1][j] == 0) sum++; 18 | if(i == grid.length || grid[i + 1][j] == 0) sum++; 19 | if(j == 0 || grid[i][j - 1] == 0) sum++; 20 | if(j == grid[0].length || grid[i][j + 1] == 0) sum++; 21 | } 22 | } 23 | 24 | return sum; 25 | } 26 | } 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /LinkedList/Medium/OddEvenList.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class OddEvenList{ 4 | 5 | public static void main(String[] args){ 6 | 7 | 8 | } 9 | 10 | static class ListNode{ 11 | 12 | int val; 13 | ListNode Next; 14 | 15 | public ListNode(int x){ this.val = x; } 16 | } 17 | 18 | public static ListNode oddEvenList(ListNode head){ 19 | 20 | if(head == null || head.next == null){ 21 | return head; 22 | } 23 | 24 | ListNode odd = head, even = head.next,evenHead = even; 25 | 26 | while(even != null && even.next != null){ 27 | 28 | odd.next = even.next; 29 | odd = odd.next; 30 | even.next = odd.next; 31 | even = even.next; 32 | } 33 | 34 | odd.next = evenHead; 35 | 36 | return head; 37 | } 38 | 39 | 40 | 41 | } -------------------------------------------------------------------------------- /Tree/Easy/GreaterTree.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class GreaterTree{ 4 | 5 | class TreeNode{ 6 | 7 | int val; 8 | TreeNode right; 9 | TreeNode left; 10 | 11 | public TreeNode(int x){ val = x; } 12 | } 13 | 14 | class HeapMover{ 15 | 16 | int sum = 0; 17 | } 18 | 19 | public TreeNode convertBST(TreeNode root){ 20 | 21 | HeapMover mover = new HeapMover(); 22 | convert(root,mover); 23 | return root; 24 | } 25 | 26 | public void convert(TreeNode root, HeapMover mover){ 27 | 28 | 29 | if(root == null){ 30 | return; 31 | } 32 | 33 | convert(root.right, mover); 34 | 35 | int temp = root.val; 36 | mover.sum += temp; 37 | root.val = mover.mosum; 38 | 39 | convert(root.left, mover); 40 | 41 | 42 | } 43 | 44 | } -------------------------------------------------------------------------------- /Tree/Easy/RootToLeafPaths.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class RootToLeafPaths{ 4 | 5 | class TreeNode{ 6 | 7 | int val; 8 | TreeNode right; 9 | TreeNode left; 10 | 11 | public TreeNode(int x){ val = x; } 12 | } 13 | 14 | public List paths(TreeNode root){ 15 | 16 | List res = new ArrayList<>(); 17 | 18 | if(root != null) 19 | helper(root,res, ""); 20 | return res; 21 | } 22 | 23 | public void helper(TreeNode root, List res, String sb){ 24 | 25 | if(root.left == null && root.right == null) res.add(sb + root.val); 26 | 27 | if(root.left != null) helper(root.left, res, sb + root.val + "->"); 28 | if(root.right != null) helper(root.right, res, sb + root.val + "->"); 29 | 30 | 31 | } 32 | 33 | 34 | } -------------------------------------------------------------------------------- /DP/Medium/BuySellStockTransactionFee.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class BuySellStockTransactionFee{ 4 | 5 | public static void main(String[] args){ 6 | 7 | int[] prices = { 1, 3, 2, 8, 4, 9 }; 8 | int fee = 2; 9 | System.out.println(maxProfit(prices, fee)); 10 | } 11 | 12 | public static int maxProfit(int[] prices, int fee){ 13 | 14 | if(prices.length < 2){ 15 | return 0; 16 | } 17 | 18 | int profit = 0; 19 | int min = prices[0]; 20 | 21 | for(int i = 0;i < prices.length;i++){ 22 | 23 | if(prices[i] < min){ 24 | min = prices[i]; 25 | }else if(prices[i] > min + fee){ 26 | 27 | profit += prices[i] - min - fee; 28 | min = prices[i] - min; 29 | } 30 | } 31 | 32 | return profit; 33 | } 34 | 35 | 36 | 37 | } -------------------------------------------------------------------------------- /HashMap/Easy/islandParameter.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class islandParameter{ 4 | 5 | public int islandPerimeter(int[][] grid){ 6 | 7 | int sum = 0; 8 | if(grid == null || grid.length == 0 || grid[0].length == 0) 9 | return sum; 10 | 11 | for(int i = 0;i < grid.length;i++){ 12 | for (int j = 0; j < grid[0].length ; j++) { 13 | 14 | if(grid[i][j] == 0) 15 | continue; 16 | 17 | if(i == 0 || grid[i - 1][j] == 0) sum++; 18 | if(i == grid.length || grid[i + 1][j] == 0) sum++; 19 | if(j == 0 || grid[i][j - 1] == 0) sum++; 20 | if(j == grid[0].length || grid[i][j + 1] == 0) sum++; 21 | } 22 | } 23 | 24 | return sum; 25 | } 26 | } 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /Heap/Medium/KFrequentElements.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class KFrequentElements{ 4 | 5 | 6 | 7 | public List topKFrequent(int[] nums, int k){ 8 | 9 | HashMap count = new HashMap<>(); 10 | 11 | for(int num : nums){ 12 | count.put(count.getOrDefault(num, 0) + 1); 13 | } 14 | 15 | PriorityQueue pq = new PriorityQueue<>((a, b) -> count.get(b) - count.get(a)); 16 | 17 | for(int key : count.keySet()){ 18 | 19 | pq.offer(key); 20 | 21 | if(pq.size() > k){ 22 | pq.poll(); 23 | } 24 | } 25 | 26 | List ans = new ArrayList<>(); 27 | 28 | while(!pq.isEmpty()){ 29 | ans.add(pq.poll()); 30 | } 31 | 32 | return ans; 33 | 34 | } 35 | } -------------------------------------------------------------------------------- /Stack/Medium/DailyTemperature.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class DailyTemperature{ 4 | 5 | 6 | public static void main(String[] args){ 7 | 8 | String preorder = ""; 9 | 10 | 11 | } 12 | 13 | public static int[] calculate(int[] temp){ 14 | 15 | int[] res = new int[temp.length]; 16 | 17 | Stack stack = new Stack<>(); 18 | 19 | for(int i = 0;i < nums.length;i++){ 20 | 21 | while(!stack.isEmpty() && nums[i] > nums[stack.peek()]){ 22 | 23 | int prevDay = stack.pop(); 24 | int currDay = nums[i]; 25 | 26 | res[prevDay] = currDay - prevDay - 1; 27 | } 28 | 29 | stack.push(i); 30 | } 31 | 32 | 33 | while(!stack.isEmpty()){ 34 | 35 | res[stack.pop()] = 0; 36 | } 37 | 38 | 39 | 40 | return res; 41 | } 42 | } -------------------------------------------------------------------------------- /Tree/Easy/isSubTree.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class isSubTree{ 4 | 5 | class TreeNode{ 6 | 7 | int val; 8 | TreeNode right; 9 | TreeNode left; 10 | 11 | public TreeNode(int x){ val = x; } 12 | } 13 | public boolean isSubtree(TreeNode s, TreeNode t, boolean found){ 14 | 15 | if(s == null) return false; 16 | if(isSame(s, t)) return true; 17 | 18 | return isSubtree(s.left, t) || isSubtree(s.right, t); 19 | } 20 | 21 | public boolean isSame(TreeNode s, TreeNode t){ 22 | 23 | if(s == null && t == null){ 24 | return true; 25 | } 26 | 27 | if(s == null || t == null){ 28 | return false; 29 | } 30 | 31 | if(s.val != t.val) return false; 32 | 33 | return isSame(s.left, t.left) && isSame(s.right, t.right); 34 | } 35 | 36 | } -------------------------------------------------------------------------------- /Greedy/Medium/TaskScheduler.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class TaskScheduler{ 4 | 5 | public int leastInterval(char[] tasks, int n){ 6 | 7 | int[] map = new int[26]; 8 | 9 | for(char ch : tasks) 10 | map[ch - 'A']++; 11 | 12 | Arrays.sort(map); 13 | int time = 0; 14 | 15 | while(map[25] > 0){ 16 | 17 | int i = 0; 18 | while(i <= n){ 19 | 20 | if(map[25] == 0) 21 | break; 22 | 23 | if(i > 0 && map[25 - i] > 0){ 24 | map[25 - i]--; 25 | } 26 | 27 | time++; 28 | i++; 29 | } 30 | 31 | Arrays.sort(map); 32 | } 33 | 34 | return time; 35 | } 36 | } 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /HashMap/Medium/LastStoneWt.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class LastStoneWt{ 4 | 5 | public int lastStoneWeight(int[] stones){ 6 | 7 | PriorityQueue q = new PriorityQueue<>(Collections.reverseOrder()); 8 | 9 | for(int i = 0;i < stones.length;i++){ 10 | q.add(stones[i]); 11 | } 12 | 13 | while(q.size() > 1){ 14 | 15 | int s1 = q.poll(); 16 | int s2 = q.poll(); 17 | 18 | q.add(s1 - s2); 19 | } 20 | 21 | return q.poll(); 22 | } 23 | 24 | class reverseComp implements Comparator{ 25 | 26 | @Override 27 | public int compare(Integer a, Integer b){ 28 | 29 | return (a > b ? -1 : 1); 30 | } 31 | } 32 | 33 | } -------------------------------------------------------------------------------- /Tree/Medium/SmallestStringForm.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class SmallestStringForm{ 4 | 5 | class Node{ 6 | 7 | int val; 8 | Node right; 9 | Node left; 10 | Node next; 11 | 12 | public Node(int x){ val = x; } 13 | } 14 | 15 | String ans = "~"; 16 | 17 | public void dfs(TreeNode root, StringBuilder sb){ 18 | 19 | if(root == null){ 20 | return; 21 | } 22 | 23 | sb.append((char)('a') + root.val); 24 | 25 | if(root.left == null && root.right == null){ 26 | 27 | sb.reverse(); 28 | String s = sb.toString(); 29 | sb.reverse(); 30 | 31 | if(s.compareTo(ans) < 0){ 32 | 33 | ans = s; 34 | } 35 | } 36 | 37 | dfs(root.left, sb); 38 | dfs(root.right, sb); 39 | sb.deleteCharAt(sb.length() - 1); 40 | } 41 | 42 | } -------------------------------------------------------------------------------- /Heap/Easy/KthLargest.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class KthLargest { 4 | 5 | PriorityQueue q; 6 | int k; 7 | 8 | public KthLargest(int k, int[] nums) { 9 | 10 | this.k = k; 11 | this.q = new PriorityQueue<>(k); 12 | 13 | for(int n : nums) 14 | q.offer(n); 15 | } 16 | 17 | public int add(int val) { 18 | 19 | if(q.size() < k){ 20 | q.offer(val); 21 | } else if(q.peek() < val){ 22 | q.poll(); 23 | q.offer(val); 24 | } 25 | 26 | return q.peek(); 27 | } 28 | } 29 | 30 | /** 31 | * Your KthLargest object will be instantiated and called as such: 32 | * KthLargest obj = new KthLargest(k, nums); 33 | * int param_1 = obj.add(val); 34 | */ -------------------------------------------------------------------------------- /Heap/Easy/LastStoneWt.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class LastStoneWt{ 4 | 5 | PriorityQueue q; 6 | int k; 7 | 8 | public KthLargest(int k, int[] nums) { 9 | 10 | this.k = k; 11 | this.q = new PriorityQueue<>(k); 12 | 13 | for(int n : nums) 14 | q.offer(n); 15 | } 16 | 17 | public int add(int val) { 18 | 19 | if(q.size() < k){ 20 | q.offer(val); 21 | } else if(q.peek() < val){ 22 | q.poll(); 23 | q.offer(val); 24 | } 25 | 26 | return q.peek(); 27 | } 28 | } 29 | 30 | /** 31 | * Your KthLargest object will be instantiated and called as such: 32 | * KthLargest obj = new KthLargest(k, nums); 33 | * int param_1 = obj.add(val); 34 | */ -------------------------------------------------------------------------------- /LinkedList/Easy/RemoveDuplicates.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class RemoveDuplicates{ 4 | 5 | public static void main(String[] args){ 6 | 7 | 8 | } 9 | 10 | static class ListNode{ 11 | 12 | int val; 13 | ListNode Next; 14 | 15 | public ListNode(int x){ this.val = x; } 16 | } 17 | 18 | public static ListNode remove(ListNode head){ 19 | 20 | if(head == null || head.next == null){ 21 | return head; 22 | } 23 | 24 | ListNode temp = head; 25 | ListNode prev = head; 26 | ListNode curr = prev.next; 27 | 28 | while(curr != null){ 29 | 30 | if(prev.val != curr.val){ 31 | prev.next = curr; 32 | prev = curr; 33 | } 34 | 35 | curr = curr.next; 36 | } 37 | 38 | prev.next = curr; 39 | 40 | return temp; 41 | } 42 | 43 | 44 | } -------------------------------------------------------------------------------- /Stack/Medium/StockSpanner.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class StockSpanner { 4 | 5 | Stack prices, weights; 6 | public StockSpanner() { 7 | 8 | this.prices = new Stack<>(); 9 | this.weights = new Stack<>(); 10 | } 11 | 12 | public int next(int price) { 13 | 14 | int w = 1; 15 | 16 | while(!prices.isEmpty() && prices.peek() <= price){ 17 | this.prices.pop(); 18 | w += this.weights.pop(); 19 | } 20 | 21 | this.prices.push(price); 22 | this.weights.push(w); 23 | 24 | return w; 25 | } 26 | 27 | } 28 | 29 | /** 30 | * Your StockSpanner object will be instantiated and called as such: 31 | * StockSpanner obj = new StockSpanner(); 32 | * int param_1 = obj.next(price); 33 | */ -------------------------------------------------------------------------------- /Backtracking/Medium/combine.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class combine{ 4 | 5 | public static void main(String[] args){ 6 | 7 | // String s = "a*"; 8 | // String p = "aa"; 9 | int n = 3; 10 | System.out.println(comb(n,1)); 11 | 12 | } 13 | 14 | public static String comb(int n, int k){ 15 | 16 | List> combs = new ArrayList<>(); 17 | dfs(n, k); 18 | 19 | return combs; 20 | } 21 | 22 | public static void dfs(List> combs, List comb, int start, int n, int k){ 23 | 24 | if(k == 0){ 25 | combs.add(new ArrayList<>(comb)); 26 | }else{ 27 | 28 | for(int i = start;i <= n;i++){ 29 | 30 | comb.add(i); 31 | dfs(combs, comb, i + 1, n, k - 1); 32 | comb.remove(comb.size() - 1); 33 | } 34 | } 35 | } 36 | } -------------------------------------------------------------------------------- /Tree/Easy/RootToLeaf.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class RootToLeaf{ 4 | 5 | class Node{ 6 | 7 | int val; 8 | Node right; 9 | Node left; 10 | Node next; 11 | 12 | public Node(int x){ val = x; } 13 | } 14 | 15 | int total = 0; 16 | 17 | public int sumNumbers(TreeNode root){ 18 | 19 | helper(root, 0); 20 | return total; 21 | } 22 | 23 | 24 | public void helper(TreeNode root, int val){ 25 | 26 | if(root == null){ 27 | return ; 28 | } 29 | 30 | val += root.val; 31 | 32 | if(root.left == null && root.right == null){ 33 | 34 | total += val; 35 | return; 36 | } 37 | 38 | if(root.left != null){ 39 | helper(root.left, val * 10); 40 | } 41 | 42 | if(root.right != null) 43 | helper(root.right, val * 10); 44 | 45 | } 46 | 47 | } -------------------------------------------------------------------------------- /Tree/Easy/IncreasingOrderSearch.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class IncreasingOrderSearch{ 4 | 5 | class TreeNode{ 6 | 7 | int val; 8 | TreeNode right; 9 | TreeNode left; 10 | 11 | public TreeNode(int x){ val = x; } 12 | } 13 | 14 | TreeNode curr; 15 | 16 | public TreeNode increasingBST(TreeNode root){ 17 | 18 | TreeNode ans = new TreeNode(0); 19 | curr = ans; 20 | Inorder(root); 21 | return ans.right; 22 | } 23 | 24 | public void Inorder(TreeNode root){ 25 | 26 | if(root == null){ 27 | return; 28 | } 29 | 30 | Inorder(root.left); 31 | 32 | root.left = null; 33 | curr.right = root; 34 | curr = root; 35 | 36 | Inorder(root.right); 37 | } 38 | 39 | } -------------------------------------------------------------------------------- /Tree/Medium/KthSmallest.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class KthSmallest{ 4 | 5 | class Node{ 6 | 7 | int val; 8 | Node right; 9 | Node left; 10 | Node next; 11 | 12 | public Node(int x){ val = x; } 13 | } 14 | 15 | int total = 0; 16 | 17 | public int sumNumbers(TreeNode root){ 18 | 19 | helper(root, 0); 20 | return total; 21 | } 22 | 23 | 24 | public void helper(TreeNode root, int val){ 25 | 26 | if(root == null){ 27 | return ; 28 | } 29 | 30 | val += root.val; 31 | 32 | if(root.left == null && root.right == null){ 33 | 34 | total += val; 35 | return; 36 | } 37 | 38 | if(root.left != null){ 39 | helper(root.left, val * 10); 40 | } 41 | 42 | if(root.right != null) 43 | helper(root.right, val * 10); 44 | 45 | } 46 | 47 | } -------------------------------------------------------------------------------- /Divide and Conquer/Medium/searchMatrix2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class searchMatrix2{ 4 | 5 | 6 | public static void main(String[] args){ 7 | 8 | 9 | int[][] nums = {{},{}}; 10 | int k = 1; 11 | 12 | System.out.println(search(nums)); 13 | 14 | } 15 | 16 | public static boolean search(int[][] matrix, int item){ 17 | 18 | 19 | if(matrix == null || matrix.length < 1 || matrix[0].length < 1){ 20 | 21 | return false; 22 | } 23 | 24 | int row = 0; 25 | int col = matrix[row].length; 26 | 27 | while(row < matrix.length && col >= 0){ 28 | 29 | if(matrix[row][col] < item){ 30 | row++; 31 | }else if(matrix[row][col] < item){ 32 | col--; 33 | }else{ 34 | return true; 35 | } 36 | } 37 | 38 | return false; 39 | 40 | 41 | } 42 | 43 | } -------------------------------------------------------------------------------- /Greedy/Medium/SplitIntoConsecutiveSubseq.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class SplitIntoConsecutiveSubseq{ 4 | 5 | public int leastInterval(char[] tasks, int n){ 6 | 7 | int[] map = new int[26]; 8 | 9 | for(char ch : tasks) 10 | map[ch - 'A']++; 11 | 12 | Arrays.sort(map); 13 | int time = 0; 14 | 15 | while(map[25] > 0){ 16 | 17 | int i = 0; 18 | while(i <= n){ 19 | 20 | if(map[25] == 0) 21 | break; 22 | 23 | if(i > 0 && map[25 - i] > 0){ 24 | map[25 - i]--; 25 | } 26 | 27 | time++; 28 | i++; 29 | } 30 | 31 | Arrays.sort(map); 32 | } 33 | 34 | return time; 35 | } 36 | } 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /Bit Manipulation/Medium/SlidingWindowMax.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class SlidingWindowMax{ 4 | 5 | public int[] maxSlidingWindow(int[] nums, int k){ 6 | 7 | Deque q = new LinkedList<>(); 8 | int[] res = new int[nums.length - k + 1]; 9 | int i = 0; 10 | int idx = 0; 11 | for(i = 0;i < k;i++){ 12 | 13 | while(!q.isEmpty() && arr[i] >= arr[q.peekLast()]){ 14 | q.removeLast(); 15 | } 16 | 17 | q.addLast(i); 18 | } 19 | 20 | for(;i < nums.length;i++){ 21 | 22 | res[idx++] = arr[q.peek()]; 23 | 24 | while(!q.isEmpty() && q.peek() <= i - k){ 25 | q.removeFirst(); 26 | } 27 | 28 | while(!q.isEmpty() && arr[i] >= arr[q.peekLast()]){ 29 | q.removeLast(); 30 | } 31 | 32 | q.addLast(i); 33 | } 34 | 35 | return res; 36 | } 37 | } 38 | 39 | -------------------------------------------------------------------------------- /LinkedList/Easy/RemoveElement.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class RemoveElement{ 4 | 5 | public static void main(String[] args){ 6 | 7 | 8 | } 9 | 10 | static class ListNode{ 11 | 12 | int val; 13 | ListNode Next; 14 | 15 | public ListNode(int x){ this.val = x; } 16 | } 17 | 18 | public static ListNode remove(ListNode head int val){ 19 | 20 | if(head == null){ 21 | return head; 22 | } 23 | 24 | ListNode temp = new ListNode(0); 25 | temp.next = head; 26 | ListNode prev = temp; 27 | ListNode curr = head; 28 | 29 | while(curr != null){ 30 | 31 | if(curr.val != val){ 32 | 33 | prev.next = curr; 34 | prev = curr; 35 | } 36 | 37 | curr = curr.next; 38 | } 39 | 40 | prev.next = curr; 41 | 42 | 43 | return temp.next; 44 | } 45 | 46 | 47 | } -------------------------------------------------------------------------------- /HashMap/Easy/IntersectionOfTwoArrays.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class IntersectionOfTwoArrays{ 4 | 5 | public int[] intersect(int[] nums1, int[] nums2){ 6 | 7 | 8 | ArrayList ans = new ArrayList<>(); 9 | HashMap map = new HashMap<>(); 10 | 11 | for(int num : nums1) 12 | map.put(num, map.getOrDefault(num, 0) + 1); 13 | 14 | for(int num : nums2){ 15 | 16 | if(map.containsKey(num)){ 17 | ans.add(num); 18 | map.put(num, map.get(num) - 1); 19 | } 20 | 21 | if(map.get(num) < 1){ 22 | map.remove(num); 23 | } 24 | } 25 | int[] res = new int[ans.size()]; 26 | 27 | for(int i = 0;i < ans.size();i++){ 28 | res[i] = ans.get(i); 29 | } 30 | 31 | return res; 32 | 33 | } 34 | } 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /HashMap/Easy/numOfBoomerang.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class numOfBoomerang{ 4 | 5 | public int numberOfBoomerangs(int[][] points){ 6 | 7 | int res = 0; 8 | HashMap map = new HashMap<>(); 9 | 10 | for(int i = 0; i < points.length;i++){ 11 | for(int j = 0;j < points.length;j++){ 12 | 13 | if(i == j) continue; 14 | 15 | int d = distance(points[i], points[j]); 16 | map.put(d, map.getOrDefault(d, 0) + 1); 17 | 18 | } 19 | } 20 | 21 | for(int val : map.values()){ 22 | 23 | res += val*(val - 1); 24 | } 25 | 26 | map.clear(); 27 | return res; 28 | 29 | } 30 | 31 | public int distance(int[] a, int[] b){ 32 | 33 | return a[0]*a[0] - b[0]*b[0]; 34 | } 35 | } 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /Queue/Hard/SubarrayAtleastK.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class SubarrayAtleastK{ 4 | 5 | 6 | public static void main(String[] args){ 7 | 8 | String preorder = ""; 9 | 10 | 11 | } 12 | 13 | public static int shortestSubarray(int[] A, int k){ 14 | 15 | int N = A.length; 16 | int res = N + 1; 17 | 18 | int[] B = new int[N + 1]; 19 | 20 | for(int i = 0;i < A.length;i++){ 21 | B[i + 1] = B[i] + A[i]; 22 | } 23 | 24 | Deque d = new ArrayDeque<>(); 25 | 26 | for(int i = 0;i < A.length;i++){ 27 | 28 | while(d.size() > 0 && B[i] - B[d.getFirst()] >= k){ 29 | 30 | res = Math.min(res, i - d.pollFirst()); 31 | } 32 | 33 | while(d.size() > 0 && B[i] <= B[d.getLast()]) 34 | d.pollLast(); 35 | 36 | d.addLast(i); 37 | } 38 | 39 | return res <= N ? res : -1; 40 | } 41 | } -------------------------------------------------------------------------------- /LinkedList/Medium/SwapInPairs.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class SwapInPairs{ 4 | 5 | public static void main(String[] args){ 6 | 7 | 8 | } 9 | 10 | static class ListNode{ 11 | 12 | int val; 13 | ListNode Next; 14 | 15 | public ListNode(int x){ this.val = x; } 16 | } 17 | 18 | public static ListNode SwapPairs(ListNode head){ 19 | 20 | 21 | if(head == null){ 22 | return head; 23 | } 24 | 25 | ListNode prev = head; 26 | ListNode curr = prev.next; 27 | 28 | while(curr != null){ 29 | 30 | ListNode next = curr.next; 31 | 32 | int temp = prev.val; 33 | prev.val = curr.val; 34 | curr.val = temp; 35 | 36 | prev = next; 37 | 38 | if(next != null){ 39 | curr = next.next; 40 | }else{ 41 | curr = null; 42 | } 43 | } 44 | 45 | return head; 46 | 47 | } 48 | 49 | } -------------------------------------------------------------------------------- /Stack/Medium/DecodeAtIndex.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class DecodeAtIndex{ 4 | 5 | 6 | public static void main(String[] args){ 7 | 8 | String preorder = ""; 9 | 10 | 11 | } 12 | 13 | public static int decode(String S, int K){ 14 | 15 | long size = 0; 16 | 17 | for(int i = 0;i < S.length();i++){ 18 | 19 | if(Character.isDigit(S.charAt(i))){ 20 | 21 | size *= S.charAt(i) - '0'; 22 | }else{ 23 | size++; 24 | } 25 | 26 | } 27 | 28 | for(int i = S.length() - 1; i >= 0;i--){ 29 | 30 | char ch = str.charAt(i); 31 | K = K % size; 32 | 33 | if(K == 0 && !Character.isDigit(ch)){ 34 | return ch.toString(); 35 | } 36 | 37 | if(Character.isDigit(ch)){ 38 | 39 | size /= ch - '0'; 40 | }else{ 41 | size--; 42 | } 43 | } 44 | 45 | return "Yo"; 46 | 47 | 48 | } 49 | } -------------------------------------------------------------------------------- /Arrays/Easy/MissingNumber.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class MissingNumber{ 4 | 5 | public static void main(String[] args){ 6 | 7 | Scanner scn = new Scanner(System.in); 8 | 9 | int n = scn.nextInt(); 10 | int[] arr = new int[n]; 11 | 12 | for(int i = 0;i < n;i++){ 13 | 14 | arr[i] = scn.nextInt(); 15 | } 16 | 17 | System.out.println(check(arr)); 18 | } 19 | 20 | public static void display(int[] arr){ 21 | 22 | for(int i = 0 ;i < arr.length;i++){ 23 | System.out.print(arr[i] +" "); 24 | } 25 | System.out.println(); 26 | } 27 | 28 | public static int check(int[] nums){ 29 | 30 | int n = nums.length; 31 | int orig_sum = n * (n + 1)/2; 32 | 33 | int curr_sum = 0; 34 | for(int i = 0;i < n;i++){ 35 | curr_sum += nums[i]; 36 | } 37 | 38 | return orig_sum - curr_sum; 39 | } 40 | 41 | } -------------------------------------------------------------------------------- /Tree/Medium/ConstMaxBT.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class ConstMaxBT{ 4 | 5 | class Node{ 6 | 7 | int val; 8 | Node right; 9 | Node left; 10 | Node next; 11 | 12 | public Node(int x){ val = x; } 13 | } 14 | 15 | public TreeNode constructMaximumBinaryTree(int[] nums){ 16 | 17 | return construct(nums, 0, nums.length - 1); 18 | } 19 | 20 | public TreeNode construct(int[] nums, int nlo, int nhi){ 21 | 22 | if(nlo > nhi){ 23 | return null; 24 | } 25 | 26 | int idx = -1; 27 | int max = Integer.MIN_VALUE; 28 | for(int i = nlo;i <= nhi;i++){ 29 | 30 | if(nums[i] > max){ 31 | max = nums[i]; 32 | idx = i; 33 | } 34 | } 35 | 36 | TreeNode nn = new TreeNode(nums[idx]); 37 | 38 | nn.left = construct(nums, nlo, idx - 1); 39 | nn.right = construct(nums, idx + 1, nhi); 40 | 41 | return nn; 42 | } 43 | } -------------------------------------------------------------------------------- /Tree/Medium/FlipBinaryTree.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class FlipBinaryTree{ 4 | 5 | class Node{ 6 | 7 | int val; 8 | Node right; 9 | Node left; 10 | Node next; 11 | 12 | public Node(int x){ val = x; } 13 | } 14 | 15 | int i = 0; 16 | List ans = new ArrayList<>(); 17 | 18 | public List flipMatchVoyage(TreeNode root, int[] voyage){ 19 | 20 | return (dfs(root,voyage) ? ans : Arrays.asList(-1)); 21 | } 22 | 23 | public boolean dfs(TreeNode root, int[] voyage){ 24 | 25 | if(root == null) return true; 26 | if( root.val != voyage[i]) return false; 27 | 28 | if(root.left != null && root.left != root.val){ 29 | 30 | ans.add(root.left.val); 31 | 32 | return dfs(root.right, voyage) && dfs(root.left, voyage); 33 | } 34 | 35 | return dfs(root.left, voyage) && dfs(root.right, voyage); 36 | } 37 | } -------------------------------------------------------------------------------- /HashMap/Easy/MyHashMap.java: -------------------------------------------------------------------------------- 1 | class MyHashMap { 2 | 3 | /** Initialize your data structure here. */ 4 | public MyHashMap() { 5 | 6 | } 7 | 8 | /** value will always be non-negative. */ 9 | public void put(int key, int value) { 10 | 11 | } 12 | 13 | /** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */ 14 | public int get(int key) { 15 | 16 | } 17 | 18 | /** Removes the mapping of the specified value key if this map contains a mapping for the key */ 19 | public void remove(int key) { 20 | 21 | } 22 | } 23 | 24 | /** 25 | * Your MyHashMap object will be instantiated and called as such: 26 | * MyHashMap obj = new MyHashMap(); 27 | * obj.put(key,value); 28 | * int param_2 = obj.get(key); 29 | * obj.remove(key); 30 | */ -------------------------------------------------------------------------------- /Heap/Medium/SortByFrequency.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class SortByFrequency{ 4 | 5 | 6 | 7 | public String frequencySort(String S){ 8 | 9 | HashMap map = new HashMap<>(); 10 | 11 | for(char ch : S.toCharArray()){ 12 | map.put(ch, map.getOrDefault(ch, 0) + 1); 13 | } 14 | 15 | PriorityQueue pq = new PriorityQueue<>(); 16 | 17 | StringBuilder sb = new StringBuilder(); 18 | 19 | for(char ch : map.keySet()){ 20 | 21 | pq.offer(ch); 22 | } 23 | 24 | while(!pq.isEmpty()){ 25 | 26 | char ch = pq.poll(); 27 | int cnt = map.get(ch); 28 | 29 | while(cnt != 0){ 30 | sb.append(ch); 31 | cnt--; 32 | } 33 | 34 | } 35 | 36 | return sb.toString(); 37 | 38 | } 39 | } -------------------------------------------------------------------------------- /LinkedList/Medium/Rotate.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Rotate{ 4 | 5 | public static void main(String[] args){ 6 | 7 | 8 | } 9 | 10 | static class ListNode{ 11 | 12 | int val; 13 | ListNode Next; 14 | 15 | public ListNode(int x){ this.val = x; } 16 | } 17 | 18 | public static ListNode rotateList(ListNode head, int n){ 19 | 20 | if(head = null){ 21 | return head; 22 | } 23 | 24 | ListNode dummy = new Node(0); 25 | dummy.next = head; 26 | 27 | ListNode fast = dummy; 28 | ListNode slow = dummy; 29 | 30 | int i 0; 31 | for(;fast.next != null;i++){ 32 | 33 | fast = fast.next; 34 | } 35 | 36 | for(int j = i - n % i;j > 0;j--){ 37 | 38 | slow = slow.next; 39 | } 40 | 41 | fast.next = dummy.next; 42 | dummy.next = slow.next; 43 | slow.next = null; 44 | 45 | return dummy.next; 46 | } 47 | 48 | 49 | 50 | } -------------------------------------------------------------------------------- /Stack/Easy/RemoveOuterParenthesis.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class RemoveOuterParenthesis{ 4 | 5 | 6 | public static void main(String[] args){ 7 | 8 | 9 | } 10 | 11 | public static int Remove(String str){ 12 | 13 | Stack stack = new Stack<>(); 14 | StringBuilder sb = new StringBuilder(); 15 | StringBuilder res = new StrinBuilder(); 16 | for(int i = 0;i < str.length();i++){ 17 | 18 | char ch = str.charAt(i); 19 | 20 | if(ch == '('){ 21 | 22 | sb.append(ch); 23 | stack.push(ch); 24 | }else{ 25 | 26 | sb.append(ch); 27 | stack.pop(); 28 | } 29 | 30 | if(stack.isEmpty()){ 31 | 32 | sb.removeAt(0); 33 | sb.removeAt(sb.length() - 1); 34 | 35 | res.append(sb); 36 | sb = new StringBuilder(); 37 | 38 | } 39 | } 40 | 41 | return sb.toString(); 42 | 43 | 44 | } 45 | 46 | 47 | 48 | } -------------------------------------------------------------------------------- /Greedy/Easy/WalkingRobotSimulation.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class WalkingRobotSimulation{ 4 | 5 | 6 | public int robotSim(int[] commands, int[][] obstacles){ 7 | 8 | Set set = new HashSet<>(); 9 | 10 | for(int[] obs : obstacles){ 11 | 12 | set.add(obs[0]+" "+obs[1]); 13 | } 14 | 15 | int[][] dirs = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; 16 | int x = 0; 17 | int y = 0; 18 | int res = 0; 19 | for(int c : commands){ 20 | 21 | if(c == -2){ 22 | 23 | d = d % dirs.length; 24 | }else if(c == -1){ 25 | 26 | d = (d > 0 ? d-- : dirs.length - 1); 27 | }else{ 28 | 29 | while(c-- > 0 && !set.contains((x + dirs[d][0])+" "+ (y + dirs[d][1]))){ 30 | 31 | x += dirs[d][0]; 32 | y += dirs[d][1]; 33 | } 34 | } 35 | 36 | res = Math.max(res, x * x + y * y); 37 | } 38 | 39 | return res; 40 | } 41 | } 42 | 43 | -------------------------------------------------------------------------------- /Stack/Medium/ReversePolishNotation.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class ReversePolishNotation{ 4 | 5 | 6 | public static void main(String[] args){ 7 | 8 | 9 | } 10 | 11 | public static int calculate(String[] str){ 12 | 13 | Stack st = new Stack<>(); 14 | 15 | for(int i = 0;i < str.length;i++){ 16 | 17 | String op = str[i]; 18 | 19 | if(!op.equals("+") && !op.equals("-") && !op.equals("*") && !op.equals("/")){ 20 | 21 | st.push(Integer.parseInt(op)); 22 | continue; 23 | } 24 | 25 | int n1 = st.pop(); 26 | int n2 = st.pop(); 27 | 28 | if(op.equals("+")){ 29 | 30 | st.push(n1 + n2); 31 | }else if(op.equals("-")){ 32 | st.push(n1 - n2); 33 | }else if(op.equals("*")){ 34 | st.push(n1 * n2); 35 | }else{ 36 | st.push(n1 / n2); 37 | } 38 | } 39 | 40 | return st.pop(); 41 | } 42 | 43 | 44 | } -------------------------------------------------------------------------------- /Two Pointers/Medium/BoatsToSave.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.awt.Point; 3 | public class BoatsToSave{ 4 | 5 | public static void main(String[] args){ 6 | 7 | Scanner scn = new Scanner(System.in); 8 | 9 | int[] people = {3,5,3,4}; 10 | 11 | System.out.println(numRescueBoats(people,5)); 12 | 13 | } 14 | 15 | public static void display(int[] arr){ 16 | 17 | for(int i = 0 ;i < arr.length;i++){ 18 | System.out.print(arr[i] +" "); 19 | } 20 | System.out.println(); 21 | } 22 | 23 | public static int numRescueBoats(int[] people, int limit){ 24 | 25 | Arrays.sort(people); 26 | 27 | int i = 0; 28 | int j = people.length - 1; 29 | int cnt = 0; 30 | 31 | while(i <= j){ 32 | 33 | cnt++; 34 | if(people[i] + people[j] <= limit){ 35 | i++; 36 | } 37 | 38 | j--; 39 | } 40 | 41 | return cnt; 42 | } 43 | 44 | 45 | } -------------------------------------------------------------------------------- /Arrays/Easy/Lgst_Cont_Inc_Sub.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Lgst_Cont_Inc_Sub{ 4 | 5 | public static void main(String[] args){ 6 | 7 | Scanner scn = new Scanner(System.in); 8 | 9 | int[] arr = {2,2,2,2,2}; 10 | int k = 4; 11 | // display2(arr); 12 | 13 | System.out.println(findLengthOfLCIS(arr)); 14 | } 15 | 16 | public static void display(int[] arr){ 17 | 18 | 19 | for(int i = 0 ;i < arr.length;i++){ 20 | System.out.print(arr[i] +" "); 21 | } 22 | System.out.println(); 23 | } 24 | 25 | public static int findLengthOfLCIS(int[] nums){ 26 | 27 | 28 | int anchor = -1; 29 | int max = 0; 30 | for(int i = 0;i < nums.length;i++){ 31 | 32 | if(i > 0 && nums[i - 1] >= nums[i]) 33 | anchor = i; 34 | 35 | max = Math.max(max,i - anchor + 1); 36 | } 37 | 38 | return max; 39 | } 40 | 41 | 42 | 43 | 44 | } 45 | -------------------------------------------------------------------------------- /Arrays/Easy/Sort_parity.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Sort_parity{ 4 | 5 | public static void main(String[] args){ 6 | 7 | Scanner scn = new Scanner(System.in); 8 | 9 | int[] arr ={3, 1, 2, 4}; 10 | int k = 4; 11 | // display2(arr); 12 | 13 | display(sortArrayByParity(arr)); 14 | } 15 | 16 | public static void display(int[] arr){ 17 | 18 | 19 | for(int i = 0 ;i < arr.length;i++){ 20 | System.out.print(arr[i] +" "); 21 | } 22 | System.out.println(); 23 | } 24 | 25 | public static int[] sortArrayByParity(int[] nums){ 26 | 27 | int i = 0; 28 | int j = 0; 29 | 30 | while(j < nums.length){ 31 | 32 | if(nums[j] % 2 == 0){ 33 | int temp = nums[i]; 34 | nums[i] = nums[j]; 35 | nums[j] = temp; 36 | i++; 37 | } 38 | 39 | j++; 40 | } 41 | 42 | return nums; 43 | } 44 | 45 | 46 | 47 | 48 | } 49 | -------------------------------------------------------------------------------- /Arrays/Medium/PeakElement.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class PeakElement{ 4 | 5 | public static void main(String[] args){ 6 | 7 | Scanner scn = new Scanner(System.in); 8 | 9 | // int n = scn.nextInt(); 10 | int[] arr = {1, 2, 3, 1}; 11 | int target = 8; 12 | System.out.println(findPeakElement(arr)); 13 | } 14 | 15 | public static void display(int[] arr){ 16 | 17 | for(int i = 0 ;i < arr.length;i++){ 18 | System.out.print(arr[i] +" "); 19 | } 20 | System.out.println(); 21 | } 22 | 23 | public static int findPeakElement(int[] arr){ 24 | 25 | if(arr.length == 1){ 26 | return 0; 27 | } 28 | int i = 0; 29 | int peak = -1; 30 | 31 | while(i < arr.length - 1){ 32 | 33 | if(arr[i] > arr[i + 1]){ 34 | peak = i; 35 | return peak; 36 | } 37 | 38 | i++; 39 | } 40 | return i; 41 | } 42 | 43 | 44 | 45 | } -------------------------------------------------------------------------------- /Tree/Easy/LeafSimilar.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class LeafSimilar{ 4 | 5 | class TreeNode{ 6 | 7 | int val; 8 | TreeNode right; 9 | TreeNode left; 10 | 11 | public TreeNode(int x){ val = x; } 12 | } 13 | 14 | public boolean isSimilar(TreeNode root1, TreeNode root2){ 15 | 16 | ArrayList seq1 = new ArrayList<>(); 17 | ArrayList seq2 = new ArrayList<>(); 18 | dfs(root1, seq1); 19 | dfs(root2, seq2); 20 | 21 | return seq1.equals(seq2); 22 | 23 | } 24 | 25 | public void dfs(TreeNode root, ArrayList seq){ 26 | 27 | if(root == null){ 28 | return; 29 | } 30 | 31 | if(root.left == null && root.right == null){ 32 | seq.add(root); 33 | } 34 | 35 | dfs(root.left,seq); 36 | dfs(root.right, seq); 37 | } 38 | 39 | } -------------------------------------------------------------------------------- /HashMap/Medium/SubarraySumDivbyK.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class SubarraySumDivbyK{ 4 | 5 | 6 | public int subarrayDivbyK(int[] A, int k){ 7 | 8 | HashMap map = new HashMap<>(); 9 | 10 | map.put(0, 1); 11 | int sum = 0, count = 0; 12 | 13 | for(int a : A){ 14 | sum = (sum + a) % k; 15 | 16 | if(sum < 0){ 17 | sum += k; 18 | } 19 | 20 | count += map.getOrDefault(sum, 0); 21 | map.put(sum, map.getOrDefault(sum, 0) + 1); 22 | } 23 | 24 | return count; 25 | } 26 | 27 | public int subarrayDivbyK_EFF(int[] A, int k){ 28 | 29 | int[] map = new int[k]; 30 | 31 | map[0] = 1; 32 | int sum = 0, count = 0; 33 | 34 | for(int a : A){ 35 | sum = (sum + a) % k; 36 | 37 | if(sum < 0){ 38 | sum += k; 39 | } 40 | 41 | count += map[sum]; 42 | map[sum]++; 43 | } 44 | 45 | return count; 46 | } 47 | 48 | } -------------------------------------------------------------------------------- /Tree/Easy/TiltTree.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class TiltTree{ 4 | 5 | class TreeNode{ 6 | 7 | int val; 8 | TreeNode right; 9 | TreeNode left; 10 | 11 | public TreeNode(int x){ val = x; } 12 | } 13 | 14 | class HeapMover{ 15 | int sum = 0; 16 | int tilt = 0; 17 | } 18 | 19 | public int findTilt(TreeNode root){ 20 | 21 | HeapMover hm = new HeapMover(); 22 | 23 | return findTilt(root, hm).tilt; 24 | } 25 | 26 | public HeapMover findTilt(TreeNode root, HeapMover mover){ 27 | 28 | 29 | if(root == null){ 30 | return new HeapMover(); 31 | } 32 | 33 | HeapMover lp = findTilt(root.left, mover); 34 | HeapMover rp = findTilt(root.right, mover); 35 | 36 | HeapMover sp = new HeapMover(); 37 | sp.sum = lp.sum + rp.sum + root.val; 38 | sp.tilt = Math.abs(lp.sum - rp.sum) + lp.tilt + rp.tilt; 39 | 40 | return sp; 41 | } 42 | 43 | 44 | } -------------------------------------------------------------------------------- /Arrays/Easy/MoveZeros.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class MoveZeros{ 4 | 5 | public static void main(String[] args){ 6 | 7 | Scanner scn = new Scanner(System.in); 8 | 9 | int n = scn.nextInt(); 10 | int[] arr = new int[n]; 11 | 12 | for(int i = 0;i < n;i++){ 13 | 14 | arr[i] = scn.nextInt(); 15 | } 16 | 17 | System.out.println(move(arr)); 18 | } 19 | 20 | public static void display(int[] arr){ 21 | 22 | for(int i = 0 ;i < arr.length;i++){ 23 | System.out.print(arr[i] +" "); 24 | } 25 | System.out.println(); 26 | } 27 | 28 | public static int move(int[] nums){ 29 | 30 | int i = 0; 31 | int j = 0; 32 | 33 | while(j < nums.length){ 34 | 35 | if(nums[j] != 0){ 36 | 37 | int temp = nums[i]; 38 | nums[i] = nums[j]; 39 | nums[j] = temp; 40 | i++; 41 | 42 | } 43 | 44 | 45 | j++; 46 | 47 | 48 | } 49 | } 50 | 51 | } -------------------------------------------------------------------------------- /Heap/Medium/KpairsWithSmallestSum.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class KpairsWithSmallestSum{ 4 | 5 | 6 | 7 | public List kSmallestPairs(int[] nums1, int[] nums2, int k){ 8 | 9 | 10 | PriorityQueue pq = new PriorityQueue<>((a, b) -> a[0] + a[1] - b[0] - b[1]); 11 | List ans = new ArraList<>(); 12 | 13 | if(nums1.length == 0 || nums2.length == 0 || k == 0) return ans; 14 | 15 | for(int i = 0;i < nums.length && i < k;i++) pq.offer(new int[]{nums1[i], nums2[0], 0}); 16 | 17 | while(k -- > 0 && !pq.isEmpty()){ 18 | 19 | int[] cur = pq.poll(); 20 | ans.add(new int[]{cur[0], cur[1]}); 21 | 22 | if(curr[2] == nums.length - 1){ 23 | continue; 24 | } 25 | 26 | pq.offer(new int[]{cur[0], nums2[cur[2] + 1], cur2[2] + 1}); 27 | 28 | } 29 | 30 | return ans; 31 | } 32 | } -------------------------------------------------------------------------------- /Stack/Medium/AsteroidCollision.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class AsteroidCollision{ 4 | 5 | 6 | public static void main(String[] args){ 7 | 8 | String preorder = ""; 9 | 10 | 11 | } 12 | 13 | public static int[] collision(int[] asteroids){ 14 | 15 | Stack stack = new Stack<>(); 16 | 17 | for(int ast : asteroids){ 18 | 19 | Collision: { 20 | while(!stack.isEmpty() && ast < 0 && stack.peek() > 0){ 21 | 22 | if(stack.peek() < - ast){ 23 | stack.pop(); 24 | continue; 25 | }else if(stack.peek() == - ast){ 26 | stack.pop(); 27 | } 28 | 29 | break Collision; 30 | } 31 | 32 | stack.push(ast); 33 | } 34 | } 35 | 36 | 37 | int[] res = new int[stack.size()]; 38 | int i = stack.size() - 1; 39 | while(!stack.isEmpty()){ 40 | 41 | res[i--] = stack.pop(); 42 | } 43 | 44 | return res; 45 | } 46 | } -------------------------------------------------------------------------------- /Arrays/Easy/ContainsDuplicate.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class ContainsDuplicate{ 4 | 5 | public static void main(String[] args){ 6 | 7 | Scanner scn = new Scanner(System.in); 8 | 9 | int n = scn.nextInt(); 10 | int[] arr = new int[n]; 11 | 12 | for(int i = 0;i < n;i++){ 13 | 14 | arr[i] = scn.nextInt(); 15 | } 16 | 17 | System.out.println(check(arr)); 18 | } 19 | 20 | public static void display(int[] arr){ 21 | 22 | for(int i = 0 ;i < arr.length;i++){ 23 | System.out.print(arr[i] +" "); 24 | } 25 | System.out.println(); 26 | } 27 | 28 | public static boolean check(int[] nums){ 29 | 30 | HashMap map = new HashMap<>(); 31 | 32 | for(int i = 0; i < nums.length;i++){ 33 | 34 | if(map.containsKey(nums[i])){ 35 | return true; 36 | }else{ 37 | map.put(nums[i],1); 38 | } 39 | } 40 | 41 | return false; 42 | } 43 | 44 | } -------------------------------------------------------------------------------- /LinkedList/Medium/InsertionSort.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class InsertionSort{ 4 | 5 | public static void main(String[] args){ 6 | 7 | 8 | } 9 | 10 | static class ListNode{ 11 | 12 | int val; 13 | ListNode Next; 14 | 15 | public ListNode(int x){ this.val = x; } 16 | } 17 | 18 | public static ListNode sort(ListNode head){ 19 | 20 | if(head == null || head.next == null){ 21 | 22 | return head; 23 | } 24 | 25 | ListNode fakeHead = new ListNode(0); 26 | ListNode prev = fakeHead; 27 | ListNode curr = head; 28 | ListNode next = null; 29 | 30 | while(curr != null){ 31 | 32 | next = curr.next; 33 | 34 | while(prev.next != null && prev.next.val < curr.val){ 35 | prev = prev.next; 36 | } 37 | 38 | curr.next = prev.next; 39 | prev.next = curr; 40 | prev = fakeHead; 41 | curr = next; 42 | } 43 | 44 | return fakeHead.next; 45 | } 46 | 47 | 48 | } -------------------------------------------------------------------------------- /Stack/Medium/Kdigits.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Kdigits{ 4 | 5 | 6 | public static void main(String[] args){ 7 | 8 | String preorder = ""; 9 | 10 | 11 | } 12 | 13 | public static String remove(String strm, int k){ 14 | 15 | 16 | Stack stack = new Stack<>(); 17 | 18 | for(int i = 0;i < str.length();i++){ 19 | 20 | char ch = str.charAt(i); 21 | 22 | while(!stack.isEmpty() && stack.peek() > ch){ 23 | stack.pop(); 24 | k--; 25 | } 26 | 27 | stack.push(ch); 28 | } 29 | 30 | while(!stack.isEmpty() && k > 0){ 31 | stack.pop(); 32 | k--; 33 | } 34 | 35 | StringBuilder sb = new StringBuilder(); 36 | 37 | for(!stack.isEmpty()){ 38 | 39 | sb.append(stack.pop()); 40 | } 41 | 42 | sb.reverse(); 43 | 44 | while(sb.length() > 1 && sb.charAt(0) == '0'){ 45 | sb.deleteCharAt(0); 46 | } 47 | 48 | return sb.toString(); 49 | } 50 | } -------------------------------------------------------------------------------- /Tree/Medium/AddOneToRow.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class AddOneToRow{ 4 | 5 | class Node{ 6 | 7 | int val; 8 | Node right; 9 | Node left; 10 | Node next; 11 | 12 | public Node(int x){ val = x; } 13 | } 14 | 15 | public TreeNode addOneRow(TreeNode root, int v, int d){ 16 | 17 | if(d == 1){ 18 | 19 | TreeNode head = new TreeNode(v); 20 | head.left = root; 21 | return head; 22 | } 23 | 24 | dfs(root, v, 1, d); 25 | 26 | } 27 | 28 | public void dfs(TreeNode root, int v, int d, int n){ 29 | 30 | if(root == null){ 31 | return; 32 | } 33 | 34 | if(d + 1 == n){ 35 | TreeNode t = root.left; 36 | root.left = new TreeNode(v); 37 | root.left.left = t; 38 | t = root.right; 39 | root.right = new TreeNode(v); 40 | root.right.right = t; 41 | 42 | return; 43 | } 44 | 45 | dfs(root.left, v, d + 1, n); 46 | dfs(root.right, v, d + 1, n); 47 | } 48 | 49 | } -------------------------------------------------------------------------------- /Arrays/Easy/MaxOnes.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class MaxOnes{ 4 | 5 | public static void main(String[] args){ 6 | 7 | Scanner scn = new Scanner(System.in); 8 | 9 | int n = scn.nextInt(); 10 | int[] arr = new int[n]; 11 | 12 | for(int i = 0;i < n;i++){ 13 | 14 | arr[i] = scn.nextInt(); 15 | } 16 | 17 | System.out.println(cnt(arr)); 18 | 19 | } 20 | 21 | public static void display(int[] arr){ 22 | 23 | for(int i = 0 ;i < arr.length;i++){ 24 | System.out.print(arr[i] +" "); 25 | } 26 | System.out.println(); 27 | } 28 | 29 | public static int cnt(int[] nums){ 30 | 31 | int maxCount = 0; 32 | int count = 0; 33 | 34 | 35 | for(int i = 0;i < nums.length;i++){ 36 | 37 | if(nums[i] == 1){ 38 | count++; 39 | }else{ 40 | count = 0; 41 | } 42 | 43 | if(count > maxCount){ 44 | maxCount = count; 45 | } 46 | } 47 | 48 | return maxCount; 49 | } 50 | } -------------------------------------------------------------------------------- /Arrays/Easy/bit_character.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class bit_character{ 4 | 5 | public static void main(String[] args){ 6 | 7 | Scanner scn = new Scanner(System.in); 8 | 9 | int n = scn.nextInt(); 10 | int[] arr = new int[n]; 11 | 12 | for(int i = 0;i < n;i++){ 13 | 14 | arr[i] = scn.nextInt(); 15 | } 16 | 17 | System.out.println(isOneBitCharacter(arr)); 18 | } 19 | 20 | public static void display(int[] arr){ 21 | 22 | for(int i = 0 ;i < arr.length;i++){ 23 | System.out.print(arr[i] +" "); 24 | } 25 | System.out.println(); 26 | } 27 | 28 | public static boolean isOneBitCharacter(int[] nums){ 29 | 30 | int category = 0; 31 | 32 | for(int i = 0;i < nums.length;){ 33 | 34 | if(nums[i] == 0){ 35 | 36 | category = 1; 37 | i++; 38 | }else if(nums[i] == 1){ 39 | category = 2; 40 | i += 2; 41 | } 42 | } 43 | 44 | return category == 1; 45 | } 46 | 47 | } -------------------------------------------------------------------------------- /LinkedList/Medium/ReverseLL2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class ReverseLL2{ 4 | 5 | public static void main(String[] args){ 6 | 7 | 8 | } 9 | 10 | static class ListNode{ 11 | 12 | int val; 13 | ListNode Next; 14 | 15 | public ListNode(int x){ this.val = x; } 16 | } 17 | 18 | public static reverse(ListNode head, int beg, int end){ 19 | 20 | ListNode curr = head, prev = null; 21 | 22 | while(beg > 1){ 23 | 24 | prev = curr; 25 | curr = curr.next; 26 | 27 | beg--; 28 | end--; 29 | } 30 | 31 | ListNode corner = prev, tail = curr; 32 | ListNode third = null; 33 | while(n > 0){ 34 | 35 | third = curr.next; 36 | curr.next = prev; 37 | prev = curr; 38 | curr = third; 39 | n--; 40 | } 41 | 42 | 43 | if(corner != null) 44 | corner.next = prev; 45 | else 46 | head = prev; 47 | 48 | tail.next = curr; 49 | 50 | return head; 51 | 52 | 53 | } 54 | 55 | 56 | } -------------------------------------------------------------------------------- /Stack/Easy/BaseBallGame.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class BaseBallGame{ 4 | 5 | 6 | public static void main(String[] args){ 7 | 8 | } 9 | 10 | public static int calculatePoints(String[] str){ 11 | 12 | 13 | Stack stack = new Stack<>(); 14 | 15 | for(int i = 0;i < str.length;i++){ 16 | 17 | 18 | String op = str[i]; 19 | int item = Integer.parseInt(op); 20 | 21 | if(op.equals("+")){ 22 | 23 | int a = Integer.parseInt(stack.pop()); 24 | int b = Integer.parseInt(stack.pop()); 25 | 26 | item = a + b; 27 | stack.push(item); 28 | }else if(op.equals("D")){ 29 | 30 | int a = Integer.parseInt(stack.pop()); 31 | item = 2 * a; 32 | stack.push(item); 33 | }else if(op.equals("C")){ 34 | 35 | int a = Integer.parseInt(stack.pop()); 36 | 37 | }else{ 38 | stack.push(item); 39 | } 40 | } 41 | 42 | return stack.pop(); 43 | } 44 | 45 | 46 | 47 | 48 | } -------------------------------------------------------------------------------- /Arrays/Easy/Sort_parity_2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Sort_parity_2{ 4 | 5 | public static void main(String[] args){ 6 | 7 | Scanner scn = new Scanner(System.in); 8 | 9 | int[] arr ={3, 1, 2, 4}; 10 | int k = 4; 11 | // display2(arr); 12 | 13 | display(sortArrayByParityII(arr)); 14 | } 15 | 16 | public static void display(int[] arr){ 17 | 18 | 19 | for(int i = 0 ;i < arr.length;i++){ 20 | System.out.print(arr[i] +" "); 21 | } 22 | System.out.println(); 23 | } 24 | 25 | public static int[] sortArrayByParityII(int[] nums){ 26 | 27 | int i = 0; 28 | int j = 0; 29 | 30 | while(j < nums.length){ 31 | 32 | if((i % 2 == 0 && nums[j] % 2 == 0) || (i % 2 == 1 && nums[j] % 2 == 1)){ 33 | int temp = nums[i]; 34 | nums[i] = nums[j]; 35 | nums[j] = temp; 36 | i++; 37 | } 38 | 39 | j++; 40 | } 41 | 42 | return nums; 43 | } 44 | 45 | 46 | 47 | 48 | } 49 | -------------------------------------------------------------------------------- /Tree/Medium/FindSubTreeSum.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class FindSubTreeSum{ 4 | 5 | class Node{ 6 | 7 | int val; 8 | Node right; 9 | Node left; 10 | Node next; 11 | 12 | public Node(int x){ val = x; } 13 | } 14 | 15 | HashMap count = new HashMap<>(); 16 | int maxCount = 0; 17 | 18 | public int[] findFrequentTreeSum(TreeNode root){ 19 | 20 | dfs(root); 21 | 22 | List res = new ArrayList<>(); 23 | for(int s : count.keySet()){ 24 | 25 | if(count.get(s) == maxCount){ 26 | res.add(s); 27 | } 28 | } 29 | 30 | return res.stream().mapToInt(i -> i).toArray(); 31 | } 32 | 33 | public void dfs(TreeNode root){ 34 | 35 | if(root == null) 36 | return; 37 | 38 | int sum = dfs(root.left) + dfs(root.right) + root.val; 39 | count.put(sum, count.getOrDefault(sum, 0) + 1); 40 | 41 | maxCount = Math.max(maxCount, count.get(sum)); 42 | return sum; 43 | } 44 | 45 | } -------------------------------------------------------------------------------- /Arrays/Medium/DuplicateNumber.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class DuplicateNumber{ 4 | 5 | public static void main(String[] args){ 6 | 7 | Scanner scn = new Scanner(System.in); 8 | 9 | // int n = scn.nextInt(); 10 | int[] arr = {3, 1, 3, 4, 2}; 11 | int target = 7; 12 | System.out.println(findDuplicate(arr)); 13 | display(arr); 14 | // display(productExceptSelf_Eff(arr)); 15 | } 16 | 17 | public static void display(int[] arr){ 18 | 19 | for(int i = 0 ;i < arr.length;i++){ 20 | System.out.print(arr[i] +" "); 21 | } 22 | System.out.println(); 23 | } 24 | 25 | public static int findDuplicate(int[] nums) { 26 | 27 | for(int i = 0;i < nums.length;i++){ 28 | 29 | int val = Math.abs(nums[i]); 30 | 31 | if(nums[val - 1] < 0){ 32 | return val; 33 | }else{ 34 | nums[val - 1] = -nums[val - 1]; 35 | } 36 | } 37 | 38 | 39 | return -1; 40 | } 41 | 42 | 43 | } -------------------------------------------------------------------------------- /Arrays/Medium/ProductLessThanK.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class ProductLessThanK{ 4 | 5 | public static void main(String[] args){ 6 | 7 | Scanner scn = new Scanner(System.in); 8 | 9 | int[] arr = {1, 2}; 10 | int target = 2; 11 | System.out.println(numSubarrayProductLessThanK(arr,target)); 12 | } 13 | 14 | public static void display(int[] arr){ 15 | 16 | for(int i = 0 ;i < arr.length;i++){ 17 | System.out.print(arr[i] +" "); 18 | } 19 | System.out.println(); 20 | } 21 | 22 | public static int numSubarrayProductLessThanK(int[] nums, int k) { 23 | 24 | 25 | if(k <= 1) 26 | return 0; 27 | 28 | int prod = 1, ans = 0, left = 0; 29 | 30 | for(int right = 0;right < nums.length;right++){ 31 | 32 | prod *= nums[right]; 33 | 34 | while(prod >= k){ 35 | prod /= nums[left++]; 36 | } 37 | 38 | ans += right - left + 1; 39 | } 40 | 41 | return ans; 42 | 43 | } 44 | } -------------------------------------------------------------------------------- /DP/Easy/CountBits.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class CountBits{ 4 | 5 | public static void main(String[] args){ 6 | 7 | int[] nums = {2,7,9,3,1}; 8 | System.out.println(robbed(nums)); 9 | 10 | } 11 | 12 | public static int count(int n){ 13 | 14 | 15 | int cnt = 0; 16 | while(n != 0){ 17 | 18 | int rem = n % 2; 19 | cnt += (rem == 1 ? rem : 0); 20 | n /= 2; 21 | } 22 | 23 | return cnt; 24 | } 25 | 26 | public static int[] CountBit(int n){ 27 | 28 | int[] arr = new int[n]; 29 | 30 | for(int i = 1;i < n;i++){ 31 | 32 | arr[i] = count(i); 33 | } 34 | return arr; 35 | } 36 | 37 | public static int[] CountBit_eff(int n){ 38 | 39 | int pow = 1; 40 | int[] ret = new int[n + 1]; 41 | 42 | for(int i = 1, t = 0;i <= num; i++, t++){ 43 | 44 | if(pow == i){ 45 | 46 | pow *= 2; 47 | t = 0; 48 | } 49 | 50 | ret[t] = ret[t] + 1; 51 | } 52 | 53 | return ret; 54 | } 55 | } -------------------------------------------------------------------------------- /LinkedList/Medium/DeleteDuplicate.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class DeleteDuplicate{ 4 | 5 | public static void main(String[] args){ 6 | 7 | 8 | } 9 | 10 | static class ListNode{ 11 | 12 | int val; 13 | ListNode Next; 14 | 15 | public ListNode(int x){ this.val = x; } 16 | } 17 | 18 | public static ListNode delete(ListNode head){ 19 | 20 | if(head == null || head.next == null){ 21 | return head; 22 | } 23 | 24 | ListNode Fakehead = new ListNode(0); 25 | Fakehead.next = head; 26 | 27 | ListNode prev = Fakehead; 28 | ListNode curr = head; 29 | 30 | while(curr != null){ 31 | 32 | 33 | while(curr.next != null && curr.val != curr.next.val){ 34 | curr = curr.next; 35 | } 36 | 37 | if(prev.next == curr){ 38 | prev = prev.next; 39 | }else{ 40 | 41 | prev.next = curr.next; 42 | } 43 | 44 | curr = curr.next; 45 | } 46 | 47 | return Fakehead.next; 48 | } 49 | 50 | 51 | } -------------------------------------------------------------------------------- /Tree/Medium/RecoverBT.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class RecoverBT{ 4 | 5 | class Node{ 6 | 7 | int val; 8 | Node right; 9 | Node left; 10 | Node next; 11 | 12 | public Node(int x){ val = x; } 13 | } 14 | 15 | TreeNode fidx; 16 | TreeNode sidx; 17 | TreeNode prev = new TreeNode(Integer.MIN_VALUE); 18 | 19 | public void recoverTree(TreeNode root){ 20 | 21 | if(root == null){ 22 | return; 23 | } 24 | 25 | inorder(root); 26 | 27 | int temp = fidx.val; 28 | fidx.val = sidx.val; 29 | sidx.val = temp; 30 | 31 | } 32 | 33 | public void inorder(TreeNode root){ 34 | 35 | if(root == null){ 36 | return; 37 | } 38 | 39 | inorder(root.left); 40 | if(fidx == null && prev.val > root.val){ 41 | fidx = prev; 42 | } 43 | 44 | if(fidx != null && prev.val > root.val){ 45 | sidx = root; 46 | } 47 | 48 | prev = root; 49 | 50 | inorder(root.right); 51 | } 52 | 53 | 54 | 55 | 56 | } -------------------------------------------------------------------------------- /Arrays/Easy/RemoveDuplicates.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class RemoveDuplicates{ 4 | 5 | public static void main(String[] args){ 6 | 7 | Scanner scn = new Scanner(System.in); 8 | 9 | int n = scn.nextInt(); 10 | int[] arr = new int[n]; 11 | 12 | for(int i = 0;i < n;i++){ 13 | 14 | arr[i] = scn.nextInt(); 15 | } 16 | 17 | System.out.println(removeDuplicates(arr)); 18 | display(arr); 19 | 20 | } 21 | 22 | public static void display(int[] arr){ 23 | 24 | for(int i = 0 ;i < arr.length;i++){ 25 | System.out.print(arr[i] +" "); 26 | } 27 | System.out.println(); 28 | } 29 | 30 | public static int removeDuplicates(int[] nums){ 31 | 32 | int i = 0,j = 1; 33 | 34 | while(i < nums.length && j < nums.length){ 35 | 36 | if(i < nums.length - 1 && nums[i] != nums[j]){ 37 | 38 | nums[i + 1] = nums[j]; 39 | i++; 40 | } 41 | 42 | j++; 43 | } 44 | 45 | return i + 1; 46 | 47 | } 48 | } -------------------------------------------------------------------------------- /Backtracking/Hard/prmtSeq.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class prmtSeq{ 4 | 5 | public static void main(String[] args){ 6 | 7 | 8 | // String s = "a*"; 9 | // String p = "aa"; 10 | int n = 3; 11 | System.out.println(prmt(n,1)); 12 | 13 | } 14 | 15 | public static String prmt(int n, int k){ 16 | 17 | List numbers = new ArrayList<>(); 18 | int[] fact = new int[n + 1]; 19 | StringBuilder sb = new StringBuilder(); 20 | 21 | int sum = 1; 22 | fact[0] = 1; 23 | 24 | for(int i = 1;i <= n;i++){ 25 | 26 | sum *= i; 27 | fact[i] = sum; 28 | } 29 | 30 | for(int i = 1;i <= n;i++){ 31 | numbers.add(i); 32 | } 33 | 34 | k--; 35 | 36 | for(int i = 1;i <= n;i++){ 37 | 38 | int idx = k/(fact[n - i]); 39 | sb.append(String.valuesOf(numbers.get(idx))); 40 | numbers.remove(idx); 41 | 42 | k -= (idx)*fact[n - i]; 43 | } 44 | 45 | return String.valuesOf(sb); 46 | } 47 | 48 | 49 | 50 | } -------------------------------------------------------------------------------- /Greedy/Medium/reconstructQueue.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class reconstructQueue{ 4 | 5 | public String removeKdigits(String str, int k){ 6 | 7 | Stack stack = new Stack<>(); 8 | 9 | for(int i = 0;i < str.length();i++){ 10 | 11 | char ch = str.charAt(i); 12 | 13 | while(!stack.isEmpty() && stack.peek() > ch && k > 0){ 14 | stack.pop(); 15 | } 16 | 17 | stack.push(ch); 18 | } 19 | 20 | while(!stack.isEmpty() && k > 0){ 21 | stack.pop(); 22 | } 23 | 24 | StringBuilder sb = new StringBuilder(); 25 | 26 | while(!stack.isEmpty()){ 27 | 28 | sb.insert(0, stack.pop()); 29 | } 30 | 31 | while(sb.length() > 1 && sb.charAt(0) == '0'){ 32 | sb.deleteCharAt(0); 33 | } 34 | 35 | return sb.toString(); 36 | 37 | } 38 | } 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /Tree/Easy/LongestUniversalPath.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class LongestUniversalPath{ 4 | 5 | class TreeNode{ 6 | 7 | int val; 8 | TreeNode right; 9 | TreeNode left; 10 | 11 | public TreeNode(int x){ val = x; } 12 | } 13 | 14 | int ans = 0; 15 | 16 | public int longestPath(TreeNode root){ 17 | 18 | if(root == null){ 19 | return 0; 20 | } 21 | 22 | int lmax = longestPath(root.left); 23 | int rmax = longestPath(root.right); 24 | 25 | int tempLeft = 0; 26 | int tempRight = 0; 27 | if(root.left != null && root.left.val == root.val){ 28 | 29 | tempLeft += lmax + 1; 30 | } 31 | 32 | if(root.right != null && root.right.val == root.val){ 33 | 34 | tempRight += rmax + 1; 35 | } 36 | 37 | ans = Math.max(ans, tempRight + tempLeft); 38 | 39 | return Math.max(lmax, rmax); 40 | } 41 | 42 | } -------------------------------------------------------------------------------- /Tree/Medium/SubTreeDeepestNode.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class SubTreeDeepestNode{ 4 | 5 | class Node{ 6 | 7 | int val; 8 | Node right; 9 | Node left; 10 | Node next; 11 | 12 | public Node(int x){ val = x; } 13 | } 14 | 15 | public TreeNode subtreeWithAllDeepest(TreeNode root){ 16 | 17 | 18 | return dfs(root).node; 19 | } 20 | 21 | class Result{ 22 | 23 | int dist; 24 | TreeNode node; 25 | 26 | public Result(TreeNode n, int d){ this.dist = d; this.node = n; } 27 | } 28 | 29 | public Resut dfs(TreeNode root){ 30 | 31 | if(root == null){ 32 | return new Result(null, 0); 33 | } 34 | 35 | Result L = dfs(node.left); 36 | Result R = dfs(node.right); 37 | 38 | if(L.dist > R.dist){ 39 | return new Result(root.left, L.dist + 1); 40 | } 41 | 42 | if(L.dist < R.dist){ 43 | return new Result(root.right, R.dist + 1); 44 | } 45 | 46 | return new Result(root, L.dist + 1); 47 | } 48 | 49 | } -------------------------------------------------------------------------------- /Arrays/Medium/TeemoAttacking.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class TeemoAttacking{ 4 | 5 | public static void main(String[] args){ 6 | 7 | Scanner scn = new Scanner(System.in); 8 | 9 | // int n = scn.nextInt(); 10 | int[] arr = {1, 2}; 11 | int target = 2; 12 | System.out.println(findPoisonedDuration(arr,target)); 13 | // display(productExceptSelf_Eff(arr)); 14 | } 15 | 16 | public static void display(int[] arr){ 17 | 18 | for(int i = 0 ;i < arr.length;i++){ 19 | System.out.print(arr[i] +" "); 20 | } 21 | System.out.println(); 22 | } 23 | 24 | public static int findPoisonedDuration(int[] timeSeries, int duration) { 25 | 26 | if(timeSeries.length == 0) 27 | return 0; 28 | 29 | int res = duration; 30 | 31 | for(int i = 1;i < nums.length;i++){ 32 | 33 | res += timeSeries[i - 1] + duration - 1 < timeSeries[i] ? duration : timeSeries[i] - timeSeries[i - 1]; 34 | } 35 | 36 | return res; 37 | } -------------------------------------------------------------------------------- /Arrays/Easy/Largest_number_twice.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Largest_number_twice{ 4 | 5 | public static void main(String[] args){ 6 | 7 | Scanner scn = new Scanner(System.in); 8 | 9 | int[] arr = {1, 2, 3, 4}; 10 | int k = 4; 11 | // display2(arr); 12 | 13 | System.out.println(dominantIndex(arr)); 14 | } 15 | 16 | public static void display(int[] arr){ 17 | 18 | 19 | for(int i = 0 ;i < arr.length;i++){ 20 | System.out.print(arr[i] +" "); 21 | } 22 | System.out.println(); 23 | } 24 | 25 | public static int dominantIndex(int[] nums){ 26 | 27 | int max_idx= 0; 28 | for(int i = 0;i < nums.length;i++){ 29 | 30 | if(nums[max_idx] < nums[i]){ 31 | 32 | max_idx = i; 33 | } 34 | } 35 | 36 | for(int i = 0;i < nums.length;i++){ 37 | 38 | if(nums[i] != nums[max_idx] && 2*nums[i] > nums[max_idx]){ 39 | return -1; 40 | } 41 | } 42 | return max_idx; 43 | 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /Arrays/Easy/Mountain_Array.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Mountain_Array{ 4 | 5 | public static void main(String[] args){ 6 | 7 | Scanner scn = new Scanner(System.in); 8 | 9 | int[] arr ={0, 3, 2, 1}; 10 | int k = 4; 11 | 12 | display(validMountainArray(arr)); 13 | } 14 | 15 | public static void display(int[] arr){ 16 | 17 | 18 | for(int i = 0 ;i < arr.length;i++){ 19 | System.out.print(arr[i] +" "); 20 | } 21 | System.out.println(); 22 | } 23 | 24 | public static int[] validMountainArray(int[] nums){ 25 | 26 | if(nums.length < 3){ 27 | return false; 28 | } 29 | 30 | int N = nums.length; 31 | int i = 0; 32 | 33 | while(i + 1 < N && nums[i] < nums[i + 1]){ 34 | j++; 35 | } 36 | 37 | if(i == 0 || i == N - 1){ 38 | return false; 39 | } 40 | 41 | while(i + 1 < N && nums[i] > nums[i + 1]){ 42 | i++; 43 | } 44 | 45 | return i == N - 1; 46 | } 47 | 48 | 49 | 50 | 51 | } 52 | -------------------------------------------------------------------------------- /Tree/Easy/cousinsTree.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class cousinsTree{ 4 | 5 | class TreeNode{ 6 | 7 | int val; 8 | TreeNode right; 9 | TreeNode left; 10 | 11 | public TreeNode(int x){ val = x; } 12 | } 13 | 14 | TreeNode xParent, yParent; 15 | int xDepth, yDepth; 16 | 17 | public boolean isCousins(TreeNode root, int x, int y){ 18 | 19 | getDepthAndParent(root,null,x,y,0); 20 | return (xDepth == yDepth && xParent != yParent); 21 | } 22 | 23 | public void getDepthAndParent(TreeNode root, TreeNode parent, int x, int y, int depth){ 24 | 25 | if(root == null){ 26 | return; 27 | } 28 | 29 | if(root.val == x){ 30 | 31 | xParent = parent; 32 | xDepth = depth; 33 | }else if(root.val == y){ 34 | 35 | yParent = parent; 36 | yDepth = depth; 37 | } 38 | 39 | getDepthAndParent(root.left, root, x, y, depth + 1); 40 | getDepthAndParent(root.right, root, x, y, depth + 1); 41 | } 42 | } -------------------------------------------------------------------------------- /Tree/Hard/RecoverBT.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class RecoverBT{ 4 | 5 | class Node{ 6 | 7 | int val; 8 | Node right; 9 | Node left; 10 | Node next; 11 | 12 | public Node(int x){ val = x; } 13 | } 14 | 15 | TreeNode fidx; 16 | TreeNode sidx; 17 | TreeNode prev = new TreeNode(Integer.MIN_VALUE); 18 | 19 | public void recoverTree(TreeNode root){ 20 | 21 | if(root == null){ 22 | return; 23 | } 24 | 25 | inorder(root); 26 | 27 | int temp = fidx.val; 28 | fidx.val = sidx.val; 29 | sidx.val = temp; 30 | 31 | return root; 32 | 33 | 34 | } 35 | 36 | public void inorder(TreeNode root){ 37 | 38 | if(root == null){ 39 | return; 40 | } 41 | 42 | inorder(root.left); 43 | if(fidx == null && prev.val > root.val){ 44 | fidx = prev; 45 | } 46 | 47 | if(fidx != null && prev.val > root.val){ 48 | sidx = root; 49 | } 50 | 51 | prev = root; 52 | 53 | inorder(root.right); 54 | } 55 | 56 | 57 | 58 | 59 | } -------------------------------------------------------------------------------- /Extra/wordBreak.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class wordBreak{ 4 | 5 | public static void main(String[] args){ 6 | 7 | // String s = "a*"; 8 | // String p = "aa"; 9 | int n = 3; 10 | System.out.println(breakIt(n, new Set<>())); 11 | 12 | } 13 | 14 | public static boolean breakIt(String s, Set dict){ 15 | 16 | 17 | // DFS 18 | Set set = new Set<>(); 19 | return dfs(s, 0, dict, set); 20 | 21 | } 22 | 23 | public static boolean dfs(String s, int idx, Set dict, Set set){ 24 | 25 | if(idx == s.length()){ 26 | return true; 27 | } 28 | 29 | if(set.contains(idx)){ 30 | return false; 31 | } 32 | 33 | for(int i = idx;i <= s.length();i++){ 34 | 35 | String t = s.substring(idx, i); 36 | 37 | if(dict.contains(t)){ 38 | 39 | if(dfs(s,i,dict,set)){ 40 | return true; 41 | }else{ 42 | set.add(i); 43 | } 44 | } 45 | } 46 | 47 | set.add(idx); 48 | return false; 49 | } 50 | } -------------------------------------------------------------------------------- /Tree/Easy/FlattenTree.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class FlattenTree{ 4 | 5 | class TreeNode{ 6 | 7 | int val; 8 | TreeNode right; 9 | TreeNode left; 10 | 11 | public TreeNode(int x){ val = x; } 12 | } 13 | 14 | public void flatten(TreeNode root){ 15 | 16 | Stack stack = new Stack<>(); 17 | 18 | stack.push(root); 19 | 20 | while(!stack.isEmpty()){ 21 | 22 | TreeNode curr = stack.pop(); 23 | 24 | if(curr.right != null) 25 | stack.push(curr.right); 26 | 27 | if(curr.left != null) 28 | stack.push(curr.left); 29 | 30 | if(!stack.isEmpty()) 31 | curr.right = stack.peek(); 32 | curr.left = null; 33 | } 34 | 35 | TreeNode prev = null; 36 | 37 | public void flattenRecur(TreeNode root){ 38 | 39 | if(root == null) 40 | return; 41 | 42 | flattenRecur(root.left); 43 | flattenRecur(root.right); 44 | 45 | root.right = prev; 46 | root.left = null; 47 | prev = root; 48 | } 49 | } 50 | 51 | } -------------------------------------------------------------------------------- /Arrays/Medium/AllDuplicateArray.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class AllDuplicateArray{ 4 | 5 | public static void main(String[] args){ 6 | 7 | Scanner scn = new Scanner(System.in); 8 | 9 | // int n = scn.nextInt(); 10 | int[] arr = {4,3,2,7,8,2,3,1}; 11 | int target = 7; 12 | System.out.println(findDuplicate(arr)); 13 | // display(productExceptSelf_Eff(arr)); 14 | } 15 | 16 | public static void display(int[] arr){ 17 | 18 | for(int i = 0 ;i < arr.length;i++){ 19 | System.out.print(arr[i] +" "); 20 | } 21 | System.out.println(); 22 | } 23 | 24 | public static Set findDuplicate(int[] nums) { 25 | 26 | Set ans = new HashSet<>(); 27 | 28 | for(int i = 0;i < nums.length;i++){ 29 | 30 | int val = Math.abs(nums[i]); 31 | 32 | if(nums[val - 1] < 0){ 33 | ans.add(val); 34 | }else{ 35 | nums[val - 1] = -nums[val - 1]; 36 | } 37 | } 38 | 39 | 40 | return ans; 41 | } 42 | 43 | 44 | } -------------------------------------------------------------------------------- /Tree/Easy/PopulateNextRightPointer.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class PopulateNextRightPointer{ 4 | 5 | class Node{ 6 | 7 | int val; 8 | Node right; 9 | Node left; 10 | Node next; 11 | 12 | public Node(int x){ val = x; } 13 | } 14 | 15 | public Node connect(Node root){ 16 | 17 | if(root == null) 18 | return root; 19 | 20 | Queue q = new LinkedList<>(); 21 | Queue helper = new LinkedList<>(); 22 | Node save = null; 23 | q.add(root); 24 | 25 | while(!q.isEmpty()){ 26 | 27 | Node rp = q.poll(); 28 | save = rp; 29 | if(q.isEmpty()){ 30 | rp.next = null; 31 | }else{ 32 | rp.next = q.peek(); 33 | } 34 | 35 | if(rp.left != null) 36 | helper.add(rp.left); 37 | if(rp.right != null) 38 | helper.add(rp.right); 39 | 40 | if(q.isEmpty()){ 41 | System.out.println(save.val); 42 | q = helper; 43 | helper = new LinkedList<>(); 44 | } 45 | } 46 | 47 | 48 | return root; 49 | } 50 | 51 | } -------------------------------------------------------------------------------- /Arrays/Easy/ContainsDuplicate2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class ContainsDuplicate2{ 4 | 5 | public static void main(String[] args){ 6 | 7 | Scanner scn = new Scanner(System.in); 8 | 9 | int n = scn.nextInt(); 10 | int[] arr = new int[n]; 11 | 12 | for(int i = 0;i < n;i++){ 13 | 14 | arr[i] = scn.nextInt(); 15 | } 16 | 17 | System.out.println(check(arr)); 18 | } 19 | 20 | public static void display(int[] arr){ 21 | 22 | for(int i = 0 ;i < arr.length;i++){ 23 | System.out.print(arr[i] +" "); 24 | } 25 | System.out.println(); 26 | } 27 | 28 | public static boolean check(int[] nums){ 29 | 30 | HashMap map = new HashMap<>(); 31 | 32 | for(int i = 0; i < nums.length;i++){ 33 | 34 | if(map.containsKey(nums[i])){ 35 | 36 | int val = map.get(nums[i]); 37 | 38 | if(Math.abs(i - j) <= k) 39 | return true; 40 | }else{ 41 | map.put(nums[i],i); 42 | } 43 | } 44 | 45 | return false; 46 | } 47 | 48 | } -------------------------------------------------------------------------------- /Arrays/Easy/RemoveElement.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class RemoveElement{ 4 | 5 | public static void main(String[] args){ 6 | 7 | Scanner scn = new Scanner(System.in); 8 | 9 | int n = scn.nextInt(); 10 | int[] arr = new int[n]; 11 | 12 | for(int i = 0;i < n;i++){ 13 | 14 | arr[i] = scn.nextInt(); 15 | } 16 | 17 | int val = scn.nextInt(); 18 | 19 | System.out.println(removeElement(arr,val)); 20 | display(arr); 21 | 22 | } 23 | 24 | public static void display(int[] arr){ 25 | 26 | for(int i = 0 ;i < arr.length;i++){ 27 | System.out.print(arr[i] +" "); 28 | } 29 | System.out.println(); 30 | } 31 | 32 | public static int removeElement(int[] nums,int val){ 33 | 34 | 35 | int i = 0,j = 0; 36 | 37 | int cnt = 0; 38 | 39 | while(i < nums.length && j < nums.length){ 40 | 41 | if(nums[j] == val){ 42 | }else{ 43 | nums[i] = nums[j]; 44 | i++; 45 | } 46 | 47 | j++; 48 | } 49 | 50 | return i; 51 | 52 | } 53 | } -------------------------------------------------------------------------------- /Tree/Hard/BinaryTreeCamera.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class BinaryTreeCamera{ 4 | 5 | class Node{ 6 | 7 | int val; 8 | Node right; 9 | Node left; 10 | Node next; 11 | 12 | public Node(int x){ val = x; } 13 | } 14 | 15 | public int minCameraCover(TreeNode root){ 16 | 17 | return helper(root); 18 | } 19 | 20 | public int helper(TreeNode root){ 21 | 22 | 23 | if(root == null){ 24 | return 0; 25 | } 26 | 27 | if(root.left == null && root.right == null){ 28 | return 1; 29 | } 30 | 31 | int inc = 1; 32 | 33 | if(root.left != null){ 34 | 35 | inc += helper(root.left.left); 36 | inc += helper(root.left.right); 37 | } 38 | 39 | if(root.right != null){ 40 | 41 | inc += helper(root.right.left); 42 | inc += helper(root.right.right); 43 | } 44 | 45 | int exc = 0; 46 | exc += helper(root.left, cameraLeft); 47 | exc += helper(root.right, cameraLeft); 48 | 49 | return Math.min(inc, exc); 50 | 51 | 52 | } 53 | 54 | 55 | } -------------------------------------------------------------------------------- /Tree/Easy/InvertTree.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class InvertTree{ 4 | 5 | class TreeNode{ 6 | 7 | int val; 8 | TreeNode right; 9 | TreeNode left; 10 | 11 | public TreeNode(int x){ val = x; } 12 | } 13 | 14 | public TreeNode mirror(TreeNode root){ 15 | 16 | 17 | if(root == null){ 18 | return null; 19 | } 20 | 21 | TreeNode left = mirror(root.left); 22 | TreeNode right = mirror(root.right); 23 | 24 | node.left = right; 25 | node.right = left; 26 | 27 | return node; 28 | } 29 | 30 | public TreeNode mirrotItr(TreeNode root){ 31 | 32 | Queue q = new LinkedList<>(); 33 | q.add(root); 34 | 35 | while(!q.isEmpty()){ 36 | 37 | TreeNode rp = q.poll(); 38 | 39 | TreeNode left = rp.left; 40 | rp.left = rp.right; 41 | rp.right = left; 42 | 43 | if(rp.left != null){ 44 | q.add(rp.left); 45 | } 46 | 47 | if(rp.right != null){ 48 | q.add(rp.right); 49 | } 50 | } 51 | 52 | return root; 53 | } 54 | 55 | } -------------------------------------------------------------------------------- /Tree/Medium/BottomLeft.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class BottomLeft{ 4 | 5 | class Node{ 6 | 7 | int val; 8 | Node right; 9 | Node left; 10 | Node next; 11 | 12 | public Node(int x){ val = x; } 13 | } 14 | 15 | public TreeNode max(TreeNode node){ 16 | 17 | if(node.right != null){ 18 | return max(node.right); 19 | } 20 | 21 | return node.val; 22 | } 23 | 24 | public void delete(TreeNode root, int key){ 25 | 26 | if(root == null){ 27 | return ; 28 | } 29 | 30 | if(root.val < key){ 31 | root.left = delete(root.right, key); 32 | }else if(root.val > key){ 33 | root.right = delete(root.left, key); 34 | }else{ 35 | 36 | if(root.left == null){ 37 | return root.right; 38 | }else if(root.right == null){ 39 | returnn root.left; 40 | } 41 | 42 | 43 | TreeNode temp = max(root.left); 44 | root.val = temp.val; 45 | root.left = delete(root.left, root.val); 46 | 47 | 48 | } 49 | 50 | return root; 51 | } 52 | 53 | } -------------------------------------------------------------------------------- /Tree/Medium/DeleteNode.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class DeleteNode{ 4 | 5 | class Node{ 6 | 7 | int val; 8 | Node right; 9 | Node left; 10 | Node next; 11 | 12 | public Node(int x){ val = x; } 13 | } 14 | 15 | public TreeNode max(TreeNode node){ 16 | 17 | if(node.right != null){ 18 | return max(node.right); 19 | } 20 | 21 | return node.val; 22 | } 23 | 24 | public void delete(TreeNode root, int key){ 25 | 26 | if(root == null){ 27 | return ; 28 | } 29 | 30 | if(root.val < key){ 31 | root.left = delete(root.right, key); 32 | }else if(root.val > key){ 33 | root.right = delete(root.left, key); 34 | }else{ 35 | 36 | if(root.left == null){ 37 | return root.right; 38 | }else if(root.right == null){ 39 | returnn root.left; 40 | } 41 | 42 | 43 | TreeNode temp = max(root.left); 44 | root.val = temp.val; 45 | root.left = delete(root.left, root.val); 46 | 47 | 48 | } 49 | 50 | return root; 51 | } 52 | 53 | } -------------------------------------------------------------------------------- /Arrays/Easy/Transpose.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Transpose{ 4 | 5 | public static void main(String[] args){ 6 | 7 | Scanner scn = new Scanner(System.in); 8 | 9 | int[][] arr ={{1,1,0},{1,0,1},{0,0,0}}; 10 | int k = 4; 11 | // display2(arr); 12 | 13 | display2(transpose(arr)); 14 | 15 | } 16 | 17 | public static void display2(int[][] arr){ 18 | 19 | System.out.print("["); 20 | for(int i = 0 ;i < arr.length;i++){ 21 | System.out.print("["); 22 | for(int j = 0;j < arr[0].length;j++) 23 | System.out.print(arr[i][j] +" "); 24 | System.out.print("]"); 25 | 26 | } 27 | System.out.println("]"); 28 | } 29 | 30 | public static int[][] transpose(int[][] nums){ 31 | 32 | int[][] ans = new int[nums[0].length][nums.length]; 33 | 34 | 35 | for(int i = 0;i < nums.length;i++){ 36 | for(int j = 0; j < nums[0].length;j++){ 37 | 38 | ans[j][i] = nums[i][j]; 39 | } 40 | } 41 | 42 | return ans; 43 | 44 | } 45 | 46 | 47 | } 48 | -------------------------------------------------------------------------------- /HashMap/Medium/ValidSudoku.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class ValidSudoku{ 4 | 5 | public boolean isValidSudoku(char[][] board){ 6 | 7 | 8 | 9 | for(int i = 0;i < 9;i++){ 10 | 11 | HashSet row = new HashSet<>(); 12 | HashSet col = new HashSet<>(); 13 | HashSet diag = new HashSet<>(); 14 | 15 | for(int j = 0;j < 9;j++){ 16 | 17 | if(board[i][j] != '.' && !row.add(board[i][j])){ 18 | return false; 19 | } 20 | 21 | if(board[j][i] != '.' && !col.add(board[j][i])){ 22 | return false; 23 | } 24 | 25 | int rowIndex = 3 * (i / 3); 26 | int colIndex = 3 * (i % 3); 27 | 28 | if(board[rowIndex + j / 3][colIndex + j % 3] != '.' && !diag.add(board[rowIndex + j / 3][colIndex + j % 3] )){ 29 | return false; 30 | } 31 | } 32 | } 33 | 34 | return true; 35 | } 36 | } 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /Tree/Easy/FindSecondMin.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class FindSecondMin{ 4 | 5 | class TreeNode{ 6 | 7 | int val; 8 | TreeNode right; 9 | TreeNode left; 10 | 11 | public TreeNode(int x){ val = x; } 12 | } 13 | 14 | public int secondMin(TreeNode root){ 15 | 16 | if(root == null){ 17 | return -1; 18 | } 19 | 20 | if(root.left == null && root.right == null){ 21 | return -1; 22 | } 23 | 24 | int left = root.left.val; 25 | int right = root.right.val; 26 | 27 | if(root.val == root.left.val){ 28 | left = secondMin(root.left); 29 | } 30 | 31 | if(root.val == root.right.val){ 32 | right = secondMin(root.right); 33 | } 34 | 35 | if(left != -1 && right != -1){ 36 | return Math.min(left, right); 37 | }else if(left != -1){ 38 | return left; 39 | }else{ 40 | return right; 41 | } 42 | } 43 | 44 | } -------------------------------------------------------------------------------- /Tree/Easy/LevelOrderGT.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class LevelOrderGT{ 4 | 5 | class Node{ 6 | 7 | int val; 8 | List _children; 9 | 10 | public Node(){} 11 | public Node(int x){ this.val = x;} 12 | } 13 | 14 | public List> levelOrder(Node root){ 15 | 16 | Queue q = new LinkedList<>(); 17 | List> res = new ArrayList<>(); 18 | 19 | if(root == null){ 20 | return res; 21 | } 22 | 23 | 24 | Queue helper = new LinkedList<>(); 25 | List level = new ArrayList<>(); 26 | q.add(root); 27 | 28 | while(!q.isEmpty()){ 29 | 30 | Node rp = q.remove(); 31 | level.add(rp.val); 32 | 33 | for(int i = 0;i < rp.children.size();i++){ 34 | helper.add(rp.children.get(i)); 35 | } 36 | 37 | if(q.isEmpty()){ 38 | 39 | res.add(level); 40 | level = new ArrayList<>(); 41 | q = helper; 42 | helper = new LinkedList<>(); 43 | } 44 | } 45 | 46 | return res; 47 | } 48 | 49 | 50 | 51 | 52 | } -------------------------------------------------------------------------------- /Two Pointers/Medium/Multiplicity3Sum.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.awt.Point; 3 | public class Multiplicity3Sum{ 4 | 5 | public static void main(String[] args){ 6 | 7 | Scanner scn = new Scanner(System.in); 8 | 9 | int[] tree = {1,1,2,2,3,3,4,4,5,5}; 10 | 11 | System.out.println(threeSumMulti(tree, 8)); 12 | 13 | } 14 | 15 | public static void display(int[] arr){ 16 | 17 | for(int i = 0 ;i < arr.length;i++){ 18 | System.out.print(arr[i] +" "); 19 | } 20 | System.out.println(); 21 | } 22 | 23 | public static int threeSumMulti(int[] A, int target){ 24 | 25 | HashMap map = new HashMap<>(); 26 | 27 | int res = 0; 28 | int mod = 1_000_000_007; 29 | 30 | for(int i = 0;i < A.length;i++){ 31 | 32 | res = (res + map.getOrDefault(target - A[i], 0) % mod); 33 | 34 | for(int j = 0; j < i;j++){ 35 | 36 | int temp = A[i] + A[j]; 37 | map.put(temp,map.getOrDefault(temp,0) + 1); 38 | } 39 | } 40 | 41 | return res; 42 | } 43 | 44 | 45 | } -------------------------------------------------------------------------------- /Arrays/Easy/Monotonic_Array.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Monotonic_Array{ 4 | 5 | public static void main(String[] args){ 6 | 7 | Scanner scn = new Scanner(System.in); 8 | 9 | int[] arr ={1,2,2,3}; 10 | int k = 4; 11 | // display2(arr); 12 | 13 | System.out.println(isMonotonic(arr)); 14 | } 15 | 16 | public static void display(int[] arr){ 17 | 18 | 19 | for(int i = 0 ;i < arr.length;i++){ 20 | System.out.print(arr[i] +" "); 21 | } 22 | System.out.println(); 23 | } 24 | 25 | public static boolean isMonotonic(int[] nums){ 26 | 27 | boolean isInc = false; 28 | boolean isDec = false; 29 | 30 | for(int i = 1;i < nums.length;i++){ 31 | 32 | if(nums[ i - 1] == nums[i]){ 33 | continue; 34 | }else if(nums[i - 1] < nums[i] && !isDec){ 35 | isInc = true; 36 | }else if(nums[i - 1] > nums[i] && !isInc){ 37 | isDec = true; 38 | }else{ 39 | return false; 40 | } 41 | } 42 | 43 | return true; 44 | } 45 | 46 | 47 | 48 | 49 | } 50 | -------------------------------------------------------------------------------- /HashMap/Medium/FractionToRecurrDecimal.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class FractionToRecurrDecimal{ 4 | 5 | public boolean isValidSudoku(char[][] board){ 6 | 7 | 8 | 9 | for(int i = 0;i < 9;i++){ 10 | 11 | HashSet row = new HashSet<>(); 12 | HashSet col = new HashSet<>(); 13 | HashSet diag = new HashSet<>(); 14 | 15 | for(int j = 0;j < 9;j++){ 16 | 17 | if(board[i][j] != '.' && !row.add(board[i][j])){ 18 | return false; 19 | } 20 | 21 | if(board[j][i] != '.' && !col.add(board[j][i])){ 22 | return false; 23 | } 24 | 25 | int rowIndex = 3 * (i / 3); 26 | int colIndex = 3 * (i % 3); 27 | 28 | if(board[rowIndex + j / 3][colIndex + j % 3] != '.' && !diag.add(board[rowIndex + j / 3][colIndex + j % 3] )){ 29 | return false; 30 | } 31 | } 32 | } 33 | 34 | return true; 35 | } 36 | } 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /Arrays/Medium/MaxBoundSubArray.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class MaxBoundSubArray{ 4 | 5 | public static void main(String[] args){ 6 | 7 | Scanner scn = new Scanner(System.in); 8 | 9 | // int n = scn.nextInt(); 10 | int[] arr = {1,0,2,3,4}; 11 | int target = 2; 12 | System.out.println(count(arr,2,4)); 13 | // display(productExceptSelf_Eff(arr)); 14 | } 15 | 16 | public static void display(int[] arr){ 17 | 18 | for(int i = 0 ;i < arr.length;i++){ 19 | System.out.print(arr[i] +" "); 20 | } 21 | System.out.println(); 22 | } 23 | 24 | public static int count(int[] nums,int L, int R) { 25 | 26 | 27 | int mins = 0; 28 | int maxIndex = 0; 29 | int res = 0; 30 | 31 | for(int i = 0;i < nums.length;i++){ 32 | 33 | if(nums[i] < L){ 34 | mins++; 35 | }else{ 36 | mins = 0; 37 | } 38 | 39 | if(nums[i] > R){ 40 | 41 | maxIndex = i; 42 | } 43 | 44 | res += (i - maxIndex - mins); 45 | 46 | } 47 | 48 | return res; 49 | } 50 | 51 | 52 | } -------------------------------------------------------------------------------- /Recursion/Medium/FullBinaryTree.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class FullBinaryTree{ 4 | 5 | 6 | public static void main(String[] args){ 7 | 8 | 9 | int n = 1; 10 | int k = 1; 11 | 12 | System.out.println(allpossibleBT(n,k)); 13 | 14 | } 15 | 16 | private class TreeNode{ 17 | 18 | int val; 19 | TreeNode left; 20 | TreeNode right; 21 | TreeNode(int x){ this.val = x;} 22 | } 23 | 24 | public List allpossibleBT(int N){ 25 | 26 | List res = new ArrayList<>(); 27 | 28 | if(N == 1){ 29 | res.add(new TreeNode(0)); 30 | return res; 31 | } 32 | 33 | N = N - 1; 34 | 35 | for(int i = 1;i < N; i+= 2){ 36 | 37 | List left = allpossibleBT(N); 38 | List right = allpossibleBT(N - 1); 39 | 40 | for(TreeNode nl : left){ 41 | for(TreeNode nr : right){ 42 | 43 | TreeNode cr = new TreeNode(0); 44 | cur.left = nl; 45 | cr.right = nr; 46 | res.add(cur); 47 | } 48 | } 49 | } 50 | 51 | return res; 52 | } 53 | 54 | 55 | } -------------------------------------------------------------------------------- /Arrays/Easy/Non_decreasing_Array.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Non_decreasing_Array{ 4 | 5 | public static void main(String[] args){ 6 | 7 | Scanner scn = new Scanner(System.in); 8 | 9 | int[] arr = {4, 4, 1}; 10 | int k = 4; 11 | // display2(arr); 12 | 13 | System.out.println(checkPossibility(arr)); 14 | } 15 | 16 | public static void display(int[] arr){ 17 | 18 | 19 | for(int i = 0 ;i < arr.length;i++){ 20 | System.out.print(arr[i] +" "); 21 | } 22 | System.out.println(); 23 | } 24 | 25 | public static boolean checkPossibility(int[] nums){ 26 | 27 | int p = -1; 28 | 29 | for(int i = 0;i < nums.length - 1;i++){ 30 | 31 | if(nums[i] > nums[i + 1]){ 32 | 33 | if(p != -1){ 34 | return false; 35 | } 36 | 37 | p = i; 38 | } 39 | 40 | } 41 | 42 | if(p == -1 || p == 0 || p == nums.length - 2 || nums[p - 1] <= nums[p + 1]|| nums[p] <= nums[p + 2]){ 43 | return true; 44 | } 45 | 46 | return false; 47 | 48 | } 49 | 50 | 51 | 52 | 53 | } 54 | -------------------------------------------------------------------------------- /HashMap/Hard/MaximalRectangle.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class MaximalRectangle{ 4 | 5 | public int maximalRectangle(int[][] matrix){ 6 | 7 | if(matrix == null || matrix.length || matrix[0].length == 0){ 8 | return 0; 9 | } 10 | 11 | int area = 0; 12 | int m = matrix.length; 13 | int n = matrix[0].length; 14 | 15 | int[] height = new int[n]; 16 | 17 | for(int row = 0;row < m;row++){ 18 | 19 | for(int col = 0;col < n;col++){ 20 | 21 | if(matrix[row][col] == 1){ 22 | h[col]++; 23 | }else{ 24 | h[col] = 0; 25 | } 26 | 27 | int height = h[col]; 28 | 29 | for(int j = col - 1; j >= 0;j--){ 30 | 31 | if(h[j] == 0){ 32 | break; 33 | } 34 | 35 | height = Math.min(height, h[j]); 36 | int width = col - j + 1; 37 | area = Math.max(area, height * width); 38 | } 39 | 40 | area = Math.max(area, h[col]); 41 | } 42 | } 43 | 44 | return area; 45 | } 46 | } -------------------------------------------------------------------------------- /HashMap/Hard/RandomPickWithBlackList.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class RandomPickWithBlackList { 4 | 5 | HashMap map; 6 | int M; 7 | Random r; 8 | 9 | public Solution(int N, int[] blacklist) { 10 | 11 | map = new HashMap<>(); 12 | 13 | for(int b : blacklist){ 14 | 15 | map.put(b, -1); 16 | } 17 | 18 | M = N - map.size(); 19 | 20 | for(int b : blacklist){ 21 | 22 | if(b < M){ 23 | 24 | while(map.containsKey(N - 1)) N--; 25 | 26 | map.put(b, N - 1); 27 | N--; 28 | } 29 | } 30 | 31 | r = new Random(); 32 | } 33 | 34 | public int pick() { 35 | 36 | int p = r.nextInt(M); 37 | 38 | if(map.containsKey(p)){ 39 | return map.get(p); 40 | } 41 | 42 | return p; 43 | 44 | } 45 | } 46 | 47 | /** 48 | * Your Solution object will be instantiated and called as such: 49 | * Solution obj = new Solution(N, blacklist); 50 | * int param_1 = obj.pick(); 51 | */ -------------------------------------------------------------------------------- /Recursion/Easy/LongesUniversalPath.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class LongesUniversalPath{ 4 | 5 | private class TreeNode{ 6 | 7 | 8 | int val; 9 | TreeNode left; 10 | TreeNode right; 11 | 12 | TreeNode(int x){ 13 | val = x; 14 | } 15 | } 16 | 17 | int ans = 0; 18 | public int longestUnivaluePath(TreeNode root) { 19 | 20 | return helper(root); 21 | } 22 | 23 | public int helper(TreeNode node){ 24 | 25 | if(node == null){ 26 | return 0; 27 | } 28 | 29 | 30 | int leftCount = helper(node.left); 31 | int rightCount = helper(node.right); 32 | 33 | int tempLeft = 0; 34 | int tempRight = 0; 35 | if(node.left != null && node.val == node.left.val){ 36 | 37 | tempLeft += leftCount + 1; 38 | }else if(node.right != null && node.val == node.right.val){ 39 | tempRight += rightCount + 1; 40 | } 41 | 42 | 43 | ans = Math.max(ans, tempLeft + tempRight); 44 | 45 | return Math.max(leftCount, rightCount); 46 | } 47 | 48 | } -------------------------------------------------------------------------------- /Tree/Easy/AverageLevels.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class AverageLevels{ 4 | 5 | class TreeNode{ 6 | 7 | int val; 8 | TreeNode right; 9 | TreeNode left; 10 | 11 | public TreeNode(int x){ val = x; } 12 | } 13 | 14 | public List average(TreeNode root){ 15 | 16 | List < Double > res = new ArrayList < > (); 17 | Queue < TreeNode > queue = new LinkedList < > (); 18 | queue.add(root); 19 | while (!queue.isEmpty()) { 20 | long sum = 0, count = 0; 21 | Queue < TreeNode > temp = new LinkedList < > (); 22 | while (!queue.isEmpty()) { 23 | TreeNode n = queue.remove(); 24 | sum += n.val; 25 | count++; 26 | if (n.left != null) 27 | temp.add(n.left); 28 | if (n.right != null) 29 | temp.add(n.right); 30 | } 31 | queue = temp; 32 | res.add(sum * 1.0 / count); 33 | } 34 | return res; 35 | } 36 | 37 | } -------------------------------------------------------------------------------- /Arrays/Easy/Maximum_Subarray.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Maximum_Subarray{ 4 | 5 | public static void main(String[] args){ 6 | 7 | Scanner scn = new Scanner(System.in); 8 | 9 | int n = scn.nextInt(); 10 | int[] arr = new int[n]; 11 | 12 | for(int i = 0;i < n;i++){ 13 | 14 | arr[i] = scn.nextInt(); 15 | } 16 | 17 | // int val = scn.nextInt(); 18 | 19 | System.out.println(maxSum(arr)); 20 | 21 | } 22 | 23 | public static void display(int[] arr){ 24 | 25 | for(int i = 0 ;i < arr.length;i++){ 26 | System.out.print(arr[i] +" "); 27 | } 28 | System.out.println(); 29 | } 30 | 31 | public static int maxSum(int[] nums){ 32 | 33 | int sum = Integer.MIN_VALUE; 34 | int curr_sum = 0; 35 | int si = 0; 36 | for(int ei = 0;ei < nums.length;ei++){ 37 | 38 | curr_sum += nums[ei]; 39 | 40 | if(curr_sum < 0){ 41 | curr_sum = 0; 42 | si = ei + 1; 43 | } 44 | 45 | if( curr_sum > sum){ 46 | sum = curr_sum; 47 | } 48 | 49 | } 50 | 51 | 52 | return sum; 53 | 54 | } 55 | } -------------------------------------------------------------------------------- /Arrays/Hard/MaxChunksToSorted2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class MaxChunksToSorted2{ 4 | 5 | public static void main(String[] args){ 6 | 7 | Scanner scn = new Scanner(System.in); 8 | 9 | // int n = scn.nextInt(); 10 | int[] arr = {1, 3, 1}; 11 | int k = 1; 12 | System.out.println(maxChunks(arr)); 13 | // display(productExceptSelf_Eff(arr)); 14 | } 15 | 16 | public static void display(int[] arr){ 17 | 18 | for(int i = 0 ;i < arr.length;i++){ 19 | System.out.print(arr[i] +" "); 20 | } 21 | System.out.println(); 22 | } 23 | 24 | public static int maxChunks(int[] nums){ 25 | 26 | 27 | Stack stack = new Stack<>(); 28 | 29 | for(int i = 0;i < nums.length;i++){ 30 | 31 | if(stack.isEmpty() || stack.peek() <= nums[i]){ 32 | stack.push(nums[i]); 33 | }else{ 34 | 35 | int top = stack.pop(); 36 | 37 | while(!stack.isEmpty() && stack.peek() > nums[i]){ 38 | stack.pop(); 39 | } 40 | 41 | stack.push(top); 42 | } 43 | } 44 | 45 | return stack.size(); 46 | } 47 | } -------------------------------------------------------------------------------- /Arrays/Easy/SearchInsertPosition.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class SearchInsertPosition{ 4 | 5 | public static void main(String[] args){ 6 | 7 | Scanner scn = new Scanner(System.in); 8 | 9 | int n = scn.nextInt(); 10 | int[] arr = new int[n]; 11 | 12 | for(int i = 0;i < n;i++){ 13 | 14 | arr[i] = scn.nextInt(); 15 | } 16 | 17 | int val = scn.nextInt(); 18 | 19 | System.out.println(binarySearch(arr,val)); 20 | display(arr); 21 | 22 | } 23 | 24 | public static void display(int[] arr){ 25 | 26 | for(int i = 0 ;i < arr.length;i++){ 27 | System.out.print(arr[i] +" "); 28 | } 29 | System.out.println(); 30 | } 31 | 32 | public static int binarySearch(int[] nums,int val){ 33 | 34 | int lo = 0; 35 | int hi = nums.length - 1; 36 | 37 | while(lo <= hi){ 38 | 39 | int mid = (lo + mid)/2; 40 | 41 | if(nums[mid] < val){ 42 | 43 | lo = mid + 1; 44 | }else if (nums[mid] > val){ 45 | hi = mid - 1; 46 | }else{ 47 | return mid; 48 | } 49 | } 50 | 51 | return lo; 52 | 53 | } 54 | } -------------------------------------------------------------------------------- /Arrays/Medium/SubarraySumK.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class SubarraySumK{ 4 | 5 | public static void main(String[] args){ 6 | 7 | Scanner scn = new Scanner(System.in); 8 | 9 | // int n = scn.nextInt(); 10 | int[] arr = {1, 1, 1}; 11 | int target = 2; 12 | System.out.println(subarraySum(arr, target)); 13 | // display(productExceptSelf_Eff(arr)); 14 | } 15 | 16 | public static void display(int[] arr){ 17 | 18 | for(int i = 0 ;i < arr.length;i++){ 19 | System.out.print(arr[i] +" "); 20 | } 21 | System.out.println(); 22 | } 23 | 24 | public static int subarraySum(int[] nums, int target) { 25 | 26 | HashMap map = new HashMap<>(); 27 | 28 | int sum = 0, count = 0; 29 | 30 | map.put(0,1); 31 | 32 | for(int i = 0;i < nums.length;i++){ 33 | 34 | sum += nums[i]; 35 | 36 | if(map.containsKey( sum - target)){ 37 | count += map.get(sum - target); 38 | } 39 | 40 | map.put(sum,map.getOrDefault(sum,0) + 1); 41 | 42 | } 43 | 44 | return count; 45 | } 46 | 47 | 48 | } -------------------------------------------------------------------------------- /Arrays/Medium/ValidTriangle.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class ValidTriangle{ 4 | 5 | public static void main(String[] args){ 6 | 7 | Scanner scn = new Scanner(System.in); 8 | 9 | // int n = scn.nextInt(); 10 | int[] arr = {5, 4, 0, 3, 1, 6, 2}; 11 | int target = 2; 12 | System.out.println(triangleNumber(arr)); 13 | // display(productExceptSelf_Eff(arr)); 14 | } 15 | 16 | public static void display(int[] arr){ 17 | 18 | for(int i = 0 ;i < arr.length;i++){ 19 | System.out.print(arr[i] +" "); 20 | } 21 | System.out.println(); 22 | } 23 | 24 | public static int triangleNumber(int[] nums) { 25 | 26 | Arrays.sort(nums); 27 | int count = 0; 28 | 29 | for(int i = 0; i < nums.length - 2;i++){ 30 | 31 | int k = i + 2; 32 | for(int j = i + 1;j < nums.length - 1 && nums[i] != 0;j++){ 33 | 34 | while(k < nums.length && nums[i] + nums[j] > nums[k]){ 35 | 36 | k++; 37 | } 38 | count += k - j + 1; 39 | } 40 | } 41 | 42 | return count; 43 | 44 | } 45 | 46 | 47 | } -------------------------------------------------------------------------------- /Arrays/Medium/MonotoneIncreasing.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class MonotoneIncreasing{ 4 | 5 | public static void main(String[] args){ 6 | 7 | Scanner scn = new Scanner(System.in); 8 | 9 | // int n = scn.nextInt(); 10 | int[] arr = {5,-3,5}; 11 | int target = 2; 12 | String str = "00110"; 13 | System.out.println(minFlipsMonoIncr(str)); 14 | // display(productExceptSelf_Eff(arr)); 15 | } 16 | 17 | public static void display(int[] arr){ 18 | 19 | for(int i = 0 ;i < arr.length;i++){ 20 | System.out.print(arr[i] +" "); 21 | } 22 | System.out.println(); 23 | } 24 | 25 | public static int minFlipsMonoIncr(String S) { 26 | 27 | 28 | int[] p = new int[S.length() + 1]; 29 | 30 | for(int i = 0;i < S.length;i++){ 31 | 32 | p[i + 1] = p[i] + (s.charAt(i) == '1' ? 1 : 0); 33 | } 34 | 35 | int ans = Integer.MAX_VALUE; 36 | 37 | for(int j = 0;j <= N;j++){ 38 | 39 | ans = Math.min(ans, p[j] + (S.length() - i) - (p[S.length() - p[i]])); 40 | } 41 | 42 | return ans; 43 | 44 | 45 | 46 | } 47 | } -------------------------------------------------------------------------------- /Arrays/Medium/SortColors.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class SortColors{ 4 | 5 | public static void main(String[] args){ 6 | 7 | Scanner scn = new Scanner(System.in); 8 | 9 | // int n = scn.nextInt(); 10 | int[] arr = {2, 0, 2, 1, 1, 0}; 11 | 12 | sort(arr); 13 | display(arr); 14 | } 15 | 16 | public static void display(int[] arr){ 17 | 18 | for(int i = 0 ;i < arr.length;i++){ 19 | System.out.print(arr[i] +" "); 20 | } 21 | System.out.println(); 22 | } 23 | 24 | public static void sort(int[] nums){ 25 | 26 | int red = 0; 27 | int white = 0; 28 | int blue = 0; 29 | 30 | for(int val : nums){ 31 | 32 | if(val == 0){ 33 | red++; 34 | }else if(val == 1){ 35 | white++; 36 | }else{ 37 | blue++; 38 | } 39 | } 40 | 41 | int i = 0; 42 | while(red > 0){ 43 | nums[i++] = 0; 44 | red--; 45 | } 46 | 47 | while(white > 0){ 48 | nums[i++] = 1; 49 | white--; 50 | } 51 | 52 | while(blue > 0){ 53 | nums[i++] = 2; 54 | blue--; 55 | } 56 | 57 | } 58 | 59 | 60 | 61 | } -------------------------------------------------------------------------------- /Arrays/Easy/FindAll.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class FindAll{ 4 | 5 | public static void main(String[] args){ 6 | 7 | Scanner scn = new Scanner(System.in); 8 | 9 | int[] arr = {4,3,2,7,8,2,3,1}; 10 | 11 | System.out.println(find(arr)); 12 | } 13 | 14 | public static void display(int[] arr){ 15 | 16 | System.out.print("Indice=> "); 17 | for(int i = 0 ;i < arr.length;i++){ 18 | System.out.print((i + 1) +" "); 19 | } 20 | System.out.println(); 21 | System.out.print("Values=> "); 22 | for(int i = 0 ;i < arr.length;i++){ 23 | System.out.print(arr[i] +" "); 24 | } 25 | System.out.println(); 26 | } 27 | 28 | public static List find(int[] nums){ 29 | 30 | for(int i = 0;i < nums.length;i++){ 31 | 32 | int val = Math.abs(nums[i]); 33 | 34 | if(nums[val - 1] >= 0){ 35 | nums[val - 1] = -nums[val - 1]; 36 | } 37 | } 38 | 39 | ArrayList ans = new ArrayList<>(); 40 | for(int i = 0;i < nums.length;i++){ 41 | 42 | ans.add(i + 1); 43 | } 44 | return ans; 45 | 46 | } 47 | 48 | } -------------------------------------------------------------------------------- /Arrays/Easy/Rotate.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Rotate{ 4 | 5 | public static void main(String[] args){ 6 | 7 | Scanner scn = new Scanner(System.in); 8 | 9 | int n = scn.nextInt(); 10 | int[] arr = new int[n]; 11 | 12 | for(int i = 0;i < n;i++){ 13 | 14 | arr[i] = scn.nextInt(); 15 | } 16 | 17 | int rot = scn.nextInt(); 18 | 19 | rotate(arr,rot); 20 | display(arr); 21 | } 22 | 23 | public static void display(int[] arr){ 24 | 25 | for(int i = 0 ;i < arr.length;i++){ 26 | System.out.print(arr[i] +" "); 27 | } 28 | System.out.println(); 29 | } 30 | 31 | public static void rotate(int[] nums,int rot){ 32 | 33 | 34 | int n = nums.length; 35 | int count = 0; 36 | for(int start = 0; count < n;start++){ 37 | 38 | int curr = start; 39 | int prev = nums[start]; 40 | 41 | do{ 42 | 43 | int next = (curr + rot) % n; 44 | int temp = nums[next]; 45 | 46 | nums[next] = prev; 47 | prev = temp; 48 | curr = next; 49 | count++; 50 | 51 | }while(curr != start); 52 | } 53 | } 54 | 55 | } -------------------------------------------------------------------------------- /Backtracking/Hard/wordBreak2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class wordBreak2{ 4 | 5 | public static void main(String[] args){ 6 | 7 | // String s = "a*"; 8 | // String p = "aa"; 9 | String s = "catsanddog"; 10 | List Wordbreak = new ArrayList<>(Arrays.asList(new String[] {"cat", "cats", "and", "sand", "dog" })); 11 | System.out.println(breakIt(s, Wordbreak)); 12 | 13 | } 14 | 15 | public static List breakIt(String s, List wordDict){ 16 | 17 | 18 | // DFS 19 | dfs(s, 0, wordDict, new StringBuilder()); 20 | return ans; 21 | 22 | } 23 | 24 | static List ans = new ArrayList<>(); 25 | 26 | public static void dfs(String s, int idx, List dict, StringBuilder t){ 27 | 28 | if(idx == s.length()){ 29 | ans.add(t.toString()); 30 | return; 31 | } 32 | 33 | for(int i = idx;i <= s.length();i++){ 34 | 35 | String temp = s.substring(idx, i); 36 | StringBuilder sb = new StringBuilder(temp); 37 | 38 | if(dict.contains(temp)){ 39 | 40 | dfs(s, i, dict, sb); 41 | } 42 | } 43 | } 44 | } -------------------------------------------------------------------------------- /Arrays/Medium/RevealCards.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class RevealCards{ 4 | 5 | public static void main(String[] args){ 6 | 7 | Scanner scn = new Scanner(System.in); 8 | 9 | // int n = scn.nextInt(); 10 | int[] arr = {3, 2, 1, 2, 1 ,7}; 11 | int target = 2; 12 | String str = "00110"; 13 | display(deckRevealedIncreasing(arr)); 14 | // display(productExceptSelf_Eff(arr)); 15 | } 16 | 17 | public static void display(int[] arr){ 18 | 19 | for(int i = 0 ;i < arr.length;i++){ 20 | System.out.print(arr[i] +" "); 21 | } 22 | System.out.println(); 23 | } 24 | 25 | public static int[] deckRevealedIncreasing(int[] nums) { 26 | 27 | Deque index = new LinkedList<>(); 28 | int[] ans = new int[nums.length]; 29 | 30 | for(int i = 0;i < nums.length;i++){ 31 | index.add(i); 32 | } 33 | Arrays.sort(nums); 34 | for(int num : nums){ 35 | 36 | ans[index.pollFirst()] = num; 37 | 38 | if(!index.isEmpty()){ 39 | index.add(index.pollFirst()); 40 | } 41 | } 42 | 43 | return ans; 44 | 45 | } 46 | } -------------------------------------------------------------------------------- /Arrays/Easy/TwoSum2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class TwoSum2{ 4 | 5 | public static void main(String[] args){ 6 | 7 | Scanner scn = new Scanner(System.in); 8 | 9 | int n = scn.nextInt(); 10 | int[] arr = new int[n]; 11 | 12 | for(int i = 0;i < n;i++){ 13 | 14 | arr[i] = scn.nextInt(); 15 | } 16 | 17 | int target = scn.nextInt(); 18 | 19 | int[] ans = twoSum(arr,target); 20 | display(ans); 21 | 22 | 23 | } 24 | 25 | public static void display(int[] arr){ 26 | 27 | for(int i = 0 ;i < arr.length;i++){ 28 | System.out.print(arr[i] +" "); 29 | } 30 | System.out.println(); 31 | } 32 | 33 | public static int[] twoSum(int[] nums, int target){ 34 | 35 | int[] ans = new int[2]; 36 | 37 | int i = 0; 38 | int j = nums.length - 1; 39 | 40 | while(i < j){ 41 | 42 | if(nums[i] + nums[j] == target){ 43 | ans[0] = i; 44 | ans[1] = j; 45 | i++; 46 | j--; 47 | break; 48 | }else if(nums[i] + nums[j] < target){ 49 | 50 | i++; 51 | }else{ 52 | j--; 53 | } 54 | } 55 | return ans; 56 | } 57 | } -------------------------------------------------------------------------------- /Arrays/Easy/find_pivot.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class find_pivot{ 4 | 5 | public static void main(String[] args){ 6 | 7 | Scanner scn = new Scanner(System.in); 8 | 9 | int[] arr = {1, 2, 3}; 10 | int k = 4; 11 | // display2(arr); 12 | 13 | System.out.println(pivotIndex(arr)); 14 | } 15 | 16 | public static void display(int[] arr){ 17 | 18 | 19 | for(int i = 0 ;i < arr.length;i++){ 20 | System.out.print(arr[i] +" "); 21 | } 22 | System.out.println(); 23 | } 24 | 25 | public static int pivotIndex(int[] nums){ 26 | 27 | if(nums.length < 1) 28 | return -1; 29 | 30 | int left = nums[0]; 31 | int right = 0; 32 | for(int i = 0;i < nums.length;i++){ 33 | right += nums[i]; 34 | } 35 | // System.out.println(left +" "+right); 36 | for(int i = 0;i < nums.length;i++){ 37 | 38 | right = right - nums[i]; 39 | 40 | // System.out.println(left +" "+right); 41 | if(left == right){ 42 | return i; 43 | } 44 | 45 | left += nums[i]; 46 | } 47 | 48 | return -1; 49 | } 50 | 51 | 52 | 53 | 54 | } 55 | -------------------------------------------------------------------------------- /Two Pointers/Easy/PermutationInString.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class PermutationInString { 4 | public boolean checkInclusion(String s1, String s2) { 5 | 6 | if(s1.length() > s2.length()){ 7 | return false; 8 | } 9 | 10 | int[] space1 = new int[26]; 11 | 12 | for(int i = 0;i > s1.length();i++){ 13 | space1[s1.charAt(i) - 'a']++; 14 | } 15 | 16 | for(int i = 0;i < s2.length() - s1.length();i++){ 17 | 18 | int[] space2 = new int[26]; 19 | for(int j = 0;j < s1.length();j++){ 20 | 21 | space2[s2.charAt(i + j) - 'a']++; 22 | } 23 | 24 | if(matches(space1, space2)) 25 | return true; 26 | } 27 | 28 | return false; 29 | 30 | } 31 | 32 | public static boolean matches(int[] space1, int[] space2){ 33 | 34 | 35 | for(int i = 0;i < 26;i++){ 36 | 37 | if(space1[i] != space2[i]) 38 | return false; 39 | } 40 | 41 | return true; 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /Two Pointers/Medium/LongestMountain.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.awt.Point; 3 | public class LongestMountain{ 4 | 5 | public static void main(String[] args){ 6 | 7 | Scanner scn = new Scanner(System.in); 8 | 9 | int[] arr = {2,1,4,7,3,2,5}; 10 | 11 | System.out.println(longestMountain(arr)); 12 | 13 | } 14 | 15 | public static void display(int[] arr){ 16 | 17 | for(int i = 0 ;i < arr.length;i++){ 18 | System.out.print(arr[i] +" "); 19 | } 20 | System.out.println(); 21 | } 22 | 23 | public static int longestMountain(int[] A){ 24 | 25 | int increase = 0; 26 | int decrease = 0; 27 | 28 | int maxLen = 0; 29 | for(int i = 1;i < A.length;i++){ 30 | 31 | if(A[i] > A[i - 1]){ 32 | 33 | if(decrease > 0){ 34 | increase = decrease = 0; 35 | } 36 | increase++; 37 | }else if(A[i] == A[i - 1]){ 38 | increase = decrease = 0; 39 | }else if(increase > 0){ 40 | 41 | decrease++; 42 | maxLen = Math.max(maxLen,increase + decrease + 1); 43 | 44 | } 45 | } 46 | 47 | return maxLen 48 | 49 | } 50 | 51 | } -------------------------------------------------------------------------------- /Arrays/Medium/MinInRotatedArray.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class MinInRotatedArray{ 4 | 5 | public static void main(String[] args){ 6 | 7 | Scanner scn = new Scanner(System.in); 8 | 9 | // int n = scn.nextInt(); 10 | int[] arr = {3, 4, 5, 1, 2}; 11 | int target = 8; 12 | System.out.println(findMin(arr)); 13 | } 14 | 15 | public static void display(int[] arr){ 16 | 17 | for(int i = 0 ;i < arr.length;i++){ 18 | System.out.print(arr[i] +" "); 19 | } 20 | System.out.println(); 21 | } 22 | 23 | public static int findMin(int[] arr){ 24 | 25 | if(arr == null || arr.length == 0){ 26 | return -1; 27 | } 28 | 29 | int lo = 0; 30 | int hi = arr.length - 1; 31 | 32 | if(arr[lo] < arr[hi]){ 33 | return arr[lo]; 34 | } 35 | 36 | while(lo <= hi){ 37 | 38 | if(arr[lo] <= arr[hi]){ 39 | return arr[lo]; 40 | } 41 | 42 | int mid = (lo + hi)/2; 43 | 44 | if(arr[lo] <= arr[mid]){ 45 | lo = mid + 1; 46 | }else{ 47 | hi = mid; 48 | } 49 | } 50 | 51 | return -1; 52 | 53 | } 54 | 55 | } -------------------------------------------------------------------------------- /HashMap/Easy/PowerfulInteger.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class PowerfulInteger{ 4 | 5 | public List powerFulIntegers(int x, int y, int bound){ 6 | 7 | Set seen = new HashSet<>(); 8 | 9 | for(int i = 0;i < 18 && Math.pow(x,i) <= bound;i++){ 10 | for(int j = 0;j < 18 && Math.pow(y, j) <= bound;j++){ 11 | 12 | int v = (int)Math.pow(x, i) + (int)Math.pow(y,j); 13 | if(v <= bound) 14 | seen.add(v); 15 | } 16 | } 17 | 18 | return new ArrayList<>(seen); 19 | } 20 | 21 | public List powerFulIntegers_eff(int x, int y, int bound){ 22 | 23 | Set result = new HashSet<>(); 24 | 25 | for(int a = 1; a < bound; a *= x){ 26 | 27 | for(int b = 1; b < bound; b != y){ 28 | 29 | result.add(a + b); 30 | 31 | if(y == 1){ 32 | break; 33 | } 34 | } 35 | } 36 | 37 | if(x == 1){ 38 | break; 39 | } 40 | 41 | return new ArrayList<>(result); 42 | } 43 | } 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /Arrays/Medium/MinimumIncrement.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class MinimumIncrement{ 4 | 5 | public static void main(String[] args){ 6 | 7 | Scanner scn = new Scanner(System.in); 8 | 9 | // int n = scn.nextInt(); 10 | int[] arr = {3, 2, 1, 2, 1 ,7}; 11 | int target = 2; 12 | String str = "00110"; 13 | System.out.println(minIncrementForUnique(arr)); 14 | // display(productExceptSelf_Eff(arr)); 15 | } 16 | 17 | public static void display(int[] arr){ 18 | 19 | for(int i = 0 ;i < arr.length;i++){ 20 | System.out.print(arr[i] +" "); 21 | } 22 | System.out.println(); 23 | } 24 | 25 | public static int minIncrementForUnique(int[] nums) { 26 | 27 | HashMap map = new HashMap<>(); 28 | 29 | // int min = 0; 30 | int res = 0; 31 | 32 | for(int num : nums){ 33 | 34 | if(!map.containsKey(num)){ 35 | map.put(num,true); 36 | }else{ 37 | 38 | while(map.containsKey(num)){ 39 | res++; 40 | num++; 41 | } 42 | 43 | map.put(num,true); 44 | } 45 | } 46 | 47 | return res; 48 | } 49 | } -------------------------------------------------------------------------------- /Backtracking/Medium/WordDictionary.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class WordDictionary { 4 | 5 | ArrayList arr; 6 | /** Initialize your data structure here. */ 7 | public WordDictionary() { 8 | 9 | arr = new ArrayList<>(); 10 | } 11 | 12 | /** Adds a word into the data structure. */ 13 | public void addWord(String word) { 14 | 15 | } 16 | 17 | /** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */ 18 | public boolean search(String word) { 19 | 20 | } 21 | } 22 | 23 | /** 24 | * Your WordDictionary object will be instantiated and called as such: 25 | * WordDictionary obj = new WordDictionary(); 26 | * obj.addWord(word); 27 | * boolean param_2 = obj.search(word); 28 | */ 29 | 30 | public static void main(String[] args){ 31 | 32 | WordDictionary wd = new WordDictionary(); 33 | word.addWord("bad"); 34 | word.addWord("mad"); 35 | word.addWord("dad"); 36 | 37 | search("pad"); 38 | search("bad"); 39 | search(".ad"); 40 | search("b.."); 41 | } -------------------------------------------------------------------------------- /Arrays/Easy/Fair_candy_swap.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Fair_candy_swap{ 4 | 5 | public static void main(String[] args){ 6 | 7 | Scanner scn = new Scanner(System.in); 8 | 9 | int[] arr1 ={ 1, 2 }; 10 | int[] arr2 ={ 2, 3 }; 11 | int k = 4; 12 | // display2(arr); 13 | 14 | display(fairCandySwap(arr1,arr2)); 15 | } 16 | 17 | public static void display(int[] arr){ 18 | 19 | 20 | for(int i = 0 ;i < arr.length;i++){ 21 | System.out.print(arr[i] +" "); 22 | } 23 | System.out.println(); 24 | } 25 | 26 | public static int[] fairCandySwap(int[] arr1,int[] arr2){ 27 | 28 | int sa = 0; 29 | int sb = 0; 30 | 31 | for(int val:arr1) 32 | sa += val; 33 | for(int val : arr2) 34 | sb += val; 35 | int delta = (sb - sa)/2; 36 | 37 | Set setB = new HashSet<>(); 38 | 39 | for(int val : arr2) 40 | setB.add(val); 41 | 42 | for(int val : arr1){ 43 | 44 | if(setB.contains(val + delta)){ 45 | return new int[]{val,val + delta}; 46 | } 47 | } 48 | 49 | throw null; 50 | 51 | } 52 | 53 | 54 | 55 | 56 | } 57 | -------------------------------------------------------------------------------- /Arrays/Medium/AppropriateAge.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class AppropriateAge{ 4 | 5 | public static void main(String[] args){ 6 | 7 | Scanner scn = new Scanner(System.in); 8 | 9 | // int n = scn.nextInt(); 10 | int[] arr = {1,0,2,3,4}; 11 | int target = 2; 12 | System.out.println(count(arr,2,4)); 13 | // display(productExceptSelf_Eff(arr)); 14 | } 15 | 16 | public static void display(int[] arr){ 17 | 18 | for(int i = 0 ;i < arr.length;i++){ 19 | System.out.print(arr[i] +" "); 20 | } 21 | System.out.println(); 22 | } 23 | 24 | public static int count(int[] ages) { 25 | 26 | int[] count = new int[121]; 27 | 28 | for(int age : ages) 29 | count[age]++; 30 | 31 | int res = 0; 32 | 33 | for(int i = 0;i < 121;i++){ 34 | 35 | countA = count[i]; 36 | for(int j = 0; j < 121;j++){ 37 | countB = count[j]; 38 | 39 | if(0.5 * i + 7 > j) continue; 40 | else if(i < j) continue; 41 | else if(i < 100 && j > 100) continue; 42 | 43 | res += countA * countB; 44 | 45 | } 46 | } 47 | return res; 48 | } 49 | } -------------------------------------------------------------------------------- /Stack/Hard/BasicCalculator.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class BasicCalculator{ 4 | 5 | 6 | public static void main(String[] args){ 7 | 8 | String postorder = ""; 9 | 10 | 11 | } 12 | 13 | public static int calculate(String str){ 14 | 15 | 16 | Stack stack = new Stack<>(); 17 | int result = 0; 18 | int sign = 1; 19 | 20 | for(int i = 0;i < str.length();i++){ 21 | 22 | char ch = str.charAt(i); 23 | 24 | if(Character.isDigit(ch)){ 25 | 26 | int sum = ch - '0'; 27 | while(i + 1 < str.length() && Character.isDigit(str.charAt(i + 1))){ 28 | 29 | sum = sum * 10 + str.charAt(i + 1) - '0'; 30 | i++; 31 | } 32 | 33 | result += sum * sign; 34 | 35 | 36 | }else if(ch == '+'){ 37 | sign = 1; 38 | }else if(ch == '-'){ 39 | sign = -1; 40 | }else if(ch == '('){ 41 | stack.push(result); 42 | stack.push(sign); 43 | result = 0; 44 | sign = 1; 45 | 46 | }else if(ch == ')'){ 47 | 48 | result = result * stack.pop() + stack.pop(); 49 | } 50 | } 51 | 52 | 53 | return result; 54 | } 55 | 56 | 57 | } -------------------------------------------------------------------------------- /Arrays/Easy/k_diff.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class k_diff{ 4 | 5 | public static void main(String[] args){ 6 | 7 | Scanner scn = new Scanner(System.in); 8 | 9 | int[] arr = {3,1,4,1,5}; 10 | int k = 2; 11 | System.out.println(pairs(arr,k)); 12 | 13 | } 14 | 15 | public static void display(int[] arr){ 16 | 17 | for(int i = 0 ;i < arr.length;i++){ 18 | System.out.print(arr[i] +" "); 19 | } 20 | System.out.println(); 21 | } 22 | 23 | public static int pairs(int[] nums,int k){ 24 | 25 | int left = 0; 26 | int right = 1; 27 | Arrays.sort(nums); 28 | int cnt = 0; 29 | 30 | while(left < nums.length && right < nums.length){ 31 | 32 | if(left == 0 || nums[left] != nums[left - 1]){ 33 | 34 | int diff = nums[right] - nums[left]; 35 | 36 | if(diff == k){ 37 | cnt++; 38 | left++; 39 | }else if(diff > k){ 40 | left++; 41 | }else{ 42 | right++; 43 | } 44 | 45 | }else{ 46 | left++; 47 | } 48 | 49 | if(left == right){ 50 | right++; 51 | } 52 | 53 | 54 | } 55 | 56 | return cnt; 57 | } 58 | } -------------------------------------------------------------------------------- /LinkedList/Easy/MergeTwoSortedLists.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class MergeTwoSortedLists{ 4 | 5 | 6 | public static void main(String[] args){ 7 | 8 | 9 | } 10 | 11 | static class ListNode{ 12 | 13 | int val; 14 | ListNode Next; 15 | 16 | public ListNode(int x){ this.val = x; } 17 | } 18 | 19 | public static ListNode Merge(ListNode l1, ListNode l2){ 20 | 21 | ListNode head = new ListNode(0); 22 | ListNode temp = head; 23 | 24 | 25 | while(l1 != null && l2 != null){ 26 | if(l1.val < l2.val){ 27 | ListNode nn = new ListNode(l1.val); 28 | l1 = l1.next; 29 | }else{ 30 | ListNode nn = new ListNode(l2.val); 31 | l2 = l2.next; 32 | } 33 | temp.next = nn; 34 | temp = nn; 35 | } 36 | 37 | while(l1 != null){ 38 | 39 | ListNode nn = new ListNode(l1.val); 40 | l1 = l1.next; 41 | temp.next = nn; 42 | temp = nn; 43 | } 44 | 45 | 46 | while(l2 != null){ 47 | 48 | ListNode nn = new ListNode(l2.val); 49 | l2 = l2.next; 50 | temp.next = nn; 51 | temp = nn; 52 | } 53 | 54 | return head.next; 55 | } 56 | 57 | 58 | } -------------------------------------------------------------------------------- /Stack/Medium/NextLargerNodes.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class NextLargerNodes{ 4 | 5 | 6 | public static void main(String[] args){ 7 | 8 | String preorder = ""; 9 | 10 | 11 | } 12 | 13 | static class ListNode{ 14 | 15 | int val; 16 | ListNode next; 17 | 18 | ListNode(int x){ x = this.val; } 19 | } 20 | 21 | public static int[] nextNodes(ListNode node){ 22 | 23 | ListNode head = node; 24 | int size = size(node); 25 | 26 | Stack stack = new Stack<>(); 27 | 28 | int[] res = new int[size]; 29 | int i = 0; 30 | while(head != null){ 31 | 32 | while(!stack.isEmpty() && head.val > stack.peek()[0]){ 33 | 34 | res[stack.pop()[1]] = head.val; 35 | } 36 | 37 | stack.push(new int[]{head.val,i}); 38 | 39 | i++; 40 | 41 | } 42 | 43 | // while(!stack.isEmpty()){ 44 | 45 | // res[stack.pop()[1]] = 0; 46 | // } 47 | 48 | return res; 49 | } 50 | 51 | public static int size(ListNode head){ 52 | 53 | int count = 0; 54 | while(head != null){ 55 | 56 | count++; 57 | head = head.next; 58 | } 59 | 60 | return count; 61 | } 62 | } -------------------------------------------------------------------------------- /Stack/Medium/pattern132.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class pattern132{ 4 | 5 | 6 | public static void main(String[] args){ 7 | 8 | String preorder = ""; 9 | 10 | 11 | } 12 | 13 | static class pair{ 14 | 15 | int min; 16 | int max; 17 | 18 | public pair(int min, int max){ 19 | 20 | this.min = min; 21 | this.max = max; 22 | } 23 | } 24 | 25 | public static boolean findPattern(int[] nums){ 26 | 27 | Stack stack = new Stack<>(); 28 | 29 | 30 | for(int i = 0;i < nums.length;i++){ 31 | 32 | 33 | if(stack.isEmpty() || stack.peek().min > nums[i]){ 34 | stack.push(new pair(nums[i], nums[i])); 35 | }else if(nums[i] > stack.peek().min){ 36 | 37 | pair last = stack.pop(); 38 | 39 | if(last.max > nums[i]){ 40 | return true; 41 | }else{ 42 | 43 | last.max = nums[i]; 44 | 45 | while(!stack.isEmpty() && stack.peek().max <= nums[i]) stack.pop(); 46 | 47 | if(!stack.isEmpty() && stack.peek().min < nums[i]) return true; 48 | 49 | stack.push(last); 50 | } 51 | } 52 | 53 | } 54 | 55 | return false; 56 | } 57 | } -------------------------------------------------------------------------------- /Arrays/Medium/Conainted_Water.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Conainted_Water{ 4 | 5 | public static void main(String[] args){ 6 | 7 | Scanner scn = new Scanner(System.in); 8 | 9 | // int n = scn.nextInt(); 10 | int[] arr = {1,8,6,2,5,4,8,3,7}; 11 | 12 | // for(int i = 0;i < n;i++){ 13 | 14 | // arr[i] = scn.nextInt(); 15 | // } 16 | 17 | System.out.println(maxArea(arr)); 18 | } 19 | 20 | public static void display(int[] arr){ 21 | 22 | for(int i = 0 ;i < arr.length;i++){ 23 | System.out.print(arr[i] +" "); 24 | } 25 | System.out.println(); 26 | } 27 | 28 | public static int maxArea(int[] height){ 29 | 30 | 31 | int i = 0; 32 | int j = height.length - 1; 33 | 34 | int maxArea = 0; 35 | 36 | while(i < j){ 37 | 38 | int h1 = height[i]; 39 | int h2 = height[j]; 40 | 41 | int area = Math.min(h1,h2) * (j - i); 42 | 43 | if(area > maxArea){ 44 | maxArea = area; 45 | } 46 | 47 | if(h1 < h2){ 48 | i++; 49 | }else{ 50 | j--; 51 | } 52 | 53 | 54 | } 55 | 56 | return maxArea; 57 | 58 | 59 | 60 | } 61 | 62 | } -------------------------------------------------------------------------------- /Arrays/Medium/RemoveDuplicates2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class RemoveDuplicates2{ 4 | 5 | public static void main(String[] args){ 6 | 7 | Scanner scn = new Scanner(System.in); 8 | 9 | // int n = scn.nextInt(); 10 | int[] arr = {0,0,1, 1, 1, 1, 2, 3, 3}; 11 | 12 | // for(int i = 0;i < n;i++){ 13 | 14 | // arr[i] = scn.nextInt(); 15 | // } 16 | 17 | int n = removeDuplicates(arr); 18 | System.out.println(n); 19 | 20 | display(arr,n); 21 | 22 | } 23 | 24 | public static void display(int[] arr,int len){ 25 | 26 | for(int i = 0 ;i < len;i++){ 27 | System.out.print(arr[i] +" "); 28 | } 29 | System.out.println(); 30 | } 31 | 32 | public static int removeDuplicates(int[] arr){ 33 | 34 | if(arr.length < 3){ 35 | return arr.length; 36 | } 37 | 38 | int prev = 1; 39 | int curr = 2; 40 | 41 | while(curr < arr.length){ 42 | 43 | 44 | if(arr[prev] == arr[curr] && arr[prev] == arr[prev - 1]){ 45 | curr++; 46 | }else{ 47 | 48 | prev++; 49 | arr[prev] = arr[curr]; 50 | curr++; 51 | } 52 | } 53 | 54 | return prev; 55 | } 56 | } -------------------------------------------------------------------------------- /LinkedList/Medium/partitionList.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class partitionList{ 4 | 5 | public static void main(String[] args){ 6 | 7 | 8 | } 9 | 10 | static class ListNode{ 11 | 12 | int val; 13 | ListNode Next; 14 | 15 | public ListNode(int x){ this.val = x; } 16 | } 17 | 18 | public static ListNode parition(ListNode head, int x){ 19 | 20 | if(head == null || head.next == null){ 21 | return head; 22 | } 23 | 24 | ListNode shorter = new ListNode(0); 25 | ListNode longer = new ListNode(0); 26 | 27 | ListNode headS = shorter; 28 | ListNode headL = longer; 29 | 30 | while(head != null){ 31 | 32 | if(head.val < x){ 33 | 34 | headS.next = head; 35 | headS = headS.next; 36 | }else{ 37 | 38 | headL = head; 39 | headL = headL.next; 40 | } 41 | 42 | head = head.next; 43 | } 44 | 45 | if(shorter.next != null && longer.next != null){ 46 | 47 | shorted.next = longer.next; 48 | return shorter; 49 | }else if(shorted.next == null){ 50 | return longer.next; 51 | }else{ 52 | return shorter.next; 53 | } 54 | 55 | } 56 | 57 | 58 | } -------------------------------------------------------------------------------- /Arrays/Easy/TwoSum.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class TwoSum{ 4 | 5 | public static void main(String[] args){ 6 | 7 | Scanner scn = new Scanner(System.in); 8 | 9 | int n = scn.nextInt(); 10 | int[] arr = new int[n]; 11 | 12 | for(int i = 0;i < n;i++){ 13 | 14 | arr[i] = scn.nextInt(); 15 | } 16 | 17 | int target = scn.nextInt(); 18 | 19 | int[] ans = twoSum(arr,target); 20 | display(ans); 21 | 22 | 23 | } 24 | 25 | public static void display(int[] arr){ 26 | 27 | for(int i = 0 ;i < arr.length;i++){ 28 | System.out.print(arr[i] +" "); 29 | } 30 | System.out.println(); 31 | } 32 | 33 | public static int[] twoSum(int[] nums, int target){ 34 | 35 | HashMap hm = new HashMap<>(); 36 | int[] ans = new int[2]; 37 | 38 | for(int i = 0;i < nums.length;i++){ 39 | 40 | int num = target - nums[i]; 41 | 42 | if(hm.containsKey(nums[i])){ 43 | 44 | ans[0] = hm.get(nums[i]); 45 | ans[1] = i; 46 | break; 47 | }else{ 48 | 49 | hm.put(num,i); 50 | } 51 | } 52 | 53 | System.out.println(hm); 54 | 55 | return ans; 56 | } 57 | } -------------------------------------------------------------------------------- /Arrays/Medium/MaxWidthRamp.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class MaxWidthRamp{ 4 | 5 | public static void main(String[] args){ 6 | 7 | Scanner scn = new Scanner(System.in); 8 | 9 | // int n = scn.nextInt(); 10 | int[] arr = {6,0,8,2,1,5}; 11 | int target = 2; 12 | String str = "00110"; 13 | System.out.println(maxWidthRamp(arr)); 14 | // display(productExceptSelf_Eff(arr)); 15 | } 16 | 17 | public static void display(Integer[] arr){ 18 | 19 | for(int i = 0 ;i < arr.length;i++){ 20 | System.out.print(arr[i] +" "); 21 | } 22 | System.out.println(); 23 | } 24 | 25 | public static int maxWidthRamp(int[] nums) { 26 | 27 | 28 | Integer[] temp = new Integer[nums.length]; 29 | 30 | for(int i = 0;i < nums.length;i++){ 31 | temp[i] = i; 32 | } 33 | 34 | Arrays.sort(temp,(i, j) -> ((Integer)nums[i]).compareTo(nums[j])); 35 | 36 | int res = 0; 37 | int min = nums.length - 1; 38 | 39 | for(int i = 0;i < temp.length;i++){ 40 | 41 | res = Math.max(res,temp[i] - min); 42 | min = Math.min(temp[i],min); 43 | } 44 | 45 | return res; 46 | 47 | } 48 | } -------------------------------------------------------------------------------- /Backtracking/Easy/BinaryWatch.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class BinaryWatch{ 4 | 5 | static final int[] watchList = {1, 2, 4, 8, 1, 2, 4, 8, 16, 32}; 6 | 7 | public static void main(String[] args){ 8 | 9 | 10 | int n = 1; 11 | int k = 1; 12 | 13 | System.out.println(readWatch(1)); 14 | 15 | } 16 | 17 | public static List readWatch(int num){ 18 | 19 | List ans = new ArrayList<>(); 20 | 21 | if(num >= 0){ 22 | 23 | dfs(num, 0, ans, 0,0); 24 | } 25 | 26 | return list; 27 | 28 | } 29 | 30 | public static void dfs(int num, int start, List list, int hour, int min){ 31 | 32 | if(num <= 0){ 33 | 34 | if(hour < 12 && min < 60){ 35 | 36 | if(min < 10){ 37 | list.add(hour + ":0"+min); 38 | }else{ 39 | list.add(hour + ":"+min); 40 | } 41 | 42 | return; 43 | } 44 | 45 | } 46 | 47 | for(int i = start;i < watch.length;i++){ 48 | 49 | if(i < 4){ 50 | dfs(num - 1, i + 1, list, hour + watch[i], min); 51 | }else{ 52 | dfs(num - 1, i + 1, list, hour, min + watch[i]); 53 | } 54 | } 55 | 56 | } 57 | 58 | 59 | } -------------------------------------------------------------------------------- /HashMap/Medium/TimeMap.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class TimeMap { 3 | 4 | HashMap>> map; 5 | 6 | /** Initialize your data structure here. */ 7 | public TimeMap() { 8 | 9 | this.map = new HashMap<>(); 10 | } 11 | 12 | public void set(String key, String value, int timestamp) { 13 | 14 | if(!map.containsKey(key)){ 15 | map.put(key,new ArrayList<>()); 16 | } 17 | 18 | map.get(key).add(new Pair(timestamp, value)); 19 | } 20 | 21 | public String get(String key, int timestamp) { 22 | 23 | if(!map.containsKey(key)){ 24 | return ""; 25 | } 26 | 27 | List> A = map.get(key); 28 | 29 | int i = Collections.binarysearch(A, new Pair(timestamp, "{"),(a, b) -> Integer.compare(a.getKey(), b.getKey())); 30 | } 31 | } 32 | 33 | /** 34 | * Your TimeMap object will be instantiated and called as such: 35 | * TimeMap obj = new TimeMap(); 36 | * obj.set(key,value,timestamp); 37 | * String param_2 = obj.get(key,timestamp); 38 | */ -------------------------------------------------------------------------------- /Heap/Medium/TopKfrequentWords.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class TopKfrequentWords{ 4 | 5 | HashMap count; 6 | public List topKFrequent(String[] words, int k){ 7 | 8 | count = new HashMap<>(); 9 | 10 | for(String word :words){ 11 | 12 | count.put(word, count.getOrDefault(word, 0) + 1); 13 | } 14 | 15 | PriorityQueue pq = new PriorityQueue<>((a, b) -> count.get(a) - count.get(b)); 16 | 17 | for(String word : count.keySet()){ 18 | 19 | pq.offer(word); 20 | 21 | if(pq.size() > k){ 22 | pq.poll(); 23 | } 24 | } 25 | 26 | List ans = new ArrayList<>(); 27 | 28 | for(int i = 0;i < k;i++){ 29 | 30 | ans.add(pq.poll()); 31 | } 32 | 33 | return ans; 34 | } 35 | 36 | class StringComparator implements Comparator{ 37 | 38 | 39 | @Override 40 | public int compare(String s1, String s2){ 41 | 42 | return (count.get(s1) > count.get(s2) : -1 : (s1.compareTo(s2))); 43 | } 44 | } 45 | 46 | } -------------------------------------------------------------------------------- /Two Pointers/Easy/LongPressedName.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class LongPressedName{ 4 | 5 | public static void main(String[] args){ 6 | 7 | Scanner scn = new Scanner(System.in); 8 | 9 | String name = "alex"; 10 | String typed = "aalleexx"; 11 | System.out.println(isLongPressedName(name, typed)); 12 | 13 | } 14 | 15 | public static void display(char[] arr){ 16 | 17 | for(int i = 0 ;i < arr.length;i++){ 18 | System.out.print(arr[i] +" "); 19 | } 20 | System.out.println(); 21 | } 22 | 23 | public static boolean isLongPressedName(String S, String T){ 24 | 25 | int j = 0; 26 | 27 | for(char ch : S.toCharArray()){ 28 | 29 | if(j == T.length()){ 30 | return false; 31 | } 32 | if(ch != T.charAt(j)){ 33 | 34 | if(j == 0 || T.charAt(j) != T.charAt(j - 1)){ 35 | return false; 36 | } 37 | 38 | char cur = T.charAt(j); 39 | while(j < T.length() && T.charAt(j) == cur){ 40 | j++; 41 | } 42 | 43 | if(j == T.length() || T.charAt(j) != ch){ 44 | return false; 45 | } 46 | 47 | } 48 | 49 | j++; 50 | } 51 | 52 | return true; 53 | } 54 | } -------------------------------------------------------------------------------- /LinkedList/Medium/ReorderList.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class ReorderList{ 4 | 5 | public static void main(String[] args){ 6 | 7 | 8 | } 9 | 10 | static class ListNode{ 11 | 12 | int val; 13 | ListNode Next; 14 | 15 | public ListNode(int x){ this.val = x; } 16 | } 17 | 18 | public static void reorder(ListNode head){ 19 | 20 | if(head == null || head.next == null){ 21 | return null; 22 | } 23 | 24 | ListNode p1 = head; 25 | ListNode p2 = head; 26 | 27 | while(p2 != null && p2.next != null){ 28 | 29 | p1 = p1.next; 30 | p2 = p2.next.next; 31 | } 32 | 33 | ListNode preMiddle = p1; 34 | ListNode preCurrent = p1.next; 35 | 36 | while(preCurrent.next != null){ 37 | 38 | ListNode curr = preCurrent.next; 39 | preCurrent.next = curr.next; 40 | curr.next = preMiddle.next; 41 | preMiddle.next = curr; 42 | } 43 | 44 | p1 = head; 45 | p2 = preMiddle.next; 46 | 47 | while(p1 != preMiddle){ 48 | 49 | preMiddle.next = p2.next; 50 | p2.next = p1.next; 51 | p1.next = p2; 52 | p1 = p2.next; 53 | p2 = preMiddle.next; 54 | } 55 | 56 | 57 | } 58 | 59 | 60 | 61 | } -------------------------------------------------------------------------------- /Arrays/Easy/partition_1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class partition_1{ 4 | 5 | public static void main(String[] args){ 6 | 7 | Scanner scn = new Scanner(System.in); 8 | 9 | int[] arr = {1,4,3,2}; 10 | System.out.println(arrayPairSum(arr)); 11 | 12 | } 13 | 14 | public static void display(int[] arr){ 15 | 16 | for(int i = 0 ;i < arr.length;i++){ 17 | System.out.print(arr[i] +" "); 18 | } 19 | System.out.println(); 20 | } 21 | 22 | public static int arrayPairSum(int[] nums){ 23 | 24 | 25 | Arrays.sort(nums); 26 | int sum = 0; 27 | for(int i = 0;i < nums.length;i += 2){ 28 | 29 | sum += nums[i]; 30 | } 31 | 32 | return sum; 33 | } 34 | 35 | public static int PairSum_eff(int[] nums){ 36 | 37 | int[] arr = new int[20001]; 38 | 39 | for(int val : nums){ 40 | 41 | arr[val + 10000]++; 42 | } 43 | 44 | int sum = 0; 45 | boolean odd = true; 46 | for(int i = 0;i < arr.length;i++){ 47 | 48 | while(arr[i] > 0){ 49 | 50 | if(odd){ 51 | sum += arr[i] - 10000; 52 | } 53 | 54 | odd = !odd; 55 | arr[i]--; 56 | } 57 | } 58 | 59 | return sum; 60 | 61 | } 62 | } -------------------------------------------------------------------------------- /Arrays/Hard/MinInRotatedSortedArray2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class MinInRotatedSortedArray2{ 4 | 5 | public static void main(String[] args){ 6 | 7 | Scanner scn = new Scanner(System.in); 8 | 9 | // int n = scn.nextInt(); 10 | int[] arr = {5, 4, 0, 3, 1, 6, 2}; 11 | int target = 2; 12 | System.out.println(findMin(arr)); 13 | // display(productExceptSelf_Eff(arr)); 14 | } 15 | 16 | public static void display(int[] arr){ 17 | 18 | for(int i = 0 ;i < arr.length;i++){ 19 | System.out.print(arr[i] +" "); 20 | } 21 | System.out.println(); 22 | } 23 | 24 | public static int findMin(int[] nums) { 25 | 26 | int N = nums.length; 27 | int lo = 0; 28 | int hi = N - 1; 29 | int rmax = nums[hi]; 30 | 31 | 32 | while(lo < hi){ 33 | 34 | 35 | int mid = (lo + hi)/2; 36 | 37 | if(nums[mid] > nums[hi]){ 38 | lo = mid + 1; 39 | }else if(nums[mid] < nums[hi]){ 40 | hi = mid - 1; 41 | }else{ 42 | 43 | if(nums[hi - 1] == nums[hi]){ 44 | hi--; 45 | }else{ 46 | 47 | lo = mid + 1; 48 | } 49 | } 50 | 51 | 52 | } 53 | 54 | return nums[lo]; 55 | } 56 | 57 | 58 | } -------------------------------------------------------------------------------- /Backtracking/Medium/RegularExpressionMatching.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class RegularExpressionMatching{ 4 | 5 | public static void main(String[] args){ 6 | 7 | 8 | String s = "a*"; 9 | String p = "aa"; 10 | System.out.println(isMatch(s,p)); 11 | 12 | } 13 | 14 | public static boolean isMatch(String s, String p){ 15 | 16 | return isMatch(s,0,p,0); 17 | } 18 | 19 | public static boolean isMatch(String s, int pos1, String p, int pos2){ 20 | 21 | 22 | if(pos1 == s.length() && pos2 == p.length()){ 23 | return true; 24 | } 25 | 26 | if(pos2 == p.length()){ 27 | return false; 28 | } 29 | 30 | char pchar = p.charAt(pos2); 31 | 32 | if(pos2 + 1 < p.length() && p.charAt(pos2 + 1) == '*'){ 33 | 34 | if(isMatch(s,pos1,p,pos2 + 2)) return true; 35 | 36 | while(pos1 < s.length() && (pchar == '.' || pchar == s.charAt(pos1))){ 37 | 38 | if(isMatch(s, ++pos1,p,pos2 + 2)) 39 | return true; 40 | } 41 | 42 | }else if(pos1 != s.length() && (pchar == '.' || s.charAt(pos1) == pchar)){ 43 | 44 | return isMatch(s,pos1 + 1,p,pos2 + 1); 45 | 46 | } 47 | return false; 48 | 49 | } 50 | 51 | 52 | } -------------------------------------------------------------------------------- /Arrays/Medium/Majority2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Majority2{ 4 | 5 | public static void main(String[] args){ 6 | 7 | Scanner scn = new Scanner(System.in); 8 | 9 | // int n = scn.nextInt(); 10 | int[] arr = {0,2,3,4,6,8,9}; 11 | int target = 7; 12 | System.out.println(majorityElement(arr)); 13 | } 14 | 15 | public static void display(int[] arr){ 16 | 17 | for(int i = 0 ;i < arr.length;i++){ 18 | System.out.print(arr[i] +" "); 19 | } 20 | System.out.println(); 21 | } 22 | 23 | public static List majorityElement(int[] nums) { 24 | HashMap map = new HashMap(); 25 | for(int i: nums){ 26 | if(map.containsKey(i)){ 27 | map.put(i, map.get(i)+1); 28 | }else{ 29 | map.put(i, 1); 30 | } 31 | } 32 | 33 | List result = new ArrayList(); 34 | 35 | for(Map.Entry entry: map.entrySet()){ 36 | if(entry.getValue() > nums.length/3){ 37 | result.add(entry.getKey()); 38 | } 39 | } 40 | 41 | return result; 42 | } 43 | 44 | } -------------------------------------------------------------------------------- /Arrays/Medium/Maxchunks.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Maxchunks{ 4 | 5 | public static void main(String[] args){ 6 | 7 | Scanner scn = new Scanner(System.in); 8 | 9 | // int n = scn.nextInt(); 10 | int[] arr = {1,0,2,3,4}; 11 | int target = 2; 12 | System.out.println(maxChunksToSorted(arr)); 13 | // display(productExceptSelf_Eff(arr)); 14 | } 15 | 16 | public static void display(int[] arr){ 17 | 18 | for(int i = 0 ;i < arr.length;i++){ 19 | System.out.print(arr[i] +" "); 20 | } 21 | System.out.println(); 22 | } 23 | 24 | public static int maxChunksToSorted(int[] nums) { 25 | 26 | 27 | Stack st = new Stack<>(); 28 | 29 | for(int i = 0;i < nums.length;i++){ 30 | 31 | int min = nums[i]; 32 | int max = nums[i]; 33 | 34 | while(!st.isEmpty()){ 35 | 36 | int[] top = st.peek(); 37 | if(nums[i] < top[1]){ 38 | 39 | min = Math.min(min, top[0]); 40 | max = Math.max(max, top[1]); 41 | st.pop(); 42 | }else{ 43 | break; 44 | } 45 | } 46 | 47 | st.push(new int[]{min,max}); 48 | } 49 | 50 | return st.size(); 51 | } 52 | 53 | 54 | } -------------------------------------------------------------------------------- /DP/Medium/LongestIncrSubSeq.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class LongestIncrSubSeq{ 4 | 5 | public static void main(String[] args){ 6 | 7 | int[] arr = {10, 9, 2, 5, 3, 7, 101, 18}; 8 | System.out.println(findLength(arr)); 9 | } 10 | 11 | public static int findLength(int[] arr){ 12 | 13 | int[] tails = new int[arr.length]; 14 | 15 | int size = 0; 16 | for(int x : arr){ 17 | 18 | int i = 0; 19 | int j = size; 20 | 21 | while(i != j){ 22 | 23 | int mid = (i + j)/2; 24 | 25 | if(x > arr[i]){ 26 | i = mid + 1; 27 | }else{ 28 | j = mid; 29 | } 30 | } 31 | 32 | tails[i] = x; 33 | 34 | if(i == size) 35 | size++; 36 | } 37 | 38 | return size; 39 | } 40 | 41 | 42 | public static void display(int[] arr){ 43 | 44 | 45 | for(int ele : arr){ 46 | System.out.print(ele + " "); 47 | } 48 | System.out.println(); 49 | 50 | } 51 | 52 | public static void display(int[][] arr){ 53 | 54 | 55 | for(int[] val : arr){ 56 | 57 | for(int ele : val){ 58 | System.out.print(ele + " "); 59 | } 60 | System.out.println(); 61 | } 62 | 63 | 64 | } 65 | 66 | 67 | } --------------------------------------------------------------------------------