├── .gitignore ├── Array1 ├── ArrangeNumbersinArray.java ├── ArraySum.java ├── DuplicateArray.java ├── FindtheDifference.java ├── TripleSum.java ├── UniqueNumber.java ├── linearSearch.java ├── pairSumInArray.java ├── printArrayIntersection.java ├── sort01.java └── swapAlternate.java ├── Array2 ├── BinarySearch.java ├── BubbleSort.java ├── IntersectionSort.java ├── MaximizeTheSum.java ├── MergeTwoSortedArray.java ├── PushZeroToEnd.java ├── RotateArray.java ├── SeclectionSort.java ├── SecondLargestArray.java ├── checkarrayrotation.java ├── sort012.java └── sumoftwoarray.java ├── Assignment Fundamental Programming -1 ├── Question 1.txt ├── Question 10.txt ├── Question 2.txt ├── Question 3.txt ├── Question 4.txt ├── Question 5.txt ├── Question 6.txt ├── Question 7.txt ├── Question 8.txt └── Question 9.txt ├── Assignment2 Fundamental Programming2 ├── Question 1.txt ├── Question 2.java ├── Question 3.txt ├── Question 4.txt ├── Question 5.txt ├── Question 6.txt └── Question 7.txt ├── BST ├── BSTClass.java ├── BSTTOLL.java ├── CheckBST.java ├── ConstructBST.java ├── DistanceFromKtoNode.java ├── FindPathinBST.java ├── LCAofBST.java ├── LCAofBT.java ├── LargesTBst.java ├── NodeinaGivenRanga.java ├── PairSumBT.java ├── PairSuminBST.java ├── PathSumRoottoLeaf.java ├── Replacesum.java ├── SearchinBST.java └── insertDuplicateNode.java ├── Better Solutions and Quick Sort ├── PalindromeString.java ├── QuickSort.java └── arrayEquilibrium.java ├── Binary Tree ├── CountLeafNodeBT.java ├── Findiameter.java ├── FinfNode.java ├── LevelWiseLL.java ├── MaxDataNodeBT.java ├── NodeWithoutSibling.java ├── NodesGreaterThanXBT.java ├── PrintLevelWiseBT.java ├── RemoveLeafNode.java ├── SumofAllNodesBt.java ├── ZigZagTree.java ├── getTreeFromPostAndIn.java ├── getTreeFromPreorderAndInorder.java ├── isBalanced.java ├── levelOrderTransveral.java ├── mirror.java └── sumofallnodesBT2.java ├── DP 1 AND 2 ├── BalancedBTSNum.java ├── BinarySearch.java ├── MinCountofSq,java ├── MinStepsto1using Dp.java ├── NumberBalancedBTsDp.java ├── Stair.java └── minstepsto1.java ├── DP ├── BinarySearch.java ├── EditDistance.java ├── EditDistance1.java ├── LCS.java ├── LIS1.java ├── LootHouse.java ├── MaxSquareWithAllZeros.java ├── MinSumPath.java ├── Staircase.javaaa ├── WaysToMakeChange.java ├── allpossibleways.java ├── balancedBTSDP.java ├── balancedBTSrec.java ├── cointower.java ├── findMaxSquareWithAllZeros.java ├── knapsack01.java ├── knapsack01d.java ├── mcm.java ├── shortestsub.java └── takenumberto1.java ├── Fundamental Programming 1 ├── Question 1.txt ├── Question 2.txt ├── Question 3.txt ├── Question 4.txt ├── Question 5.txt ├── Question 6.txt ├── Question 7.txt ├── Question 8.txt └── README.md ├── Graphs ├── BFSTransversal.java ├── BinarySearch.java ├── HasPath.java ├── allcomponentsConnected.java ├── getPathBFS.java └── isConnected.java ├── HashMaps ├── HighestFrequency.java ├── LIS.java ├── LongestSubsetWithZeroSum.java ├── PairSumTo0.java ├── VerticalOrder.java ├── extractuniqueChar.java ├── pairwithdifferencek.java └── printArray.java ├── LL1 ├── DeleteNodell.java ├── Lengthofll.java ├── PrintithNode.java ├── deletenodereLLc.java ├── insertNode.java ├── insertnoderecLL.java └── lengthRecLL.java ├── Lecture 3 Fundamental Programming ├── Question 1.txt ├── Question 2.txt ├── Question 3.txt ├── Question 4.txt ├── Question 5.txt ├── Question 6.txt └── Question 7.txt ├── Linked List 2 ├── AppendLastNtoFirst.java ├── BubbleSortLL.java ├── DeleteEveryNnode.java ├── FindanElement.java ├── FindanElementrec.java ├── MergeSortll.java ├── RearrageLL.java ├── evenafteroddLL.java ├── kReverse.java └── nthNodeFRomLast.java ├── LinkedList1 ├── DeleteNode.java ├── ElliminateDuplicate.java ├── InsertNode.java ├── LengthRec.java ├── LengthofLL.java ├── Palindrome.java ├── PrintReverLL.java ├── ReverseLLiterarive.java ├── ReverseLLrecusive.java ├── deletenoderec.java ├── insertnoderec.java ├── mergetwosortedll.java ├── midpoint.java ├── printiThNode.java └── swaptwonode.java ├── OOPS 1 ├── ComplexNumber.java └── PolynomialClass.java ├── Priority Queues ├── BuytheTicket.java ├── KLargestNumber.java ├── KthLargestElement.java ├── RemoveMin.java ├── RunningMedian.java ├── checkmaxheap.java ├── inPlaceHeapSort.java ├── maxPQ.java └── mergeKsortaraay.java ├── README.md ├── Recursion 1 ├── AllIndicesOfNumber.java ├── CheckNumberInArray.java ├── CheckPalindromerec.java ├── CountZero.java ├── FirstIndexofNumber.java ├── GeometricSum.java ├── LasttIndexofNumber.java ├── MultiplicationRec.java ├── NumberOfDigits.java ├── PairSum.java ├── PrintNumbers.java ├── RemoveX.java ├── ReplacePi.java ├── StringtoInt.java ├── SumNnaturalNumber.java ├── SumOfArray.java ├── TowerOfhanoi.java ├── power.java └── sumofDigitrec.java ├── Recursion 2 ├── BinarySearchRec.java ├── ChechAB.java ├── KeypadCombination.java ├── MergeSort.java ├── PrintKeypad.java ├── PrintPermuation.java ├── PrintSubsequence.java ├── PrintSubsetSumtoK.java ├── Printallcodes.java ├── RemoveDuplicate.java ├── ReplaceCharacter.java ├── ReturnKeypad.java ├── ReturnPermutation.java ├── ReturnSubsequenceofstring.java ├── ReturnSubsetSumtoK.java ├── ReturnSubsetofanArray.java ├── ReturnallCodes.java ├── StairCase.java └── printsubsetofanArray.java ├── Stack and queue ├── IfbracketareBalanced.java ├── QueueUsingLL.java ├── ReverseQueue.java ├── ReverseStack.java ├── StackUsingLL.java ├── checkRedundantbrackets.java └── stockspan.java ├── String and 2D ├── CheckPermuation.java ├── CompressString.java ├── FindLargestRowandColoum.java ├── HighestOccuringCharacter.java ├── RemoveChar.java ├── RemoveConsecutiveDuplicates.java ├── ReplaceChar.java ├── ReverseEachWord.java ├── ReverseEachWord1.java ├── ReverseStringWordWise.java ├── SpiralPrint.java ├── StringPalindrome.java ├── allsubstringsofaString.java ├── countWords.java ├── minimunLengthword.java ├── rowWiseSum.java └── wavePrint.java ├── Test 1 ├── NumberStarPattern2.java ├── SecondLargest.java └── pyramidNumberPattern.java ├── Tree ├── CountNODELeaf.java ├── PrintLevelwise.java ├── ReplaceNodeWithDepth.java ├── countleafnode.java ├── countnumberofnodes.java ├── genericelemet.java ├── identical.java ├── maxdatanode.java ├── nextLargerElement.java ├── nodesatdepthk.java ├── nodesgreaterthanX.java ├── numberofnodesgreaterthanX.java ├── printtreelevelwise.java ├── secondlargestelementinTree.java ├── sumofallnodes.java └── sumofchildandnodemax.java └── trie ├── CountW.java ├── patternmatching.java └── searchword.java /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | # Package Files # 14 | *.jar 15 | *.war 16 | *.nar 17 | *.ear 18 | *.zip 19 | *.tar.gz 20 | *.rar 21 | 22 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 23 | hs_err_pid* 24 | -------------------------------------------------------------------------------- /Array1/ArrangeNumbersinArray.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Solution { 3 | 4 | public static int[] takeInput() { 5 | Scanner s = new Scanner(System.in); 6 | int n = s.nextInt(); 7 | int[] arr = new int[n]; 8 | for(int i = 0;i < n;i++) { 9 | arr[i] = s.nextInt(); 10 | } 11 | return arr; 12 | } 13 | 14 | public static void printArray(int[] arr) { 15 | for(int i = 0;i < arr.length;i++) { 16 | System.out.print(arr[i] + " "); 17 | } 18 | } 19 | public static int[] arrange(int n){ 20 | 21 | /* Your class should be named Solution 22 | * Don't write main(). 23 | * Don't read input, it is passed as function argument. 24 | * Return output and don't print it. 25 | * Taking input and printing output is handled automatically. 26 | */ 27 | int i = 0,j = n-1,c = 1; 28 | int[] arr = new int[n]; 29 | for(c = 1; c <= n ;c++){ 30 | if(c % 2 != 0) { 31 | arr[i] = c; 32 | i++; 33 | } 34 | else { 35 | arr[j] = c; 36 | j--;} 37 | } 38 | return arr; 39 | 40 | } 41 | 42 | public static void main(String[] args) { 43 | Scanner s = new Scanner(System.in); 44 | int n = s.nextInt(); 45 | int[] arr = arrange(n); 46 | printArray(arr); 47 | 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /Array1/ArraySum.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Scanner; 3 | 4 | public class Solution { 5 | 6 | 7 | public static int sum(int[] input){ 8 | 9 | int retVal = Sum(input); 10 | return retVal; 11 | 12 | } 13 | 14 | public static int Sum(int[] arr) { 15 | int sum = 0; 16 | for(int i=0; iarr[j+1]) { 23 | int t = arr[j]; 24 | arr[j]=arr[j+1]; 25 | arr[j+1] = t; 26 | } 27 | } 28 | } 29 | 30 | } 31 | public static void main(String[] args) { 32 | int[] arr = takeInput(); 33 | bubbleSort(arr); 34 | printArray(arr); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Array2/IntersectionSort.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class InsertionSort { 3 | //arr is the input array you have to sort 4 | public static void insertionSort(int[] arr){ 5 | //write your code here 6 | int start=1; 7 | int end=arr.length-1; 8 | int temp; 9 | while(start<=end) 10 | { 11 | int i=start-1; 12 | temp=arr[start]; 13 | while(i>=0 && arr[i]>temp) 14 | { 15 | arr[i+1]=arr[i]; 16 | i-=1; 17 | } 18 | arr[i+1]=temp; 19 | start+=1; 20 | } 21 | 22 | 23 | } 24 | public static void printArray(int input[]) 25 | { 26 | for(int i=0;ilargest) { 21 | secondLargest=largest; 22 | largest=arr[j]; 23 | }else if(arr[j]==largest) { 24 | continue; 25 | }else if(arr[j]secondLargest) { 27 | secondLargest=arr[j]; 28 | } 29 | } 30 | } 31 | return secondLargest; 32 | 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Array2/checkarrayrotation.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class SecondLargest{ 3 | 4 | public static int secondLargestElement(int[] arr) { 5 | /* Your class should be named SecondLargest 6 | * Don't write main(). 7 | * Don't read input, it is passed as function argument. 8 | * Return output and don't print it. 9 | * Taking input and printing output is handled automatically. 10 | */ 11 | if((arr.length<=1)||(arr[0]==arr[arr.length-1])) { 12 | return -2147483648; 13 | } 14 | 15 | 16 | int largest=Integer.MIN_VALUE; 17 | int secondLargest=Integer.MIN_VALUE; 18 | 19 | for(int j=0;jlargest) { 21 | secondLargest=largest; 22 | largest=arr[j]; 23 | }else if(arr[j]==largest) { 24 | continue; 25 | }else if(arr[j]secondLargest) { 27 | secondLargest=arr[j]; 28 | } 29 | } 30 | } 31 | return secondLargest; 32 | 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Assignment Fundamental Programming -1/Question 1.txt: -------------------------------------------------------------------------------- 1 | Find average Marks 2 | Write a program to input name (as a single character) and marks of three tests of a student (all integers). Then calculate and print the name and average (integer) of best two test marks. 3 | All the test marks are integers and calculate average also as integer. That is, you need to print the integer part of average only, neglect the decimal part. 4 | Input format : 5 | Line 1 : Name (Single character) 6 | Line 2 : 3 Test marks (separated by space) 7 | Output format : 8 | Name Average (separated by space) 9 | Sample Input 1 : 10 | a 11 | 345 123 10 12 | Sample Output 1 : 13 | a 234 14 | Sample Input 2 : 15 | b 16 | 23 12 44 17 | Sample Output 2 : 18 | b 33 19 | 20 | import java.util.Scanner; 21 | public class Main { 22 | public static void main(String[] args) { 23 | // Write your code here 24 | Scanner s = new Scanner(System.in); 25 | char r=s.next().charAt(0); 26 | int m1 =s.nextInt(); 27 | int m2 =s.nextInt(); 28 | int m3 =s.nextInt(); 29 | int sub = 0; 30 | int average; 31 | if (m1<=m2 && m1<=m3) 32 | { 33 | sub=m1; 34 | } 35 | else if (m2<=m1 && m2<=m3) 36 | { 37 | sub=m2; 38 | } 39 | else if (m3<=m2 && m3<=m1) 40 | { 41 | sub=m3; 42 | } 43 | average=(m1+m2+m3-sub)/2; 44 | 45 | System.out.println(r+" "+average); 46 | } 47 | 48 | } 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /Assignment Fundamental Programming -1/Question 10.txt: -------------------------------------------------------------------------------- 1 | Number Pattern 2 | Print the following pattern for the given N number of rows. 3 | Pattern for N = 4 4 | 1234 5 | 123 6 | 12 7 | 1 8 | Input format : 9 | Integer N (Total no. of rows) 10 | Output format : 11 | Pattern in N lines 12 | Sample Input : 13 | 5 14 | Sample Output : 15 | 12345 16 | 1234 17 | 123 18 | 12 19 | 1 20 | 21 | import java.util.Scanner; 22 | public class Main { 23 | 24 | public static void main(String[] args) { 25 | // Write your code here 26 | Scanner s = new Scanner(System.in); 27 | int a = s.nextInt(); 28 | int i =1; 29 | int temp =a; 30 | while(i<=a) 31 | { 32 | int j=1; 33 | while(j<=temp) 34 | { 35 | System.out.print(j); 36 | j+=1; 37 | } 38 | i+=1; 39 | temp-=1; 40 | System.out.print("\n"); 41 | } 42 | } 43 | } 44 | 45 | 46 | -------------------------------------------------------------------------------- /Assignment Fundamental Programming -1/Question 2.txt: -------------------------------------------------------------------------------- 1 | Find character case 2 | Write a program to determine whether the entered character is in uppercase or lowercase, or is an invalid character. 3 | Print 4 | 1 for uppercase 5 | 0 for lowercase 6 | -1 for any other character (special characters or others) 7 | Input format : 8 | Single Character 9 | Output format : 10 | 1 or 0 or -1 11 | Sample Input 1 : 12 | v 13 | Sample Output 1 : 14 | 0 15 | Sample Input 2 : 16 | V 17 | Sample Output 2 : 18 | 1 19 | Sample Input 3 : 20 | # 21 | Sample Output 3 : 22 | -1 23 | 24 | 25 | import java.util.Scanner; 26 | public class Main { 27 | public static void main(String[] args) { 28 | // Write your code here 29 | Scanner s = new Scanner(System.in); 30 | char c = s.next().charAt(0); 31 | 32 | if (c >= 'a' && c <= 'z') { 33 | System.out.println(0);} 34 | else if (c >= 'A' && c <= 'Z') { 35 | System.out.println(1); 36 | } 37 | else { 38 | System.out.println(-1); 39 | } 40 | } 41 | } 42 | 43 | 44 | -------------------------------------------------------------------------------- /Assignment Fundamental Programming -1/Question 3.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/surabhiraj99/CodingNinjas/87084f5538d5636297873fa2a29658835aec9105/Assignment Fundamental Programming -1/Question 3.txt -------------------------------------------------------------------------------- /Assignment Fundamental Programming -1/Question 5.txt: -------------------------------------------------------------------------------- 1 | Sum of even & odd 2 | Write a program to input an integer N and print the sum of all its even digits and sum of all its odd digits separately. 3 | Digits means numbers not the places. That is, if the given integer is "13245", even digits are 2 & 4 and odd digits are 1, 3 & 5. 4 | Input format : 5 | Integer N 6 | Output format : 7 | Sum_of_Even_Digits Sum_of_Odd_Digits 8 | (Print first even sum and then odd sum separated by space) 9 | Sample Input : 10 | 1234 11 | Sample Output : 12 | 6 4 13 | 14 | import java.util.Scanner; 15 | public class Main { 16 | public static void main(String[] args) { 17 | // Write your code here 18 | Scanner s = new Scanner(System.in); 19 | 20 | int n=s.nextInt(); 21 | int div,evensum=0,oddsum=0; 22 | 23 | while(n!=0){ 24 | div=n%10; 25 | if(div%2==0){ 26 | evensum+=div; 27 | } 28 | else{ 29 | oddsum+=div; 30 | } 31 | n=n/10; 32 | } 33 | System.out.print(evensum+" "+oddsum); 34 | } 35 | } 36 | 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /Assignment Fundamental Programming -1/Question 6.txt: -------------------------------------------------------------------------------- 1 | Find power of a number 2 | Write a program to find x to the power n (i.e. x^n). Take x and n from the user. You need to print the answer. 3 | Input format : 4 | Two integers x and n (separated by space) 5 | Output Format : 6 | x^n (i.e. x raise to the power n) 7 | Sample Input 1 : 8 | 3 4 9 | Sample Output 1 : 10 | 81 11 | Sample Input 2 : 12 | 2 5 13 | Sample Output 2 : 14 | 32 15 | 16 | import java.util.Scanner; 17 | public class Main { 18 | 19 | public static void main(String[] args) { 20 | // Write your code here 21 | Scanner s = new Scanner(System.in); 22 | int n = s.nextInt(); 23 | int n1 = s.nextInt(); 24 | int j = 1; 25 | int i = 1; 26 | while (i <= n1) { 27 | j = j * n; 28 | i = i + 1; 29 | } 30 | System.out.println(j); 31 | } 32 | 33 | } 34 | 35 | 36 | -------------------------------------------------------------------------------- /Assignment Fundamental Programming -1/Question 7.txt: -------------------------------------------------------------------------------- 1 | Number Pattern 1 2 | Print the following pattern for the given N number of rows. 3 | Pattern for N = 4 4 | 1 5 | 11 6 | 111 7 | 1111 8 | Input format : 9 | Integer N (Total no. of rows) 10 | Output format : 11 | Pattern in N lines 12 | Sample Input : 13 | 5 14 | Sample Output : 15 | 1 16 | 11 17 | 111 18 | 1111 19 | 11111 20 | 21 | 22 | import java.util.Scanner; 23 | public class Main { 24 | 25 | public static void main(String[] args) { 26 | // Write your code here 27 | Scanner s = new Scanner(System.in); 28 | int a = s.nextInt(); 29 | int i=1; 30 | while(i<=a) 31 | { 32 | int j=1; 33 | while(j<=i) 34 | { 35 | System.out.print("1"); 36 | j+=1; 37 | } 38 | System.out.print("\n"); 39 | i+=1; 40 | } 41 | } 42 | } 43 | 44 | -------------------------------------------------------------------------------- /Assignment Fundamental Programming -1/Question 8.txt: -------------------------------------------------------------------------------- 1 | Number Pattern 2 2 | Print the following pattern for the given N number of rows. 3 | Pattern for N = 4 4 | 1 5 | 11 6 | 202 7 | 3003 8 | Input format : 9 | Integer N (Total no. of rows) 10 | Output format : 11 | Pattern in N lines 12 | Sample Input : 13 | 5 14 | Sample Output : 15 | 1 16 | 11 17 | 202 18 | 3003 19 | 40004 20 | 21 | 22 | import java.util.Scanner; 23 | public class Main { 24 | 25 | public static void main(String[] args) { 26 | // Write your code here 27 | Scanner s = new Scanner(System.in); 28 | 29 | int n=s.nextInt(); 30 | int i=1; 31 | int incr=1; 32 | while(i<=n){ 33 | int j=1; 34 | while(j<=i){ 35 | if(j==1){ 36 | System.out.print(incr); 37 | } 38 | else if(j==i){ 39 | System.out.print(incr); 40 | incr++; 41 | } 42 | else{ 43 | System.out.print("0"); 44 | } 45 | j++; 46 | } 47 | System.out.println(""); 48 | i++; 49 | } 50 | 51 | 52 | } 53 | } 54 | 55 | 56 | -------------------------------------------------------------------------------- /Assignment Fundamental Programming -1/Question 9.txt: -------------------------------------------------------------------------------- 1 | Number Pattern 3 2 | Print the following pattern for the given N number of rows. 3 | Pattern for N = 4 4 | 1 5 | 11 6 | 121 7 | 1221 8 | Input format : 9 | Integer N (Total no. of rows) 10 | Output format : 11 | Pattern in N lines 12 | Sample Input : 13 | 5 14 | Sample Output : 15 | 1 16 | 11 17 | 121 18 | 1221 19 | 12221 20 | 21 | 22 | import java.util.Scanner; 23 | public class Main { 24 | 25 | public static void main(String[] args) { 26 | // Write your code here 27 | Scanner s = new Scanner(System.in); 28 | int a = s.nextInt(); 29 | int i=1; 30 | System.out.println("1"); 31 | while(i<=a-1) 32 | { 33 | int j=1; 34 | System.out.print(1); 35 | while(j<=(i+1-2)) 36 | { 37 | System.out.print("2"); 38 | j+=1; 39 | 40 | } 41 | System.out.print("1"+"\n"); 42 | i+=1; 43 | } 44 | } 45 | } 46 | 47 | 48 | -------------------------------------------------------------------------------- /Assignment2 Fundamental Programming2/Question 1.txt: -------------------------------------------------------------------------------- 1 | Sum or Product 2 | Write a program that asks the user for a number N and a choice C. 3 | And then give him the possibility to choose between computing the sum 4 | and computing the product of 1 ,..., N.If user enters C is equal to - 5 | 1 : Print sum of 1 to N numbers 6 | 2 : Print product of 1 to N numbers 7 | Any other number : print -1 8 | Input format : 9 | Line 1 : Integer N 10 | Line 2 : Choice C (1 or 2) 11 | Output Format : 12 | Sum or product according to user's choice 13 | Sample Input 1 : 14 | 10 15 | 1 16 | Sample Output 1 : 17 | 55 18 | Sample Input 2 : 19 | 10 20 | 2 21 | Sample Output 2 : 22 | 3628800 23 | Sample Input 3 : 24 | 10 25 | 4 26 | Sample Output 3 : 27 | -1 28 | 29 | import java.util.Scanner; 30 | public class Main { 31 | 32 | public static void main(String[] args) { 33 | // Write your code here 34 | Scanner s = new Scanner(System.in); 35 | int n=s.nextInt(); 36 | s.nextLine(); 37 | int c=s.nextInt(); 38 | if(c==1) { 39 | int sum=0; 40 | int i=1; 41 | while(i<=n) { 42 | sum=sum+i; 43 | i=i+1; 44 | } 45 | System.out.println(sum); 46 | } 47 | 48 | else if(c==2) { 49 | int product=1; 50 | int i=1; 51 | while(i<=n) { 52 | product=product*i; 53 | i=i+1; 54 | } 55 | System.out.println(product); 56 | } 57 | else System.out.println(-1); 58 | 59 | } 60 | } 61 | 62 | 63 | -------------------------------------------------------------------------------- /Assignment2 Fundamental Programming2/Question 2.java: -------------------------------------------------------------------------------- 1 | Terms of AP 2 | Write a program to print first x terms of the series 3N + 2 which are not multiples of 4. 3 | N varies from 1 to 1000. 4 | Input format : 5 | Integer x 6 | Output format : 7 | Terms of series (separated by space) 8 | Sample Input 1 : 9 | 10 10 | Sample Output 1 : 11 | 5 11 14 17 23 26 29 35 38 41 12 | Sample Input 2 : 13 | 4 14 | Sample Output 2 : 15 | 5 11 14 17 16 | 17 | 18 | import java.util.Scanner; 19 | public class Main { 20 | 21 | public static void main(String[] args) { 22 | // Write your code here 23 | Scanner s = new Scanner(System.in); 24 | int n=s.nextInt(); 25 | int i=1;int count=0; 26 | while(count0){ 26 | rev=10*rev+k%10; 27 | k=k/10; 28 | } 29 | System.out.println(rev); 30 | } 31 | } 32 | 33 | -------------------------------------------------------------------------------- /Assignment2 Fundamental Programming2/Question 5.txt: -------------------------------------------------------------------------------- 1 | Decimal to Binary 2 | Given a decimal number (integer N), convert it into binary and print. 3 | The binary number should be in the form of an integer. 4 | Note : The given input number could be large, so the corresponding binary number can exceed the integer range. So take the answer as long. 5 | Input format : 6 | Integer N 7 | Output format : 8 | Corresponding Binary number (long) 9 | Sample Input 1 : 10 | 12 11 | Sample Output 1 : 12 | 1100 13 | 14 | 15 | import java.util.Scanner; 16 | public class Main { 17 | 18 | public static void main(String[] args) { 19 | // Write your code here 20 | Scanner s = new Scanner(System.in); 21 | 22 | int n=s.nextInt(); 23 | long rem=0,res=0; 24 | int i=0; 25 | while(n>0){ 26 | rem=n%2; 27 | res=(long)(Math.pow(10, i)*rem)+res; 28 | n=n/2; 29 | i++; 30 | 31 | } 32 | System.out.println(res); 33 | } 34 | } 35 | 36 | -------------------------------------------------------------------------------- /Assignment2 Fundamental Programming2/Question 6.txt: -------------------------------------------------------------------------------- 1 | Binary to decimal 2 | Given a binary number as an integer N, convert it into decimal and print. 3 | Input format : 4 | An integer N 5 | Output format : 6 | Corresponding Decimal number (as integer) 7 | Sample Input 1 : 8 | 1100 9 | Sample Output 1 : 10 | 12 11 | 12 | import java.util.Scanner; 13 | public class Main { 14 | 15 | public static void main(String[] args) { 16 | // Write your code here 17 | Scanner s = new Scanner(System.in); 18 | 19 | int n=s.nextInt(); 20 | long rem=0,res=0; 21 | int i=0; 22 | while(n>0){ 23 | rem=n%2; 24 | res=(long)(Math.pow(10, i)*rem)+res; 25 | n=n/2; 26 | i++; 27 | 28 | } 29 | System.out.println(res); 30 | } 31 | } 32 | 33 | -------------------------------------------------------------------------------- /Assignment2 Fundamental Programming2/Question 7.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/surabhiraj99/CodingNinjas/87084f5538d5636297873fa2a29658835aec9105/Assignment2 Fundamental Programming2/Question 7.txt -------------------------------------------------------------------------------- /BST/CheckBST.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public static int maximum(BinaryTreeNode root) { 3 | if (root == null) { 4 | return Integer.MIN_VALUE; 5 | } 6 | return Math.max(root.data, Math.max(maximum(root.left), maximum(root.right))); 7 | } 8 | 9 | public static int minimum(BinaryTreeNode root) { 10 | if (root == null) { 11 | return Integer.MAX_VALUE; 12 | } 13 | return Math.min(root.data, Math.min(minimum(root.left), minimum(root.right))); 14 | } 15 | 16 | public static boolean isBST(BinaryTreeNode root) { 17 | /* Your class should be named Solution 18 | * Don't write main(). 19 | * Don't read input, it is passed as function argument. 20 | * Return output and don't print it. 21 | * Taking input and printing output is handled automatically. 22 | */ 23 | if (root == null) { 24 | return true; 25 | } 26 | int leftMax = maximum(root.left); 27 | int rightMin = minimum(root.right); 28 | if (root.data <= leftMax) { 29 | return false; 30 | } 31 | if (root.data > rightMin) { 32 | return false; 33 | } 34 | boolean isLeftBST = isBST(root.left); 35 | boolean isRightBST = isBST(root.right); 36 | if (isLeftBST && isRightBST) { 37 | return true; 38 | } else { 39 | return false; 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /BST/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){ 17 | /* Your class should be named Solution 18 | * Don't write main(). 19 | * Don't read input, it is passed as function argument. 20 | * Return output and don't print it. 21 | * Taking input and printing output is handled automatically. 22 | */ 23 | return sortedArrayToBST(arr, 0, arr.length); 24 | } 25 | public static BinaryTreeNode sortedArrayToBST(int[] arr, int startIndex, int endIndex) { 26 | if (startIndex >= endIndex) { 27 | return null; 28 | } 29 | int middle; 30 | if ((startIndex + endIndex) % 2 == 0) { 31 | middle = ((startIndex + endIndex) / 2) - 1; 32 | } else { 33 | middle = (startIndex + endIndex) / 2; 34 | } 35 | BinaryTreeNode root = new BinaryTreeNode(arr[middle]); 36 | root.left = sortedArrayToBST(arr, startIndex, middle); 37 | root.right = sortedArrayToBST(arr, middle + 1, endIndex); 38 | return root; 39 | } 40 | 41 | public static void preOrder(BinaryTreeNode root) { 42 | if (root == null) { 43 | return; 44 | } 45 | System.out.print(root.data + " "); 46 | preOrder(root.left); 47 | preOrder(root.right); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /BST/FindPathinBST.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | 3 | public class Solution { 4 | 5 | /* Binary Tree Node class 6 | * 7 | * class BinaryTreeNode { 8 | T data; 9 | BinaryTreeNode left; 10 | BinaryTreeNode right; 11 | 12 | public BinaryTreeNode(T data) { 13 | this.data = data; 14 | } 15 | } 16 | */ 17 | 18 | public static ArrayList findPath(BinaryTreeNode root, int data){ 19 | /* Your class should be named Solution 20 | * Don't write main(). 21 | * Don't read input, it is passed as function argument. 22 | * Return output and don't print it. 23 | * Taking input and printing output is handled automatically. 24 | */ 25 | if (root == null) { 26 | return null; 27 | } 28 | if (root.data == data) { 29 | ArrayList output = new ArrayList<>(); 30 | output.add(root.data); 31 | return output; 32 | } 33 | if (root.data > data) { 34 | ArrayList leftOutput = findPath(root.left, data); 35 | if (leftOutput != null) { 36 | leftOutput.add(root.data); 37 | return leftOutput; 38 | } else { 39 | return null; 40 | } 41 | } else { 42 | ArrayList rightOutput = findPath(root.right, data); 43 | if (rightOutput != null) { 44 | rightOutput.add(root.data); 45 | return rightOutput; 46 | } else { 47 | return null; 48 | } 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /BST/LCAofBST.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 int lcaInBST(BinaryTreeNode root , int a , int b){ 17 | // Write your code here 18 | if (root == null) { 19 | return -1; 20 | } 21 | if (root.data == a || root.data == b) { 22 | return root.data; 23 | } 24 | int c = lcaInBST(root.left, a, b); 25 | int d = lcaInBST(root.right, a, b); 26 | if (c == -1 && d != -1) { 27 | return d; 28 | } else if (c != -1 && d == -1) { 29 | return c; 30 | } else if (c == -1 && d == -1) { 31 | return -1; 32 | } else { 33 | return root.data; 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /BST/LCAofBT.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 int lcaBinaryTree(BinaryTreeNode root , int a, int b){ 17 | // Write your code here 18 | if (root == null) { 19 | return -1; 20 | } 21 | if (root.data == a || root.data == b) { 22 | return root.data; 23 | } 24 | int c = lcaBinaryTree(root.left, a, b); 25 | int d = lcaBinaryTree(root.right, a, b); 26 | if (c == -1 && d != -1) { 27 | return d; 28 | } else if (c != -1 && d == -1) { 29 | return c; 30 | } else if (c == -1 && d == -1) { 31 | return -1; 32 | } else { 33 | return root.data; 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /BST/NodeinaGivenRanga.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 void printNodeFromK1ToK2(BinaryTreeNode root,int k1,int k2){ 17 | if(root == null){ 18 | return ; 19 | } 20 | if(root.datak2){ 24 | printNodeFromK1ToK2(root.left,k1,k2); 25 | } 26 | else { 27 | printNodeFromK1ToK2(root.left,k1,k2); 28 | System.out.print(root.data+" "); 29 | printNodeFromK1ToK2(root.right,k1,k2); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /BST/PathSumRoottoLeaf.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | public class Solution { 3 | 4 | /* Binary Tree Node class 5 | * 6 | * class BinaryTreeNode { 7 | T data; 8 | BinaryTreeNode left; 9 | BinaryTreeNode right; 10 | 11 | public BinaryTreeNode(T data) { 12 | this.data = data; 13 | } 14 | } 15 | */ 16 | 17 | public static void rootToLeafPathsSumToK(BinaryTreeNode root,ArrayList path, int k) { 18 | // Write your code here 19 | if(root==null){ 20 | return; 21 | } 22 | k-=root.data; 23 | path.add(root.data); 24 | 25 | if(root.left==null && root.right== null){ 26 | if(k==0){ 27 | for(int i : path){ 28 | System.out.print(i+" "); 29 | } 30 | System.out.println(); 31 | } 32 | path.remove(path.size()-1); 33 | return; 34 | } 35 | rootToLeafPathsSumToK(root.left,path,k); 36 | rootToLeafPathsSumToK(root.right,path,k); 37 | path.remove(path.size()-1); 38 | } 39 | public static void rootToLeafPathsSumToK(BinaryTreeNode root, int k) { 40 | ArrayList path = new ArrayList(); 41 | rootToLeafPathsSumToK(root,path,k); 42 | } 43 | } 44 | 45 | -------------------------------------------------------------------------------- /BST/Replacesum.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 | public static int replaceSum(BinaryTreeNode root,int sum) { 16 | if(root==null){ 17 | return sum; 18 | } 19 | int rs = replaceSum(root.right,sum); 20 | root.data = root.data+rs; 21 | int ls = replaceSum(root.left,root.data); 22 | return ls; 23 | } 24 | public static void replaceWithLargerNodesSum(BinaryTreeNode root) { 25 | // Write your code here 26 | int sum = replaceSum(root,0); 27 | } 28 | } 29 | 30 | -------------------------------------------------------------------------------- /BST/SearchinBST.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 searchInBST(BinaryTreeNode root , int k){ 17 | /* Your class should be named Solution 18 | * Don't write main(). 19 | * Don't read input, it is passed as function argument. 20 | * Return output and don't print it. 21 | * Taking input and printing output is handled automatically. 22 | */ 23 | if(root==null){ 24 | return null; 25 | } 26 | if(root.data.equals(k)){ 27 | return root; 28 | } 29 | if(root.data > k){ 30 | return searchInBST(root.left,k); 31 | 32 | } else { 33 | return searchInBST(root.right , k); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /BST/insertDuplicateNode.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 void insertDuplicateNode(BinaryTreeNode root) { 17 | // Write your code here 18 | if(root==null){ 19 | return; 20 | } 21 | BinaryTreeNode newNode = new BinaryTreeNode(root.data); 22 | BinaryTreeNode rootLeft = root.left; 23 | root.left = newNode; 24 | newNode.left = rootLeft; 25 | insertDuplicateNode(rootLeft); 26 | insertDuplicateNode(root.right); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Better Solutions and Quick Sort/PalindromeString.java: -------------------------------------------------------------------------------- 1 | 2 | public class PalindromeSubstrings { 3 | 4 | 5 | public static int countPalindromeSubstrings(String s) { 6 | // Write your code here 7 | int n = s.length(); 8 | int count = 0; 9 | for (int i = 0; i < n; i++) { 10 | for (int j = i + 1; j <= n; j++) { 11 | if (j - i == 1) { 12 | count++; 13 | } else { 14 | if (isPalindrome(s, i, j)) { 15 | count++; 16 | } 17 | } 18 | } 19 | } 20 | return count; 21 | } 22 | 23 | public static boolean isPalindrome(String s, int x, int y) { 24 | int n = y - x; 25 | int mid = n / 2; 26 | for (int i = 0; i < mid; i++) { 27 | if (s.charAt(x++) != s.charAt(--y)) { 28 | return false; 29 | } 30 | } 31 | return true; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Better Solutions and Quick Sort/arrayEquilibrium.java: -------------------------------------------------------------------------------- 1 | public class ArrayEquilibrium{ 2 | public static void main(String[] args) { 3 | int[] arr = { -7, 1, 5, 2, -4, 3, 0 }; 4 | System.out.println(arrayEquilibrium(arr)); 5 | } 6 | 7 | //arr is the given array in which you need to find the equilibrium index 8 | public static int arrayEquilibrium(int[] arr){ 9 | //write your code here 10 | int[] countArr = new int[arr.length]; 11 | for (int i = 0; i < arr.length; i++) { 12 | countArr[i] = arr[i]; 13 | } 14 | for (int i = 1; i < arr.length; i++) { 15 | countArr[i] += countArr[i - 1]; 16 | } 17 | int fullSum = countArr[countArr.length - 1]; 18 | for (int i = 1; i < arr.length - 1; i++) { 19 | int leftSum = countArr[i - 1]; 20 | int rightSum = fullSum - countArr[i]; 21 | if (leftSum == rightSum) return i; 22 | } 23 | return -1; 24 | } 25 | } 26 | //time complexity n^2 27 | //space complaexity no extra space so n 28 | -------------------------------------------------------------------------------- /Binary Tree/CountLeafNodeBT.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 | public static void printLevelWise(BinaryTreeNode root) { 16 | /* Your class should be named Solution 17 | * Don't write main(). 18 | * Don't read input, it is passed as function argument. 19 | * Print output and don't return it. 20 | * Taking input is handled automatically. 21 | */ 22 | QueueUsingLL> pendingNodes = new QueueUsingLL<>(); 23 | pendingNodes.enqueue(root); 24 | 25 | while (!pendingNodes.isEmpty()) { 26 | BinaryTreeNode frontNode = null; 27 | try { 28 | frontNode = pendingNodes.dequeue(); 29 | } catch (QueueEmptyException e) { 30 | e.printStackTrace(); 31 | } 32 | System.out.print(frontNode.data + ":"); 33 | if (frontNode.left != null) { 34 | pendingNodes.enqueue(frontNode.left); 35 | System.out.print("L:" + frontNode.left.data + ","); 36 | } else { 37 | System.out.print("L:" + "-1" + ","); 38 | } 39 | if (frontNode.right != null) { 40 | pendingNodes.enqueue(frontNode.right); 41 | System.out.print("R:" + frontNode.right.data); 42 | System.out.println(); 43 | } else { 44 | System.out.print("R:" + "-1"); 45 | System.out.println(); 46 | } 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /Binary Tree/Findiameter.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 | public static int height(BinaryTreeNode root){ 16 | if(root == null){ 17 | return 0; 18 | } 19 | int leftHeight = height(root.left); 20 | int rightHeight = height(root.right); 21 | int myHeight = 1+Math.max(leftHeight,rightHeight); 22 | return myHeight; 23 | } 24 | 25 | public static int diameterOfBinaryTree(BinaryTreeNode root){ 26 | /* Your class should be named Solution 27 | * Don't write main(). 28 | * Don't read input, it is passed as function argument. 29 | * Return output and don't print it. 30 | * Taking input and printing output is handled automatically. 31 | */ 32 | if(root == null){ 33 | return 0; 34 | } 35 | int leftD = diameterOfBinaryTree(root.left); 36 | int rightD = diameterOfBinaryTree(root.right); 37 | 38 | int leftHeight = height(root.left); 39 | int rightHeight = height(root.right); 40 | int myValue = leftHeight+rightHeight+1; 41 | int finalD = Math.max(myValue, Math.max(leftD,rightD)); 42 | return finalD; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Binary Tree/FinfNode.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 boolean isNodePresent(BinaryTreeNode root,int x){ 17 | /* Your class should be named Solution 18 | * Don't write main(). 19 | * Don't read input, it is passed as function argument. 20 | * Return output and don't print it. 21 | * Taking input and printing output is handled automatically. 22 | */ 23 | boolean val=false; 24 | 25 | if(root.data==x){ 26 | return true; 27 | } 28 | //checking left side of tree is null or not 29 | if(root.left!=null){ 30 | val=isNodePresent(root.left,x); 31 | } 32 | //we need to only check if and only if we didn't found 33 | //in left side of tree otherwise value will be overridden 34 | if(val==false){ 35 | if(root.right!=null){ 36 | val=isNodePresent(root.right,x); 37 | } 38 | } 39 | return val; 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /Binary Tree/MaxDataNodeBT.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 maxDataNode(BinaryTreeNode root){ 17 | /* Your class should be named Solution 18 | * Don't write main(). 19 | * Don't read input, it is passed as function argument. 20 | * Return output and don't print it. 21 | * Taking input and printing output is handled automatically. 22 | */ 23 | if(root==null){ 24 | return null; 25 | } 26 | int max= root.data; 27 | BinaryTreeNode maxNode = root; 28 | BinaryTreeNode leftMax = maxDataNode(root.left); 29 | if(leftMax != null){ 30 | if(leftMax.data>max){ 31 | max=leftMax.data; 32 | maxNode=leftMax; 33 | } 34 | } 35 | BinaryTreeNode rightMax = maxDataNode(root.right); 36 | if(rightMax != null){ 37 | if(rightMax.data>max){ 38 | max=rightMax.data; 39 | maxNode=rightMax; 40 | } 41 | 42 | } 43 | return maxNode; 44 | } 45 | } 46 | 47 | -------------------------------------------------------------------------------- /Binary Tree/NodeWithoutSibling.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | 3 | 4 | public class Solution { 5 | 6 | /* Binary Tree Node class 7 | * 8 | * class BinaryTreeNode { 9 | T data; 10 | BinaryTreeNode left; 11 | BinaryTreeNode right; 12 | 13 | public BinaryTreeNode(T data) { 14 | this.data = data; 15 | } 16 | } 17 | */ 18 | 19 | public static void printNodesWithoutSibling(BinaryTreeNode root) { 20 | 21 | // Write your code here 22 | QueueUsingLL> pendingNodes = new QueueUsingLL<>(); 23 | pendingNodes.enqueue(root); 24 | while (!pendingNodes.isEmpty()) { 25 | BinaryTreeNode frontNode = null; 26 | int counter = pendingNodes.size(); 27 | for (int i = 0; i < counter; i++) { 28 | try { 29 | frontNode = pendingNodes.dequeue(); 30 | } catch (QueueEmptyException e) { 31 | e.printStackTrace(); 32 | } 33 | if (frontNode.left == null && frontNode.right != null) { 34 | System.out.println(frontNode.right.data + " "); 35 | } else if (frontNode.left != null && frontNode.right == null) { 36 | System.out.println(frontNode.left.data + " "); 37 | } 38 | if (frontNode.left != null) { 39 | pendingNodes.enqueue(frontNode.left); 40 | } 41 | if (frontNode.right != null) { 42 | pendingNodes.enqueue(frontNode.right); 43 | } 44 | } 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /Binary Tree/NodesGreaterThanXBT.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 int numNodesGreaterX(BinaryTreeNode root,int x){ 17 | /* Your class should be named Solution 18 | * Don't write main(). 19 | * Don't read input, it is passed as function argument. 20 | * Return output and don't print it. 21 | * Taking input and printing output is handled automatically. 22 | */ 23 | if(root==null){ 24 | return 0; 25 | } 26 | int val=0; 27 | 28 | if(root.data>x){ 29 | val = 1; 30 | } 31 | int left = numNodesGreaterX(root.left,x); 32 | int right = numNodesGreaterX(root.right,x); 33 | val = val+left+right; 34 | return val; 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /Binary Tree/PrintLevelWiseBT.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 | public static void printLevelWise(BinaryTreeNode root) { 16 | /* Your class should be named Solution 17 | * Don't write main(). 18 | * Don't read input, it is passed as function argument. 19 | * Print output and don't return it. 20 | * Taking input is handled automatically. 21 | */ 22 | QueueUsingLL> pendingNodes = new QueueUsingLL<>(); 23 | pendingNodes.enqueue(root); 24 | 25 | while (!pendingNodes.isEmpty()) { 26 | BinaryTreeNode frontNode = null; 27 | try { 28 | frontNode = pendingNodes.dequeue(); 29 | } catch (QueueEmptyException e) { 30 | e.printStackTrace(); 31 | } 32 | System.out.print(frontNode.data + ":"); 33 | if (frontNode.left != null) { 34 | pendingNodes.enqueue(frontNode.left); 35 | System.out.print("L:" + frontNode.left.data + ","); 36 | } else { 37 | System.out.print("L:" + "-1" + ","); 38 | } 39 | if (frontNode.right != null) { 40 | pendingNodes.enqueue(frontNode.right); 41 | System.out.print("R:" + frontNode.right.data); 42 | System.out.println(); 43 | } else { 44 | System.out.print("R:" + "-1"); 45 | System.out.println(); 46 | } 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /Binary Tree/RemoveLeafNode.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | 3 | 4 | public class Solution { 5 | 6 | /* Binary Tree Node class 7 | * 8 | * class BinaryTreeNode { 9 | T data; 10 | BinaryTreeNode left; 11 | BinaryTreeNode right; 12 | 13 | public BinaryTreeNode(T data) { 14 | this.data = data; 15 | } 16 | } 17 | */ 18 | 19 | public static BinaryTreeNode removeAllLeaves(BinaryTreeNode root){ 20 | 21 | // Write your code here 22 | if(root==null){ 23 | //if its null then we need to return some new node but not null 24 | //to identify the actual nodes where both left and right pointers are null 25 | return new BinaryTreeNode(-1); 26 | }else if(root.left==null&&root.right==null){//checking both pointers 27 | return null; 28 | } 29 | BinaryTreeNode value1; 30 | BinaryTreeNode value2; 31 | 32 | value1=removeAllLeaves(root.left); 33 | value2=removeAllLeaves(root.right); 34 | if(value1==null){//if value1 null then it means both its pointers are null but not a new node(not null values) 35 | root.left=null; 36 | } 37 | if(value2==null){//same as above 38 | root.right=null; 39 | } 40 | return root;//updated root 41 | } 42 | 43 | 44 | 45 | } 46 | -------------------------------------------------------------------------------- /Binary Tree/SumofAllNodesBt.java: -------------------------------------------------------------------------------- 1 | 2 | public class Solution { 3 | 4 | /* Binary Tree Node class 5 | * 6 | * class BinaryTreeNode { 7 | T data; 8 | BinaryTreeNode left; 9 | BinaryTreeNode right; 10 | 11 | public BinaryTreeNode(T data) { 12 | this.data = data; 13 | } 14 | } 15 | */ 16 | 17 | public static int sum(BinaryTreeNode root){ 18 | if(root==null){ 19 | return 0; 20 | } 21 | // Write your code here 22 | int nodes = root.data; 23 | int leftSum = sum(root.left); 24 | int rightSum = sum(root.right); 25 | return nodes+leftSum+rightSum; 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /Binary Tree/isBalanced.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 | public static int height(BinaryTreeNode root) { 16 | if (root == null) { 17 | return 0; 18 | } else if (root.left == null && root.right == null) { 19 | return 1; 20 | } 21 | int levelCount = 0; 22 | levelCount = Math.max(height(root.left), height(root.right)); 23 | return levelCount + 1; 24 | } 25 | 26 | public static boolean checkBalanced(BinaryTreeNode root) { 27 | if (root == null) { 28 | return true; 29 | } 30 | if (Math.abs(height(root.left) - height(root.right)) > 1) { 31 | return false; 32 | } 33 | boolean leftAnswer = checkBalanced(root.left); 34 | boolean rightAnswer = checkBalanced(root.right); 35 | return leftAnswer && rightAnswer; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Binary Tree/levelOrderTransveral.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.LinkedList; 3 | import java.util.Queue; 4 | 5 | public class Solution { 6 | 7 | /* Binary Tree Node class 8 | * 9 | * class BinaryTreeNode { 10 | T data; 11 | BinaryTreeNode left; 12 | BinaryTreeNode right; 13 | 14 | public BinaryTreeNode(T data) { 15 | this.data = data; 16 | } 17 | } 18 | */ 19 | 20 | public static void printLevelWise(BinaryTreeNode root){ 21 | 22 | // Write your code here 23 | 24 | Queue>pendingNodes=new LinkedList<>(); 25 | pendingNodes.add(root); 26 | pendingNodes.add(null); 27 | while(true){ 28 | BinaryTreeNode front=pendingNodes.remove(); 29 | if(front==null){ 30 | System.out.println(); 31 | if(pendingNodes.isEmpty()){ 32 | break; 33 | } 34 | pendingNodes.add(null); 35 | continue; 36 | } 37 | System.out.print(front.data+" "); 38 | if(front.left!=null){ 39 | pendingNodes.add(front.left); 40 | } 41 | if(front.right!= null){ 42 | pendingNodes.add(front.right); 43 | } 44 | } 45 | } 46 | 47 | 48 | 49 | } 50 | -------------------------------------------------------------------------------- /Binary Tree/sumofallnodesBT2.java: -------------------------------------------------------------------------------- 1 | 2 | public class Solution { 3 | 4 | /* Binary Tree Node class 5 | * 6 | * class BinaryTreeNode { 7 | T data; 8 | BinaryTreeNode left; 9 | BinaryTreeNode right; 10 | 11 | public BinaryTreeNode(T data) { 12 | this.data = data; 13 | } 14 | } 15 | */ 16 | 17 | public static int sum(BinaryTreeNode root){ 18 | 19 | // Write your code here 20 | if(root==null){ 21 | return 0; 22 | } 23 | // Write your code here 24 | int nodes = root.data; 25 | int leftSum = sum(root.left); 26 | int rightSum = sum(root.right); 27 | return nodes+leftSum+rightSum; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /DP 1 AND 2/BalancedBTSNum.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | 3 | public static int balancedTreesOfHeightH(int height){ 4 | /* Your class should be named Solution 5 | * Don't write main(). 6 | * Don't read input, it is passed as function argument. 7 | * Return output and don't print it. 8 | * Taking input and printing output is handled automatically. 9 | */ 10 | int modValue = (int)Math.pow(10, 9) + 7; 11 | 12 | 13 | if(height==0 || height==1) { 14 | 15 | return 1; 16 | } 17 | 18 | int x = balancedTreesOfHeightH(height-1); 19 | 20 | int y = balancedTreesOfHeightH(height-2); 21 | 22 | 23 | long ans1 = ((long)x*x)%(modValue); 24 | 25 | long ans2 = (2*x*(long)y)%(modValue); 26 | 27 | 28 | int res1 = (int) ans1; 29 | 30 | int res2 = (int) ans2; 31 | 32 | 33 | int ans = (res1 + res2)%(modValue); 34 | 35 | return ans; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /DP 1 AND 2/BinarySearch.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class BinarySearch{ 3 | public static int[] takeInput() { 4 | Scanner s = new Scanner(System.in); 5 | int n = s.nextInt(); 6 | int[] arr = new int[n]; 7 | for(int i = 0;i 0) { 21 | return storage[m][n]; 22 | 23 | } 24 | 25 | 26 | 27 | 28 | if (m== 0) { 29 | storage[m][n] = n; 30 | return storage[m][n]; 31 | 32 | } 33 | if (n== 0) { 34 | storage[m][n] = m; 35 | return storage[m][n]; 36 | 37 | } 38 | if (s1.charAt(0) == s2.charAt(0)) { 39 | storage[m][n] = editDistance(s1.substring(1), s2.substring(1), storage); 40 | return storage[m][n]; 41 | 42 | } else { 43 | int op1 = editDistance(s1, s2.substring(1),storage); 44 | int op2 = editDistance(s1.substring(1), s2,storage); 45 | int op3 = editDistance(s1.substring(1),s2.substring(1),storage); 46 | storage[m][n] = 1 + Math.min(op1, Math.min(op2, op3)); 47 | return storage[m][n]; 48 | } 49 | 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /DP/LCS.java: -------------------------------------------------------------------------------- 1 | 2 | public class Solution { 3 | 4 | 5 | public static int lcs(String s1, String s2){ 6 | 7 | /* Your class should be named Solution 8 | * Don't write main(). 9 | * Don't read input, it is passed as function argument. 10 | * Return output and don't print it. 11 | */ 12 | int[][] storage = new int[s1.length() + 1][s2.length() + 1]; 13 | return lcs(s1, s2, storage); 14 | 15 | } 16 | 17 | public static int lcs(String s1, String s2, int[][] storage) { 18 | 19 | int m = s1.length(); 20 | int n = s2.length(); 21 | 22 | if (storage[m][n] > 0) { 23 | return storage[m][n]; 24 | } 25 | if (m == 0 || n == 0) { 26 | storage[m][n] = 0; 27 | return storage[m][n]; 28 | } 29 | if (s1.charAt(0) == s2.charAt(0)) { 30 | storage[m][n] = 1 + lcs(s1.substring(1), s2.substring(1), storage); 31 | return storage[m][n]; 32 | 33 | } else { 34 | int op1 = lcs(s1.substring(1), s2, storage); 35 | int op2 = lcs(s1, s2.substring(1), storage); 36 | storage[m][n] = Math.max(op1, op2); 37 | return storage[m][n]; 38 | } 39 | 40 | } 41 | } 42 | 43 | 44 | -------------------------------------------------------------------------------- /DP/LIS1.java: -------------------------------------------------------------------------------- 1 | 2 | public class Solution { 3 | 4 | public static int lis(int arr[]) { 5 | 6 | /*Your class should be named Solution. 7 | *Don't write main(). 8 | *Don't take input, it is passed as function argument. 9 | *Don't print output. 10 | *Taking input and printing output is handled automatically. 11 | */ 12 | 13 | int[] output = new int[arr.length]; 14 | output[0] = 1; 15 | 16 | for (int i = 1; i < arr.length; i++) { 17 | int res = 1; 18 | for (int j = i - 1; j >= 0; j--) { 19 | if (arr[i] > arr[j]) { 20 | int tempRes = output[j] + 1; 21 | if (tempRes > res) 22 | res = tempRes; 23 | } 24 | } 25 | output[i] = res; 26 | } 27 | int max = 0; 28 | for (int v : output) { 29 | if (max < v) 30 | max = v; 31 | } 32 | return max; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /DP/MinSumPath.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | 3 | public static int minCostPath(int input[][]) { 4 | /* Your class should be named Solution 5 | * Don't write main(). 6 | * Don't read input, it is passed as function argument. 7 | * Return output and don't print it. 8 | * Taking input and printing output is handled automatically. 9 | */ 10 | int m = input.length; 11 | 12 | int n = input[0].length; 13 | 14 | 15 | int[][] dp = new int[m+1][n+1]; 16 | 17 | for(int i=0;i=0;i--){ 14 | for(int j = 1 ;j<=value;j++){ 15 | int count1 = 0; 16 | if(i+1 <= n-1){ 17 | count1 = way[i+1][j]; 18 | } 19 | int count2 = 0; 20 | if(j-denominations[i]>=0){ 21 | count2 = way[i][j-denominations[i]]; 22 | } 23 | way[i][j] = count1+count2; 24 | } 25 | } 26 | return way[0][value]; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /DP/allpossibleways.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | //value,i 3 | public static int canSumTo(int k,int x,int n){ 4 | int rest = (int) (x-Math.pow(k,n)); 5 | if(rest==0){ 6 | return 1; 7 | } else if(rest < 0){ 8 | return 0; 9 | } 10 | int r = 0; 11 | for(int i = k+1;i= 1) { 16 | dp[i] = Math.max(dp[i], dp[i - x]^1); 17 | } 18 | if (i - y >= 1) { 19 | dp[i] = Math.max(dp[i], dp[i - y]^1); 20 | 21 | } 22 | } 23 | 24 | } 25 | if (dp[n] != 0) { 26 | return "Beerus"; 27 | } 28 | else { 29 | return "Whis"; 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /DP/knapsack01.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | 3 | public static int knapsack(int[] weight,int value[],int maxWeight, int n){ 4 | /* Your class should be named Solution. 5 | * Don't write main() function. 6 | * Don't read input, it is passed as function argument. 7 | * Return output and don't print it. 8 | * Taking input and printing output is handled automatically. 9 | */ 10 | if(maxWeight==0||n==0){ 11 | return 0; 12 | } 13 | if(weight[n-1]>maxWeight){ 14 | return knapsack(weight,value,maxWeight,n-1); 15 | } else{ 16 | return Math.max(value[n-1]+knapsack(weight,value,maxWeight-weight[n-1],n-1), 17 | knapsack(weight,value,maxWeight,n-1)); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /DP/knapsack01d.java: -------------------------------------------------------------------------------- 1 | 2 | public class Solution { 3 | 4 | 5 | 6 | public static int knapsack(int[] weight,int value[],int maxWeight){ 7 | 8 | /* Your class should be named Solution. 9 | * Don't write main() function. 10 | * Don't read input, it is passed as function argument. 11 | * Return output and don't print it. 12 | * Taking input and printing output is handled automatically. 13 | */ 14 | int storagePrev[] = new int[maxWeight+1]; 15 | int storageCurrent[] = new int [maxWeight+1]; 16 | for(int i=1;iw){ 19 | storageCurrent[w] = storagePrev[w]; 20 | } 21 | else { 22 | storageCurrent[w]= Math.max(storagePrev[w - weight[i-1]]+ value[i-1],storagePrev[w]); 23 | } 24 | } 25 | storagePrev = storageCurrent ; 26 | storageCurrent = new int[maxWeight+1]; 27 | 28 | } 29 | return storagePrev[maxWeight]; 30 | } 31 | 32 | } 33 | 34 | -------------------------------------------------------------------------------- /DP/shortestsub.java: -------------------------------------------------------------------------------- 1 | 2 | public class solution { 3 | static int INF = 1000000000; 4 | int [][] dp; 5 | int [][] next; 6 | public int solve(String S,String V){ 7 | /* Your class should be named Solution 8 | * Don't write main(). 9 | * Don't read input. Input is passed as function argument. 10 | * Return output and don't print it. 11 | * Taking input and printing output is handled automatically. 12 | */ 13 | int n = S.length(), m = V.length() ,i,j,prev; 14 | dp = new int[n+1][m+1]; 15 | next = new int[n+1][m+1]; 16 | for(i=0;im2 && m3>m2) { 36 | System.out.println(c+ " "+ (m1+m3)/2); 37 | } 38 | if(m2>m1 && m3>m1) { 39 | System.out.println(c+ " "+ (m3+m2)/2); 40 | } 41 | if(m2>m3 && m1>m3) { 42 | System.out.println(c+ " "+(m1+m2)/2); 43 | } 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /Fundamental Programming 1/Question 2.txt: -------------------------------------------------------------------------------- 1 | //Find Character Case 2 | Write a program to determine whether the entered character is in uppercase or lowercase, or is an invalid character. 3 | Print 4 | 1 for uppercase 5 | 0 for lowercase 6 | -1 for any other character (special characters or others) 7 | Input format : 8 | Single Character 9 | Output format : 10 | 1 or 0 or -1 11 | Sample Input 1 : 12 | v 13 | Sample Output 1 : 14 | 0 15 | Sample Input 2 : 16 | V 17 | Sample Output 2 : 18 | 1 19 | Sample Input 3 : 20 | # 21 | Sample Output 3 : 22 | -1// 23 | 24 | import java.util.Scanner; 25 | public class Main { 26 | 27 | public static void main(String[] args) { 28 | // Write your code here 29 | Scanner s = new Scanner(System.in); 30 | char c = s.next().charAt(0); 31 | 32 | if (c >= 'a' && c <= 'z') { 33 | System.out.println(0);} 34 | else if (c >= 'A' && c <= 'Z') { 35 | System.out.println(1); 36 | } 37 | else { 38 | System.out.println(-1); 39 | } 40 | } 41 | } 42 | 43 | 44 | -------------------------------------------------------------------------------- /Fundamental Programming 1/Question 3.txt: -------------------------------------------------------------------------------- 1 | //Sum of Even Numbers till N 2 | Given a number N, print sum of all even numbers from 1 to N. 3 | Input Format : 4 | Integer N 5 | Output Format : 6 | Required Sum 7 | Sample Input 1 : 8 | 6 9 | Sample Output 1 : 10 | 12// 11 | 12 | import java.util.Scanner; 13 | public class Solution { 14 | public static void main(String[] args) { 15 | int sum=0; 16 | Scanner s = new Scanner(System.in); 17 | int N = s.nextInt(); 18 | for(int i=0; i<=N; i=i+2){ 19 | sum = sum + i; 20 | } 21 | System.out.println(sum); 22 | } 23 | } 24 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /Fundamental Programming 1/Question 4.txt: -------------------------------------------------------------------------------- 1 | //Celsius to Fahrenheit Table 2 | Given three values - Start Fahrenheit Value (S), End Fahrenheit value (E) and 3 | Step Size (W), you need to convert all Fahrenheit values from Start to End at the gap of W, 4 | into their corresponding Celsius values and print the table. 5 | Input Format : 6 | 3 integers - S, E and W respectively 7 | Output Format : 8 | Fahrenheit to Celsius conversion table. One line for every Fahrenheit and 9 | corresponding Celsius value. On Fahrenheit value and its corresponding 10 | Celsius value should be separate by tab ("\t")// 11 | 12 | import java.util.Scanner; 13 | public class Solution { 14 | public static void main(String[] args) { 15 | Scanner s= new Scanner(System.in); 16 | int S = s.nextInt(); 17 | int E = s.nextInt(); 18 | int W = s.nextInt(); 19 | int cel; 20 | while (S<=E) { 21 | cel = 5 * (S-32)/9; 22 | System.out.print(S); 23 | System.out.print(" "); 24 | System.out.println(cel); 25 | S=S+W; 26 | } 27 | } 28 | 29 | } 30 | 31 | 32 | -------------------------------------------------------------------------------- /Fundamental Programming 1/Question 5.txt: -------------------------------------------------------------------------------- 1 | //Number Pattern 1 2 | Print the following pattern 3 | Pattern for N = 4 4 | 1 5 | 23 6 | 345 7 | 4567 8 | Input Format : 9 | N (Total no. of rows) 10 | Output Format : 11 | Pattern in N lines 12 | Sample Input 1 : 13 | 3 14 | Sample Output 1 : 15 | 1 16 | 23 17 | 345// 18 | 19 | 20 | public class Solution { 21 | public static void main(String[] args) { 22 | int i,j,n,k; 23 | Scanner s = new Scanner(System.in); 24 | n=s.nextInt(); 25 | for(i=1;i<=n;i++) 26 | { 27 | k=i; 28 | for(j=1;j<=i;j++,k++) 29 | { 30 | 31 | System.out.print(k); 32 | } 33 | System.out.println(""); 34 | } 35 | } 36 | 37 | } 38 | 39 | 40 | -------------------------------------------------------------------------------- /Fundamental Programming 1/Question 6.txt: -------------------------------------------------------------------------------- 1 | //Number Pattern 2 2 | Print the following pattern 3 | Pattern for N = 4 4 | 1 5 | 23 6 | 345 7 | 4567 8 | Input Format : 9 | N (Total no. of rows) 10 | Output Format : 11 | Pattern in N lines 12 | Sample Input : 13 | 5 14 | Sample Output : 15 | 1 16 | 23 17 | 345 18 | 4567 19 | 56789// 20 | 21 | import java.util.Scanner; 22 | public class Solution { 23 | public static void main(String[] args) { 24 | Scanner scanner = new Scanner(System.in); 25 | 26 | int n = scanner.nextInt(); 27 | int i = 1; 28 | while (i <= n) { 29 | int space = 1; 30 | while (space <= (n - i)) { 31 | System.out.print(" "); 32 | space++; 33 | } 34 | int j = 1, val = i; 35 | while (j <= i) { 36 | System.out.print(val); 37 | j++; 38 | val++; 39 | } 40 | System.out.println(); 41 | i++; 42 | } 43 | } 44 | } 45 | 46 | 47 | -------------------------------------------------------------------------------- /Fundamental Programming 1/Question 7.txt: -------------------------------------------------------------------------------- 1 | //Star Pattern 2 | Print the following pattern 3 | Pattern for N = 4 4 | * 5 | *** 6 | ***** 7 | ******* 8 | Input Format : 9 | N (Total no. of rows) 10 | Output Format : 11 | Pattern in N lines 12 | Sample Input 1 : 13 | 3 14 | Sample Output 1 : 15 | * 16 | *** 17 | *****// 18 | 19 | import java.util.Scanner; 20 | public class Solution { 21 | public static void main(String[] args) { 22 | int i,j,n; 23 | Scanner s = new Scanner(System.in); 24 | n = s.nextInt(); 25 | 26 | for(i=1;i<=n;i++) 27 | { 28 | for(j=i;j queue = new LinkedList<>(); 26 | queue.add(start); 27 | visited[start] = true; 28 | while (!queue.isEmpty()) { 29 | int front = queue.poll(); 30 | System.out.print(front+" "); 31 | for (int i = 0; i < V; i++) { 32 | if (edges[front][i] == 1 && visited[i]!= true) { 33 | queue.add(i); 34 | visited[i] = true; 35 | } 36 | } 37 | } 38 | 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Graphs/BinarySearch.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class BinarySearch{ 3 | public static int[] takeInput() { 4 | Scanner s = new Scanner(System.in); 5 | int n = s.nextInt(); 6 | int[] arr = new int[n]; 7 | for(int i = 0;i map = new LinkedHashMap<>(); 12 | for (int e : arr) { 13 | if (map.containsKey(e)) { 14 | int tempCount = map.get(e); 15 | map.put(e, tempCount + 1); 16 | } else { 17 | map.put(e, 1); 18 | } 19 | } 20 | int max = -1; 21 | int maxInt = arr[0]; 22 | for (int k : map.keySet()) { 23 | int c = map.get(k); 24 | if (c > max) { 25 | max = c; 26 | maxInt = k; 27 | } 28 | } 29 | return maxInt; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /HashMaps/LIS.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.*; 3 | public class solution { 4 | 5 | public static ArrayList longestSubsequence(int[] arr){ 6 | // Write your code here 7 | HashMap map = new LinkedHashMap<>(); 8 | ArrayList result = new ArrayList<>(); 9 | for (int v : arr) { 10 | map.put(v, false); 11 | } 12 | for (int e : map.keySet()) { 13 | if (!map.get(e)) { 14 | ArrayList tempList = new ArrayList<>(); 15 | tempList.add(e); 16 | map.put(e, true); 17 | for (int i = 1; map.containsKey(e + i); i++) { 18 | tempList.add(e + i); 19 | map.put(e + i, true); 20 | } 21 | if (result.size() < tempList.size()) { 22 | result = tempList; 23 | } 24 | } 25 | } 26 | return result; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /HashMaps/LongestSubsetWithZeroSum.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | /* 4 | - You are given an integer array containing positive and negative numbers. 5 | - Your task is to find out the length of the longest continuous subset of this array whose elements add upto zero. 6 | 7 | */ 8 | //sum repeat 9 | public class solution 10 | { 11 | public static int lengthOfLongestSubsetWithZeroSum(ArrayList arr) 12 | { 13 | // write your code here. 14 | HashMap map = new HashMap<>(); 15 | int start = -1, end = -1; 16 | int sum[] = new int [arr.size()]; 17 | sum[0]= arr.get(0); 18 | map.put(sum[0],0); 19 | for(int i = 1 ; ii && (map.get(sum[i])-i > end - start || start == -1)){ 25 | start = i; 26 | end = map.get(sum[i]); 27 | } 28 | } 29 | if(map.get(0)!=null&&(start==-1||map.get(0)> end - start ||(map.get(0)== end - start 30 | -1 && start >0))){ 31 | start = -1; 32 | end = map.get(0); 33 | } 34 | return (end - start); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /HashMaps/extractuniqueChar.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class solution { 3 | 4 | public static String uniqueChar(String str){ 5 | // Write your code here 6 | Map mp=new HashMap<>(); 7 | String uniqueString=""; 8 | for(int i=0;i map1 = new HashMap(); 12 | HashMap map2 = new HashMap(); 13 | 14 | for (int e : arr1) { 15 | if (map1.containsKey(e)) { 16 | int temp = map1.get(e); 17 | map1.put(e, temp + 1); 18 | } else { 19 | map1.put(e, 1); 20 | } 21 | } 22 | 23 | for (int e : arr2) { 24 | if (map2.containsKey(e)) { 25 | int temp = map2.get(e); 26 | map2.put(e, temp + 1); 27 | } else { 28 | map2.put(e, 1); 29 | } 30 | } 31 | 32 | for (int k : map1.keySet()) { 33 | if (map2.containsKey(k)) { 34 | int v1 = map1.get(k); 35 | int v2 = map2.get(k); 36 | int counter = v1 <= v2 ? v1 : v2; 37 | for (int i = 0; i < counter; i++) { 38 | System.out.println(k); 39 | } 40 | } 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /LL1/DeleteNodell.java: -------------------------------------------------------------------------------- 1 | /*************** 2 | * Following is the Node class already written 3 | class LinkedListNode { 4 | T data; 5 | LinkedListNode next; 6 | 7 | public Node(T data) { 8 | this.data = data; 9 | } 10 | } 11 | ***************/ 12 | 13 | public class Solution { 14 | 15 | public static LinkedListNode deleteIthNode(LinkedListNode head, int i){ 16 | /* Your class should be named Solution 17 | * Don't write main(). 18 | * Don't read input, it is passed as function argument. 19 | * Return output and don't print it. 20 | * Taking input and printing output is handled automatically. 21 | */ 22 | if(head==null){ 23 | return null; 24 | } 25 | 26 | LinkedListNode temp=head; 27 | int actual_length=0; 28 | while(temp!=null){ 29 | temp=temp.next; 30 | actual_length++; 31 | } 32 | 33 | if(i>actual_length-1){ 34 | return head; 35 | } 36 | temp=head; 37 | if(i==0){ 38 | head=temp.next; 39 | return head; 40 | } 41 | LinkedListNode prev=null; 42 | int len=0; 43 | 44 | while(len { 4 | T data; 5 | LinkedListNode next; 6 | 7 | public LinkedListNode(T data) { 8 | this.data = data; 9 | } 10 | } 11 | ***************/ 12 | 13 | public class Solution { 14 | 15 | public static int length(LinkedListNode head){ 16 | /* Your class should be named Solution 17 | * Don't write main(). 18 | * Don't read input, it is passed as function argument. 19 | * Return output and don't print it. 20 | * Taking input and printing output is handled automatically. 21 | */ 22 | int count = 0; 23 | while (head!= null){ 24 | count++; 25 | head = head.next; 26 | 27 | } 28 | return count; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /LL1/PrintithNode.java: -------------------------------------------------------------------------------- 1 | /*************** 2 | * Following is the Node class already written 3 | class LinkedListNode { 4 | T data; 5 | LinkedListNode next; 6 | 7 | public Node(T data) { 8 | this.data = data; 9 | } 10 | } 11 | ***************/ 12 | 13 | public class Solution { 14 | 15 | public static void printIth(LinkedListNode head, int i){ 16 | /* Your class should be named Solution 17 | * Don't write main(). 18 | * Don't read input, it is passed as function argument. 19 | * Print output and don't return it. 20 | * Taking input is handled automatically. 21 | */ 22 | LinkedListNode temp = head; 23 | int count = 0; 24 | while (temp != null) { 25 | if (count == i) { 26 | System.out.println(temp.data); 27 | return; 28 | } else { 29 | count++; 30 | temp = temp.next; 31 | if (count == i) { 32 | System.out.println(temp.data); 33 | return; 34 | } 35 | } 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /LL1/deletenodereLLc.java: -------------------------------------------------------------------------------- 1 | /*************** 2 | * Following is the Node class already written 3 | class LinkedListNode { 4 | T data; 5 | LinkedListNode next; 6 | 7 | public Node(T data) { 8 | this.data = data; 9 | } 10 | } 11 | ***************/ 12 | 13 | public class Solution { 14 | 15 | public static LinkedListNode deleteIthNodeRec(LinkedListNode head, int i){ 16 | /* Your class should be named Solution 17 | * Don't write main(). 18 | * Don't read input, it is passed as function argument. 19 | * Return output and don't print it. 20 | * Taking input and printing output is handled automatically. 21 | */ 22 | if (i == 0) { 23 | return head.next; 24 | } 25 | if (head.next == null) { 26 | return head; 27 | } 28 | head.next = deleteIthNodeRec(head.next, i - 1); 29 | return head; 30 | 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /LL1/insertNode.java: -------------------------------------------------------------------------------- 1 | /*************** 2 | * Following is the Node class already written 3 | class Node { 4 | T data; 5 | Node next; 6 | 7 | public Node(T data) { 8 | this.data = data; 9 | } 10 | } 11 | ***************/ 12 | 13 | public class Solution { 14 | public static int length(Node head){ 15 | int ct = 0; 16 | while(head!=null){ 17 | ct++; 18 | head=head.next; 19 | } 20 | return ct; 21 | } 22 | public static Node insert(Node head, int data, int pos){ 23 | /* Your class should be named Solution 24 | * Don't write main(). 25 | * Don't read input, it is passed as function argument. 26 | * Return output and don't print it. 27 | * Taking input and printing output is handled automatically. 28 | */ 29 | int len = length(head); 30 | if(pos>len||pos<0){ 31 | return head; 32 | } 33 | int count = 0; 34 | Node curr = head; 35 | Node prev = null; 36 | while(countnewmode=new Node<>(data); 42 | if(prev!=null){ 43 | prev.next=newmode; 44 | newmode.next=curr; 45 | return head; 46 | } 47 | else { 48 | newmode.next=head; 49 | return newmode; 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /LL1/insertnoderecLL.java: -------------------------------------------------------------------------------- 1 | /*************** 2 | * Following is the Node class already written 3 | class LinkedListNode { 4 | T data; 5 | LinkedListNode next; 6 | 7 | public Node(T data) { 8 | this.data = data; 9 | } 10 | } 11 | ***************/ 12 | 13 | public class Solution { 14 | 15 | public static LinkedListNode insertR(LinkedListNode head, int data, int pos){ 16 | /* Your class should be named Solution 17 | * Don't write main(). 18 | * Don't read input, it is passed as function argument. 19 | * Return output and don't print it. 20 | * Taking input and printing output is handled automatically. 21 | */ 22 | if (pos == 0) { 23 | LinkedListNode newNode = new LinkedListNode(data); 24 | newNode.next = head; 25 | return newNode; 26 | } 27 | if (head == null) { 28 | return head; 29 | } 30 | head.next = insertR(head.next, data, pos - 1); 31 | return head; 32 | 33 | 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /LL1/lengthRecLL.java: -------------------------------------------------------------------------------- 1 | /*************** 2 | * Following is the Node class already written 3 | class LinkedListNode { 4 | T data; 5 | LinkedListNode next; 6 | 7 | public Node(T data) { 8 | this.data = data; 9 | } 10 | } 11 | ***************/ 12 | 13 | public class Solution { 14 | 15 | public static int length(LinkedListNode head){ 16 | /* Your class should be named Solution 17 | * Don't write main(). 18 | * Don't read input, it is passed as function argument. 19 | * Return output and don't print it. 20 | * Taking input and printing output is handled automatically. 21 | */ 22 | LinkedListNode temp = head; 23 | int count = 1; 24 | while (temp.next != null) { 25 | count++; 26 | temp = temp.next; 27 | } 28 | return count; 29 | 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Lecture 3 Fundamental Programming/Question 1.txt: -------------------------------------------------------------------------------- 1 | All Prime Numbers 2 | Given an integer N, print all the prime numbers that lies in between 2 to N (both inclusive). 3 | Print the prime numbers in different lines. 4 | Input Format : 5 | Integer N 6 | Output Format : 7 | Prime number in different lines 8 | Constraints : 9 | 1 <= N <= 100 10 | Sample Input : 11 | 9 12 | Sample Output : 13 | 2 14 | 3 15 | 5 16 | 7 17 | 18 | import java.util.Scanner; 19 | public class Solution { 20 | public static boolean checkPrime(int x) { 21 | boolean Prime = true; 22 | for (int i = 2; i < x; i++) { 23 | if (x % i == 0) { 24 | Prime = false; 25 | break; 26 | } 27 | } 28 | return Prime; 29 | } 30 | public static void main(String[] args) { 31 | 32 | /* Your class should be named Solution. 33 | * Read input as specified in the question. 34 | * Print output as specified in the question. 35 | */ 36 | Scanner s = new Scanner(System.in); 37 | int N = s.nextInt(); 38 | boolean Prime; 39 | for (int i = 2; i <= N; i++) { 40 | Prime = checkPrime(i); 41 | if (Prime == true) { 42 | System.out.println(i); 43 | } 44 | } 45 | } } 46 | 47 | 48 | -------------------------------------------------------------------------------- /Lecture 3 Fundamental Programming/Question 2.txt: -------------------------------------------------------------------------------- 1 | Nth Fibonacci Number 2 | Nth term of fibonacci series F(n) is calculated using following formula - 3 | F(n) = F(n-1) + F(n-2), 4 | Provided N you have to find out the Nth Fibonacci Number. Also F(1) = F(2) = 1. 5 | Input Format : 6 | Integer n 7 | Constraints: 8 | Time Limit: 1 second 9 | Output Format : 10 | Nth Fibonacci term i.e. F(n) 11 | Sample Input : 12 | 4 13 | Sample Output : 14 | 3 15 | 16 | import java.util.Scanner; 17 | public class Solution { 18 | public static int nFibonacci(int n){ 19 | int y; 20 | if(n == 1) 21 | return 1; 22 | if(n == 2) 23 | return 1; 24 | y = nFibonacci(n-1) + nFibonacci(n-2); 25 | return y; 26 | } 27 | public static void main(String[] args) { 28 | Scanner s = new Scanner(System.in); 29 | int n = s.nextInt(); 30 | int x = nFibonacci(n); 31 | System.out.print(x); 32 | } 33 | } 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /Lecture 3 Fundamental Programming/Question 3.txt: -------------------------------------------------------------------------------- 1 | Trailing Zeros in Factorial 2 | Given an integer n, find the number of trailing 0s in its factorial. Do this without actually finding the n!, 3 | as the integer given can be large and its factorial can exceed the integer limits. 4 | Input format : 5 | Integer n 6 | Output Format : 7 | Count of trailing 0s 8 | Constraints : 9 | 1 <= n <= 0^11 10 | Sample Input 11 | 50 12 | Sample Output 13 | 12 14 | 15 | 16 | import java.util.Scanner; 17 | public class Main { 18 | public static int MaxPower(int n ,int m) 19 | { int r,q,s = 0; 20 | while(n!=0) 21 | { 22 | q = n / m; 23 | r = n % m; 24 | s+=q; 25 | n = q; 26 | } 27 | return s; 28 | } 29 | public static void main(String[] args) { 30 | // Write your code here 31 | Scanner s = new Scanner(System.in); 32 | int n = s.nextInt(); 33 | int s2 = MaxPower(n,2); 34 | int s5 = MaxPower(n,5); 35 | if (s2 < s5) 36 | System.out.print(s2); 37 | else 38 | System.out.print(s5); 39 | } 40 | } 41 | 42 | 43 | -------------------------------------------------------------------------------- /Lecture 3 Fundamental Programming/Question 4.txt: -------------------------------------------------------------------------------- 1 | Arrow Pattern 2 | 3 | Print the following pattern for the given number of rows. 4 | Assume N is always odd. 5 | Note : There is space after every star. 6 | Pattern for N = 7 7 | * 8 | * * 9 | * * * 10 | * * * * 11 | * * * 12 | * * 13 | * 14 | Input format : 15 | Integer N (Total no. of rows) 16 | Output format : 17 | Pattern in N lines 18 | Sample Input : 19 | 11 20 | Sample Output : 21 | * 22 | * * 23 | * * * 24 | * * * * 25 | * * * * * 26 | * * * * * * 27 | * * * * * 28 | * * * * 29 | * * * 30 | * * 31 | * 32 | 33 | import java.util.Scanner; 34 | public class Solution { 35 | public static void main(String[] args) { 36 | Scanner s = new Scanner(System.in); 37 | int a = s.nextInt(); 38 | int i=1; 39 | int l=(a/2)+1; 40 | int nex=a-l; 41 | while(i<=l) 42 | { 43 | int j=1; 44 | while(j=1) 60 | { 61 | int j=1; 62 | while(j 0;i--) 27 | { 28 | for(int j = i;j <= c1;j++) 29 | { 30 | System.out.print(j); 31 | } 32 | for(int k = n-1;k > c2;k--) 33 | { 34 | System.out.print(n); 35 | } 36 | c2++; 37 | System.out.println(); 38 | } 39 | } 40 | } 41 | 42 | 43 | -------------------------------------------------------------------------------- /Lecture 3 Fundamental Programming/Question 6.txt: -------------------------------------------------------------------------------- 1 | Fahrenheit Table 2 | Given three values - Start Fahrenheit Value (S), End Fahrenheit value (E) and Step Size (W), 3 | you need to convert all Fahrenheit values from Start to End at the gap of W, 4 | into their corresponding Celsius values and print the table. 5 | Input Format : 6 | 3 integers - S, E and W respectively 7 | Output Format : 8 | Fahrenheit to Celsius conversion table. One line for every Fahrenheit and Celsius Fahrenheit value. 9 | Fahrenheit value and its corresponding Celsius value should be separate by tab ("\t") 10 | Sample Input : 11 | 0 12 | 100 13 | 20 14 | Sample Output : 15 | 0 -17 16 | 20 -6 17 | 40 4 18 | 60 15 19 | 80 26 20 | 100 37 21 | 22 | 23 | import java.util.Scanner; 24 | public class Solution { 25 | public static void printFahrenheitTable(int start, int end, int step) { 26 | while(start<=end) { 27 | int C=((start-32)*(5))/9; 28 | System.out.println(start+"\t"+C); 29 | start=start+step; 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Lecture 3 Fundamental Programming/Question 7.txt: -------------------------------------------------------------------------------- 1 | Fibonacci Number 2 | Given a number N, figure out if it is a member of fibonacci series or not. 3 | Return true if the number is member of fibonacci series else false. 4 | Fibonacci Series is defined by the recurrence 5 | F(n) = F(n-1) + F(n-2) 6 | Input Format : 7 | Integer N 8 | Output Format : 9 | true or false 10 | Sample Input 1 : 11 | 5 12 | Sample Output 1 : 13 | true 14 | Sample Input 2 : 15 | 14 16 | Sample Output 2 : 17 | false 18 | 19 | import java.util.Scanner; 20 | public class Solution { 21 | public static boolean pSqaure(int x){ 22 | int y = (int)Math.sqrt(x); 23 | if (y*y == x) 24 | return true; 25 | else 26 | return false; 27 | } 28 | public static boolean checkMember(int n){ 29 | if(pSqaure(5*n*n-4) || pSqaure(5*n*n+4)) 30 | return true; 31 | else 32 | return false; 33 | } 34 | public static void main(String args[]) { 35 | 36 | Scanner s = new Scanner(System.in); 37 | int n = s.nextInt(); 38 | boolean checkMember = checkMember(n); 39 | System.out.print(checkMember); 40 | } 41 | } 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /Linked List 2/AppendLastNtoFirst.java: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | class LinkedListNode { 4 | public T data; 5 | public LinkedListNode next; 6 | 7 | public LinkedListNode(T data) { 8 | this.setData(data); 9 | this.next = null; 10 | } 11 | 12 | public T getData() { 13 | return data; 14 | } 15 | 16 | public void setData(T data) { 17 | this.data = data; 18 | } 19 | 20 | } 21 | * */ 22 | public class Solution { 23 | public static int length(LinkedListNode head) { 24 | LinkedListNode temp = head; 25 | int count = 1; 26 | while (temp.next != null) { 27 | count++; 28 | temp = temp.next; 29 | } 30 | return count; 31 | } 32 | public static LinkedListNode append(LinkedListNode root, int n) { 33 | int value = length(root) - n; 34 | LinkedListNode temp = root; 35 | while (temp.next != null) { 36 | temp = temp.next; 37 | } 38 | temp.next = root; 39 | int count = 0; 40 | while (count < value) { 41 | temp = temp.next; 42 | count++; 43 | } 44 | LinkedListNode head = temp.next; 45 | temp.next = null; 46 | return head; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /Linked List 2/BubbleSortLL.java: -------------------------------------------------------------------------------- 1 | 2 | public class Solution { 3 | public static int length(LinkedListNode head) { 4 | LinkedListNode temp = head; 5 | int count = 1; 6 | while (temp.next != null) { 7 | count++; 8 | temp = temp.next; 9 | } 10 | return count; 11 | } 12 | 13 | 14 | public static LinkedListNode bubbleSort(LinkedListNode head ) 15 | { 16 | //Write your code here 17 | int n = length(head); 18 | for (int i = 0; i < n - 1; i++) { 19 | LinkedListNode prev = null; 20 | LinkedListNode curr = head; 21 | for (int j = 0; j < n - i - 1; j++) { 22 | if (curr.data <= curr.next.data) { 23 | prev = curr; curr = curr.next; 24 | } else { 25 | if (prev == null) { 26 | LinkedListNode fwd = curr.next; 27 | head = head.next; 28 | curr.next = fwd.next; 29 | fwd.next = curr; 30 | prev = fwd; 31 | } else { 32 | LinkedListNode fwd = curr.next; 33 | prev.next = fwd; 34 | curr.next = fwd.next; 35 | fwd.next = curr; 36 | prev = fwd; 37 | } 38 | } 39 | } 40 | } 41 | return head; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Linked List 2/DeleteEveryNnode.java: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | class LinkedListNode { 4 | public T data; 5 | public LinkedListNode next; 6 | 7 | public LinkedListNode(T data) { 8 | this.setData(data); 9 | this.next = null; 10 | } 11 | 12 | public T getData() { 13 | return data; 14 | } 15 | 16 | public void setData(T data) { 17 | this.data = data; 18 | } 19 | 20 | } 21 | * */ 22 | public class solution { 23 | public static LinkedListNode skipMdeleteN(LinkedListNode head, int M, int N) { 24 | LinkedListNode trav = head; 25 | 26 | while (trav != null) { 27 | for (int i = 0; i < M - 1; i++) { 28 | if (trav == null) 29 | break; 30 | trav = trav.next; 31 | } 32 | LinkedListNode prevM = trav; 33 | 34 | if (prevM == null) 35 | return head; 36 | 37 | for (int i = 0; i < N; i++) { 38 | if (trav == null) 39 | break; 40 | trav = trav.next; 41 | } 42 | 43 | if (trav != null) 44 | prevM.next = trav.next; 45 | else 46 | prevM.next = null; 47 | 48 | if (prevM.next == null) 49 | return head; 50 | trav = trav.next; 51 | } 52 | 53 | return head; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /Linked List 2/FindanElement.java: -------------------------------------------------------------------------------- 1 | /*************** 2 | * Following is the Node class already written 3 | class LinkedListNode { 4 | T data; 5 | LinkedListNode next; 6 | 7 | public Node(T data) { 8 | this.data = data; 9 | } 10 | } 11 | ***************/ 12 | 13 | public class Solution { 14 | public static int indexOfNIter(LinkedListNode head, int n) { 15 | /* Your class should be named Solution 16 | * Don't write main(). 17 | * Don't read input, it is passed as function argument. 18 | * Return output and don't print it. 19 | * Taking input and printing output is handled automatically. 20 | */ 21 | LinkedListNode temp = head; 22 | int index = 0; 23 | while (temp.next != null) { 24 | if (temp.data == n) { 25 | return index; 26 | } else { 27 | temp = temp.next; 28 | index++; 29 | } 30 | } 31 | if (temp.data == n) { 32 | return index; 33 | } 34 | return -1; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Linked List 2/FindanElementrec.java: -------------------------------------------------------------------------------- 1 | /*************** 2 | * Following is the Node class already written 3 | class LinkedListNode { 4 | T data; 5 | LinkedListNode next; 6 | 7 | public Node(T data) { 8 | this.data = data; 9 | } 10 | } 11 | ***************/ 12 | 13 | public class Solution { 14 | public static int index = 0; 15 | public static int indexOfNRec(LinkedListNode head, int n) { 16 | /* Your class should be named Solution 17 | * Don't write main(). 18 | * Don't read input, it is passed as function argument. 19 | * Return output and don't print it. 20 | * Taking input and printing output is handled automatically. 21 | */ 22 | LinkedListNode temp = head; 23 | if (head == null) { 24 | return -1; 25 | } 26 | if (temp.data != n) { 27 | index++; 28 | temp = temp.next; 29 | return indexOfNRec(temp, n); 30 | } 31 | return index; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Linked List 2/evenafteroddLL.java: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | class LinkedListNode { 4 | public T data; 5 | public LinkedListNode next; 6 | 7 | public LinkedListNode(T data) { 8 | this.setData(data); 9 | this.next = null; 10 | } 11 | 12 | 13 | 14 | } 15 | * */ 16 | public class Solution { 17 | public static LinkedListNode sortEvenOdd(LinkedListNode head) { 18 | if (head == null) { 19 | return head; 20 | } 21 | LinkedListNode evenHead = null, oddHead = null, evenTail = null, oddTail = null; 22 | while (head != null) { 23 | if (head.data % 2 == 0) { 24 | if (evenHead == null) { 25 | evenHead = head; 26 | evenTail = head; 27 | } else { 28 | evenTail.next = head; 29 | evenTail = evenTail.next; 30 | } 31 | } else { 32 | if (oddHead == null) { 33 | oddHead = head; 34 | oddTail = head; 35 | } else { 36 | oddTail.next = head; 37 | oddTail = oddTail.next; 38 | } 39 | } 40 | head = head.next; 41 | } 42 | if (oddHead == null) { 43 | return evenHead; 44 | } else { 45 | oddTail.next = evenHead; 46 | } 47 | if (evenHead != null) { 48 | evenTail.next = null; 49 | } 50 | return oddHead; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /Linked List 2/kReverse.java: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | class LinkedListNode { 4 | public T data; 5 | public LinkedListNode next; 6 | 7 | public LinkedListNode(T data) { 8 | this.setData(data); 9 | this.next = null; 10 | } 11 | 12 | public T getData() { 13 | return data; 14 | } 15 | 16 | public void setData(T data) { 17 | this.data = data; 18 | } 19 | 20 | } 21 | * */ 22 | public class Solution { 23 | public static LinkedListNode kReverse(LinkedListNode head, int k) { 24 | if(head== null){ 25 | return null; 26 | } //base case 27 | LinkedListNode current = head; 28 | LinkedListNode next = null; 29 | LinkedListNode prev = null; 30 | int count = 0; 31 | /* Reverse first k nodes of linked list */ 32 | while (count < k && current != null) { 33 | next = current.next; 34 | current.next = prev; 35 | prev = current; 36 | current = next; 37 | count++; 38 | } 39 | if (next != null) { 40 | head.next = kReverse(next, k); 41 | } 42 | return prev; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Linked List 2/nthNodeFRomLast.java: -------------------------------------------------------------------------------- 1 | /*************** 2 | * Following is the Node class already written 3 | class LinkedListNode { 4 | T data; 5 | LinkedListNode next; 6 | 7 | public Node(T data) { 8 | this.data = data; 9 | } 10 | } 11 | ***************/ 12 | 13 | public class Solution { 14 | public static int getLength(LinkedListNode head) { 15 | LinkedListNode temp = head; 16 | int count = 0; 17 | while(temp != null) { 18 | count++; 19 | temp = temp.next; 20 | } 21 | return count; 22 | } 23 | 24 | public static LinkedListNode nthNodeFromLastIter(LinkedListNode head, int n){ 25 | /* Your class should be named Solution 26 | * Don't write main(). 27 | * Don't read input, it is passed as function argument. 28 | * Return output and don't print it. 29 | * Taking input and printing output is handled automatically. 30 | */ 31 | LinkedListNode main = head, ref = head; 32 | while(n-- != -1) { 33 | ref = ref.next; 34 | if(ref == null) 35 | return null; 36 | } 37 | while(ref != null) { 38 | main = main.next; 39 | ref = ref.next; 40 | } 41 | return main; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /LinkedList1/DeleteNode.java: -------------------------------------------------------------------------------- 1 | /*************** 2 | * Following is the Node class already written 3 | class LinkedListNode { 4 | T data; 5 | LinkedListNode next; 6 | 7 | public Node(T data) { 8 | this.data = data; 9 | } 10 | } 11 | ***************/ 12 | 13 | public class Solution { 14 | 15 | public static LinkedListNode deleteIthNode(LinkedListNode head, int i){ 16 | /* Your class should be named Solution 17 | * Don't write main(). 18 | * Don't read input, it is passed as function argument. 19 | * Return output and don't print it. 20 | * Taking input and printing output is handled automatically. 21 | */ 22 | if(head==null){ 23 | return null; 24 | } 25 | 26 | LinkedListNode temp=head; 27 | int actual_length=0; 28 | while(temp!=null){ 29 | temp=temp.next; 30 | actual_length++; 31 | } 32 | 33 | if(i>actual_length-1){ 34 | return head; 35 | } 36 | temp=head; 37 | if(i==0){ 38 | head=temp.next; 39 | return head; 40 | } 41 | LinkedListNode prev=null; 42 | int len=0; 43 | 44 | while(len { 4 | T data; 5 | Node next; 6 | 7 | public Node(T data) { 8 | this.data = data; 9 | } 10 | } 11 | ***************/ 12 | 13 | public class Solution { 14 | public static int length(Node head){ 15 | int ct = 0; 16 | while(head!=null){ 17 | ct++; 18 | head=head.next; 19 | } 20 | return ct; 21 | } 22 | public static Node insert(Node head, int data, int pos){ 23 | /* Your class should be named Solution 24 | * Don't write main(). 25 | * Don't read input, it is passed as function argument. 26 | * Return output and don't print it. 27 | * Taking input and printing output is handled automatically. 28 | */ 29 | int len = length(head); 30 | if(pos>len||pos<0){ 31 | return head; 32 | } 33 | int count = 0; 34 | Node curr = head; 35 | Node prev = null; 36 | while(countnewmode=new Node<>(data); 42 | if(prev!=null){ 43 | prev.next=newmode; 44 | newmode.next=curr; 45 | return head; 46 | } 47 | else { 48 | newmode.next=head; 49 | return newmode; 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /LinkedList1/LengthRec.java: -------------------------------------------------------------------------------- 1 | /*************** 2 | * Following is the Node class already written 3 | class LinkedListNode { 4 | T data; 5 | LinkedListNode next; 6 | 7 | public Node(T data) { 8 | this.data = data; 9 | } 10 | } 11 | ***************/ 12 | 13 | public class Solution { 14 | 15 | public static int length(LinkedListNode head){ 16 | /* Your class should be named Solution 17 | * Don't write main(). 18 | * Don't read input, it is passed as function argument. 19 | * Return output and don't print it. 20 | * Taking input and printing output is handled automatically. 21 | */ 22 | LinkedListNode temp = head; 23 | int count = 1; 24 | while (temp.next != null) { 25 | count++; 26 | temp = temp.next; 27 | } 28 | return count; 29 | 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /LinkedList1/LengthofLL.java: -------------------------------------------------------------------------------- 1 | /*************** 2 | * Following is the Node class already written 3 | class LinkedListNode { 4 | T data; 5 | LinkedListNode next; 6 | 7 | public LinkedListNode(T data) { 8 | this.data = data; 9 | } 10 | } 11 | ***************/ 12 | 13 | public class Solution { 14 | 15 | public static int length(LinkedListNode head){ 16 | /* Your class should be named Solution 17 | * Don't write main(). 18 | * Don't read input, it is passed as function argument. 19 | * Return output and don't print it. 20 | * Taking input and printing output is handled automatically. 21 | */ 22 | int count = 0; 23 | while (head!= null){ 24 | count++; 25 | head = head.next; 26 | 27 | } 28 | return count; 29 | } 30 | } -------------------------------------------------------------------------------- /LinkedList1/Palindrome.java: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | class LinkedListNode { 4 | public T data; 5 | public LinkedListNode next; 6 | 7 | public LinkedListNode(T data) { 8 | this.setData(data); 9 | this.next = null; 10 | } 11 | 12 | public T getData() { 13 | return data; 14 | } 15 | 16 | public void setData(T data) { 17 | this.data = data; 18 | } 19 | 20 | } 21 | * */ 22 | public class Solution { 23 | static LinkedListNode left=null; 24 | public static boolean isPalindrome_2(LinkedListNode head) { 25 | left=head; 26 | return find(head); 27 | 28 | } 29 | public static boolean find(LinkedListNode right){ 30 | if(right==null){ 31 | return true; 32 | } 33 | boolean val=find(right.next); 34 | if(val==true){ 35 | if(left.data==right.data){ 36 | left=left.next; 37 | return true; 38 | } 39 | } 40 | return false; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /LinkedList1/PrintReverLL.java: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | class LinkedListNode { 4 | public T data; 5 | public LinkedListNode next; 6 | 7 | public LinkedListNode(T data) { 8 | this.setData(data); 9 | this.next = null; 10 | } 11 | 12 | public T getData() { 13 | return data; 14 | } 15 | 16 | public void setData(T data) { 17 | this.data = data; 18 | } 19 | 20 | } 21 | * */ 22 | public class Solution { 23 | public static void printReverseRecursive(LinkedListNode root) { 24 | LinkedListNode head = root; 25 | LinkedListNode temp = head; 26 | if (head.next == null) { 27 | System.out.println(head.getData()); 28 | return; 29 | } else { 30 | while (head.next.next != null) { 31 | head = head.next; 32 | } 33 | System.out.print(head.next.getData() + " "); 34 | head.next = null; 35 | printReverseRecursive(temp); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /LinkedList1/ReverseLLiterarive.java: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | class LinkedListNode { 4 | public T data; 5 | public LinkedListNode next; 6 | 7 | public LinkedListNode(T data) { 8 | this.setData(data); 9 | this.next = null; 10 | } 11 | 12 | public T getData() { 13 | return data; 14 | } 15 | 16 | public void setData(T data) { 17 | this.data = data; 18 | } 19 | 20 | } 21 | * */ 22 | public class Solution { 23 | public static LinkedListNode reverse_I(LinkedListNode head) { 24 | LinkedListNode current = head; 25 | LinkedListNode previous = null; 26 | LinkedListNode temp; 27 | while (current != null) { 28 | temp = current.next; 29 | current.next = previous; 30 | previous = current; 31 | current = temp; 32 | } 33 | return previous; 34 | } 35 | //tc o(n) 36 | //sc o(1) 37 | 38 | } 39 | -------------------------------------------------------------------------------- /LinkedList1/ReverseLLrecusive.java: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | class LinkedListNode { 4 | public T data; 5 | public LinkedListNode next; 6 | 7 | public LinkedListNode(T data) { 8 | this.setData(data); 9 | this.next = null; 10 | } 11 | 12 | public T getData() { 13 | return data; 14 | } 15 | 16 | public void setData(T data) { 17 | this.data = data; 18 | } 19 | 20 | } 21 | * */ 22 | public class Solution { 23 | 24 | public static LinkedListNode reverse_R(LinkedListNode head) { 25 | 26 | LinkedListNode h = head; 27 | LinkedListNode temp = h; 28 | if (h.next == null) { 29 | return h; 30 | } 31 | while (h.next.next != null) { 32 | h = h.next; 33 | } 34 | LinkedListNode tempHead = h.next; 35 | h.next = null; 36 | tempHead.next = reverse_R(temp); 37 | return tempHead; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /LinkedList1/deletenoderec.java: -------------------------------------------------------------------------------- 1 | /*************** 2 | * Following is the Node class already written 3 | class LinkedListNode { 4 | T data; 5 | LinkedListNode next; 6 | 7 | public Node(T data) { 8 | this.data = data; 9 | } 10 | } 11 | ***************/ 12 | 13 | public class Solution { 14 | 15 | public static LinkedListNode deleteIthNodeRec(LinkedListNode head, int i){ 16 | /* Your class should be named Solution 17 | * Don't write main(). 18 | * Don't read input, it is passed as function argument. 19 | * Return output and don't print it. 20 | * Taking input and printing output is handled automatically. 21 | */ 22 | if (i == 0) { 23 | return head.next; 24 | } 25 | if (head.next == null) { 26 | return head; 27 | } 28 | head.next = deleteIthNodeRec(head.next, i - 1); 29 | return head; 30 | 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /LinkedList1/insertnoderec.java: -------------------------------------------------------------------------------- 1 | /*************** 2 | * Following is the Node class already written 3 | class LinkedListNode { 4 | T data; 5 | LinkedListNode next; 6 | 7 | public Node(T data) { 8 | this.data = data; 9 | } 10 | } 11 | ***************/ 12 | 13 | public class Solution { 14 | 15 | public static LinkedListNode insertR(LinkedListNode head, int data, int pos){ 16 | /* Your class should be named Solution 17 | * Don't write main(). 18 | * Don't read input, it is passed as function argument. 19 | * Return output and don't print it. 20 | * Taking input and printing output is handled automatically. 21 | */ 22 | if (pos == 0) { 23 | LinkedListNode newNode = new LinkedListNode(data); 24 | newNode.next = head; 25 | return newNode; 26 | } 27 | if (head == null) { 28 | return head; 29 | } 30 | head.next = insertR(head.next, data, pos - 1); 31 | return head; 32 | 33 | 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /LinkedList1/mergetwosortedll.java: -------------------------------------------------------------------------------- 1 | /*************** 2 | * Following is the Node class already written 3 | class LinkedListNode { 4 | T data; 5 | LinkedListNode next; 6 | 7 | public Node(T data) { 8 | this.data = data; 9 | } 10 | } 11 | ***************/ 12 | 13 | public class Solution { 14 | 15 | public static LinkedListNode mergeTwoList(LinkedListNode head1, LinkedListNode head2) { 16 | /* Your class should be named Solution 17 | * Don't write main(). 18 | * Don't read input, it is passed as function argument. 19 | * Return output and don't print it. 20 | * Taking input and printing output is handled automatically. 21 | */ 22 | LinkedListNode head = null, tail = null; 23 | if (head1.data < head2.data) { 24 | head = head1; 25 | tail = head1; 26 | head1 = head1.next; 27 | } else { 28 | head = head2; 29 | tail = head2; 30 | head2 = head2.next; 31 | } 32 | while (head1 != null && head2 != null) { 33 | if (head1.data < head2.data) { 34 | tail.next = head1; 35 | tail = tail.next; 36 | head1 = head1.next; 37 | } else { 38 | tail.next = head2; 39 | tail = tail.next; 40 | head2 = head2.next; 41 | } 42 | } 43 | if (head1 != null) { 44 | tail.next = head1; 45 | } 46 | if (head2 != null) { 47 | tail.next = head2; 48 | } 49 | return head; 50 | 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /LinkedList1/midpoint.java: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | class LinkedListNode { 4 | public T data; 5 | public LinkedListNode next; 6 | 7 | public LinkedListNode(T data) { 8 | this.setData(data); 9 | this.next = null; 10 | } 11 | 12 | public T getData() { 13 | return data; 14 | } 15 | 16 | public void setData(T data) { 17 | this.data = data; 18 | } 19 | 20 | } 21 | * */ 22 | public class Solution { 23 | public static int length(LinkedListNode head) { 24 | LinkedListNode temp = head; 25 | int count = 1; 26 | while (temp.next != null) { 27 | count++; 28 | temp = temp.next; 29 | } 30 | return count; 31 | } 32 | public static int printMiddel(LinkedListNode head) { 33 | int middle = 0; 34 | LinkedListNode slow = head; 35 | LinkedListNode fast = head; 36 | if (length(head) % 2 == 0) { 37 | while (fast.next.next != null) { 38 | slow = slow.next; 39 | fast = fast.next.next; 40 | } 41 | middle = slow.getData(); 42 | } else { 43 | while (fast.next != null) { 44 | slow = slow.next; 45 | fast = fast.next.next; 46 | } 47 | middle = slow.getData(); 48 | } 49 | return middle; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /LinkedList1/printiThNode.java: -------------------------------------------------------------------------------- 1 | /*************** 2 | * Following is the Node class already written 3 | class LinkedListNode { 4 | T data; 5 | LinkedListNode next; 6 | 7 | public Node(T data) { 8 | this.data = data; 9 | } 10 | } 11 | ***************/ 12 | 13 | public class Solution { 14 | 15 | public static void printIth(LinkedListNode head, int i){ 16 | /* Your class should be named Solution 17 | * Don't write main(). 18 | * Don't read input, it is passed as function argument. 19 | * Print output and don't return it. 20 | * Taking input is handled automatically. 21 | */ 22 | LinkedListNode temp = head; 23 | int count = 0; 24 | while (temp != null) { 25 | if (count == i) { 26 | System.out.println(temp.data); 27 | return; 28 | } else { 29 | count++; 30 | temp = temp.next; 31 | if (count == i) { 32 | System.out.println(temp.data); 33 | return; 34 | } 35 | } 36 | } 37 | } 38 | } -------------------------------------------------------------------------------- /LinkedList1/swaptwonode.java: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | /* 5 | class LinkedListNode { 6 | public T data; 7 | public LinkedListNode next; 8 | 9 | public LinkedListNode(T data) { 10 | this.setData(data); 11 | this.next = null; 12 | } 13 | 14 | public T getData() { 15 | return data; 16 | } 17 | 18 | public void setData(T data) { 19 | this.data = data; 20 | } 21 | 22 | } 23 | * */ 24 | public class Solution { 25 | public static LinkedListNode swap_nodes(LinkedListNode head,int i,int j){ 26 | LinkedListNode temp = head, prev = null; 27 | LinkedListNode node1 = null, node2 = null, 28 | node1_prev = null, node2_prev = null; 29 | int pos = 0; 30 | while(temp != null) { 31 | if(pos == i) { 32 | node1_prev = prev; 33 | node1 = temp; 34 | } else if (pos == j) { 35 | node2_prev = prev; 36 | node2 = temp; 37 | } 38 | prev = temp; 39 | temp = temp.next; 40 | pos++; 41 | } 42 | if(node1_prev != null) { 43 | node1_prev.next = node2; 44 | } else { 45 | head = node2; 46 | } 47 | if(node2_prev != null) { 48 | node2_prev.next = node1; 49 | } else { 50 | head = node1; 51 | } 52 | LinkedListNode temp1 = node2.next; 53 | node2.next = node1.next; 54 | node1.next = temp1; 55 | return head; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /Priority Queues/BuytheTicket.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class Solution { 3 | 4 | public static int buyTicket(int input[], int k) { 5 | /* Your class should be named Solution 6 | * Don't write main(). 7 | * Don't read input, it is passed as function argument. 8 | * Return output and don't print it. 9 | * Taking input and printing output is handled automatically. 10 | */ 11 | Queue q = new LinkedList(); 12 | PriorityQueue pq = new PriorityQueue(10,Collections.reverseOrder()); 13 | for(int i: input){ 14 | q.add(i); 15 | pq.add(i); 16 | } 17 | int count =0; 18 | while(!pq.isEmpty()){ 19 | if(q.peek().equals(pq.peek())){ 20 | if(k==0){ 21 | return count+1; 22 | } 23 | else 24 | { 25 | count++; 26 | q.poll(); 27 | pq.poll(); 28 | k--; 29 | } 30 | } 31 | else { 32 | q.add(q.peek()); 33 | q.poll(); 34 | if(k==0){ 35 | k = q.size()-1; 36 | 37 | } 38 | else { 39 | k--; 40 | } 41 | } 42 | } 43 | return count; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Priority Queues/KLargestNumber.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.ArrayList; 3 | import java.util.Comparator; 4 | import java.util.PriorityQueue; 5 | import java.util.Scanner; 6 | 7 | class MaxPQComparator implements Comparator { 8 | 9 | @Override 10 | public int compare(Integer o1, Integer o2) { 11 | if (o1 < o2) { 12 | return -1; 13 | } else if (o1 > o2) { 14 | return 1; 15 | } else { 16 | return 0; 17 | } 18 | } 19 | } 20 | 21 | public class Solution { 22 | 23 | public static ArrayList kLargest(int input[], int k) { 24 | /* Your class should be named Solution 25 | * Don't write main(). 26 | * Don't read input, it is passed as function argument. 27 | * Return output and don't print it. 28 | * Taking input and printing output is handled automatically. 29 | */ 30 | MaxPQComparator maxPQComparator = new MaxPQComparator(); 31 | PriorityQueue pQueue = new PriorityQueue<>(maxPQComparator); 32 | ArrayList output = new ArrayList<>(); 33 | for (int i = 0; i < input.length; i++) { 34 | pQueue.add(input[i]); 35 | } 36 | while (pQueue.size() != k) { 37 | pQueue.remove(); 38 | } 39 | while (!pQueue.isEmpty()) { 40 | output.add(pQueue.remove()); 41 | } 42 | return output; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Priority Queues/KthLargestElement.java: -------------------------------------------------------------------------------- 1 | import java.util.PriorityQueue; 2 | public class Solution { 3 | 4 | public static int kthLargest(int arr[], int k) { 5 | /* Your class should be named Solution 6 | * Don't write main(). 7 | * Don't read input, it is passed as function argument. 8 | * Return output and don't print it. 9 | * Taking input and printing output is handled automatically. 10 | */ 11 | PriorityQueue pQueue = new PriorityQueue<>(); 12 | int i = 0; 13 | for (; i < k; i++) { 14 | pQueue.add(arr[i]); 15 | } 16 | for (; i < arr.length; i++) { 17 | if (pQueue.element() < arr[i]) { 18 | pQueue.remove(); 19 | pQueue.add(arr[i]); 20 | } 21 | } 22 | return pQueue.element(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Priority Queues/checkmaxheap.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public static boolean maxHeapProperty = true; 3 | public static boolean checkMaxHeap(int arr[]) { 4 | /* Your class should be named Solution 5 | * Don't write main(). 6 | * Don't read input, it is passed as function argument. 7 | * Return output and don't print it. 8 | * Taking input and printing output is handled automatically. 9 | */ 10 | int parentIndex, leftChildIndex, rightChildIndex; 11 | for (int i = 0; i < arr.length; i++) { 12 | parentIndex = i; 13 | leftChildIndex = 2 * parentIndex + 1; 14 | rightChildIndex = 2 * parentIndex + 2; 15 | if (leftChildIndex < arr.length) { 16 | if (arr[parentIndex] < arr[leftChildIndex]) { 17 | maxHeapProperty = false; 18 | break; 19 | } 20 | if (rightChildIndex < arr.length && arr[parentIndex] < arr[rightChildIndex]) { 21 | maxHeapProperty = false; 22 | } 23 | } 24 | } 25 | return maxHeapProperty; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Priority Queues/mergeKsortaraay.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.PriorityQueue; 3 | 4 | public class Solution { 5 | 6 | public static ArrayList mergeKSortedArrays(ArrayList> input) { 7 | /* Your class should be named Solution 8 | * Don't write main(). 9 | * Don't read input, it is passed as function argument. 10 | * Return output and don't print it. 11 | * Taking input and printing output is handled automatically. 12 | */ 13 | PriorityQueue pQueue = new PriorityQueue<>(); 14 | ArrayList output = new ArrayList<>(); 15 | int maxSize = Integer.MIN_VALUE; 16 | for (ArrayList list : input) { 17 | if (maxSize < list.size()) { 18 | maxSize = list.size(); 19 | } 20 | } 21 | int i = 0, j = 0; 22 | while (i != maxSize) { 23 | j = 0; 24 | for (; j < input.size(); j++) { 25 | if (i >= input.get(j).size()) { 26 | continue; 27 | } else { 28 | pQueue.add(input.get(j).get(i)); 29 | } 30 | } 31 | i++; 32 | } 33 | while (!pQueue.isEmpty()) { 34 | output.add(pQueue.remove()); 35 | } 36 | return output; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Coding-Ninjas- -------------------------------------------------------------------------------- /Recursion 1/AllIndicesOfNumber.java: -------------------------------------------------------------------------------- 1 | 2 | public class Solution { 3 | 4 | public static int[] allIndexes(int input[], int x) { 5 | /* Your class should be named Solution 6 | * Don't write main(). 7 | * Don't read input, it is passed as function argument. 8 | * Return output and don't print it. 9 | * Taking input and printing output is handled automatically. 10 | */ 11 | return allIndexes(input,x,0); 12 | } 13 | public static int[] allIndexes(int input[], int x,int startIndex){ 14 | if(startIndex == input.length){ 15 | int[] rv = new int[0]; 16 | return rv; 17 | } 18 | int[] roaIndex = allIndexes(input,x,startIndex + 1); 19 | if(input[startIndex] == x){ 20 | int[] totalIndex = new int[(roaIndex.length) + 1]; 21 | totalIndex[0] = startIndex; 22 | for(int i = 0;i < roaIndex.length;i++){ 23 | totalIndex[i+1] = roaIndex[i]; 24 | } 25 | return totalIndex; 26 | } 27 | else{ 28 | return roaIndex; 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Recursion 1/CheckNumberInArray.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | 3 | public static boolean checkNumber(int input[], int x) { 4 | /* Your class should be named Solution 5 | * Don't write main(). 6 | * Don't read input, it is passed as function argument. 7 | * Return output and don't print it. 8 | * Taking input and printing output is handled automatically. 9 | */ 10 | return check(input, 0, x); 11 | } 12 | public static boolean check(int[] input, int startIndex, int x) { 13 | if (startIndex == input.length) { 14 | return false; 15 | } 16 | 17 | if (input[startIndex] == x) { 18 | return true; 19 | } 20 | return check(input, startIndex + 1, x); 21 | 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Recursion 1/CheckPalindromerec.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class solution { 3 | 4 | public static boolean isStringPalindrome(String input) { 5 | // Write your code here 6 | if(input.length() == 1){ 7 | return true; 8 | } 9 | if(input.length() == 2){ 10 | if(input.charAt(0) == input.charAt(1)) 11 | return true; 12 | else 13 | return false; 14 | } 15 | if(input.charAt(0) == input.charAt(input.length()-1)){ 16 | return isStringPalindrome(input.substring(1,input.length() - 1)); 17 | } 18 | else 19 | return false; 20 | 21 | } 22 | 23 | public static void main(String[] args) { 24 | Scanner s = new Scanner(System.in); 25 | System.out.print(isStringPalindrome(s.nextLine())); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Recursion 1/CountZero.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class solution { 3 | 4 | public static int countZerosRec(int input){ 5 | // Write your code here 6 | if(input == 0) 7 | return 0; 8 | if(input % 10 == 0){ 9 | return countZerosRec(input / 10) + 1; 10 | } 11 | else{ 12 | return countZerosRec(input / 10); 13 | } 14 | } 15 | 16 | public static void main(String[] args) { 17 | Scanner s = new Scanner(System.in); 18 | System.out.print(countZerosRec(s.nextInt())); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Recursion 1/FirstIndexofNumber.java: -------------------------------------------------------------------------------- 1 | 2 | public class Solution { 3 | 4 | public static int firstIndex(int input[], int x) { 5 | /* Your class should be named Solution 6 | * Don't write main(). 7 | * Don't read input, it is passed as function argument. 8 | * Return output and don't print it. 9 | * Taking input and printing output is handled automatically. 10 | */ 11 | return index(input, 0, x); 12 | 13 | } 14 | public static int index(int[] input, int startIndex, int x) { 15 | if (startIndex == input.length) { 16 | return -1; 17 | } 18 | if (input[startIndex] == x) { 19 | return startIndex; 20 | } 21 | return index(input, startIndex + 1, x); 22 | 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Recursion 1/GeometricSum.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class solution { 3 | 4 | public static double findGeometricSum(int k){ 5 | // Write your code here 6 | if(k==0){ 7 | return 1; 8 | } 9 | return findGeometricSum(k - 1)+(1 / Math.pow(2,k)); 10 | 11 | } 12 | 13 | public static void main(String[] args) { 14 | Scanner s = new Scanner(System.in); 15 | System.out.print(findGeometricSum(s.nextInt())); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Recursion 1/LasttIndexofNumber.java: -------------------------------------------------------------------------------- 1 | 2 | public class Solution { 3 | 4 | public static int lastIndex(int input[], int x) { 5 | /* Your class should be named Solution 6 | * Don't write main(). 7 | * Don't read input, it is passed as function argument. 8 | * Return output and don't print it. 9 | * Taking input and printing output is handled automatically. 10 | */ 11 | return lastIndexOfElement(input,x, 0); 12 | } 13 | public static int lastIndexOfElement(int[] input,int x,int startIndex){ 14 | if(startIndex == input.length) 15 | return -1; 16 | int index = lastIndexOfElement(input,x,startIndex + 1); 17 | if(index == -1) 18 | { 19 | if(x == input[startIndex]) 20 | return startIndex; 21 | else 22 | return -1; 23 | } 24 | else 25 | return index; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Recursion 1/MultiplicationRec.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class solution { 3 | 4 | public static int multiplyTwoIntegers(int m, int n){ 5 | // Write your code here 6 | if(n == 1) 7 | return m; 8 | int result = m + multiplyTwoIntegers(m,n - 1); 9 | return result; 10 | } 11 | 12 | public static void main(String[] args) { 13 | Scanner s = new Scanner(System.in); 14 | System.out.println(multiplyTwoIntegers(s.nextInt(),s.nextInt())); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Recursion 1/NumberOfDigits.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | 3 | public static int count(int n){ 4 | if(n == 0){ 5 | return 0; 6 | } 7 | int smallAns = count(n / 10); 8 | return smallAns + 1; 9 | } 10 | 11 | } 12 | -------------------------------------------------------------------------------- /Recursion 1/PairSum.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class solution { 3 | 4 | // Return the updated string 5 | public static String addStars(String s) { 6 | // Write your code here 7 | if(s.length() <= 1){ 8 | return s; 9 | } 10 | String r = addStars(s.substring(1)); 11 | if(s.charAt(0) == r.charAt(0)){ 12 | return s.charAt(0) + "*" + r; 13 | } 14 | else{ 15 | return s.charAt(0) + r; 16 | } 17 | } 18 | 19 | public static void main(String[] args) { 20 | Scanner s = new Scanner(System.in); 21 | System.out.print(addStars(s.nextLine())); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Recursion 1/PrintNumbers.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | 3 | public static void print(int n){ 4 | if(n == 1){ 5 | System.out.print(n + " "); 6 | return; 7 | } 8 | 9 | print(n - 1); 10 | System.out.print(n+" "); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /Recursion 1/RemoveX.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class solution { 3 | 4 | // Return the changed string 5 | public static String removeX(String input){ 6 | // Write your code here 7 | if(input.length() == 0){ 8 | return ""; 9 | } 10 | if(input.charAt(0) == 'x'){ 11 | return removeX(input.substring(1)); 12 | } 13 | else{ 14 | return input.charAt(0) + removeX(input.substring(1)); 15 | } 16 | } 17 | 18 | public static void main(String[] args) { 19 | Scanner s = new Scanner(System.in); 20 | System.out.println(removeX(s.nextLine())); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Recursion 1/ReplacePi.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class solution { 3 | 4 | // Return the changed string 5 | public static String replace(String input){ 6 | // Write your code here 7 | if(input.length() <= 1){ 8 | return input; 9 | } 10 | String r = replace(input.substring(1)); 11 | if(input.charAt(0) == 'p'){ 12 | if(r.charAt(0) == 'i'){ 13 | return "3.14" + r.substring(1); 14 | } 15 | else{ 16 | return input.charAt(0) + r; 17 | } 18 | } 19 | else{ 20 | return input.charAt(0) + r; 21 | } 22 | } 23 | 24 | public static void main(String[] args) { 25 | Scanner s = new Scanner(System.in); 26 | System.out.print(replace(s.nextLine())); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Recursion 1/StringtoInt.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class solution { 3 | 4 | public static int convertStringToInt(String input){ 5 | // Write your code here 6 | return convertStringToInteger(input,input.length()-1); 7 | } 8 | public static int convertStringToInteger(String input,int i){ 9 | if(input.length() == 0){ 10 | return 0; 11 | } 12 | return ((int)input.charAt(0) - 48)*(int)(Math.pow(10,i)) + convertStringToInteger(input.substring(1),i - 1); 13 | 14 | } 15 | 16 | public static void main(String[] args) { 17 | Scanner s = new Scanner(System.in); 18 | System.out.print(convertStringToInt(s.nextLine())); 19 | 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Recursion 1/SumNnaturalNumber.java: -------------------------------------------------------------------------------- 1 | 2 | public class Solution { 3 | 4 | public static int sum(int n) { 5 | /* Your class should be named Solution 6 | * Don't write main(). 7 | * Don't read input, it is passed as function argument. 8 | * Return output and don't print it. 9 | * Taking input and printing output is handled automatically. 10 | */ 11 | if(n<=1) { 12 | return 1; 13 | } 14 | int ans1 = sum(n-1); 15 | 16 | int ans = n+ans1; 17 | return ans; 18 | 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Recursion 1/SumOfArray.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | 3 | public static int sum(int input[]) { 4 | return sumofArray(input,0) ; 5 | 6 | } 7 | 8 | public static int sumofArray(int[] arr,int startIndex) { 9 | 10 | if(startIndex == arr.length){ 11 | return 0; 12 | } 13 | int smallSum = sumofArray(arr,startIndex+1); 14 | int mySum = smallSum+arr[startIndex]; 15 | return mySum; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Recursion 1/TowerOfhanoi.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class solution { 3 | 4 | public static void towerOfHanoi(int disks, char source, char auxiliary, char destination) { 5 | // Write your code here 6 | if(disks == 0){ 7 | return;` 8 | } 9 | towerOfHanoi(disks-1,source,destination,auxiliary); 10 | System.out.println(source + " " + destination); 11 | towerOfHanoi(disks-1,auxiliary,source,destination); 12 | } 13 | 14 | public static void main(String[] args) { 15 | Scanner s = new Scanner(System.in); 16 | int disks = s.nextInt(); 17 | char source = s.next().charAt(0); 18 | char auxiliary = s.next().charAt(0); 19 | char destination = s.next().charAt(0); 20 | towerOfHanoi(disks, source, auxiliary, destination); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Recursion 1/power.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | 3 | public static int power(int x, int n) { 4 | 5 | if(n!=0){ 6 | return(x*(power(x,n-1))); 7 | } else 8 | return 1; 9 | } 10 | } 11 | 12 | -------------------------------------------------------------------------------- /Recursion 1/sumofDigitrec.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class solution { 3 | 4 | public static int sumOfDigits(int input){ 5 | // Write your code here 6 | if(input == 0){ 7 | return 0; 8 | } 9 | return input % 10 + sumOfDigits(input / 10); 10 | 11 | } 12 | 13 | public static void main(String[] args) { 14 | Scanner s = new Scanner(System.in); 15 | System.out.print(sumOfDigits(s.nextInt())); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Recursion 2/BinarySearchRec.java: -------------------------------------------------------------------------------- 1 | public class solution { 2 | public static void main(String[] args) { 3 | int[] arr = { 1, 2, 5, 6, 7 }; 4 | System.out.println(binarySearch(arr, 7)); 5 | 6 | } 7 | 8 | // element - number to be searched 9 | public static int binarySearch(int input[], int element) { 10 | // Write your code here 11 | return binarySearch(input, element, 0, input.length - 1); 12 | 13 | } 14 | public static int binarySearch(int[] input, int element, int start, int end) { 15 | 16 | int mid = (end + start) / 2; 17 | if (start >= end) { 18 | return -1; 19 | } 20 | if (input[mid] == element) { 21 | return mid; 22 | 23 | } 24 | if (element > input[mid]) { 25 | return binarySearch(input, element, mid + 1, end); 26 | } 27 | return binarySearch(input, element, start, mid - 1); 28 | 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Recursion 2/ChechAB.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Solution { 3 | 4 | public static boolean checkAB(String input) { 5 | // Write your code here 6 | if(input.length() == 0){ 7 | return true; 8 | } 9 | if(input.length() == 1){ 10 | if(input.charAt(0) == 'a') 11 | return true; 12 | else 13 | return false; 14 | } 15 | if(input.charAt(0) == 'a'){ 16 | if(input.charAt(1) == 'b' && input.length() > 2 && input.charAt(2) == 'b') 17 | return checkAB(input.substring(3)); 18 | else if(input.charAt(1) == 'a') 19 | return checkAB(input.substring(1)); 20 | else 21 | return false; 22 | } 23 | else 24 | return false; 25 | } 26 | 27 | public static void main(String[] args) { 28 | Scanner s = new Scanner(System.in); 29 | System.out.println(checkAB(s.next())); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Recursion 2/PrintPermuation.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class solution { 3 | 4 | public static void permutations(String input){ 5 | // Write your code here 6 | printPermutations(input,""); 7 | } 8 | public static void printPermutations(String input,String outputSoFar){ 9 | if(input.length() == 0){ 10 | System.out.println(outputSoFar); 11 | return; 12 | } 13 | for(int i = 0; i < input.length();i++){ 14 | char ch = input.charAt(i); 15 | String toBePermuted = input.substring(0,i) + input.substring(i + 1); 16 | printPermutations(toBePermuted,outputSoFar + ch); 17 | } 18 | } 19 | 20 | public static void main(String[] args) { 21 | Scanner s = new Scanner(System.in); 22 | printPermutations(s.next(),""); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Recursion 2/PrintSubsequence.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class solution { 3 | 4 | public static void printSubsequences(String input) { 5 | // Write your code here 6 | printSubsequences(input, ""); 7 | } 8 | public static void printSubsequences(String input, String outputSoFar) { 9 | if (input.length() == 0) { 10 | System.out.println(outputSoFar); 11 | return; 12 | } 13 | printSubsequences(input.substring(1), outputSoFar); 14 | printSubsequences(input.substring(1), outputSoFar + input.charAt(0)); 15 | } 16 | 17 | public static void main(String[] args) { 18 | Scanner scanner = new Scanner(System.in); 19 | 20 | String input = scanner.next(); 21 | 22 | printSubsequences(input); 23 | } 24 | } 25 | 26 | -------------------------------------------------------------------------------- /Recursion 2/PrintSubsetSumtoK.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class solution { 3 | public static void printSubsetsSumTok(int input[], int k) { 4 | // Write your code here 5 | int[] output=new int[0]; 6 | printSubsetsSumTok(input, k, 0,output); 7 | 8 | } 9 | 10 | public static void printSubsetsSumTok(int[] input, int k, int start,int[] output) { 11 | if (start == input.length) { 12 | if (k == 0) { 13 | for (int i : output) { 14 | System.out.print(i + " "); 15 | } 16 | System.out.println(); 17 | return; 18 | } 19 | else { 20 | return; 21 | } 22 | 23 | } 24 | int[] newoutput = new int[output.length + 1]; 25 | int i = 0; 26 | for (; i < output.length; i++) { 27 | newoutput[i] = output[i]; 28 | 29 | } 30 | newoutput[i] = input[start]; 31 | printSubsetsSumTok(input, k, start + 1, output); 32 | printSubsetsSumTok(input, k-input[start], start+1, newoutput); 33 | 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Recursion 2/Printallcodes.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.util.*; 3 | public class solution { 4 | 5 | 6 | 7 | public static void printAllPossibleCodes(String input) { 8 | // Write your code here 9 | 10 | printAllPossibleCodes(input, ""); 11 | } 12 | 13 | public static void printAllPossibleCodes(String input, String output) { 14 | if (input.length() == 0) { 15 | System.out.println(output); 16 | return; 17 | } 18 | char firstdigit = getchar(input.charAt(0) - '0'); 19 | int firsttwodigit1 = 0; 20 | if (input.length() >= 2) { 21 | firsttwodigit1 = (input.charAt(0) - '0') * 10 + (input.charAt(1) - '0'); 22 | } 23 | char firsttwodigit = getchar(firsttwodigit1); 24 | printAllPossibleCodes(input.substring(1), output + firstdigit); 25 | if (firsttwodigit1 > 10 && firsttwodigit1 <= 26) { 26 | printAllPossibleCodes(input.substring(2), output + firsttwodigit); 27 | } 28 | 29 | } 30 | 31 | public static char getchar(int n) { 32 | return (char) (96 + n); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Recursion 2/RemoveDuplicate.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | 3 | public static String removeConsecutiveDuplicates(String s) { 4 | // Write your code here 5 | if(s.length()==1) 6 | { 7 | return s ; 8 | } 9 | String myans = removeConsecutiveDuplicates(s.substring(1)) ; 10 | if(!(s.charAt(0)==myans.charAt(0))) 11 | { 12 | String temp = s.charAt(0) + myans ; 13 | myans = temp ; 14 | } 15 | return myans ; 16 | } 17 | 18 | public static void main(String[] args) { 19 | 20 | System.out.println(removeConsecutiveDuplicates("aaabbc")); 21 | 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /Recursion 2/ReplaceCharacter.java: -------------------------------------------------------------------------------- 1 | 2 | public class Solution { 3 | 4 | public static String replaceCharacter(String input, char c1, char c2) { 5 | /* Your class should be named Solution 6 | * Don't write main(). 7 | * Don't read input, it is passed as function argument. 8 | * Return output and don't print it. 9 | * Taking input and printing output is handled automatically. 10 | */ 11 | if(input.length()==1) 12 | { 13 | if(input.charAt(0)==c1) { 14 | return c2+"" ;} 15 | else { 16 | return input ;} 17 | } 18 | String myans = replaceCharacter(input.substring(1), c1, c2) ; 19 | if(input.charAt(0)==c1) 20 | { 21 | String temp = c2 + myans ; 22 | myans = temp ; 23 | } 24 | else 25 | { 26 | String temp = input.charAt(0) + myans ; 27 | myans = temp ; 28 | } 29 | return myans ; 30 | } 31 | 32 | public static void main(String[] args) { 33 | 34 | System.out.println(replaceCharacter("lokilokill", 'l', 't')); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Recursion 2/ReturnPermutation.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class solution { 3 | public static int fact(int n) { 4 | int fact = 1; 5 | for(int i = n;i >= 1;i--) { 6 | fact = fact * i; 7 | } 8 | return fact; 9 | } 10 | 11 | 12 | public static String[] permutationOfString(String input){ 13 | // Write your code here 14 | if(input.length() == 1){ 15 | String[] retVal = {input}; 16 | return retVal; 17 | } 18 | String[] rop = permutationOfString(input.substring(1)); 19 | String[] tp = new String[fact(rop[0].length() + 1)]; 20 | int k = 0; 21 | for(int i = 0;i < rop.length;i++){ 22 | String temp = rop[i]; 23 | for(int j = 0;j <= temp.length();j++,k++) { 24 | tp[k] = temp.substring(0,j) + input.charAt(0) + temp.substring(j); 25 | } 26 | } 27 | return tp; 28 | } 29 | 30 | public static void main(String[] args) { 31 | Scanner s = new Scanner(System.in); 32 | String input = s.next(); 33 | String[] result = permutationOfString(input); 34 | for(String val:result) { 35 | System.out.println(val); 36 | } 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /Recursion 2/ReturnSubsequenceofstring.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class solution { 3 | 4 | // Return the subsequences saved in a string array 5 | public static String[] subsequence(String input){ 6 | // Write your code here 7 | if(input.length() == 0) { 8 | String[] rv = {""}; 9 | return rv; 10 | } 11 | // Wrap the code except (base case) in else to increase readabilty 12 | else { 13 | String[] restSubSequences = subsequence(input.substring(1)); 14 | String[] totalSubSequences = new String[restSubSequences.length * 2]; 15 | int i,j = 0; 16 | for(i = 0;i < restSubSequences.length ;i++) { 17 | totalSubSequences[i] = restSubSequences[i]; 18 | } 19 | for(;i < totalSubSequences.length;i++,j++) { 20 | totalSubSequences[i] = input.charAt(0) + restSubSequences[j]; 21 | } 22 | return totalSubSequences; 23 | } 24 | } 25 | 26 | public static void main(String[] args) { 27 | Scanner s = new Scanner(System.in); 28 | String input = s.next(); 29 | String[] res = subsequence(input); 30 | for(String val:res) { 31 | System.out.println(val); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Recursion 2/ReturnSubsetSumtoK.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class solution { 3 | 4 | // Return a 2D array that contains all the subsets which sum to k 5 | public static int[][] subsetsSumK(int input[], int p) { 6 | // Write your code here 7 | return subsetsSumK(input, p, 0); 8 | 9 | } 10 | 11 | public static int[][] subsetsSumK(int[] input, int p, int start) { 12 | if (start == input.length) { 13 | if (p == 0) { 14 | return new int[1][0]; 15 | } 16 | else { 17 | return new int[0][0]; 18 | } 19 | } 20 | int cn=input[start]; 21 | int[][] smallans1=subsetsSumK(input, p,start+1); 22 | int[][] smallans2=subsetsSumK(input, p-cn, start+1); 23 | 24 | int[][] myans=new int[smallans1.length+smallans2.length][]; 25 | int k=0; 26 | for(int i=0;i= 2) { 18 | firsttwodigits = (input.charAt(0) - '0') * 10 + (input.charAt(1) - '0'); 19 | if (firsttwodigits > 10 && firsttwodigits <= 26) { 20 | smallAns2 = getCode(input.substring(2)); 21 | } 22 | } 23 | String[] myAns = new String[smallAns1.length + smallAns2.length]; 24 | int k = 0; 25 | for (int i = 0; i < smallAns1.length; i++) { 26 | char first = getchar(firstdigit); 27 | myAns[k] = first + smallAns1[i]; 28 | k++; 29 | 30 | } 31 | for (int i = 0; i < smallAns2.length; i++) { 32 | char first = getchar(firsttwodigits); 33 | myAns[k] = first + smallAns2[i]; 34 | k++; 35 | } 36 | return myAns; 37 | 38 | } 39 | 40 | public static char getchar(int n) { 41 | return (char)(96+n); 42 | 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /Recursion 2/StairCase.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Solution { 3 | 4 | public static int findWays(int n){ 5 | 6 | 7 | /*Your class should be named Solution. 8 | *Don't write main(). 9 | *Don't take input, it is passed as function argument. 10 | *Don't print output. 11 | *Taking input and printing output is handled automatically. 12 | */ 13 | if(n == 0){ 14 | return 0; 15 | } 16 | if(n ==1){ 17 | return 1; 18 | } 19 | if(n == 2){ 20 | return 2; 21 | } 22 | if(n == 3){ 23 | return 4; 24 | } 25 | int y = findWays(n - 1) + findWays(n - 2) + findWays(n-3); 26 | return y; 27 | } 28 | 29 | public static void main(String[] args) { 30 | Scanner s = new Scanner(System.in); 31 | System.out.println(findWays(s.nextInt())); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Stack and queue/ReverseQueue.java: -------------------------------------------------------------------------------- 1 | /********** Given Queue Class ************** 2 | // You can directly use these functions 3 | 4 | class Queue { 5 | 6 | public int size(); 7 | 8 | public boolean isEmpty(); 9 | 10 | public T front(); 11 | 12 | public void enqueue(T element); 13 | 14 | public T dequeue(); 15 | } 16 | 17 | **********************************/ 18 | 19 | 20 | 21 | public class Solution { 22 | 23 | public static void reverseQueue(Queue q) { 24 | // Write your code here 25 | if(q.isEmpty()){ 26 | return; 27 | } 28 | int a=q.dequeue(); 29 | reverseQueue(q); 30 | q.enqueue(a); 31 | 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Stack and queue/ReverseStack.java: -------------------------------------------------------------------------------- 1 | import java.util.Stack; 2 | 3 | public class Solution { 4 | 5 | public static void reverseStack(Stack s1, Stack s2) { 6 | int temp; 7 | int counter; 8 | int limiter = s1.size() - 1; 9 | for (int i = 0; i < s1.size() - 1; i++) { 10 | counter = i; 11 | s2.push(s1.pop()); 12 | temp = s2.pop(); 13 | while (counter < limiter) { 14 | s2.push(s1.pop()); 15 | counter++; 16 | } 17 | s1.push(temp); 18 | while (s2.size() != 0) { 19 | s1.push(s2.pop()); 20 | } 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Stack and queue/checkRedundantbrackets.java: -------------------------------------------------------------------------------- 1 | import java.util.Stack; 2 | 3 | public class solution { 4 | public static boolean isRedundant = false; 5 | public static boolean consecutiveStartBrackets = false; 6 | public static boolean checkRedundantBrackets(String input) { 7 | // Write your code here 8 | int i = 0; 9 | Stack stackLL = new Stack<>(); 10 | for (i = 0; i < input.length() - 1; i++) { 11 | if (input.charAt(i) == '(') { 12 | if (input.charAt(i + 1) == ')') { 13 | isRedundant = true; 14 | break; 15 | } else if (input.charAt(i + 1) == '(') { 16 | consecutiveStartBrackets = true; 17 | } 18 | stackLL.push(input.charAt(i)); 19 | } 20 | if (input.charAt(i) == ')') { 21 | if (input.charAt(i + 1) == ')') { 22 | if (consecutiveStartBrackets == true) { 23 | isRedundant = true; 24 | break; 25 | } 26 | } else if (!stackLL.isEmpty() && stackLL.pop() == '(') { 27 | stackLL.pop(); 28 | continue; 29 | } else { 30 | break; 31 | } 32 | } 33 | } 34 | return isRedundant; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Stack and queue/stockspan.java: -------------------------------------------------------------------------------- 1 | import java.util.Stack; 2 | public class Solution { 3 | 4 | public static int[] stockSpan(int[] price) { 5 | // Write your code here 6 | int length=price.length; 7 | int[] S = new int[price.length]; 8 | Stack myStack = new Stack<>(); 9 | int h = 0; 10 | for(int i = 0; i price[myStack.peek()]){ 14 | myStack.pop(); 15 | } else { 16 | break; 17 | } 18 | } 19 | h = myStack.isEmpty() ? -1 : myStack.peek(); 20 | S[i] = i-h; 21 | myStack.push(i); 22 | } 23 | return S; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /String and 2D/CheckPermuation.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class solution { 3 | public static String arrangeString(String inputString) { 4 | int i,j; 5 | String str = ""; 6 | j = 97; 7 | while(j <= 122) { 8 | i = 0; 9 | do { 10 | if((int)inputString.charAt(i) == j) { 11 | str = str + (char)j; 12 | } 13 | i++; 14 | }while(i < inputString.length()); 15 | j++; 16 | } 17 | return str; 18 | 19 | } 20 | public static boolean isPermutation(String input1, String input2) { 21 | // Write your code here 22 | boolean result = true;; 23 | if(input1.length() != input2.length()) 24 | result = false; 25 | else { 26 | String str1 = arrangeString(input1); 27 | String str2 = arrangeString(input2); 28 | for(int i = 0;i < str1.length();i++) { 29 | if(str1.charAt(i) != str2.charAt(i)) { 30 | result = false; 31 | break; 32 | } 33 | } 34 | } 35 | return result; 36 | } 37 | 38 | public static void main(String[] args) { 39 | Scanner s = new Scanner(System.in); 40 | String input1 = s.nextLine(); 41 | String input2 = s.nextLine(); 42 | boolean result = isPermutation(input1,input2); 43 | System.out.print(result); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /String and 2D/CompressString.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Solution { 3 | 4 | public static String compress(String inputString) { 5 | // Write your code here 6 | int i,c = 1,j = 0; 7 | String result = ""; 8 | for(i = 0;i < inputString.length() - 1;i++) { 9 | if(inputString.charAt(i+1) == inputString.charAt(i)) { 10 | c++; 11 | } 12 | else { 13 | if(c != 1) 14 | result = result + inputString.charAt(i) + c; 15 | else 16 | result = result + inputString.charAt(i); 17 | c = 1; 18 | } 19 | } 20 | if(c != 1) 21 | result = result + inputString.charAt(i) + c; 22 | else 23 | result = result + inputString.charAt(i); 24 | return result; 25 | } 26 | 27 | public static void main(String[] args) { 28 | Scanner s = new Scanner(System.in); 29 | String str = s.nextLine(); 30 | String result = compress(str); 31 | System.out.print(result); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /String and 2D/HighestOccuringCharacter.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Scanner; 3 | public class solution { 4 | 5 | public static char highestOccuringCharacter(String inputString) { 6 | // Write your code here 7 | int c1 = 0,c2 = 0,i,j; 8 | char maxChar = '\0'; 9 | j = 0; 10 | while(j < inputString.length()) { 11 | i = 0; 12 | do { 13 | if(inputString.charAt(i) == inputString.charAt(j)) { 14 | c2++; 15 | } 16 | i++; 17 | }while(i < inputString.length()); 18 | if(c2 > c1) { 19 | maxChar = inputString.charAt(j) ; 20 | c1 = c2; 21 | } 22 | c2 = 0; 23 | j++; 24 | } 25 | return maxChar; 26 | } 27 | 28 | public static void main(String[] args) { 29 | Scanner s = new Scanner(System.in); 30 | String inputString = s.nextLine(); 31 | char result = highestOccuringCharacter(inputString); 32 | System.out.print(result); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /String and 2D/RemoveChar.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class solution { 3 | public static int[][] takeInput(){ 4 | Scanner s = new Scanner(System.in); 5 | int rows = s.nextInt(); 6 | int cols = s.nextInt(); 7 | int[][] arr = new int[rows][cols]; 8 | for(int i = 0;i < rows;i++) { 9 | for(int j= 0;j < cols;j++) { 10 | arr[i][j] = s.nextInt(); 11 | } 12 | } 13 | return arr; 14 | } 15 | 16 | public static String removeAllOccurrencesOfChar(String input, char c) { 17 | // Write your code here 18 | String result = ""; 19 | for(int i= 0;i < input.length();i++) { 20 | if(input.charAt(i) == c) 21 | continue; 22 | result += input.charAt(i); 23 | } 24 | return result; 25 | } 26 | 27 | public static void main(String[] args) { 28 | Scanner s = new Scanner(System.in); 29 | String input = s.nextLine(); 30 | char c = s.next().charAt(0); 31 | String result = removeAllOccurrencesOfChar(input,c); 32 | System.out.print(result); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /String and 2D/RemoveConsecutiveDuplicates.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class solution { 3 | 4 | public static String removeConsecutiveDuplicates(String input) { 5 | // Write your code here 6 | String result = ""; 7 | char ch = input.charAt(0); 8 | result += ch; 9 | for(int i = 1;i < input.length();i++) { 10 | if(input.charAt(i) == ch); 11 | else 12 | result += input.charAt(i); 13 | ch = input.charAt(i); 14 | } 15 | return result; 16 | } 17 | 18 | public static void main(String[] args) { 19 | Scanner s = new Scanner(System.in); 20 | String str = s.nextLine(); 21 | String result = removeConsecutiveDuplicates(str); 22 | System.out.print(result); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /String and 2D/ReplaceChar.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Solution { 3 | 4 | public static String replaceCharacter(String input, char c1, char c2) { 5 | /* Your class should be named Solution 6 | * Don't write main(). 7 | * Don't read input, it is passed as function argument. 8 | * Return output and don't print it. 9 | * Taking input and printing output is handled automatically. 10 | */ 11 | String str = ""; 12 | for(int i = 0;i < input.length();i++) { 13 | if(input.charAt(i) == c1) { 14 | str += c2; 15 | }else { 16 | str += input.charAt(i); 17 | } 18 | } 19 | return str; 20 | } 21 | 22 | public static void main(String[] args) { 23 | Scanner s = new Scanner(System.in); 24 | String str = s.nextLine(); 25 | char c1 = s.next().charAt(0); 26 | char c2 = s.next().charAt(0); 27 | String str2 = replaceCharacter(str,c1,c2); 28 | System.out.print(str2); 29 | } 30 | 31 | } 32 | 33 | -------------------------------------------------------------------------------- /String and 2D/ReverseEachWord.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Solution { 3 | 4 | // Return the reversed string. No need to print 5 | public static String reverseEachWord(String input) { 6 | // Write your code here 7 | String reverseWord = "",result = ""; 8 | int start = 0,j,end; 9 | for(int i= 0;i < input.length();i++) { 10 | if(input.charAt(i) == ' ') { 11 | end = i - 1; 12 | reverseWord = ""; 13 | for(j = end;j >= start;j--) { 14 | reverseWord += input.charAt(j); 15 | } 16 | start = i + 1; 17 | result = result + reverseWord + " "; 18 | } 19 | } 20 | reverseWord = ""; 21 | for(j = input.length() - 1;j >= start;j--) { 22 | reverseWord += input.charAt(j); 23 | } 24 | result = result + reverseWord + " "; 25 | return result; 26 | } 27 | 28 | public static void main(String[] args) { 29 | Scanner s = new Scanner(System.in); 30 | String str = s.nextLine(); 31 | String str1 = reverseEachWord(str); 32 | System.out.print(str1); 33 | 34 | 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /String and 2D/ReverseEachWord1.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Solution { 3 | 4 | // Return the reversed string. No need to print 5 | public static String reverseEachWord(String input) { 6 | // Write your code here 7 | String reverseWord = "",result = ""; 8 | int start = 0,j,end; 9 | for(int i= 0;i < input.length();i++) { 10 | if(input.charAt(i) == ' ') { 11 | end = i - 1; 12 | reverseWord = ""; 13 | for(j = end;j >= start;j--) { 14 | reverseWord += input.charAt(j); 15 | } 16 | start = i + 1; 17 | result = result + reverseWord + " "; 18 | } 19 | } 20 | reverseWord = ""; 21 | for(j = input.length() - 1;j >= start;j--) { 22 | reverseWord += input.charAt(j); 23 | } 24 | result = result + reverseWord + " "; 25 | return result; 26 | } 27 | 28 | public static void main(String[] args) { 29 | Scanner s = new Scanner(System.in); 30 | String str = s.nextLine(); 31 | String str1 = reverseEachWord(str); 32 | System.out.print(str1); 33 | 34 | 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /String and 2D/ReverseStringWordWise.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Solution { 3 | public static String reverseWordWise(String input) { 4 | // Write your code here 5 | int end = input.length() - 1,start,j; 6 | String result ="",word = ""; 7 | for(int i= input.length() - 1;i >= 0;i--) { 8 | if(input.charAt(i) == ' ') { 9 | start = i + 1; 10 | word = ""; 11 | for(j = start;j <= end;j++) { 12 | word += input.charAt(j); 13 | } 14 | end = i - 1; 15 | result = result + word + " "; 16 | } 17 | } 18 | word = ""; 19 | for(j = 0;j <= end;j++) { 20 | word = word + input.charAt(j); 21 | } 22 | result += word; 23 | return result; 24 | } 25 | 26 | public static void main(String[] args) { 27 | Scanner s = new Scanner(System.in); 28 | String str = s.nextLine(); 29 | String str1 = reverseWordWise(str); 30 | System.out.print(str1); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /String and 2D/StringPalindrome.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Solution { 3 | 4 | public static boolean checkPalindrome(String str){ 5 | /* Your class should be named Solution 6 | * Don't write main(). 7 | * Don't read input, it is passed as function argument. 8 | * Return output and don't print it. 9 | * Taking input and printing output is handled automatically. 10 | */ 11 | boolean isPalindrome = true; 12 | for(int i = 0,j = str.length() - 1;i < (str.length()/2);i++,j--){ 13 | if(str.charAt(i) != str.charAt(j)){ 14 | isPalindrome = false; 15 | break; 16 | } 17 | } 18 | return isPalindrome; 19 | } 20 | 21 | public static void main(String[] args) { 22 | Scanner s = new Scanner(System.in); 23 | String str = s.nextLine(); 24 | boolean isPalindrome = checkPalindrome(str); 25 | System.out.print(isPalindrome); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /String and 2D/allsubstringsofaString.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Solution { 3 | 4 | public static void printSubstrings(String str){ 5 | 6 | /* Your class should be named Solution 7 | * Don't write main(). 8 | * Don't read input, it is passed as function argument. 9 | * Print output as specified in the question. 10 | */ 11 | for(int i = 0; i < str.length();i++) { 12 | for(int j = i+1 ;j <= str.length();j++) { 13 | System.out.println(str.substring(i,j)); 14 | } 15 | } 16 | } 17 | 18 | public static void main(String[] args) { 19 | Scanner s = new Scanner(System.in); 20 | String str = s.nextLine(); 21 | printSubstrings(str); 22 | } 23 | 24 | 25 | } 26 | -------------------------------------------------------------------------------- /String and 2D/countWords.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Solution { 3 | 4 | public static int countWords(String str){ 5 | 6 | /* Your class should be named Solution 7 | * Don't write main(). 8 | * Don't read input, it is passed as function argument. 9 | * Return output and don't print it. 10 | * Taking input and printing output is handled automatically. 11 | */ 12 | int noOfWords = 1; 13 | for(int i = 0;i < str.length();i++) { 14 | if(str.charAt(i) == ' ') 15 | noOfWords++; 16 | } 17 | return noOfWords; 18 | } 19 | 20 | public static void main(String[] args) { 21 | Scanner s= new Scanner(System.in); 22 | String str = s.nextLine(); 23 | int noOfWords = countWords(str); 24 | System.out.print(noOfWords); 25 | } 26 | 27 | 28 | } 29 | -------------------------------------------------------------------------------- /String and 2D/minimunLengthword.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Solution { 3 | 4 | public static String minLengthWord(String input){ 5 | 6 | // Write your code here 7 | int start = 0,end,j,c = 0,d,i = 0,k; 8 | String str = "",result = ""; 9 | for(i= 0;i < input.length();i++) { 10 | if(input.charAt(i) == ' ') { 11 | end = i - 1; 12 | for(j = start; j <= end;j++) 13 | { 14 | str += input.charAt(j); 15 | 16 | } 17 | result = str; 18 | start = i+1; 19 | break; 20 | } 21 | } 22 | for(k = i+1;k < input.length();k++) { 23 | if(input.charAt(k) == ' ') { 24 | end = k - 1; 25 | str = ""; 26 | for(j = start; j <= end;j++) 27 | { 28 | str += input.charAt(j); 29 | 30 | } 31 | if(str.length() < result.length()) 32 | result = str; 33 | start = k + 1; 34 | } 35 | } 36 | str = ""; 37 | for(k = start;k < input.length();k++) { 38 | str += input.charAt(k); 39 | } 40 | if(str.length() < result.length()) 41 | result = str; 42 | return result; 43 | } 44 | 45 | public static void main(String[] args) { 46 | Scanner s = new Scanner(System.in); 47 | String str = s.nextLine(); 48 | String result = minLengthWord(str); 49 | System.out.print(result); 50 | } 51 | } 52 | 53 | -------------------------------------------------------------------------------- /String and 2D/rowWiseSum.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Solution { 3 | public static int[][] takeInput(){ 4 | Scanner s = new Scanner(System.in); 5 | int rows = s.nextInt(); 6 | int cols = s.nextInt(); 7 | int[][] arr = new int[rows][cols]; 8 | for(int i = 0;i < rows;i++) { 9 | for(int j= 0;j < cols;j++) { 10 | arr[i][j] = s.nextInt(); 11 | } 12 | } 13 | return arr; 14 | } 15 | 16 | public static void main(String[] args) { 17 | /* Your class should be named Solution. 18 | * Read input as specified in the question. 19 | * Print output as specified in the question. 20 | */ 21 | int[][] arr =takeInput(); 22 | int rSum,i,j; 23 | for(i = 0;i < arr.length;i++) { 24 | rSum = 0; 25 | for(j = 0;j < arr[i].length;j++) { 26 | rSum += arr[i][j]; 27 | } 28 | System.out.print(rSum + " "); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /String and 2D/wavePrint.java: -------------------------------------------------------------------------------- 1 | 2 | public class Solution { 3 | 4 | 5 | // input - input 2D array 6 | public static void wavePrint(int input[][]){ 7 | 8 | // Write your code here 9 | for (int i = 0; i < input[0].length; i++) { 10 | if (i % 2 == 0) { 11 | for (int j = 0; j <=input.length-1; j++) { 12 | System.out.print(input[j][i]+" "); 13 | } 14 | } 15 | else if (i % 2 != 0) { 16 | for (int j = input.length - 1; j >= 0; j--) { 17 | System.out.print(input[j][i]+" "); 18 | } 19 | } 20 | } 21 | 22 | } 23 | 24 | 25 | public static void main(String[] args) { 26 | int[][] arr = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}; 27 | wavePrint(arr); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Test 1/NumberStarPattern2.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Scanner; 3 | public class Solution { 4 | 5 | public static void main(String[] args) { 6 | 7 | // Write your code here 8 | Scanner s = new Scanner(System.in); 9 | int n = s.nextInt(); 10 | int n1=n,n2=n-1,n3=n; 11 | for(int i = n;i>=1;i--){ 12 | for(int j=1;j<=n1;j++){ 13 | System.out.print(j); 14 | } 15 | for(int j = n2;j < n-1;j++){ 16 | System.out.print("**"); 17 | } 18 | for(int j = n3;j >= 1;j--){ 19 | System.out.print(j); 20 | 21 | } 22 | n1--; 23 | n2--; 24 | n3--; 25 | System.out.println(); 26 | } 27 | 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Test 1/SecondLargest.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | // Write your code here 7 | Scanner s = new Scanner(System.in); 8 | int max,max1,t; 9 | int j =0; 10 | int n = s.nextInt(); 11 | if(n==0){ 12 | System.out.println(Integer.MIN_VALUE); 13 | return; 14 | } 15 | int a = s.nextInt(); 16 | if(n==1){ 17 | System.out.println(Integer.MIN_VALUE); 18 | return; 19 | } 20 | int b = s.nextInt(); 21 | if(a max) 31 | { 32 | max1 = max; 33 | max = i; 34 | } 35 | else if(i > max1 && i != max) 36 | { 37 | max1 = i; 38 | } 39 | else; 40 | j++; 41 | } 42 | if(max1 == max) 43 | System.out.print(Integer.MIN_VALUE); 44 | else 45 | System.out.print(max1); 46 | 47 | } 48 | } 49 | 50 | -------------------------------------------------------------------------------- /Test 1/pyramidNumberPattern.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 s = new Scanner(System.in); 7 | int n = s.nextInt(); 8 | int i,j,n1 = n-1,n2 = 1,n3 = 1; 9 | for(i = 1;i <= n;i++){ 10 | for(j = n1;j > 0;j--){ 11 | System.out.print(" "); 12 | } 13 | for(j = n2;j >= 2;j--){ 14 | System.out.print(j); 15 | } 16 | for(j = 1;j <= n3;j++){ 17 | System.out.print(j); 18 | } 19 | n1--; 20 | n2++; 21 | n3++; 22 | System.out.println(); 23 | } 24 | 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Tree/CountNODELeaf.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | 3 | /* TreeNode structure 4 | * 5 | * class TreeNode { 6 | T data; 7 | ArrayList> children; 8 | 9 | TreeNode(T data){ 10 | this.data = data; 11 | children = new ArrayList>(); 12 | } 13 | }*/ 14 | public static int count = 0; 15 | public static int countLeafNodes(TreeNode root){ 16 | 17 | // Write your code here 18 | if (root.children.size() == 0) { 19 | count++; 20 | } 21 | for (int i = 0; i < root.children.size(); i++) { 22 | countLeafNodes(root.children.get(i)); 23 | } 24 | return count; 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /Tree/ReplaceNodeWithDepth.java: -------------------------------------------------------------------------------- 1 | import java.util.Queue; 2 | import java.util.LinkedList; 3 | public class Solution { 4 | 5 | /* TreeNode structure 6 | * 7 | * class TreeNode { 8 | T data; 9 | ArrayList> children; 10 | 11 | TreeNode(T data){ 12 | this.data = data; 13 | children = new ArrayList>(); 14 | } 15 | }*/ 16 | 17 | public static void printLevelWise(TreeNode root){ 18 | Queue> pendingNodes = new LinkedList<>(); 19 | pendingNodes.add(root); 20 | while (!pendingNodes.isEmpty()) { 21 | TreeNode frontNode = null; 22 | int counter = pendingNodes.size(); 23 | for (int i = 0; i < counter; i++) { 24 | // try { 25 | // frontNode = pendingNodes.remove(); 26 | // } catch (QueueEmptyException e) { 27 | // e.printStackTrace(); 28 | // } 29 | System.out.print(frontNode.data + " "); 30 | for (int j = 0; j < frontNode.children.size(); j++) { 31 | pendingNodes.add(frontNode.children.get(j)); 32 | } 33 | } 34 | System.out.println(); 35 | } 36 | } 37 | 38 | public static void replaceWithDepthValue(TreeNode root) { 39 | replaceWithDepthValue(root, 0); 40 | } 41 | 42 | public static void replaceWithDepthValue(TreeNode root, int depth) { 43 | if (depth == 0) { 44 | root.data = depth; 45 | } 46 | root.data = depth; 47 | for (int i = 0; i < root.children.size(); i++) { 48 | replaceWithDepthValue(root.children.get(i), depth + 1); 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /Tree/countleafnode.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | 3 | /* TreeNode structure 4 | * 5 | * class TreeNode { 6 | T data; 7 | ArrayList> children; 8 | 9 | TreeNode(T data){ 10 | this.data = data; 11 | children = new ArrayList>(); 12 | } 13 | }*/ 14 | public static int count = 0; 15 | public static int countLeafNodes(TreeNode root){ 16 | 17 | // Write your code here 18 | if (root.children.size() == 0) { 19 | count++; 20 | } 21 | for (int i = 0; i < root.children.size(); i++) { 22 | countLeafNodes(root.children.get(i)); 23 | } 24 | return count; 25 | } 26 | } 27 | 28 | 29 | -------------------------------------------------------------------------------- /Tree/countnumberofnodes.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | 3 | /* TreeNode structure 4 | * 5 | * class TreeNode { 6 | T data; 7 | ArrayList> children; 8 | 9 | TreeNode(T data){ 10 | this.data = data; 11 | children = new ArrayList>(); 12 | } 13 | }*/ 14 | 15 | 16 | 17 | public static int countNodes(TreeNode root){ 18 | if(root == null){ 19 | return 0; 20 | } 21 | 22 | int ans=1; 23 | for(int i = 0;i { 6 | T data; 7 | ArrayList> children; 8 | 9 | TreeNode(T data){ 10 | this.data = data; 11 | children = new ArrayList>(); 12 | } 13 | }*/ 14 | public static boolean isPresent = false; 15 | 16 | public static boolean checkIfContainsX(TreeNode root, int x){ 17 | 18 | // Write your code here 19 | if (root == null) { 20 | isPresent = false; 21 | } else if (root.data == x) { 22 | return true; 23 | } 24 | for (int i = 0; i < root.children.size(); i++) { 25 | isPresent = checkIfContainsX(root.children.get(i), x); 26 | } 27 | return isPresent; 28 | } 29 | 30 | } 31 | 32 | -------------------------------------------------------------------------------- /Tree/identical.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | 3 | /* TreeNode structure 4 | * 5 | * class TreeNode { 6 | T data; 7 | ArrayList> children; 8 | 9 | TreeNode(T data){ 10 | this.data = data; 11 | children = new ArrayList>(); 12 | } 13 | }*/ 14 | public static boolean isIdentical = true; 15 | public static boolean checkIdentical(TreeNode root1, TreeNode root2){ 16 | 17 | // Write your code here 18 | if (root1.data != root2.data || root1.children.size() != root2.children.size()) { 19 | return false; 20 | } 21 | QueueUsingLL> pendingNodes1 = new QueueUsingLL<>(); 22 | for (int i = 0; i < root1.children.size(); i++) { 23 | pendingNodes1.enqueue(root1.children.get(i)); 24 | } 25 | QueueUsingLL> pendingNodes2 = new QueueUsingLL<>(); 26 | for (int i = 0; i < root2.children.size(); i++) { 27 | pendingNodes2.enqueue(root2.children.get(i)); 28 | } 29 | if (!pendingNodes1.isEmpty() && !pendingNodes2.isEmpty()) { 30 | try { 31 | isIdentical = checkIdentical(pendingNodes1.front(), pendingNodes2.front()); 32 | } catch (QueueEmptyException e) { 33 | e.printStackTrace(); 34 | } 35 | } 36 | return isIdentical; 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /Tree/maxdatanode.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | 3 | /* TreeNode structure 4 | * 5 | * class TreeNode { 6 | T data; 7 | ArrayList> children; 8 | 9 | TreeNode(T data){ 10 | this.data = data; 11 | children = new ArrayList>(); 12 | } 13 | }*/ 14 | 15 | 16 | 17 | public static TreeNode maxDataNode(TreeNode root){ 18 | /* Your class should be named Solution 19 | * Don't write main(). 20 | * Don't read input, it is passed as function argument. 21 | * Return output and don't print it. 22 | * Taking input and printing output is handled automatically. 23 | */ 24 | if(root==null){ 25 | return null; 26 | } 27 | TreeNode maxnode = root ; 28 | int max=root.data; 29 | for (int i =0;i childmax = maxDataNode(root.children.get(i)); 31 | if(childmax.data >= max){ 32 | max= childmax.data; 33 | maxnode = childmax; 34 | } 35 | 36 | } 37 | return maxnode; 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /Tree/nextLargerElement.java: -------------------------------------------------------------------------------- 1 | import java.util.Queue; 2 | import java.util.LinkedList; 3 | public class Solution { 4 | 5 | /* TreeNode structure 6 | * 7 | * class TreeNode { 8 | T data; 9 | ArrayList> children; 10 | 11 | TreeNode(T data){ 12 | this.data = data; 13 | children = new ArrayList>(); 14 | } 15 | }*/ 16 | public static TreeNode ans; 17 | public static TreeNode temp = null; 18 | public static Queue> pendingNodes = new LinkedList<>(); 19 | public static TreeNode findNextLargerNode(TreeNode root, int n){ 20 | 21 | // Write your code here 22 | if (ans == null) { 23 | if (root.data > n) { 24 | ans = root; 25 | } 26 | } else { 27 | if (root.data < ans.data && root.data > n) { 28 | ans = root; 29 | } 30 | } 31 | for (int i = 0; i < root.children.size(); i++) { 32 | pendingNodes.add(root.children.get(i)); 33 | try { 34 | temp = findNextLargerNode(pendingNodes.remove(), n); 35 | if (temp.data > n) { 36 | if (temp.data < ans.data) { 37 | ans = temp; 38 | } 39 | } 40 | 41 | } catch (Exception e) { 42 | e.printStackTrace(); 43 | } 44 | } 45 | return ans; 46 | } 47 | 48 | } 49 | 50 | -------------------------------------------------------------------------------- /Tree/nodesatdepthk.java: -------------------------------------------------------------------------------- 1 | 2 | public class Solution { 3 | 4 | /* TreeNode structure 5 | * 6 | * class TreeNode { 7 | T data; 8 | ArrayList> children; 9 | 10 | TreeNode(T data){ 11 | this.data = data; 12 | children = new ArrayList>(); 13 | } 14 | }*/ 15 | 16 | 17 | 18 | public static void printNodesAtDepthK(TreeNode root, int k){ 19 | /* Your class should be named Solution 20 | * Don't write main(). 21 | * Don't read input, it is passed as function argument. 22 | * Print output and don't return it. 23 | * Taking input is handled automatically. 24 | */ 25 | if (k < 0) { 26 | return; 27 | } else if (k == 0) { 28 | System.out.print(root.data+" "); 29 | return; 30 | } 31 | for (int i = 0; i < root.children.size(); i++) { 32 | printNodesAtDepthK(root.children.get(i), k - 1); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Tree/nodesgreaterthanX.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | 3 | /* TreeNode class 4 | * 5 | * class TreeNode { 6 | T data; 7 | ArrayList> children; 8 | 9 | TreeNode(T data){ 10 | this.data = data; 11 | children = new ArrayList>(); 12 | } 13 | }*/ 14 | public static int count = 0; 15 | 16 | 17 | public static int numNodeGreater(TreeNode root,int x){ 18 | 19 | // Write your code here 20 | if (root == null) { 21 | return 0; 22 | } else if (root.data > x) { 23 | count++; 24 | } 25 | for (int i = 0; i < root.children.size(); i++) { 26 | numNodeGreater(root.children.get(i), x); 27 | } 28 | return count; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /Tree/numberofnodesgreaterthanX.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | 3 | /* TreeNode class 4 | * 5 | * class TreeNode { 6 | T data; 7 | ArrayList> children; 8 | 9 | TreeNode(T data){ 10 | this.data = data; 11 | children = new ArrayList>(); 12 | } 13 | }*/ 14 | 15 | public static int count = 0; 16 | public static int numNodeGreater(TreeNode root,int x){ 17 | 18 | // Write your code here 19 | if (root == null) { 20 | return 0; 21 | } else if (root.data > x) { 22 | count++; 23 | } 24 | for (int i = 0; i < root.children.size(); i++) { 25 | numNodeGreater(root.children.get(i), x); 26 | } 27 | return count; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /Tree/printtreelevelwise.java: -------------------------------------------------------------------------------- 1 | import java.util.Queue; 2 | import java.util.LinkedList; 3 | public class Solution { 4 | 5 | /* TreeNode structure 6 | * 7 | * class TreeNode { 8 | T data; 9 | ArrayList> children; 10 | 11 | TreeNode(T data){ 12 | this.data = data; 13 | children = new ArrayList>(); 14 | } 15 | }*/ 16 | 17 | public static void printLevelWise(TreeNode root){ 18 | /* Your class should be named Solution. 19 | * Don't write main() function. 20 | * Don't read input, it is passed as function argument. 21 | * Print output as specified in the question 22 | */ 23 | Queue> q = new LinkedList<>(); 24 | q.add(root); 25 | while(!q.isEmpty()){ 26 | TreeNodefront=q.remove(); 27 | if(front!=null){ 28 | System.out.print(front.data+":"); 29 | for(int i=0;i { 7 | T data; 8 | ArrayList> children; 9 | 10 | TreeNode(T data){ 11 | this.data = data; 12 | children = new ArrayList>(); 13 | } 14 | }*/ 15 | 16 | 17 | 18 | public static int sumOfAllNode(TreeNode root){ 19 | if(root==null){ 20 | return 0; 21 | } 22 | // Write your code here 23 | int nodes = root.data; 24 | for(int i = 0;i { 7 | T data; 8 | ArrayList> children; 9 | 10 | TreeNode(T data){ 11 | this.data = data; 12 | children = new ArrayList>(); 13 | } 14 | }*/ 15 | public static TreeNode ans; 16 | public static int maxSum = 0; 17 | 18 | public static TreeNode maxSumNode(TreeNode root){ 19 | // Write your code here 20 | if (root.children.size() == 0) { 21 | return root; 22 | } 23 | int sum = root.data; 24 | for (int i = 0; i < root.children.size(); i++) { 25 | sum += root.children.get(i).data; 26 | } 27 | if (sum > maxSum) { 28 | maxSum = sum; 29 | ans = root; 30 | } 31 | for (int i = 0; i < root.children.size(); i++) { 32 | if (maxSumNode(root.children.get(i)).data > maxSum) { 33 | maxSum = maxSumNode(root.children.get(i)).data; 34 | ans = root.children.get(i); 35 | } 36 | } 37 | return ans; 38 | } 39 | 40 | 41 | } 42 | -------------------------------------------------------------------------------- /trie/patternmatching.java: -------------------------------------------------------------------------------- 1 | // #include "TrieNode.h" 2 | #include 3 | #include 4 | class TrieNode { 5 | public : 6 | char data; 7 | TrieNode **children; 8 | bool isTerminal; 9 | 10 | TrieNode(char data) { 11 | this -> data = data; 12 | children = new TrieNode*[26]; 13 | for(int i = 0; i < 26; i++) { 14 | children[i] = NULL; 15 | } 16 | isTerminal = false; 17 | } 18 | }; 19 | 20 | class Trie { 21 | TrieNode *root; 22 | 23 | public : 24 | int count; 25 | 26 | Trie() { 27 | this->count = 0; 28 | root = new TrieNode('\0'); 29 | } 30 | 31 | bool insertWord(TrieNode *root, string word) { 32 | // Base case 33 | if(word.size() == 0) { 34 | if (!root->isTerminal) { 35 | root -> isTerminal = true; 36 | return true; 37 | } else { 38 | return false; 39 | } 40 | } 41 | 42 | // Small Calculation 43 | int index = word[0] - 'a'; 44 | TrieNode *child; 45 | if(root -> children[index] != NULL) { 46 | child = root -> children[index]; 47 | } 48 | else { 49 | child = new TrieNode(word[0]); 50 | root -> children[index] = child; 51 | } 52 | 53 | // Recursive call 54 | return insertWord(child, word.substr(1)); 55 | } 56 | 57 | // For user 58 | void insertWord(string word) { 59 | if (insertWord(root, word)) { 60 | this->count++; 61 | } 62 | } 63 | 64 | bool patternMatching(vector vect, string pattern) { 65 | // Complete this function 66 | // Return true or false 67 | 68 | } 69 | }; 70 | --------------------------------------------------------------------------------