├── Data Strucutures In java ├── Lecture 17 DP 2 │ ├── New Text Document.txt │ ├── Edit Distance Recursive.txt │ ├── CodingNinjas Solutions │ │ └── 9. All Possible Ways.txt │ └── LCSMemoization.txt ├── Lecture 11 Binary Trees │ ├── Assignments │ │ ├── New Text Document.txt │ │ └── sumOfNodes.txt │ ├── Pair.java │ ├── BinaryTreeNode (2).java │ ├── BinaryTreeNode.java │ ├── BinaryTreeUse.java │ ├── Exercises │ │ ├── printTreeLevelWise.txt │ │ ├── postOrderBinaryTree.txt │ │ ├── preOrderBinaryTree.txt │ │ └── mirrorofTree.txt │ └── BinaryTreeUse (2).java ├── Test 3 │ ├── CheckCousins.java │ └── LongestRootToLeafPath.java ├── Lecture 13 Hash Maps │ ├── OurHashMap Implementation │ │ ├── MapNode.java │ │ └── OurMapUse.java │ ├── RemoveDuplicates.java │ ├── MapUse.java │ └── Assignments │ │ └── ExtractUniqueCharacters.java ├── Lecture 14 Priority Queues │ ├── PriorityQueueUse.java │ ├── sortKSorted.java │ ├── kLargestElememts.java │ ├── Assignments │ │ └── checkMaxHeap.java │ ├── CodingNinjasSolutions │ │ ├── checkMaxHeap.txt │ │ ├── kLargestElements.txt │ │ └── kthLargestElement.txt │ └── StringLength.java ├── Lecture 12 Binary Search Trees │ ├── BinarySearchTree Class │ │ └── Driver.java │ └── Exercises │ │ ├── SortedArrayToBst.java │ │ ├── printElementsInRange.txt │ │ └── searchNodeinBst.txt ├── Lecture 20 Oops - 4 │ └── Player.java ├── Lecture 3 Recursion 1 │ ├── Sum of digits (recursive).txt │ ├── Number of Digits.txt │ ├── Geometric Sum.txt │ ├── Print Numbers.txt │ ├── Multiplication (Recursive).txt │ ├── Check Palindrome (recursive).txt │ ├── Check Number in Array.txt │ ├── Count Zeros.txt │ ├── Sum of Array.txt │ ├── First Index of Number.txt │ ├── Calculate Power.txt │ ├── Last Index of Number.txt │ └── All Indices of Number.txt ├── Assignment Recursion 1 │ ├── Remove X.txt │ ├── Pair Star.txt │ ├── Replace pi (recursive).txt │ ├── String to Integer.txt │ └── Tower of Hanoi.txt ├── Lecture 15 Tries and Huffman Coding │ └── TrieUse.java ├── Lecture 4 Recursion 2 │ ├── Remove Duplicates Recursively.txt │ ├── Replace Character Recursively.txt │ ├── Staircase.txt │ ├── Print Permutations.txt │ ├── Binary Search (Recursive).txt │ ├── Return Permutations - String.txt │ ├── Check AB.txt │ ├── Print all Codes - String.txt │ ├── Print Subsets of Array.txt │ └── Merge Sort Code.txt ├── Lecture 16 DP 1 │ └── Coding Ninjas Solutions │ │ ├── StairCase.txt │ │ ├── Code Number of Balanced BTs.txt │ │ ├── Code Number of Balanced BTs Using DP.txt │ │ ├── Code Min Steps to One - DP.txt │ │ ├── Code Min Steps to One.txt │ │ └── Code Minimum Count of Squares.txt ├── Lecture 5 Time and Complexity Analysis │ ├── Find the Unique Element.txt │ ├── Duplicate in array.txt │ ├── Check Array Rotation.txt │ └── Pair sum in array.txt ├── Test 1 │ ├── Does s contain t.txt │ └── Maximum Profit on App.txt ├── Lecture 7 Linked List 1 │ └── Length of LL.txt ├── Lecture 9 Stacks and Queues │ ├── Reverse a Stack.txt │ └── Reverse Queue.txt └── Test 2 │ └── Delete Alternate Nodes.txt ├── Introduction to Java ├── Lecture 12, Strings │ ├── New Text Document.txt │ ├── PrintAllChars.java │ ├── InputStrings.java │ ├── IntroductionToStrings.java │ ├── Exercises │ │ ├── AllSubStringsOfString.java │ │ ├── CountWords.java │ │ └── ReverseStringWordWise.java │ ├── MoreOnStrings.java │ └── Assignments │ │ ├── RemoveCharcater.java │ │ └── RemoveConsecutiveDuplicates.java ├── Lecture 5, Patterns 1 │ ├── lecture5CharacterPattern │ │ ├── package-info.java │ │ ├── BasicPattern1.java │ │ └── Basicpattern2.java │ ├── SquarePatterns1.java │ ├── SquarePatterns.java │ ├── lecture5traingularpatterns │ │ ├── BasicPattern1.java │ │ ├── BasicPattern2.java │ │ └── BasicPattern3.java │ ├── BasicPatterns.java │ ├── ReversePattern.java │ └── lectureAssignmentsExercises │ │ ├── ReverseNumberPattern.java │ │ ├── TriangleNumberPattern.java │ │ ├── SquarePattern.java │ │ ├── AlphaPattern.java │ │ ├── TriangularStarPattern.java │ │ ├── InterestingAlphabets.java │ │ └── CharacterPattern.java ├── Lecture 10 Arrays 2, Searching and Sorting │ ├── Assignments │ │ ├── test.java │ │ ├── RotateArrayLeftdTimesFirst.java │ │ ├── RotateArrayLeftdTimesOptimized.java │ │ └── CheckArrayRotation.java │ └── Exercises │ │ ├── InsertionSort.java │ │ ├── BubbleSort.java │ │ ├── SelectionSort.java │ │ └── MergeSort.java ├── Lecture 4, Conditionals and Loops │ ├── Assignments │ │ ├── TotalSalary.java │ │ ├── MultiplicationTable.java │ │ ├── Factors.java │ │ ├── PowerOfNumber.java │ │ └── EvenOddSum.java │ ├── SumofNNumbers.java │ ├── FindCharacterCase.java │ └── CheckPrimality.java ├── Lecture 7, Operators and For Loop │ ├── Assignments │ │ ├── CheckNumberSequence.java │ │ ├── SquareRoot.java │ │ ├── TermsOfAp.java │ │ ├── BinaryToDecima.java │ │ ├── ReverseOfANumber.java │ │ ├── DecimalToBinary.java │ │ └── SumOrProduct.java │ ├── Break.java │ ├── Continue.java │ ├── Return.java │ └── Exercises │ │ ├── NthFibonacciNumber.java │ │ └── AllPrimeNumners.java ├── Test 2 │ ├── Important Notes and Answers.txt │ ├── NumberPattern.java │ ├── MaximumNumber.java │ └── ProbabilityOfBalls.java ├── Lecture 2, Getting Started with Java │ ├── HelloWorld.java │ ├── DataTypes.java │ ├── InputOutput.java │ ├── Exercises │ │ └── AverageMarks.java │ └── InputOutputExample.java ├── Lecure 6, Patterns 2 │ ├── InvertedTraingle.java │ ├── MirrorImagePattern.java │ ├── IsoscelesTrainglePattern.java │ ├── Exercises │ │ ├── InvertedNumberPattern.java │ │ ├── MirrorImageNumberPattern.java │ │ ├── StarPattern.java │ │ ├── TraingleOfNumbers.java │ │ └── DiamondOfStars.java │ └── Assignments │ │ ├── ParallelogramPattern.java │ │ ├── OddSquare.java │ │ └── SumPattern.java ├── Lecture 11, 2D Arrays │ ├── Storing 2d Arrays.txt │ ├── Types of 2d Arrays.txt │ ├── InputOf2DArrays.java │ ├── InputOutputFunctionArrays.java │ └── LargestColumnSum.java ├── Lecture 8, Functions and Scope │ ├── FactorialNumber.java │ ├── EvenNumbersBetween2Numbers.java │ └── Exercises │ │ └── FibonacciNumber.java ├── Test 1 │ ├── NumberStarPattern.java │ ├── Checkpalindrome.java │ └── CheckAp.java ├── Lecture 9 Arrays 1 │ ├── InputOfArray.java │ ├── Exercises │ │ ├── SwapAlternate.java │ │ └── ArraySum.java │ ├── Basic Arrays.txt │ ├── PrintAllPairs.java │ ├── InputFunctionArrays.java │ ├── LargestElement.java │ ├── Arrays Storage.txt │ └── Assignments │ │ ├── Sort01.java │ │ └── PairSum.java ├── Lecture 13, Time Complexity │ └── Exercises │ │ └── Simple Exercises.txt ├── Course Test 1 │ └── RectangularNumbers.java └── Test 3 │ ├── FindLeadersInArray.java │ └── MinimumLengthWord.java └── README.md /Data Strucutures In java/Lecture 17 DP 2/New Text Document.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Introduction to Java/Lecture 12, Strings/New Text Document.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Data Strucutures In java/Lecture 11 Binary Trees/Assignments/New Text Document.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Data Strucutures In java/Lecture 11 Binary Trees/Pair.java: -------------------------------------------------------------------------------- 1 | 2 | public class Pair { 3 | public T first; 4 | public V second; 5 | 6 | } 7 | -------------------------------------------------------------------------------- /Data Strucutures In java/Test 3/CheckCousins.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kumasumit/CodingNinjas_Java_DSA_Premium/HEAD/Data Strucutures In java/Test 3/CheckCousins.java -------------------------------------------------------------------------------- /Introduction to Java/Lecture 5, Patterns 1/lecture5CharacterPattern/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | /** 5 | * @author KSumit 6 | * 7 | */ 8 | package lecture5CharacterPattern; -------------------------------------------------------------------------------- /Introduction to Java/Lecture 10 Arrays 2, Searching and Sorting/Assignments/test.java: -------------------------------------------------------------------------------- 1 | public class test{ 2 | public static void main(String[] args) { 3 | int a = 5; 4 | System.out.println(a); 5 | } 6 | } -------------------------------------------------------------------------------- /Introduction to Java/Lecture 4, Conditionals and Loops/Assignments/TotalSalary.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kumasumit/CodingNinjas_Java_DSA_Premium/HEAD/Introduction to Java/Lecture 4, Conditionals and Loops/Assignments/TotalSalary.java -------------------------------------------------------------------------------- /Introduction to Java/Lecture 7, Operators and For Loop/Assignments/CheckNumberSequence.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kumasumit/CodingNinjas_Java_DSA_Premium/HEAD/Introduction to Java/Lecture 7, Operators and For Loop/Assignments/CheckNumberSequence.java -------------------------------------------------------------------------------- /Data Strucutures In java/Lecture 11 Binary Trees/BinaryTreeNode (2).java: -------------------------------------------------------------------------------- 1 | 2 | public class BinaryTreeNode { 3 | 4 | public BinaryTreeNode(T data) { 5 | // TODO Auto-generated constructor stub 6 | this.data = data; 7 | 8 | } 9 | public T data; 10 | public BinaryTreeNode left; 11 | public BinaryTreeNode right; 12 | 13 | 14 | } 15 | -------------------------------------------------------------------------------- /Introduction to Java/Test 2/Important Notes and Answers.txt: -------------------------------------------------------------------------------- 1 | 1. a&1 always gives 0, when a is even and 1 when a is odd. 2 | 2. the right shift >> is equivalent to divising a number by 2. 3 | 3. the left shift << is equivalent to multiplying a number by 2. 4 | 4. a^b gives 0, only if both a and b are equal. 5 | 5. 1^0=0^1=1, 1^1=0^0 = 0. 6 | 6. 1&1=1, all other cases are zero. -------------------------------------------------------------------------------- /Data Strucutures In java/Lecture 11 Binary Trees/BinaryTreeNode.java: -------------------------------------------------------------------------------- 1 | //creates a generic binary tree 2 | public class BinaryTreeNode{ 3 | //We will create a generic class 4 | public BinaryTreeNode(T data){ 5 | this.data = data; 6 | } 7 | public T data; 8 | public BinaryTreeNode left; 9 | public BinaryTreeNode right; 10 | //we made all public so that visibility is not restricted 11 | } -------------------------------------------------------------------------------- /Data Strucutures In java/Lecture 13 Hash Maps/OurHashMap Implementation/MapNode.java: -------------------------------------------------------------------------------- 1 | //first create a MapNode 2 | public class MapNode{ 3 | K key; 4 | V value; 5 | MapNode next; //to store the pointer for the next node 6 | //constructor for mapnode 7 | public MapNode(K key, V value){ 8 | this.key = key; 9 | this.value = value; 10 | //by default next of new node is null 11 | } 12 | 13 | } -------------------------------------------------------------------------------- /Data Strucutures In java/Lecture 13 Hash Maps/OurHashMap Implementation/OurMapUse.java: -------------------------------------------------------------------------------- 1 | //the third and final class which implements Hashmaps 2 | public class OurMapUse { 3 | public static void main(String[] args) { 4 | Map map = new Map<>(); 5 | for(int i=0;i<20;i++) 6 | { 7 | map.insert("abc"+i+1+i); 8 | System.out.print("i = "+i+"lf = "+map.loadFactor()); 9 | } 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /Introduction to Java/Lecture 2, Getting Started with Java/HelloWorld.java: -------------------------------------------------------------------------------- 1 | 2 | 3 | public class HelloWorld { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | System.out.print("Hello World"); 8 | System.out.println("\n"); //here /n is used to display in the new line 9 | System.out.println("This is Kumar Sumit"); 10 | //you can also display on new line by System.out.println. 11 | System.out.println("this is neha kumari"); 12 | 13 | 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /Introduction to Java/Lecture 2, Getting Started with Java/DataTypes.java: -------------------------------------------------------------------------------- 1 | 2 | 3 | public class DataTypes { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | int c = 3*2/3; 8 | // will give output 2 because of associativity 3*2 will be done first, and then /3 will be done. 9 | //the execution flows from left to right. 10 | int d = 3/3 *2; //here 3/3 is done first after that 1*2 is 2 and we get the answer. 11 | System.out.print(d); 12 | 13 | 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /Introduction to Java/Lecure 6, Patterns 2/InvertedTraingle.java: -------------------------------------------------------------------------------- 1 | package lecture6; 2 | import java.util.Scanner; 3 | public class InvertedTraingle { 4 | 5 | public static void main(String[] args) { 6 | Scanner s = new Scanner(System.in); 7 | int n = s.nextInt(); 8 | int rows = 1; 9 | while(rows<=n) { 10 | int cols = 1; 11 | while(cols<=n-rows+1) { 12 | System.out.print('*'); 13 | cols++; 14 | } 15 | System.out.println(); 16 | rows++; 17 | s.close(); 18 | 19 | } 20 | 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /Introduction to Java/Lecture 4, Conditionals and Loops/SumofNNumbers.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Scanner; 3 | public class SumofNNumbers { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | Scanner s = new Scanner(System.in); 8 | int n = s.nextInt(); 9 | int i = 1; 10 | int sum= 0; 11 | while(i<=n) { 12 | System.out.println(i); //prints i for every step. 13 | sum= sum +i; 14 | i= i+1; 15 | } 16 | System.out.println(sum);//prints sum 17 | s.close(); 18 | 19 | 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /Introduction to Java/Lecture 11, 2D Arrays/Storing 2d Arrays.txt: -------------------------------------------------------------------------------- 1 | public class Store2darrays { 2 | 3 | 4 | 5 | public static void main(String args[]) { 6 | int[]arr={1,2,3,4}; 7 | System.out.println(arr.length); 8 | int[][] arr2d={{1,2,3,4},{5,6,7,8},{9,10,11,12}}; 9 | System.out.println(arr2d.length); 10 | System.out.println(arr2d); 11 | System.out.println(arr2d[0]); 12 | System.out.println(arr2d[1]); 13 | System.out.println(arr2d.length); 14 | System.out.println(arr2d[0].length); 15 | } 16 | } -------------------------------------------------------------------------------- /Introduction to Java/Lecture 5, Patterns 1/SquarePatterns1.java: -------------------------------------------------------------------------------- 1 | package lecture5; 2 | import java.util.Scanner; 3 | public class SquarePatterns1 { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | Scanner s = new Scanner(System.in); 8 | int n = s.nextInt(); 9 | int rows = 1; 10 | while(rows<=n) { 11 | int cols = 1; 12 | while(cols<=n) { 13 | System.out.print(cols); 14 | cols++; 15 | } 16 | System.out.println(); 17 | rows++; 18 | 19 | } 20 | s.close(); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /Data Strucutures In java/Lecture 14 Priority Queues/PriorityQueueUse.java: -------------------------------------------------------------------------------- 1 | //here we use inbuilt Priority Queue in java 2 | import java.util.PriorityQueue; 3 | 4 | public class PriorityQueueUse { 5 | public static void main(String[] args) { 6 | PriorityQueue pq = new PriorityQueue<>(); 7 | int arr[] = {9,1,0,4,7,3}; 8 | for(int i=0;i takeInput(Scanner s){ 4 | int rootData; 5 | 6 | } 7 | public static void main(String[] args) { 8 | // BinaryTreeNode root = new BinaryTreeNode(1); 9 | // BinaryTreeNode node1 = new BinaryTreeNode(2); 10 | // BinaryTreeNode node2 = new BinaryTreeNode(3); 11 | // root.left = node1; 12 | // root.right = node2; 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /Introduction to Java/Lecture 12, Strings/PrintAllChars.java: -------------------------------------------------------------------------------- 1 | //this shows how to iterate over the strngs and print a new character. 2 | import java.util.Scanner; 3 | 4 | 5 | public class PrintAllChars { 6 | public static void printAllChars(String str){ 7 | for(int i=0;i=1;j--) { 21 | if(i==j) { 22 | System.out.print("*"); 23 | }else { 24 | System.out.print(j); 25 | } 26 | } 27 | System.out.println(); 28 | 29 | } 30 | s.close(); 31 | 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /Introduction to Java/Lecture 5, Patterns 1/lecture5traingularpatterns/BasicPattern3.java: -------------------------------------------------------------------------------- 1 | package lecture5traingularpatterns; 2 | import java.util.Scanner; 3 | public class BasicPattern3 { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | Scanner s = new Scanner(System.in); 8 | int n = s.nextInt(); 9 | int rows = 1; 10 | 11 | while(rows<=n) { 12 | int printValue = rows; 13 | int cols = 1; 14 | while(cols<=rows) { 15 | System.out.print(printValue); 16 | printValue++; 17 | cols++; 18 | 19 | } 20 | System.out.println(); 21 | rows++; 22 | } 23 | s.close(); 24 | 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /Introduction to Java/Lecure 6, Patterns 2/MirrorImagePattern.java: -------------------------------------------------------------------------------- 1 | package lecture6; 2 | import java.util.Scanner; 3 | public class MirrorImagePattern { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | Scanner s = new Scanner(System.in); 8 | int n = s.nextInt(); 9 | int rows = 1; 10 | while(rows<=n) { 11 | int stars = 1; 12 | int spaces = 1; 13 | while(spaces <= n-rows) { 14 | System.out.print(' '); 15 | spaces++; 16 | } 17 | while(stars<=rows) { 18 | System.out.print('*'); 19 | stars++; 20 | } 21 | System.out.println(); 22 | rows++; 23 | } 24 | s.close(); 25 | 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /Introduction to Java/Lecture 5, Patterns 1/lectureAssignmentsExercises/ReverseNumberPattern.java: -------------------------------------------------------------------------------- 1 | package lectureAssignmentsExercises; 2 | 3 | import java.util.Scanner; 4 | public class ReverseNumberPattern { 5 | 6 | public static void main(String[] args) { 7 | // TODO Auto-generated method stub 8 | Scanner s = new Scanner(System.in); 9 | int n = s.nextInt(); 10 | int rows = 1; 11 | while (rows <= n) { 12 | int printValue = rows; 13 | int cols = 1; 14 | while (cols <= rows) { 15 | System.out.print(printValue); 16 | printValue--; 17 | cols++; 18 | } 19 | System.out.println(); 20 | rows++; 21 | } 22 | s.close(); 23 | 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /Data Strucutures In java/Lecture 3 Recursion 1/Sum of digits (recursive).txt: -------------------------------------------------------------------------------- 1 | Write a recursive function that returns the sum of the digits of a given integer. 2 | Input format : 3 | Integer N 4 | Output format : 5 | Sum of digits of N 6 | Constraints : 7 | 0 <= N <= 10^9 8 | Sample Input 1 : 9 | 12345 10 | Sample Output 1 : 11 | 15 12 | Sample Input 2 : 13 | 9 14 | Sample Output 2 : 15 | 9 16 | 17 | Solution:///////////// 18 | 19 | public class solution { 20 | 21 | public static int sumOfDigits(int input){ 22 | int sum; 23 | if(input<10){ 24 | return input; 25 | } 26 | sum = (input % 10) + sumOfDigits(input / 10); 27 | return sum; 28 | 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Data Strucutures In java/Lecture 3 Recursion 1/Number of Digits.txt: -------------------------------------------------------------------------------- 1 | Given the code to find out and return the number of digits present in a number recursively. But it contains few bugs, that you need to rectify such that all the test cases should pass. 2 | Input Format : 3 | Integer n 4 | Output Format : 5 | Count of digits 6 | Constraints : 7 | 1 <= n <= 10^6 8 | Sample Input 1 : 9 | 156 10 | Sample Output 1 : 11 | 3 12 | Sample Input 2 : 13 | 7 14 | Sample Output 2 : 15 | 1 16 | Solution:////////////////////// 17 | public class Solution { 18 | 19 | public static int count(int n){ 20 | if(n == 0){ 21 | return 0; 22 | } 23 | int smallAns = count(n / 10); 24 | return smallAns + 1; 25 | } 26 | 27 | } -------------------------------------------------------------------------------- /Introduction to Java/Lecture 8, Functions and Scope/EvenNumbersBetween2Numbers.java: -------------------------------------------------------------------------------- 1 | package lecture8; 2 | import java.util.Scanner; 3 | public class EvenNumbersBetween2Numbers { 4 | public static void printEvenNumbers(int start, int end) { 5 | if(start%2!=0) { //to check if number is odd, if it is increment it by 1 6 | start++; 7 | } 8 | for(int i=start;i<=end;i+=2) { 9 | System.out.println(i); 10 | } 11 | 12 | } 13 | 14 | public static void main(String[] args) { 15 | // TODO Auto-generated method stub 16 | Scanner s = new Scanner(System.in); 17 | int start = s.nextInt(); 18 | int end = s.nextInt(); 19 | printEvenNumbers(start, end); 20 | s.close(); 21 | 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /Data Strucutures In java/Lecture 3 Recursion 1/Geometric Sum.txt: -------------------------------------------------------------------------------- 1 | Given k, find the geometric sum i.e. 2 | 1 + 1/2 + 1/4 + 1/8 + ... + 1/(2^k) 3 | using recursion. 4 | Input format : 5 | Integer k 6 | Output format : 7 | Geometric sum (upto 5 decimal places) 8 | Constraints : 9 | 0 <= k <= 1000 10 | Sample Input 1 : 11 | 3 12 | Sample Output 1 : 13 | 1.87500 14 | Sample Input 2 : 15 | 4 16 | Sample Output 2 : 17 | 1.93750 18 | 19 | Solution:///////////////////////////// 20 | 21 | public class solution { 22 | 23 | public static double findGeometricSum(int k){ 24 | if(k==0){ 25 | return 1; 26 | 27 | } 28 | return findGeometricSum(k-1) + 1 / Math.pow(2, k); 29 | 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Introduction to Java/Lecture 12, Strings/InputStrings.java: -------------------------------------------------------------------------------- 1 | //how to take Input in Java 2 | import java.util.Scanner; 3 | public class InputStrings { 4 | public static void main(String[] args) { 5 | Scanner s = new Scanner(System.in); 6 | String str1,str2,str3; 7 | str1= s.next(); //input is given as abc def ghi jkl 8 | System.out.println(str1); //abc is printed //next only takes in next token separated by tab or space. 9 | str2 = s.next(); 10 | System.out.println(str2); //def is printed 11 | str3 = s.nextLine(); 12 | System.out.println(str3); //ghi jkl is printed because it prints everything that remains until it sees a newline 13 | s.close(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Introduction to Java/Lecture 9 Arrays 1/InputOfArray.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class InputOfArray { 3 | public static void main(String args[]) { 4 | Scanner s = new Scanner(System.in); 5 | int n = s.nextInt(); //gives the size the array. 6 | int arr[] = new int[n]; //creates a new integer array of size n 7 | for(int i=0;i pq = new PriorityQueue<>(); 7 | int i=0; 8 | for(;i pq = new PriorityQueue<>(); 7 | int i=0; 8 | for(;i=1) { 26 | System.out.print(dec); 27 | dec--; 28 | } 29 | 30 | System.out.println(); 31 | rows++; 32 | } 33 | s.close(); 34 | 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /Introduction to Java/Test 1/Checkpalindrome.java: -------------------------------------------------------------------------------- 1 | //Write a function to check whether a given number is palindrome or not 2 | //if it is 121 answer is true , if it is 345 answer is false. 3 | 4 | package test1; 5 | 6 | import java.util.Scanner; 7 | 8 | public class Checkpalindrome { 9 | 10 | public static void main(String[] args) { 11 | // TODO Auto-generated method stub 12 | 13 | Scanner s = new Scanner(System.in); 14 | int n = s.nextInt(); 15 | int reversedNumber = 0; 16 | int testNumber = n; 17 | while(n>0){ 18 | int lastDigit = n%10; 19 | reversedNumber = reversedNumber *10+ lastDigit; 20 | n = n/10; 21 | } 22 | if(reversedNumber==testNumber){ 23 | System.out.println("true"); 24 | 25 | }else{ 26 | System.out.println("false"); 27 | 28 | } 29 | s.close(); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /Data Strucutures In java/Lecture 15 Tries and Huffman Coding/TrieUse.java: -------------------------------------------------------------------------------- 1 | public class TrieUse { 2 | //a class to check the Trie 3 | public static void main(String[] args) 4 | { 5 | Trie t = new Trie(); 6 | t.add("this"); 7 | t.add("news"); 8 | System.out.println(t.search("word")); //false 9 | System.out.println(t.search("news")); //true 10 | System.out.println(t.search("this")); //true 11 | t.remove("this"); 12 | System.out.println(t.search("this")); //false after removal 13 | t.remove("news"); 14 | System.out.println(t.search("news")); //false after removal 15 | System.out.println(t.countWords()); //0 because both words are removed 16 | t.add("first"); 17 | t.add("second"); 18 | t.add("third"); 19 | System.out.println(t.countWords()); //3 because three words are added 20 | t.print(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Data Strucutures In java/Lecture 4 Recursion 2/Remove Duplicates Recursively.txt: -------------------------------------------------------------------------------- 1 | Given a string S, remove consecutive duplicates from it recursively. 2 | Input Format : 3 | String S 4 | Output Format : 5 | Output string 6 | Constraints : 7 | 1 <= |S| <= 10^3 8 | where |S| represents the length of string 9 | Sample Input 1 : 10 | aabccba 11 | Sample Output 1 : 12 | abcba 13 | Sample Input 2 : 14 | xxxyyyzwwzzz 15 | Sample Output 2 : 16 | xyzwz 17 | 18 | Solution:/// 19 | public class Solution { 20 | 21 | public static String removeConsecutiveDuplicates(String s) { 22 | if(s.length()<= 1){ 23 | return s; 24 | } 25 | if(s.charAt(0) == s.charAt(1)) { 26 | return removeConsecutiveDuplicates(s.substring(1)); 27 | 28 | } 29 | else{ 30 | String small = removeConsecutiveDuplicates(s.substring(1)); 31 | return s.charAt(0) + small; 32 | } 33 | 34 | } 35 | 36 | } -------------------------------------------------------------------------------- /Data Strucutures In java/Lecture 3 Recursion 1/Check Palindrome (recursive).txt: -------------------------------------------------------------------------------- 1 | Check whether a given String S is a palindrome using recursion. Return true or false. 2 | Input Format : 3 | String S 4 | Output Format : 5 | 'true' or 'false' 6 | Constraints : 7 | 0 <= |S| <= 1000 8 | where |S| represents length of string S. 9 | Sample Input 1 : 10 | racecar 11 | Sample Output 1: 12 | true 13 | Sample Input 2 : 14 | ninja 15 | Sample Output 2: 16 | false 17 | 18 | Solution:////// 19 | 20 | public class solution { 21 | public static boolean solve(String str, int start, int end) { 22 | if(start >= end){ 23 | return true; 24 | } 25 | if(str.charAt(start) != str.charAt(end)){ 26 | return false; 27 | } 28 | return solve(str, start+1, end-1); 29 | 30 | } 31 | 32 | public static boolean isStringPalindrome(String input) { 33 | return solve(input, 0, input.length()-1); 34 | 35 | 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Introduction to Java/Lecture 11, 2D Arrays/InputOf2DArrays.java: -------------------------------------------------------------------------------- 1 | //check how to get input in 2d Arrays 2 | import java.util.Scanner; 3 | public class InputOf2DArrays{ 4 | public static void main(String[] args) { 5 | Scanner s = new Scanner(System.in); 6 | System.out.println("Enter the number of rows"); 7 | int rows = s.nextInt(); 8 | System.out.println("Enter the number of columns"); 9 | int cols = s.nextInt(); 10 | int arr[][] = new int[rows][cols]; 11 | for(int i=0;i arr[parentIndex]) { 13 | return false; 14 | } 15 | if(rightChildIndex < arr.length && arr[rightChildIndex] > arr[parentIndex]) { 16 | return false; 17 | } 18 | 19 | 20 | } 21 | return true; 22 | 23 | 24 | } 25 | 26 | public static void main(String[] args) { 27 | int arr[] = {42,20,18, 6,14,11,9,4}; 28 | System.out.println(CheckMaxHeap(arr)); 29 | 30 | } 31 | 32 | 33 | 34 | } 35 | -------------------------------------------------------------------------------- /Introduction to Java/Lecture 7, Operators and For Loop/Assignments/SquareRoot.java: -------------------------------------------------------------------------------- 1 | /* 2 | Square Root (Integral) 3 | 4 | Given a number N, find its square root. You need to find and print only the integral part of square root of N. 5 | For eg. if number given is 18, answer is 4. 6 | Input format : 7 | Integer N 8 | Output Format : 9 | Square root of N (integer part only) 10 | Constraints : 11 | 0 <= N <= 10^8 12 | Sample Input 1 : 13 | 10 14 | Sample Output 1 : 15 | 3 16 | Sample Input 2 : 17 | 4 18 | Sample Output 2 : 19 | 2 20 | */ 21 | 22 | package lecture7.Assignments; 23 | import java.util.Scanner; 24 | public class SquareRoot { 25 | 26 | public static void main(String[] args) { 27 | // TODO Auto-generated method stub 28 | Scanner s = new Scanner(System.in); 29 | int n = s.nextInt(); 30 | int i=1; 31 | while(i*i<=n){ 32 | i++; 33 | } 34 | System.out.println(i-1); 35 | s.close(); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /Introduction to Java/Lecture 4, Conditionals and Loops/Assignments/MultiplicationTable.java: -------------------------------------------------------------------------------- 1 | /*Multiplication Table 2 | 3 | Write a program to print multiplication table of n 4 | Input Format : 5 | A single integer, n 6 | Output Format : 7 | First 10 multiples of n each printed in new line 8 | Constraints : 9 | 0 <= n <= 10,000 10 | Sample Input 1 : 11 | 2 12 | Sample Output 1 : 13 | 2 14 | 4 15 | 6 16 | 8 17 | 10 18 | 12 19 | 14 20 | 16 21 | 18 22 | 20 23 | Sample Input 2 : 24 | 5 25 | Sample Output 2 : 26 | 5 27 | 10 28 | 15 29 | 20 30 | 25 31 | 30 32 | 35 33 | 40 34 | 45 35 | 50 36 | */ 37 | 38 | 39 | import java.util.Scanner; 40 | public class MultiplicationTable { 41 | 42 | public static void main(String[] args) { 43 | // TODO Auto-generated method stub 44 | Scanner s = new Scanner(System.in); 45 | int number = s.nextInt(); 46 | for(int i=1;i<11;i++){ 47 | System.out.println(number*i); 48 | } 49 | s.close(); 50 | 51 | 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /Introduction to Java/Lecture 9 Arrays 1/LargestElement.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class LargestElement { 3 | public static int largestItem(int arr[]){ 4 | int largestNumber = Integer.MIN_VALUE; 5 | for(int i=0;ilargestNumber){ 7 | largestNumber = arr[i]; 8 | } 9 | } 10 | return largestNumber; 11 | } 12 | 13 | public static int[] takeInput(){ 14 | Scanner s = new Scanner(System.in); 15 | int n = s.nextInt(); 16 | int arr[] = new int[n]; 17 | for(int i=0;i=1) { 24 | //code to print increasing numbers 25 | for(int cols=1;cols<=rows;cols++) { 26 | System.out.print(cols); 27 | } 28 | //code to print stars 29 | int starsCount = (n-rows)*2; 30 | for(int i=1;i<=starsCount;i++) { 31 | System.out.print("*"); 32 | } 33 | //code to print decreasing numbers 34 | int printValue = rows; 35 | for(int i=1;i<=rows;i++) { 36 | System.out.print(printValue); 37 | printValue--; 38 | } 39 | System.out.println(); 40 | rows--; 41 | } 42 | s.close(); 43 | 44 | 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /Data Strucutures In java/Assignment Recursion 1/Pair Star.txt: -------------------------------------------------------------------------------- 1 | Given a string S, compute recursively a new string where identical chars that are adjacent in the original string are separated from each other by a "*". 2 | Input format : 3 | String S 4 | Output format : 5 | Modified string 6 | Constraints : 7 | 0 <= |S| <= 1000 8 | where |S| represents length of string S. 9 | Sample Input 1 : 10 | hello 11 | Sample Output 1: 12 | hel*lo 13 | Sample Input 2 : 14 | aaaa 15 | Sample Output 2 : 16 | a*a*a*a 17 | 18 | Solution://///////// 19 | 20 | public class solution { 21 | 22 | // Return the updated string 23 | public static String addStars(String s) { 24 | if(s.length() <= 1){ 25 | return s; 26 | } 27 | String out = addStars(s.substring(1)); 28 | if(s.charAt(0) == s.charAt(1)){ 29 | out = s.charAt(0) + "*" + s.charAt(1) + out.substring(1); 30 | 31 | }else{ 32 | out = s.charAt(0) + out; 33 | } 34 | return out; 35 | 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Data Strucutures In java/Lecture 4 Recursion 2/Replace Character Recursively.txt: -------------------------------------------------------------------------------- 1 | Given an input string S and two characters c1 and c2, you need to replace every occurrence of character c1 with character c2 in the given string. 2 | Do this recursively. 3 | Input Format : 4 | Line 1 : Input String S 5 | Line 2 : Character c1 and c2 (separated by space) 6 | Output Format : 7 | Updated string 8 | Constraints : 9 | 1 <= Length of String S <= 10^6 10 | Sample Input : 11 | abacd 12 | a x 13 | Sample Output : 14 | xbxcd 15 | 16 | Solution://///////// 17 | 18 | public class Solution { 19 | 20 | public static String replaceCharacter(String input, char c1, char c2) { 21 | if(input.length() == 0){ 22 | return ""; 23 | } 24 | 25 | char c; 26 | if(input.charAt(0)==c1){ 27 | c=c2; 28 | } 29 | else{ 30 | c= input.charAt(0); 31 | } 32 | 33 | String ans = replaceCharacter(input.substring(1), c1, c2); 34 | return (c+ans); 35 | 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Data Strucutures In java/Assignment Recursion 1/Replace pi (recursive).txt: -------------------------------------------------------------------------------- 1 | Given a string, compute recursively a new string where all appearances of "pi" have been replaced by "3.14". 2 | Constraints : 3 | 1 <= |S| <= 50 4 | where |S| represents the length of string S. 5 | Sample Input 1 : 6 | xpix 7 | Sample Output : 8 | x3.14x 9 | Sample Input 2 : 10 | pipi 11 | Sample Output : 12 | 3.143.14 13 | Sample Input 3 : 14 | pip 15 | Sample Output : 16 | 3.14p 17 | Constraints:- 18 | 1<=|S|<=50 19 | 20 | Solution://///////// 21 | public class solution { 22 | 23 | // Return the changed string 24 | public static String replace(String input){ 25 | String output; 26 | if(input.length() <= 1){ 27 | return input; 28 | } 29 | String small = replace(input.substring(1)); 30 | if(input.charAt(0)== 'p' && small.charAt(0)== 'i'){ 31 | output = "3.14" + small.substring(1); 32 | } 33 | else{ 34 | output = input.charAt(0) + small; 35 | } 36 | return output; 37 | 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Introduction to Java/Lecture 10 Arrays 2, Searching and Sorting/Assignments/RotateArrayLeftdTimesFirst.java: -------------------------------------------------------------------------------- 1 | //given an array rotate it left by d times. 2 | //input {2, 6, 3, 1, 5} 3 | //output after rotations by 2 times {3, 1, 5 ,1, 2, 6} 4 | 5 | public class RotateArrayLeftdTimesFirst { 6 | public static void rotate(int[] arr, int d) { 7 | //Your code goes here 8 | int j=1; 9 | while(j<=d){ 10 | int temp = arr[0]; 11 | int i; 12 | for(i=0;i0){ 34 | ans = ans *x; 35 | n--; 36 | } 37 | System.out.println(ans); 38 | s.close(); 39 | 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /Introduction to Java/Lecture 10 Arrays 2, Searching and Sorting/Assignments/RotateArrayLeftdTimesOptimized.java: -------------------------------------------------------------------------------- 1 | //given an array rotate it left by d times. 2 | //input {2, 6, 3, 1, 5} 3 | //output after rotations by 2 times {3, 1, 5 ,1, 2, 6} 4 | 5 | public class RotateArrayLeftdTimesOptimized { 6 | public static void rotate(int[] arr, int d) { 7 | //Your code goes here 8 | int n = arr.length; 9 | int temp[] = new int[d]; 10 | for(int j=0;j0){ 32 | int lastDigit = n%10; 33 | decimalNumber = decimalNumber + lastDigit*placeValue; 34 | placeValue = placeValue * 2; 35 | n=n/10; 36 | } 37 | System.out.println(decimalNumber); 38 | 39 | s.close(); 40 | 41 | 42 | 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /Data Strucutures In java/Lecture 3 Recursion 1/Sum of Array.txt: -------------------------------------------------------------------------------- 1 | Given an array of length N, you need to find and return the sum of all elements of the array. 2 | Do this recursively. 3 | Input Format : 4 | Line 1 : An Integer N i.e. size of array 5 | Line 2 : N integers which are elements of the array, separated by spaces 6 | Output Format : 7 | Sum 8 | Constraints : 9 | 1 <= N <= 10^3 10 | Sample Input 1 : 11 | 3 12 | 9 8 9 13 | Sample Output 1 : 14 | 26 15 | Sample Input 2 : 16 | 3 17 | 4 2 1 18 | Sample Output 2 : 19 | 7 20 | 21 | Solution:///////// 22 | public class Solution { 23 | 24 | public static int sum(int input[], int startIndex) { 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 | if(startIndex == input.length){ 32 | return 0; 33 | } 34 | return input[startIndex] + sum(input, startIndex+1); 35 | 36 | } 37 | 38 | public static int sum(int input[]){ 39 | return sum(input, 0); 40 | } 41 | } -------------------------------------------------------------------------------- /Introduction to Java/Lecture 5, Patterns 1/lectureAssignmentsExercises/AlphaPattern.java: -------------------------------------------------------------------------------- 1 | /* 2 | Code : Alpha Pattern 3 | 4 | Print the following pattern for the given N number of rows. 5 | Pattern for N = 3 6 | A 7 | BB 8 | CCC 9 | Input format : 10 | Integer N (Total no. of rows) 11 | Output format : 12 | Pattern in N lines 13 | Constraints 14 | 0 <= N <= 26 15 | Sample Input 1: 16 | 7 17 | Sample Output 1: 18 | A 19 | BB 20 | CCC 21 | DDDD 22 | EEEEE 23 | FFFFFF 24 | GGGGGGG 25 | Sample Input 2: 26 | 6 27 | Sample Output 2: 28 | A 29 | BB 30 | CCC 31 | DDDD 32 | EEEEE 33 | FFFFFF 34 | */ 35 | package lectureAssignmentsExercises; 36 | 37 | import java.util.Scanner; 38 | public class AlphaPattern { 39 | 40 | public static void main(String[] args) { 41 | // TODO Auto-generated method stub 42 | Scanner s = new Scanner(System.in); 43 | int n = s.nextInt(); 44 | int rows = 1; 45 | while (rows <= n) { 46 | int cols = 1; 47 | while (cols <= rows) { 48 | char colValue = (char) ('A' + rows - 1); 49 | System.out.print(colValue); 50 | cols++; 51 | } 52 | System.out.println(); 53 | rows++; 54 | } 55 | s.close(); 56 | 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /Introduction to Java/Lecture 4, Conditionals and Loops/FindCharacterCase.java: -------------------------------------------------------------------------------- 1 | /*Find Character Case 2 | 3 | Write a program that takes a character as input and prints either 1, 0 or -1 according to the following rules. 4 | 1, if the character is an uppercase alphabet (A - Z) 5 | 0, if the character is a lowercase alphabet (a - z) 6 | -1, if the character is not an alphabet 7 | Input format 8 | Single Character 9 | Output format 10 | 1 or 0 or -1 11 | Constraints 12 | Input can be any character 13 | Sample Input 1 14 | v 15 | Sample Output 1 16 | 0 17 | Sample Input 2 18 | V 19 | Sample Output 2 20 | 1 21 | Sample Input 3 22 | # 23 | Sample Output 3 24 | -1 25 | */ 26 | 27 | //Solution 28 | import java.util.Scanner; 29 | public class FindCharacterCase { 30 | 31 | public static void main(String[] args) { 32 | // TODO Auto-generated method stub 33 | Scanner s = new Scanner(System.in); 34 | char ch = s.next().charAt(0); 35 | if(ch>='a' && ch<='z') { 36 | System.out.println("0"); 37 | }else if(ch>='A' && ch<='Z') { 38 | System.out.println("1"); 39 | }else { 40 | System.out.println("-1"); 41 | } 42 | s.close(); 43 | 44 | 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /Data Strucutures In java/Lecture 13 Hash Maps/RemoveDuplicates.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.HashMap; 3 | 4 | /* 5 | remove duplicates from the array and 6 | return the unique values in A ArrayList, 7 | always nmaintain the order 8 | */ 9 | public class RemoveDuplicates { 10 | public static ArrayList removeDuplicates(int arr[]) { 11 | ArrayList output = new ArrayList<>(); 12 | HashMap seen = new HashMap<>(); 13 | for (Integer element : arr) { 14 | if (seen.containsKey(element)) { 15 | continue; 16 | } 17 | output.add(element); 18 | seen.put(element, true); 19 | } 20 | return output; 21 | } 22 | 23 | // Driver code to check our function 24 | public static void main(String[] args) { 25 | int arr[] = {1,3,1,4,5,100000, 200, 5,100000, 4}; 26 | ArrayList output = removeDuplicates(arr); 27 | for(int i=0;i=0) 32 | op2=storage[i-2]; 33 | if(i-3>=0) 34 | op3=storage[i-3]; 35 | storage[i]=op1+op2+op3; 36 | } 37 | return storage[n]; 38 | } 39 | 40 | } -------------------------------------------------------------------------------- /Introduction to Java/Lecure 6, Patterns 2/Assignments/ParallelogramPattern.java: -------------------------------------------------------------------------------- 1 | /*Parallelogram Pattern 2 | 3 | Write a program to print parallelogram pattern for the given N number of rows. 4 | For N = 4 5 | 6 | 7 | 8 | The dots represent spaces. 9 | Input Format : 10 | A single integer : N 11 | Output Format : 12 | Required Pattern 13 | Constraints : 14 | 0 <= N <= 50 15 | Sample Input 1 : 16 | 3 17 | Sample Output 1 : 18 | *** 19 | *** 20 | *** 21 | Sample Input 2 : 22 | 5 23 | Sample Output 2 : 24 | ***** 25 | ***** 26 | ***** 27 | ***** 28 | ***** 29 | */ 30 | package lecture6.Assignments; 31 | import java.util.Scanner; 32 | public class ParallelogramPattern { 33 | 34 | public static void main(String[] args) { 35 | // TODO Auto-generated method stub 36 | Scanner s = new Scanner(System.in); 37 | int n = s.nextInt(); 38 | int colRows = 1; 39 | while(colRows<=n) { 40 | int spaces = 1; 41 | while(spaces<=colRows-1) { 42 | System.out.print(' '); 43 | spaces++; 44 | } 45 | int stars = 1; 46 | while(stars<=n) { 47 | System.out.print('*'); 48 | stars++; 49 | } 50 | System.out.println(); 51 | colRows++; 52 | } 53 | s.close(); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /Introduction to Java/Lecture 4, Conditionals and Loops/CheckPrimality.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Scanner; 3 | public class CheckPrimality { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | Scanner s = new Scanner(System.in); 8 | int n = s.nextInt(); 9 | boolean isPrime = true; 10 | int div = 2; // we start from 2 because 1 and 2 are anyway prime. 11 | //case 1 12 | // while(div0){ 34 | int lastDigit = n%10; 35 | reversedNumber = reversedNumber * 10 + lastDigit; 36 | n=n/10; 37 | } 38 | System.out.println(reversedNumber); 39 | s.close(); 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /Introduction to Java/Lecure 6, Patterns 2/Assignments/OddSquare.java: -------------------------------------------------------------------------------- 1 | /*Odd Square 2 | 3 | Write a program to print the pattern for the given N number of rows. 4 | For N = 4 5 | 1357 6 | 3571 7 | 5713 8 | 7135 9 | Input Format : 10 | A single integer: N 11 | Output Format : 12 | Required Pattern 13 | Constraints : 14 | 0 <= N <= 50 15 | Sample Input 1 : 16 | 3 17 | Sample Output 1 : 18 | 135 19 | 351 20 | 513 21 | Sample Input 2 : 22 | 5 23 | Sample Output 2 : 24 | 13579 25 | 35791 26 | 57913 27 | 79135 28 | 91357 29 | */ 30 | 31 | package lecture6.Assignments; 32 | 33 | import java.util.Scanner; 34 | 35 | public class OddSquare { 36 | 37 | public static void main(String[] args) { 38 | // TODO Auto-generated method stub 39 | Scanner s = new Scanner(System.in); 40 | int n = s.nextInt(); 41 | int currRows = 1; 42 | while (currRows <= n) { 43 | int printValue = 2*currRows -1; 44 | int currCol = 1; 45 | while(currCol<=n) { 46 | System.out.print(printValue); 47 | printValue = printValue + 2; 48 | if(printValue > 2*n -1) { 49 | printValue = 1; 50 | } 51 | currCol++; 52 | } 53 | System.out.println(); 54 | currRows++; 55 | } 56 | s.close(); 57 | 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /Introduction to Java/Test 1/CheckAp.java: -------------------------------------------------------------------------------- 1 | //Given input consists of n numbers. Check whether those n numbers form an arithmetic progression or not. Print true or false. 2 | /* 3 | 6 4 | 2 6 10 14 18 22 true 5 | 6 6 | 2 6 10 15 19 23 false 7 | */ 8 | 9 | package test1; 10 | import java.util.Scanner; 11 | 12 | public class CheckAp { 13 | 14 | public static void main(String[] args) { 15 | // TODO Auto-generated method stub 16 | Scanner s = new Scanner(System.in); 17 | int n = s.nextInt(); 18 | int firstNumber = s.nextInt(); 19 | int prevNumber = s.nextInt(); 20 | int d = prevNumber - firstNumber; 21 | int count = 2; 22 | boolean isAP = true; 23 | while(count0){ 35 | int remainder = n%2; 36 | binary = binary + remainder*placevalue; 37 | placevalue = placevalue*10; 38 | n=n/2; 39 | 40 | } 41 | System.out.println(binary); 42 | s.close(); 43 | 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /Introduction to Java/Lecture 11, 2D Arrays/LargestColumnSum.java: -------------------------------------------------------------------------------- 1 | //Given a 2d array calculate the largest sum of any column and return that sum. 2 | //input 2d array 123 3 | // 456 colsum are 5, 7, 9 output must be 9 4 | 5 | public class LargestColumnSum { 6 | public static int largestColSum(int arr[][]){ 7 | int rows = arr.length; //gives no of rows 8 | int cols = arr[0].length; //gives no of columns , which will be same for each row here 9 | int largest = Integer.MIN_VALUE; //in case for blank array 10 | 11 | for(int j=0;jlargest){ 18 | largest = sum; 19 | } 20 | 21 | } 22 | //now we come out of cols for loop , we have largest sum for entire aarary 23 | return largest; 24 | } 25 | public static void main(String[] args) { 26 | int arr[][] = {{1, 2, 3}, {4, 5,6}}; 27 | int largestColSum = largestColSum(arr); 28 | System.out.println(largestColSum); //outputs 9 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Introduction to Java/Lecture 10 Arrays 2, Searching and Sorting/Exercises/InsertionSort.java: -------------------------------------------------------------------------------- 1 | //given an unsorted array, sort it using insertion sort 2 | //int arr[] = {9, 1, 4, 2 ,6}; 3 | //output: {1,2, 4,6,9} 4 | public class InsertionSort{ 5 | //coding ninjas solution code 6 | public static void insertionSort(int arr[]){ 7 | int n = arr.length; 8 | for(int i=1;i=0 && arr[j]>temp){ 14 | arr[j+1] = arr[j]; 15 | j--; 16 | } 17 | arr[j+1] = temp; 18 | 19 | } 20 | } 21 | //main code to check the solution code 22 | public static void printArray(int arr[]){ 23 | for(int i=0;i longestRootToLeafPath(BinaryTreeNode root) { 27 | 28 | if(root==null) { 29 | return new ArrayList<>(); 30 | } 31 | 32 | ArrayList leftArrayList = longestRootToLeafPath(root.left); 33 | ArrayList rightArrayList = longestRootToLeafPath(root.right); 34 | 35 | if(leftArrayList.size()>=rightArrayList.size()) { 36 | 37 | leftArrayList.add(root.data); 38 | return leftArrayList; 39 | 40 | } 41 | else { 42 | rightArrayList.add(root.data); 43 | return rightArrayList; 44 | } 45 | 46 | 47 | } 48 | } -------------------------------------------------------------------------------- /Introduction to Java/Lecture 5, Patterns 1/lectureAssignmentsExercises/InterestingAlphabets.java: -------------------------------------------------------------------------------- 1 | /*Code : Interesting Alphabets 2 | 3 | Print the following pattern for the given number of rows. 4 | Pattern for N = 5 5 | E 6 | DE 7 | CDE 8 | BCDE 9 | ABCDE 10 | Input format : 11 | N (Total no. of rows) 12 | Output format : 13 | Pattern in N lines 14 | Constraints 15 | 0 <= N <= 26 16 | Sample Input 1: 17 | 8 18 | Sample Output 1: 19 | H 20 | GH 21 | FGH 22 | EFGH 23 | DEFGH 24 | CDEFGH 25 | BCDEFGH 26 | ABCDEFGH 27 | Sample Input 2: 28 | 7 29 | Sample Output 2: 30 | G 31 | FG 32 | EFG 33 | DEFG 34 | CDEFG 35 | BCDEFG 36 | ABCDEFG 37 | */ 38 | 39 | 40 | 41 | 42 | package lectureAssignmentsExercises; 43 | 44 | import java.util.Scanner; 45 | public class InterestingAlphabets { 46 | 47 | public static void main(String[] args) { 48 | // TODO Auto-generated method stub 49 | Scanner s = new Scanner(System.in); 50 | int n = s.nextInt(); 51 | int rows = 1; 52 | while (rows <= n) { 53 | int cols = 1; 54 | char printValue = (char) ('A' + n - rows); 55 | while (cols <= rows) { 56 | System.out.print(printValue); 57 | printValue = (char) (printValue + 1); 58 | cols++; 59 | } 60 | System.out.println(); 61 | rows++; 62 | } 63 | s.close(); 64 | 65 | } 66 | 67 | } 68 | -------------------------------------------------------------------------------- /Introduction to Java/Lecture 8, Functions and Scope/Exercises/FibonacciNumber.java: -------------------------------------------------------------------------------- 1 | //write a program to check whether a input n is part of Fibonacci Series 2 | //if yes print true else false. 3 | 4 | package lecture8.Exercises; 5 | 6 | import java.util.Scanner; 7 | public class FibonacciNumber { 8 | 9 | public class Solution { 10 | //this is the function user has to complete. 11 | public static boolean checkMember(int n){ 12 | 13 | /* Your class should be named Solution 14 | * Don't write main(). 15 | * Don't read input, it is passed as function argument. 16 | * Return output and don't print it. 17 | * Taking input and printing output is handled automatically. 18 | */ 19 | int a = 0; 20 | int b = 1; 21 | int c; 22 | while(a=1) { 58 | System.out.print('*'); 59 | decreasing--; 60 | } 61 | System.out.println(); 62 | rows++; 63 | } 64 | s.close(); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /Introduction to Java/Test 3/FindLeadersInArray.java: -------------------------------------------------------------------------------- 1 | /* 2 | Find Leaders in array 3 | Send Feedback 4 | Given an integer array A of size n. Find and print all the leaders present in the input array. An array element A[i] is called Leader, if all the elements following it (i.e. present at its right) are less than or equal to A[i]. 5 | Print all the leader elements separated by space and in the reverse order. That means whichever leader comes at last should be printed first. 6 | Input Format : 7 | Line 1 : Integer n, size of array 8 | Line 2 : Array A elements (separated by space) 9 | Output Format : 10 | leaders of array (separated by space) 11 | Constraints : 12 | 1 <= n <= 10^6 13 | Sample Input 1 : 14 | 6 15 | 3 12 34 2 0 -1 16 | Sample Output 1 : 17 | -1 0 2 34 18 | Sample Input 2 : 19 | 5 20 | 13 17 5 4 6 21 | Sample Output 2 : 22 | 6 17 23 | */ 24 | public class FindLeadersInArray{ 25 | //coding ninjas solution 26 | public static void leaders(int[] arr) { 27 | int max = Integer.MIN_VALUE; 28 | for(int i=arr.length-1;i>=0;i--){ 29 | if(arr[i]>=max){ 30 | System.out.print(arr[i]+ " "); 31 | max = arr[i]; 32 | } 33 | } 34 | } 35 | //main driver code 36 | public static void main(String[] args) { 37 | int arr[] = {13, 17, 5, 4, 6}; 38 | leaders(arr); //output 6 17 39 | } 40 | } -------------------------------------------------------------------------------- /Introduction to Java/Lecture 10 Arrays 2, Searching and Sorting/Exercises/BubbleSort.java: -------------------------------------------------------------------------------- 1 | //given an unsorted array, sort it using bubble sort 2 | //int arr[] = {9, 1, 4, 2 ,6}; 3 | //output: {1,2, 4,6,9} 4 | public class BubbleSort{ 5 | //coding ninjas solution code 6 | public static void bubbleSort(int arr[]){ 7 | int n = arr.length; 8 | for(int i=0;iarr[j+1]){ 15 | int temp = arr[j+1]; 16 | arr[j+1] = arr[j]; 17 | arr[j] = temp; 18 | } 19 | } 20 | 21 | } 22 | } 23 | //main code to check the solution code 24 | public static void printArray(int arr[]){ 25 | for(int i=0;i 0) { 30 | int newNum = (n / (i * 10)) * i + n % i; 31 | i = i * 10; 32 | if (maxNum < newNum) { 33 | maxNum = newNum; 34 | } 35 | } 36 | return maxNum; 37 | } 38 | 39 | } 40 | 41 | public static void main(String[] args) { 42 | // TODO Auto-generated method stub 43 | Scanner s = new Scanner(System.in); 44 | int n = s.nextInt(); 45 | System.out.println(Solution.max_number(n)); 46 | s.close(); 47 | 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /Data Strucutures In java/Lecture 3 Recursion 1/Last Index of Number.txt: -------------------------------------------------------------------------------- 1 | Given an array of length N and an integer x, you need to find and return the last index of integer x present in the array. Return -1 if it is not present in the array. 2 | Last index means - if x is present multiple times in the array, return the index at which x comes last in the array. 3 | You should start traversing your array from 0, not from (N - 1). 4 | Do this recursively. Indexing in the array starts from 0. 5 | Input Format : 6 | Line 1 : An Integer N i.e. size of array 7 | Line 2 : N integers which are elements of the array, separated by spaces 8 | Line 3 : Integer x 9 | Output Format : 10 | last index or -1 11 | Constraints : 12 | 1 <= N <= 10^3 13 | Sample Input : 14 | 4 15 | 9 8 10 8 16 | 8 17 | Sample Output : 18 | 3 19 | 20 | Solution: // 21 | 22 | public class Solution { 23 | 24 | public static int lastIndex(int input[], int x, int startIndex) { 25 | if(startIndex==input.length){ 26 | return -1; 27 | } 28 | int smallAns = lastIndex(input,x, startIndex+1); 29 | if(smallAns!=-1){ 30 | return smallAns; 31 | } 32 | if(input[startIndex]==x){ 33 | return startIndex; 34 | } 35 | else{ 36 | return -1; 37 | } 38 | 39 | } 40 | 41 | public static int lastIndex(int input[], int x) { 42 | return lastIndex(input,x,0); 43 | 44 | } 45 | 46 | } -------------------------------------------------------------------------------- /Data Strucutures In java/Assignment Recursion 1/Tower of Hanoi.txt: -------------------------------------------------------------------------------- 1 | Tower of Hanoi 2 | Send Feedback 3 | Tower of Hanoi is a mathematical puzzle where we have three rods and n disks. The objective of the puzzle is to move all disks from source rod to destination rod using third rod (say auxiliary). The rules are : 4 | 1) Only one disk can be moved at a time. 5 | 2) A disk can be moved only if it is on the top of a rod. 6 | 3) No disk can be placed on the top of a smaller disk. 7 | Print the steps required to move n disks from source rod to destination rod. 8 | Source Rod is named as 'a', auxiliary rod as 'b' and destination rod as 'c'. 9 | Input Format : 10 | Integer n 11 | Output Format : 12 | Steps in different lines (in one line print source and destination rod name separated by space) 13 | Constraints : 14 | 0 <= n <= 20 15 | Sample Input 1 : 16 | 2 17 | Sample Output 1 : 18 | a b 19 | a c 20 | b c 21 | Sample Input 2 : 22 | 3 23 | Sample Output 2 : 24 | a c 25 | a b 26 | c b 27 | a c 28 | b a 29 | b c 30 | a c 31 | 32 | Solution:///////////////////////////// 33 | 34 | public class solution { 35 | 36 | public static void towerOfHanoi(int disks, char source, char auxiliary, char destination) { 37 | if(disks == 0){ 38 | return; 39 | } 40 | towerOfHanoi(disks-1, source, destination, auxiliary); 41 | System.out.println(source+ " "+ destination); 42 | towerOfHanoi(disks-1, auxiliary, source, destination); 43 | 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Data Strucutures In java/Lecture 13 Hash Maps/MapUse.java: -------------------------------------------------------------------------------- 1 | import java.util.HashMap; 2 | import java.util.Set; 3 | public class MapUse{ 4 | 5 | //main driver code 6 | public static void main(String[] args) { 7 | HashMap map = new HashMap<>(); 8 | 9 | //insert 10 | map.put("abc", 1); 11 | map.put("def", 2); 12 | map.put("abc1", 10); 13 | map.put("def1", 20); 14 | map.put("abc", 5); //updates abc from 1 to 5, every key is unique, no dupplicate keys 15 | //check size 16 | System.out.println("size "+ map.size()); 17 | 18 | //check Presence 19 | if(map.containsKey("abc")){ 20 | System.out.println("has abc"); 21 | } 22 | if(map.containsKey("abc1")){ 23 | System.out.println("has abc1"); 24 | } 25 | 26 | //get Value 27 | int v = 0; 28 | if(map.containsKey("abc1")){ //checks whether the key is actually present 29 | v = map.get("abc1"); //to protect from null pointer exception 30 | } 31 | System.out.println(v); 32 | 33 | //remove 34 | int s = map.remove("abc"); //removes the value and also returns the value 35 | System.out.println(s); 36 | 37 | //iterate 38 | Set keys = map.keySet(); 39 | //gives a set of all keys as a Set 40 | for(String str: keys){ 41 | System.out.println(str); 42 | } 43 | } 44 | 45 | } -------------------------------------------------------------------------------- /Introduction to Java/Lecture 12, Strings/MoreOnStrings.java: -------------------------------------------------------------------------------- 1 | 2 | 3 | //Some most commonly used functions on Strings 4 | public class MoreOnStrings { 5 | 6 | 7 | 8 | //Driver Code 9 | public static void main(String[] args) { 10 | String str1 = " Coding "; 11 | String str2 = "is awesome"; 12 | String str3= str1+str2; //+ is used to concatenate strings 13 | //you cant use comma to concatenate strings, 14 | //like you do in javascript 15 | System.out.println(str3); //Coding is awesome. 16 | System.out.println(str1.concat(str2)); //Coding is awesome. 17 | //str1.concat(str2) concats str2 to end of str1 18 | //it does not change str1 or str2 but returns a new string combing str2 to end of str1 19 | System.out.println(str2.concat(str1)); //concats str1 to end of str2 20 | //is awesome Coding 21 | System.out.println(str3.contains(str1)); //checks whether str1 is present in str3 or not 22 | //returns true in this case 23 | System.out.println(str1.contains(str2)); //checks whether str2 is present to str1 24 | //returns fasle 25 | System.out.println(str3.substring(0)); 26 | //prints everything from index 0 to end of str3 27 | // Coding is awesome is returned 28 | System.out.println(str3.substring(0,5)); 29 | //prints everything in str3 from 0 to 4, 5 is not included. 30 | //outputs Codi 31 | 32 | 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Data Strucutures In java/Lecture 11 Binary Trees/Exercises/printTreeLevelWise.txt: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Following is the structure used to represent the Binary Tree Node 4 | 5 | class BinaryTreeNode { 6 | T data; 7 | BinaryTreeNode left; 8 | BinaryTreeNode right; 9 | 10 | public BinaryTreeNode(T data) { 11 | this.data = data; 12 | this.left = null; 13 | this.right = null; 14 | } 15 | } 16 | 17 | */ 18 | import java.util.Queue; 19 | import java.util.LinkedList; 20 | public class Solution { 21 | 22 | public static void printLevelWise(BinaryTreeNode root) { 23 | //Your code goes here 24 | if(root == null){ 25 | return; 26 | } 27 | Queue> queue = new LinkedList<>(); 28 | queue.add(root); 29 | while(!queue.isEmpty()){ 30 | BinaryTreeNode frontNode = queue.poll(); 31 | System.out.print(frontNode.data+":"); 32 | if(frontNode.left!=null){ 33 | System.out.print("L:"+frontNode.left.data+","); 34 | queue.add(frontNode.left); 35 | 36 | }else{ 37 | System.out.print("L:"+-1+","); 38 | 39 | } 40 | if(frontNode.right!=null){ 41 | System.out.print("R:"+frontNode.right.data); 42 | queue.add(frontNode.right); 43 | 44 | }else{ 45 | System.out.print("R:"+-1); 46 | 47 | } 48 | System.out.println(); 49 | } 50 | } 51 | } -------------------------------------------------------------------------------- /Data Strucutures In java/Lecture 4 Recursion 2/Binary Search (Recursive).txt: -------------------------------------------------------------------------------- 1 | Given an integer sorted array (sorted in increasing order) and an element x, find the x in given array using binary search. Return the index of x. 2 | Return -1 if x is not present in the given array. 3 | Note : If given array size is even, take first mid. 4 | Input format : 5 | 6 | Line 1 : Array size 7 | 8 | Line 2 : Array elements (separated by space) 9 | 10 | Line 3 : x (element to be searched) 11 | 12 | Sample Input : 13 | 6 14 | 2 3 4 5 6 8 15 | 5 16 | Sample Output: 17 | 3 18 | 19 | Solutions://////////////// 20 | public class solution { 21 | 22 | // element - number to be searched 23 | private static int search(int arr[], int start, int end, int target){ 24 | 25 | 26 | 27 | int mid = (start+end)/2; 28 | 29 | 30 | if (start>=end){ 31 | return -1; 32 | } 33 | 34 | if(target ==arr[mid]){ 35 | return mid; 36 | } 37 | 38 | else if (target { 28 | T data; 29 | BinaryTreeNode left; 30 | BinaryTreeNode right; 31 | 32 | public BinaryTreeNode(T data) { 33 | this.data = data; 34 | this.left = null; 35 | this.right = null; 36 | } 37 | } 38 | 39 | */ 40 | 41 | public class Solution { 42 | 43 | public static void postOrder(BinaryTreeNode root) { 44 | //Your code goes here 45 | if(root==null){ 46 | return; 47 | } 48 | postOrder(root.left); 49 | postOrder(root.right); 50 | System.out.print(root.data+" "); 51 | 52 | } 53 | 54 | } -------------------------------------------------------------------------------- /Data Strucutures In java/Lecture 4 Recursion 2/Return Permutations - String.txt: -------------------------------------------------------------------------------- 1 | Given a string S, find and return all the possible permutations of the input string. 2 | Note 1 : The order of permutations is not important. 3 | Note 2 : If original string contains duplicate characters, permutations will also be duplicates. 4 | Input Format : 5 | String S 6 | Output Format : 7 | All permutations (in different lines) 8 | Sample Input : 9 | abc 10 | Sample Output : 11 | abc 12 | acb 13 | bac 14 | bca 15 | cab 16 | cba 17 | Solutions:////////////////////////////// 18 | public class solution { 19 | 20 | public static String[] permutationOfString(String input){ 21 | // Write your code here 22 | if(input.length() == 0){ 23 | return new String[1]; 24 | } 25 | if(input.length() == 1){ 26 | String[] arr = new String[1]; 27 | arr[0] = input; 28 | return arr; 29 | 30 | } 31 | char c = input.charAt(0); 32 | String[] temp = permutationOfString(input.substring(1)); 33 | String[] output = new String[temp.length * input.length()]; 34 | int k = 0; 35 | for(int i =0; i< temp.length; i++){ 36 | String newstr = temp[i]; 37 | for(int j=0; j { 29 | T data; 30 | BinaryTreeNode left; 31 | BinaryTreeNode right; 32 | 33 | public BinaryTreeNode(T data) { 34 | this.data = data; 35 | this.left = null; 36 | this.right = null; 37 | } 38 | } 39 | 40 | */ 41 | 42 | public class Solution { 43 | 44 | public static void preOrder(BinaryTreeNode root) { 45 | //Your code goes here 46 | if(root==null){ 47 | return; 48 | } 49 | System.out.print(root.data+" "); 50 | preOrder(root.left); 51 | 52 | preOrder(root.right); 53 | } 54 | } -------------------------------------------------------------------------------- /Data Strucutures In java/Lecture 17 DP 2/CodingNinjas Solutions/9. All Possible Ways.txt: -------------------------------------------------------------------------------- 1 | All possible ways 2 | Send Feedback 3 | Given two integers a and b. You need to find and return the count of possible ways in which we can represent the number a as the sum of unique integers raise to the power b. 4 | For example: if a = 10 and b = 2, only way to represent 10 as sum of unique integers raised to power 2 is- 5 | 10 = 1^2 + 3^2 6 | Hence, answer is 1. 7 | Note : x^y represents x raise to the power y 8 | Input Format: 9 | The first line of input contains two space separated integers, that denote the value of a and b. 10 | Output Format: 11 | The first and only line of output contains count of ways in which a can be represented as sum of unique integers raised to power b. 12 | Constraints : 13 | 1 <= a <= 10^4 14 | 1 < b <= 20 15 | Time Limit: 1 second 16 | Sample Input 1 : 17 | 10 2 18 | Sample Output 1 : 19 | 1 20 | Sample Input 2 : 21 | 100 2 22 | Sample Output 2 : 23 | 3 24 | Explanation: 25 | Following are the three ways: 26 | 1. 100 = 10^2 27 | 2. 100 = 8^2 + 6^2 28 | 3. 100 = 7^2+5^2+4^2+3^2+1^2 29 | Solution: 30 | 31 | public class Solution { 32 | 33 | 34 | 35 | 36 | public static int allWays(int x, int n) { 37 | 38 | return allWays(x,n,1); 39 | } 40 | public static int allWays(int x, int n,int num) { 41 | 42 | int val = x -(int)Math.pow(num,n); 43 | if(val==0){ 44 | return 1; 45 | } 46 | if(val<0){ 47 | return 0; 48 | } 49 | return allWays(x,n,num+1) + allWays(val,n,num+1); 50 | 51 | 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /Data Strucutures In java/Lecture 4 Recursion 2/Check AB.txt: -------------------------------------------------------------------------------- 1 | Suppose you have a string, S, made up of only 'a's and 'b's. Write a recursive function that checks if the string was generated using the following rules: 2 | a. The string begins with an 'a' 3 | b. Each 'a' is followed by nothing or an 'a' or "bb" 4 | c. Each "bb" is followed by nothing or an 'a' 5 | If all the rules are followed by the given string, return true otherwise return false. 6 | Input format : 7 | String S 8 | Output format : 9 | 'true' or 'false' 10 | Constraints : 11 | 1 <= |S| <= 1000 12 | where |S| represents length of string S. 13 | Sample Input 1 : 14 | abb 15 | Sample Output 1 : 16 | true 17 | Sample Input 2 : 18 | abababa 19 | Sample Output 2 : 20 | false 21 | Explanation for Sample Input 2 22 | In the above example, a is not followed by either "a" or "bb", instead it's followed by "b" which results in false to be returned. 23 | 24 | Solutions:////////////////////////////// 25 | public class Solution { 26 | 27 | public static boolean checkAB(String input) { 28 | // Write your code here 29 | if (input.length()==0) 30 | { 31 | return true; 32 | } 33 | 34 | if (input.charAt(0) != 'a') 35 | { 36 | return false; 37 | } 38 | 39 | if (input.length() >= 3 && input.substring(0,3).equals("abb")) 40 | { 41 | return checkAB(input.substring(3)); 42 | } 43 | else 44 | { 45 | return checkAB(input.substring(1)); 46 | } 47 | 48 | } 49 | 50 | } -------------------------------------------------------------------------------- /Introduction to Java/Lecture 9 Arrays 1/Exercises/ArraySum.java: -------------------------------------------------------------------------------- 1 | /* 2 | Arrays Sum 3 | 4 | Given an array/list(ARR) of length N, you need to find and return the sum of all the elements in the array/list. 5 | Input Format : 6 | The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow. 7 | 8 | The first line of each test case or query contains an integer 'N' representing the size of the array/list. 9 | 10 | Second line contains 'N' single space separated integers representing the elements in the array/list. 11 | Output Format : 12 | For each test case, print the sum of the numbers in the array/list. 13 | 14 | Output for every test case will be printed in a separate line. 15 | Constraints : 16 | 1 <= t <= 10^2 17 | 0 <= N <= 10^5 18 | 19 | Time Limit: 1sec 20 | Sample Input 1: 21 | 1 22 | 3 23 | 9 8 9 24 | Sample Output 1: 25 | 26 26 | Sample Input 2: 27 | 2 28 | 5 29 | 1 2 3 4 5 30 | 3 31 | 10 20 30 32 | Sample Output 2: 33 | 15 34 | 60 35 | */ 36 | public class ArraySum { 37 | // Coding Ninjas Solution Class 38 | public static int sum(int[] arr) { 39 | // Your code goes here 40 | int length = arr.length; 41 | int sum = 0; 42 | for (int i = 0; i < length; i++) { 43 | sum = sum + arr[i]; 44 | } 45 | return sum; 46 | } 47 | 48 | //main code to check the program 49 | public static void main(String[] args) { 50 | int arr[] = {1, 2,3}; 51 | System.out.println(sum(arr)); //outputs 6 the solution code is ok 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /Data Strucutures In java/Lecture 5 Time and Complexity Analysis/Find the Unique Element.txt: -------------------------------------------------------------------------------- 1 | You have been given an integer array/list(ARR) of size N. Where N is equal to [2M + 1]. 2 | Now, in the given array/list, 'M' numbers are present twice and one number is present only once. 3 | You need to find and return that number which is unique in the array/list. 4 | Note: 5 | Unique element is always present in the array/list according to the given condition. 6 | Input format : 7 | The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow. 8 | 9 | First line of each test case or query contains an integer 'N' representing the size of the array/list. 10 | 11 | Second line contains 'N' single space separated integers representing the elements in the array/list. 12 | Output Format : 13 | For each test case, print the unique element present in the array. 14 | 15 | Output for every test case will be printed in a separate line. 16 | Constraints : 17 | 1 <= t <= 10^2 18 | 0 <= N <= 10^6 19 | 20 | Time Limit: 1 sec 21 | Sample Input 1: 22 | 1 23 | 7 24 | 2 3 1 6 3 6 2 25 | Sample Output 1: 26 | 1 27 | Sample Input 2: 28 | 2 29 | 5 30 | 2 4 7 2 7 31 | 9 32 | 1 3 1 3 6 6 7 10 7 33 | Sample Output 2: 34 | 4 35 | 10 36 | Solutions://///////////////////////////// 37 | public class Solution { 38 | 39 | public static int findUnique(int[] arr) { 40 | //Your code goes here 41 | int val=0; 42 | for (int i=0;i root) { 38 | //Your code goes here. 39 | if(root == null){ 40 | return 0; 41 | } 42 | int smallSum = getSum(root.left) + getSum(root.right); 43 | return root.data + smallSum; 44 | 45 | } 46 | 47 | } -------------------------------------------------------------------------------- /Data Strucutures In java/Test 1/Does s contain t.txt: -------------------------------------------------------------------------------- 1 | Given two string s and t, write a function to check if s contains all characters of t (in the same order as they are in string t). 2 | Return true or false. 3 | Do it recursively. 4 | E.g. : s = “abchjsgsuohhdhyrikkknddg” contains all characters of t=”coding” in the same order. So function will return true. 5 | Input Format : 6 | Line 1 : String s 7 | Line 2 : String t 8 | Output Format : 9 | true or false 10 | Sample Input 1 : 11 | abchjsgsuohhdhyrikkknddg 12 | coding 13 | Sample Output 1 : 14 | true 15 | Sample Input 2 : 16 | abcde 17 | aeb 18 | Sample Output 2 : 19 | false 20 | Solutions:///////////////////////////// 21 | 22 | public class Solution { 23 | public static boolean checkSequence(String a, String b) { 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 | 31 | return checkSequence(a,b,0,0); 32 | } 33 | 34 | public static boolean checkSequence(String a, String b, int a_idx, int b_idx) 35 | { 36 | if (b_idx==b.length()) 37 | { 38 | return true; 39 | } 40 | if (a_idx==a.length()) 41 | { 42 | return false; 43 | } 44 | if (b.charAt(b_idx)==a.charAt(a_idx)) 45 | { 46 | return checkSequence(a,b,a_idx+1,b_idx+1); 47 | } 48 | else 49 | { 50 | return checkSequence(a,b,a_idx+1,b_idx); 51 | } 52 | 53 | } 54 | } -------------------------------------------------------------------------------- /Introduction to Java/Lecture 12, Strings/Exercises/CountWords.java: -------------------------------------------------------------------------------- 1 | /* 2 | Count Words 3 | 4 | For a given input string(str), find and return the total number of words present in it. 5 | It is assumed that two words will have only a single space in between. Also, there wouldn't be any leading and trailing spaces in the given input string. 6 | Input Format: 7 | The first and only line of input contains a string without any leading and trailing spaces. 8 | Output Format: 9 | The only line of output prints an integer value denoting the tool number of words present in the string. 10 | Note: 11 | You are not required to print anything. It has already been taken care of. 12 | Constraints: 13 | 0 <= N <= 10^6 14 | Where N is the length of the input string. 15 | 16 | Time Limit: 1 sec 17 | Sample Input 1: 18 | Coding Ninjas! 19 | Sample Output 1: 20 | 2 21 | Sample Input 2: 22 | this is a sample string 23 | Sample Output 2: 24 | 5 25 | */ 26 | package Exercises; 27 | public class CountWords{ 28 | //coding ninjas solution 29 | public static int countWords(String str) { 30 | //Your code goes here 31 | int n = str.length(); 32 | if(n==0){ 33 | return 0; 34 | } 35 | int count = 1; 36 | for(int i=0;i arr[parentIndex]) { 34 | return false; 35 | } 36 | if(rightChildIndex < arr.length && arr[rightChildIndex] > arr[parentIndex]) { 37 | return false; 38 | } 39 | 40 | 41 | } 42 | return true; 43 | 44 | } 45 | } -------------------------------------------------------------------------------- /Data Strucutures In java/Lecture 16 DP 1/Coding Ninjas Solutions/Code Number of Balanced BTs.txt: -------------------------------------------------------------------------------- 1 | Code: Number of Balanced BTs 2 | Send Feedback 3 | Given an integer h, find the possible number of balanced binary trees of height h. You just need to return the count of possible binary trees which are balanced. 4 | This number can be huge, so, return output modulus 10^9 + 7. 5 | Write a simple recursive solution. 6 | Input Format : 7 | The first and only line of input contains an integer, that denotes the value of h. Here, h is the height of the tree. 8 | Output Format : 9 | The first and only line of output contains the count of balanced binary trees modulus 10^9 + 7. 10 | Constraints : 11 | 1 <= h <= 24 12 | Time Limit: 1 sec 13 | Sample Input 1: 14 | 3 15 | Sample Output 1: 16 | 15 17 | Sample Input 2: 18 | 4 19 | Sample Output 2: 20 | 315 21 | Solution:=========================================================================== 22 | public class Solution { 23 | 24 | private static long balancedBTs(long n,int mod){ 25 | if(n==0||n==1){ 26 | return 1; 27 | } 28 | long x=balancedBTs(n-1); 29 | long y=balancedBTs(n-2); 30 | return (x*x +2*x*y)%mod; 31 | } 32 | 33 | public static long balancedBTs(long n){ 34 | 35 | /* Your class should be named Solution 36 | * Don't write main(). 37 | * Don't read input, it is passed as function argument. 38 | * Return output and don't print it. 39 | * Taking input and printing output is handled automatically. 40 | */ 41 | 42 | int mod=(int)Math.pow(10,9)+7; 43 | return balancedBTs(n,mod); 44 | 45 | } 46 | } -------------------------------------------------------------------------------- /Introduction to Java/Lecture 2, Getting Started with Java/InputOutputExample.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Scanner; 3 | 4 | public class InputOutputExample { 5 | 6 | public static void main(String[] args) { 7 | // TODO Auto-generated method stub 8 | Scanner s = new Scanner(System.in); 9 | //Case 1 10 | // int a = s.nextInt(); 11 | // int b = s.nextInt(); 12 | // System.out.println(a+b); //here when we give 5 15 as input a=5, b=15, and we get 20 as answer. 13 | // String str = s.next(); //here it takes only one word as input , so if we give Coding Ninjas as Input 14 | // System.out.println(str); //we get coding as output, because it is first word. 15 | // 16 | // //Case 2 17 | // int a = s.nextInt(); 18 | // String str = s.next(); 19 | // System.out.print(a); 20 | // System.out.println(str); //for input 10 abc def, output is 10abc 21 | // 22 | // //Case 3 23 | // String str = s.next(); 24 | // int a = s.nextInt(); 25 | // System.out.println(str + " "+ a); 26 | //for input abc def 10, ouput is Input Mismatch Exception 27 | //s.next() scans and returns abc the next token as String. 28 | //a token is part of entered line that is separated from other tokens by space, newline or tab. 29 | //So, when Input is "10 abc def " then s.next() returns the first token as string i.e "abc" and 30 | //s.nextInt() tries to convert the next token "def" into int, which gives Input Mismatch Exception. 31 | s.close(); 32 | 33 | 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /Data Strucutures In java/Lecture 13 Hash Maps/Assignments/ExtractUniqueCharacters.java: -------------------------------------------------------------------------------- 1 | /* 2 | Extract Unique characters 3 | Send Feedback 4 | Given a string S, you need to remove all the duplicates. That means, the output string should contain each character only once. The respective order of characters should remain same, as in the input string. 5 | Input format: 6 | The first and only line of input contains a string, that denotes the value of S. 7 | Output format : 8 | The first and only line of output contains the updated string, as described in the task. 9 | Constraints : 10 | 0 <= Length of S <= 10^8 11 | Time Limit: 1 sec 12 | Sample Input 1 : 13 | ababacd 14 | Sample Output 1 : 15 | abcd 16 | Sample Input 2 : 17 | abcde 18 | Sample Output 2 : 19 | abcde 20 | */ 21 | 22 | package hashMapImplementation; 23 | import java.util.HashMap; 24 | public class ExtractUniqueCharacters { 25 | public static String uniqueChar(String str){ 26 | if(str.length()==0) { 27 | return ""; 28 | } 29 | String ans = ""; 30 | HashMap map = new HashMap<>(); 31 | for(int currentIndex=0;currentIndex=1) { 62 | System.out.print(printValueRight); 63 | printValueRight--; 64 | decreasing--; 65 | } 66 | System.out.println(); 67 | rows++; 68 | } 69 | s.close(); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /Data Strucutures In java/Lecture 14 Priority Queues/CodingNinjasSolutions/kLargestElements.txt: -------------------------------------------------------------------------------- 1 | /* 2 | Code : K largest elements 3 | Send Feedback 4 | You are given with an integer k and an array of integers that contain numbers in random order. Write a program to find k largest numbers from given array. You need to save them in an array and return it. 5 | Time complexity should be O(nlogk) and space complexity should be not more than O(k). 6 | Order of elements in the output is not important. 7 | Input Format : 8 | Line 1 : Size of array (n) 9 | Line 2 : Array elements (separated by space) 10 | Line 3 : Integer k 11 | Output Format : 12 | k largest elements 13 | Sample Input : 14 | 13 15 | 2 12 9 16 10 5 3 20 25 11 1 8 6 16 | 4 17 | Sample Output : 18 | 12 19 | 16 20 | 20 21 | 25 22 | */ 23 | import java.util.ArrayList; 24 | import java.util.PriorityQueue; 25 | public class Solution { 26 | 27 | public static ArrayList kLargest(int input[], int k) { 28 | /* Your class should be named Solution 29 | * Don't write main(). 30 | * Don't read input, it is passed as function argument. 31 | * Return output and don't print it. 32 | * Taking input and printing output is handled automatically. 33 | */ 34 | 35 | PriorityQueue priorityQueue = new PriorityQueue<>(); 36 | for(int i=0;i ans = new ArrayList<>(); 46 | while(!priorityQueue.isEmpty()) { 47 | ans.add(priorityQueue.remove()); 48 | } 49 | return ans; 50 | } 51 | } -------------------------------------------------------------------------------- /Introduction to Java/Lecture 7, Operators and For Loop/Assignments/SumOrProduct.java: -------------------------------------------------------------------------------- 1 | /* 2 | Sum or Product 3 | 4 | Write a program that asks the user for a number N and a choice C. And then give them the possibility to choose between computing the sum and computing the product of all integers in the range 1 to N (both inclusive). 5 | If C is equal to - 6 | 1, then print the sum 7 | 2, then print the product 8 | Any other number, then print '-1' (without the quotes) 9 | Input format : 10 | Line 1 : Integer N 11 | Line 2 : Choice C 12 | Output Format : 13 | Sum or product according to user's choice 14 | Constraints : 15 | 1 <= N <= 12 16 | Sample Input 1 : 17 | 10 18 | 1 19 | Sample Output 1 : 20 | 55 21 | Sample Input 2 : 22 | 10 23 | 2 24 | Sample Output 2 : 25 | 3628800 26 | Sample Input 3 : 27 | 10 28 | 4 29 | Sample Output 3 : 30 | -1 31 | */ 32 | package lecture7.Assignments; 33 | import java.util.Scanner; 34 | public class SumOrProduct { 35 | 36 | public static void main(String[] args) { 37 | // TODO Auto-generated method stub 38 | Scanner s = new Scanner(System.in); 39 | int n = s.nextInt(); 40 | int choice = s.nextInt(); 41 | if(choice == 1){ 42 | int sum = 0; 43 | for(int i=1;i<=n;i++){ 44 | sum = sum +i; 45 | } 46 | System.out.println(sum); 47 | }else if(choice == 2){ 48 | int mul = 1; 49 | for(int i=1;i<=n;i++){ 50 | mul = mul * i; 51 | } 52 | System.out.println(mul); 53 | }else{ 54 | System.out.println(-1); 55 | } 56 | s.close(); 57 | 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /Data Strucutures In java/Lecture 16 DP 1/Coding Ninjas Solutions/Code Number of Balanced BTs Using DP.txt: -------------------------------------------------------------------------------- 1 | Code: Number of Balanced BTs Using DP 2 | Send Feedback 3 | Given an integer h, find the possible number of balanced binary trees of height h. You just need to return the count of possible binary trees which are balanced. 4 | This number can be huge, so, return output modulus 10^9 + 7. 5 | Time complexity should be O(h). 6 | Input Format : 7 | The first and only line of input contains an integer, that denotes the value of h. Here, h is the height of the tree. 8 | Output Format : 9 | The first and only line of output contains the count of balanced binary trees modulus 10^9 + 7. 10 | Constraints : 11 | 1 <= h <= 10^6 12 | Time Limit: 1 sec 13 | Sample Input 1: 14 | 3 15 | Sample Output 1: 16 | 15 17 | Sample Input 2: 18 | 4 19 | Sample Output 2: 20 | 315 21 | Solution================================================================= 22 | 23 | public class Solution { 24 | 25 | public static int balancedBTs(int height){ 26 | if(height==0 || height==1) 27 | return 1; 28 | if(height==2) 29 | return 3; 30 | int storage[]=new int[height+1]; 31 | storage[0]=1; 32 | storage[1]=1; 33 | storage[2]=3; 34 | int mod=(int)Math.pow(10,9)+7; 35 | for(int i=3;i storage[i / 3]){ 45 | min = storage[i / 3]; 46 | } 47 | } 48 | if(i % 2 == 0){ 49 | if(min > storage[i / 2]){ 50 | min = storage[i / 2]; 51 | } 52 | } 53 | storage[i] = 1 + min; 54 | } 55 | return storage[n]; 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /Data Strucutures In java/Lecture 16 DP 1/Coding Ninjas Solutions/Code Min Steps to One.txt: -------------------------------------------------------------------------------- 1 | Code: Min Steps to One 2 | Send Feedback 3 | Given a positive integer 'n', find and return the minimum number of steps that 'n' has to take to get reduced to 1. You can perform any one of the following 3 steps: 4 | 1.) Subtract 1 from it. (n = n - ­1) , 5 | 2.) If its divisible by 2, divide by 2.( if n % 2 == 0, then n = n / 2 ) , 6 | 3.) If its divisible by 3, divide by 3. (if n % 3 == 0, then n = n / 3 ). 7 | Write brute-force recursive solution for this. 8 | Input format : 9 | The first and the only line of input contains an integer value, 'n'. 10 | Output format : 11 | Print the minimum number of steps. 12 | Constraints : 13 | 1 <= n <= 200 14 | 15 | Time Limit: 1 sec 16 | Sample Input 1 : 17 | 4 18 | Sample Output 1 : 19 | 2 20 | Explanation of Sample Output 1 : 21 | For n = 4 22 | Step 1 : n = 4 / 2 = 2 23 | Step 2 : n = 2 / 2 = 1 24 | Sample Input 2 : 25 | 7 26 | Sample Output 2 : 27 | 3 28 | Explanation of Sample Output 2 : 29 | For n = 7 30 | Step 1 : n = 7 ­- 1 = 6 31 | Step 2 : n = 6 / 3 = 2 32 | Step 3 : n = 2 / 2 = 1 33 | Solution 34 | Dp/// 35 | public class Solution { 36 | 37 | public static int countMinStepsToOne(int n) { 38 | //Your code goes here 39 | if(n == 1){ 40 | return 0; 41 | } 42 | int op1 = countMinStepsToOne(n - 1); 43 | int minSteps = op1; 44 | if(n % 3 == 0){ 45 | int op2 = countMinStepsToOne(n / 3); 46 | if(op2 < minSteps){ 47 | minSteps = op2; 48 | } 49 | } 50 | if(n % 2 == 0){ 51 | int op3 = countMinStepsToOne(n / 2); 52 | if(op3 < minSteps){ 53 | minSteps = op3; 54 | } 55 | } 56 | return 1 + minSteps; 57 | 58 | } 59 | 60 | } -------------------------------------------------------------------------------- /Data Strucutures In java/Lecture 7 Linked List 1/Length of LL.txt: -------------------------------------------------------------------------------- 1 | For a given singly linked list of integers, find and return its length. Do it using an iterative method. 2 | Input format : 3 | The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow. 4 | 5 | First and the only line of each test case or query contains elements of the singly linked list separated by a single space. 6 | Remember/Consider : 7 | While specifying the list elements for input, -1 indicates the end of the singly linked list and hence, would never be a list element. 8 | Output format : 9 | For each test case, print the length of the linked list. 10 | 11 | Output for every test case will be printed in a separate line. 12 | Constraints : 13 | 1 <= t <= 10^2 14 | 0 <= N <= 10^5 15 | Time Limit: 1 sec 16 | Sample Input 1 : 17 | 1 18 | 3 4 5 2 6 1 9 -1 19 | Sample Output 1 : 20 | 7 21 | Sample Input 2 : 22 | 2 23 | 10 76 39 -3 2 9 -23 9 -1 24 | -1 25 | Sample Output 2 : 26 | 8 27 | 0 28 | Solutions:///////////////////// 29 | /* 30 | 31 | Following is the Node class already written for the Linked List 32 | 33 | class LinkedListNode { 34 | T data; 35 | LinkedListNode next; 36 | 37 | public Node(T data) { 38 | this.data = data; 39 | } 40 | } 41 | 42 | */ 43 | 44 | 45 | public class Solution { 46 | 47 | public static int length(LinkedListNode head){ 48 | if (head == null){ 49 | return 0; 50 | } 51 | 52 | LinkedListNode temp = head; 53 | int count = 1; 54 | while(temp.next!=null){ 55 | count++; 56 | temp = temp.next; 57 | } 58 | return count; 59 | 60 | } 61 | } -------------------------------------------------------------------------------- /Introduction to Java/Lecture 12, Strings/Assignments/RemoveCharcater.java: -------------------------------------------------------------------------------- 1 | /* 2 | Remove character 3 | 4 | For a given a string(str) and a character X, write a function to remove all the occurrences of X from the given string. 5 | The input string will remain unchanged if the given character(X) doesn't exist in the input string. 6 | Input Format: 7 | The first line of input contains a string without any leading and trailing spaces. 8 | 9 | The second line of input contains a character(X) without any leading and trailing spaces. 10 | Output Format: 11 | The only line of output prints the updated string. 12 | Note: 13 | You are not required to print anything explicitly. It has already been taken care of. 14 | Constraints: 15 | 0 <= N <= 10^6 16 | Where N is the length of the input string. 17 | 18 | Time Limit: 1 second 19 | Sample Input 1: 20 | aabccbaa 21 | a 22 | Sample Output 1: 23 | bccb 24 | Sample Input 2: 25 | xxyyzxx 26 | y 27 | Sample Output 2: 28 | xxzxx 29 | */ 30 | package Assignments; 31 | public class RemoveCharcater { 32 | //coding ninjas Solution 33 | public static String removeAllOccurrencesOfChar(String str, char ch) { 34 | // Your code goes here 35 | int n = str.length(); 36 | if(n==0){ //base case for empty string 37 | return str; 38 | } 39 | String result = ""; 40 | for(int i=0;i { 23 | T data; 24 | BinaryTreeNode left; 25 | BinaryTreeNode right; 26 | 27 | public BinaryTreeNode(T data) { 28 | this.data = data; 29 | } 30 | } 31 | */ 32 | public static BinaryTreeNode SortedArrayToBST(int[] arr,int si,int ei) 33 | { 34 | 35 | if(si>ei) 36 | return null; 37 | int mid=(si+ei)/2; 38 | BinaryTreeNode root=new BinaryTreeNode<>(arr[mid]); 39 | 40 | root.left=SortedArrayToBST(arr,si,mid-1); 41 | root.right=SortedArrayToBST(arr,mid+1,ei); 42 | return root; 43 | 44 | } 45 | 46 | public static BinaryTreeNode SortedArrayToBST(int[] arr, int n) 47 | { 48 | return SortedArrayToBST(arr, 0, n-1); 49 | } 50 | } -------------------------------------------------------------------------------- /Introduction to Java/Lecture 4, Conditionals and Loops/Assignments/EvenOddSum.java: -------------------------------------------------------------------------------- 1 | /*Sum of even & odd 2 | 3 | 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. 4 | Digits mean numbers, not the places! That is, if the given integer is "13245", even digits are 2 & 4 and odd digits are 1, 3 & 5. 5 | Input format : 6 | Integer N 7 | Output format : 8 | Sum_of_Even_Digits Sum_of_Odd_Digits 9 | (Print first even sum and then odd sum separated by space) 10 | Constraints 11 | 0 <= N <= 10^8 12 | Sample Input 1: 13 | 1234 14 | Sample Output 1: 15 | 6 4 16 | Sample Input 2: 17 | 552245 18 | Sample Output 2: 19 | 8 15 20 | Explanation for Input 2: 21 | For the given input, the even digits are 2, 2 and 4 and if we take the sum of these digits it will come out to be 8(2 + 2 + 4) and similarly, if we look at the odd digits, they are, 5, 5 and 5 which makes a sum of 15(5 + 5 + 5). Hence the answer would be, 8(evenSum) 15(oddSum) 22 | */ 23 | 24 | 25 | import java.util.Scanner; 26 | public class EvenOddSum { 27 | 28 | public static void main(String[] args) { 29 | // TODO Auto-generated method stub 30 | Scanner s = new Scanner(System.in); 31 | int number = s.nextInt(); 32 | int evenSum = 0; 33 | int oddSum = 0; 34 | while(number>0){ 35 | int lastNumber = number%10; 36 | if(lastNumber%2==0){ 37 | evenSum += lastNumber; 38 | }else{ 39 | oddSum += lastNumber; 40 | } 41 | number = number/10; 42 | 43 | } 44 | System.out.println(evenSum + "\t"+ oddSum); 45 | s.close(); 46 | 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /Introduction to Java/Test 2/ProbabilityOfBalls.java: -------------------------------------------------------------------------------- 1 | /* 2 | An urn contains 8 balls : 4 red , 2 blue and 2 green. Now n balls are drawn out of the urn. Calculate the probability that out of n drawn balls exactly x balls are red. 3 | Find your answer multiplied by 100 and return the integer part. 4 | All required values for computation will be in integer range. 5 | x is always less than equal to 4 and x is always less than equal to n. 6 | 3 1 7 | 42 8 | */ 9 | 10 | package test2; 11 | 12 | import java.util.Scanner; 13 | 14 | public class ProbabilityOfBalls { 15 | public class Solution { 16 | 17 | public static int factorial(int n) { 18 | int fact = 1; 19 | for (int i = 1; i <= n; i++) { 20 | fact = fact * i; 21 | } 22 | return fact; 23 | } 24 | 25 | public static int NCR(int n, int r) { 26 | int factn = factorial(n); 27 | int factr = factorial(r); 28 | int factnr = factorial(n - r); 29 | return factn / (factr * factnr); 30 | 31 | } 32 | 33 | public static int probability(int n, int x) { 34 | 35 | /* 36 | * Your class should be named Solution Don't write main(). Don't read input, it 37 | * is passed as function argument. Return output and don't print it. Taking 38 | * input and printing output is handled automatically. 39 | */ 40 | int favourableOutcome = NCR(4, x) * NCR(4, n - x); 41 | int totalOutcome = NCR(8, n); 42 | int ans = (favourableOutcome*100) / totalOutcome; 43 | return ans; 44 | 45 | } 46 | 47 | } 48 | 49 | public static void main(String[] args) { 50 | // TODO Auto-generated method stub 51 | Scanner s = new Scanner(System.in); 52 | int n = s.nextInt(); 53 | int x = s.nextInt(); 54 | System.out.println(Solution.probability(n, x)); 55 | s.close(); 56 | 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /Introduction to Java/Lecture 9 Arrays 1/Assignments/Sort01.java: -------------------------------------------------------------------------------- 1 | /* 2 | Sort 0 1 3 | 4 | You have been given an integer array/list(ARR) of size N that contains only integers, 0 and 1. Write a function to sort this array/list. Think of a solution which scans the array/list only once and don't require use of an extra array/list. 5 | Note: 6 | You need to change in the given array/list itself. Hence, no need to return or print anything. 7 | Input format : 8 | The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow. 9 | 10 | First line of each test case or query contains an integer 'N' representing the size of the array/list. 11 | 12 | Second line contains 'N' single space separated integers(all 0s and 1s) representing the elements in the array/list. 13 | Output format : 14 | For each test case, print the sorted array/list elements in a row separated by a single space. 15 | 16 | Output for every test case will be printed in a separate line. 17 | Constraints : 18 | 1 <= t <= 10^2 19 | 0 <= N <= 10^5 20 | Time Limit: 1 sec 21 | Sample Input 1: 22 | 1 23 | 7 24 | 0 1 1 0 1 0 1 25 | Sample Output 1: 26 | 0 0 0 1 1 1 1 27 | Sample Input 2: 28 | 2 29 | 8 30 | 1 0 1 1 0 1 0 1 31 | 5 32 | 0 1 0 1 0 33 | Sample Output 2: 34 | 0 0 0 1 1 1 1 1 35 | 0 0 0 1 1 36 | */ 37 | public class Sort01 { 38 | //coding ninjas solution 39 | public static void sortZeroesAndOne(int[] arr) { 40 | //Your code goes here 41 | int nextZeros = 0; 42 | for(int i=0;i { 24 | T data; 25 | BinaryTreeNode left; 26 | BinaryTreeNode right; 27 | 28 | public BinaryTreeNode(T data) { 29 | this.data = data; 30 | } 31 | } 32 | */ 33 | 34 | public static void elementsInRangeK1K2(BinaryTreeNode root,int k1,int k2){ 35 | if(root==null) 36 | return ; 37 | if(k1< root.data ) { 38 | elementsInRangeK1K2(root.left, k1, k2); 39 | } 40 | if(root.data >=k1 && root.data <=k2) { 41 | System.out.print(root.data+" "); 42 | } 43 | 44 | if(k2>root.data) { 45 | elementsInRangeK1K2(root.right, k1, k2); 46 | } 47 | 48 | 49 | } 50 | 51 | 52 | } -------------------------------------------------------------------------------- /Introduction to Java/Lecture 10 Arrays 2, Searching and Sorting/Exercises/MergeSort.java: -------------------------------------------------------------------------------- 1 | //Given a sorted arary1 of size m. 2 | //Given a sorted array2 of size n. 3 | //Create a new sorted Array of size(m+n) by combining two ararys. 4 | 5 | public class MergeSort{ 6 | 7 | public static int[] mergeSort(int arr1[], int arr2[]){ 8 | int m = arr1.length; 9 | int n = arr2.length; 10 | int arr3[] = new int[m+n]; 11 | int i=0, j=0, k=0; //Initialize all pointers for arr1, arr2, arr3 to i, j and k and initialize them to zero. 12 | while(i=2){ 32 | char firstTwoChar = (char)((input.charAt(0)-'0')*10 + (input.charAt(1)-'0')+'a'-1); 33 | 34 | if (firstTwoChar>='a' && firstTwoChar <='z'){ 35 | printAllPossibleCodes(input.substring(2), output + firstTwoChar); 36 | } 37 | } 38 | 39 | 40 | // printAllPossibleCodes(input.substring(1), output); 41 | // printAllPossibleCodes(input.substring(1),output + input.charAt(0)); 42 | } 43 | 44 | 45 | public static void printAllPossibleCodes(String input) { 46 | // Write your code here 47 | 48 | //int start = 0; 49 | String output = ""; 50 | printAllPossibleCodes(input,output); 51 | 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /Data Strucutures In java/Lecture 14 Priority Queues/StringLength.java: -------------------------------------------------------------------------------- 1 | import java.util.Comparator; 2 | import java.util.PriorityQueue; 3 | 4 | class StringLengthComparator implements Comparator 5 | { 6 | public int compare(String o1, String o2) 7 | { 8 | if(o1.length()o2.length()){ 11 | return 1; 12 | }else{ 13 | return 0; 14 | } 15 | 16 | } 17 | 18 | } 19 | class StringLengthComparator1 implements Comparator 20 | { 21 | public int compare(String o1, String o2) 22 | { 23 | if(o1.length()o2.length()){ 26 | return -1;//means here we swap it 27 | }else{ 28 | return 0; 29 | } 30 | 31 | } 32 | 33 | } 34 | 35 | 36 | public class StringLength 37 | { public static void main(String[] args) 38 | { 39 | String arr[] = {"this", "at", "a", "their", "queues"}; 40 | StringLengthComparator stringComparator = new StringLengthComparator(); 41 | StringLengthComparator1 stringComparator1 = new StringLengthComparator1(); 42 | PriorityQueue pq = new PriorityQueue<>(stringComparator); 43 | PriorityQueue pq1 = new PriorityQueue<>(stringComparator1); 44 | 45 | for(int i=0;i { T data; BinaryTreeNode left; BinaryTreeNode right; 29 | * 30 | * public BinaryTreeNode(T data) { this.data = data; } } 31 | */ 32 | 33 | 34 | public static boolean searchInBST(BinaryTreeNode root, int data) { 35 | if(root == null){ 36 | return false; 37 | } 38 | if(root.data == data){ 39 | return true; 40 | } 41 | if(data < root.data){ 42 | return searchInBST(root.left, data); 43 | 44 | }else{ 45 | return searchInBST(root.right, data); 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /Introduction to Java/Lecture 12, Strings/Exercises/ReverseStringWordWise.java: -------------------------------------------------------------------------------- 1 | /* 2 | Reverse String Word Wise 3 | 4 | Reverse the given string word wise. That is, the last word in given string should come at 1st place, last second word at 2nd place and so on. Individual words should remain as it is. 5 | Input format 6 | String in a single line 7 | Output format 8 | Word wise reversed string in a single line 9 | Constraints 10 | 0 = S = 10^7 11 | where S represents the length of string, S. 12 | Sample Input 1 13 | Welcome to Coding Ninjas 14 | Sample Output 1 15 | Ninjas Coding to Welcome 16 | Sample Input 2 17 | Always indent your code 18 | Sample Output 2 19 | code your indent Always 20 | */ 21 | 22 | package Exercises; 23 | public class ReverseStringWordWise { 24 | //coding ninjas solution code 25 | public static String reverseWordWise(String str) { 26 | // Write your code here 27 | if(str.length()==0){ 28 | return ""; 29 | } 30 | int n= str.length(); 31 | int currentWordStart; 32 | int currentWordEnd=n; 33 | String reversedString = ""; 34 | int i; 35 | for(i=n-1;i>=0;i--){ 36 | if(str.charAt(i)==' '){ 37 | currentWordStart=i+1; 38 | reversedString += str.substring(currentWordStart, currentWordEnd)+ " "; 39 | currentWordEnd = i; 40 | } 41 | } 42 | //System.out.println(currentWordEnd); 43 | //thi swe are doing for the first word. 44 | currentWordStart=i+1; 45 | reversedString += str.substring(currentWordStart, currentWordEnd); 46 | return reversedString; 47 | } 48 | 49 | //main driver code to check solution 50 | public static void main(String[] args) { 51 | String str = "Welcome to"; 52 | String result = reverseWordWise(str); 53 | System.out.println(result); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /Introduction to Java/Lecture 9 Arrays 1/Assignments/PairSum.java: -------------------------------------------------------------------------------- 1 | /* 2 | Pair Sum 3 | 4 | You have been given an integer array/list(ARR) and a number X. Find and return the total number of pairs in the array/list which sum to X. 5 | Note: 6 | Given array/list can contain duplicate elements. 7 | Input format : 8 | The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow. 9 | 10 | First line of each test case or query contains an integer 'N' representing the size of the first array/list. 11 | 12 | Second line contains 'N' single space separated integers representing the elements in the array/list. 13 | 14 | Third line contains an integer 'X'. 15 | Output format : 16 | For each test case, print the total number of pairs present in the array/list. 17 | 18 | Output for every test case will be printed in a separate line. 19 | Constraints : 20 | 1 <= t <= 10^2 21 | 0 <= N <= 10^3 22 | 0 <= X <= 10^9 23 | Time Limit: 1 sec 24 | Sample Input 1: 25 | 1 26 | 9 27 | 1 3 6 2 5 4 3 2 4 28 | 7 29 | Sample Output 1: 30 | 7 31 | Sample Input 2: 32 | 2 33 | 9 34 | 1 3 6 2 5 4 3 2 4 35 | 12 36 | 6 37 | 2 8 10 5 -2 5 38 | 10 39 | Sample Output 2: 40 | 0 41 | 2 42 | 43 | 44 | Explanation for Input 2: 45 | Since there doesn't exist any pair with sum equal to 12 for the first query, we print 0. 46 | 47 | For the second query, we have 2 pairs in total that sum up to 10. They are, (2, 8) and (5, 5). 48 | */ 49 | public class PairSum { 50 | //coding ninjas solution part 51 | public static int pairSum(int arr[], int x) { 52 | //Your code goes here 53 | int numPairs = 0; 54 | for(int i=0;i input, Stack extra) { 37 | if (input.size()==0 || input.size()==1) 38 | { 39 | return; 40 | } 41 | 42 | int top=input.pop(); 43 | reverseStack(input,extra); 44 | 45 | while(!input.isEmpty()) 46 | { 47 | extra.push(input.pop()); 48 | } 49 | input.push(top); 50 | 51 | while(!extra.isEmpty()) 52 | { 53 | input.push(extra.pop()); 54 | } 55 | 56 | } 57 | } -------------------------------------------------------------------------------- /Data Strucutures In java/Lecture 5 Time and Complexity Analysis/Pair sum in array.txt: -------------------------------------------------------------------------------- 1 | You have been given an integer array/list(ARR) and a number 'num'. Find and return the total number of pairs in the array/list which sum to 'num'. 2 | Note: 3 | Given array/list can contain duplicate elements. 4 | Input format : 5 | The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow. 6 | 7 | First line of each test case or query contains an integer 'N' representing the size of the first array/list. 8 | 9 | Second line contains 'N' single space separated integers representing the elements in the array/list. 10 | 11 | Third line contains an integer 'num'. 12 | Output format : 13 | For each test case, print the total number of pairs present in the array/list. 14 | 15 | Output for every test case will be printed in a separate line. 16 | Constraints : 17 | 1 <= t <= 10^2 18 | 0 <= N <= 10^4 19 | 0 <= num <= 10^9 20 | 21 | Time Limit: 1 sec 22 | Sample Input 1: 23 | 1 24 | 9 25 | 1 3 6 2 5 4 3 2 4 26 | 7 27 | Sample Output 1: 28 | 7 29 | Sample Input 2: 30 | 2 31 | 9 32 | 1 3 6 2 5 4 3 2 4 33 | 12 34 | 6 35 | 2 8 10 5 -2 5 36 | 10 37 | Sample Output 2: 38 | 0 39 | 2 40 | 41 | 42 | Explanation for Input 2: 43 | Since there doesn't exist any pair with sum equal to 12 for the first query, we print 0. 44 | 45 | For the second query, we have 2 pairs in total that sum up to 10. They are, (2, 8) and (5, 5). 46 | Solutions:///////////////////// 47 | public class Solution { 48 | 49 | public static int pairSum(int[] arr, int num) { 50 | //Your code goes here 51 | int count=0; 52 | for (int i=0;i priorityQueue = new PriorityQueue<>(); 34 | for(int i=0;i priorityQueue.peek() ) 39 | { //if element is greater than the smallest stored in priority Queue remove it 40 | priorityQueue.remove(); 41 | priorityQueue.add(input[i]); //add the greater element to the priority Queue 42 | } 43 | } 44 | //after entire process is completed, inside queue you have k largest elements, remove from the queue to get kth largest element 45 | return priorityQueue.remove(); 46 | 47 | 48 | } 49 | } -------------------------------------------------------------------------------- /Data Strucutures In java/Lecture 4 Recursion 2/Print Subsets of Array.txt: -------------------------------------------------------------------------------- 1 | Given an integer array (of length n), find and print all the subsets of input array. 2 | Subsets are of length varying from 0 to n, that contain elements of the array. But the order of elements should remain same as in the input array. 3 | Note : The order of subsets are not important. Just print the subsets in different lines. 4 | Input format : 5 | Line 1 : Integer n, Size of array 6 | Line 2 : Array elements (separated by space) 7 | Constraints : 8 | 1 <= n <= 15 9 | Sample Input: 10 | 3 11 | 15 20 12 12 | Sample Output: 13 | [] (this just represents an empty array, don't worry about the square brackets) 14 | 12 15 | 20 16 | 20 12 17 | 15 18 | 15 12 19 | 15 20 20 | 15 20 12 21 | Solutions://////////////////// 22 | public class solution { 23 | public static void printSubsets(int input[]) { 24 | // Write your code here 25 | printSubsetsHelper(input,0,new int[0]); 26 | } 27 | 28 | private static void printSubsetsHelper(int[] input, int startIndex, int[] output) 29 | { 30 | //Base case - If start index = input.length, print the output and return 31 | if (startIndex==input.length) 32 | { 33 | for (int i=0;i { 39 | T data; 40 | BinaryTreeNode left; 41 | BinaryTreeNode right; 42 | 43 | public BinaryTreeNode(T data) { 44 | this.data = data; 45 | this.left = null; 46 | this.right = null; 47 | } 48 | } 49 | 50 | */ 51 | 52 | public class Solution { 53 | 54 | public static void mirrorBinaryTree(BinaryTreeNode root){ 55 | //Your code goes here 56 | if(root==null) 57 | return; 58 | if(root.left==null && root.right==null) 59 | return; 60 | mirrorBinaryTree(root.left); 61 | mirrorBinaryTree(root.right); 62 | 63 | BinaryTreeNode temp = root.left; 64 | root.left=root.right; 65 | root.right=temp; 66 | 67 | } 68 | 69 | } -------------------------------------------------------------------------------- /Introduction to Java/Lecure 6, Patterns 2/Exercises/DiamondOfStars.java: -------------------------------------------------------------------------------- 1 | /*Code : Diamond of stars 2 | 3 | Print the following pattern for the given number of rows. 4 | Note: N is always odd. 5 | 6 | 7 | Pattern for N = 5 8 | 9 | 10 | 11 | The dots represent spaces. 12 | 13 | 14 | 15 | Input format : 16 | N (Total no. of rows and can only be odd) 17 | Output format : 18 | Pattern in N lines 19 | Constraints : 20 | 1 <= N <= 49 21 | Sample Input 1: 22 | 5 23 | Sample Output 1: 24 | * 25 | *** 26 | ***** 27 | *** 28 | * 29 | Sample Input 2: 30 | 3 31 | Sample Output 2: 32 | * 33 | *** 34 | * 35 | */ 36 | package lecture6.Exercises; 37 | import java.util.Scanner; 38 | public class DiamondOfStars { 39 | 40 | public static void main(String[] args) { 41 | // TODO Auto-generated method stub 42 | Scanner s = new Scanner(System.in); 43 | int n = s.nextInt(); 44 | int firstHalf=(n+1)/2; 45 | int secondHalf = n - firstHalf; 46 | 47 | //for 1st half 48 | int currRow = 1; 49 | while(currRow<=firstHalf) 50 | { 51 | //for spaces 52 | int spaces = 1; 53 | while(spaces <= firstHalf-currRow) { 54 | System.out.print(' '); 55 | spaces++; 56 | } 57 | //for stars 58 | int currCol = 1; 59 | while(currCol<=2*currRow-1) { 60 | System.out.print('*'); 61 | currCol++; 62 | } 63 | 64 | System.out.println(); 65 | currRow++; 66 | } 67 | currRow=secondHalf; 68 | //for 2nd half 69 | while(currRow>=1) { 70 | //for spaces 71 | int spaces = 1; 72 | while(spaces<=secondHalf-currRow+1) { 73 | System.out.print(' '); 74 | spaces++; 75 | } 76 | //for stars 77 | int currCol = 1; 78 | while(currCol<=2*currRow-1) { 79 | System.out.print('*'); 80 | currCol++; 81 | } 82 | System.out.println(); 83 | currRow--; 84 | } 85 | 86 | s.close(); 87 | 88 | 89 | } 90 | 91 | } 92 | -------------------------------------------------------------------------------- /Introduction to Java/Lecture 12, Strings/Assignments/RemoveConsecutiveDuplicates.java: -------------------------------------------------------------------------------- 1 | /* 2 | Remove Consecutive Duplicates 3 | 4 | For a given string(str), remove all the consecutive duplicate characters. 5 | Example: 6 | Input String: "aaaa" 7 | Expected Output: "a" 8 | 9 | Input String: "aabbbcc" 10 | Expected Output: "abc" 11 | Input Format: 12 | The first and only line of input contains a string without any leading and trailing spaces. All the characters in the string would be in lower case. 13 | Output Format: 14 | The only line of output prints the updated string. 15 | Note: 16 | You are not required to print anything. It has already been taken care of. 17 | Constraints: 18 | 0 <= N <= 10^6 19 | Where N is the length of the input string. 20 | 21 | Time Limit: 1 second 22 | Sample Input 1: 23 | aabccbaa 24 | Sample Output 1: 25 | abcba 26 | Sample Input 2: 27 | xxyyzxx 28 | Sample Output 2: 29 | xyzx 30 | */ 31 | 32 | package Assignments; 33 | public class RemoveConsecutiveDuplicates { 34 | //coding ninjas solution 35 | public static String removeConsecutiveDuplicates(String str) { 36 | int n = str.length(); 37 | if(n==0){ //to handle the case of empty string 38 | return str; 39 | } 40 | String answer = ""; //we create answer string to store the output. 41 | int startIndex = 0; 42 | while(startIndex < n){ 43 | char UniqueChar = str.charAt(startIndex); 44 | int nextUniqueChar = startIndex + 1; 45 | while(nextUniqueChar takeInput(Scanner s ){ 6 | int rootData; 7 | System.out.println("Enter root data"); 8 | rootData = s.nextInt(); 9 | //lets assume if user wants to end input, it returns -1 10 | //base case if user wants to create a null tree 11 | if(rootData == -1) { 12 | return null; 13 | } 14 | BinaryTreeNode root = new BinaryTreeNode(rootData); 15 | root.left = takeInput(s); //recursive call for left child. 16 | root.right = takeInput(s); //recursive call for right child. 17 | return root; //we get the complete tree. 18 | 19 | } 20 | 21 | //print function for Binary Tree depth wise, 22 | public static void printTree(BinaryTreeNode root) { 23 | if(root == null) { 24 | return; 25 | } 26 | String toBePrinted = root.data + ""; //we are doing + "" to convert root.data into String 27 | if(root.left != null) { 28 | toBePrinted += " L:" + root.left.data+","; 29 | } 30 | if(root.right != null) { 31 | toBePrinted += " R:" + root.right.data; //Whenever you use .data, make sure the thing before dot is not null 32 | } 33 | //now do 2 recursive calls one for left child another for right child 34 | System.out.println(toBePrinted); 35 | printTree(root.left); 36 | printTree(root.right); 37 | } 38 | 39 | public static void main(String[] args) { 40 | 41 | // TODO Auto-generated method stub 42 | Scanner s = new Scanner(System.in); 43 | BinaryTreeNode root = takeInput(s); 44 | printTree(root); 45 | s.close(); 46 | // BinaryTreeNode root = new BinaryTreeNode(1); 47 | // BinaryTreeNode node1 = new BinaryTreeNode(2); 48 | // BinaryTreeNode node2 = new BinaryTreeNode(5); 49 | // root.left = node1; 50 | // root.right = node2; 51 | 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /Data Strucutures In java/Lecture 9 Stacks and Queues/Reverse Queue.txt: -------------------------------------------------------------------------------- 1 | You have been given a queue that can store integers as the data. You are required to write a function that reverses the populated queue itself without using any other data structures. 2 | Example: 3 | Alt txt 4 | 5 | Alt txt 6 | 7 | Input Format: 8 | The first list of input contains an integer 't' denoting the number of test cases/queries to be run. 9 | Then the test cases follow. 10 | 11 | The first line input for each test case/query contains an integer N, denoting the total number of elements in the queue. 12 | 13 | The second line of input contains N integers separated by a single space, representing the order in which the elements are enqueued into the queue. 14 | Output Format: 15 | For each test case/query, the only line of output prints the order in which the queue elements are dequeued, all of them separated by a single space. 16 | 17 | Output for every test case/query will be printed on a new line. 18 | Note: 19 | You are not required to print the expected output explicitly, it has already been taken care of. Just make the changes in the input queue itself. 20 | Constraints: 21 | 1 <= t <= 100 22 | 1 <= N <= 10^4 23 | -2^31 <= data <= 2^31 - 1 24 | 25 | Time Limit: 1sec 26 | Sample Input 1: 27 | 1 28 | 6 29 | 1 2 3 4 5 10 30 | Note: 31 | Here, 1 is at the front and 10 is at the rear of the queue. 32 | Sample Output 1: 33 | 10 5 4 3 2 1 34 | Sample Input 2: 35 | 2 36 | 5 37 | 2 8 15 1 10 38 | 3 39 | 10 20 30 40 | Sample Output 2: 41 | 10 1 15 8 2 42 | 30 20 10 43 | Solutions://///////// 44 | import java.util.LinkedList; 45 | import java.util.Queue; 46 | 47 | public class Solution { 48 | 49 | public static void reverseQueue(Queue input) { 50 | //Your code goes here 51 | if (input.size()==0 || input.size()==1) 52 | { 53 | return; 54 | } 55 | 56 | int temp=input.remove(); 57 | reverseQueue(input); 58 | input.add(temp); 59 | } 60 | 61 | } -------------------------------------------------------------------------------- /Data Strucutures In java/Lecture 4 Recursion 2/Merge Sort Code.txt: -------------------------------------------------------------------------------- 1 | Sort an array A using Merge Sort. 2 | Change in the input array itself. So no need to return or print anything. 3 | Input format : 4 | Line 1 : Integer n i.e. Array size 5 | Line 2 : Array elements (separated by space) 6 | Output format : 7 | Array elements in increasing order (separated by space) 8 | Constraints : 9 | 1 <= n <= 10^3 10 | Sample Input 1 : 11 | 6 12 | 2 6 8 5 4 3 13 | Sample Output 1 : 14 | 2 3 4 5 6 8 15 | Sample Input 2 : 16 | 5 17 | 2 1 5 2 3 18 | Sample Output 2 : 19 | 1 2 2 3 5 20 | 21 | Solution:///////////////////////// 22 | public class solution { 23 | 24 | public static void mergeSort(int[] input){ 25 | if(input.length <=1){ 26 | return; 27 | } 28 | int mid = input.length / 2; 29 | int part1[] = new int[mid]; 30 | int part2[] = new int[input.length - mid]; 31 | for(int i=0; i 20 -> 30 -> 40 -> 50 -> 60 -> null 4 | Alternate nodes will be: 20, 40, and 60. 5 | 6 | Hence after deleting, the list will be: 7 | Output: 10 -> 30 -> 50 -> null 8 | Note : 9 | The head of the list will remain the same. Don't need to print or return anything. 10 | Input format : 11 | The first and the only line of input will contain the elements of the Singly Linked List separated by a single space and terminated by -1. 12 | Output Format : 13 | The only line of output will contain the updated list elements. 14 | Input Constraints: 15 | 1 <= N <= 10 ^ 6. 16 | Where N is the size of the Singly Linked List 17 | 18 | Time Limit: 1 sec 19 | Sample Input 1: 20 | 1 2 3 4 5 -1 21 | Sample Output 1: 22 | 1 3 5 23 | Explanation of Sample Input 1: 24 | 2, 4 are alternate nodes so we need to delete them 25 | Sample Input 2: 26 | 10 20 30 40 50 60 70 -1 27 | Sample Output 2: 28 | 10 30 50 70 29 | Solutions://///////////////////// 30 | /* 31 | 32 | Following is the class used to represent the Node of a Singly Linked List 33 | 34 | class Node { 35 | public T data; 36 | public Node next; 37 | 38 | public Node(T data) { 39 | this.setData(data); 40 | this.next = null; 41 | } 42 | 43 | public T getData() { 44 | return data; 45 | } 46 | 47 | public void setData(T data) { 48 | this.data = data; 49 | } 50 | 51 | } 52 | */ 53 | 54 | public class Solution { 55 | 56 | public static void deleteAlternateNodes(Node head) { 57 | //Your code goes here 58 | while(head.next!=null){ 59 | if(head.next.next!=null){ 60 | head.next=head.next.next; 61 | head=head.next; 62 | }else{ 63 | head.next=null; 64 | } 65 | } 66 | } 67 | } --------------------------------------------------------------------------------