├── 2D Arrays ├── LargestRowOrColumn.java ├── PrintLikeAWave.java ├── PrintSpiral.java ├── RowWiseSum.java └── TotalSum.java ├── Arrays-1 ├── ArrangeNumbersInJava.java ├── ArrayIntersection.java ├── FindDuplicate.java ├── FindUnique.java ├── LinearSearch.java ├── PairSum.java ├── ReturnArraySum.java ├── SortZeroOne.java ├── SwapAlternate.java └── TripletSum.java ├── Arrays-2 ├── BinarySearch.java ├── BubbleSort.java ├── CheckArrayRotation.java ├── InsertionSort.java ├── MergeTwoSortedArrays.java ├── PushZerosToEnd.java ├── RotateArray.java ├── SecondLargest.java ├── SelectionSort.java ├── SortZeroOneTwo.java └── SumOfTwoArrays.java ├── BackTracking ├── RatInAMaze.java └── RatInAMazeAllPaths.java ├── Binary Search Trees - I ├── BSTtoLL.java ├── BinaryTreeNode.java ├── CheckIsBST.java ├── CheckIsBSTImproved.java ├── ConstructBST.java ├── ElementsBetweenK1andK2.java ├── IsBSTReturn.java ├── LCAofBST.java ├── PairSuminBST.java └── SearchInBST.java ├── Binary Search Trees-II ├── FindPathinBST.java ├── LargestBST.java ├── LevelWiseLinkedList.java └── RootToNodeInBT.java ├── Binary Trees - II ├── BalanceBetter.java ├── BalancedTreeReturn.java ├── CreateAndInsertDuplicateNode.java ├── InOrderAndPostOrder.java ├── InOrderAndPreOrder.java ├── IsBalanced.java ├── LevelOrderTraversal.java ├── MinAndMax.java ├── MirrorBinaryTree.java ├── PathSumRootToLeaf.java ├── RemoveLeafNodes.java └── TakeAndPrintLevelWise.java ├── Binary Trees-1 ├── HeightOfTree.java ├── IsNodePresent.java ├── NodeWithLargestData.java ├── NodesGreaterThanX.java ├── NodesWithoutSibling.java ├── NumOfLeafNodes.java ├── PostOrderBinaryTree.java ├── PreOrderBinaryTree.java ├── PrintNodesAtDepthK.java ├── ReplaceNodeWithDepth.java └── SumOfNodes.java ├── Conditionals And Loops ├── Factors.java ├── FarhenheitToCelcius.java ├── FindCharacterCase.java ├── FindPowerofaNumber.java ├── MultiplicationTable.java ├── SumOfEvenandOdd.java └── TotalSalary.java ├── Data Structures(Test-1) ├── DoesScontainsT.java ├── MaximumProfit.java └── SplitArray.java ├── Dynamic Programming-1 ├── ByteLandian.java ├── LootHouses.java ├── MinNumOfSquares.java ├── MinNumOfSquares_Iterative.java ├── MinStepsTo1.java ├── MinStepsToOne_DP.java └── StairCase.java ├── Dynamic Programming-II ├── CountNumberOfSubsetWithGivenDifference.java ├── CountOfSubsets_Iterative.java ├── EqualSumPartition_Iterative.java ├── KnapSack_Iterative.java ├── KnapSack_Memoization.java ├── LongestCommonSubsequence_Iterative.java ├── LongestCommonSubsequence_Memoization.java ├── LongestCommonSubsequence_Recursive.java ├── LongestCommonSubstring_Iterative.java ├── LongestPalindromicSubsequence.java ├── LongestRepeatingSubsequence.java ├── MatrixChainMultiplication_Memoization.java ├── MatrixChainMultiplication_Recursive.java ├── MaxCoinChange.java ├── MinCostPath.java ├── MinCostPath_Iterative.java ├── MinCostPath_Memoization.java ├── MinNoOfInsertionToMakeAPalindrome.java ├── MinNumOfCoinsChange.java ├── MinimumSubsetSum.java ├── PalindromePartitioning_Memoization.java ├── PalindromePartitioning_Recursive.java ├── PrintLongestCommonSubsequence.java ├── PrintShortestCommonSupersequence.java ├── RodCutting.java ├── SequencePatternMatching.java ├── ShortestCommonSuperSequence.java ├── StringAtoStringB.java ├── SubsetSumProblem_Iterative.java └── TargetSum.java ├── Functions and Scope ├── FarhenheitToCelcius.java └── Fibonacci.java ├── Graphs-1 ├── BreadthFirstTraversal.java ├── DepthFirstTraversal.java └── HasPath.java ├── Graphs ├── BFSOfAGraph.java ├── DFSOfAGraph.java └── SetRepresentation.java ├── HashMaps ├── ExtractUniqueCharacters.java ├── LongestConsecutiveSequence.java ├── MaximumFrequencyNumber.java ├── Operations │ ├── Map.java │ └── MapNode.java ├── PairSumZero.java ├── PairsWithDifferenceK.java └── PrintIntersection.java ├── Linked List -1 ├── AppendLastNtoFirst.java ├── DeleteNodeinLL.java ├── EliminateDuplicates.java ├── FindANode.java ├── InsertNodeinLL.java ├── LengthofLL.java ├── PrintReverse.java └── PrintithNode.java ├── Linked List -2 ├── BubbleSortinLL.java ├── DeleteEveryNnodes.java ├── DeleteNodeRecursively.java ├── EvenAfterOddLL.java ├── FindANodeinLL.java ├── MergeSortLL.java ├── MergeTwoSortedLL.java ├── MiddleNodeinLL.java ├── ReverseLL.java └── SwapTwoNodesofLL.java ├── OOPS-1 └── ComplexNumberProblem.java ├── Operators & For Loops ├── AllPrimeNumbers.java ├── BinaryToDecimal.java ├── CheckNumbersSequence.java ├── DecimalToBinary.java ├── NthFibonacciNumber.java ├── ReverseOfANumber.java ├── SquareRootIntegral.java ├── SumOrProduct.java └── TermsOfAP.java ├── Patterns-1 ├── AlphaPattern.java ├── CharacterPattern.java ├── InterestingAlphabets.java ├── ReverseNumber.java ├── TriangleNumber.java └── TriangularStar.java ├── Patterns-2 ├── DiamondOfStars.java ├── HalfDiamond.java ├── InvertedNumber.java ├── MirrorImageNumber.java ├── OddSquare.java ├── Parallelogram.java ├── StarPattern.java ├── SumPattern.java └── TriangleOfNumbers.java ├── Priority Queue-I ├── MaxPriorityQueue.java └── Minimum Priority Queue │ ├── Element.java │ └── MinPriorityQueue.java ├── Priority Queue-II ├── CheckMaxHeap.java ├── KLargestElement.java ├── KSmallestElements.java └── KthLargestElement.java ├── Queues ├── Eclipse Implementation │ ├── QueueUse.java │ ├── QueueUsingLL.java │ └── ReverseFirstK.java ├── QueueUsingLL.java ├── ReverseQueue.java ├── ReverseTheFirstK.java └── StackUsing2Queues.java ├── README.md ├── Recursion Assignment ├── CheckAB.java ├── CheckPalindrome.java ├── CountZeros.java ├── GeometricSum.java ├── PairStar.java ├── StairCase.java ├── StringToInteger.java ├── SumofDigits.java └── multiplication.java ├── Recursion-1 ├── CalculatePower.java ├── CheckNumberinArray.java ├── FirstIndex.java ├── LastIndex.java ├── NumberOfDigits.java ├── PrintnNaturalNumbers.java └── SumOfArray.java ├── Recursion-2 ├── MergeSort.java ├── QuickSort.java ├── RemoveDuplicatesRecurssively.java ├── RemoveX.java └── TowerOfHanoi.java ├── Recursion-3 ├── FindSubsequences.java ├── PrintPermutationsOfString.java ├── PrintSubsequences.java ├── ReturnKeypadCode.java └── ReturnPermutationsOfString.java ├── Stack ├── BracketsBalanced.java ├── MinimumBracketReversal.java ├── ReverseStack.java ├── StackUsingLL.java └── StockSpan.java ├── Strings ├── AllSubstrings.java ├── CheckPermutation.java ├── CompressTheString.java ├── CountWords.java ├── HighestOccuringCharacter.java ├── RemoveCharacter.java ├── RemoveConsecDuplicates.java ├── ReverseEachWord.java ├── ReverseStringWordWise.java └── StringPalindrome.java ├── Test-1(java fundametals) ├── CheckAmstrong.java ├── NumberStarPattern1.java └── ZerosAndStars.java ├── Test-2(java fundamentals) ├── LeadersInArray.java ├── MinimumLengthWord.java └── Print2DArray.java └── TimeComplexity.java ├── ArrayEquilibriumIndex.java ├── ArrayIntersection.java ├── DuplicateInArray.java ├── FindTheUniqueElement.java └── RotationOfArray.java /2D Arrays/LargestRowOrColumn.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Solution { 3 | 4 | public static int[][] takeInput() 5 | { 6 | 7 | Scanner sc = new Scanner(System.in); 8 | //System.out.println("Enter R: "); 9 | int r = sc.nextInt(); 10 | //System.out.println("Enter C: "); 11 | int c = sc.nextInt(); 12 | 13 | if(r==0 & c==0) 14 | { 15 | System.out.println("row 0 -2147483648"); 16 | 17 | } 18 | else if(r>0 && c>0){ 19 | int arr[][]=new int[r][c]; 20 | 21 | for(int i = 0;imaxc) 48 | { 49 | maxc = sum; 50 | ci = j; 51 | 52 | } 53 | 54 | } 55 | 56 | int maxr = Integer.MIN_VALUE; 57 | int ri = 0; 58 | for(int i = 0;imaxr) 65 | { 66 | maxr=sum; 67 | ri=i; 68 | } 69 | } 70 | 71 | if(maxr>=maxc) 72 | { 73 | System.out.println("row "+ri+" "+maxr); 74 | } 75 | else 76 | { 77 | System.out.println("column "+ci+" "+maxc); 78 | } 79 | } 80 | 81 | } 82 | -------------------------------------------------------------------------------- /2D Arrays/PrintLikeAWave.java: -------------------------------------------------------------------------------- 1 | 2 | public class Solution { 3 | 4 | public static void wavePrint(int mat[][]){ 5 | //Your code goes here 6 | 7 | //the case when row = 0 and column = 0 8 | if(mat.length == 0){ 9 | return; 10 | } 11 | int r = mat.length; 12 | int c = mat[0].length; 13 | 14 | if(r==0 && c==0) 15 | { 16 | System.out.print(" "); 17 | 18 | } 19 | else 20 | { 21 | 22 | for(int j=0;j=0) 37 | { 38 | System.out.print(mat[i][j]+" "); 39 | i--; 40 | } 41 | } 42 | 43 | } 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /2D Arrays/PrintSpiral.java: -------------------------------------------------------------------------------- 1 | 2 | public class Solution { 3 | 4 | public static void spiralPrint(int matrix[][]){ 5 | //Your code goes here 6 | 7 | if(matrix.length == 0) 8 | { 9 | return; 10 | } 11 | int rs = 0; 12 | int re = matrix.length-1; 13 | int cs = 0; 14 | int ce = matrix[0].length - 1; 15 | int n = matrix.length; 16 | int m = matrix[0].length; 17 | int count = 1; 18 | 19 | while(count<=(n*m)) 20 | { 21 | int i = rs; 22 | int j = cs; 23 | while(j<=ce) 24 | { 25 | System.out.print(matrix[rs][j]+" "); //1 2 3 4 26 | j++; 27 | count++; 28 | } 29 | rs++; 30 | i++; 31 | while(i<=re) 32 | { 33 | System.out.print(matrix[i][ce]+" "); // 1 2 3 4 5 6 7 8 34 | i++; 35 | count++; 36 | 37 | } 38 | ce--; 39 | int k = ce; 40 | while(k>=cs) 41 | { 42 | System.out.print(matrix[re][k]+" ");// 1 2 3 4 5 6 7 8 9 10 11 43 | k--; 44 | count++; 45 | } 46 | 47 | re--; 48 | int l = re; 49 | while(l>=rs) 50 | { 51 | System.out.print(matrix[l][cs]+" "); // 1 2 3 4 5 6 7 8 9 10 11 12 13 14 52 | l--; 53 | count++; 54 | } 55 | cs++; 56 | 57 | } 58 | } 59 | } 60 | 61 | 62 | 63 | -------------------------------------------------------------------------------- /2D Arrays/RowWiseSum.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Solution { 3 | 4 | public static void rowWiseSum(int[][] mat) { 5 | int row = mat.length; 6 | int col = mat[0].length; 7 | 8 | int sum=0; 9 | for(int i = 0;i=0;j--) 66 | { 67 | all[k]=mat[r-j-1][j]; 68 | k++; 69 | } 70 | //removing duplicates from array 71 | Arrays.sort(all); 72 | 73 | // for(int i = 0;i arr[j]) { 11 | temp = arr[i]; 12 | arr[i] = arr[j]; 13 | arr[j] = temp; 14 | } 15 | } 16 | } 17 | 18 | for(int i =0;iarr[j]) 13 | { 14 | temp = arr[j]; 15 | arr[j]=arr[i]; 16 | arr[i]=temp; 17 | } 18 | } 19 | } 20 | 21 | } 22 | 23 | public static void main(String args[]) 24 | { 25 | int arr[] = {0,1,1,0,1,0,1}; 26 | sortZeroesAndOne(arr); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Arrays-1/SwapAlternate.java: -------------------------------------------------------------------------------- 1 | 2 | public class Solution { 3 | 4 | public static void swapAlternate(int arr[]) { 5 | //Your code goes here 6 | int n = arr.length; 7 | for(int i = 0;ix) 13 | { 14 | e = mid - 1; 15 | } 16 | else if (arr[mid]arr[j+1]) 13 | { 14 | int temp = arr[j]; 15 | arr[j]=arr[j+1]; 16 | arr[j+1]= temp; 17 | } 18 | } 19 | } 20 | } 21 | 22 | 23 | public static void main(String[] args) { 24 | // TODO Auto-generated method stub 25 | int arr[] = {2,13,4,1,3,6,28}; 26 | bubbleSort(arr); 27 | 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Arrays-2/CheckArrayRotation.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class Solution { 3 | 4 | public static int arrayRotateCheck(int[] arr){ 5 | //Your code goes here 6 | int n = arr.length; 7 | int ans = 0; 8 | for(int i = 0;i=0 && arr[j]>temp) 13 | { 14 | arr[j+1]=arr[j]; 15 | j--; 16 | } 17 | arr[j+1]=temp; 18 | } 19 | } 20 | 21 | public static void main(String args[]) 22 | { 23 | int arr[]={1,2,3,4,5,6,7,8,9}; 24 | insertionSort(arr); 25 | } 26 | 27 | 28 | } 29 | -------------------------------------------------------------------------------- /Arrays-2/MergeTwoSortedArrays.java: -------------------------------------------------------------------------------- 1 | 2 | public class Solution { 3 | 4 | public static int[] merge(int arr1[], int arr2[]) { 5 | //Your code goes here 6 | int m = arr1.length; 7 | int n = arr2.length; 8 | 9 | int arr[] = new int[m+n]; 10 | int i = 0; 11 | int j = 0; 12 | int k = 0; 13 | while(i= 0 && j >= 0) { 10 | int sum = arr1[i] + arr2[j] + carry; 11 | output[k] = sum % 10; 12 | carry = sum / 10; 13 | i -= 1; 14 | j -= 1; 15 | k -= 1; 16 | } 17 | while(i >= 0) { 18 | int sum = arr1[i] + carry; 19 | output[k] = sum % 10; 20 | carry = sum / 10; 21 | i -= 1; 22 | k -= 1; } 23 | while(j >= 0) { 24 | int sum = arr2[j] + carry; 25 | output[k] = sum % 10; 26 | carry = sum / 10; 27 | j -= 1; 28 | k -= 1; 29 | } 30 | output[0] = carry; 31 | } 32 | } 33 | 34 | -------------------------------------------------------------------------------- /BackTracking/RatInAMaze.java: -------------------------------------------------------------------------------- 1 | package allproblems; 2 | 3 | public class RatInAMaze { 4 | 5 | public static boolean ratInAMaze(int maze[][]) 6 | { 7 | int n = maze.length; //row length 8 | int path[][]= new int[n][n]; 9 | return solveMaze(maze, 0, 0, path); 10 | } 11 | 12 | public static boolean solveMaze(int maze[][],int i,int j, int path[][]) 13 | { 14 | int n = maze.length; 15 | //checking if the cell is a valid cell or not 16 | if(i<0 || i>=n || j<0 || j>=n || maze[i][j]==0 || path[i][j]==1) 17 | { 18 | return false; 19 | } 20 | //include cell in current path 21 | path[i][j]=1; 22 | 23 | //destination cell 24 | if(i==n-1 && j==n-1) 25 | { 26 | return true; 27 | } 28 | 29 | //explore in all directions 30 | //top 31 | if(solveMaze(maze, i-1, j, path)) 32 | { 33 | return true; 34 | } 35 | //right 36 | if(solveMaze(maze, i, j+1, path)) 37 | { 38 | return true; 39 | } 40 | //down 41 | if(solveMaze(maze, i+1, j, path)) 42 | { 43 | return true; 44 | } 45 | //left 46 | if(solveMaze(maze, i, j-1, path)) 47 | { 48 | return true; 49 | } 50 | 51 | return false; 52 | 53 | 54 | } 55 | 56 | public static void main(String[] args) { 57 | int maze[][]= {{1,1,0},{1,1,0},{1,1,1}}; 58 | boolean pathpossible = ratInAMaze(maze); 59 | System.out.println("Path exists?: "+pathpossible); 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /BackTracking/RatInAMazeAllPaths.java: -------------------------------------------------------------------------------- 1 | package allproblems; 2 | 3 | public class PrintRatInAMaze { 4 | 5 | public static void ratInMaze(int maze[][]) 6 | { 7 | int n = maze.length; 8 | int path[][]=new int[n][n]; 9 | printAllPaths(maze,0,0,path); 10 | } 11 | 12 | public static void printAllPaths(int maze[][],int i,int j, int path[][]) 13 | { 14 | int n = maze.length; 15 | if(i<0 || i>=n || j<0 || j>=n || maze[i][j]==0 || path[i][j]==1) 16 | { 17 | return; 18 | } 19 | path[i][j]=1; 20 | 21 | if(i==n-1 && j==n-1) 22 | { 23 | for(int r = 0;r { T data; BinaryTreeNode left; BinaryTreeNode 9 | * right; 10 | * 11 | * public BinaryTreeNode(T data) { this.data = data; } } 12 | */ 13 | 14 | /* 15 | * LinkedList Node Class 16 | * 17 | * 18 | * class LinkedListNode { T data; LinkedListNode next; 19 | * 20 | * public LinkedListNode(T data) { this.data = data; } } 21 | */ 22 | 23 | public static LinkedListNode constructLinkedList(BinaryTreeNode root) { 24 | ArrayList list = new ArrayList<>(); 25 | 26 | helper(root,list); 27 | 28 | Collections.sort(list); 29 | 30 | LinkedListNode head = null; 31 | LinkedListNode tail = null; 32 | int i=0; 33 | while(i cn = new LinkedListNode(list.get(i)); 36 | 37 | if(head==null) 38 | { 39 | head=cn; 40 | tail = cn; 41 | } 42 | else 43 | { 44 | tail.next=cn; 45 | tail=tail.next; 46 | } 47 | i++; 48 | } 49 | return head; 50 | } 51 | 52 | public static void helper(BinaryTreeNode root, ArrayList list) 53 | { 54 | if(root==null) 55 | { 56 | return; 57 | } 58 | 59 | list.add(root.data); 60 | helper(root.left,list); 61 | helper(root.right,list); 62 | 63 | 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /Binary Search Trees - I/BinaryTreeNode.java: -------------------------------------------------------------------------------- 1 | public class BinaryTreeNode{ 2 | T data; 3 | BinaryTreeNode left; 4 | BinaryTreeNode right; 5 | 6 | public BinaryTreeNode(T data){ 7 | this.data=data; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /Binary Search Trees - I/ConstructBST.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | 3 | /* Binary Tree Node class 4 | * 5 | * class BinaryTreeNode { 6 | T data; 7 | BinaryTreeNode left; 8 | BinaryTreeNode right; 9 | 10 | public BinaryTreeNode(T data) { 11 | this.data = data; 12 | } 13 | } 14 | */ 15 | 16 | public static BinaryTreeNode SortedArrayToBST(int[] arr, int n){ 17 | if(arr.length==0) 18 | { 19 | return null; 20 | } 21 | int mid; 22 | if(n%2!=0) 23 | { 24 | mid = n/2; 25 | } 26 | else 27 | { 28 | mid = (n/2) -1; 29 | } 30 | BinaryTreeNode root = new BinaryTreeNode<>(arr[mid]); 31 | int left[] = new int[mid]; 32 | for(int i =0;i { 6 | T data; 7 | BinaryTreeNode left; 8 | BinaryTreeNode right; 9 | 10 | public BinaryTreeNode(T data) { 11 | this.data = data; 12 | } 13 | } 14 | */ 15 | 16 | public static void elementsInRangeK1K2(BinaryTreeNode root,int k1,int k2){ 17 | if(root==null) 18 | { 19 | return; 20 | } 21 | if(root.data>=k1 && root.data<=k2) 22 | { 23 | elementsInRangeK1K2(root.left,k1,k2); 24 | System.out.print(root.data+" "); 25 | elementsInRangeK1K2(root.right,k1,k2); 26 | } 27 | else if(root.datak2) 32 | { 33 | elementsInRangeK1K2(root.left,k1,k2); 34 | } 35 | } 36 | 37 | 38 | } 39 | -------------------------------------------------------------------------------- /Binary Search Trees - I/IsBSTReturn.java: -------------------------------------------------------------------------------- 1 | public class IsBSTReturn 2 | { 3 | int min; 4 | int max; 5 | boolean isBST; 6 | 7 | public IsBSTReturn(int min,int max,boolean isBST){ 8 | this.min=min; 9 | this.max=max; 10 | this.isBST=isBST; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Binary Search Trees - I/LCAofBST.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | 3 | /* 4 | * Binary Tree Node class 5 | * 6 | * class BinaryTreeNode { T data; BinaryTreeNode left; BinaryTreeNode right; 7 | * 8 | * public BinaryTreeNode(T data) { this.data = data; } } 9 | */ 10 | 11 | 12 | 13 | public static int getLCA(BinaryTreeNode root, int a, int b) { 14 | 15 | if(root==null) 16 | { 17 | return -1; 18 | } 19 | 20 | if(aroot.data && b>root.data) 25 | { 26 | return getLCA(root.right,a,b); 27 | } 28 | return root.data; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Binary Search Trees - I/PairSuminBST.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class Solution { 3 | /* 4 | * BinaryTreeNode class 5 | * 6 | * class BinaryTreeNode 7 | * { 8 | * T data; 9 | * BinaryTreeNode left; 10 | * BinaryTreeNode right; 11 | * public BinaryTreeNode(T data) 12 | * { 13 | * this.data = data; 14 | * } 15 | * } 16 | */ 17 | 18 | public static void printNodesSumToS(BinaryTreeNode root, int s) { 19 | ArrayList list = new ArrayList<>(); 20 | helper(root,list); 21 | 22 | int i = 0,j=list.size()-1; 23 | 24 | while(i root,ArrayList list) 45 | { 46 | if(root==null) 47 | { 48 | return; 49 | } 50 | helper(root.left,list); 51 | list.add(root.data); 52 | helper(root.right,list); 53 | } 54 | 55 | 56 | } 57 | -------------------------------------------------------------------------------- /Binary Search Trees - I/SearchInBST.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | 3 | /* 4 | * Binary Tree Node class 5 | * 6 | * class BinaryTreeNode { T data; BinaryTreeNode left; BinaryTreeNode right; 7 | * 8 | * public BinaryTreeNode(T data) { this.data = data; } } 9 | */ 10 | 11 | 12 | public static boolean searchInBST(BinaryTreeNode root, int k) { 13 | if(root==null) 14 | { 15 | return false; 16 | } 17 | 18 | if(root.data==k) 19 | { 20 | return true; 21 | } 22 | 23 | else if(k { 9 | * T data; 10 | * BinaryTreeNode left; 11 | * BinaryTreeNode right; 12 | * public BinaryTreeNode(T data) 13 | * { 14 | * this.data = data; 15 | * } 16 | * } 17 | */ 18 | 19 | public static ArrayList getPath(BinaryTreeNode root, int data){ 20 | if(root==null) 21 | { 22 | return null; 23 | } 24 | 25 | if(root.data==data) 26 | { 27 | ArrayList output = new ArrayList<>(); 28 | output.add(root.data); 29 | return output; 30 | } 31 | 32 | if(data leftO = getPath(root.left,data); 35 | if(leftO!=null) 36 | { 37 | leftO.add(root.data); 38 | return leftO; 39 | } 40 | } 41 | else if(data>=root.data) 42 | { 43 | ArrayList rightO = getPath(root.right,data); 44 | if(rightO!=null) 45 | { 46 | rightO.add(root.data); 47 | return rightO; 48 | } 49 | } 50 | 51 | 52 | return null; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /Binary Trees - II/BalanceBetter.java: -------------------------------------------------------------------------------- 1 | package binarytress; 2 | 3 | import java.util.Scanner; 4 | 5 | public class BalanceBetter { 6 | 7 | public static BinaryTreeNode takeInput(boolean isRoot,int parentData, boolean isLeft){ 8 | if(isRoot) 9 | { 10 | System.out.println("Enter root data: "); 11 | } 12 | else 13 | { 14 | if(isLeft) 15 | { 16 | System.out.println("Enter left node of "+parentData); 17 | } 18 | else 19 | { 20 | System.out.println("Enter right node of "+parentData); 21 | } 22 | } 23 | Scanner sc = new Scanner(System.in); 24 | int nodeData = sc.nextInt(); 25 | if(nodeData==-1) 26 | { 27 | return null; 28 | } 29 | BinaryTreeNode mainRoot = new BinaryTreeNode<>(nodeData); 30 | BinaryTreeNode leftRoot = takeInput(false, nodeData, true); 31 | BinaryTreeNode rightRoot = takeInput(false, nodeData, false); 32 | 33 | mainRoot.left=leftRoot; 34 | mainRoot.right=rightRoot; 35 | 36 | return mainRoot; 37 | } 38 | 39 | public static BalancedTreeReturn balancebetter(BinaryTreeNode root) 40 | { 41 | if(root == null) 42 | { 43 | int height = 0; 44 | boolean isBal = true; 45 | BalancedTreeReturn ans = new BalancedTreeReturn(); 46 | ans.height=height; 47 | ans.isBalanced=isBal; 48 | return ans; 49 | } 50 | BalancedTreeReturn leftOutput = balancebetter(root.left); 51 | BalancedTreeReturn rightOutput = balancebetter(root.right); 52 | boolean isBal = true; 53 | 54 | int height = 1 + Math.max(leftOutput.height, rightOutput.height); 55 | 56 | if(Math.abs(leftOutput.height - rightOutput.height) > 1) 57 | { 58 | isBal = false; 59 | } 60 | if(!leftOutput.isBalanced || !rightOutput.isBalanced) 61 | { 62 | isBal = false; 63 | } 64 | BalancedTreeReturn ans = new BalancedTreeReturn(); 65 | ans.height=height; 66 | ans.isBalanced = isBal; 67 | return ans; 68 | } 69 | 70 | public static void main(String[] args) { 71 | BinaryTreeNode root = takeInput(true, 0, true); 72 | System.out.println("Is balanced : "+ balancebetter(root).isBalanced); 73 | 74 | } 75 | 76 | } 77 | -------------------------------------------------------------------------------- /Binary Trees - II/BalancedTreeReturn.java: -------------------------------------------------------------------------------- 1 | package binarytress; 2 | 3 | public class BalancedTreeReturn { 4 | 5 | int height; 6 | boolean isBalanced; 7 | 8 | } 9 | -------------------------------------------------------------------------------- /Binary Trees - II/CreateAndInsertDuplicateNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Following is the structure used to represent the Binary Tree Node 4 | 5 | class BinaryTreeNode { 6 | T data; 7 | BinaryTreeNode left; 8 | BinaryTreeNode right; 9 | 10 | public BinaryTreeNode(T data) { 11 | this.data = data; 12 | this.left = null; 13 | this.right = null; 14 | } 15 | } 16 | 17 | */ 18 | 19 | public class Solution { 20 | 21 | public static void insertDuplicateNode(BinaryTreeNode root) { 22 | if(root==null) 23 | { 24 | return; 25 | } 26 | 27 | BinaryTreeNode dup = new BinaryTreeNode(root.data); 28 | BinaryTreeNode remaining = root.left; 29 | 30 | root.left=dup; 31 | dup.left=remaining; 32 | 33 | insertDuplicateNode(remaining); 34 | insertDuplicateNode(root.right); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /Binary Trees - II/InOrderAndPostOrder.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Following is the structure used to represent the Binary Tree Node 4 | 5 | class BinaryTreeNode { 6 | T data; 7 | BinaryTreeNode left; 8 | BinaryTreeNode right; 9 | 10 | public BinaryTreeNode(T data) { 11 | this.data = data; 12 | this.left = null; 13 | this.right = null; 14 | } 15 | } 16 | 17 | */ 18 | 19 | public class Solution { 20 | 21 | public static BinaryTreeNode buildTree(int[] postOrder, int[] inOrder) { 22 | 23 | return helper(postOrder,inOrder,0,postOrder.length-1,0,inOrder.length-1); 24 | } 25 | 26 | public static BinaryTreeNode helper(int[] po,int in[],int siPo,int eiPo, int siIn,int eiIn) 27 | { 28 | if(siIn>eiIn) 29 | { 30 | return null; 31 | } 32 | 33 | BinaryTreeNode root = new BinaryTreeNode(po[eiPo]); 34 | 35 | int position = -1; 36 | for(int i =siIn;i<=eiIn;i++) 37 | { 38 | if(in[i]==root.data) 39 | { 40 | position=i; 41 | break; 42 | } 43 | } 44 | 45 | 46 | root.left= helper(po,in,siPo,siPo+(position-1-siIn),siIn,position-1); 47 | root.right = helper(po,in,siPo+(position-1-siIn)+1,eiPo-1,position+1,eiIn); 48 | 49 | return root; 50 | 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /Binary Trees - II/InOrderAndPreOrder.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Following is the structure used to represent the Binary Tree Node 4 | 5 | class BinaryTreeNode { 6 | T data; 7 | BinaryTreeNode left; 8 | BinaryTreeNode right; 9 | 10 | public BinaryTreeNode(T data) { 11 | this.data = data; 12 | this.left = null; 13 | this.right = null; 14 | } 15 | } 16 | 17 | */ 18 | 19 | public class Solution { 20 | 21 | public static BinaryTreeNode buildTree(int[] preOrder, int[] inOrder) { 22 | BinaryTreeNode root = helper(preOrder,inOrder,0,preOrder.length-1,0,inOrder.length-1); 23 | return root; 24 | } 25 | 26 | public static BinaryTreeNode helper(int[] pre, int[] in, int siPre,int eiPre, int siIn, int eiIn){ 27 | if(siPre>eiPre) 28 | { 29 | return null; 30 | } 31 | int rootData = pre[siPre]; 32 | BinaryTreeNode rootmain = new BinaryTreeNode(rootData); 33 | 34 | int rootIndex = -1; 35 | for(int i = siIn;i<=eiIn;i++) 36 | { 37 | if(in[i]==rootData) 38 | { 39 | rootIndex=i; 40 | break; 41 | } 42 | } 43 | int siPreLeft = siPre +1; 44 | int siInLeft = siIn; 45 | int eiInLeft = rootIndex - 1; 46 | int siInRight = rootIndex + 1; 47 | int eiPreRight= eiPre; 48 | int eiInRight = eiIn; 49 | 50 | int LeftSubTreeLength = eiInLeft - siInLeft + 1; 51 | int eiPreLeft = siPreLeft + LeftSubTreeLength-1; 52 | int siPreRight = eiPreLeft+1; 53 | 54 | BinaryTreeNode left = helper(pre,in,siPreLeft,eiPreLeft,siInLeft,eiInLeft); 55 | BinaryTreeNode right = helper(pre,in,siPreRight,eiPreRight,siInRight,eiInRight); 56 | 57 | rootmain.left=left; 58 | rootmain.right=right; 59 | 60 | return rootmain; 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /Binary Trees - II/IsBalanced.java: -------------------------------------------------------------------------------- 1 | package binarytress; 2 | 3 | import java.util.Scanner; 4 | 5 | public class IsBalanced { 6 | 7 | public static BinaryTreeNode takeInput(boolean isRoot,int parentData, boolean isLeft){ 8 | if(isRoot) 9 | { 10 | System.out.println("Enter root data: "); 11 | } 12 | else 13 | { 14 | if(isLeft) 15 | { 16 | System.out.println("Enter left node of "+parentData); 17 | } 18 | else 19 | { 20 | System.out.println("Enter right node of "+parentData); 21 | } 22 | } 23 | Scanner sc = new Scanner(System.in); 24 | int nodeData = sc.nextInt(); 25 | if(nodeData==-1) 26 | { 27 | return null; 28 | } 29 | BinaryTreeNode mainRoot = new BinaryTreeNode<>(nodeData); 30 | BinaryTreeNode leftRoot = takeInput(false, nodeData, true); 31 | BinaryTreeNode rightRoot = takeInput(false, nodeData, false); 32 | 33 | mainRoot.left=leftRoot; 34 | mainRoot.right=rightRoot; 35 | 36 | return mainRoot; 37 | } 38 | 39 | public static int height(BinaryTreeNode root) 40 | { 41 | if(root==null) 42 | { 43 | return 0; 44 | } 45 | 46 | int leftHeight= height(root.left); 47 | int rightHeight = height(root.right); 48 | return 1+Math.max(leftHeight, rightHeight); 49 | } 50 | public static boolean isBalanced(BinaryTreeNode root) 51 | { 52 | if(root == null) 53 | { 54 | return true; 55 | } 56 | int leftHeight = height(root.left); 57 | int rightHeight = height(root.right); 58 | 59 | if(Math.abs(leftHeight - rightHeight)>1) 60 | { 61 | return false; 62 | } 63 | 64 | boolean isLeftBalanced = isBalanced(root.left); 65 | boolean isRightBalanced = isBalanced(root.right); 66 | 67 | return isLeftBalanced && isRightBalanced; 68 | } 69 | 70 | public static void main(String[] args) { 71 | BinaryTreeNode root = takeInput(true, 0, true); 72 | System.out.println("Is balanced: "+isBalanced(root)); 73 | 74 | } 75 | 76 | } 77 | -------------------------------------------------------------------------------- /Binary Trees - II/LevelOrderTraversal.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Following is the structure used to represent the Binary Tree Node 4 | 5 | class BinaryTreeNode { 6 | T data; 7 | BinaryTreeNode left; 8 | BinaryTreeNode right; 9 | 10 | public BinaryTreeNode(T data) { 11 | this.data = data; 12 | this.left = null; 13 | this.right = null; 14 | } 15 | } 16 | 17 | */ 18 | import java.util.LinkedList; 19 | import java.util.Queue; 20 | public class Solution { 21 | 22 | public static void printLevelWise(BinaryTreeNode root) { 23 | if(root==null) 24 | { 25 | return; 26 | } 27 | Queue> q = new LinkedList<>(); 28 | q.add(root); 29 | q.add(null); 30 | 31 | while(!q.isEmpty()) 32 | { 33 | BinaryTreeNode front = q.poll(); 34 | 35 | if(front==null) 36 | { 37 | System.out.println(); 38 | if(!q.isEmpty()) 39 | { 40 | q.add(null); 41 | } 42 | } 43 | else 44 | { 45 | System.out.print(front.data+" "); 46 | if(front.left!=null) 47 | { 48 | q.add(front.left); 49 | } 50 | if(front.right!=null) 51 | { 52 | q.add(front.right); 53 | } 54 | } 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /Binary Trees - II/MinAndMax.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Following is the structure used to represent the Binary Tree Node 4 | 5 | class BinaryTreeNode { 6 | T data; 7 | BinaryTreeNode left; 8 | BinaryTreeNode right; 9 | 10 | public BinaryTreeNode(T data) { 11 | this.data = data; 12 | this.left = null; 13 | this.right = null; 14 | } 15 | } 16 | 17 | */ 18 | 19 | 20 | 21 | /* 22 | 23 | Representation of the Pair Class 24 | 25 | class Pair { 26 | T minimum; 27 | U maximum; 28 | 29 | public Pair(T minimum, U maximum) { 30 | this.minimum = minimum; 31 | this.maximum = maximum; 32 | } 33 | 34 | } 35 | 36 | */ 37 | 38 | public class Solution { 39 | 40 | public static Pair getMinAndMax(BinaryTreeNode root) { 41 | if(root==null) 42 | { 43 | Pair ans = new Pair(0,0); 44 | return ans; 45 | } 46 | 47 | int max = helpermax(root); 48 | int min = helpermin(root); 49 | 50 | Pair ans = new Pair(min,max); 51 | 52 | return ans; 53 | 54 | } 55 | 56 | public static int helpermax(BinaryTreeNode root) 57 | { 58 | if(root==null) 59 | { 60 | return 0; 61 | } 62 | 63 | int base = root.data; 64 | int lrec = helpermax(root.left); 65 | int rrec = helpermax(root.right); 66 | 67 | if(lrec>base) 68 | { 69 | base = lrec; 70 | } 71 | if(rrec>base) 72 | { 73 | base=rrec; 74 | } 75 | return base; 76 | 77 | 78 | } 79 | 80 | public static int helpermin(BinaryTreeNode root) 81 | { 82 | if(root==null) 83 | { 84 | return Integer.MAX_VALUE; 85 | } 86 | int base = root.data; 87 | int lrec = helpermin(root.left); 88 | int rrec = helpermin(root.right); 89 | 90 | if (lrec < base) 91 | base = lrec; 92 | if (rrec < base) 93 | base = rrec; 94 | return base; 95 | } 96 | 97 | } 98 | -------------------------------------------------------------------------------- /Binary Trees - II/MirrorBinaryTree.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Following is the structure used to represent the Binary Tree Node 4 | 5 | class BinaryTreeNode { 6 | T data; 7 | BinaryTreeNode left; 8 | BinaryTreeNode right; 9 | 10 | public BinaryTreeNode(T data) { 11 | this.data = data; 12 | this.left = null; 13 | this.right = null; 14 | } 15 | } 16 | 17 | */ 18 | 19 | public class Solution { 20 | 21 | public static void mirrorBinaryTree(BinaryTreeNode root){ 22 | if(root==null) 23 | { 24 | return; 25 | } 26 | 27 | if(root.left==null && root.right==null) 28 | { 29 | return; 30 | } 31 | 32 | BinaryTreeNode temp = root.left; 33 | root.left=root.right; 34 | root.right=temp; 35 | mirrorBinaryTree(root.left); 36 | mirrorBinaryTree(root.right); 37 | 38 | 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /Binary Trees - II/PathSumRootToLeaf.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Following is the structure used to represent the Binary Tree Node 4 | 5 | class BinaryTreeNode { 6 | T data; 7 | BinaryTreeNode left; 8 | BinaryTreeNode right; 9 | 10 | public BinaryTreeNode(T data) { 11 | this.data = data; 12 | this.left = null; 13 | this.right = null; 14 | } 15 | } 16 | 17 | */ 18 | import java.util.*; 19 | public class Solution { 20 | 21 | public static void rootToLeafPathsSumToK(BinaryTreeNode root, int k) { 22 | ArrayList path = new ArrayList<>(); 23 | 24 | helper(root,path,k); 25 | 26 | 27 | } 28 | 29 | public static void helper(BinaryTreeNode root, ArrayList path, int k) 30 | { 31 | if(root==null) 32 | { 33 | return; 34 | } 35 | 36 | k=k-root.data; 37 | path.add(root.data);// 2 3 4 4 38 | if(root.left==null && root.right==null) 39 | { 40 | if(k==0) 41 | { 42 | for(int i=0;i takeInput(boolean isRoot,int parentData, boolean isLeft){ 8 | if(isRoot) 9 | { 10 | System.out.println("Enter root data: "); 11 | } 12 | else 13 | { 14 | if(isLeft) 15 | { 16 | System.out.println("Enter left node of "+parentData); 17 | } 18 | else 19 | { 20 | System.out.println("Enter right node of "+parentData); 21 | } 22 | } 23 | Scanner sc = new Scanner(System.in); 24 | int nodeData = sc.nextInt(); 25 | if(nodeData==-1) 26 | { 27 | return null; 28 | } 29 | BinaryTreeNode mainRoot = new BinaryTreeNode<>(nodeData); 30 | BinaryTreeNode leftRoot = takeInput(false, nodeData, true); 31 | BinaryTreeNode rightRoot = takeInput(false, nodeData, false); 32 | 33 | mainRoot.left=leftRoot; 34 | mainRoot.right=rightRoot; 35 | 36 | return mainRoot; 37 | } 38 | 39 | public static BinaryTreeNode RemoveLeafNodes(BinaryTreeNode root) 40 | { 41 | if(root==null) 42 | { 43 | return null; 44 | } 45 | if(root.left==null && root.right==null) 46 | { 47 | return null; 48 | } 49 | root.left=RemoveLeafNodes(root.left); 50 | root.right=RemoveLeafNodes(root.right); 51 | 52 | return root; 53 | } 54 | 55 | public static void print(BinaryTreeNode root) 56 | { 57 | if(root==null) 58 | { 59 | return; 60 | } 61 | System.out.print(root.data+": "); 62 | if(root.left!=null) 63 | { 64 | System.out.print("L"+root.left.data); 65 | } 66 | if(root.right!=null) 67 | { 68 | System.out.print(",R"+root.right.data); 69 | } 70 | System.out.println(); 71 | print(root.left); 72 | print(root.right); 73 | } 74 | 75 | public static void main(String[] args) { 76 | BinaryTreeNode root = takeInput(true, 0, true); 77 | BinaryTreeNode ans = RemoveLeafNodes(root); 78 | print(ans); 79 | 80 | } 81 | 82 | } 83 | -------------------------------------------------------------------------------- /Binary Trees-1/HeightOfTree.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Following is the structure used to represent the Binary Tree Node 4 | 5 | class BinaryTreeNode { 6 | T data; 7 | BinaryTreeNode left; 8 | BinaryTreeNode right; 9 | 10 | public BinaryTreeNode(T data) { 11 | this.data = data; 12 | this.left = null; 13 | this.right = null; 14 | } 15 | } 16 | 17 | */ 18 | 19 | public class Solution { 20 | 21 | public static int height(BinaryTreeNode root) { 22 | if(root==null) 23 | { 24 | return 0; 25 | } 26 | int LeftHeight = height(root.left); 27 | int RightHeight = height(root.right); 28 | 29 | return 1 + Math.max(LeftHeight,RightHeight); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /Binary Trees-1/IsNodePresent.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Following is the structure used to represent the Binary Tree Node 4 | 5 | class BinaryTreeNode { 6 | T data; 7 | BinaryTreeNode left; 8 | BinaryTreeNode right; 9 | 10 | public BinaryTreeNode(T data) { 11 | this.data = data; 12 | this.left = null; 13 | this.right = null; 14 | } 15 | } 16 | 17 | */ 18 | 19 | public class Solution { 20 | 21 | public static boolean isNodePresent(BinaryTreeNode root, int x) 22 | { 23 | if(root==null) 24 | { 25 | return false; 26 | } 27 | boolean ans = false; 28 | if(root.data==x) 29 | { 30 | return true; 31 | } 32 | else 33 | { 34 | if(root.left!=null) 35 | { 36 | ans = isNodePresent(root.left, x); 37 | if(ans==true) 38 | { 39 | return ans; 40 | } 41 | } 42 | if(root.right!=null) 43 | { 44 | ans = isNodePresent(root.right, x); 45 | if(ans==true) 46 | { 47 | return ans; 48 | } 49 | } 50 | } 51 | return ans; 52 | } 53 | } 54 | 55 | // best solution: 56 | 57 | /* public class Solution { 58 | 59 | public static boolean isNodePresent(BinaryTreeNode root, int x) 60 | { 61 | if(root==null) 62 | { 63 | return false; 64 | } 65 | 66 | if(root.data==x) 67 | { 68 | return true; 69 | } 70 | 71 | return (isNodePresent(root.left,x) || isNodePresent(root.right,x)); 72 | } 73 | } */ 74 | -------------------------------------------------------------------------------- /Binary Trees-1/NodeWithLargestData.java: -------------------------------------------------------------------------------- 1 | package binarytress; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Largest { 6 | 7 | public static BinaryTreeNode takeInput(boolean isRoot,int parentData, boolean isLeft){ 8 | if(isRoot) 9 | { 10 | System.out.println("Enter root data: "); 11 | } 12 | else 13 | { 14 | if(isLeft) 15 | { 16 | System.out.println("Enter the leftNode of "+parentData); 17 | } 18 | else 19 | { 20 | System.out.println("Enter the rightNode of "+ parentData); 21 | } 22 | } 23 | Scanner sc=new Scanner(System.in); 24 | int rootData = sc.nextInt(); 25 | if(rootData==-1) 26 | { 27 | return null; 28 | } 29 | BinaryTreeNode mainRoot = new BinaryTreeNode(rootData); 30 | BinaryTreeNode leftNode = takeInput(false, rootData, true); 31 | BinaryTreeNode rightNode = takeInput(false, rootData, false); 32 | 33 | mainRoot.left=leftNode; 34 | mainRoot.right=rightNode; 35 | 36 | return mainRoot; 37 | } 38 | 39 | public static void print(BinaryTreeNode root) 40 | { 41 | if(root==null) 42 | { 43 | return; 44 | } 45 | System.out.print(root.data+" : "); 46 | if(root.left!=null) 47 | { 48 | System.out.print("L"+root.left.data); 49 | } 50 | if(root.right!=null) 51 | { 52 | System.out.print(",R"+root.right.data); 53 | } 54 | System.out.println(); 55 | print(root.left); 56 | } 57 | 58 | public static int largest(BinaryTreeNode root) 59 | { 60 | if(root==null) 61 | { 62 | return -1; 63 | } 64 | int LargestLeft = largest(root.left); 65 | int LargestRight= largest(root.right); 66 | 67 | return Math.max(root.data, Math.max(LargestLeft, LargestRight)); 68 | } 69 | 70 | public static void main(String[] args) { 71 | BinaryTreeNode root = takeInput(true, 0, true); 72 | System.out.println("largest root: "+largest(root)); 73 | 74 | } 75 | 76 | } 77 | -------------------------------------------------------------------------------- /Binary Trees-1/NodesGreaterThanX.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Following is the structure used to represent the Binary Tree Node 4 | 5 | class BinaryTreeNode { 6 | T data; 7 | BinaryTreeNode left; 8 | BinaryTreeNode right; 9 | 10 | public BinaryTreeNode(T data) { 11 | this.data = data; 12 | this.left = null; 13 | this.right = null; 14 | } 15 | } 16 | 17 | */ 18 | 19 | public class Solution { 20 | 21 | public static int countNodesGreaterThanX(BinaryTreeNode root, int x) { 22 | if(root==null) 23 | { 24 | return -1; 25 | } 26 | 27 | int count = (root.data>x)?1:0; 28 | if(root.left!=null) 29 | { 30 | count+=countNodesGreaterThanX(root.left,x); 31 | } 32 | if(root.right!=null) 33 | { 34 | count+=countNodesGreaterThanX(root.right,x); 35 | } 36 | return count; 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /Binary Trees-1/NodesWithoutSibling.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Following is the structure used to represent the Binary Tree Node 4 | 5 | class BinaryTreeNode { 6 | T data; 7 | BinaryTreeNode left; 8 | BinaryTreeNode right; 9 | 10 | public BinaryTreeNode(T data) { 11 | this.data = data; 12 | this.left = null; 13 | this.right = null; 14 | } 15 | } 16 | 17 | */ 18 | 19 | public class Solution { 20 | 21 | public static void printNodesWithoutSibling(BinaryTreeNode root) { 22 | 23 | if(root == null) 24 | { 25 | return; 26 | } 27 | 28 | if(root.left==null && root.right!=null) 29 | { 30 | System.out.print(root.right.data+" "); 31 | 32 | } 33 | 34 | if(root.left!=null && root.right==null) 35 | { 36 | System.out.print(root.left.data+" "); 37 | 38 | } 39 | printNodesWithoutSibling(root.left); 40 | printNodesWithoutSibling(root.right); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /Binary Trees-1/NumOfLeafNodes.java: -------------------------------------------------------------------------------- 1 | package binarytress; 2 | 3 | import java.util.Scanner; 4 | 5 | public class NumOfLeafNodes { 6 | 7 | public static BinaryTreeNode takeInput(boolean isRoot,int parentData, boolean isLeft){ 8 | if(isRoot) 9 | { 10 | System.out.println("Enter root data: "); 11 | } 12 | else 13 | { 14 | if(isLeft) 15 | { 16 | System.out.println("Enter the leftNode of "+parentData); 17 | } 18 | else 19 | { 20 | System.out.println("Enter the rightNode of "+ parentData); 21 | } 22 | } 23 | Scanner sc=new Scanner(System.in); 24 | int rootData = sc.nextInt(); 25 | if(rootData==-1) 26 | { 27 | return null; 28 | } 29 | BinaryTreeNode mainRoot = new BinaryTreeNode(rootData); 30 | BinaryTreeNode leftNode = takeInput(false, rootData, true); 31 | BinaryTreeNode rightNode = takeInput(false, rootData, false); 32 | 33 | mainRoot.left=leftNode; 34 | mainRoot.right=rightNode; 35 | 36 | return mainRoot; 37 | } 38 | 39 | public static int NumOfLeaves(BinaryTreeNode root) { 40 | if(root==null) 41 | { 42 | return 0; 43 | } 44 | 45 | if(root.left==null && root.right==null) 46 | { 47 | return 1; 48 | } 49 | 50 | return NumOfLeaves(root.left) + NumOfLeaves(root.right); 51 | } 52 | 53 | public static void main(String[] args) { 54 | BinaryTreeNode root = takeInput(true, 0,true); 55 | System.out.println("Number of Leaf nodes: "+NumOfLeaves(root)); 56 | 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /Binary Trees-1/PostOrderBinaryTree.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Following the structure used for Binary Tree 4 | 5 | class BinaryTreeNode { 6 | T data; 7 | BinaryTreeNode left; 8 | BinaryTreeNode right; 9 | 10 | public BinaryTreeNode(T data) { 11 | this.data = data; 12 | this.left = null; 13 | this.right = null; 14 | } 15 | } 16 | 17 | */ 18 | 19 | public class Solution { 20 | 21 | public static void postOrder(BinaryTreeNode root) { 22 | if(root==null) 23 | { 24 | return; 25 | } 26 | postOrder(root.left); 27 | postOrder(root.right); 28 | System.out.print(root.data+" "); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /Binary Trees-1/PreOrderBinaryTree.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Following the structure used for Binary Tree 4 | 5 | class BinaryTreeNode { 6 | T data; 7 | BinaryTreeNode left; 8 | BinaryTreeNode right; 9 | 10 | public BinaryTreeNode(T data) { 11 | this.data = data; 12 | this.left = null; 13 | this.right = null; 14 | } 15 | } 16 | 17 | */ 18 | 19 | public class Solution { 20 | 21 | public static void preOrder(BinaryTreeNode root) { 22 | if(root==null) 23 | { 24 | return; 25 | } 26 | 27 | System.out.print(root.data+" "); 28 | preOrder(root.left); 29 | preOrder(root.right); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /Binary Trees-1/PrintNodesAtDepthK.java: -------------------------------------------------------------------------------- 1 | package binarytress; 2 | 3 | import java.util.Scanner; 4 | 5 | public class PrintNodesAtDepthK { 6 | 7 | public static BinaryTreeNode takeInput(boolean isRoot,int parentData, boolean isLeft){ 8 | if(isRoot) 9 | { 10 | System.out.println("Enter root data: "); 11 | } 12 | else 13 | { 14 | if(isLeft) 15 | { 16 | System.out.println("Enter the leftNode of "+parentData); 17 | } 18 | else 19 | { 20 | System.out.println("Enter the rightNode of "+ parentData); 21 | } 22 | } 23 | Scanner sc=new Scanner(System.in); 24 | int rootData = sc.nextInt(); 25 | if(rootData==-1) 26 | { 27 | return null; 28 | } 29 | BinaryTreeNode mainRoot = new BinaryTreeNode(rootData); 30 | BinaryTreeNode leftNode = takeInput(false, rootData, true); 31 | BinaryTreeNode rightNode = takeInput(false, rootData, false); 32 | 33 | mainRoot.left=leftNode; 34 | mainRoot.right=rightNode; 35 | 36 | return mainRoot; 37 | } 38 | 39 | public static void print(BinaryTreeNode root) 40 | { 41 | if(root==null) 42 | { 43 | return; 44 | } 45 | System.out.print(root.data+" : "); 46 | if(root.left!=null) 47 | { 48 | System.out.print("L"+root.left.data); 49 | } 50 | if(root.right!=null) 51 | { 52 | System.out.print(",R"+root.right.data); 53 | } 54 | System.out.println(); 55 | print(root.left); 56 | } 57 | 58 | public static void printatK(BinaryTreeNode root, int k) 59 | { 60 | if(root==null) 61 | { 62 | return; 63 | } 64 | if(k==0) 65 | { 66 | System.out.print(root.data+" "); 67 | return; 68 | } 69 | printatK(root.left,k-1); 70 | printatK(root.right,k-1); 71 | } 72 | 73 | public static void main(String[] args) { 74 | BinaryTreeNode root = takeInput(true, 0,true); 75 | printatK(root, 2); 76 | 77 | } 78 | 79 | } 80 | -------------------------------------------------------------------------------- /Binary Trees-1/ReplaceNodeWithDepth.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Following is the structure used to represent the Binary Tree Node 4 | 5 | class BinaryTreeNode { 6 | T data; 7 | BinaryTreeNode left; 8 | BinaryTreeNode right; 9 | 10 | public BinaryTreeNode(T data) { 11 | this.data = data; 12 | this.left = null; 13 | this.right = null; 14 | } 15 | } 16 | 17 | */ 18 | 19 | public class Solution { 20 | 21 | public static void changeToDepthTree(BinaryTreeNode root) { 22 | helper(root,0); 23 | } 24 | 25 | public static BinaryTreeNode helper(BinaryTreeNode root, int depth) 26 | { 27 | if(root==null) 28 | { 29 | return null; 30 | } 31 | 32 | root.data=depth; 33 | if(root.left!=null) 34 | { 35 | helper(root.left,depth+1); 36 | } 37 | if(root.right!=null) 38 | { 39 | helper(root.right,depth+1); 40 | } 41 | return root; 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /Binary Trees-1/SumOfNodes.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Following is the Binary Tree Node class structure. 4 | 5 | class BinaryTreeNode { 6 | T data; 7 | BinaryTreeNode left; 8 | BinaryTreeNode right; 9 | 10 | public BinaryTreeNode(T data) { 11 | this.data = data; 12 | this.left = null; 13 | this.right = null; 14 | } 15 | } 16 | 17 | */ 18 | 19 | public class Solution { 20 | 21 | public static int getSum(BinaryTreeNode root) { 22 | if(root==null) 23 | { 24 | return 0; 25 | } 26 | int leftSum = getSum(root.left); 27 | int rightSum = getSum(root.right); 28 | 29 | return root.data + leftSum + rightSum; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /Conditionals And Loops/Factors.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Solution { 3 | 4 | public static void main(String[] args) { 5 | // Write your code here 6 | int num; 7 | Scanner sc= new Scanner(System.in); 8 | num = sc.nextInt(); 9 | int i=2; 10 | while(i=65 && i <=90) 12 | { 13 | System.out.println("1"); 14 | } 15 | else if(i>=97 && i<=122) 16 | { 17 | System.out.println("0"); 18 | } 19 | else 20 | { 21 | System.out.println("-1"); 22 | } 23 | 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Conditionals And Loops/FindPowerofaNumber.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Solution { 3 | 4 | public static void main(String[] args) { 5 | // Write your code here 6 | int number; 7 | int power; 8 | Scanner sc = new Scanner(System.in); 9 | number = sc.nextInt(); 10 | power = sc.nextInt(); 11 | 12 | int answer=1; 13 | 14 | int i = 1; 15 | while(i<=power) 16 | { 17 | answer = answer * number; 18 | i=i+1; 19 | } 20 | System.out.println(answer); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /Conditionals And Loops/MultiplicationTable.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Main { 3 | 4 | public static void main(String[] args) { 5 | // Write your code here 6 | int number,number1; 7 | int i =1; 8 | Scanner sc = new Scanner(System.in); 9 | number = sc.nextInt(); 10 | while(0<=number && number<=10000 && i<=10) 11 | { 12 | number1 = number*i; 13 | i=i+1; 14 | System.out.println(number1); 15 | } 16 | 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Conditionals And Loops/SumOfEvenandOdd.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Main { 3 | 4 | public static void main(String[] args) { 5 | // Write your code here 6 | int evensum=0; 7 | int oddsum=0; 8 | int n; 9 | Scanner sc = new Scanner(System.in); 10 | n=sc.nextInt(); 11 | 12 | 13 | while(n>0) { 14 | int dummynum = n %10; 15 | 16 | if(dummynum %2 == 0) 17 | { 18 | evensum=evensum + dummynum; 19 | } 20 | else 21 | { 22 | oddsum = oddsum+dummynum; 23 | } 24 | 25 | n=n/10; 26 | 27 | } 28 | System.out.println(evensum+" "+oddsum); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /Conditionals And Loops/TotalSalary.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Main { 3 | 4 | public static void main(String[] args) { 5 | // Write your code here 6 | double bs,hra,da,pf,fs; 7 | int allow,printsalary; 8 | char grade; 9 | Scanner sc = new Scanner(System.in); 10 | bs = sc.nextDouble(); 11 | grade = sc.next().charAt(0); 12 | hra=0.20*bs; 13 | da=0.50*bs; 14 | pf=0.11*bs; 15 | if(grade=='A') 16 | { 17 | allow = 1700; 18 | fs = bs+hra+da+allow-pf; 19 | printsalary = (int)Math.round(fs); 20 | System.out.println(printsalary); 21 | } 22 | else if(grade=='B') 23 | { 24 | allow = 1500; 25 | fs = bs+hra+da+allow-pf; 26 | printsalary = (int)Math.round(fs); 27 | System.out.println(printsalary); 28 | } 29 | else 30 | { 31 | allow = 1300; 32 | fs = bs+hra+da+allow-pf; 33 | printsalary = (int)Math.round(fs); 34 | System.out.println(printsalary); 35 | } 36 | 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /Data Structures(Test-1)/DoesScontainsT.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public static boolean checkSequence(String a, String b) { 3 | if(a.length()ei) { 12 | 13 | return lsum==rsum; 14 | 15 | } 16 | if(arr[si] % 5==0) { 17 | return helper(arr, si+1, ei, lsum + arr[si], rsum); 18 | } 19 | else if(arr[si] %3==0) { 20 | return helper(arr, si+1, ei, lsum, rsum+arr[si]); 21 | } 22 | else { 23 | return helper(arr, si+1, ei, lsum+arr[si], rsum) || helper(arr, si+1, ei, lsum, rsum+arr[si]); 24 | } 25 | 26 | 27 | 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Dynamic Programming-1/ByteLandian.java: -------------------------------------------------------------------------------- 1 | package dp; 2 | import java.util.HashMap; 3 | 4 | public class ByteLandian { 5 | 6 | public static long bytelandian(long n, HashMap memo) { 7 | if(n<=1) 8 | { 9 | return n; 10 | } 11 | 12 | 13 | 14 | long ans2=Integer.MIN_VALUE; 15 | 16 | if(!memo.containsKey(n/2)) { 17 | ans2=bytelandian(n/2, memo); 18 | 19 | } 20 | else 21 | { 22 | ans2= memo.get(n/2); 23 | } 24 | 25 | long ans3=Integer.MIN_VALUE; 26 | 27 | if(!memo.containsKey(n/3)) { 28 | ans3=bytelandian(n/3, memo); 29 | 30 | } 31 | else 32 | { 33 | ans3= memo.get(n/3); 34 | } 35 | 36 | 37 | long ans4=Integer.MIN_VALUE; 38 | 39 | if(!memo.containsKey(n/4)) { 40 | ans4=bytelandian(n/4, memo); 41 | 42 | } 43 | else 44 | { 45 | ans4= memo.get(n/4); 46 | } 47 | 48 | long sum = ans2+ans3+ans4; 49 | long var = Math.max(n, sum); 50 | memo.put(n, var); 51 | 52 | return memo.get(n); 53 | } 54 | 55 | 56 | public static void main(String[] args) { 57 | HashMap map = new HashMap<>(); 58 | long ans = bytelandian(12,map); 59 | System.out.println(ans); 60 | 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /Dynamic Programming-1/LootHouses.java: -------------------------------------------------------------------------------- 1 | package dp; 2 | 3 | public class LootHouses { 4 | 5 | public static int maxMoneyLooted(int[] houses) { 6 | int n = houses.length; 7 | int dp[]=new int[n]; 8 | 9 | dp[0]=houses[0]; 10 | dp[1]=Math.max(houses[1], houses[0]); 11 | 12 | for(int i =2;imax) 28 | { 29 | max=dp[i][j]; 30 | } 31 | } 32 | } 33 | 34 | return max; 35 | } 36 | 37 | public static void main(String[] args) { 38 | 39 | String x = "GeeksForGeeks"; 40 | String y = "GeeksQuiz"; 41 | 42 | int ans = lcsub(x, y); 43 | System.out.println(ans); 44 | 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /Dynamic Programming-II/LongestPalindromicSubsequence.java: -------------------------------------------------------------------------------- 1 | package dp_av; 2 | 3 | public class LongestPalindromicSubsequence { 4 | 5 | public static int longestpalindromess(String a) 6 | { 7 | int n = a.length(); 8 | String b=""; 9 | for(int i =n-1;i>=0;i--) 10 | { 11 | b+=a.charAt(i); 12 | } 13 | 14 | int m = b.length(); 15 | 16 | int dp[][]=new int[n+1][m+1]; 17 | 18 | for(int i =1;i=j) 23 | { 24 | return 0; 25 | } 26 | 27 | if(dp[i][j]!=-1) 28 | { 29 | return dp[i][j]; 30 | } 31 | int ans = Integer.MAX_VALUE; 32 | for(int k = i;k<=j-1;k++) 33 | { 34 | int tempans = solve(arr,i,k,dp) + solve(arr,k+1,j,dp) + (arr[i-1] * arr[k] * arr[j]); 35 | if(tempans=j) 15 | { 16 | return 0; 17 | } 18 | int ans = Integer.MAX_VALUE; 19 | for(int k = i;k<=j-1;k++) 20 | { 21 | int tempans = solve(arr,i,k) + solve(arr,k+1,j) + (arr[i-1] * arr[k] * arr[j]); 22 | if(tempans=m || j>=n) 21 | { 22 | return Integer.MAX_VALUE; 23 | } 24 | 25 | int down = minimum(input,i+1,j); 26 | int left = minimum(input,i,j+1); 27 | int diag = minimum(input,i+1,j+1); 28 | 29 | int mincost = input[i][j]+Math.min(down, Math.min(left, diag)); 30 | 31 | return mincost; 32 | } 33 | 34 | public static void main(String[] args) { 35 | int arr[][] = {{5,7,2,4},{1,8,1,3},{6,2,9,5},{1,6,2,8}}; 36 | System.out.println(minCostPath(arr)); 37 | 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /Dynamic Programming-II/MinCostPath_Iterative.java: -------------------------------------------------------------------------------- 1 | package dp; 2 | 3 | public class MinCostPath_Iterative { 4 | 5 | public static int minCostPath(int[][] input) { 6 | 7 | int m= input.length; 8 | int n = input[0].length; 9 | 10 | int dp[][]=new int[m+1][n+1]; 11 | 12 | for(int i =0;i=0;i--) 21 | { 22 | for(int j =n-1;j>=0;j--) 23 | { 24 | if(i==m-1 && j==n-1) 25 | { 26 | dp[i][j]=input[i][j]; 27 | continue; 28 | } 29 | int ans1=dp[i+1][j]; 30 | int ans2=dp[i][j+1]; 31 | int ans3=dp[i+1][j+1]; 32 | 33 | dp[i][j]=input[i][j]+ Math.min(ans1, Math.min(ans2, ans3)); 34 | } 35 | } 36 | 37 | return dp[0][0]; 38 | 39 | } 40 | 41 | public static void main(String[] args) { 42 | int arr[][] = {{5,7,2,4},{1,8,1,3},{6,2,9,5},{1,6,2,8}}; 43 | System.out.println(minCostPath(arr)); 44 | 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /Dynamic Programming-II/MinCostPath_Memoization.java: -------------------------------------------------------------------------------- 1 | package dp; 2 | 3 | public class MinCostPath_Memoization { 4 | 5 | public static int minCostPath(int[][] input) { 6 | 7 | int dp[][] = new int[input.length+1][input[0].length+1]; 8 | for(int i =0;i=m || j>=n) 30 | { 31 | return Integer.MAX_VALUE; 32 | } 33 | 34 | int ans1,ans2,ans3; 35 | if(dp[i+1][j]==Integer.MIN_VALUE) 36 | { 37 | ans1=minimum(input,i+1,j,dp); 38 | dp[i+1][j]=ans1; 39 | } 40 | else 41 | { 42 | ans1=dp[i+1][j]; 43 | } 44 | 45 | if(dp[i][j+1]==Integer.MIN_VALUE) 46 | { 47 | ans2=minimum(input,i,j+1,dp); 48 | dp[i][j+1]=ans2; 49 | } 50 | else 51 | { 52 | ans2=dp[i][j+1]; 53 | } 54 | if(dp[i+1][j+1]==Integer.MIN_VALUE) 55 | { 56 | ans3=minimum(input,i+1,j+1,dp); 57 | dp[i+1][j+1]=ans3; 58 | } 59 | else 60 | { 61 | ans3=dp[i+1][j+1]; 62 | } 63 | 64 | int ans = input[i][j]+ Math.min(ans1, Math.min(ans2, ans3)); 65 | return ans; 66 | } 67 | 68 | public static void main(String[] args) { 69 | int arr[][] = {{5,7,2,4},{1,8,1,3},{6,2,9,5},{1,6,2,8}}; 70 | System.out.println(minCostPath(arr)); 71 | } 72 | 73 | } 74 | -------------------------------------------------------------------------------- /Dynamic Programming-II/MinNoOfInsertionToMakeAPalindrome.java: -------------------------------------------------------------------------------- 1 | package dp_av; 2 | 3 | public class MinNoOfInsertionToMakePalindrome { 4 | 5 | public static int ninsertion(String x) 6 | { 7 | int n = x.length(); 8 | String y = ""; 9 | for(int i =n-1;i>=0;i--) 10 | { 11 | y+=x.charAt(i); 12 | } 13 | 14 | int dp[][]=new int[n+1][n+1]; 15 | 16 | for(int i =1;i out = new ArrayList<>(); 8 | int range = 0; 9 | for(int i =0;i helper(int arr[],int range) 32 | { 33 | int n = arr.length; 34 | boolean dp[][]=new boolean[n+1][range+1]; 35 | 36 | for(int i =0;i val = new ArrayList<>(); 62 | for(int i =0;i<=range/2;i++) 63 | { 64 | if(dp[n][i]==true) 65 | { 66 | val.add(i); 67 | } 68 | } 69 | return val; 70 | } 71 | 72 | public static void main(String[] args) { 73 | int arr[]= {1,6,5,11}; 74 | System.out.println(mindiff(arr)); 75 | 76 | } 77 | 78 | } 79 | -------------------------------------------------------------------------------- /Dynamic Programming-II/PalindromePartitioning_Memoization.java: -------------------------------------------------------------------------------- 1 | package dp_av; 2 | 3 | public class PalindromePartitioning_Memoization { 4 | 5 | public static int partition(String x) 6 | { 7 | int dp[][]=new int[1001][1001]; 8 | for(int i =0;i=j) 21 | { 22 | return 0; 23 | } 24 | 25 | if(dp[i][j]!=-1) 26 | { 27 | return dp[i][j]; 28 | } 29 | int ans = Integer.MAX_VALUE; 30 | if(palindrome(x, i, j)==true) 31 | { 32 | return 0; 33 | } 34 | else 35 | { 36 | for(int k =i;k=j) 13 | { 14 | return 0; 15 | } 16 | int ans = Integer.MAX_VALUE; 17 | if(palindrome(x, i, j)==true) 18 | { 19 | return 0; 20 | } 21 | else 22 | { 23 | for(int k =i;k0 && j>0) 34 | { 35 | if(x.charAt(i-1)==y.charAt(j-1)) 36 | { 37 | ans+=x.charAt(i-1); 38 | i--; 39 | j--; 40 | } 41 | else 42 | { 43 | if(dp[i][j-1]>dp[i-1][j]) 44 | { 45 | j--; 46 | } 47 | else 48 | { 49 | i--; 50 | } 51 | } 52 | } 53 | 54 | String ansfinal=""; 55 | for(int k = ans.length()-1;k>=0;k--) 56 | { 57 | ansfinal+=ans.charAt(k); 58 | } 59 | 60 | return ansfinal; 61 | } 62 | 63 | public static void main(String[] args) { 64 | 65 | String x = "GeeksForGeeks"; 66 | String y = "GeeksQuiz"; 67 | 68 | String ans = printlcs(x,y); 69 | System.out.println(ans); 70 | } 71 | 72 | } 73 | -------------------------------------------------------------------------------- /Dynamic Programming-II/PrintShortestCommonSupersequence.java: -------------------------------------------------------------------------------- 1 | package dp_av; 2 | 3 | public class PrintShortestCommonSupersequence { 4 | 5 | public static String printss(String x,String y) 6 | { 7 | int n = x.length(); 8 | int m = y.length(); 9 | 10 | int dp[][]=new int[n+1][m+1]; 11 | String ans=""; 12 | 13 | for(int i =1;i0 && j > 0) 33 | { 34 | if(x.charAt(i-1)==y.charAt(j-1)) 35 | { 36 | ans+=x.charAt(i-1); 37 | i--; 38 | j--; 39 | } 40 | else 41 | { 42 | if(dp[i-1][j]>dp[i][j-1]) 43 | { 44 | ans+=x.charAt(i-1); 45 | i--; 46 | } 47 | else 48 | { 49 | ans+=y.charAt(j-1); 50 | j--; 51 | } 52 | } 53 | } 54 | 55 | while(i>0) 56 | { 57 | ans+=x.charAt(i-1); 58 | i--; 59 | } 60 | while(j>0) 61 | { 62 | ans+=y.charAt(j-1); 63 | j--; 64 | } 65 | 66 | String ansfinal=""; 67 | 68 | for(int l = ans.length()-1;l>=0;l--) 69 | { 70 | ansfinal+=ans.charAt(l); 71 | } 72 | 73 | return ansfinal; 74 | } 75 | 76 | public static void main(String[] args) { 77 | String x = "AGGTAB"; 78 | String y = "GXTXAYB"; 79 | 80 | String ans = printss(x,y); 81 | System.out.println(ans); 82 | 83 | } 84 | 85 | } 86 | -------------------------------------------------------------------------------- /Dynamic Programming-II/RodCutting.java: -------------------------------------------------------------------------------- 1 | package dp_av; 2 | 3 | public class RodCutting { 4 | 5 | public static int crod(int price[],int size) 6 | { 7 | int n = price.length; 8 | int length[]=new int[n]; 9 | for(int i=0;i pending = new LinkedList<>(); 11 | boolean visited[] = new boolean[adjmatrix.length]; 12 | visited[0]=true; 13 | pending.add(0); 14 | 15 | while(!pending.isEmpty()) 16 | { 17 | int currentVertex = pending.poll(); 18 | System.out.print(currentVertex+" "); 19 | 20 | for(int i = 0;i pending = new LinkedList<>(); 12 | boolean visited[] = new boolean[adjmatrix.length]; 13 | 14 | pending.add(0); 15 | visited[0]=true; 16 | 17 | int n = adjmatrix.length; 18 | if(v1>=n || v2>=n) 19 | { 20 | return false; 21 | } 22 | 23 | while(!pending.isEmpty()) 24 | { 25 | if(visited[v1]==true && visited[v2]==true) 26 | { 27 | return true; 28 | } 29 | 30 | int currentVertex = pending.poll(); 31 | for(int i =0;i adj[]; 7 | 8 | BFSOfAGraph(int v) 9 | { 10 | V=v; 11 | adj = new LinkedList[v]; 12 | for(int i =0;i queue = new LinkedList<>(); 28 | 29 | visited[s]=true; 30 | queue.add(s); 31 | 32 | while(!queue.isEmpty()) 33 | { 34 | s = queue.poll(); 35 | System.out.print(s+" "); 36 | 37 | Iterator i = adj[s].listIterator(); 38 | 39 | while(i.hasNext()) 40 | { 41 | int n = i.next(); 42 | 43 | if(!visited[n]) 44 | { 45 | visited[n]=true; 46 | queue.add(n); 47 | } 48 | } 49 | } 50 | } 51 | 52 | public static void main(String[] args) { 53 | BFSOfAGraph g= new BFSOfAGraph(4); 54 | 55 | g.addEdge(0, 1); 56 | g.addEdge(0, 2); 57 | g.addEdge(1, 2); 58 | g.addEdge(2, 0); 59 | g.addEdge(2, 3); 60 | g.addEdge(3, 3); 61 | 62 | 63 | System.out.println("BFS OF 2"); 64 | 65 | g.BFS(2); 66 | 67 | } 68 | 69 | } 70 | -------------------------------------------------------------------------------- /Graphs/DFSOfAGraph.java: -------------------------------------------------------------------------------- 1 | package Graphs; 2 | import java.util.*; 3 | public class DFSOfAGraph { 4 | 5 | private int V; 6 | private LinkedList adj[]; 7 | 8 | DFSOfAGraph(int v) 9 | { 10 | this.V=v; 11 | adj = new LinkedList[v]; 12 | 13 | for(int i =0;i i = adj[v].listIterator(); 30 | while(i.hasNext()) 31 | { 32 | int n = i.next(); 33 | if(!visited[n]) 34 | { 35 | DFSUtil(n, visited); 36 | } 37 | } 38 | } 39 | 40 | void DFS(int v) 41 | { 42 | boolean visited[] = new boolean[V]; 43 | DFSUtil(v, visited); 44 | } 45 | 46 | public static void main(String[] args) { 47 | DFSOfAGraph g = new DFSOfAGraph(5); 48 | 49 | g.addEdge(0, 1); 50 | g.addEdge(0, 2); 51 | g.addEdge(1, 2); 52 | g.addEdge(2, 0); 53 | g.addEdge(2, 3); 54 | g.addEdge(3, 3); 55 | g.addEdge(1, 4); 56 | 57 | g.DFS(2); 58 | 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /HashMaps/ExtractUniqueCharacters.java: -------------------------------------------------------------------------------- 1 | import java.util.HashMap; 2 | 3 | public class Solution { 4 | 5 | public static String uniqueChar(String str){ 6 | HashMap map = new HashMap<>(); 7 | String ans=""; 8 | for(int i =0;i longestConsecutiveIncreasingSequence(int[] arr) { 7 | HashMap map = new HashMap<>(); 8 | for(int i =0;ioverallcount) 28 | { 29 | overallcount=currentcount; 30 | maxStart = arr[i]; 31 | } 32 | } 33 | } 34 | ArrayList ans = new ArrayList<>(); 35 | ans.add(maxStart); 36 | ans.add(maxStart+overallcount-1); 37 | 38 | return ans; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /HashMaps/MaximumFrequencyNumber.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class Solution { 3 | 4 | public static int maxFrequencyNumber(int[] arr){ 5 | HashMap map = new HashMap<>(); 6 | 7 | for(int i =0;i { 4 | 5 | K key; 6 | V value; 7 | MapNode next; 8 | 9 | public MapNode(K key, V value) 10 | { 11 | this.key=key; 12 | this.value=value; 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /HashMaps/PairSumZero.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Solution { 4 | public static int PairSum(int[] input, int size) { 5 | HashMap map = new HashMap<>(); 6 | 7 | for(int i =0;i map = new HashMap<>(); 6 | int count = 0; 7 | if(k!=0) 8 | { 9 | for(int i =0;i0){ 56 | count+=(n*(n-1)/2); 57 | map.put(arr[i],0); 58 | } 59 | } 60 | 61 | 62 | 63 | } 64 | 65 | return count; 66 | } 67 | 68 | } 69 | -------------------------------------------------------------------------------- /HashMaps/PrintIntersection.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class Solution { 3 | 4 | public static void intersection(int[] arr1, int[] arr2) { 5 | Arrays.sort(arr1); 6 | Arrays.sort(arr2); 7 | 8 | HashMap map = new HashMap<>(); 9 | for(int i =0;i0) 31 | { 32 | System.out.print(arr2[j]+" "); 33 | value--; 34 | map.put(arr2[j],value); 35 | } 36 | 37 | } 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Linked List -1/AppendLastNtoFirst.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | 3 | public static LinkedListNode appendLastNToFirst(LinkedListNode head, int n) { 4 | int totalelements = 0; 5 | LinkedListNode count =head; 6 | if(head==null) 7 | { 8 | return null; 9 | } 10 | if(n==0) 11 | { 12 | return count; 13 | } 14 | while(count!=null) 15 | { 16 | totalelements++; 17 | count=count.next; 18 | } 19 | int sub = totalelements-n; //5-3=2 20 | LinkedListNode nh = head; 21 | int c=0; 22 | while(c newhead = nh; 28 | 29 | LinkedListNode prev = newhead; 30 | while(prev.next!=null) 31 | { 32 | prev=prev.next; 33 | } 34 | prev.next = head; 35 | 36 | LinkedListNode br = head; 37 | int co = 0; 38 | while(co deleteNode(LinkedListNode head, int pos) { 4 | LinkedListNode prev = head; 5 | if(pos==0) 6 | { 7 | LinkedListNode cont = head.next; 8 | return cont; 9 | } 10 | else 11 | { 12 | int count = 0; 13 | while(count removeDuplicates(LinkedListNode head) { 4 | LinkedListNode temp = head; 5 | if(temp==null) 6 | { 7 | return null; 8 | } 9 | else if(temp.next==null) 10 | { 11 | return head; 12 | } 13 | while(temp!=null && temp.next!=null) 14 | { 15 | if((temp.data).equals(temp.next.data)) 16 | { 17 | temp.next=temp.next.next; 18 | } 19 | else 20 | { 21 | temp=temp.next; 22 | } 23 | 24 | } 25 | 26 | return head; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /Linked List -1/FindANode.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | 3 | public static int findNode(LinkedListNode head, int n) { 4 | LinkedListNode pos = head; 5 | int count = 0; 6 | if(head==null) 7 | { 8 | return -1; 9 | } 10 | else if(pos.data==n) 11 | { 12 | return 0; 13 | } 14 | 15 | LinkedListNode tail = head; 16 | 17 | 18 | while(tail!=null) 19 | { 20 | if(tail.data==n) 21 | { 22 | return count; 23 | } 24 | else 25 | { 26 | count++; 27 | tail = tail.next; 28 | } 29 | 30 | 31 | } 32 | 33 | return -1; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Linked List -1/InsertNodeinLL.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | 3 | public static LinkedListNode insert(LinkedListNode head, int pos, int data){ 4 | LinkedListNode target = new LinkedListNode<>(data); 5 | if(pos==0) 6 | { 7 | target.next = head; 8 | return target; 9 | } 10 | else 11 | { 12 | int count = 0; 13 | LinkedListNode prev = head; 14 | while(count head){ 4 | LinkedListNode temp = head; 5 | int count =0; 6 | while(temp!=null) 7 | { 8 | count++; 9 | temp=temp.next; 10 | } 11 | return count; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Linked List -1/PrintReverse.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | 3 | public static void printReverse(LinkedListNode root) { 4 | LinkedListNode temp = root; 5 | if(root==null) 6 | { 7 | return; 8 | } 9 | printReverse(root.next); 10 | System.out.print(root.data+" "); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /Linked List -1/PrintithNode.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | 3 | public static void printIthNode(LinkedListNode head, int i){ 4 | LinkedListNode temp = head; 5 | int count = 0; 6 | if(temp==null) 7 | { 8 | return; 9 | } 10 | while(temp!=null) 11 | { 12 | if(count==i) 13 | { 14 | System.out.println(temp.data); 15 | } 16 | count++; 17 | temp=temp.next; 18 | } 19 | 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Linked List -2/BubbleSortinLL.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | 3 | public static int length(LinkedListNode head) 4 | { 5 | LinkedListNode temp = head; 6 | int count = 0; 7 | while(temp!=null) 8 | { 9 | count++; 10 | temp=temp.next; 11 | } 12 | return count; 13 | } 14 | 15 | public static LinkedListNode bubbleSort(LinkedListNode head) { 16 | 17 | LinkedListNode OG=head; 18 | int n = length(head); 19 | for(int i =0;i prev=null,curr=OG; 22 | for(int j =0;j curr2=curr.next; 34 | LinkedListNode temp = curr2.next; 35 | 36 | curr2.next=curr; 37 | curr.next=temp; 38 | prev=curr2; 39 | OG=curr2; 40 | } 41 | else 42 | { 43 | LinkedListNode curr2= curr.next; 44 | LinkedListNode temp= curr2.next; 45 | prev.next=curr2; 46 | curr2.next=curr; 47 | curr.next=temp; 48 | prev= curr2; 49 | 50 | } 51 | } 52 | } 53 | } 54 | return OG; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /Linked List -2/DeleteEveryNnodes.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | 3 | public static LinkedListNode skipMdeleteN(LinkedListNode head, int M, int N) { 4 | if(head==null) 5 | { 6 | return null; 7 | 8 | } 9 | if(M==0) 10 | { 11 | return null; 12 | } 13 | if(N==0) 14 | { 15 | return head; 16 | } 17 | 18 | LinkedListNode mhead=head; 19 | LinkedListNode t1=head,t2=head; 20 | 21 | while(head!=null) 22 | { 23 | t1=head; 24 | for(int i =1;i deleteNodeRec(LinkedListNode head, int pos) { 5 | 6 | if(head==null) 7 | { 8 | return head; 9 | } 10 | 11 | if(pos==0) 12 | { 13 | return head.next; 14 | } 15 | else 16 | { 17 | head.next = deleteNodeRec(head.next,pos-1); 18 | 19 | return head; 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Linked List -2/EvenAfterOddLL.java: -------------------------------------------------------------------------------- 1 | 2 | public class Solution { 3 | 4 | public static LinkedListNode evenAfterOdd(LinkedListNode head) { 5 | LinkedListNode oddH=null,oddT=null,evenH=null,evenT=null; 6 | LinkedListNode temp = head; 7 | while(temp!=null) 8 | { 9 | if(temp.data%2==0) 10 | { 11 | if(evenH==null) 12 | { 13 | evenH=temp; 14 | evenT=temp; 15 | } 16 | else 17 | { 18 | evenT.next=temp; 19 | evenT=temp; 20 | } 21 | } 22 | else 23 | { 24 | if(oddH==null) 25 | { 26 | oddH=temp; 27 | oddT=temp; 28 | } 29 | else 30 | { 31 | oddT.next=temp; 32 | oddT=temp; 33 | } 34 | } 35 | temp=temp.next; 36 | } 37 | if(oddH==null) 38 | { 39 | return evenH; 40 | } 41 | if(evenH==null) 42 | { 43 | return oddH; 44 | } 45 | oddT.next=evenH; 46 | evenT.next=null; 47 | 48 | return oddH; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /Linked List -2/FindANodeinLL.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | 3 | 4 | public static int findNodeRec(LinkedListNode head, int n) { 5 | if(head==null) 6 | { 7 | return -1; 8 | } 9 | else if(head!=null && head.data==n) 10 | { 11 | return 0; 12 | } 13 | else if(head.next==null&& head.data!=n) 14 | { 15 | return -1; 16 | } 17 | int ans = findNodeRec(head.next,n); 18 | if(ans==-1) 19 | { 20 | return -1; 21 | } 22 | return ans+1; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Linked List -2/MergeTwoSortedLL.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | 3 | public static LinkedListNode mergeTwoSortedLinkedLists(LinkedListNode head1, LinkedListNode head2) { 4 | if(head1==null) 5 | { 6 | return head2; 7 | } 8 | if(head2==null) 9 | { 10 | return head1; 11 | } 12 | LinkedListNode t1=head1,t2=head2; 13 | LinkedListNode head=null,tail=null; 14 | 15 | if(t1.data<=t2.data) 16 | { 17 | head=t1; 18 | tail=t1; 19 | t1=t1.next; 20 | } 21 | else 22 | { 23 | head=t2; 24 | tail=t2; 25 | t2=t2.next; 26 | } 27 | while(t1!=null && t2!=null) 28 | { 29 | if(t1.data<=t2.data) 30 | { 31 | tail.next=t1; 32 | tail = t1; 33 | t1=t1.next; 34 | } 35 | else 36 | { 37 | tail.next=t2; 38 | tail=t2; 39 | t2=t2.next; 40 | } 41 | } 42 | if(t1!=null) 43 | { 44 | tail.next=t1; 45 | } 46 | if(t2!=null) 47 | { 48 | tail.next=t2; 49 | } 50 | return head; 51 | } 52 | 53 | 54 | } 55 | -------------------------------------------------------------------------------- /Linked List -2/MiddleNodeinLL.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | 3 | public static LinkedListNode midPoint(LinkedListNode head) { 4 | LinkedListNode slow = head; 5 | LinkedListNode fast = head; 6 | if(head==null) 7 | { 8 | return null; 9 | } 10 | 11 | while(fast.next!=null && fast.next.next!=null) 12 | { 13 | slow=slow.next; 14 | fast=fast.next.next; 15 | } 16 | return slow; 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /Linked List -2/ReverseLL.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | 3 | public static LinkedListNode reverseLinkedListRec(LinkedListNode head) { 4 | if(head==null || head.next == null) 5 | { 6 | return head; 7 | } 8 | 9 | LinkedListNode shead = reverseLinkedListRec(head.next); 10 | LinkedListNode tail = shead; 11 | while(tail.next!=null) 12 | { 13 | tail=tail.next; 14 | } 15 | tail.next=head; 16 | head.next=null; 17 | 18 | return shead; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /Linked List -2/SwapTwoNodesofLL.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | 3 | public static LinkedListNode swapNodes(LinkedListNode head, int i, int j) { 4 | 5 | if(i==j) 6 | { 7 | return head; 8 | } 9 | if(i>j) 10 | { 11 | int t = i; 12 | i=j; 13 | j=t; 14 | } 15 | 16 | 17 | LinkedListNode p1=null,p2=null,n1=null,n2=null; 18 | LinkedListNode temp = head; 19 | LinkedListNode tail = null; 20 | int count = 0; 21 | while(temp!=null) 22 | { 23 | if(count==i) 24 | { 25 | 26 | p1= tail; 27 | n1=temp; 28 | } 29 | else if(count==j) 30 | { 31 | p2=tail; 32 | n2=temp; 33 | } 34 | tail=temp; 35 | temp=temp.next; 36 | count++; 37 | 38 | } 39 | if(p1==null) 40 | { 41 | head=n2; 42 | } 43 | else 44 | { 45 | p1.next=n2; 46 | p2.next=n1; 47 | 48 | } 49 | 50 | LinkedListNode nextelem = n2.next; 51 | n2.next=n1.next; 52 | n1.next=nextelem; 53 | 54 | 55 | 56 | return head; 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /OOPS-1/ComplexNumberProblem.java: -------------------------------------------------------------------------------- 1 | public class ComplexNumbers { 2 | // Complete this class 3 | public int real; 4 | public int imaginary; 5 | 6 | public ComplexNumbers(int real,int imaginary) 7 | { 8 | this.real=real; 9 | this.imaginary = imaginary; 10 | } 11 | 12 | public void plus(ComplexNumbers c2) 13 | { 14 | this.real = c2.real + this.real; 15 | this.imaginary = c2.imaginary + this.imaginary; 16 | 17 | } 18 | 19 | public void multiply(ComplexNumbers c2) 20 | { int temp = this.real; 21 | this.real = (this.real*c2.real) - (this.imaginary*c2.imaginary); 22 | this.imaginary = (this.imaginary*c2.real) +(temp*c2.imaginary); 23 | } 24 | 25 | public void print() 26 | { 27 | System.out.print(real+" "+"+"+" "+"i"+imaginary); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /Operators & For Loops/AllPrimeNumbers.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Solution { 3 | 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int n; 8 | Scanner sc = new Scanner(System.in); 9 | n = sc.nextInt(); 10 | 11 | for(int i =2;i<=n;i++) 12 | { 13 | if(i!=2 && i%2==0) 14 | { 15 | continue; 16 | } 17 | else if(i!=3 && i%3==0) 18 | { 19 | continue; 20 | } 21 | else if(i!=5 && i%5==0) 22 | { 23 | continue; 24 | } 25 | else if(i!=7 && i%7==0) 26 | { 27 | continue; 28 | } 29 | else { 30 | System.out.println(i); 31 | } 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Operators & For Loops/BinaryToDecimal.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Main { 3 | 4 | public static void main(String[] args) { 5 | // TODO Auto-generated method stub 6 | int n; 7 | Scanner sc = new Scanner(System.in); 8 | n = sc.nextInt(); 9 | 10 | int sum = 0; 11 | int pv = 1; 12 | 13 | while(n>0) 14 | { 15 | int a = n%10; 16 | 17 | sum = sum + pv*a; 18 | n=n/10; 19 | pv=pv*2; 20 | } 21 | System.out.print(sum); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /Operators & For Loops/CheckNumbersSequence.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | Scanner scan=new Scanner(System.in); 8 | int n=scan.nextInt(); 9 | int pre=scan.nextInt(); 10 | int i=1; 11 | boolean isDec=true; 12 | int count=0; 13 | while(i<=n-1) { 14 | int curr=scan.nextInt(); 15 | if(pre==curr) { 16 | System.out.println(false); 17 | return ; 18 | } 19 | else if(pre0) { 9 | int a=n%2; 10 | res=res+a*(long)Math.pow(10,p++); 11 | n=n/2; 12 | } 13 | System.out.println(res); 14 | 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /Operators & For Loops/NthFibonacciNumber.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Solution { 3 | 4 | 5 | public static void main(String[] args) { 6 | int n; 7 | Scanner sc = new Scanner(System.in); 8 | n = sc.nextInt(); 9 | int a=0; 10 | int b= 1; 11 | 12 | 13 | for(int i = 1; i<=n;i++) 14 | { 15 | int c = a + b; 16 | a=b; 17 | b=c; 18 | } 19 | System.out.print(a); 20 | 21 | 22 | 23 | 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /Operators & For Loops/ReverseOfANumber.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Main { 3 | 4 | public static void main(String[] args) { 5 | // TODO Auto-generated method stub 6 | int n; 7 | Scanner sc= new Scanner(System.in); 8 | 9 | n=sc.nextInt(); 10 | int rev = 0; 11 | 12 | while(n>0) 13 | { 14 | int a = n%10; 15 | rev = rev*10+ a; 16 | n=n/10; 17 | } 18 | System.out.print(rev); 19 | 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /Operators & For Loops/SquareRootIntegral.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Main { 3 | 4 | public static void main(String[] args) { 5 | 6 | Scanner sc=new Scanner(System.in); 7 | int n =sc.nextInt(); 8 | 9 | int i=0; 10 | int sq = 0; 11 | while(i*i<=n) 12 | { 13 | sq = i; 14 | i+=1; 15 | } 16 | System.out.print(sq); 17 | 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /Operators & For Loops/SumOrProduct.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Main { 3 | 4 | public static void main(String[] args) { 5 | // Write your code here 6 | int n; 7 | Scanner sc = new Scanner(System.in); 8 | n = sc.nextInt(); 9 | 10 | int choice; 11 | choice = sc.nextInt(); 12 | 13 | if(choice==1) 14 | { 15 | int sum = 0; 16 | int i = 1; 17 | while(i<=n) 18 | { 19 | sum = sum+ i; 20 | i=i+1; 21 | } 22 | System.out.println(sum); 23 | } 24 | 25 | 26 | else if(choice == 2) 27 | { 28 | int product = 1; 29 | int i = 1; 30 | while(i<=n) 31 | { 32 | product = product * i; 33 | i+=1; 34 | } 35 | System.out.println(product); 36 | } 37 | else 38 | { 39 | System.out.println("-1"); 40 | } 41 | 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Operators & For Loops/TermsOfAP.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Main { 3 | 4 | public static void main(String[] args) { 5 | // Write your code here 6 | int n; 7 | Scanner sc = new Scanner(System.in); 8 | n=sc.nextInt(); 9 | 10 | int i = 1; 11 | while(i<=n) 12 | { 13 | int sum = 3*i + 2; 14 | i+=1; 15 | if(sum%4==0) 16 | { 17 | int skip = 1; 18 | n = n + skip; 19 | continue; 20 | } 21 | System.out.print(sum+" "); 22 | 23 | } 24 | 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Patterns-1/AlphaPattern.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Solution { 3 | 4 | public static void main(String[] args) { 5 | 6 | /* Your class should be named Solution. 7 | * Read input as specified in the question. 8 | * Print output as specified in the question. 9 | */ 10 | int n; 11 | Scanner sc = new Scanner(System.in); 12 | n = sc.nextInt(); 13 | 14 | int i=1; 15 | while(i<=n) 16 | { int p = i; 17 | int j = 1; 18 | while(j<=i) 19 | { 20 | char jthChar = (char)('A'+ p-1 ); 21 | System.out.print(jthChar); 22 | j=j+1; 23 | 24 | } 25 | p=p+1; 26 | System.out.println(); 27 | i=i+1; 28 | } 29 | 30 | 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /Patterns-1/CharacterPattern.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Solution { 3 | 4 | 5 | public static void main(String[] args) { 6 | 7 | /* Your class should be named Solution. 8 | * Read input as specified in the question. 9 | * Print output as specified in the question. 10 | */ 11 | int n; 12 | int i = 1; 13 | Scanner sc = new Scanner(System.in); 14 | n = sc.nextInt(); 15 | 16 | while(i<=n) 17 | { 18 | int j = 1; 19 | int p=i; 20 | while(j<=i) 21 | { 22 | char jthChar = (char)('A'+p-1); 23 | System.out.print(jthChar); 24 | j=j+1; 25 | p=p+1; 26 | } 27 | System.out.println(); 28 | i = i +1; 29 | } 30 | 31 | 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /Patterns-1/InterestingAlphabets.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Solution { 3 | public static void main(String[] args) { 4 | //Your code goes here 5 | int n; 6 | 7 | Scanner sc = new Scanner(System.in); 8 | n = sc.nextInt(); 9 | int i = 1; 10 | while(i<=n) 11 | { 12 | int j = 1; 13 | int p = i; 14 | while(j<=i) 15 | { 16 | char jthChar = (char)('A'+ n-p); 17 | System.out.print(jthChar); 18 | j=j+1; 19 | p=p-1; 20 | } 21 | 22 | System.out.println(); 23 | i=i+1; 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Patterns-1/ReverseNumber.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Solution { 3 | 4 | 5 | public static void main(String[] args) { 6 | 7 | /* Your class should be named Solution. 8 | * Read input as specified in the question. 9 | * Print output as specified in the question. 10 | */ 11 | int n; 12 | int i=1; 13 | Scanner sc = new Scanner(System.in); 14 | n=sc.nextInt(); 15 | 16 | while(i<=n) 17 | { 18 | int j =1; 19 | int p=i; 20 | while(j<=i) 21 | { 22 | System.out.print(p); 23 | j=j+1; 24 | p=p-1; 25 | } 26 | 27 | System.out.println(); 28 | i=i+1; 29 | } 30 | 31 | 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /Patterns-1/TriangleNumber.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Solution { 3 | 4 | 5 | public static void main(String[] args) { 6 | 7 | /* Your class should be named Solution. 8 | * Read input as specified in the question. 9 | * Print output as specified in the question. 10 | */ 11 | int n; 12 | int i=1; 13 | Scanner sc= new Scanner(System.in); 14 | n = sc.nextInt(); 15 | while(i<=n) 16 | { 17 | int j=1; 18 | while(j<=i) 19 | { 20 | System.out.print(i); 21 | j=j+1; 22 | } 23 | System.out.println(); 24 | i=i+1; 25 | } 26 | 27 | 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /Patterns-1/TriangularStar.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Solution { 3 | 4 | 5 | public static void main(String[] args) { 6 | 7 | /* Your class should be named Solution. 8 | * Read input as specified in the question. 9 | * Print output as specified in the question. 10 | */ 11 | int n; 12 | int i =1; 13 | Scanner sc = new Scanner(System.in); 14 | n=sc.nextInt(); 15 | 16 | while(i<=n) 17 | { 18 | int j =1; 19 | while(j<=i) 20 | { 21 | System.out.print('*'); 22 | j=j+1; 23 | } 24 | System.out.println(); 25 | i=i+1; 26 | } 27 | 28 | 29 | 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /Patterns-2/DiamondOfStars.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Solution { 3 | public static void main(String args[]) 4 | { 5 | Scanner sc = new Scanner(System.in); 6 | { 7 | int n = sc.nextInt(); 8 | int firsthalf = (n+1)/2; 9 | int secondhalf= firsthalf-1; 10 | 11 | //first half 12 | 13 | int currRow=1; 14 | while(currRow<=firsthalf) 15 | { 16 | int spaces =1; 17 | while(spaces<=(firsthalf-currRow)) 18 | { 19 | System.out.print(" "); 20 | spaces = spaces + 1; 21 | } 22 | int currCol = 1; 23 | while(currCol<=(2*currRow)-1) 24 | { 25 | System.out.print('*'); 26 | currCol = currCol + 1; 27 | } 28 | System.out.println(); 29 | currRow = currRow + 1; 30 | } 31 | //second half 32 | 33 | currRow = secondhalf; 34 | while(currRow>=1) 35 | { 36 | int spaces = 1; 37 | while(spaces<=(secondhalf-currRow + 1)) 38 | { 39 | System.out.print(' '); 40 | spaces = spaces + 1; 41 | 42 | } 43 | int currCol = 1; 44 | while(currCol<=(2*currRow)-1) 45 | { 46 | System.out.print('*'); 47 | currCol = currCol + 1; 48 | } 49 | System.out.println(); 50 | currRow = currRow - 1; 51 | } 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /Patterns-2/HalfDiamond.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Solution { 3 | 4 | public static void main(String[] args) { 5 | // Write your code here 6 | Scanner scan=new Scanner(System.in); 7 | int n=scan.nextInt(); 8 | 9 | for(int i=1;i<=n+1;i++){ 10 | 11 | 12 | for(int j=1;j<=i;j++){ 13 | 14 | if(j==1){ 15 | System.out.print("*"); 16 | } 17 | else{ 18 | System.out.print(j-1); 19 | } 20 | } 21 | 22 | for(int j= i-1;j>=1;j--){ 23 | 24 | if(j==1){ 25 | System.out.print("*"); 26 | } 27 | else{ 28 | System.out.print(j-1); 29 | } 30 | 31 | } 32 | 33 | System.out.println(); 34 | 35 | 36 | } 37 | 38 | if(n==0){ 39 | System.out.println("*"); 40 | return ; 41 | } 42 | 43 | 44 | for(int i=1;i<=n;i++){ 45 | 46 | 47 | for(int j=1;j<= (n-i+1 ) ;j++ ){ 48 | 49 | if(j==1){ 50 | System.out.print("*"); 51 | } 52 | else{ 53 | System.out.print(j-1); 54 | } 55 | 56 | } 57 | for(int j=n-i;j>=1;j--){ 58 | if(j==1){ 59 | System.out.print("*"); 60 | }else{ 61 | System.out.print(j-1); 62 | } 63 | } 64 | System.out.println(); 65 | 66 | 67 | } 68 | 69 | 70 | 71 | 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /Patterns-2/InvertedNumber.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Solution { 3 | 4 | 5 | public static void main(String[] args) { 6 | 7 | /* Your class should be named Solution. 8 | * Read input as specified in the question. 9 | * Print output as specified in the question. 10 | */ 11 | int n; 12 | Scanner sc = new Scanner(System.in); 13 | n = sc.nextInt(); 14 | int p=n; 15 | int i =1; 16 | while(i<=n) 17 | { 18 | int j = 1; 19 | while(j<=n-i+1) 20 | { 21 | 22 | System.out.print(p); 23 | j=j+1; 24 | 25 | } 26 | p = p-1; 27 | System.out.println(); 28 | i=i+1; 29 | } 30 | 31 | 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /Patterns-2/MirrorImageNumber.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Solution { 3 | 4 | 5 | public static void main(String[] args) { 6 | 7 | /* Your class should be named Solution. 8 | * Read input as specified in the question. 9 | * Print output as specified in the question. 10 | */ 11 | int n; 12 | Scanner sc = new Scanner(System.in); 13 | n = sc.nextInt(); 14 | 15 | int i = 1; 16 | while(i<=n) 17 | { 18 | int spaces = 1; 19 | while(spaces<=n-i) 20 | { 21 | System.out.print(' '); 22 | spaces = spaces + 1; 23 | } 24 | int num = 1; 25 | while(num<= i) 26 | { 27 | System.out.print(num); 28 | num = num + 1; 29 | } 30 | System.out.println(); 31 | i=i+1; 32 | } 33 | 34 | 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /Patterns-2/OddSquare.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Main { 3 | 4 | public static void main(String[] args) { 5 | // TODO Auto-generated method stub 6 | int n; 7 | Scanner sc = new Scanner(System.in); 8 | n = sc.nextInt(); 9 | int i = 1; 10 | while(i<=n) 11 | { 12 | int j = 1; 13 | int p = 2*i - 1; 14 | while(j<=n) 15 | { 16 | System.out.print(p); 17 | p=p+2; 18 | j=j+1; 19 | 20 | if(p==(2*n+1)) 21 | { 22 | p=1; 23 | } 24 | } 25 | System.out.println(); 26 | i=i+1; 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Patterns-2/Parallelogram.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Main { 3 | 4 | public static void main(String[] args) { 5 | // TODO Auto-generated method stub 6 | int n; 7 | Scanner sc = new Scanner(System.in); 8 | n = sc.nextInt(); 9 | 10 | int i =1; 11 | 12 | while(i<=n) 13 | { 14 | int spaces = 1; 15 | while(spaces<=i-1) 16 | { 17 | System.out.print(' '); 18 | spaces = spaces+1; 19 | } 20 | 21 | 22 | int stars = 1; 23 | while(stars<=n) 24 | { 25 | System.out.print('*'); 26 | stars = stars + 1; 27 | } 28 | System.out.println(); 29 | i = i +1; 30 | 31 | } 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /Patterns-2/StarPattern.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Solution { 3 | 4 | 5 | public static void main(String[] args) { 6 | 7 | /* Your class should be named Solution. 8 | * Read input as specified in the question. 9 | * Print output as specified in the question. 10 | */ 11 | int n; 12 | int i = 1; 13 | Scanner sc= new Scanner(System.in); 14 | n = sc.nextInt(); 15 | 16 | while(i<=n) 17 | { 18 | int spaces = 1; 19 | while(spaces<=n-i) 20 | { 21 | System.out.print(' '); 22 | spaces = spaces + 1; 23 | } 24 | int numb = 1; 25 | while(numb<=i) 26 | { 27 | System.out.print('*'); 28 | numb = numb + 1; 29 | } 30 | int dec = i - 1; 31 | while(dec>=1) 32 | { 33 | System.out.print('*'); 34 | dec = dec - 1; 35 | } 36 | System.out.println(); 37 | i = i +1; 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Patterns-2/SumPattern.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Main { 3 | 4 | public static void main(String[] args) { 5 | // Write your code here 6 | Scanner scan=new Scanner(System.in); 7 | int n=scan.nextInt(); 8 | 9 | for(int i=1;i<=n;i++){ 10 | int sum=0; 11 | for(int j=1;j<=i;j++){ 12 | sum+=j; 13 | System.out.print(j); 14 | if(j!=i){ 15 | System.out.print("+"); 16 | } 17 | } 18 | System.out.println("="+sum); 19 | 20 | 21 | } 22 | 23 | 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Patterns-2/TriangleOfNumbers.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Solution { 4 | 5 | public static void main(String[] args) { 6 | /* Your class should be named Solution. 7 | * Read input as specified in the question. 8 | * Print output as specified in the question. 9 | */ 10 | int n; 11 | int i = 1; 12 | 13 | Scanner sc = new Scanner(System.in); 14 | n = sc.nextInt(); 15 | 16 | while(i<=n) 17 | { 18 | int spaces = 1; 19 | int p =i; 20 | while(spaces<=n-i) 21 | { 22 | System.out.print(' '); 23 | spaces = spaces + 1; 24 | 25 | } 26 | int numb = 1; 27 | 28 | while(numb<=i) 29 | { 30 | System.out.print(p); 31 | numb = numb + 1; 32 | p=p+1; 33 | } 34 | int dec = p -2; 35 | while(dec>=i) 36 | { 37 | System.out.print(dec); 38 | dec= dec - 1; 39 | } 40 | System.out.println(); 41 | i = i +1; 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Priority Queue-I/Minimum Priority Queue/Element.java: -------------------------------------------------------------------------------- 1 | package priorityqueue; 2 | 3 | public class Element { 4 | 5 | T value; 6 | int priority; 7 | 8 | public Element(T value, int priority) 9 | { 10 | this.value=value; 11 | this.priority=priority; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Priority Queue-II/CheckMaxHeap.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | 3 | public static boolean checkMaxHeap(int arr[]) { 4 | int n = arr.length; 5 | boolean ans = true; 6 | for(int i =0;iarr[i+1]) 9 | { 10 | continue; 11 | } 12 | else 13 | { 14 | return false; 15 | } 16 | } 17 | return ans; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Priority Queue-II/KLargestElement.java: -------------------------------------------------------------------------------- 1 | package priorityqueue; 2 | 3 | import java.util.*; 4 | 5 | public class InbuiltPQ { 6 | public static ArrayList kLargest(int input[], int k) { 7 | ArrayList ans = new ArrayList<>(); 8 | PriorityQueue pq = new PriorityQueue<>(); 9 | for(int i = 0;i ans = kLargest(arr, 4); 35 | System.out.println(ans); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /Priority Queue-II/KSmallestElements.java: -------------------------------------------------------------------------------- 1 | package priorityqueue; 2 | 3 | import java.util.*; 4 | 5 | public class InbuiltPQ { 6 | public static ArrayList kSmallest(int n, int[] input, int k) { 7 | ArrayList ans = new ArrayList<>(); 8 | PriorityQueue pq = new PriorityQueue<>(Collections.reverseOrder()); 9 | for(int i =0;iinput[i]) 18 | { 19 | pq.poll(); 20 | pq.add(input[i]); 21 | } 22 | } 23 | while(!pq.isEmpty()) 24 | { 25 | ans.add(pq.peek()); 26 | pq.poll(); 27 | } 28 | return ans; 29 | 30 | } 31 | public static void main(String[] args) { 32 | int arr[] = {2,12,9,16,10,5,3,20,25,11,1,8,6}; 33 | ArrayList ans = kSmallest(arr.length,arr, 4); 34 | System.out.println(ans); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /Priority Queue-II/KthLargestElement.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class Solution { 3 | 4 | public static int kthLargest(int n, int[] input, int k) { 5 | ArrayList al = new ArrayList<>(); 6 | PriorityQueue pq = new PriorityQueue(Collections.reverseOrder()); 7 | 8 | 9 | for(int i =0;i queue = new QueueUsingLL<>(); 9 | int arr[] = {10,20,30,40,50}; 10 | for(int i:arr) 11 | { 12 | queue.enqueue(i); 13 | } 14 | 15 | while(!queue.isEmpty()) 16 | { 17 | try { 18 | System.out.print(queue.dequeue()+" "); 19 | } catch (QueueEmptyException e) { 20 | System.out.print(e); 21 | } 22 | } 23 | 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /Queues/Eclipse Implementation/QueueUsingLL.java: -------------------------------------------------------------------------------- 1 | 2 | 3 | import QueueArray.QueueEmptyException; 4 | 5 | public class QueueUsingLL { 6 | 7 | Node front; 8 | Node rear; 9 | int size; 10 | 11 | public QueueUsingLL() { 12 | front=null; 13 | rear=null; 14 | size=0; 15 | } 16 | 17 | public int size() { 18 | return size; 19 | } 20 | public boolean isEmpty() { 21 | return size==0; 22 | } 23 | public void enqueue(T elem) 24 | { 25 | Node cn = new Node<>(elem); 26 | if(front==null) 27 | { 28 | front = cn; 29 | rear=cn; 30 | } 31 | else 32 | { 33 | rear.next=cn; 34 | rear=rear.next; 35 | } 36 | size++; 37 | } 38 | public T front() throws QueueEmptyException { 39 | if(front==null) 40 | { 41 | throw new QueueEmptyException(); 42 | } 43 | return front.data; 44 | } 45 | public T dequeue() throws QueueEmptyException{ 46 | if(size==0) 47 | { 48 | throw new QueueEmptyException(); 49 | } 50 | T temp = front.data; 51 | front=front.next; 52 | if(front==null) 53 | { 54 | rear=null; 55 | } 56 | size--; 57 | return temp; 58 | } 59 | 60 | 61 | 62 | } 63 | -------------------------------------------------------------------------------- /Queues/Eclipse Implementation/ReverseFirstK.java: -------------------------------------------------------------------------------- 1 | import java.util.Queue; 2 | import java.util.Scanner; 3 | import java.util.Stack; 4 | import QueueArray.QueueEmptyException; 5 | import java.util.LinkedList; 6 | 7 | public class ReverseFirstK { 8 | 9 | public static QueueUsingLL reverseKElements(QueueUsingLL input, int k) { 10 | 11 | QueueUsingLL total = new QueueUsingLL<>(); 12 | Stack s1=new Stack<>(); 13 | int a = 0; 14 | while(a inp = new QueueUsingLL<>(); 42 | for(int i:arr) 43 | { 44 | 45 | inp.enqueue(i); 46 | } 47 | QueueUsingLL ans = new QueueUsingLL<>(); 48 | ans = reverseKElements(inp, 4); 49 | while(!ans.isEmpty()) 50 | { 51 | try { 52 | System.out.print(ans.dequeue()+" "); 53 | } catch (QueueEmptyException e) { 54 | // TODO Auto-generated catch block 55 | 56 | } 57 | } 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /Queues/QueueUsingLL.java: -------------------------------------------------------------------------------- 1 | /* 2 | Following is the structure of the node class for a Singly Linked List 3 | 4 | class Node { 5 | int data; 6 | Node next; 7 | 8 | public Node(int data) { 9 | this.data = data; 10 | this.next = null; 11 | } 12 | 13 | } 14 | 15 | */ 16 | 17 | public class Queue { 18 | 19 | private Node front; 20 | private Node rear; 21 | private int size; 22 | 23 | 24 | public Queue() { 25 | front = null; 26 | rear=null; 27 | size=0; 28 | } 29 | 30 | 31 | 32 | /*----------------- Public Functions of Stack -----------------*/ 33 | 34 | 35 | public int getSize() { 36 | return size; 37 | } 38 | 39 | 40 | public boolean isEmpty() { 41 | return size==0; 42 | } 43 | 44 | 45 | public void enqueue(int data) { 46 | Node cn = new Node(data); 47 | if(front==null) 48 | { 49 | front=cn; 50 | rear=cn; 51 | } 52 | else 53 | { 54 | rear.next=cn; 55 | rear=rear.next; 56 | } 57 | size++; 58 | } 59 | 60 | 61 | public int dequeue() { 62 | if(front==null) 63 | { 64 | return -1; 65 | } 66 | int temp = front.data; 67 | front=front.next; 68 | size--; 69 | return temp; 70 | } 71 | 72 | 73 | public int front() { 74 | if(front==null) 75 | { 76 | return -1; 77 | } 78 | return front.data; 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /Queues/ReverseQueue.java: -------------------------------------------------------------------------------- 1 | import java.util.LinkedList; 2 | import java.util.Queue; 3 | import java.util.Stack; 4 | 5 | public class Solution { 6 | 7 | public static void reverseQueue(Queue input) { 8 | Queue inp = input; 9 | Stack s=new Stack<>(); 10 | Queue ans = new LinkedList<>(); 11 | while(!inp.isEmpty()) 12 | { 13 | s.push(inp.poll()); 14 | } 15 | 16 | while(!s.isEmpty()) 17 | { 18 | ans.add(s.pop()); 19 | } 20 | 21 | while(!ans.isEmpty()) 22 | { 23 | System.out.print(ans.poll()+" "); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Queues/ReverseTheFirstK.java: -------------------------------------------------------------------------------- 1 | import java.util.LinkedList; 2 | import java.util.Queue; 3 | import java.util.Stack; 4 | 5 | public class Solution { 6 | 7 | public static Queue reverseKElements(Queue input, int k) { 8 | Stack rev = new Stack<>(); 9 | Queue total = new LinkedList<>(); 10 | 11 | int a=0; 12 | while(a s1; 6 | private Queue s2; 7 | 8 | 9 | public Stack() { 10 | //Implement the Constructor 11 | s1=new LinkedList<>(); 12 | s2=new LinkedList<>(); 13 | } 14 | 15 | 16 | 17 | /*----------------- Public Functions of Stack -----------------*/ 18 | 19 | 20 | public int getSize() { 21 | //Implement the getSize() function 22 | return this.s1.size(); 23 | } 24 | 25 | public boolean isEmpty() { 26 | //Implement the isEmpty() function 27 | return this.s1.size()==0; 28 | } 29 | 30 | public void push(int element) { 31 | //Implement the push(element) function 32 | this.s1.add(element); 33 | } 34 | 35 | public int pop() { 36 | if (this.isEmpty()) { 37 | return -1; 38 | } 39 | while (this.s1.size() > 1) { 40 | this.s2.add(this.s1.poll()); 41 | } 42 | int ans = this.s1.poll(); 43 | Queue temp = this.s1; 44 | this.s1 = this.s2; 45 | this.s2 = temp; 46 | return ans; 47 | } 48 | public int top() { 49 | if (this.isEmpty()) { 50 | return -1; 51 | } 52 | while (this.s1.size() > 1) { 53 | this.s2.add(this.s1.poll()); 54 | } 55 | int ans = s1.poll(); 56 | s2.add(ans); 57 | Queue temp = this.s1; 58 | this.s1 = this.s2; 59 | this.s2 = temp; 60 | return ans; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | :) 2 | -------------------------------------------------------------------------------- /Recursion Assignment/CheckAB.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | 3 | public static boolean checkAB(String input) { 4 | 5 | if(input.length()==0) 6 | { 7 | return true; 8 | } 9 | boolean ans = true; 10 | if(input.charAt(0)=='a') 11 | { 12 | if(input.length()>2 &&(input.charAt(1)=='b'&& input.charAt(2)=='b')) 13 | { 14 | ans = checkAB(input.substring(3)); 15 | } 16 | else 17 | { 18 | ans = checkAB(input.substring(1)); 19 | } 20 | 21 | } 22 | else 23 | { 24 | ans = false; 25 | } 26 | 27 | 28 | return ans; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /Recursion Assignment/CheckPalindrome.java: -------------------------------------------------------------------------------- 1 | public class solution { 2 | 3 | public static boolean isStringPalindrome(String input) { 4 | if(input.length()<=1) 5 | { 6 | return true; 7 | } 8 | 9 | 10 | if(input.charAt(0)==input.charAt(input.length()-1)) 11 | { 12 | return isStringPalindrome(input.substring(1, input.length()-1)); 13 | } 14 | else 15 | { 16 | return false; 17 | } 18 | 19 | 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Recursion Assignment/CountZeros.java: -------------------------------------------------------------------------------- 1 | public class solution { 2 | 3 | public static int countZerosRec(int input){ 4 | if(input<10) 5 | { 6 | if(input==0) 7 | { 8 | return 1; 9 | } 10 | else 11 | { 12 | return 0; 13 | } 14 | } 15 | int count = 0; 16 | 17 | if(input%10==0) 18 | { 19 | count++; 20 | } 21 | int fi = countZerosRec(input/10); 22 | return count+fi; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Recursion Assignment/GeometricSum.java: -------------------------------------------------------------------------------- 1 | public class solution { 2 | 3 | public static double findGeometricSum(int k){ 4 | if(k==0) 5 | { 6 | return 1/(Math.pow(2,k)); 7 | } 8 | 9 | double sum = findGeometricSum(k-1); 10 | double ans = 1/Math.pow(2,k)+sum; 11 | 12 | 13 | return ans; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Recursion Assignment/PairStar.java: -------------------------------------------------------------------------------- 1 | public class solution { 2 | 3 | public static String addStars(String s) { 4 | 5 | if(s.length()==1) 6 | { 7 | return s; 8 | } 9 | 10 | String small = addStars(s.substring(1)); 11 | if(small.charAt(0)==s.charAt(0)) 12 | { 13 | return s.charAt(0)+"*"+small; 14 | } 15 | return s.charAt(0)+small; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Recursion Assignment/StairCase.java: -------------------------------------------------------------------------------- 1 | public class staircase { 2 | 3 | public static int staircase(int input) 4 | { 5 | if(input<=1) 6 | { 7 | return 1; 8 | } 9 | else if(input==2) 10 | { 11 | return 2; 12 | } 13 | 14 | int x = staircase(input-1); 15 | int y = staircase(input - 2); 16 | int z = staircase(input - 3); 17 | 18 | return x+y+z; 19 | } 20 | 21 | public static void main(String[] args) { 22 | System.out.println((staircase(4))); 23 | 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /Recursion Assignment/StringToInteger.java: -------------------------------------------------------------------------------- 1 | public class solution { 2 | 3 | public static int convertStringToInt(String input){ 4 | { 5 | if(input.length()==0) 6 | { 7 | return 0; 8 | } 9 | int ans = 0; 10 | if(input.charAt(0)==0) 11 | { 12 | convertStringToInt(input.substring(1)); 13 | } 14 | else 15 | { 16 | ans = Integer.parseInt(input); 17 | } 18 | return ans; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Recursion Assignment/SumofDigits.java: -------------------------------------------------------------------------------- 1 | 2 | public class solution { 3 | 4 | public static int sumOfDigits(int input){ 5 | 6 | if(input/10 ==0) 7 | { 8 | return input; 9 | } 10 | 11 | int sum = sumOfDigits(input/10); 12 | int ans = sum+(input%10); 13 | return ans; 14 | 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Recursion Assignment/multiplication.java: -------------------------------------------------------------------------------- 1 | public class solution { 2 | 3 | public static int multiplyTwoIntegers(int m, int n){ 4 | if(m==0||n==0) 5 | { 6 | return 0; 7 | } 8 | 9 | if(m==1) 10 | { 11 | return n; 12 | } 13 | int fi = multiplyTwoIntegers(m-1,n); 14 | int ans = fi+n; 15 | return ans; 16 | 17 | 18 | 19 | 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Recursion-1/CalculatePower.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | 3 | 4 | public static int power(int x, int n) { 5 | if(n==0) 6 | { 7 | return 1; 8 | } 9 | 10 | int smallOutput = power(x, n -1); 11 | int output = x * smallOutput; 12 | 13 | return output; 14 | 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /Recursion-1/CheckNumberinArray.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | 3 | public static boolean checkNumber(int input[], int x) { 4 | 5 | if(input[0]==x) 6 | { 7 | return true; 8 | } 9 | if(input.length==1 && input[0]!=x) 10 | { 11 | return false; 12 | } 13 | 14 | int smallarray[] = new int[input.length-1]; 15 | for(int i = 1;i=ei) 12 | { 13 | return; 14 | } 15 | int pivotIndex = partition(input,si,ei); 16 | quickS(input,pivotIndex+1,ei); 17 | quickS(input,si,pivotIndex-1); 18 | 19 | } 20 | 21 | public static int partition(int a[],int si,int ei) 22 | { 23 | int pivotElement = a[si]; 24 | int smallnumcount = 0; 25 | 26 | for(int i = si+1;i<=ei;i++) 27 | { 28 | if(a[i]= pivotElement) 46 | { 47 | j--; 48 | } 49 | else 50 | { 51 | temp = a[i]; 52 | a[i]=a[j]; 53 | a[j] = temp; 54 | i++; 55 | j--; 56 | } 57 | } 58 | return si + smallnumcount; 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /Recursion-2/RemoveDuplicatesRecurssively.java: -------------------------------------------------------------------------------- 1 | public class RemoveD { 2 | public static String removeConsecutiveDuplicates(String s) { 3 | if(s.length()==1) 4 | { 5 | return s; 6 | } 7 | String smallO = removeConsecutiveDuplicates(s.substring(1)); 8 | if(s.charAt(0)==smallO.charAt(0)) 9 | { 10 | return smallO; 11 | } 12 | else 13 | { 14 | return s.charAt(0)+smallO; 15 | } 16 | 17 | } 18 | 19 | public static void main(String[] args) { 20 | System.out.println(removeConsecutiveDuplicates("aabccba")); 21 | 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /Recursion-2/RemoveX.java: -------------------------------------------------------------------------------- 1 | public class RemoveX { 2 | public static String removeX(String input){ 3 | // Write your code here 4 | if(input.length()==0) 5 | { 6 | return input; 7 | } 8 | 9 | String smallO = removeX(input.substring(1)); 10 | if(input.charAt(0)=='x') 11 | { 12 | return smallO; 13 | } 14 | else 15 | return input.charAt(0)+smallO; 16 | 17 | 18 | } 19 | public static void main(String[] args) { 20 | System.out.println(removeX("xaxb")); 21 | 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /Recursion-2/TowerOfHanoi.java: -------------------------------------------------------------------------------- 1 | 2 | public class solution { 3 | 4 | public static void towerOfHanoi(int disks, char source, char auxiliary, char destination) { 5 | if(disks==1) 6 | { 7 | System.out.println(source+" "+destination); 8 | return; 9 | } 10 | if(disks==0) 11 | { 12 | return; 13 | } 14 | 15 | towerOfHanoi(disks-1,source,destination,auxiliary); 16 | System.out.println(source+" "+destination); 17 | towerOfHanoi(disks-1,auxiliary,source,destination); 18 | 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Recursion-3/FindSubsequences.java: -------------------------------------------------------------------------------- 1 | package practice; 2 | 3 | public class Subsequences { 4 | 5 | public static String[] findSub(String s) 6 | { 7 | if(s.length()==0) 8 | { 9 | String ans[] = {""}; 10 | return ans; 11 | } 12 | String smallAns[] = findSub(s.substring(1)); 13 | String ans[] = new String[2 * smallAns.length]; 14 | for(int i =0;i s = new Stack<>(); 6 | 7 | 8 | // for(int i=0;i stack = new Stack<>(); 34 | 35 | 36 | if(stack.isEmpty()==true && expression.charAt(0)==')') 37 | { 38 | return false; 39 | } 40 | for(int i =0;i s = new Stack<>(); 10 | int count = 0; 11 | for(int i =0;i input, Stack extra) { 5 | if(input.size()<=1) 6 | { 7 | return; 8 | } 9 | int temp = input.pop(); 10 | reverseStack(input, extra); 11 | while(!input.isEmpty()) 12 | { 13 | int z = input.pop(); 14 | extra.push(z); 15 | } 16 | input.push(temp); 17 | while(!extra.isEmpty()) 18 | { 19 | int k = extra.pop(); 20 | input.push(k); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Stack/StackUsingLL.java: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | Following is the structure of the node class for a Singly Linked List 4 | 5 | class Node { 6 | int data; 7 | Node next; 8 | 9 | public Node(int data) { 10 | this.data = data; 11 | this.next = null; 12 | } 13 | 14 | } 15 | 16 | */ 17 | 18 | public class Stack { 19 | 20 | private Node head; 21 | private int size; 22 | 23 | 24 | public Stack() { 25 | head=null; 26 | size=0; 27 | } 28 | 29 | 30 | 31 | 32 | /*----------------- Public Functions of Stack -----------------*/ 33 | 34 | 35 | public int getSize() { 36 | //Implement the getSize() function 37 | return size; 38 | } 39 | 40 | public boolean isEmpty() { 41 | //Implement the isEmpty() function 42 | return size==0; 43 | } 44 | 45 | public void push(int element) { 46 | //Implement the push(element) function 47 | Node newNode = new Node(element); 48 | newNode.next=head; 49 | head=newNode; 50 | size++; 51 | 52 | } 53 | 54 | public int pop() { 55 | //Implement the pop() function 56 | if(head==null){ 57 | return -1; 58 | } 59 | int temp = head.data; 60 | head=head.next; 61 | size--; 62 | return temp; 63 | } 64 | 65 | public int top() { 66 | //Implement the top() function 67 | if(head==null){ 68 | return -1; 69 | } 70 | return head.data; 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /Stack/StockSpan.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Stack; 3 | public class Solution { 4 | 5 | public static int[] stockSpan(int[] price) { 6 | //O(n^2) 7 | // int n = price.length; 8 | // int ans[]=new int[n]; 9 | // int i = n-1; 10 | // while(i>=0) 11 | // { 12 | // int k =i; 13 | // int count = 0; 14 | // for(int j=i-1;j>=0;j--) 15 | // { 16 | // if(price[i]price[j]) 21 | // { 22 | // count++; 23 | // } 24 | // } 25 | // i--; 26 | // ans[k]=count+1; 27 | // } 28 | // return ans; 29 | // } 30 | // } 31 | 32 | Stack stk = new Stack<>(); 33 | int n = price.length; 34 | int[] output = new int[n]; 35 | stk.push(0); 36 | output[0] = 1; 37 | for (int i = 1; i < n; ++i) { 38 | while (!stk.isEmpty() && price[stk.peek()] < price[i]) { 39 | stk.pop(); 40 | } 41 | if (stk.isEmpty()) { 42 | output[i] = i + 1; 43 | } else { 44 | output[i] = i - stk.peek(); 45 | } 46 | stk.push(i); 47 | } 48 | return output; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /Strings/AllSubstrings.java: -------------------------------------------------------------------------------- 1 | 2 | public class Solution { 3 | 4 | public static void printSubstrings(String str) { 5 | int n = str.length(); 6 | 7 | String ans = ""; 8 | 9 | int i=0; 10 | while(i1) { 12 | x=x+(char)(count+'0'); 13 | } 14 | 15 | } 16 | 17 | else if(x.charAt(x.length()-1)==str.charAt(i)) { 18 | count++; 19 | } 20 | else { 21 | if(count>1) { 22 | x=x+(char)(count+'0'); 23 | } 24 | x=x+str.charAt(i); 25 | count=1; 26 | } 27 | 28 | } 29 | return x; 30 | 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /Strings/CountWords.java: -------------------------------------------------------------------------------- 1 | 2 | public class Solution { 3 | 4 | public static int countWords(String str) { 5 | //Your code goes here 6 | int count = 1; 7 | if(str.length()==0) 8 | { 9 | return count = 0; 10 | } 11 | for(int i = 0;imax) 19 | { 20 | max = count; 21 | maxIndex = i; 22 | } 23 | 24 | } 25 | ans = str.charAt(maxIndex); 26 | count=0; 27 | } 28 | 29 | return ans; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Strings/RemoveCharacter.java: -------------------------------------------------------------------------------- 1 | 2 | public class Solution { 3 | 4 | public static String removeAllOccurrencesOfChar(String str, char ch) { 5 | // Your code goes here 6 | int n = str.length(); 7 | String ans= ""; 8 | for(int i = 0;i=0;i--) { 7 | 8 | if(i==0) { 9 | x=x+input.substring(0,space); 10 | } 11 | 12 | else if(input.charAt(i)==' ') { 13 | x=x+input.substring(i+1,space)+" "; 14 | space=i; 15 | } 16 | } 17 | return x; 18 | 19 | } 20 | } 21 | 22 | 23 | -------------------------------------------------------------------------------- /Strings/StringPalindrome.java: -------------------------------------------------------------------------------- 1 | 2 | public class Solution { 3 | 4 | public static boolean isPalindrome(String str) { 5 | //Your code goes here 6 | int n = str.length(); 7 | 8 | boolean pal = true; 9 | for(int i = 0;i 0) { 8 | digits++; 9 | num /= 10; 10 | } 11 | int newNum = 0; 12 | num = n; 13 | while(num > 0) { 14 | int last = num % 10; 15 | newNum += Math.pow(last, digits); 16 | num /= 10; 17 | } 18 | if(newNum == n) { 19 | System.out.println("true"); 20 | } 21 | else { 22 | System.out.println("false"); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Test-1(java fundametals)/NumberStarPattern1.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class runner { 3 | 4 | public static void main(String[] args) { 5 | // Write your code here 6 | int n; 7 | Scanner sc = new Scanner(System.in); 8 | n=sc.nextInt(); 9 | 10 | int i = 1; 11 | while(i<=n) 12 | { 13 | int j = 1; 14 | while(j<=n) 15 | { 16 | if(n-j+1==i) 17 | { 18 | System.out.print('*'); 19 | } 20 | else 21 | { 22 | System.out.print(n-j+1); 23 | } 24 | j+=1; 25 | 26 | } 27 | i+=1; 28 | System.out.println(); 29 | } 30 | 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Test-1(java fundametals)/ZerosAndStars.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Solution { 3 | 4 | public static void main(String[] args) { 5 | 6 | Scanner scan=new Scanner(System.in); 7 | int n=scan.nextInt(); 8 | int l=2*n+1; 9 | for(int i=1;i<=n;i++) { 10 | for(int j=1;j<=l;j++) { 11 | if(j==(n+1) || j==i || i== l-j+1) { 12 | System.out.print("*"); 13 | }else { 14 | System.out.print(0); 15 | } 16 | } 17 | System.out.println(); 18 | } 19 | 20 | 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /Test-2(java fundamentals)/LeadersInArray.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | 3 | public static void leaders(int[] input) { 4 | 5 | int n = input.length; 6 | boolean flag = true; 7 | 8 | for(int i=0;i=input[j]) 13 | { 14 | flag =true; 15 | } 16 | else 17 | { 18 | flag = false; 19 | break; 20 | } 21 | } 22 | if(flag==true) 23 | { 24 | System.out.print(input[i]+" "); 25 | } 26 | else 27 | { 28 | continue; 29 | } 30 | } 31 | System.out.print(input[n-1]); 32 | 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Test-2(java fundamentals)/MinimumLengthWord.java: -------------------------------------------------------------------------------- 1 | 2 | public class Solution { 3 | 4 | public static String minLengthWord(String input){ 5 | 6 | int startIndex = 0; 7 | int endIndex = 0; 8 | int min = Integer.MAX_VALUE; 9 | int count = 0; 10 | String ans = ""; 11 | String minans= ""; 12 | 13 | for(int i = startIndex;i