├── 30 Days of Code ├── Day 00: Hello, World.java ├── Day 01: Data Types.java ├── Day 02: Operators.java ├── Day 03: Intro to Conditional Statements.java ├── Day 04: Class vs. Instance.java ├── Day 05: Loops.java ├── Day 06: Let's Review.java ├── Day 07: Arrays.java ├── Day 08: Dictionaries and Maps.java ├── Day 09: Recursion 3.java ├── Day 10: Binary Numbers.java ├── Day 11: 2D Arrays.java ├── Day 12: Inheritance.java ├── Day 13: Abstract Classes.java ├── Day 14: Scope.java ├── Day 15: Linked List.java ├── Day 16: Exceptions - String to Integer.java ├── Day 17: More Exceptions.java ├── Day 18: Queues and Stacks.java ├── Day 19: Interfaces.java ├── Day 20: Sorting.java ├── Day 22: Binary Search Trees.java ├── Day 23: BST Level-Order Traversal.java ├── Day 24: More Linked Lists.java ├── Day 25: Running Time and Complexity.java ├── Day 26: Nested Logic.java ├── Day 27: Testing.java ├── Day 28: RegEx, Patterns, and Intro to Databases.java └── Day 29: Bitwise AND.java ├── Cracking the Coding Interview └── DATA STRUCTURES │ ├── Arrays: Left Rotation.java │ ├── Hash Tables: Ransom Note.java │ └── Trees: Is This a Binary Search Tree?.java ├── Interview Preparation Kit ├── Arrays │ ├── 2D Array - DS.java │ ├── Array Manipulation.java │ ├── Arrays: Left Rotation.java │ ├── Minimum Swaps 2.java │ └── New Year Chaos.java ├── Dictionaries and Hashmaps │ ├── Hash Tables: Ransom Note.java │ ├── Sherlock and Anagrams.java │ └── Two Strings.java ├── Greedy Algorithms │ ├── Luck Balance.java │ └── Minimum Absolute Difference in an Array.java ├── Linked Lists │ ├── Find Merge Point of Two Lists.java │ ├── Insert a node at a specific position in a linked list.java │ ├── Inserting a Node Into a Sorted Doubly Linked List.java │ └── Reverse a doubly linked list.java ├── Miscellaneous │ └── Flipping bits.java ├── Recursion and Backtracking │ └── Recursive Digit Sum ├── Search │ └── Pairs.java ├── Sorting │ └── Mark and Toys.java ├── Stacks and Queues │ └── Balanced Brackets.java ├── String Manipulation │ └── Alternating Characters.java └── Trees │ ├── Binary Search Tree : Lowest Common Ancestor.java │ ├── Tree: Height of a Binary Tree.java │ └── Trees: Is This a Binary Search Tree?.java ├── Java ├── BigNumber │ ├── Java BigInteger.java │ └── Java Primality Test.java ├── Data Structures │ ├── Java 1D Array.java │ ├── Java 2D Array.java │ ├── Java Arraylist.java │ ├── Java List.java │ ├── Java Map.java │ └── Java Subarray.java ├── Exception Handling │ ├── Java Exception Handling (Try-catch).java │ └── Java Exception Handling.java ├── Introduction │ ├── Java Currency Formatter.java │ ├── Java Datatypes.java │ ├── Java Date and Time.java │ ├── Java If-Else.java │ ├── Java Int to String.java │ ├── Java Loops I.java │ ├── Java Loops II.java │ ├── Java Output Formatting.java │ ├── Java Static Initializer Block.java │ ├── Java Stdin and Stdout I.java │ ├── Java Stdin and Stdout II.java │ └── Welcome to Java!.java ├── Object Oriented Programming │ ├── Java Abstract Class.java │ ├── Java Inheritance I.java │ ├── Java Inheritance II.java │ ├── Java Instanceof keyword.java │ ├── Java Interface.java │ ├── Java Iterator.java │ ├── Java Method Overriding 2 (Super Keyword).java │ └── Java Method Overriding.java └── Strings │ ├── Java Anagrams.java │ ├── Java Regex.java │ ├── Java String Reverse.java │ ├── Java String Tokens.java │ ├── Java Strings Introduction.java │ ├── Java Substring Comparisons.java │ └── Java Substring.java ├── Mathematics ├── Algebra │ ├── Combo Meal.java │ └── Stepping Stones Game.java ├── Fundamentals │ ├── Army Game.java │ ├── Best Divisor.java │ ├── Connecting Towns.java │ ├── Cutting Paper Squares.java │ ├── Filling Jars.java │ ├── Find the Point.java │ ├── Halloween party.java │ ├── Handshake.java │ ├── Leonardo's Prime Factors.java │ ├── Maximum Draws.java │ ├── Minimum Height Triangle.java │ ├── Restaurant.java │ ├── Reverse Game.java │ ├── Sherlock and Moving Tiles.java │ ├── Strange Grid Again.java │ └── Summing the N series.java ├── Geometry │ ├── Points On a Line.java │ └── Rectangular Game.java ├── Linear Algebra Foundations │ ├── Determinant of the matrix #1 │ ├── Determinant of the matrix #2 │ ├── Determinant of the matrix #3 │ ├── Eigenvalue of a Matrix I │ ├── Linear Algebra Foundations #1 - Matrix Addition │ ├── Linear Algebra Foundations #10 - Eigenvectors │ ├── Linear Algebra Foundations #2 - Matrix Subtraction │ ├── Linear Algebra Foundations #3- Matrix Multiplication │ ├── Linear Algebra Foundations #4- Matrix Multiplication │ ├── Linear Algebra Foundations #5 - The 100th Power of a Matrix │ ├── Linear Algebra Foundations #6 - An Equation involving Matrices │ ├── Linear Algebra Foundations #7 - The 1000th Power of a Matrix │ ├── Linear Algebra Foundations #8 - Systems of Equations │ └── Linear Algebra Foundations #9 - Eigenvalues └── Number Theory │ ├── Closest Number.java │ ├── Constructing a Number.java │ ├── Identify Smith Numbers.java │ └── Sherlock and GCD.java ├── Problem Solving-Algorithms ├── Bit Manipulation │ ├── AND Product.java │ ├── Counter game.java │ ├── Flipping bits.java │ ├── Lonely Integer.java │ ├── Maximizing XOR.java │ ├── Sansa and XOR.java │ ├── Sum vs XOR.java │ ├── The Great XOR.java │ └── Xor-sequence.java ├── Graph Theory │ ├── Even Tree.java │ ├── Journey to the Moon.java │ └── Roads and Libraries.java ├── Greedy │ ├── Beautiful Pairs.java │ ├── Grid Challenge.java │ ├── Jim and the Orders.java │ ├── Largest Permutation.java │ ├── Luck Balance.java │ ├── Marc's Cakewalk.java │ ├── Mark and Toys.java │ ├── Maximum Perimeter Triangle.java │ ├── Minimum Absolute Difference in an Array.java │ ├── Permuting Two Arrays.java │ ├── Priyanka and Toys.java │ └── Sherlock and The Beast.java ├── Implementation │ ├── ACM ICPC Team.java │ ├── Absolute Permutation.java │ ├── Almost Sorted.java │ ├── Angry Professor.java │ ├── Append and Delete.java │ ├── Apple and Orange.java │ ├── Beautiful Days at the Movies.java │ ├── Beautiful Triplets.java │ ├── Between Two Sets.java │ ├── Birthday Chocolate.java │ ├── Bon Appétit.java │ ├── Breaking the Records.java │ ├── Cats and a Mouse.java │ ├── Cavity Map.java │ ├── Chocolate Feast.java │ ├── Circular Array Rotation.java │ ├── Climbing the Leaderboard.java │ ├── Counting Valleys.java │ ├── Cut the sticks.java │ ├── Day of the Programmer.java │ ├── Designer PDF Viewer.java │ ├── Divisible Sum Pairs.java │ ├── Drawing Book.java │ ├── Electronics Shop.java │ ├── Encryption.java │ ├── Equalize the Array.java │ ├── Extra Long Factorials.java │ ├── Fair Rations.java │ ├── Find Digits.java │ ├── Flatland Space Station.java │ ├── Grading Students.java │ ├── Halloween Sale.java │ ├── Happy Ladybugs.java │ ├── Jumping on the Clouds.java │ ├── Jumping on the Clouds: Revisited.java │ ├── Kangaroo.java │ ├── Library Fine.java │ ├── Lisa's Workbook.java │ ├── Manasa and Stone.java │ ├── Matrix Layer Rotation.java │ ├── Migratory Birds.java │ ├── Minimum Distances.java │ ├── Modified Kaprekar Numbers.java │ ├── Picking Numbers.java │ ├── Repeated String.java │ ├── Save the Prisoner!.java │ ├── Sequence Equation.java │ ├── Service Lane.java │ ├── Sherlock and Squares.java │ ├── Sock Merchant.java │ ├── Strange Counter.java │ ├── Taum and B'day.java │ ├── The Grid Search.java │ ├── The Hurdle Race.java │ ├── The Time in Words.java │ ├── Utopian Tree.java │ └── Viral Advertising.java ├── Recursion │ └── Recursive Digit Sum.java ├── Search │ ├── Connected Cells in a Grid.java │ ├── Hackerland Radio Transmitters.java │ ├── Ice Cream Parlor.java │ ├── Missing Numbers.java │ ├── Pairs.java │ └── Sherlock and Array.java ├── Sorting │ ├── Big Sorting.java │ ├── Closest Numbers.java │ ├── Correctness and the Loop Invariant.java │ ├── Counting Sort 1.java │ ├── Counting Sort 2.java │ ├── Find the Median.java │ ├── Insertion Sort - Part 1.java │ ├── Insertion Sort - Part 2.java │ ├── Intro to Tutorial Challenges.java │ ├── Quicksort 1 - Partition.java │ └── Running Time of Algorithms.java ├── Strings │ ├── Alternating Characters.java │ ├── Anagram.java │ ├── Beautiful Binary String.java │ ├── Caesar Cipher.java │ ├── CamelCase.java │ ├── Funny String.java │ ├── Game of Thrones - I.java │ ├── Gemstones.java │ ├── HackerRank in a String!.java │ ├── Making Anagrams.java │ ├── Mars Exploration.java │ ├── Palindrome Index.java │ ├── Pangrams.java │ ├── Separate the Numbers.java │ ├── Sherlock and Anagrams.java │ ├── Sherlock and the Valid String.java │ ├── String Construction.java │ ├── Strong Password.java │ ├── Super Reduced String.java │ ├── The Love-Letter Mystery.java │ ├── Two Characters.java │ ├── Two Strings.java │ └── Weighted Uniform Strings.java └── Warmup │ ├── A Very Big Sum.java │ ├── Birthday Cake Candles.java │ ├── Compare the Triplets.java │ ├── Diagonal Difference.java │ ├── Mini-Max Sum.java │ ├── Plus Minus.java │ ├── Simple Array Sum.java │ ├── Solve Me First.java │ ├── Staircase.java │ └── Time Conversion.java ├── Problem Solving-Data Structures ├── Arrays │ ├── 2D Array - DS.java │ ├── Array Manipulation.java │ ├── Arrays - DS.java │ ├── Dynamic Array.java │ ├── Left Rotation.java │ └── Sparse Arrays.java ├── Balanced Trees │ └── Self Balancing Tree.java ├── Heap │ ├── Jesse and Cookies.java │ └── QHEAP1.java ├── Linked Lists │ ├── Compare two linked lists.java │ ├── Delete a Node.java │ ├── Delete duplicate-value nodes from a sorted linked list.java │ ├── Find Merge Point of Two Lists.java │ ├── Get Node Value.java │ ├── Insert a Node at the Tail of a Linked List.java │ ├── Insert a node at a specific position in a linked list.java │ ├── Insert a node at the head of a linked list.java │ ├── Inserting a Node Into a Sorted Doubly Linked List.java │ ├── Merge two sorted linked lists.java │ ├── Print in Reverse.java │ ├── Print the Elements of a Linked List.java │ ├── Reverse a doubly linked list.java │ └── Reverse a linked list.java ├── Queues │ └── Queue using Two Stacks.java ├── Stacks │ ├── Balanced Brackets.java │ ├── Equal Stacks.java │ └── Maximum Element.java └── Trees │ ├── Binary Search Tree : Insertion.java │ ├── Binary Search Tree : Lowest Common Ancestor.java │ ├── Is This a Binary Search Tree?.java │ ├── Tree : Top View.java │ ├── Tree: Height of a Binary Tree.java │ ├── Tree: Inorder Traversal.java │ ├── Tree: Level Order Traversal.java │ ├── Tree: Postorder Traversal.java │ └── Tree: Preorder Traversal.java └── README.md /30 Days of Code/Day 00: Hello, World.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | public class Solution 3 | { 4 | public static void main(String[] args)throws IOException 5 | { 6 | InputStreamReader isr=new InputStreamReader(System.in); 7 | BufferedReader br=new BufferedReader(isr); 8 | String s=br.readLine(); 9 | System.out.println("Hello, World.\n"+s); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /30 Days of Code/Day 01: Data Types.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import java.text.*; 4 | import java.math.*; 5 | import java.util.regex.*; 6 | 7 | public class Solution { 8 | 9 | public static void main(String[] args) { 10 | int i = 4; 11 | double d = 4.0; 12 | String s = "HackerRank "; 13 | 14 | Scanner scan = new Scanner(System.in); 15 | /* Declare second integer, double, and String variables. */ 16 | int a; 17 | double b; 18 | String c; 19 | String da; 20 | String g; 21 | 22 | /* Read and save an integer, double, and String to your variables.*/ 23 | a=scan.nextInt(); 24 | b=scan.nextDouble(); 25 | c=scan.next(); 26 | da=scan.nextLine(); 27 | // Note: If you have trouble reading the entire String, please go back and review the Tutorial closely. 28 | 29 | /* Print the sum of both integer variables on a new line. */ 30 | System.out.println(i+a); 31 | System.out.println(d+b); 32 | g=s+c; 33 | g=g+da; 34 | System.out.println(g); 35 | /* Print the sum of the double variables on a new line. */ 36 | 37 | /* Concatenate and print the String variables on a new line; 38 | the 's' variable above should be printed first. */ 39 | scan.close(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /30 Days of Code/Day 02: Operators.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.math.*; 3 | 4 | public class Arithmetic { 5 | 6 | public static void main(String[] args) { 7 | Scanner scan = new Scanner(System.in); 8 | double mealCost = scan.nextDouble(); // original meal price 9 | int tipPercent = scan.nextInt(); // tip percentage 10 | int taxPercent = scan.nextInt(); // tax percentage 11 | scan.close(); 12 | double s=mealCost*tipPercent/100; 13 | double ss=mealCost*taxPercent/100; 14 | ss=mealCost+ss+s; 15 | // Write your calculation code here. 16 | 17 | // cast the result of the rounding operation to an int and save it as totalCost 18 | int totalCost = (int) Math.round(ss); 19 | System.out.println("The total meal cost is "+totalCost+" dollars."); 20 | // Print your result 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /30 Days of Code/Day 03: Intro to Conditional Statements.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import java.text.*; 4 | import java.math.*; 5 | import java.util.regex.*; 6 | public class Solution { 7 | 8 | public static void main(String[] args) { 9 | Scanner scan = new Scanner(System.in); 10 | int n = scan.nextInt(); 11 | scan.close(); 12 | String ans=""; 13 | 14 | // if 'n' is NOT evenly divisible by 2 (i.e.: n is odd) 15 | if(n%2==1){ 16 | ans = "Weird"; 17 | } 18 | else{ 19 | if(n>=2&&n<=5) 20 | { 21 | ans="Not Weird"; 22 | } 23 | else if(n>=6&&n<=20) 24 | { 25 | ans="Weird"; 26 | } 27 | else 28 | { 29 | ans="Not Weird"; 30 | } 31 | // Complete the code 32 | } 33 | System.out.println(ans); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /30 Days of Code/Day 04: Class vs. Instance.java: -------------------------------------------------------------------------------- 1 | public class Person { 2 | private int age; 3 | 4 | public Person(int initialAge) { 5 | if(initialAge>=0) 6 | { 7 | age=initialAge; 8 | } 9 | else 10 | { 11 | System.out.println("Age is not valid, setting age to 0."); 12 | age=0; 13 | }// Add some more code to run some checks on initialAge 14 | } 15 | 16 | public void amIOld() { 17 | if(age<13) 18 | {// Write code determining if this person's age is old and print the correct statement: 19 | System.out.println("You are young."); 20 | } 21 | else if(age>=13&&age<18) 22 | { 23 | System.out.println("You are a teenager."); 24 | } 25 | else 26 | { 27 | System.out.println("You are old."); 28 | } 29 | } 30 | 31 | public void yearPasses() { 32 | age++;// Increment this person's age. 33 | } 34 | -------------------------------------------------------------------------------- /30 Days of Code/Day 05: Loops.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import java.text.*; 4 | import java.math.*; 5 | import java.util.regex.*; 6 | 7 | public class Solution { 8 | 9 | public static void main(String[] args) { 10 | Scanner in = new Scanner(System.in); 11 | int n = in.nextInt(); 12 | in.close(); 13 | for(int x=1;x<=10;x++) 14 | { 15 | System.out.println(n+" x "+x+" = "+(n*x)); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /30 Days of Code/Day 06: Let's Review.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Solution { 5 | 6 | public static void main(String[] args) { 7 | Scanner sc=new Scanner(System.in); 8 | int t=sc.nextInt(); 9 | String ar[]=new String[t]; 10 | for(int x=0;x=0;x--) 16 | { 17 | System.out.print(arr[x]+" "); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /30 Days of Code/Day 08: Dictionaries and Maps.java: -------------------------------------------------------------------------------- 1 | //Complete this code or write your own from scratch 2 | import java.util.*; 3 | import java.io.*; 4 | 5 | class Solution{ 6 | public static void main(String []argh){ 7 | Scanner in = new Scanner(System.in); 8 | int n = in.nextInt(); 9 | HashMap m=new HashMap<>(); 10 | for(int i = 0; i < n; i++){ 11 | m.put(in.next(),new Integer(in.nextInt())); 12 | } 13 | while(in.hasNext()){ 14 | String s = in.next(); 15 | if(m.containsKey(s)) 16 | { 17 | System.out.println(s+"="+m.get(s)); 18 | } 19 | else 20 | { 21 | System.out.println("Not found"); 22 | } 23 | } 24 | in.close(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /30 Days of Code/Day 09: Recursion 3.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import java.text.*; 4 | import java.math.*; 5 | import java.util.regex.*; 6 | 7 | public class Solution { 8 | 9 | static int factorial(int n) { 10 | if(n<2) 11 | { 12 | return 1; 13 | } 14 | else 15 | { 16 | return n*factorial(n-1); 17 | } 18 | 19 | } 20 | 21 | public static void main(String[] args) { 22 | Scanner in = new Scanner(System.in); 23 | int n = in.nextInt(); 24 | int result = factorial(n); 25 | System.out.println(result); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /30 Days of Code/Day 10: Binary Numbers.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Solution { 5 | 6 | public static void main(String[] args) { 7 | Scanner sc=new Scanner(System.in); 8 | int a=sc.nextInt(); 9 | String s=""; 10 | while(a!=0) 11 | { 12 | s=a%2+s; 13 | a=a/2; 14 | } 15 | s=s+"0"; 16 | int c=0,d=0; 17 | for(int x=0;xl) 30 | { 31 | l=ar[x]; 32 | } 33 | } 34 | System.out.println(l); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /30 Days of Code/Day 13: Abstract Classes.java: -------------------------------------------------------------------------------- 1 | class MyBook extends Book 2 | { 3 | int price; 4 | MyBook(String title,String author,int price) 5 | { 6 | super(title,author); 7 | this.price=price; 8 | } 9 | void display() 10 | { 11 | System.out.println("Title: "+title); 12 | System.out.println("Author: "+author); 13 | System.out.println("Price: "+price); 14 | 15 | } 16 | } 17 | /** 18 | * Class Constructor 19 | * 20 | * @param title The book's title. 21 | * @param author The book's author. 22 | * @param price The book's price. 23 | **/ 24 | // Write your constructor here 25 | 26 | /** 27 | * Method Name: display 28 | * 29 | * Print the title, author, and price in the specified format. 30 | **/ 31 | // Write your method here 32 | 33 | // End class 34 | -------------------------------------------------------------------------------- /30 Days of Code/Day 14: Scope.java: -------------------------------------------------------------------------------- 1 | // Add your code here 2 | public Difference(int ar[]) 3 | { 4 | elements=ar; 5 | } 6 | public void computeDifference() 7 | { 8 | int m=-99999; 9 | for(int x=0;xm) 14 | { 15 | m=Math.abs(elements[x]-elements[y]); 16 | } 17 | } 18 | } 19 | maximumDifference=m; 20 | } 21 | -------------------------------------------------------------------------------- /30 Days of Code/Day 15: Linked List.java: -------------------------------------------------------------------------------- 1 | public static Node insert(Node head,int data) { 2 | Node n=head; 3 | Node a=new Node(data); 4 | if(n==null) 5 | { 6 | head=a; 7 | return head; 8 | } 9 | while(n.next!=null) 10 | { 11 | n=n.next; 12 | } 13 | n.next=a; 14 | return head; 15 | //Complete this method 16 | } 17 | -------------------------------------------------------------------------------- /30 Days of Code/Day 16: Exceptions - String to Integer.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import java.text.*; 4 | import java.math.*; 5 | import java.util.regex.*; 6 | 7 | public class Solution { 8 | 9 | public static void main(String[] args)throws IOException { 10 | Scanner in = new Scanner(System.in); 11 | String S = in.next(); 12 | try 13 | { 14 | System.out.println(Integer.parseInt(S)); 15 | } 16 | catch(Exception e) 17 | { 18 | System.out.println("Bad String"); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /30 Days of Code/Day 17: More Exceptions.java: -------------------------------------------------------------------------------- 1 | 2 | class Calculator { 3 | 4 | int power(int a, int b)throws Exception 5 | { 6 | if(a<0||b<0) 7 | { 8 | throw new Exception("n and p should be non-negative"); 9 | } 10 | return (int)(Math.pow(a,b)); 11 | } 12 | 13 | } 14 | 15 | -------------------------------------------------------------------------------- /30 Days of Code/Day 18: Queues and Stacks.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // Write your code here. 3 | int i=-1,f=-1,r=-1; 4 | char ar1[]=new char[50]; 5 | char ar2[]=new char[50]; 6 | void pushCharacter(char ch) 7 | { 8 | ar1[++i]=ch; 9 | } 10 | void enqueueCharacter(char ch) 11 | { 12 | if(r==-1) 13 | { 14 | f++; 15 | } 16 | ar2[++r]=ch; 17 | } 18 | char popCharacter() 19 | { 20 | return ar1[i--]; 21 | } 22 | char dequeueCharacter() 23 | { 24 | return ar2[f++]; 25 | } 26 | -------------------------------------------------------------------------------- /30 Days of Code/Day 19: Interfaces.java: -------------------------------------------------------------------------------- 1 | 2 | class Calculator implements AdvancedArithmetic 3 | { 4 | public int divisorSum(int n) 5 | { 6 | int s=0; 7 | for(int x=1;x<=n;x++) 8 | { 9 | if(n%x==0) 10 | { 11 | s=s+x; 12 | } 13 | } 14 | return s; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /30 Days of Code/Day 20: Sorting.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import java.text.*; 4 | import java.math.*; 5 | import java.util.regex.*; 6 | 7 | public class Solution { 8 | 9 | public static void main(String[] args) { 10 | Scanner in = new Scanner(System.in); 11 | int n = in.nextInt(); 12 | int[] a = new int[n]; 13 | for(int a_i=0; a_i < n; a_i++){ 14 | a[a_i] = in.nextInt(); 15 | } 16 | int b=0; 17 | for(int x=0;xa[y+1]) 22 | { 23 | b++; 24 | int t=a[y]; 25 | a[y]=a[y+1]; 26 | a[y+1]=t; 27 | } 28 | } 29 | } 30 | System.out.println("Array is sorted in "+b+" swaps."); 31 | System.out.println("First Element: "+a[0]); 32 | System.out.println("Last Element: "+a[n-1]); 33 | // Write Your Code Here 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /30 Days of Code/Day 22: Binary Search Trees.java: -------------------------------------------------------------------------------- 1 | public static int getHeight(Node root){ 2 | if(root ==null) 3 | { 4 | return -1; 5 | } 6 | return 1+Math.max(getHeight(root.right),getHeight(root.left)); 7 | } 8 | -------------------------------------------------------------------------------- /30 Days of Code/Day 23: BST Level-Order Traversal.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | class Node 4 | int data; 5 | Node left; 6 | Node right; 7 | */ 8 | public static void levelOrder(Node root) { 9 | int h=h(root); 10 | for(int x=0;x<=h;x++) 11 | { 12 | p(root,x); 13 | } 14 | } 15 | public static int h(Node root) 16 | { 17 | if(root==null) 18 | { 19 | return -1; 20 | } 21 | return 1+Math.max(h(root.left),h(root.right)); 22 | } 23 | public static void p(Node root,int x) 24 | { 25 | if(root==null) 26 | { 27 | return; 28 | } 29 | else if(x==0) 30 | { 31 | System.out.print(root.data+" "); 32 | } 33 | else 34 | { 35 | p(root.left,x-1); 36 | p(root.right,x-1); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /30 Days of Code/Day 24: More Linked Lists.java: -------------------------------------------------------------------------------- 1 | // Complete the removeDuplicates function below. 2 | 3 | /* 4 | * For your reference: 5 | * 6 | * SinglyLinkedListNode { 7 | * int data; 8 | * SinglyLinkedListNode next; 9 | * } 10 | * 11 | */ 12 | static Node removeDuplicates(Node head) { 13 | Node n=head; 14 | Node x=head; 15 | while(n!=null) 16 | { 17 | int i=n.data; 18 | while(x!=null&&i==x.data) 19 | { 20 | x=x.next; 21 | } 22 | n.next=x; 23 | n=n.next; 24 | } 25 | return head; 26 | } 27 | -------------------------------------------------------------------------------- /30 Days of Code/Day 25: Running Time and Complexity.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Solution { 5 | 6 | public static void main(String[] args) { 7 | Scanner sc=new Scanner(System.in); 8 | int a=sc.nextInt(); 9 | for(int x=0;xd2) 17 | { 18 | f=(d1-d2)*15; 19 | } 20 | else if(y1==y2&&m1>m2) 21 | { 22 | f=500*(m1-m2); 23 | } 24 | else if(y1>y2) 25 | { 26 | f=10000; 27 | } 28 | System.out.println(f); 29 | /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /30 Days of Code/Day 27: Testing.java: -------------------------------------------------------------------------------- 1 | public class Day27 { 2 | 3 | public static void main(String[] args){ 4 | //Tests 5 | System.out.println("5"); 6 | 7 | System.out.println("4 4"); 8 | System.out.println("-1 0 3 3"); 9 | 10 | System.out.println("6 2"); 11 | System.out.println("-1 0 1 2 3 2"); 12 | 13 | System.out.println("7 4"); 14 | System.out.println("-1 -2 0 3 3 9 6"); 15 | 16 | System.out.println("3 2"); 17 | System.out.println("-2 0 1"); 18 | 19 | System.out.println("8 5"); 20 | System.out.println("-2 -1 0 2 3 5 6 99"); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /30 Days of Code/Day 28: RegEx, Patterns, and Intro to Databases.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Solution { 5 | 6 | public static void main(String[] args) { 7 | 8 | Scanner sc=new Scanner(System.in); 9 | int n=sc.nextInt(); 10 | String ar[]=new String[n]; 11 | int q=-1; 12 | for(int x=0;xc)&&(qm) 16 | { 17 | System.out.println("No"); 18 | System.exit(0); 19 | } 20 | ArrayList a=new ArrayList<>(m); 21 | for(int x=0;x