├── level-1 ├── lecture-056 │ ├── README.md │ └── Main.java ├── lecture-058 │ └── README.md ├── lecture-042 │ ├── README.md │ └── target-sum-subsets-dp.md ├── lecture-047 │ └── README.md ├── lecture-043 │ ├── README.md │ ├── Main.java │ └── coin-change-combination.md ├── lecture-050 │ ├── README.md │ └── Main.java ├── lecture-001 │ ├── Main.class │ ├── print-t.md │ ├── variables.md │ ├── hw │ │ ├── reverse-of-a-number.md │ │ ├── count-digits.md │ │ ├── digits-of-a-number.md │ │ ├── primes.md │ │ └── primes-in-a-range.md │ ├── input-output.md │ ├── print-z.md │ ├── Main.java │ └── grading-system.md ├── lecture-048 │ ├── README.pdf │ ├── get-common-elements-1.pdf │ ├── get-common-elements-2.pdf │ ├── highest-frequency-character.pdf │ ├── GetCommonElements2.java │ ├── HighFreqChar.java │ ├── GetCommonElements.java │ ├── Main.java │ ├── highest-frequency-character.md │ ├── get-common-elements-2.md │ └── get-common-elements-1.md ├── lecture-057 │ └── notes.pdf ├── lecture-036 │ ├── lecture-036.pdf │ └── README.md ├── lecture-037 │ └── lecture-037.pdf ├── lecture-038 │ ├── lecture-038.pdf │ └── README.md ├── lecture-039 │ ├── lecture-039.pdf │ ├── README.md │ └── fold-a-linked-list.md ├── lecture-059 │ └── README.md ├── lecture-009-dc │ └── Main.java ├── lecture-044 │ ├── README.md │ ├── Main.java │ └── unbounded-knapsack.md ├── lecture-045 │ └── README.md ├── lecture-046 │ └── README.md ├── lecture-033 │ └── README.md ├── lecture-040 │ └── README.md ├── lecture-032 │ ├── README.md │ └── target-sum-subsets.md ├── lecture-041 │ └── README.md ├── lecture-030 │ ├── README.md │ ├── print-permutations.md │ └── print-encodings.md ├── lecture-060 │ ├── README.md │ └── 144. Binary Tree Preorder Traversal │ │ └── Main.java ├── archive │ ├── test-problems │ │ ├── test-001 │ │ │ ├── README.md │ │ │ ├── number-of-rectangles-that-can-form-the-largest-square.md │ │ │ ├── grandfathers-car.md │ │ │ └── all-indices-of-array.md │ │ ├── test-002 │ │ │ ├── first-missing -positive.md │ │ │ ├── sum-of-all-length-subarrays.md │ │ │ ├── determine-whether-matrix-can-be-obtained-by-rotation.md │ │ │ ├── README.md │ │ │ ├── exact-special-positions-in-a-binary-matrix.md │ │ │ └── seating-arrangement.md │ │ ├── intuit-test │ │ │ ├── 001-water-regions.md │ │ │ ├── 003-aman-and-garden.md │ │ │ └── 002-tokyo-drift.md │ │ └── README.md │ ├── level-1 │ │ └── dynamic-programming.md │ └── leetcode │ │ └── 206-reverse-linked-list.md ├── lecture-035 │ └── README.md ├── lecture-055 │ ├── Main.java │ ├── README.md │ └── valid-parentheses.md ├── lecture-003 │ ├── hw │ │ ├── gcd-lcm-naive.md │ │ ├── prime-factorisation.md │ │ └── gcd-lcm.md │ ├── reverse-a-number.md │ ├── benjamin-bulbs.md │ ├── pythagorian-tripplet.md │ └── inverse-of-a-number.md ├── lecture-053 │ ├── sort 0 1 2 │ │ └── Main.java │ ├── merge2sorted │ │ └── Main.java │ └── mergeSort │ │ └── Main.java ├── lecture-004 │ ├── pattern-2.md │ ├── pattern-1.md │ ├── pattern-4.md │ ├── pattern-3.md │ ├── pattern-5.md │ └── rotate-by-k.md ├── lecture-002 │ ├── hw │ │ ├── count-digits.md │ │ ├── fibo-till-n.md │ │ └── digits-of-a-number.md │ ├── primes-till-n.md │ ├── Main.java │ └── is-number-prime.md ├── lecture-023 │ ├── Main.java │ ├── power-linear.md │ ├── print-zigzag.md │ └── power-log.md ├── lecture-049 │ ├── s.md │ ├── KLargestElements.md │ └── README.md ├── lecture-011 │ ├── reverse-array.md │ ├── span-of-array.md │ ├── bar-chart.md │ └── Main.java ├── lecture-016 │ └── hw │ │ ├── wave-array-2.md │ │ ├── spiral-display-2.md │ │ └── Main.java ├── lecture-052 │ ├── sort 0 1 │ │ └── Main.java │ ├── Sort01.java │ ├── bubbleSort │ │ └── Main.java │ ├── selectionSort │ │ └── Main.java │ ├── insertionSort │ │ └── Main.java │ ├── Main.java │ ├── SelectionSort.java │ ├── InsertionSort.java │ ├── sort-01.md │ └── README.md ├── lecture-007 │ ├── digit-frequency.md │ ├── decimal-to-anybase.md │ ├── anybase-to-decimal.md │ ├── anybase-addition.md │ └── anybase-to-anybase.md ├── lecture-005 │ └── Main.java ├── lecture-013 │ ├── inverse-array.md │ ├── subarray.md │ ├── rotate-array.md │ └── Main.java ├── lecture-021 │ ├── print-decreasing.md │ ├── print-increasing.md │ ├── print-increasing-decreasing.md │ └── remove-primes.md ├── lecture-022 │ ├── factorial-recursion.md │ ├── print-decreasing.md │ ├── print-increasing.md │ └── print-decreasing-increasing.md ├── lecture-031 │ └── Main.java ├── lecture-020 │ ├── toggle-case.md │ └── string-with-difference-of-every-two-consecutive-characters.md ├── lecture-014 │ ├── binary-search.md │ └── first-last-index.md ├── lecture-010 │ ├── find-element-in-array.md │ └── Arrays.java ├── lecture-012 │ ├── Main.java │ └── sum-of-two-arrays.md ├── lecture-024 │ ├── display-array-in-reverse.md │ ├── display-array.md │ ├── Main.java │ └── tower-of-hanoi.md ├── lecture-019 │ └── print-all-palindromic-substrings.md ├── lecture-029 │ ├── print-subsequence.md │ ├── print-stair-path.md │ └── print-maze-path.md ├── lecture-015 │ ├── 2d-array-demo.md │ └── subsets-of-array.md ├── lecture-025 │ ├── max-of-array.md │ ├── first-index.md │ └── Main.java ├── lecture-027 │ ├── Main.java │ └── get-stair-path.md ├── lecture-026 │ ├── get-subsequence.md │ ├── Main.java │ └── all-indices.md └── lecture-018 │ └── search-in-a-sorted-2d-array.md ├── .gitignore ├── assets └── css │ └── style.scss ├── _config.yml └── LICENSE.md /level-1/lecture-056/README.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .vscode/ 3 | node_modules/ -------------------------------------------------------------------------------- /level-1/lecture-058/README.md: -------------------------------------------------------------------------------- 1 | # Stack Problems 2 | -------------------------------------------------------------------------------- /level-1/lecture-042/README.md: -------------------------------------------------------------------------------- 1 | # Lecture 42 | Dynamic Programming 2 | -------------------------------------------------------------------------------- /level-1/lecture-047/README.md: -------------------------------------------------------------------------------- 1 | # Lecture 47 | Exams so left class 2 | -------------------------------------------------------------------------------- /level-1/lecture-043/README.md: -------------------------------------------------------------------------------- 1 | # Missed class due to college exams 2 | -------------------------------------------------------------------------------- /level-1/lecture-050/README.md: -------------------------------------------------------------------------------- 1 | # Lecture 50 | Hashmap and Heap 2 | 3 | -------------------------------------------------------------------------------- /level-1/lecture-001/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thatbeautifuldream/dsa/HEAD/level-1/lecture-001/Main.class -------------------------------------------------------------------------------- /level-1/lecture-048/README.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thatbeautifuldream/dsa/HEAD/level-1/lecture-048/README.pdf -------------------------------------------------------------------------------- /level-1/lecture-057/notes.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thatbeautifuldream/dsa/HEAD/level-1/lecture-057/notes.pdf -------------------------------------------------------------------------------- /assets/css/style.scss: -------------------------------------------------------------------------------- 1 | --- 2 | --- 3 | 4 | @import "{{ site.theme }}"; 5 | 6 | header { 7 | display: none; 8 | } 9 | -------------------------------------------------------------------------------- /level-1/lecture-036/lecture-036.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thatbeautifuldream/dsa/HEAD/level-1/lecture-036/lecture-036.pdf -------------------------------------------------------------------------------- /level-1/lecture-037/lecture-037.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thatbeautifuldream/dsa/HEAD/level-1/lecture-037/lecture-037.pdf -------------------------------------------------------------------------------- /level-1/lecture-038/lecture-038.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thatbeautifuldream/dsa/HEAD/level-1/lecture-038/lecture-038.pdf -------------------------------------------------------------------------------- /level-1/lecture-039/lecture-039.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thatbeautifuldream/dsa/HEAD/level-1/lecture-039/lecture-039.pdf -------------------------------------------------------------------------------- /level-1/lecture-059/README.md: -------------------------------------------------------------------------------- 1 | # Trees 2 | 3 | - Trees are a data structure that can be used to represent hierarchical data. 4 | -------------------------------------------------------------------------------- /level-1/lecture-048/get-common-elements-1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thatbeautifuldream/dsa/HEAD/level-1/lecture-048/get-common-elements-1.pdf -------------------------------------------------------------------------------- /level-1/lecture-048/get-common-elements-2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thatbeautifuldream/dsa/HEAD/level-1/lecture-048/get-common-elements-2.pdf -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | title: DSA Repository 2 | description: This repository consists of all the material from my data structures and algorithms preparation. 3 | -------------------------------------------------------------------------------- /level-1/lecture-009-dc/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | 7 | } 8 | } -------------------------------------------------------------------------------- /level-1/lecture-048/highest-frequency-character.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thatbeautifuldream/dsa/HEAD/level-1/lecture-048/highest-frequency-character.pdf -------------------------------------------------------------------------------- /level-1/lecture-044/README.md: -------------------------------------------------------------------------------- 1 | # Lecture 44 | Did not attend due to college exams 2 | 3 | - Refer DP Problems [here](https://github.com/aman-5757/pepcodingCodes/tree/master/FJP%203/08.%20DP) -------------------------------------------------------------------------------- /level-1/lecture-045/README.md: -------------------------------------------------------------------------------- 1 | # Lecture 45 | Did not attend due to college exams 2 | 3 | - Refer DP Problems [here](https://github.com/aman-5757/pepcodingCodes/tree/master/FJP%203/08.%20DP) 4 | -------------------------------------------------------------------------------- /level-1/lecture-046/README.md: -------------------------------------------------------------------------------- 1 | # Lecture 46 | Did not attend due to college exams 2 | 3 | - Refer DP Problems [here](https://github.com/aman-5757/pepcodingCodes/tree/master/FJP%203/08.%20DP) 4 | -------------------------------------------------------------------------------- /level-1/lecture-043/Main.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Main { 5 | 6 | public static void main(String[] args) throws Exception { 7 | 8 | } 9 | } -------------------------------------------------------------------------------- /level-1/lecture-044/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | sc.close(); 7 | } 8 | } -------------------------------------------------------------------------------- /level-1/lecture-056/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | sc.close(); 7 | } 8 | } -------------------------------------------------------------------------------- /level-1/lecture-033/README.md: -------------------------------------------------------------------------------- 1 | # AGENDA : Backtracking Wrapped | OOPS + Linked Lists Started 2 | 3 | ## [Knights Tour](https://thatbeautifuldream.github.io/pepcoding-dsa/lecture-033/knights-tour.html) 4 | 5 | ## OOPS Basic Overview 6 | 7 | ## Linked Lists Overview 8 | -------------------------------------------------------------------------------- /level-1/lecture-050/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Main { 4 | 5 | // merge k sorted lists 6 | 7 | public static void main(String[] args) { 8 | Scanner sc = new Scanner(System.in); 9 | 10 | sc.close(); 11 | } 12 | } -------------------------------------------------------------------------------- /level-1/lecture-040/README.md: -------------------------------------------------------------------------------- 1 | # Agenda | Dynamic Programming 2 | 3 | ## [Fibonacci-dp](https://thatbeautifuldream.github.io/pepcoding-dsa/lecture-040/fibonacci-dp.html) 4 | 5 | ## [Climb Stairs](https://thatbeautifuldream.github.io/pepcoding-dsa/lecture-040/climb-stairs.html) 6 | -------------------------------------------------------------------------------- /level-1/lecture-001/print-t.md: -------------------------------------------------------------------------------- 1 | # Print T 2 | 3 | ```java 4 | public class Main { 5 | public static void main(String[] args){ 6 | System.out.println("*****"); 7 | System.out.println(" *"); 8 | System.out.println(" *"); 9 | System.out.println(" *"); 10 | } 11 | } 12 | ``` -------------------------------------------------------------------------------- /level-1/lecture-001/variables.md: -------------------------------------------------------------------------------- 1 | ```java 2 | public class Main { 3 | public static void main(String[] args) { 4 | int a = 10; 5 | int b = 20; 6 | int c = 30; 7 | 8 | System.out.println("a = " + a); 9 | System.out.println("b = " + b); 10 | System.out.println("c = " + c); 11 | } 12 | } 13 | ``` -------------------------------------------------------------------------------- /level-1/lecture-032/README.md: -------------------------------------------------------------------------------- 1 | # Agenda : Backtracking Problems 2 | 3 | ## [Target Sum Subsets](https://thatbeautifuldream.github.io/pepcoding-dsa/lecture-032/target-sum-subsets.html) 4 | 5 | ## [N Queens](https://thatbeautifuldream.github.io/pepcoding-dsa/lecture-032/n-queens.html) 6 | 7 | ### Homework: [Leetcode 784.Letter Case Permutation](https://leetcode.com/problems/letter-case-permutation/description/) 8 | -------------------------------------------------------------------------------- /level-1/lecture-041/README.md: -------------------------------------------------------------------------------- 1 | # Agenda | Dynamic Programming 2 | 3 | ## [Climb Stairs With Variable Jumps](https://thatbeautifuldream.github.io/pepcoding-dsa/lecture-041/climb-stairs-with-variable-jumps.html) 4 | 5 | ## [Min Cost In Maze Traversal](https://thatbeautifuldream.github.io/pepcoding-dsa/lecture-041/min-cost-in-maze-traversal.html) 6 | 7 | ## Home Work | Revise and Improve | Climb stairs with Minimum Jumps 8 | -------------------------------------------------------------------------------- /level-1/lecture-001/hw/reverse-of-a-number.md: -------------------------------------------------------------------------------- 1 | # Print reverse of a number 2 | 3 | ```java 4 | public class ReverseOfaNumber { 5 | public static void main(String[] args) { 6 | int num = 12345; 7 | int rev = 0; 8 | int rem; 9 | while (num != 0) { 10 | rem = num % 10; 11 | System.out.println(rem); 12 | num = num / 10; 13 | } 14 | } 15 | }``` -------------------------------------------------------------------------------- /level-1/lecture-030/README.md: -------------------------------------------------------------------------------- 1 | # Lecture 30 - Agenda : 2 | 3 | ## [Print Maze path with Jumps](https://thatbeautifuldream.github.io/pepcoding-dsa/lecture-030/print-maze-path-with-jumps.html) 4 | 5 | ## [Print Permutations](https://thatbeautifuldream.github.io/pepcoding-dsa/lecture-030/print-permutations.html) 6 | 7 | ## [Print Encodings](https://thatbeautifuldream.github.io/pepcoding-dsa/lecture-030/print-encodings.html) 8 | -------------------------------------------------------------------------------- /level-1/lecture-001/hw/count-digits.md: -------------------------------------------------------------------------------- 1 | # Count Digits of a Number 2 | 3 | ```java 4 | import java.util.*; 5 | 6 | public class Main { 7 | public static void main(String[] args) { 8 | Scanner sc = new Scanner(System.in); 9 | int n = sc.nextInt(); 10 | int d = 0; 11 | do { 12 | d++; 13 | n /= 10; 14 | } while (n != 0); 15 | System.out.println(d); 16 | } 17 | } 18 | ``` -------------------------------------------------------------------------------- /level-1/lecture-001/input-output.md: -------------------------------------------------------------------------------- 1 | # Basic Input & Output in Java 2 | 3 | ```java 4 | import java.util.Scanner; 5 | public class inputOutput { 6 | public static void main(String[] args) { 7 | Scanner sc = new Scanner(System.in); 8 | int a = sc.nextInt(); 9 | int b = sc.nextInt(); 10 | 11 | System.out.println("a = "+a); 12 | System.out.println("b = "+b); 13 | sc.close(); 14 | } 15 | } 16 | ``` -------------------------------------------------------------------------------- /level-1/lecture-060/README.md: -------------------------------------------------------------------------------- 1 | # Tree Traversals 2 | 3 | - Pre-order, In-order, Post-order 4 | - Pre-order: `root, left, right` 5 | - In-order: `left, root, right` 6 | - Post-order: `left, right, root` 7 | 8 | ## Lets take an example of a binary tree 9 | 10 | ```text 11 | 1 12 | / \ 13 | 2 3 14 | / \ / \ 15 | 4 5 6 7 16 | ``` 17 | 18 | - Pre-order: `1, 2, 4, 5, 3, 6, 7` 19 | - In-order: `4, 2, 5, 1, 6, 3, 7` 20 | - Post-order: `4, 5, 2, 6, 7, 3, 1` 21 | 22 | -------------------------------------------------------------------------------- /level-1/archive/test-problems/test-001/README.md: -------------------------------------------------------------------------------- 1 | # Test 001 2 | 3 | ## [Grandfather's Car](https://thatbeautifuldream.github.io/pepcoding-dsa/archive/test-problems/test-001/grandfathers-car.html) 4 | 5 | ## [All Indices of array](https://thatbeautifuldream.github.io/pepcoding-dsa/archive/test-problems/test-001/all-indices-of-array.html) 6 | 7 | ## [Number of rectangles that can form the largest Square](https://thatbeautifuldream.github.io/pepcoding-dsa/archive/test-problems/test-001/number-of-rectangles-that-can-form-the-largest-square.html) 8 | -------------------------------------------------------------------------------- /level-1/lecture-035/README.md: -------------------------------------------------------------------------------- 1 | # Agenda (Linked Lists contd.) 2 | 3 | ## [Get Value in Linked List](https://thatbeautifuldream.github.io/lecture-035/get-value-in-linked-list.html) 4 | 5 | ## [Add first in a Linked List](https://thatbeautifuldream.github.io/lecture-035/add-first-in-linked-list.html) 6 | 7 | ## [Add at index in Linked List](https://thatbeautifuldream.github.io/lecture-035/add-at-index-in-linked-list.html) 8 | 9 | ## [Remove Last In Linked List](https://thatbeautifuldream.github.io/lecture-035/remove-last-in-linked-list.html) 10 | -------------------------------------------------------------------------------- /level-1/archive/test-problems/test-002/first-missing -positive.md: -------------------------------------------------------------------------------- 1 | # First Missing Positive 2 | 3 | `Hard` 4 | 5 | > 1. Given an unsorted integer array 'arr'. 6 | > 2. Find the smallest missing positive integer. 7 | > `Note : You must implement an algorithm that runs in O(n) time and uses constant extra space.` 8 | 9 | ``` 10 | Constraints 11 | 1. 1 <= nums.length <= 10^5 12 | 2. -2^31 <= nums[i] <= 2^31 - 1 13 | 14 | Format 15 | Input 16 | Input is managed for you. 17 | 18 | Output 19 | Output is managed for you. 20 | 21 | Example 22 | Sample Input 23 | 24 | 4 25 | 3 4 -1 1 26 | 27 | Sample Output 28 | 2 29 | ``` 30 | -------------------------------------------------------------------------------- /level-1/lecture-055/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | // Stack Creation 6 | Stack st = new Stack<>(); 7 | // Stack Push 8 | st.push(10); 9 | st.push(20); 10 | st.push(30); 11 | st.push(40); 12 | st.push(50); 13 | 14 | System.out.println("Stack Elements: " + st); 15 | 16 | // Stack Overflow 17 | while (true) { 18 | st.push(10); // Stack Overflows here 19 | } 20 | // fills up java heap memory 21 | 22 | 23 | 24 | 25 | } 26 | } -------------------------------------------------------------------------------- /level-1/lecture-038/README.md: -------------------------------------------------------------------------------- 1 | # Agenda | Linked List Problems 2 | 3 | ## [Remove Duplicates In A Sorted Linked List](https://thatbeautifuldream.github.io/pepcoding-dsa/lecture-038/remove-duplicates-in-a-sorted-linked-list.html) 4 | 5 | ## [Odd Even Linked List](https://thatbeautifuldream.github.io/pepcoding-dsa/lecture-038/odd-even-linked-list.html) 6 | 7 | ## [K Reverse In Linked List](https://thatbeautifuldream.github.io/pepcoding-dsa/lecture-038/k-reverse-in-linked-list.html) 8 | 9 | ## [H/W : Is Linked List A Palindrome?](https://www.pepcoding.com/resources/online-java-foundation/linked-lists/is-linkedlist-palindromic-official/ojquestion) 10 | -------------------------------------------------------------------------------- /level-1/lecture-001/print-z.md: -------------------------------------------------------------------------------- 1 | # Print Z 2 | 3 | > 1. You are required to print a 'z' of size 5 using '\*'. 4 | 5 | ```text 6 | Input Format 7 | There is no input 8 | Output Format 9 | print-z-in-java 10 | 11 | Constraints 12 | No Constraints 13 | Sample Output 14 | ***** 15 | * 16 | * 17 | * 18 | ***** 19 | ``` 20 | 21 | ## Solution 22 | 23 | ```java 24 | public class Main { 25 | public static void main(String[] args) { 26 | System.out.println("*****"); 27 | System.out.println(" *"); 28 | System.out.println(" *"); 29 | System.out.println(" *"); 30 | System.out.println("*****"); 31 | } 32 | } 33 | ``` 34 | -------------------------------------------------------------------------------- /level-1/lecture-001/hw/digits-of-a-number.md: -------------------------------------------------------------------------------- 1 | # Digits of a Number 2 | 3 | ```java 4 | import java.util.*; 5 | 6 | public class Main { 7 | public static void main(String[] args) { 8 | // write your code here 9 | Scanner sc = new Scanner(System.in); 10 | int number = sc.nextInt(); 11 | // Converting the integer input to string data 12 | String string_number = Integer.toString(number); 13 | // Traversing through the string using for loop 14 | for (int i = 0; i < string_number.length(); i++) { 15 | // Printing the characters at each position 16 | System.out.println(string_number.charAt(i)); 17 | } 18 | } 19 | } 20 | ``` -------------------------------------------------------------------------------- /level-1/archive/test-problems/test-002/sum-of-all-length-subarrays.md: -------------------------------------------------------------------------------- 1 | # Sum of All Odd Length Subarrays 2 | 3 | `Easy` 4 | 5 | > Given an array of positive integers arr, calculate the sum of all possible odd-length subarrays. 6 | 7 | > A subarray is a contiguous subsequence of the array. 8 | 9 | > Return the sum of all odd-length subarrays of arr. 10 | 11 | ``` 12 | Constraints 13 | 1 <= arr.length <= 100 14 | 1 <= arr[i] <= 1000 15 | 16 | Format 17 | Input 18 | Input and output are managed for you. Just complete the function. 19 | 20 | Output 21 | Input and output are managed for you. Just complete the function. 22 | 23 | Example 24 | Sample Input 25 | 26 | 5 27 | 1 4 2 5 3 28 | 29 | Sample Output 30 | 58 31 | ``` 32 | -------------------------------------------------------------------------------- /level-1/lecture-001/hw/primes.md: -------------------------------------------------------------------------------- 1 | #Print Primes 2 | 3 | ```java 4 | 5 | import java.util.Scanner; 6 | 7 | public class Primes { 8 | public static void main(String[] args) { 9 | Scanner input = new Scanner(System.in); 10 | System.out.print("Enter a number: "); 11 | int n = input.nextInt(); 12 | boolean isPrime = true; 13 | for(int i = 0; i < n/2; i++){ 14 | if(n % i == 0){ 15 | isPrime = false; 16 | break; 17 | } 18 | } 19 | if(isPrime){ 20 | System.out.println(n + " is a prime number."); 21 | } 22 | else{ 23 | System.out.println(n + " is not a prime number."); 24 | } 25 | }`` 26 | ` -------------------------------------------------------------------------------- /level-1/lecture-003/hw/gcd-lcm-naive.md: -------------------------------------------------------------------------------- 1 | # GCD LCM (Naive Solution) 2 | 3 | ```java 4 | import java.util.Scanner; 5 | 6 | public class GcdLcmNaive { 7 | public static void main(String[] args) { 8 | Scanner sc = new Scanner(System.in); 9 | int num1 = sc.nextInt(); 10 | int num2 = sc.nextInt(); 11 | int gcd = 0, lcm, count = 1, small; 12 | small = (num1 < num2) ? num1 : num2; 13 | 14 | while (count <= small) { 15 | if (num1 % count == 0 && num2 % count == 0) { 16 | gcd = count; 17 | } 18 | count++; 19 | } 20 | 21 | lcm = (num1 * num2) / gcd; 22 | 23 | System.out.println(gcd); 24 | System.out.println(lcm); 25 | } 26 | } 27 | ``` -------------------------------------------------------------------------------- /level-1/lecture-039/README.md: -------------------------------------------------------------------------------- 1 | # Agenda 2 | 3 | ## [Display Reverse (recursive) - Linked List](https://thatbeautifuldream.github.io/pepcoding-dsa/lecture-039/display-reverse-recursive-linked-list.html) 4 | 5 | ## [Reverse Linked List (pointer - Recursive)](https://thatbeautifuldream.github.io/pepcoding-dsa/lecture-039/reverse-linked-list-pointer-recursive.html) 6 | 7 | ## [Is Linked List A Palindrome?](https://thatbeautifuldream.github.io/pepcoding-dsa/lecture-039/is-linked-list-a-palindrome.html) 8 | 9 | ## [Fold A Linked List](https://thatbeautifuldream.github.io/pepcoding-dsa/lecture-039/fold-a-linked-list.html) 10 | 11 | ## [Intersection Point Of Linked Lists](https://thatbeautifuldream.github.io/pepcoding-dsa/lecture-039/intersection-point-of-linked-lists.html) 12 | -------------------------------------------------------------------------------- /level-1/archive/test-problems/test-002/determine-whether-matrix-can-be-obtained-by-rotation.md: -------------------------------------------------------------------------------- 1 | # Determine Whether Matrix Can Be Obtained By Rotation 2 | 3 | `Easy` 4 | 5 | > Given two n x n binary matrices mat and target, return true if it is possible to make mat equal to target by rotating mat in 90-degree increments, or false otherwise. 6 | 7 | ``` 8 | Constraints 9 | n == mat.length == target.length 10 | n == mat[i].length == target[i].length 11 | 1 <= n <= 10 12 | mat[i][j] and target[i][j] are either 0 or 1. 13 | 14 | Format 15 | Input 16 | Input and output is managed for you. 17 | 18 | Output 19 | Input and output is managed for you. 20 | 21 | Example 22 | Sample Input 23 | 24 | 2 25 | 2 26 | 0 1 27 | 1 0 28 | 2 29 | 2 30 | 1 0 31 | 0 1 32 | 33 | Sample Output 34 | true 35 | ``` 36 | -------------------------------------------------------------------------------- /level-1/lecture-060/144. Binary Tree Preorder Traversal/Main.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. public class TreeNode { int val; TreeNode left; TreeNode 3 | * right; TreeNode() {} TreeNode(int val) { this.val = val; } TreeNode(int val, TreeNode left, 4 | * TreeNode right) { this.val = val; this.left = left; this.right = right; } } 5 | */ 6 | class Solution { 7 | public void preOrder(TreeNode root, List ls) { 8 | if (root == null) 9 | return; 10 | ls.add(root.val); 11 | preOrder(root.left, ls); 12 | preOrder(root.right, ls); 13 | } 14 | 15 | public List preorderTraversal(TreeNode root) { 16 | List ls = new ArrayList<>(); 17 | preOrder(root, ls); 18 | return ls; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /level-1/archive/test-problems/test-001/number-of-rectangles-that-can-form-the-largest-square.md: -------------------------------------------------------------------------------- 1 | # Number Of Rectangles That Can Form The Largest Square 2 | 3 | `Easy` 4 | 5 | > You are given an array rectangles where rectangles[i] = [li, wi] represents the ith rectangle of length li and width wi. 6 | 7 | > You can cut the ith rectangle to form a square with a side length of k if both k 8 | 9 | ``` 10 | Constraints 11 | 1 <= rectangles.length <= 1000 12 | rectangles[i].length == 2 13 | 1 <= li, wi <= 10^9 14 | li != wi 15 | 16 | Format 17 | Input 18 | Input and output are managed for you. Just complete the function. 19 | 20 | Output 21 | Input and output are managed for you. Just complete the function. 22 | 23 | Example 24 | Sample Input 25 | 26 | 4 27 | 5 8 28 | 3 9 29 | 5 12 30 | 16 5 31 | 32 | Sample Output 33 | 3 34 | ``` 35 | -------------------------------------------------------------------------------- /level-1/lecture-053/sort 0 1 2/Main.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public void swap(int [] arr, int i, int j){ 3 | int t = arr[i]; 4 | arr[i] = arr[j]; 5 | arr[j] = t; 6 | } 7 | public void sortColors(int[] nums) { 8 | int n = nums.length; 9 | int i = 0; 10 | int j = 0; 11 | int k = n-1; 12 | 13 | 14 | while(j <= k ){ 15 | int val = nums[j]; 16 | 17 | if(val == 0){ 18 | swap(nums, i, j); 19 | i++; 20 | j++; 21 | } 22 | else if(val == 1){ 23 | j++; 24 | } 25 | else{ 26 | //val --> 2 27 | swap(nums, j, k); 28 | k--; 29 | } 30 | } 31 | } 32 | } -------------------------------------------------------------------------------- /level-1/lecture-001/hw/primes-in-a-range.md: -------------------------------------------------------------------------------- 1 | # Print all Primes in the range given 2 | 3 | ```java 4 | import java.util.Scanner; 5 | 6 | public class Main { 7 | public static void main(String[] args) { 8 | Scanner scanner = new Scanner(System.in); 9 | int start = scanner.nextInt(); 10 | int end = scanner.nextInt(); 11 | for (int i = start; i <= end; i++) { 12 | if (isPrime(i)) { 13 | System.out.println(i); 14 | } 15 | } 16 | } 17 | 18 | public static boolean isPrime(int number) { 19 | if (number <= 1) { 20 | return false; 21 | } 22 | for (int i = 2; i <= Math.sqrt(number); i++) { 23 | if (number % i == 0) { 24 | return false; 25 | } 26 | } 27 | return true; 28 | } 29 | } 30 | ``` -------------------------------------------------------------------------------- /level-1/lecture-042/target-sum-subsets-dp.md: -------------------------------------------------------------------------------- 1 | # Target Sum Subsets - Dp 2 | 3 | `Medium` 4 | 5 | > 1. You are given a number n, representing the count of elements. 6 | > 2. You are given n numbers. 7 | > 3. You are given a number "tar". 8 | > 4. You are required to calculate and print true or false, if there is a subset the elements of which add 9 | > up to "tar" or not. 10 | 11 | ## Input Format 12 | 13 | > A number n 14 | > n1 15 | > n2 16 | > .. n number of elements 17 | > A number tar 18 | 19 | ## Output Format 20 | 21 | > true or false as required 22 | 23 | ## Constraints 24 | 25 | > 1 <= n <= 30 26 | > 0 <= n1, n2, .. n elements <= 20 27 | > 0 <= tar <= 50 28 | 29 | ## Sample Input 30 | 31 | ``` 32 | 5 33 | 4 34 | 2 35 | 7 36 | 1 37 | 3 38 | 10 39 | ``` 40 | 41 | ## Sample Output 42 | 43 | ``` 44 | true 45 | ``` 46 | 47 | ## Solution 48 | 49 | ```java 50 | 51 | ``` 52 | -------------------------------------------------------------------------------- /level-1/archive/test-problems/test-002/README.md: -------------------------------------------------------------------------------- 1 | # Test 002 2 | 3 | ## [exact-special-positions-in-a-binary-matrix](https://thatbeautifuldream.github.io/pepcoding-dsa/archive/test-problems/test-002/exact-special-positions-in-a-binary-matrix.html) 4 | 5 | ## [seating-arrangement](https://thatbeautifuldream.github.io/pepcoding-dsa/archive/test-problems/test-002/seating-arrangement.html) 6 | 7 | ## [determine-whether-matrix-can-be-obtained-by-rotation](https://thatbeautifuldream.github.io/pepcoding-dsa/archive/test-problems/test-002/determine-whether-matrix-can-be-obtained-by-rotation.html) 8 | 9 | ## [first-missing -positive](https://thatbeautifuldream.github.io/pepcoding-dsa/archive/test-problems/test-002/first-missing-positive.html) 10 | 11 | ## [sum-of-all-length-subarrays](https://thatbeautifuldream.github.io/pepcoding-dsa/archive/test-problems/test-002/sum-of-all-length-subarrays.html) 12 | -------------------------------------------------------------------------------- /level-1/archive/test-problems/intuit-test/001-water-regions.md: -------------------------------------------------------------------------------- 1 | # Water Regions 2 | 3 | `Easy` 4 | 5 | > 1. Given n non-negative integers a1, a2, ..., an. 6 | > 2. Each represents a point at coordinate (i, ai). 7 | > 3. "n" vertical lines are drawn such that the two endpoints of the line i is at (i, ai) and (i, 0). 8 | > 4. Find two lines, which, together with the x-axis forms a region, such that the region contains the most water. 9 | > Note : that you may not slant the container. 10 | 11 | ``` 12 | Constraints 13 | 14 | 1. n == height.length 15 | 2. 2 <= n <= 105 16 | 3. 0 <= height[i] <= 104 17 | 18 | Format 19 | 20 | Input 21 | 22 | 1. Take 'n', size of Array 23 | 2. Take 'n' different element for Array 24 | 25 | Output 26 | 27 | 1. Max water 28 | 29 | Example 30 | 31 | Sample Input 32 | 33 | 9 34 | 1 35 | 8 36 | 6 37 | 2 38 | 5 39 | 4 40 | 8 41 | 3 42 | 7 43 | 44 | Sample Output 45 | 46 | 49 47 | ``` 48 | -------------------------------------------------------------------------------- /level-1/lecture-036/README.md: -------------------------------------------------------------------------------- 1 | # Agenda (Linked Lists Continued) [Notes](./lecture-036.pdf) 2 | 3 | ## [Reverse Linked List (Data Iterative)](https://thatbeautifuldream.github.io/pepcoding-dsa/lecture-036/linked-list/reverse-linked-list-data-iterative.html) 4 | 5 | ## [Reverse Linked List (Pointer Iterative)](https://thatbeautifuldream.github.io/pepcoding-dsa/lecture-036/linked-list/reverse-linked-list-pointer-iterative.html) 6 | 7 | ## [Linked List To Stack Adapter](https://thatbeautifuldream.github.io/pepcoding-dsa/lecture-036/linked-list/linked-list-to-stack-adapter.html) 8 | 9 | ## [Linked List To Queue Adapter](https://thatbeautifuldream.github.io/pepcoding-dsa/lecture-036/linked-list/linked-list-to-queue-adapter.html) 10 | 11 | ## [Kth Node From End Of Linked List (Approach Discussed)](https://thatbeautifuldream.github.io/pepcoding-dsa/lecture-036/linked-list/kth-node-from-end-of-linked-list-approach-discussed.html) 12 | -------------------------------------------------------------------------------- /level-1/lecture-055/README.md: -------------------------------------------------------------------------------- 1 | # Stack Data Structure | Java 2 | 3 | - Stack is a data structure that can be used to store and manipulate a list of items in a LIFO (last in, first out) fashion. 4 | - It is a linear data structure because it is made up of a collection of elements. 5 | 6 | ## Stack Initialized with an empty list 7 | 8 | ```java 9 | Stack st = new Stack<>(); 10 | // Stack operations 11 | st.push(1); 12 | st.push(2); 13 | st.push(3); 14 | st.push(4); 15 | st.push(5); 16 | System.out.println(st); 17 | // Output: [5, 4, 3, 2, 1] 18 | ``` 19 | 20 | ## Stack Operations 21 | 22 | - `push`: Adds an element to the top of the stack. 23 | - `pop`: Removes an element from the top of the stack and returns it. 24 | - `peek`: Returns the element at the top of the stack without removing it. 25 | - `isEmpty`: Returns true if the stack is empty, false otherwise. 26 | - `size`: Returns the number of elements in the stack. 27 | -------------------------------------------------------------------------------- /level-1/lecture-004/pattern-2.md: -------------------------------------------------------------------------------- 1 | # Pattern 2 2 | 3 | > 1. You are given a number n. 4 | > 2. You've to create a pattern of \* and separated by tab as shown in output format. 5 | 6 | ``` 7 | Input Format 8 | A number n 9 | Output Format 10 | * * * 11 | * * 12 | * 13 | 14 | Constraints 15 | 1 <= n <= 100 16 | Sample Input 17 | 5 18 | Sample Output 19 | * * * * * 20 | * * * * 21 | * * * 22 | * * 23 | * 24 | ``` 25 | 26 | ## Solution 27 | 28 | ```java 29 | import java.util.*; 30 | 31 | public class Main { 32 | 33 | public static void main(String[] args) { 34 | Scanner scn = new Scanner(System.in); 35 | int n = scn.nextInt(); 36 | int nst = n; 37 | for(int r = 1; r <= n; r++) { 38 | for(int cst = 1; cst <= nst; cst++) { 39 | System.out.print("*\t"); 40 | } 41 | nst--; 42 | System.out.println(); 43 | } 44 | 45 | } 46 | } 47 | ``` 48 | -------------------------------------------------------------------------------- /level-1/lecture-004/pattern-1.md: -------------------------------------------------------------------------------- 1 | # Pattern 1 2 | 3 | > 1. You are given a number n. 4 | > 2. You've to create a pattern of \* and separated by tab as shown in output format. 5 | 6 | ``` 7 | Input Format 8 | A number n 9 | Output Format 10 | * 11 | * * 12 | * * * 13 | * * * * 14 | . 15 | . 16 | * * * * ...n times ... * 17 | 18 | 19 | Constraints 20 | 1 <= n <= 100 21 | Sample Input 22 | 2 23 | Sample Output 24 | * 25 | * * 26 | ``` 27 | 28 | ## Solution 29 | 30 | ```java 31 | import java.util.*; 32 | 33 | public class Main { 34 | 35 | public static void main(String[] args) { 36 | Scanner scn = new Scanner(System.in); 37 | int n = scn.nextInt(); 38 | int nst = 1; 39 | for(int r = 1; r <= n; r++) { 40 | for(int cst = 1; cst <= nst; cst++) { 41 | System.out.print("*\t"); 42 | } 43 | nst++; 44 | System.out.println(); 45 | } 46 | 47 | } 48 | } 49 | ``` 50 | -------------------------------------------------------------------------------- /level-1/lecture-002/hw/count-digits.md: -------------------------------------------------------------------------------- 1 | # Count Digits In A Number 2 | 3 | > 1. You've to count the number of digits in a number. 4 | > 2. Take as input "n", the number for which the digits has to be counted. 5 | > 3. Print the digits in that number. 6 | 7 | ``` 8 | Input Format 9 | "n" where n is any integer. 10 | Output Format 11 | "d" where d is the number of digits in the number "n" 12 | 13 | Constraints 14 | 1 <= n < 10^9 15 | Sample Input 16 | 65784383 17 | Sample Output 18 | 8 19 | ``` 20 | 21 | ## Solution 22 | 23 | ```java 24 | import java.util.*; 25 | 26 | public class Main { 27 | 28 | public static void main(String[] args) { 29 | // write your code here 30 | Scanner sc = new Scanner(System.in); 31 | int n = sc.nextInt(); 32 | int d = 0; 33 | while (n != 0) { 34 | d++; 35 | n /= 10; 36 | } 37 | System.out.println(d); 38 | sc.close(); 39 | } 40 | } 41 | ``` 42 | -------------------------------------------------------------------------------- /level-1/archive/test-problems/test-002/exact-special-positions-in-a-binary-matrix.md: -------------------------------------------------------------------------------- 1 | # Exact Special Positions in a Binary Matrix 2 | 3 | `Easy` 4 | 5 | > Given a rows x cols matrix mat, where mat[i][j] is either 0 or 1, print all the coordinate of special positions in mat. 6 | 7 | > A position (i,j) is called special if mat[i][j] == 1 and all other elements in row i and column j are 0 (rows and columns are 0-indexed). 8 | 9 | ``` 10 | Constraints 11 | rows == mat.length 12 | cols == mat[i].length 13 | 1 <= rows, cols <= 100 14 | mat[i][j] is 0 or 1 15 | 16 | Format 17 | Input 18 | First line contains two integers which represents number of rows (n) and columns (m) of a matrix respectively. 19 | Next n lines contains m integers each which represents content of matrix. 20 | 21 | Output 22 | Print coordinates of all special positions. 23 | 24 | Example 25 | Sample Input 26 | 27 | 3 3 28 | 1 0 0 29 | 0 1 0 30 | 0 0 1 31 | 32 | Sample Output 33 | 0, 0 34 | 1, 1 35 | 2, 2 36 | ``` 37 | -------------------------------------------------------------------------------- /level-1/lecture-023/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Main { 4 | 5 | public static int powerLinear(int n, int p) { 6 | return (n == 0) ? 1 : n * powerLinear(n, p - 1); 7 | } 8 | 9 | public static int powerLogarithmic(int n, int p) { 10 | return (p == 0) ? 1 : (p % 2 == 0) ? powerLogarithmic(n * n, p / 2) : n * powerLogarithmic(n * n, p / 2); 11 | } 12 | 13 | public static void pzz(int n) { 14 | if (n == 0) 15 | return; 16 | System.out.print(n + " "); // pre 17 | pzz(n - 1); 18 | System.out.print(n + " "); // in 19 | pzz(n - 1); 20 | System.out.print(n + " "); // post 21 | } 22 | 23 | public static void main(String[] args) { 24 | Scanner sc = new Scanner(System.in); 25 | int n = sc.nextInt(); 26 | // int p = sc.nextInt(); 27 | // System.out.println(powerLinear(n, p)); 28 | // System.out.println(powerLogarithmic(n, p)); 29 | pzz(n); 30 | sc.close(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /level-1/lecture-039/fold-a-linked-list.md: -------------------------------------------------------------------------------- 1 | # Fold A Linked List 2 | 3 | `Easy` 4 | 5 | > 1. You are given a partially written LinkedList class. 6 | > 2. You are required to complete the body of fold function. The function is expected to place last element after 1st element, 2nd last element after 2nd element and so on. For more insight check the example 7 | 8 | ## Example 1 9 | 10 | ``` 11 | 1->2->3->4->5 12 | will fold as 13 | 1->5->2->4->3 14 | ``` 15 | 16 | ## Example 2 17 | 18 | ``` 19 | 1->2->3->4->5->6 20 | 1->6->2->5->3->4 21 | ``` 22 | 23 | ## Input Format 24 | 25 | > Input is managed for you 26 | 27 | ## Output Format 28 | 29 | > Output is managed for you 30 | 31 | Constraints 32 | 33 | > 1. Time complexity -> O(n) 34 | > 2. Space complexity -> Recursion space, O(n) 35 | 36 | ## Sample Input 37 | 38 | ``` 39 | 5 40 | 1 2 3 4 5 41 | 10 42 | 20 43 | ``` 44 | 45 | ## Sample Output 46 | 47 | ``` 48 | 1 2 3 4 5 49 | 1 5 2 4 3 50 | 10 1 5 2 4 3 20 51 | ``` 52 | 53 | ## Solution 54 | 55 | ```java 56 | // will update it later 57 | ``` 58 | -------------------------------------------------------------------------------- /level-1/lecture-003/hw/prime-factorisation.md: -------------------------------------------------------------------------------- 1 | # Prime Factorization Of A Number 2 | 3 | > 1. You are required to display the prime factorization of a number. 4 | > 2. Take as input a number n. 5 | > 3. Print all its prime factors from smallest to largest. 6 | 7 | ``` 8 | Input Format 9 | n, an integer 10 | Output Format 11 | p1 p2 p3 p4.. all prime factors of n 12 | 13 | Constraints 14 | 2 <= n < 10 ^ 9 15 | Sample Input 16 | 1440 17 | Sample Output 18 | 2 2 2 2 2 3 3 5 19 | ``` 20 | 21 | ## Solution 22 | 23 | ```java 24 | import java.util.Scanner; 25 | 26 | // prime factorisation of number 27 | public class PrimeFactorisation { 28 | public static void main(String[] args) { 29 | Scanner sc = new Scanner(System.in); 30 | int n = sc.nextInt(); 31 | for (int div = 2; div * div <= n; div++) { 32 | while (n % div == 0) { 33 | n /= div; 34 | System.out.print(div + " "); 35 | } 36 | } 37 | if (n != 1) { 38 | System.out.println(n); 39 | } 40 | } 41 | } 42 | ``` 43 | -------------------------------------------------------------------------------- /level-1/lecture-048/GetCommonElements2.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class GetCommonElement2 { 5 | 6 | public static void main(String[] args) throws Exception { 7 | Scanner scn = new Scanner(System.in); 8 | int n1 = scn.nextInt(); 9 | int[] a = new int[n1]; 10 | for (int i = 0; i < n1; i++) 11 | a[i] = scn.nextInt(); 12 | 13 | int n2 = scn.nextInt(); 14 | int[] b = new int[n2]; 15 | for (int i = 0; i < n2; i++) 16 | b[i] = scn.nextInt(); 17 | 18 | // FreqMap of A array 19 | HashMap hm = new HashMap<>(); 20 | for (int ele : a) 21 | hm.put(ele, hm.getOrDefault(ele, 0) + 1); 22 | 23 | for (int ele : b) { 24 | if (hm.containsKey(ele) && hm.get(ele) > 0) { 25 | System.out.println(ele); 26 | 27 | int oldFreq = hm.get(ele); 28 | int newFreq = oldFreq - 1; 29 | hm.put(ele, newFreq); 30 | } 31 | } 32 | 33 | } 34 | 35 | } -------------------------------------------------------------------------------- /level-1/archive/test-problems/test-001/grandfathers-car.md: -------------------------------------------------------------------------------- 1 | # Grandfather’s car 2 | 3 | `Medium` 4 | 5 | > Himanshu misses his grandfather a lot. He has one last gift from his grandfather: an old car. 6 | > This car is so old that it consumes a lot of fuel but Himanshu decides he wants to go on a ride in that car. He filled the car’s tank with fuel(filled the tank with capacity n) and left for a ride in the car. There are fuel stations on that road after every “k” kilometres but the problem is he can take only 1 litre of fuel from each fuel station.it is a fact that car consumes “t” litre of fuel per km.you have to tell what is maximum distance of ride Himanshu can go on with all this circumstances. 7 | 8 | ``` 9 | Constraints 10 | 2<=n<<=10^5 11 | 1<=t<=n 12 | 2<=m<=n 13 | 14 | Format 15 | Input 16 | n(tank capacity of car) 17 | t(consumption of fuel/km) 18 | k(distance between every two fuel station) 19 | 20 | Output 21 | An integer representing the maximum km Himanshu can travel. 22 | 23 | Example 24 | Sample Input 25 | 26 | 20 27 | 3 28 | 5 29 | 30 | Sample Output 31 | 7 32 | ``` 33 | -------------------------------------------------------------------------------- /level-1/lecture-049/s.md: -------------------------------------------------------------------------------- 1 | # Sort K-sorted Array 2 | 3 | `Easy` 4 | 5 | > 1. You are given a number n, representing the size of array a. 6 | > 2. You are given n numbers, representing elements of array a. 7 | > 3. The array is nearly sorted. Every element is at-max displaced k spots left or right to it's position in the sorted array. Hence it is being called k-sorted array. 8 | > 4. You are required to sort and print the sorted array. 9 | 10 | - Note -> You can use at-max k extra space and nlogk time complexity. 11 | 12 | ## Constraints 13 | 14 | - 1 <= n <= 30 15 | - 0 <= n1, n2, .. n elements <= 100 16 | - 0 < k <= n 17 | 18 | ## Format 19 | 20 | ## Input 21 | 22 | - Input is managed for you 23 | 24 | ## Output 25 | 26 | - Print the elements of sorted array in separate lines. 27 | 28 | ## Example 29 | 30 | ## Sample Input 31 | 32 | ``` 33 | 9 34 | 3 35 | 2 36 | 4 37 | 1 38 | 6 39 | 5 40 | 7 41 | 9 42 | 8 43 | 3 44 | ``` 45 | 46 | ## Sample Output 47 | 48 | ``` 49 | 1 50 | 2 51 | 3 52 | 4 53 | 5 54 | 6 55 | 7 56 | 8 57 | 9 58 | ``` 59 | 60 | ## Solution 61 | 62 | ```java 63 | 64 | ``` 65 | -------------------------------------------------------------------------------- /level-1/archive/level-1/dynamic-programming.md: -------------------------------------------------------------------------------- 1 | # Dynamic Programming 2 | 3 | ## Dynamic Programming and Greedy 4 | 5 | [Fibonacci-dp](https://thatbeautifuldream.github.io/pepcoding-dsa/lecture-040/fibonacci-dp.html) 6 | 7 | [Climb Stairs](https://thatbeautifuldream.github.io/pepcoding-dsa/lecture-040/climb-stairs.html) 8 | 9 | [Climb Stairs With Variable Jumps](https://thatbeautifuldream.github.io/pepcoding-dsa/lecture-041/climb-stairs-with-variable-jumps.html) 10 | 11 | [Min Cost In Maze Traversal](https://thatbeautifuldream.github.io/pepcoding-dsa/lecture-041/min-cost-in-maze-traversal.html) 12 | 13 | [Goldmine](https://thatbeautifuldream.github.io/pepcoding-dsa/lecture-042/goldmine.html) 14 | 15 | [Target Sum Subsets - Dp](https://thatbeautifuldream.github.io/pepcoding-dsa/lecture-042/target-sum-subsets-dp.html) 16 | 17 | [Coin Change Combination](https://thatbeautifuldream.github.io/pepcoding-dsa/lecture-043/coin-change-combination.html) 18 | 19 | [Unbounded Knapsack](https://thatbeautifuldream.github.io/pepcoding-dsa/lecture-044/unbounded-knapsack.html) 20 | 21 | ## Time and Space Complexity 22 | -------------------------------------------------------------------------------- /level-1/lecture-011/reverse-array.md: -------------------------------------------------------------------------------- 1 | # Reverse Array 2 | 3 | > 1. You are given a number n, representing the size of array a. 4 | > 2. You are given n numbers, representing elements of array a. 5 | > 3. You are required to reverse the contents of array a. 6 | 7 | ```java 8 | import java.io.*; 9 | import java.util.*; 10 | 11 | public class Main{ 12 | public static void reverseArray(int[] arr, int n) { 13 | int i = 0, j = n - 1; 14 | while (i < j) { 15 | int temp = arr[i]; 16 | arr[i] = arr[j]; 17 | arr[j] = temp; 18 | i++; 19 | j--; 20 | } 21 | } 22 | 23 | public static void main(String[] args) { 24 | Scanner sc = new Scanner(System.in); 25 | int n = sc.nextInt(); 26 | int[] arr = new int[n]; 27 | for (int i = 0; i < n; i++) { 28 | arr[i] = sc.nextInt(); 29 | } 30 | reverseArray(arr, n); 31 | for (int i = 0; i < n; i++) { 32 | System.out.print(arr[i] + " "); 33 | } 34 | sc.close(); 35 | } 36 | 37 | } 38 | ``` 39 | -------------------------------------------------------------------------------- /level-1/archive/test-problems/README.md: -------------------------------------------------------------------------------- 1 | # Test Problems 2 | 3 | ## [Test-01](http://thatbeautifuldream.github.io/pepcoding-dsa/archive/test-problems/test-01/) 4 | 5 | | Problem | Progress | 6 | | ----------------------------------------------------- | -------- | 7 | | All Indices of Array | ✅ | 8 | | Grandfather's Car | ✅ | 9 | | Number of Rectangles that can form the largest square | ✅ | 10 | 11 | ## [Test-02](http://thatbeautifuldream.github.io/pepcoding-dsa/archive/test-problems/test-02/) 12 | 13 | | Problem | Progress | 14 | | ---------------------------------------------------- | -------- | 15 | | Determine whether matrix can be obtained by rotation | ✅ | 16 | | Exact special positions in a binary matrix | ✅ | 17 | | First missing positive | ✅ | 18 | | Seating Arrangement | ✅ | 19 | | Sum of all length subarrays | ✅ | 20 | -------------------------------------------------------------------------------- /level-1/archive/test-problems/test-001/all-indices-of-array.md: -------------------------------------------------------------------------------- 1 | # All Indices Of Array 2 | 3 | `Easy` 4 | 5 | > 1. You are given a number n, representing the count of elements. 6 | > 2. You are given n numbers. 7 | > 3. You are given a number x. 8 | > 4. You are required to find the all indices at which x occurs in array a. 9 | > 5. Return an array of appropriate size which contains all indices at which x occurs in array a. 10 | 11 | > `Note -> The online judge can't force you to write the function recursively but that is what the spirit of question is. Write recursive and not iterative logic. The purpose of the question is to aid learning recursion and not test you.` 12 | 13 | ``` 14 | Constraints 15 | 1 <= n <= 10^4 16 | 0 <= n1, n2, .. n elements <= 10 ^ 3 17 | 0 <= x <= 10 ^ 3 18 | 19 | Format 20 | Input 21 | A number n 22 | n1 23 | n2 24 | .. n number of elements 25 | A number x 26 | 27 | Output 28 | Return the array of indices from the allIndices function. Display is managed for you. 29 | 30 | Example 31 | Sample Input 32 | 33 | 6 34 | 15 35 | 11 36 | 40 37 | 4 38 | 4 39 | 9 40 | 4 41 | 42 | Sample Output 43 | 3 44 | 4 45 | ``` 46 | -------------------------------------------------------------------------------- /level-1/lecture-016/hw/wave-array-2.md: -------------------------------------------------------------------------------- 1 | # Wave Array - 2 (Homework Problem) 2 | 3 | ```java 4 | import java.util.Scanner; 5 | 6 | public class Main { 7 | 8 | // Wave Array Homework : 9 | public static void waveArray2(int[][] arr, int row, int col) { 10 | for (int j = 0; j < col; j++) { 11 | if (j % 2 != 0) { 12 | for (int i = 0; i < row; i++) { 13 | System.out.println(arr[i][j]); 14 | } 15 | } else { 16 | for (int i = row - 1; i >= 0; i--) { 17 | System.out.println(arr[i][j]); 18 | } 19 | } 20 | } 21 | } 22 | 23 | public static void main(String[] args) { 24 | Scanner sc = new Scanner(System.in); 25 | int row = sc.nextInt(); 26 | int col = sc.nextInt(); 27 | int[][] arr = new int[row][col]; 28 | for (int i = 0; i < row; i++) { 29 | for (int j = 0; j < col; j++) { 30 | arr[i][j] = sc.nextInt(); 31 | } 32 | } 33 | waveArray2(arr, row, col); 34 | sc.close(); 35 | } 36 | } 37 | 38 | ``` 39 | -------------------------------------------------------------------------------- /level-1/lecture-004/pattern-4.md: -------------------------------------------------------------------------------- 1 | # Pattern 4 2 | 3 | > 1. You are given a number n. 4 | > 2. You've to create a pattern of \* and separated by tab as shown in output format. 5 | 6 | ``` 7 | Input Format 8 | A number n 9 | Output Format 10 | 11 | Constraints 12 | 1 <= n <= 100 13 | Sample Input 14 | 5 15 | Sample Output 16 | * * * * * 17 | * * * * 18 | * * * 19 | * * 20 | * 21 | ``` 22 | 23 | ## Solution 24 | 25 | ```java 26 | import java.util.*; 27 | 28 | public class Main { 29 | 30 | public static void main(String[] args) { 31 | Scanner scn = new Scanner(System.in); 32 | int n = scn.nextInt(); 33 | int nsp = 0, nst = n; 34 | for(int r = 1; r <= n; r++) { 35 | // spaces 36 | for(int csp = 1; csp <= nsp; csp++) { 37 | System.out.print("\t"); 38 | } 39 | // stars 40 | for(int cst = 1; cst <= nst; cst++) { 41 | System.out.print("*\t"); 42 | } 43 | //update expressions 44 | nsp++; 45 | nst--; 46 | System.out.println(); 47 | } 48 | 49 | } 50 | } 51 | ``` 52 | -------------------------------------------------------------------------------- /level-1/lecture-052/sort 0 1/Main.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Main { 5 | 6 | public static void sort01(int[] arr){ 7 | int i = 0; 8 | int j = 0; 9 | int n = arr.length; 10 | for(; j hm = new HashMap(); 9 | for (int i = 0; i < s.length(); i++) { 10 | char ch = s.charAt(i); 11 | if (hm.containsKey(ch)) { 12 | int val = hm.get(ch); 13 | hm.put(ch, val + 1); // repeat character 14 | } else { 15 | hm.put(ch, 1); // first time seeing this character 16 | } 17 | // hm.put(ch, hm.getOrDefault(ch, 0) + 1); 18 | } 19 | 20 | char maxFreqChar = s.charAt(0); 21 | for (Character key : hm.keySet()) { 22 | if (hm.get(key) > hm.get(maxFreqChar)) { 23 | maxFreqChar = key; // update maxFreqChar if current key is greater than maxFreqChar 24 | } 25 | } 26 | System.out.println(maxFreqChar); 27 | sc.close(); 28 | } 29 | } -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Milind Mishra 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /level-1/lecture-004/pattern-3.md: -------------------------------------------------------------------------------- 1 | # Pattern 3 2 | 3 | > 1. You are given a number n. 4 | > 2. You've to create a pattern of \* and separated by tab as shown in output format. 5 | 6 | ```` 7 | Input Format 8 | A number n 9 | Output Format 10 | * 11 | * * 12 | * * * 13 | * * * * 14 | 15 | Constraints 16 | 1 <= n <= 10 17 | Sample Input 18 | 5 19 | Sample Output 20 | * 21 | * * 22 | * * * 23 | * * * * 24 | * * * * * 25 | ``` 26 | 27 | ## Solution 28 | 29 | ```java 30 | import java.util.*; 31 | 32 | public class Main { 33 | 34 | public static void main(String[] args) { 35 | Scanner scn = new Scanner(System.in); 36 | int n = scn.nextInt(); 37 | int nsp = n - 1, nst = 1; 38 | for(int r = 1; r <= n; r++) { 39 | // spaces 40 | for(int csp = 1; csp <= nsp; csp++) { 41 | System.out.print("\t"); 42 | } 43 | // stars 44 | for(int cst = 1; cst <= nst; cst++) { 45 | System.out.print("*\t"); 46 | } 47 | nsp--; 48 | nst++; 49 | System.out.println(); 50 | } 51 | 52 | } 53 | } 54 | ```` 55 | -------------------------------------------------------------------------------- /level-1/lecture-007/digit-frequency.md: -------------------------------------------------------------------------------- 1 | # Digit Frequency 2 | 3 | > 1. You are given a number n. 4 | > 2. You are given a digit d. 5 | > 3. You are required to calculate the frequency of digit d in number n. 6 | 7 | ``` 8 | Input Format 9 | A number n 10 | A digit d 11 | Output Format 12 | A number representing frequency of digit d in number n 13 | 14 | Constraints 15 | 0 <= n <= 10^9 16 | 0 <= d <= 9 17 | 18 | Sample Input 19 | 994543234 20 | 4 21 | Sample Output 22 | 3 23 | ``` 24 | 25 | ## Solution 26 | 27 | ```java 28 | import java.util.*; 29 | 30 | public class Main { 31 | 32 | public static void main(String[] args) { 33 | Scanner scn = new Scanner(System.in); 34 | int n = scn.nextInt(); 35 | int d = scn.nextInt(); 36 | int f = getDigitFrequency(n, d); 37 | System.out.println(f); 38 | } 39 | 40 | public static int getDigitFrequency(int n, int d) { 41 | int count = 0; 42 | while(n != 0) { 43 | int rem = n%10; 44 | n /= 10; 45 | if(rem == d) { 46 | count++; 47 | } 48 | } 49 | return count; 50 | } 51 | } 52 | ``` 53 | -------------------------------------------------------------------------------- /level-1/archive/test-problems/test-002/seating-arrangement.md: -------------------------------------------------------------------------------- 1 | # Seating Arrangement 2 | 3 | `Easy` 4 | 5 | > There are “n” students sitting in a row(0-based indexing) in an order of their allotted ID’s in an increasing order. 6 | > The teacher wants to know the seat number of a specific student if he is sitting in that row. If the student is not present in that row then he noted the right position(seat number) of the student. 7 | 8 | > Return the seat number of the student if he’s present in a row else return it’s correct position if he will be in the row. 9 | 10 | ``` 11 | Example: 12 | Input: [1,3,7,9] , target student = 3 13 | Output: 1 14 | Input: [1,3,7,9] , target student = 5 15 | Output: 2 16 | Input: [1,3,7,9] , target student = 10 17 | Output: 4 18 | 19 | Constraints 20 | 1) 1 <= nums.length <= 10^4 21 | 2) -10^4 <= nums[i] <= 10^4 22 | 3) nums contains distinct values sorted in ascending order. 23 | 4) -10^4 <= target <= 10^4 24 | 25 | Format 26 | Input 27 | Input and Output is managed for you. 28 | 29 | Output 30 | Input and Output is managed for you. 31 | 32 | Example 33 | Sample Input 34 | 35 | 4 36 | 1 3 5 6 37 | 5 38 | 39 | Sample Output 40 | 2 41 | ``` 42 | -------------------------------------------------------------------------------- /level-1/lecture-005/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Main { 4 | 5 | public static void pattern6(int n) { // hollow diamond 6 | if (n % 2 == 0) { 7 | n++; 8 | } 9 | int nsp = 1, nst = n / 2 + 1; 10 | for (int r = 1; r <= n; r++) { 11 | // stars 12 | for (int cst = 1; cst <= nst; cst++) { 13 | System.out.print("*\t"); 14 | } 15 | // spaces 16 | for (int csp = 1; csp <= nsp; csp++) { 17 | System.out.print("\t"); 18 | } 19 | // stars 20 | for (int cst = 1; cst <= nst; cst++) { 21 | System.out.print("*\t"); 22 | } 23 | if (r <= n / 2) { 24 | nst--; 25 | nsp += 2; 26 | } else { 27 | nst++; 28 | nsp -= 2; 29 | } 30 | System.out.println(); 31 | } 32 | 33 | } 34 | 35 | public static void main(String[] args) { 36 | Scanner sc = new Scanner(System.in); 37 | int n = sc.nextInt(); 38 | pattern6(n); 39 | sc.close(); 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /level-1/lecture-048/GetCommonElements.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class GetCommonElements { 5 | 6 | public static void main(String[] args) throws Exception { 7 | Scanner sc = new Scanner(System.in); 8 | int n1 = sc.nextInt(); 9 | int a1[] = new int[n1]; 10 | for (int i = 0; i < n1; i++) { 11 | a1[i] = sc.nextInt(); 12 | } 13 | int n2 = sc.nextInt(); 14 | int a2[] = new int[n2]; 15 | for (int i = 0; i < n2; i++) { 16 | a2[i] = sc.nextInt(); 17 | } 18 | // frequency map of a1[] 19 | HashMap hm = new HashMap(); 20 | for (int ele : a1) { 21 | hm.put(ele, hm.getOrDefault(ele, 0) + 1); // it will add the element if it is not present in the map 22 | } 23 | // frequency map of a2[] 24 | for (int ele : a2) { 25 | if (hm.containsKey(ele)) { 26 | System.out.println(ele); 27 | // remove 28 | hm.remove(ele); // it will remove the element if it is present in the map 29 | } 30 | } 31 | sc.close(); 32 | } 33 | } -------------------------------------------------------------------------------- /level-1/lecture-044/unbounded-knapsack.md: -------------------------------------------------------------------------------- 1 | # Unbounded Knapsack 2 | 3 | `Easy` 4 | 5 | > 1. You are given a number n, representing the count of items. 6 | > 2. You are given n numbers, representing the values of n items. 7 | > 3. You are given n numbers, representing the weights of n items. 8 | > 4. You are given a number "cap", which is the capacity of a bag you've. 9 | > 5. You are required to calculate and print the maximum value that can be created in the bag without overflowing it's capacity. 10 | > `Note -> Each item can be taken any number of times. You are allowed to put the same item again and again.` 11 | 12 | ## Input Format 13 | 14 | - A number n 15 | - v1 v2 .. n number of elements 16 | - w1 w2 .. n number of elements 17 | - A number cap 18 | 19 | ## Output Format 20 | 21 | - A number representing the maximum value that can be created in the bag without overflowing it's capacity 22 | 23 | ## Constraints 24 | 25 | - 1 <= n <= 20 26 | - 0 <= v1, v2, .. n elements <= 50 27 | - 0 < w1, w2, .. n elements <= 10 28 | - 0 < cap <= 10 29 | 30 | ## Sample Input 31 | 32 | ```text 33 | 5 34 | 15 14 10 45 30 35 | 2 5 1 3 4 36 | 7 37 | ``` 38 | 39 | ## Sample Output 40 | 41 | `100` 42 | 43 | ## Solution 44 | -------------------------------------------------------------------------------- /level-1/lecture-007/decimal-to-anybase.md: -------------------------------------------------------------------------------- 1 | # Decimal To Any Base 2 | 3 | > 1. You are given a decimal number n. 4 | > 2. You are given a base b. 5 | > 3. You are required to convert the number n into its corresponding value in base b. 6 | 7 | ```text 8 | Input Format 9 | A number n 10 | A base b 11 | Output Format 12 | A number representing corresponding value of n in number system of base b 13 | 14 | Constraints 15 | 0 <= d <= 512 16 | 2 <= b <= 10 17 | 18 | Sample Input 19 | 57 20 | 2 21 | Sample Output 22 | 111001 23 | ``` 24 | 25 | ## Solution 26 | 27 | ```java 28 | import java.util.*; 29 | 30 | public class Main{ 31 | public static void main(String[] args) { 32 | Scanner scn = new Scanner(System.in); 33 | int n = scn.nextInt(); 34 | int b = scn.nextInt(); 35 | int dn = getValueInBase(n, b); 36 | System.out.println(dn); 37 | } 38 | 39 | public static int getValueInBase(int n, int b){ 40 | int ans = 0; 41 | int pow = 1; // 10^0 = 1 42 | while(n > 0) { 43 | int rem = n % b; 44 | n /= b; 45 | ans += rem * pow; 46 | pow *= 10; 47 | } 48 | return ans; 49 | } 50 | } 51 | ``` 52 | -------------------------------------------------------------------------------- /level-1/lecture-023/power-linear.md: -------------------------------------------------------------------------------- 1 | # Power-linear 2 | 3 | > 1. You are given a number x. 4 | > 2. You are given another number n. 5 | > 3. You are required to calculate x raised to the power n. Don't change the signature of power function . 6 | 7 | > `Note -> The online judge can't force you to write the function recursively but that is what the spirit of question is. Write recursive and not iterative logic. The purpose of the question is to aid learning recursion and not test you.` 8 | 9 | ``` 10 | Input Format 11 | A number x 12 | A number n 13 | Output Format 14 | x raised to the power n 15 | 16 | Constraints 17 | 1 <= x <= 10 18 | 0 <= n <= 9 19 | Sample Input 20 | 2 21 | 5 22 | Sample Output 23 | 32 24 | ``` 25 | 26 | ## Solution 27 | 28 | ```java 29 | import java.io.*; 30 | import java.util.*; 31 | 32 | public class Main { 33 | 34 | public static void main(String[] args) throws Exception { 35 | Scanner sc = new Scanner(System.in); 36 | int x = sc.nextInt(); 37 | int n = sc.nextInt(); 38 | System.out.println(power(x, n)); 39 | } 40 | 41 | public static int power(int x, int n){ 42 | return (n == 0) ? 1 : x * power(x, n - 1); 43 | } 44 | 45 | } 46 | ``` 47 | -------------------------------------------------------------------------------- /level-1/lecture-013/inverse-array.md: -------------------------------------------------------------------------------- 1 | # Inverse Array 2 | 3 | > 1. You are given a number n, representing the size of array a. 4 | > 2. You are given n numbers, representing elements of array a. 5 | > 3. You are required to calculate the inverse of array a. 6 | 7 | ```java 8 | import java.io.*; 9 | import java.util.*; 10 | 11 | public class Main{ 12 | public static void display(int[] a){ 13 | StringBuilder sb = new StringBuilder(); 14 | 15 | for(int val: a){ 16 | sb.append(val + "\n"); 17 | } 18 | System.out.println(sb); 19 | } 20 | 21 | public static int[] inverse(int[] arr) { 22 | int[] ans = new int[arr.length]; 23 | for (int i = 0; i < arr.length; i++) { 24 | int val = arr[i]; 25 | ans[val] = i; 26 | } 27 | return ans; 28 | } 29 | 30 | public static void main(String[] args) throws Exception { 31 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 32 | 33 | int n = Integer.parseInt(br.readLine()); 34 | int[] a = new int[n]; 35 | for(int i = 0; i < n; i++){ 36 | a[i] = Integer.parseInt(br.readLine()); 37 | } 38 | 39 | int[] inv = inverse(a); 40 | display(inv); 41 | } 42 | 43 | } 44 | ``` 45 | -------------------------------------------------------------------------------- /level-1/lecture-055/valid-parentheses.md: -------------------------------------------------------------------------------- 1 | # Leetcode | 20. Valid Parentheses 2 | 3 | > Problem [Link](https://leetcode.com/problems/valid-parentheses/) 4 | 5 | ```java 6 | class Solution { 7 | public boolean isValid(String s) { 8 | Stack st = new Stack<>(); 9 | for(int i = 0; i 1. You've to display the digits of a number in reverse. 4 | > 2. Take as input "n", the number for which digits have to be display in reverse. 5 | > 3. Print the digits of the number line-wise, but in reverse order. 6 | 7 | ``` 8 | Input Format 9 | "n" where n is any integer. 10 | Output Format 11 | d1 12 | d2 13 | d3 14 | ... digits of the number in reverse 15 | 16 | Constraints 17 | 1 <= n < 10^9 18 | Sample Input 19 | 65784383 20 | Sample Output 21 | 3 22 | 8 23 | 3 24 | 4 25 | 8 26 | 7 27 | 5 28 | 6 29 | ``` 30 | 31 | ## Solution 32 | 33 | ```java 34 | import java.util.Scanner; 35 | 36 | public class ReverseOfNumber { 37 | public static void main(String[] args) { 38 | Scanner sc = new Scanner(System.in); 39 | int n = sc.nextInt(); 40 | printInReverse(n); 41 | sc.close(); 42 | } 43 | 44 | public static int printInReverse(int num) { 45 | int lastIndex = 1, res = 0; 46 | while (num != 0) { 47 | int lastDigit = num % 10; 48 | num /= 10; 49 | res += lastIndex * (int) Math.pow(10, lastDigit - 1); 50 | lastIndex++; 51 | } 52 | return res; 53 | } 54 | } 55 | ``` 56 | -------------------------------------------------------------------------------- /level-1/lecture-021/print-decreasing.md: -------------------------------------------------------------------------------- 1 | # Print Decreasing 2 | 3 | >1. You are given a positive number n. 4 | >2. You are required to print the counting from n to 1. 5 | >3. You are required to not use any loops. Complete the body of print Decreasing function to achieve it. 6 | ``` 7 | Note -> The online judge can't force you to write the function recursively but that is what the spirit of question is. Write recursive and not iterative logic. The purpose of the question is to aid learning recursion and not test you. 8 | Input Format 9 | A number n 10 | Output Format 11 | n 12 | n - 1 13 | n - 2 14 | .. 15 | 1 16 | 17 | Constraints 18 | 1 <= n <= 1000 19 | Sample Input 20 | 5 21 | Sample Output 22 | 5 23 | 4 24 | 3 25 | 2 26 | 1 27 | ``` 28 | 29 | 30 | ```java 31 | import java.io.*; 32 | import java.util.*; 33 | 34 | public class Main { 35 | 36 | public static void main(String[] args) throws Exception { 37 | Scanner sc = new Scanner(System.in); 38 | int n = sc.nextInt(); 39 | printDecreasing(n); 40 | } 41 | 42 | public static void printDecreasing(int n){ 43 | if(n == 0) { 44 | return; 45 | } 46 | System.out.println(n); 47 | printDecreasing(n-1); 48 | } 49 | 50 | } 51 | ``` -------------------------------------------------------------------------------- /level-1/lecture-052/Sort01.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Main { 5 | 6 | public static void sort01(int[] arr) { 7 | int i = 0; 8 | int j = 0; 9 | int n = arr.length; 10 | for (; j < n; j++) { 11 | if (arr[j] == 0) { 12 | swap(arr, j, i); 13 | i++; 14 | 15 | } else { 16 | 17 | } 18 | } 19 | 20 | } 21 | 22 | // used for swapping ith and jth elements of array 23 | public static void swap(int[] arr, int i, int j) { 24 | System.out.println("Swapping index " + i + " and index " + j); 25 | int temp = arr[i]; 26 | arr[i] = arr[j]; 27 | arr[j] = temp; 28 | } 29 | 30 | public static void print(int[] arr) { 31 | for (int i = 0; i < arr.length; i++) { 32 | System.out.println(arr[i]); 33 | } 34 | } 35 | 36 | public static void main(String[] args) throws Exception { 37 | Scanner scn = new Scanner(System.in); 38 | int n = scn.nextInt(); 39 | int[] arr = new int[n]; 40 | for (int i = 0; i < n; i++) { 41 | arr[i] = scn.nextInt(); 42 | } 43 | sort01(arr); 44 | print(arr); 45 | } 46 | 47 | } -------------------------------------------------------------------------------- /level-1/lecture-007/anybase-to-decimal.md: -------------------------------------------------------------------------------- 1 | # Any Base To Decimal 2 | 3 | > 1. You are given a number n. 4 | > 2. You are given a base b. n is a number on base b. 5 | > 3. You are required to convert the number n into its corresponding value in decimal number system. 6 | 7 | ``` 8 | Input Format 9 | A number n 10 | A base b 11 | Output Format 12 | A decimal number representing corresponding value of n in base b. 13 | 14 | Constraints 15 | 0 <= d <= 1000000000 16 | 2 <= b <= 10 17 | 18 | Sample Input 19 | 111001 20 | 2 21 | Sample Output 22 | 57 23 | ``` 24 | 25 | ## Solution 26 | 27 | ```java 28 | import java.util.*; 29 | 30 | public class Main{ 31 | 32 | public static void main(String[] args) { 33 | Scanner scn = new Scanner(System.in); 34 | int n = scn.nextInt(); 35 | int b = scn.nextInt(); 36 | int d = getValueIndecimal(n, b); 37 | System.out.println(d); 38 | } 39 | 40 | public static int getValueIndecimal(int n, int b){ 41 | int ans = 0; 42 | int pow = 1; // for any base (b) : b^0 = 1 43 | while(n > 0) { 44 | int rem = n % 10; 45 | n = n / 10; 46 | ans += rem * pow; 47 | pow = pow * b; 48 | } 49 | return ans; 50 | } 51 | } 52 | ``` 53 | -------------------------------------------------------------------------------- /level-1/lecture-021/print-increasing.md: -------------------------------------------------------------------------------- 1 | # Print Increasing 2 | 3 | >1. You are given a positive number n. 4 | >2. You are required to print the counting from 1 to n. 5 | >3. You are required to not use any loops. Complete the body of print Increasing function to achieve it. Don't change the signature of the function. 6 | ``` 7 | Note -> The online judge can't force you to write the function recursively but that is what the spirit of question is. Write recursive and not iterative logic. The purpose of the question is to aid learning recursion and not test you. 8 | Input Format 9 | A number n 10 | Output Format 11 | 1 12 | 2 13 | 3 14 | .. 15 | n 16 | Constraints 17 | 1 <= n <= 1000 18 | Sample Input 19 | 5 20 | Sample Output 21 | 1 22 | 2 23 | 3 24 | 4 25 | 5 26 | ``` 27 | 28 | ```java 29 | import java.io.*; 30 | import java.util.*; 31 | 32 | public class Main { 33 | 34 | public static void main(String[] args) throws Exception { 35 | Scanner sc = new Scanner(System.in); 36 | int n = sc.nextInt(); 37 | printIncreasing(n); 38 | } 39 | 40 | public static void printIncreasing(int n){ 41 | if(n == 0) { 42 | return; 43 | } 44 | printIncreasing(n-1); 45 | System.out.println(n); 46 | } 47 | 48 | } 49 | ``` -------------------------------------------------------------------------------- /level-1/lecture-002/primes-till-n.md: -------------------------------------------------------------------------------- 1 | # Print All Primes Till N 2 | 3 | > 1. You've to print all prime numbers between a range. 4 | > 2. Take as input "low", the lower limit of range. 5 | > 3. Take as input "high", the higher limit of range. 6 | > 4. For the range print all the primes numbers between low and high (both included). 7 | 8 | ``` 9 | Input Format 10 | low 11 | high 12 | Output Format 13 | n1 14 | n2 15 | .. all primes between low and high (both included) 16 | 17 | Constraints 18 | 2 <= low < high < 10 ^ 6 19 | Sample Input 20 | 6 21 | 24 22 | Sample Output 23 | 7 24 | 11 25 | 13 26 | 17 27 | 19 28 | 23 29 | ``` 30 | 31 | ## Solution 32 | 33 | ```java 34 | import java.util.Scanner; 35 | 36 | public class Main { 37 | public static void main(String[] args) { 38 | Scanner sc = new Scanner(System.in); 39 | int lo = sc.nextInt(), hi = sc.nextInt(); 40 | for (int i = lo; i <= hi; i++) { 41 | if (isPrime(i)) { 42 | System.out.println(i); 43 | } 44 | } 45 | } 46 | 47 | public static boolean isPrime(int num) { 48 | for (int i = 2; i * i <= num; i++) { 49 | if (num % i == 0) { 50 | return false; 51 | } 52 | } 53 | return true; 54 | } 55 | } 56 | ``` 57 | -------------------------------------------------------------------------------- /level-1/lecture-003/hw/gcd-lcm.md: -------------------------------------------------------------------------------- 1 | # Gcd And Lcm 2 | 3 | > 1. You are required to print the Greatest Common Divisor (GCD) of two numbers. 4 | > 2. You are also required to print the Lowest Common Multiple (LCM) of the same numbers. 5 | > 3. Take input "num1" and "num2" as the two numbers. 6 | > 4. Print their GCD and LCM. 7 | 8 | ``` 9 | Input Format 10 | num1 11 | num2 12 | .. the numbers whose GCD and LCM we have to find. 13 | Output Format 14 | a 15 | b 16 | .. where 'a' and 'b' are the GCD and LCM respectively. 17 | 18 | Constraints 19 | 2 <= n <= 10^9 20 | Sample Input 21 | 36 22 | 24 23 | Sample Output 24 | 12 25 | 72 26 | ``` 27 | 28 | ## Solution 29 | 30 | ```java 31 | import java.util.Scanner; 32 | 33 | public class GcdLcm { 34 | public static void main(String[] args) { 35 | Scanner sc = new Scanner(System.in); 36 | int n1 = sc.nextInt(); 37 | int n2 = sc.nextInt(); 38 | int og1 = n1; 39 | int og2 = n2; 40 | 41 | while (n1 % n2 != 0) { 42 | int rem = n1 % n2; 43 | n1 = n2; 44 | n2 = rem; 45 | } 46 | 47 | int gcd = n2; 48 | int lcm = (og1 * og2) / gcd; 49 | 50 | System.out.println(gcd); 51 | System.out.println(lcm); 52 | sc.close(); 53 | } 54 | } 55 | ``` 56 | -------------------------------------------------------------------------------- /level-1/lecture-001/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | int i = 0; 6 | // 1 to 10 7 | // while (i <= 10) { 8 | // System.out.println(i); 9 | // i = i + 1; 10 | // } 11 | 12 | // 11 to 20 13 | // for (i = 11; i <= 20; i++) { 14 | // System.out.println(i); 15 | // } 16 | 17 | // print number in dec order 18 | // for (i = 10; i > 0; i--) { 19 | // System.out.println(i); 20 | // } 21 | 22 | // table of 2 23 | // for (i = 2; i <= 20; i += 2) { 24 | // System.out.println(i); 25 | // } 26 | 27 | // to print table 28 | // int tableNumber = 13; 29 | // for (int num = 1; num <= 10; num++) { 30 | // System.out.println(tableNumber + " x " + num + " = " + (tableNumber * num)); 31 | // } 32 | 33 | // Primes 34 | Scanner sc = new Scanner(System.in); 35 | boolean isPrime = true; 36 | int num = sc.nextInt(); 37 | for (i = 2; i < num; i++) { 38 | if (num % i == 0) { 39 | isPrime = false; 40 | break; 41 | } 42 | } 43 | System.out.println(isPrime); 44 | 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /level-1/lecture-022/factorial-recursion.md: -------------------------------------------------------------------------------- 1 | # Factorial 2 | 3 | > 1. You are given a number n. 4 | > 2. You are required to calculate the factorial of the number. Don't change the signature of factorial function. 5 | > `Note -> The online judge can't force you to write the function recursively but that is what the spirit of question is.Write recursive and not iterative logic. The purpose of the question is to aid learning recursion and not test you.` 6 | 7 | ``` 8 | Input Format 9 | A number n 10 | Output Format 11 | factorial of n 12 | 13 | Constraints 14 | 0 <= n <= 10 15 | Sample Input 16 | 5 17 | 18 | Sample Output 19 | 120 20 | ``` 21 | 22 | ## Notes 23 | 24 | ![Screenshot 2021-12-13 at 11 25 30 PM](https://user-images.githubusercontent.com/28717686/145863641-0b967e69-2f42-4652-9d78-629dca80b256.png) 25 | 26 | ## Solution 27 | 28 | ```java 29 | import java.io.*; 30 | import java.util.*; 31 | 32 | public class Main { 33 | 34 | public static void main(String[] args) throws Exception { 35 | Scanner sc = new Scanner(System.in); 36 | int n = sc.nextInt(); 37 | System.out.println(factorial(n)); 38 | } 39 | 40 | public static int factorial(int n){ 41 | if(n == 0) { 42 | return 1; 43 | } 44 | return n*factorial(n-1); 45 | } 46 | 47 | } 48 | ``` 49 | -------------------------------------------------------------------------------- /level-1/lecture-003/benjamin-bulbs.md: -------------------------------------------------------------------------------- 1 | # The Curious Case Of Benjamin Bulbs 2 | 3 | > 1. You are given n number of bulbs. They are all switched off. A weird fluctuation in voltage hits the circuit n times. In the 1st fluctuation all bulbs are toggled, in the 2nd fluctuation every 2nd bulb is toggled, in the 3rd fluctuation every 3rd bulb is toggled and so on. You've to find which bulbs will be switched on after n fluctuations. 4 | > 2. Take as input a number n, representing the number of bulbs. 5 | > 3. Print all the bulbs that will be on after the nth fluctuation in voltage. 6 | 7 | ``` 8 | Input Format 9 | n, an integer 10 | Output Format 11 | b1 b2 b3 b4 .. all bulbs that will be on after nth wave 12 | 13 | Constraints 14 | 2 <= n < 10^9 15 | Sample Input 16 | 6 17 | Sample Output 18 | 1 19 | 4 20 | ``` 21 | 22 | ## Solution 23 | 24 | ```java 25 | import java.util.Scanner; 26 | 27 | public class BenjaminBulbs { 28 | public static void main(String[] args) { 29 | Scanner sc = new Scanner(System.in); 30 | int n = sc.nextInt(); 31 | benjaminBulbs(n); 32 | sc.close(); 33 | } 34 | 35 | public static void benjaminBulbs(int n) { 36 | int i = 1; 37 | while (i * i <= n) { 38 | System.out.println(i * i); 39 | i++; 40 | } 41 | } 42 | } 43 | ``` 44 | -------------------------------------------------------------------------------- /level-1/lecture-031/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Main { 4 | // Recursion and Backtracking 5 | // ! Agenda : 6 | // Flood Fill Algorithm 7 | 8 | // asf -> answer so far 9 | public static void floodfill(int[][] maze, int sr, int sc, String asf, boolean[][] vis) { 10 | if (sr < 0 || sr >= maze.length || sc < 0 || sc >= maze[0].length) { 11 | return; 12 | } 13 | if (vis[sr][sc]) { 14 | return; 15 | } 16 | if (maze[sr][sc] == 0) { 17 | return; 18 | } 19 | vis[sr][sc] = true; 20 | asf += maze[sr][sc]; 21 | floodfill(maze, sr - 1, sc, asf + "t", vis); 22 | floodfill(maze, sr, sc - 1, asf + "l", vis); 23 | floodfill(maze, sr + 1, sc, asf + "d", vis); 24 | floodfill(maze, sr, sc + 1, asf + "r", vis); 25 | 26 | } 27 | 28 | public static void main(String[] args) { 29 | Scanner scn = new Scanner(System.in); 30 | int n = scn.nextInt(); 31 | int m = scn.nextInt(); 32 | int[][] arr = new int[n][m]; 33 | for (int i = 0; i < n; i++) { 34 | for (int j = 0; j < m; j++) { 35 | arr[i][j] = scn.nextInt(); 36 | } 37 | } 38 | floodfill(arr, 0, 0, ""); 39 | scn.close(); 40 | } 41 | 42 | } -------------------------------------------------------------------------------- /level-1/lecture-004/pattern-5.md: -------------------------------------------------------------------------------- 1 | # Pattern 5 2 | 3 | > 1. You are given a number n. 4 | > 2. You've to create a pattern of \* and separated by tab as shown in output format. 5 | 6 | ``` 7 | Input Format 8 | A number n 9 | Output Format 10 | * 11 | * * * 12 | * * * * * 13 | * * * 14 | * 15 | 16 | Constraints 17 | 1 <= n <= 100 18 | Also, n is odd. 19 | Sample Input 20 | 5 21 | Sample Output 22 | * 23 | * * * 24 | * * * * * 25 | * * * 26 | * 27 | ``` 28 | 29 | ## Solution 30 | 31 | ```java 32 | import java.util.*; 33 | 34 | public class Main { 35 | 36 | public static void main(String[] args) { 37 | Scanner scn = new Scanner(System.in); 38 | int n = scn.nextInt(); 39 | int nsp = n/2, nst = 1; 40 | for(int r = 1; r <= n; r++) { 41 | // spaces 42 | for(int csp = 1; csp <= nsp; csp++) { 43 | System.out.print("\t"); 44 | } 45 | // stars 46 | for(int cst = 1; cst <= nst; cst++) { 47 | System.out.print("*\t"); 48 | } 49 | if(r <= n/2) { 50 | nsp--; 51 | nst += 2; 52 | } 53 | else { 54 | nsp++; 55 | nst -= 2; 56 | } 57 | System.out.println(); 58 | } 59 | 60 | } 61 | } 62 | ``` 63 | -------------------------------------------------------------------------------- /level-1/lecture-002/hw/fibo-till-n.md: -------------------------------------------------------------------------------- 1 | # Print Fibonacci Numbers Till N 2 | 3 | > 1. You've to print first n fibonacci numbers. 4 | > 2. Take as input "n", the count of fibonacci numbers to print. 5 | > 3. Print first n fibonacci numbers. 6 | 7 | ``` 8 | Input Format 9 | n 10 | Output Format 11 | 0 12 | 1 13 | 1 14 | 2 15 | 3 16 | 5 17 | 8 18 | .. first n fibonaccis 19 | 20 | Constraints 21 | 1 <= n < 40 22 | Sample Input 23 | 10 24 | Sample Output 25 | 0 26 | 1 27 | 1 28 | 2 29 | 3 30 | 5 31 | 8 32 | 13 33 | 21 34 | 34 35 | ``` 36 | 37 | ## Solution 38 | 39 | ```java 40 | import java.util.*; 41 | 42 | // 1. You've to print first n fibonacci numbers. 43 | // 2. Take as input "n", the count of fibonacci numbers to print. 44 | // 3. Print first n fibonacci numbers. 45 | public class Main { 46 | public static void main(String[] args) { 47 | Scanner sc = new Scanner(System.in); 48 | int n = sc.nextInt(); 49 | fibo(n); 50 | sc.close(); 51 | } 52 | 53 | public static void fibo(int n) { 54 | int f1 = 0, f2 = 1, i; 55 | if (n < 1) 56 | return; 57 | System.out.println(f1); 58 | for (i = 0; i < n - 1; i++) { 59 | System.out.println(f2); 60 | int next = f1 + f2; 61 | f1 = f2; 62 | f2 = next; 63 | } 64 | 65 | } 66 | } 67 | ``` 68 | -------------------------------------------------------------------------------- /level-1/lecture-043/coin-change-combination.md: -------------------------------------------------------------------------------- 1 | # Coin Change Combination 2 | 3 | `Easy` 4 | 5 | > 1. You are given a number n, representing the count of coins. 6 | > 2. You are given n numbers, representing the denominations of n coins. 7 | > 3. You are given a number "amt". 8 | > 4. You are required to calculate and print the number of combinations of the n coins using which the amount "amt" can be paid. 9 | > Note1 -> You have an infinite supply of each coin denomination i.e. same coin denomination can be used for many installments in payment of "amt" 10 | > Note2 -> You are required to find the count of combinations and not permutations i.e. 11 | > 2 + 2 + 3 = 7 and 2 + 3 + 2 = 7 and 3 + 2 + 2 = 7 are different permutations of same 12 | > combination. You should treat them as 1 and not 3. 13 | 14 | ## Input Format 15 | 16 | > A number n 17 | > n1 18 | > n2 19 | > .. n number of elements 20 | > A number amt 21 | 22 | ## Output Format 23 | 24 | > A number representing the count of combinations of coins which can be used to pay the amount "amt" 25 | 26 | ## Constraints 27 | 28 | > 1 <= n <= 30 29 | > 0 <= n1, n2, .. n elements <= 20 30 | > 0 <= amt <= 50 31 | 32 | ## Sample Input 33 | 34 | ``` 35 | 4 36 | 2 37 | 3 38 | 5 39 | 6 40 | 7 41 | ``` 42 | 43 | ## Sample Output 44 | 45 | `2` 46 | 47 | ## Solution 48 | 49 | ```java 50 | 51 | ``` 52 | -------------------------------------------------------------------------------- /level-1/lecture-020/toggle-case.md: -------------------------------------------------------------------------------- 1 | # Toggle Case 2 | 3 | > 1. You are given a string that contains only lowercase and uppercase alphabets. 4 | > 2. You have to toggle the case of every character of the given string. 5 | 6 | ``` 7 | Input Format 8 | A String 9 | Output Format 10 | A String 11 | Constraints 12 | 1 <= length of string <= 1000 13 | Sample Input 14 | pepCODinG 15 | Sample Output 16 | PEPcodINg 17 | ``` 18 | 19 | ## Solution 20 | 21 | ````java 22 | import java.io.*; 23 | import java.util.*; 24 | 25 | public class Main { 26 | 27 | public static String toggleCase(String str) { 28 | StringBuilder ans = new StringBuilder(); 29 | for (int i = 0; i < str.length(); i++) { 30 | char ch = str.charAt(i); 31 | // CH = ch - 'a' + 'A' 32 | if (ch >= 'A' && ch <= 'Z') { 33 | char lc = (char) (ch - 'A' + 'a'); // to convert to lower case 34 | ans.append(lc); 35 | } else { 36 | char uc = (char) (ch - 'a' + 'A'); // to convert to upper case 37 | ans.append(uc); 38 | } 39 | } 40 | return ans.toString(); 41 | } 42 | 43 | public static void main(String[] args) { 44 | Scanner scn = new Scanner(System.in); 45 | String str = scn.next(); 46 | System.out.println(toggleCase(str)); 47 | } 48 | 49 | }``` 50 | ```` 51 | -------------------------------------------------------------------------------- /level-1/lecture-013/subarray.md: -------------------------------------------------------------------------------- 1 | # Subarray 2 | 3 | > 1. You are given an array of size 'n' and n elements of the same array. 4 | > 2. You are required to find and print all the subarrays of the given array. 5 | > 3. Each subarray should be space seperated and on a seperate lines. Refer to sample input and output. 6 | 7 | ```text 8 | Input Format 9 | A number n 10 | n1 11 | n2 12 | .. n number of elements 13 | Output Format 14 | [Tab separated elements of subarray] 15 | .. 16 | All subarrays 17 | ``` 18 | 19 | ```java 20 | import java.io.*; 21 | import java.util.*; 22 | 23 | public class Main{ 24 | 25 | public static void main(String[] args) throws Exception { 26 | // write your code here 27 | Scanner sc = new Scanner(System.in); 28 | int n = sc.nextInt(); 29 | int[] arr = new int[n]; 30 | for (int i = 0; i < n; i++) { 31 | arr[i] = sc.nextInt(); 32 | } 33 | subarray(arr); 34 | sc.close(); 35 | } 36 | public static void subarray(int[] arr) { 37 | for (int start = 0; start < arr.length; start++) { 38 | for (int end = start; end < arr.length; end++) { 39 | for (int i = start; i <= end; i++) { 40 | System.out.print(arr[i] + "\t"); 41 | } 42 | System.out.println(); 43 | } 44 | } 45 | } 46 | 47 | } 48 | ``` 49 | -------------------------------------------------------------------------------- /level-1/lecture-021/print-increasing-decreasing.md: -------------------------------------------------------------------------------- 1 | # Print Increasing Decreasing 2 | 3 | > 1. You are given a positive number n. 4 | > 2. You are required to print the counting from n to 1 and back to n again. 5 | > 3. You are required to not use any loops. Complete the body of pdi function to achieve it. Don't change the signature of the function. 6 | `Note -> The online judge can't force you to write the function recursively but that is what the spirit of question is.Write recursive and not iterative logic. The purpose of the question is to aid learning recursion and not test you.` 7 | ``` 8 | Input Format 9 | A number n 10 | Output Format 11 | n 12 | n - 1 13 | n - 2 14 | .. 15 | 1 16 | 1 17 | 2 18 | 3 19 | .. 20 | n 21 | 22 | Constraints 23 | 1 <= n <= 1000 24 | Sample Input 25 | 3 26 | Sample Output 27 | 3 28 | 2 29 | 1 30 | 1 31 | 2 32 | 3 33 | ``` 34 | 35 | 36 | ```java 37 | import java.io.*; 38 | import java.util.*; 39 | 40 | public class Main { 41 | 42 | public static void main(String[] args) throws Exception { 43 | Scanner sc = new Scanner(System.in); 44 | int n = sc.nextInt(); 45 | printID(n); 46 | } 47 | 48 | public static void printID(int n){ 49 | if(n == 0) { 50 | return; 51 | } 52 | System.out.println(n); 53 | printID(n-1); 54 | System.out.println(n); 55 | } 56 | 57 | } 58 | ``` -------------------------------------------------------------------------------- /level-1/lecture-003/pythagorian-tripplet.md: -------------------------------------------------------------------------------- 1 | # Pythagorean Triplet 2 | 3 | > 1. You are required to check if a given set of numbers is a valid pythagorean triplet. 4 | > 2. Take as input three numbers a, b and c. 5 | > 3. Print true if they can form a pythagorean triplet and false otherwise. 6 | 7 | ``` 8 | Input Format 9 | a, an integer 10 | b, an integer 11 | c, an integer 12 | Output Format 13 | true if the numbers form a pythagorean triplet and false otherwise 14 | 15 | Constraints 16 | 1 <= a <= 10^9 17 | 1 <= b <= 10^9 18 | 1 <= c <= 10^9 19 | Sample Input 20 | 5 3 4 21 | Sample Output 22 | true 23 | ``` 24 | 25 | ## Solution 26 | 27 | ```java 28 | import java.util.*; 29 | 30 | // print if given tripplet is a pythagorean triplet 31 | public class PythagoreanTriplets { 32 | public static void main(String[] args) { 33 | Scanner sc = new Scanner(System.in); 34 | int a = sc.nextInt(); 35 | int b = sc.nextInt(); 36 | int c = sc.nextInt(); 37 | isPythagoreanTriplets(a, b, c); 38 | sc.close(); 39 | } 40 | 41 | public static void isPythagoreanTriplets(int a, int b, int c) { 42 | if (a * a + b * b == c * c || a * a + c * c == b * b || b * b + c * c == a * a) { 43 | System.out.println("true"); 44 | } else { 45 | System.out.println("flase"); 46 | } 47 | } 48 | 49 | } 50 | ``` 51 | -------------------------------------------------------------------------------- /level-1/lecture-007/anybase-addition.md: -------------------------------------------------------------------------------- 1 | # Any Base Addition 2 | 3 | > 1. You are given a base b. 4 | > 2. You are given two numbers n1 and n2 of base b. 5 | > 3. You are required to add the two numbes and print their value in base b. 6 | 7 | ``` 8 | Input Format 9 | A base b 10 | A number n1 11 | A number n2 12 | Output Format 13 | A number representing the sum of n1 and n2 in base b. 14 | 15 | Constraints 16 | 2 <= b <= 10 17 | 0 <= n1 <= 256 18 | 0 <= n2 <= 256 19 | Sample Input 20 | 8 21 | 777 22 | 1 23 | Sample Output 24 | 1000 25 | ``` 26 | 27 | ## Solution 28 | 29 | ```java 30 | import java.util.*; 31 | 32 | public class Main{ 33 | 34 | public static void main(String[] args) { 35 | Scanner scn = new Scanner(System.in); 36 | int b = scn.nextInt(); 37 | int n1 = scn.nextInt(); 38 | int n2 = scn.nextInt(); 39 | 40 | int d = anyAddition(n1, n2, b); 41 | System.out.println(d); 42 | } 43 | 44 | public static int anyAddition(int n, int m, int b) { 45 | int ans = 0, carry = 0, pow = 1; 46 | while (n != 0 || m != 0 || carry != 0) { 47 | int sum = n % 10 + m % 10 + carry; 48 | n /= 10; 49 | m /= 10; 50 | carry = sum / b; 51 | sum %= b; 52 | ans += sum * pow; 53 | pow *= 10; 54 | } 55 | return ans; 56 | } 57 | } 58 | ``` 59 | -------------------------------------------------------------------------------- /level-1/lecture-053/merge2sorted/Main.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Main { 5 | 6 | public static int[] mergeTwoSortedArrays(int[] a, int[] b){ 7 | int n = a.length; 8 | int m = b.length; 9 | 10 | int [] c = new int[n+m]; 11 | 12 | 13 | int i = 0, j = 0, k = 0; 14 | 15 | while(i < n && j < m){ 16 | if(a[i] < b[j]){ 17 | c[k] = a[i]; 18 | i++; 19 | k++; 20 | } 21 | else{ 22 | // b[j] < a[i] 23 | c[k] = b[j]; 24 | j++; 25 | k++; 26 | } 27 | } 28 | 29 | while(i < n){ 30 | c[k] = a[i]; 31 | k++; 32 | i++; 33 | } 34 | 35 | while(j < m){ 36 | c[k] = b[j]; 37 | j++; 38 | k++; 39 | } 40 | 41 | return c; 42 | } 43 | 44 | public static void print(int[] arr){ 45 | for(int i = 0 ; i < arr.length; i++){ 46 | System.out.println(arr[i]); 47 | } 48 | } 49 | public static void main(String[] args){ 50 | Scanner scn = new Scanner(System.in); 51 | int n = scn.nextInt(); 52 | int[] a = new int[n]; 53 | for(int i = 0 ; i < n; i++){ 54 | a[i] = scn.nextInt(); 55 | } 56 | int m = scn.nextInt(); 57 | int[] b = new int[m]; 58 | for(int i = 0 ; i < m; i++){ 59 | b[i] = scn.nextInt(); 60 | } 61 | int[] mergedArray = mergeTwoSortedArrays(a,b); 62 | print(mergedArray); 63 | } 64 | 65 | } -------------------------------------------------------------------------------- /level-1/lecture-022/print-decreasing.md: -------------------------------------------------------------------------------- 1 | # Print Decreasing 2 | 3 | > 1. You are given a positive number n. 4 | > 2. You are required to print the counting from n to 1. 5 | > 3. You are required to not use any loops. Complete the body of print Decreasing function to achieve it. 6 | 7 | > `Note -> The online judge can't force you to write the function recursively but that is what the spirit of question is. Write recursive and not iterative logic. The purpose of the question is to aid learning recursion and not test you.` 8 | 9 | ``` 10 | Input Format 11 | A number n 12 | Output Format 13 | n 14 | n - 1 15 | n - 2 16 | .. 17 | 1 18 | 19 | Constraints 20 | 1 <= n <= 1000 21 | Sample Input 22 | 5 23 | Sample Output 24 | 5 25 | 4 26 | 3 27 | 2 28 | 1 29 | ``` 30 | ## Notes 31 | 32 | ![Screenshot 2022-01-02 at 3 57 42 PM](https://user-images.githubusercontent.com/28717686/147873005-f8262231-ba52-4c8d-b371-1abf8d7d4816.png) 33 | 34 | ## Solution 35 | 36 | ```java 37 | import java.io.*; 38 | import java.util.*; 39 | 40 | public class Main { 41 | 42 | public static void main(String[] args) throws Exception { 43 | Scanner sc = new Scanner(System.in); 44 | int n = sc.nextInt(); 45 | printDecreasing(n); 46 | } 47 | 48 | public static void printDecreasing(int n){ 49 | if(n == 0) { 50 | return; 51 | } 52 | System.out.println(n); 53 | printDecreasing(n-1); 54 | } 55 | 56 | } 57 | ``` 58 | -------------------------------------------------------------------------------- /level-1/lecture-002/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Main { 4 | public static Scanner sc = new Scanner(System.in); 5 | 6 | public static void main(String[] args) { 7 | // int add = add(1, 2); 8 | // System.out.println(add); 9 | // int sub = subtract(2, 1); 10 | // System.out.println(sub); 11 | int num = sc.nextInt(); 12 | printTable(num); 13 | } 14 | 15 | // no args no return 16 | public static void printMessage() { 17 | System.out.println("Hello World"); 18 | } 19 | 20 | public static void printTable(int num) { 21 | for (int i = 1; i <= 10; i++) { 22 | System.out.println(num + " * " + i + " = " + (num * i)); 23 | } 24 | } 25 | 26 | // no return 2 args 27 | public static void printTabaleInRange(int start, int end) { 28 | for (int i = start; i <= end; i++) { 29 | printTable(i); 30 | } 31 | } 32 | 33 | // with RT and with ARG 34 | public static int add(int a, int b) { 35 | return a + b; 36 | } 37 | 38 | // with RT and with ARG 39 | public static int subtract(int a, int b) { 40 | return a - b; 41 | } 42 | 43 | public static boolean isPrime(int num) { 44 | for (int i = 2; i < num; i++) { 45 | if (num % i == 0) { 46 | return false; 47 | } 48 | } 49 | return true; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /level-1/archive/leetcode/206-reverse-linked-list.md: -------------------------------------------------------------------------------- 1 | # 206. [Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/) 2 | 3 | `Easy` 4 | 5 | > Given the head of a singly linked list, reverse the list, and return the reversed list. 6 | 7 | ## Example 1: 8 | 9 | ``` 10 | Input: head = [1,2,3,4,5] 11 | Output: [5,4,3,2,1] 12 | ``` 13 | 14 | ## Example 2: 15 | 16 | ``` 17 | Input: head = [1,2] 18 | Output: [2,1] 19 | ``` 20 | 21 | ## Example 3: 22 | 23 | ``` 24 | Input: head = [] 25 | Output: [] 26 | ``` 27 | 28 | ## Constraints: 29 | 30 | ``` 31 | The number of nodes in the list is the range [0, 5000]. 32 | -5000 <= Node.val <= 5000 33 | ``` 34 | 35 | > Follow up: A linked list can be reversed either iteratively or recursively. Could you implement both? 36 | 37 | ## Solution (Java) 38 | 39 | ```java 40 | /** 41 | * Definition for singly-linked list. 42 | * public class ListNode { 43 | * int val; 44 | * ListNode next; 45 | * ListNode() {} 46 | * ListNode(int val) { this.val = val; } 47 | * ListNode(int val, ListNode next) { this.val = val; this.next = next; } 48 | * } 49 | */ 50 | class Solution { 51 | public ListNode reverseList(ListNode head) { 52 | ListNode prev = null; 53 | while(head != null) { 54 | ListNode next = head.next; 55 | head.next = prev; 56 | prev = head; 57 | head = next; 58 | } 59 | return prev; 60 | } 61 | } 62 | ``` 63 | -------------------------------------------------------------------------------- /level-1/lecture-022/print-increasing.md: -------------------------------------------------------------------------------- 1 | # Print Increasing 2 | 3 | > 1. You are given a positive number n. 4 | > 2. You are required to print the counting from 1 to n. 5 | > 3. You are required to not use any loops. Complete the body of print Increasing function to achieve it. Don't change the signature of the function. 6 | 7 | > `Note -> The online judge can't force you to write the function recursively but that is what the spirit of question is. Write recursive and not iterative logic. The purpose of the question is to aid learning recursion and not test you.` 8 | 9 | ``` 10 | Input Format 11 | A number n 12 | Output Format 13 | 1 14 | 2 15 | 3 16 | .. 17 | n 18 | 19 | Constraints 20 | 1 <= n <= 1000 21 | Sample Input 22 | 5 23 | Sample Output 24 | 1 25 | 2 26 | 3 27 | 4 28 | 5 29 | ``` 30 | 31 | ## Notes 32 | 33 | ![Screenshot 2021-12-13 at 10 34 07 PM](https://user-images.githubusercontent.com/28717686/145856456-057cced8-c91e-4ce3-bcca-8c61a6670089.png) 34 | 35 | ## Solution 36 | 37 | ```java 38 | import java.io.*; 39 | import java.util.*; 40 | 41 | public class Main { 42 | 43 | public static void main(String[] args) throws Exception { 44 | Scanner sc = new Scanner(System.in); 45 | int n = sc.nextInt(); 46 | printIncreasing(n); 47 | } 48 | 49 | public static void printIncreasing(int n){ 50 | if(n == 0) { 51 | return; 52 | } 53 | printIncreasing(n-1); 54 | System.out.println(n); 55 | } 56 | 57 | } 58 | ``` 59 | -------------------------------------------------------------------------------- /level-1/lecture-014/binary-search.md: -------------------------------------------------------------------------------- 1 | # Binary Search 2 | 3 | > Binary search is a search algorithm that works by comparing the target value to the middle element of a sorted array. If the target value is greater than the middle element, the search continues on the right half of the array; if the target value is less than the middle element, the search continues on the left half of the array. If the value is found, the algorithm stops. If the array can’t be divided evenly into two halves, the value is not found. 4 | 5 | ```java 6 | import java.util.Scanner; 7 | 8 | public class Main { 9 | 10 | public static int binarySearch(int[] arr, int target) { 11 | int start = 0, end = arr.length - 1; 12 | while (start < end) { 13 | int mid = start + (end - start) / 2; 14 | if (target == arr[mid]) { 15 | return mid; 16 | } else if (target > arr[mid]) { 17 | start = mid + 1; 18 | } else { 19 | end = mid - 1; 20 | } 21 | } 22 | return -1; 23 | } 24 | 25 | public static void main(String[] args) { 26 | Scanner sc = new Scanner(System.in); 27 | int n = sc.nextInt(); 28 | int[] arr = new int[n]; 29 | for (int i = 0; i < n; i++) { 30 | arr[i] = sc.nextInt(); 31 | } 32 | int target = sc.nextInt(); 33 | System.out.println(binarySearch(arr, target)); 34 | sc.close(); 35 | } 36 | } 37 | 38 | ``` 39 | -------------------------------------------------------------------------------- /level-1/lecture-052/bubbleSort/Main.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Main { 5 | 6 | public static void bubbleSort(int[] arr) { 7 | int n = arr.length; 8 | //iterations 9 | for(int x = 1; x <= n-1; x++){ 10 | for(int i = 0; i <= n-x-1; i++){ 11 | if(isSmaller(arr, i+1, i) == true){ 12 | swap(arr, i+1, i); 13 | } 14 | } 15 | } 16 | 17 | 18 | } 19 | 20 | // used for swapping ith and jth elements of array 21 | public static void swap(int[] arr, int i, int j) { 22 | System.out.println("Swapping " + arr[i] + " and " + arr[j]); 23 | int temp = arr[i]; 24 | arr[i] = arr[j]; 25 | arr[j] = temp; 26 | } 27 | 28 | // return true if ith element is smaller than jth element 29 | public static boolean isSmaller(int[] arr, int i, int j) { 30 | System.out.println("Comparing " + arr[i] + " and " + arr[j]); 31 | if (arr[i] < arr[j]) { 32 | return true; 33 | } else { 34 | return false; 35 | } 36 | } 37 | 38 | public static void print(int[] arr) { 39 | for (int i = 0; i < arr.length; i++) { 40 | System.out.println(arr[i]); 41 | } 42 | } 43 | 44 | public static void main(String[] args) throws Exception { 45 | Scanner scn = new Scanner(System.in); 46 | int n = scn.nextInt(); 47 | int[] arr = new int[n]; 48 | for (int i = 0; i < n; i++) { 49 | arr[i] = scn.nextInt(); 50 | } 51 | bubbleSort(arr); 52 | print(arr); 53 | } 54 | 55 | } -------------------------------------------------------------------------------- /level-1/lecture-002/is-number-prime.md: -------------------------------------------------------------------------------- 1 | # Is A Number Prime 2 | 3 | > 1. You've to check whether a given number is prime or not. 4 | > 2. Take a number "t" as input representing count of input numbers to be tested. 5 | > 3. Take a number "n" as input "t" number of times. 6 | > 4. For each input value of n, print "prime" if the number is prime and "not prime" otherwise. 7 | 8 | ``` 9 | Input Format 10 | A number t 11 | A number n 12 | A number n 13 | .. t number of times 14 | Output Format 15 | prime 16 | not prime 17 | not prime 18 | .. t number of times 19 | 20 | Constraints 21 | 1 <= t <= 10000 22 | 2 <= n < 10^9 23 | Sample Input 24 | 5 25 | 13 26 | 2 27 | 3 28 | 4 29 | 5 30 | Sample Output 31 | prime 32 | prime 33 | prime 34 | not prime 35 | prime 36 | ``` 37 | 38 | ## Solution 39 | 40 | ```java 41 | import java.util.*; 42 | 43 | public class Main{ 44 | 45 | public static boolean isPrime(int n) { 46 | for(int i = 2; i*i <= n; i++) { 47 | if(n%i == 0) { 48 | return false; 49 | } 50 | } 51 | return true; 52 | } 53 | 54 | public static void main(String[] args) { 55 | Scanner scn = new Scanner(System.in); 56 | int t = scn.nextInt(); 57 | while(t != 0) { 58 | int n = scn.nextInt(); 59 | if(isPrime(n)) { 60 | System.out.println("prime"); 61 | } 62 | else { 63 | System.out.println("not prime"); 64 | } 65 | t--; 66 | } 67 | 68 | } 69 | } 70 | ``` 71 | -------------------------------------------------------------------------------- /level-1/lecture-010/find-element-in-array.md: -------------------------------------------------------------------------------- 1 | # Find Element In An Array 2 | 3 | > 1.You are given a number n, representing the size of array a. 4 | > 2.You are given n distinct numbers, representing elements of array a. 3. You are given another number d. 4. You are required to check if d number exists in the array a and at what index (0 based). If found print the index, otherwise print -1. 5 | 6 | ``` 7 | Input Format 8 | A number n 9 | n1 10 | n2 11 | .. n number of elements 12 | A number d 13 | Output Format 14 | A number representing index at which d is found in array a and -1 if not found 15 | 16 | Constraints 17 | 1 <= n <= 10^7 18 | -10^9 <= n1, n2 19 | .. n elements <= 10^9 20 | -10^9 <= d <= 10^9 21 | Sample Input 22 | 6 23 | 15 24 | 30 25 | 40 26 | 4 27 | 11 28 | 9 29 | 40 30 | Sample Output 31 | 2 32 | ``` 33 | 34 | ## Solution 35 | 36 | ```java 37 | import java.io.*; 38 | import java.util.*; 39 | 40 | public class Main{ 41 | 42 | public static int find(int[] a, int d) { 43 | for(int i = 0; i < a.length; i++){ 44 | if(d == a[i]) 45 | return i; 46 | } 47 | return -1; 48 | } 49 | 50 | public static void main(String[] args) throws Exception { 51 | Scanner sc = new Scanner(System.in); 52 | int n = sc.nextInt(); 53 | int[] a = new int[n]; 54 | for(int i = 0; i < a.length; i++) { 55 | a[i] = sc.nextInt(); 56 | } 57 | int d = sc.nextInt(); 58 | System.out.println(find(a, d)); 59 | } 60 | } 61 | ``` 62 | -------------------------------------------------------------------------------- /level-1/lecture-048/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | // import java.util.HashMap; 3 | 4 | public class Main { 5 | public static void main(String[] args) { 6 | // declare and initialise hashmap 7 | // HashMap hashMapName = new HashMap(); 9 | HashMap hm = new HashMap(); 10 | 11 | // add elements to hashmap 12 | hm.put("India", 628); 13 | hm.put("China", 837); 14 | hm.put("Dubai", 120); 15 | hm.put("India", 200); 16 | hm.put("Pak", 837); 17 | hm.put("USA", 443); 18 | 19 | // print hashmap 20 | // System.out.println(hm); // might not get in order due to hashmap 21 | 22 | // get value from hashmap 23 | System.out.println(hm.get("India")); 24 | 25 | // containsKey() method returns true if key is present in hashmap 26 | boolean isChinaPresent = hm.containsKey("China"); // returns true or false depending on key's presence 27 | boolean isBangladeshPresent = hm.containsKey("Bangladesh"); // returns false 28 | System.out.println(isChinaPresent); // true 29 | System.out.println(isBangladeshPresent); // false 30 | 31 | // keySet() method returns set of keys 32 | for (String key : hm.keySet()) { 33 | System.out.println(key); 34 | } 35 | 36 | // size 37 | System.out.println(hm.size()); // 5 : number of elements/entry in hashmap 38 | } 39 | } -------------------------------------------------------------------------------- /level-1/lecture-012/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int n1 = sc.nextInt(); 7 | int a1[] = new int[n1]; 8 | for (int i = 0; i < a1.length; i++) { 9 | a1[i] = sc.nextInt(); 10 | } 11 | int n2 = sc.nextInt(); 12 | int a2[] = new int[n2]; 13 | for (int i = 0; i < a2.length; i++) { 14 | a2[i] = sc.nextInt(); 15 | } 16 | sumOfTwoArrays(a1, a2); 17 | sc.close(); 18 | } 19 | 20 | public static void sumOfTwoArrays(int a1[], int a2[]) { 21 | int n1 = a1.length; 22 | int n2 = a2.length; 23 | int[] sum = new int[(n1 > n2) ? n1 : n2]; 24 | int c = 0; 25 | int i = a1.length - 1; // index of a1 26 | int j = a2.length - 1; // index of a2 27 | int k = sum.length - 1; // index of sum 28 | while (k >= 0) { 29 | int d = c; 30 | if (i >= 0) { 31 | d += a1[i]; 32 | } 33 | if (j >= 0) { 34 | d += a2[j]; 35 | } 36 | c = d / 10; 37 | d = d % 10; 38 | sum[k] = d; 39 | i--; 40 | j--; 41 | k--; 42 | } 43 | 44 | if (c != 0) { 45 | System.out.println(c); 46 | } 47 | for (int val : sum) { 48 | System.out.println(val); 49 | } 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /level-1/lecture-052/selectionSort/Main.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Main { 5 | 6 | public static void selectionSort(int[] arr) { 7 | int n = arr.length; 8 | for(int x = 0; x 1. You are given as input marks of a student. 4 | > 2. Display an appropriate message based on the following rules: 5 | > 2.1 for marks above 90, print excellent. 6 | > 2.2 for marks above 80 and less than equal to 90, print good. 7 | > 2.3 for marks above 70 and less than equal to 80, print fair. 8 | > 2.4 for marks above 60 and less than equal to 70, print meets expectations. 9 | > 2.5 for marks less than equal to 60, print below par. 10 | 11 | ``` 12 | Input Format 13 | Input is handled for you and provided as variable marks 14 | Output Format 15 | Appropriate message as per student's marks 16 | Question Video 17 | 18 | COMMENTConstraints 19 | No constraints 20 | Sample Input 21 | 92 22 | Sample Output 23 | excellent 24 | ``` 25 | 26 | ## Solution 27 | 28 | ```java 29 | import java.util.Scanner; 30 | 31 | public class Main { 32 | public static void main(String[] args) { 33 | Scanner sc = new Scanner(System.in); 34 | int marks = sc.nextInt(); 35 | 36 | if (marks > 90) { 37 | System.out.println("excellent"); 38 | } else if (marks > 80 && marks <= 90) { 39 | System.out.println("good"); 40 | } else if (marks > 70 && marks <= 80) { 41 | System.out.println("fair"); 42 | } else if (marks > 60 && marks <= 70) { 43 | System.out.println("meets expectations"); 44 | } else { 45 | System.out.println("below par"); 46 | } 47 | sc.close(); 48 | } 49 | } 50 | ``` 51 | -------------------------------------------------------------------------------- /level-1/lecture-020/string-with-difference-of-every-two-consecutive-characters.md: -------------------------------------------------------------------------------- 1 | # String With Difference Of Every Two Consecutive Characters 2 | 3 | > 1. You are given a string that contains only lowercase and uppercase alphabets. 4 | > 2. You have to form a string that contains the difference of ASCII values of every two consecutive characters between those characters. 5 | 6 | ``` 7 | For "abecd", the answer should be "a1b3e-2c1d", as 8 | 'b'-'a' = 1 9 | 'e'-'b' = 3 10 | 'c'-'e' = -2 11 | 'd'-'c' = 1 12 | 13 | Input Format 14 | A String 15 | Output Format 16 | A String 17 | 18 | Constraints 19 | 1 <= length of string <= 1000 20 | Sample Input 21 | pepCODinG 22 | Sample Output 23 | p-11e11p-45C12O-11D37i5n-39G 24 | ``` 25 | 26 | ## Solution 27 | 28 | ```java 29 | import java.io.*; 30 | import java.util.*; 31 | 32 | public class Main { 33 | 34 | public static String stringDiffrence(String s) { 35 | // String With Difference Of Every Two Consecutive Characters 36 | StringBuilder ans = new StringBuilder(); 37 | ans.append(s.charAt(0)); 38 | for (int i = 1; i < s.length(); i++) { 39 | int diff = (int) (s.charAt(i) - s.charAt(i - 1)); 40 | ans.append(diff); 41 | ans.append(s.charAt(i)); 42 | } 43 | return ans.toString(); 44 | } 45 | public static void main(String[] args) { 46 | Scanner sc = new Scanner(System.in); 47 | String s = sc.nextLine(); 48 | System.out.println(stringDiffrence(s)); 49 | sc.close(); 50 | } 51 | 52 | } 53 | ``` 54 | -------------------------------------------------------------------------------- /level-1/lecture-052/insertionSort/Main.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Main { 5 | 6 | public static void insertionSort(int[] arr) { 7 | int n = arr.length; 8 | for(int x = 1; x < n; x++){ 9 | for(int i = x-1; i >= 0; i--){ 10 | if(isGreater(arr, i, i+1))// arr[i] > arr[i+1]){ 11 | { 12 | swap(arr, i , i+1); 13 | } 14 | else{ 15 | break; 16 | } 17 | } 18 | } 19 | 20 | } 21 | 22 | // used for swapping ith and jth elements of array 23 | public static void swap(int[] arr, int i, int j) { 24 | System.out.println("Swapping " + arr[i] + " and " + arr[j]); 25 | int temp = arr[i]; 26 | arr[i] = arr[j]; 27 | arr[j] = temp; 28 | } 29 | 30 | // return true if jth element is greater than ith element 31 | public static boolean isGreater(int[] arr, int j, int i) { 32 | System.out.println("Comparing " + arr[i] + " and " + arr[j]); 33 | if (arr[i] < arr[j]) { 34 | return true; 35 | } else { 36 | return false; 37 | } 38 | } 39 | 40 | public static void print(int[] arr) { 41 | for (int i = 0; i < arr.length; i++) { 42 | System.out.println(arr[i]); 43 | } 44 | } 45 | 46 | public static void main(String[] args) throws Exception { 47 | Scanner scn = new Scanner(System.in); 48 | int n = scn.nextInt(); 49 | int[] arr = new int[n]; 50 | for (int i = 0; i < n; i++) { 51 | arr[i] = scn.nextInt(); 52 | } 53 | insertionSort(arr); 54 | print(arr); 55 | } 56 | 57 | } -------------------------------------------------------------------------------- /level-1/lecture-022/print-decreasing-increasing.md: -------------------------------------------------------------------------------- 1 | # Print Increasing Decreasing 2 | 3 | > 1. You are given a positive number n. 4 | > 2. You are required to print the counting from n to 1 and back to n again. 5 | > 3. You are required to not use any loops. Complete the body of pdi function to achieve it. Don't change the signature of the function. 6 | 7 | > `Note -> The online judge can't force you to write the function recursively but that is what the spirit of question is.Write recursive and not iterative logic. The purpose of the question is to aid learning recursion and not test you.` 8 | 9 | ``` 10 | Input Format 11 | A number n 12 | Output Format 13 | n 14 | n - 1 15 | n - 2 16 | .. 17 | 1 18 | 1 19 | 2 20 | 3 21 | .. 22 | n 23 | 24 | Constraints 25 | 1 <= n <= 1000 26 | Sample Input 27 | 3 28 | Sample Output 29 | 3 30 | 2 31 | 1 32 | 1 33 | 2 34 | 3 35 | ``` 36 | ## Notes 37 | 38 | ![Screenshot 2021-12-13 at 10 45 05 PM](https://user-images.githubusercontent.com/28717686/145858279-6287292a-d39f-4a25-bb89-20e37ec381e0.png) 39 | 40 | ## Solution 41 | 42 | ```java 43 | import java.io.*; 44 | import java.util.*; 45 | 46 | public class Main { 47 | 48 | public static void main(String[] args) throws Exception { 49 | Scanner sc = new Scanner(System.in); 50 | int n = sc.nextInt(); 51 | printDI(n); 52 | } 53 | 54 | public static void printDI(int n){ 55 | if(n == 0) { 56 | return; 57 | } 58 | System.out.println(n); 59 | printDI(n-1); 60 | System.out.println(n); 61 | } 62 | 63 | } 64 | ``` 65 | -------------------------------------------------------------------------------- /level-1/lecture-002/hw/digits-of-a-number.md: -------------------------------------------------------------------------------- 1 | # Digits Of A Number 2 | 3 | > 1. You've to display the digits of a number. 4 | > 2. Take as input "n", the number for which digits have to be displayed. 5 | > 3. Print the digits of the number line-wise. 6 | 7 | ``` 8 | Input Format 9 | "n" where n is any integer. 10 | Output Format 11 | d1 12 | d2 13 | d3 14 | ... digits of the number 15 | 16 | Constraints 17 | 1 <= n < 10^9 18 | Sample Input 19 | 65784383 20 | Sample Output 21 | 6 22 | 5 23 | 7 24 | 8 25 | 4 26 | 3 27 | 8 28 | 3 29 | ``` 30 | 31 | ## Solution 32 | 33 | ```java 34 | import java.util.Scanner; 35 | 36 | // 1. You've to display the digits of a number. 37 | // 2. Take as input "n", the number for which digits have to be displayed. 38 | // 3. Print the digits of the number line-wise. 39 | public class Main { 40 | public static void digitsOfNumber(int num) { 41 | int len = countDigitOfNumber(num) - 1; 42 | while (len >= 0) { 43 | int digit = num / (int) Math.pow(10, len); 44 | num = num % (int) Math.pow(10, len); 45 | System.out.println(digit); 46 | len--; 47 | } 48 | } 49 | 50 | public static int countDigitOfNumber(int n) { 51 | int count = 0; 52 | while (n != 0) { 53 | count++; 54 | n /= 10; 55 | } 56 | return count; 57 | } 58 | 59 | public static void main(String[] args) { 60 | Scanner sc = new Scanner(System.in); 61 | int num = sc.nextInt(); 62 | digitsOfNumber(num); 63 | sc.close(); 64 | } 65 | } 66 | ``` 67 | -------------------------------------------------------------------------------- /level-1/lecture-021/remove-primes.md: -------------------------------------------------------------------------------- 1 | # Remove Primes 2 | 3 | > 1. You are given an ArrayList of positive integers. 4 | > 2. You have to remove prime numbers from the given ArrayList and return the updated ArrayList. 5 | > `Note -> The order of elements should remain same.` 6 | 7 | ``` 8 | Input Format 9 | A number N 10 | arr1 11 | arr2.. N numbers 12 | Output Format 13 | An Arraylist 14 | Constraints 15 | 1 <= N <= 10000 16 | Sample Input 17 | 4 18 | 3 12 13 15 19 | Sample Output 20 | [12, 15] 21 | ``` 22 | 23 | ## Solution 24 | 25 | ```java 26 | import java.io.*; 27 | import java.util.*; 28 | 29 | public class Main { 30 | 31 | public static boolean isPrime(int n) { 32 | 33 | for (int i = 2; i * i <= n; i++) { 34 | if (n % i == 0) { 35 | return false; 36 | } 37 | } 38 | return true; 39 | } 40 | // reverse traversing the list since we are removing elements and 41 | // we don't want to skip any elements 42 | // since it will change indices after removal 43 | public static void removePrimes(ArrayList al) { 44 | for (int i = al.size()-1; i >= 0; i--) { 45 | if (isPrime(al.get(i))) { 46 | al.remove(i); 47 | } 48 | } 49 | } 50 | public static void main(String[] args) { 51 | Scanner scn = new Scanner(System.in); 52 | int n = scn.nextInt(); 53 | ArrayList al = new ArrayList<>(); 54 | for(int i = 0 ; i < n; i++){ 55 | al.add(scn.nextInt()); 56 | } 57 | 58 | removePrimes(al); 59 | System.out.println(al); 60 | } 61 | 62 | } 63 | ``` 64 | -------------------------------------------------------------------------------- /level-1/lecture-030/print-permutations.md: -------------------------------------------------------------------------------- 1 | # Print Permutations 2 | 3 | > 1. You are given a string str. 4 | > 2. Complete the body of printPermutations function - without changing signature - to calculate and print all permutations of str. 5 | 6 | > `Note -> The online judge can't force you to write the function recursively but that is what the spirit of question is. Write recursive and not iterative logic. The purpose of the question is to aid learning recursion and not test you.` 7 | 8 | ``` 9 | Input Format 10 | A string str 11 | Output Format 12 | Permutations of str in order hinted by Sample output 13 | 14 | Constraints 15 | 0 <= str.length <= 7 16 | Sample Input 17 | abc 18 | Sample Output 19 | abc 20 | acb 21 | bac 22 | bca 23 | cab 24 | cba 25 | ``` 26 | 27 | ## Solution 28 | 29 | ```java 30 | import java.io.*; 31 | import java.util.*; 32 | 33 | public class Main { 34 | 35 | // Print Permutations 36 | public static void printPermutations(String str, String ans) { 37 | if (str.length() == 0) { 38 | System.out.println(ans); 39 | return; 40 | } 41 | for (int i = 0; i < str.length(); i++) { 42 | char ch = str.charAt(i); // get ch from str 43 | String ros = str.substring(0, i) + str.substring(i + 1); // exclude ch from str 44 | printPermutations(ros, ans + ch); 45 | } 46 | } 47 | 48 | public static void main(String[] args) { 49 | Scanner sc = new Scanner(System.in); 50 | String s = sc.next(); 51 | printPermutations(s, ""); 52 | sc.close(); 53 | } 54 | } 55 | ``` 56 | -------------------------------------------------------------------------------- /level-1/lecture-011/span-of-array.md: -------------------------------------------------------------------------------- 1 | # Span of an array 2 | 3 | > 1. You are given a number n, representing the count of elements. 4 | > 2. You are given n numbers. 5 | > 3. You are required to find the span of input. Span is defined as difference of maximum value and minimum value. 6 | 7 | ``` 8 | Input Format 9 | A number n 10 | n1 11 | n2 12 | .. n number of elements 13 | Output Format 14 | A number representing max - min 15 | 16 | Constraints 17 | 1 <= n <= 10^4 18 | 0 <= n1, n2 19 | .. n elements <= 10 ^9 20 | Sample Input 21 | 6 22 | 15 23 | 30 24 | 40 25 | 4 26 | 11 27 | 9 28 | Sample Output 29 | 36 30 | ``` 31 | 32 | ## Solution 33 | 34 | ```java 35 | import java.util.Scanner; 36 | 37 | public class Main { 38 | // span of array : max - min 39 | // diffrence of max and min 40 | public static int spanOfArray(int[] arr, int n) { 41 | int max = Integer.MIN_VALUE; // -ve infinity 42 | int min = Integer.MAX_VALUE; // +ve infinity 43 | for(int i = 0; i < n; i++) { 44 | if(arr[i] < min) { 45 | min = arr[i]; 46 | } 47 | if(arr[i] > max) { 48 | max = arr[i]; 49 | } 50 | } 51 | return (max - min); 52 | } 53 | 54 | public static void main(String[] args) { 55 | Scanner sc = new Scanner(System.in); 56 | int n = sc.nextInt(); 57 | int[] arr = new int[n]; 58 | for (int i = 0; i < n; i++) { 59 | arr[i] = sc.nextInt(); 60 | } 61 | System.out.println(spanOfArray(arr, n)); 62 | sc.close(); 63 | } 64 | 65 | } 66 | ```` 67 | -------------------------------------------------------------------------------- /level-1/lecture-024/display-array-in-reverse.md: -------------------------------------------------------------------------------- 1 | # Display Array In Reverse 2 | 3 | > 1. You are given a number n, representing the size of array a. 4 | > 2. You are given n numbers, representing elements of array a. 5 | > 3. You are required to print the elements of array from end to beginning each in a separate line. 6 | > 4. For the above purpose complete the body of displayArrReverse function. Don't change the signature. 7 | 8 | > `Note -> The online judge can't force you to write the function recursively but that is what the spirit of question is. Write recursive and not iterative logic. The purpose of the question is to aid learning recursion and not test you.` 9 | 10 | ``` 11 | Input Format 12 | A number n 13 | n1 14 | n2 15 | .. n number of elements 16 | Output Format 17 | n1 18 | n2 19 | .. n elements 20 | 21 | Constraints 22 | 1 <= n <= 30 23 | 0 <= n1, n2, .. n elements <= 10 24 | Sample Input 25 | 5 26 | 3 27 | 1 28 | 0 29 | 7 30 | 5 31 | Sample Output 32 | 5 33 | 7 34 | 0 35 | 1 36 | 3 37 | ``` 38 | 39 | 40 | ## Solution 41 | 42 | ```java 43 | import java.util.Scanner; 44 | 45 | public class Main { 46 | public static void main(String[] args) { 47 | Scanner sc = new Scanner(System.in); 48 | int n = sc.nextInt(); 49 | int[] a = new int[n]; 50 | for(int i = 0; i < a.length; i++) { 51 | a[i] = sc.nextInt(); 52 | } 53 | dar(a, 0); 54 | } 55 | public static void dar(int[] a, int i) { 56 | if(i == a.length) { 57 | return; 58 | } 59 | dar(a, i + 1); 60 | System.out.println(a[i]); 61 | } 62 | } 63 | ``` -------------------------------------------------------------------------------- /level-1/lecture-019/print-all-palindromic-substrings.md: -------------------------------------------------------------------------------- 1 | # Print All Palindromic Substrings 2 | 3 | > 1. You are given a string. 4 | > 2. You have to print all palindromic substrings of the given string. 5 | 6 | ``` 7 | Input Format 8 | A String 9 | Output Format 10 | All palindromic substrings(one in a line). 11 | First, all palindromic substrings starting from first character of string will be printed, then from second character and so on. 12 | Constraints 13 | 1 <= length of string <= 500 14 | Sample Input 15 | abcc 16 | Sample Output 17 | a 18 | b 19 | c 20 | cc 21 | c 22 | ``` 23 | 24 | ## Solution 25 | 26 | ```java 27 | import java.io.*; 28 | import java.util.*; 29 | 30 | public class Main { 31 | public static void printAllPalindromicSubstrings(String s) { 32 | for (int i = 0; i < s.length(); i++) { 33 | for (int j = i + 1; j <= s.length(); j++) { 34 | String sub = s.substring(i, j); 35 | if (isPalindrome(sub)) { 36 | System.out.println(sub); 37 | } 38 | } 39 | } 40 | } 41 | 42 | public static boolean isPalindrome(String s) { 43 | int i = 0; 44 | int j = s.length() - 1; 45 | while (i < j) { 46 | if (s.charAt(i) != s.charAt(j)) { 47 | return false; 48 | } 49 | i++; 50 | j--; 51 | } 52 | return true; 53 | } 54 | 55 | public static void main(String[] args) { 56 | Scanner scn = new Scanner(System.in); 57 | String str = scn.next(); 58 | printAllPalindromicSubstrings(str); 59 | } 60 | 61 | } 62 | ``` 63 | -------------------------------------------------------------------------------- /level-1/lecture-052/Main.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Main { 5 | 6 | public static void bubbleSort(int[] arr) { 7 | int n = arr.length; 8 | // iterations 9 | for (int x = 1; x <= n - 1; x++) { 10 | for (int i = 0; i <= n - x - 1; i++) { 11 | if (isSmaller(arr, i + 1, i) == true) { 12 | swap(arr, i + 1, i); 13 | } 14 | } 15 | } 16 | 17 | } 18 | 19 | // used for swapping ith and jth elements of array 20 | public static void swap(int[] arr, int i, int j) { 21 | System.out.println("Swapping " + arr[i] + " and " + arr[j]); 22 | int temp = arr[i]; 23 | arr[i] = arr[j]; 24 | arr[j] = temp; 25 | } 26 | 27 | // return true if ith element is smaller than jth element 28 | public static boolean isSmaller(int[] arr, int i, int j) { 29 | System.out.println("Comparing " + arr[i] + " and " + arr[j]); 30 | if (arr[i] < arr[j]) { 31 | return true; 32 | } else { 33 | return false; 34 | } 35 | } 36 | 37 | public static void print(int[] arr) { 38 | for (int i = 0; i < arr.length; i++) { 39 | System.out.println(arr[i]); 40 | } 41 | } 42 | 43 | public static void main(String[] args) throws Exception { 44 | Scanner scn = new Scanner(System.in); 45 | int n = scn.nextInt(); 46 | int[] arr = new int[n]; 47 | for (int i = 0; i < n; i++) { 48 | arr[i] = scn.nextInt(); 49 | } 50 | bubbleSort(arr); 51 | print(arr); 52 | } 53 | 54 | } -------------------------------------------------------------------------------- /level-1/lecture-029/print-subsequence.md: -------------------------------------------------------------------------------- 1 | # Print Subsequence 2 | 3 | > 1. You are given a string str. 4 | > 2. Complete the body of printSS function - without changing signature - to calculate and print all subsequences of str. 5 | 6 | > `Note -> The online judge can't force you to write the function recursively but that is what the spirit of question is. Write recursive and not iterative logic. The purpose of the question is to aid learning recursion and not test you.` 7 | 8 | ``` 9 | Input Format 10 | A string str 11 | Output Format 12 | Subsequences of str in order hinted by Sample output 13 | 14 | Constraints 15 | 0 <= str.length <= 7 16 | Sample Input 17 | yvTA 18 | 19 | Sample Output 20 | yvTA 21 | yvT 22 | yvA 23 | yv 24 | yTA 25 | yT 26 | yA 27 | y 28 | vTA 29 | vT 30 | vA 31 | v 32 | TA 33 | T 34 | A 35 | ``` 36 | 37 | ## Solution 38 | 39 | ```java 40 | import java.io.*; 41 | import java.util.*; 42 | 43 | public class Main { 44 | 45 | public static void main(String[] args) throws Exception { 46 | Scanner sc = new Scanner(System.in); 47 | String s = sc.nextLine(); 48 | printSS(s, ""); 49 | } 50 | 51 | public static void printSS(String str, String ans) { 52 | if (str.length() == 0) { // base case 53 | System.out.println(ans); // print the answer 54 | return; 55 | } 56 | char ch = str.charAt(0); // get the first character : a 57 | String ros = str.substring(1); // get the rest of the string : bc 58 | printSS(ros, ans + ch); // first call : ans contains character a 59 | printSS(ros, ans); // second call : ans does not contain character a 60 | } 61 | } 62 | ``` 63 | -------------------------------------------------------------------------------- /level-1/lecture-015/2d-array-demo.md: -------------------------------------------------------------------------------- 1 | # 2d Arrays Demo 2 | 3 | > 1. You are given a number n, representing the number of rows. 4 | > 2. You are given a number m, representing the number of columns. 5 | > 3. You are given n\*m numbers, representing elements of 2d array a. 6 | > 4. You are required to display the contents of 2d array as suggested by output format below. 7 | 8 | ``` 9 | Input Format 10 | A number n 11 | A number m 12 | e11 13 | e12.. 14 | e21 15 | e22.. 16 | .. n * m number of elements 17 | Output Format 18 | e11 e12 e13 .. 19 | e21 e22 e23 .. 20 | 21 | Constraints 22 | 1 <= n <= 10^2 23 | 1 <= m <= 10^2 24 | -10^9 <= e1, e2, .. n * m elements <= 10^9 25 | Sample Input 26 | 2 27 | 4 28 | 11 29 | 12 30 | 13 31 | 14 32 | 21 33 | 22 34 | 23 35 | 24 36 | Sample Output 37 | 11 12 13 14 38 | 21 22 23 24 39 | ``` 40 | 41 | ## Solution 42 | 43 | ```java 44 | import java.io.*; 45 | import java.util.*; 46 | 47 | public class Main{ 48 | 49 | public static void print2DArray(int[][] arr) { 50 | for (int i = 0; i < arr.length; i++) { 51 | for (int j = 0; j < arr[i].length; j++) { 52 | System.out.print(arr[i][j] + "\t"); 53 | } 54 | System.out.println(); 55 | } 56 | } 57 | 58 | public static void main(String[] args) throws Exception { 59 | Scanner sc = new Scanner(System.in); 60 | int n = sc.nextInt(); 61 | int m = sc.nextInt(); 62 | int[][] arr = new int[n][m]; 63 | for (int i = 0; i < arr.length; i++) { 64 | for (int j = 0; j < arr[i].length; j++) { 65 | arr[i][j] = sc.nextInt(); 66 | } 67 | } 68 | print2DArray(arr); 69 | } 70 | 71 | } 72 | ``` 73 | -------------------------------------------------------------------------------- /level-1/lecture-024/display-array.md: -------------------------------------------------------------------------------- 1 | # Display Array 2 | 3 | > 1. You are given a number n, representing the size of array a. 4 | > 2. You are given n numbers, representing elements of array a. 5 | > 3. You are required to print the elements of array from beginning to end each in a separate line. 6 | > 4. For the above purpose complete the body of displayArr function. Don't change the signature. 7 | 8 | > `Note -> The online judge can't force you to write the function recursively but that is what the spirit of question is. Write recursive and not iterative logic. The purpose of the question is to aid learning recursion and not test you.` 9 | 10 | 11 | ``` 12 | Input Format 13 | A number n 14 | n1 15 | n2 16 | .. n number of elements 17 | Output Format 18 | n1 19 | n2 20 | .. n elements 21 | 22 | Constraints 23 | 1 <= n <= 30 24 | 0 <= n1, n2, .. n elements <= 10 25 | Sample Input 26 | 5 27 | 3 28 | 1 29 | 0 30 | 7 31 | 5 32 | Sample Output 33 | 3 34 | 1 35 | 0 36 | 7 37 | 5 38 | ``` 39 | 40 | 41 | 42 | ## Solution 43 | 44 | ```java 45 | import java.io.*; 46 | import java.util.*; 47 | 48 | public class Main { 49 | 50 | public static void main(String[] args) throws Exception { 51 | Scanner sc = new Scanner(System.in); 52 | int n = sc.nextInt(); 53 | int[] a = new int[n]; 54 | for (int i = 0; i < n; i++) { 55 | a[i] = sc.nextInt(); 56 | } 57 | displayArray(a, 0); 58 | sc.close(); 59 | } 60 | 61 | public static void displayArray(int[] a, int i) { 62 | if (i == a.length) { 63 | return; 64 | } 65 | System.out.println(a[i]); 66 | displayArray(a, i + 1); 67 | } 68 | 69 | } 70 | ``` 71 | -------------------------------------------------------------------------------- /level-1/lecture-023/print-zigzag.md: -------------------------------------------------------------------------------- 1 | # Print Zigzag 2 | 3 | > 1. Here are a few sets of inputs and outputs for your reference 4 | 5 | > `Input1 -> 1` > `Output1 -> 1 1 1` 6 | 7 | > `Input2 -> 2` > `Output2 -> 2 1 1 1 2 1 1 1 2` 8 | 9 | > `Input3 -> 3` > `Output3 -> 3 2 1 1 1 2 1 1 1 2 3 2 1 1 1 2 1 1 1 2 3` 10 | 11 | > 2. Figure out the pattern and complete the recursive function pzz to achieve the above for any positive number n. 12 | > `Note -> The online judge can't force you to write the function recursively but that is what the spirit of question is.Write recursive and not iterative logic. The purpose of the question is to aid learning recursion and not test you.` 13 | 14 | ``` 15 | Input Format 16 | A number n 17 | Output Format 18 | As discussed in point 1 of description 19 | 20 | Constraints 21 | 1 <= n <= 10 22 | Sample Input 23 | 3 24 | Sample Output 25 | 3 2 1 1 1 2 1 1 1 2 3 2 1 1 1 2 1 1 1 2 3 26 | ``` 27 | 28 | ## Notes 29 | 30 | ![Screenshot 2021-12-14 at 11 58 45 PM](https://user-images.githubusercontent.com/28717686/146058281-d86e617d-b92a-4c53-8f5c-f99ab5739440.png) 31 | 32 | ## Solution 33 | 34 | ```java 35 | import java.io.*; 36 | import java.util.*; 37 | 38 | public class Main { 39 | 40 | public static void main(String[] args) throws Exception { 41 | Scanner sc = new Scanner(System.in); 42 | int n = sc.nextInt(); 43 | pzz(n); 44 | } 45 | 46 | public static void pzz(int n){ 47 | if(n == 0) { 48 | return; 49 | } 50 | System.out.print(n + " "); //pre 51 | pzz(n-1); 52 | System.out.print(n + " "); // in 53 | pzz(n-1); 54 | System.out.print(n + " "); // post 55 | } 56 | 57 | } 58 | ``` 59 | -------------------------------------------------------------------------------- /level-1/lecture-013/rotate-array.md: -------------------------------------------------------------------------------- 1 | # Rotate an array by k elements 2 | 3 | > 1. You are given a number n, representing the size of array a. 4 | > 2. You are given n numbers, representing elements of array a. 5 | > 3. You are given a number k. 6 | > 4. Rotate the array a, k times to the right (for positive values of k), and to the left for negative values of k. 7 | 8 | ```java 9 | import java.io.*; 10 | import java.util.*; 11 | 12 | public class Main{ 13 | public static void display(int[] a){ 14 | StringBuilder sb = new StringBuilder(); 15 | 16 | for(int val: a){ 17 | sb.append(val + " "); 18 | } 19 | System.out.println(sb); 20 | } 21 | 22 | public static void rotate(int[] arr, int k){ 23 | // write your code here 24 | k = (k > 0) ? k % arr.length : (k % arr.length) + arr.length; 25 | reverse(arr, 0, arr.length - k - 1); 26 | reverse(arr, arr.length - k, arr.length - 1); 27 | reverse(arr, 0, arr.length - 1); 28 | } 29 | public static void reverse(int[] arr, int start, int end) { 30 | while (start < end) { 31 | int temp = arr[start]; 32 | arr[start] = arr[end]; 33 | arr[end] = temp; 34 | start++; 35 | end--; 36 | } 37 | } 38 | public static void main(String[] args) throws Exception { 39 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 40 | 41 | int n = Integer.parseInt(br.readLine()); 42 | int[] a = new int[n]; 43 | for(int i = 0; i < n; i++){ 44 | a[i] = Integer.parseInt(br.readLine()); 45 | } 46 | int k = Integer.parseInt(br.readLine()); 47 | 48 | rotate(a, k); 49 | display(a); 50 | } 51 | 52 | } 53 | ``` 54 | -------------------------------------------------------------------------------- /level-1/lecture-025/max-of-array.md: -------------------------------------------------------------------------------- 1 | # Max Of An Array 2 | 3 | > 1. You are given a number n, representing the count of elements. 4 | > 2. You are given n numbers. 5 | > 3. You are required to find the maximum of input. 6 | > 4. For the purpose complete the body of maxOfArray function. Don't change the signature. 7 | 8 | > `Note -> The online judge can't force you to write the function recursively but that is what the spirit of question is. Write recursive and not iterative logic. The purpose of the question is to aid learning recursion and not test you.` 9 | 10 | ``` 11 | Input Format 12 | A number n 13 | n1 14 | n2 15 | .. n number of elements 16 | Output Format 17 | A number representing max 18 | 19 | Constraints 20 | 1 <= n <= 10^4 21 | 0 <= n1, n2, .. n elements <= 10 ^9 22 | Sample Input 23 | 6 24 | 15 25 | 30 26 | 40 27 | 4 28 | 11 29 | 9 30 | Sample Output 31 | 40 32 | ``` 33 | 34 | ## Solution 35 | 36 | ```java 37 | import java.util.*; 38 | 39 | public class Main { 40 | 41 | // Max of Array using Recursion 42 | public static int maxOfArray(int[] arr, int i) { 43 | if (i == arr.length - 1) { 44 | return arr[i]; 45 | } 46 | int recAns = maxOfArray(arr, i + 1); 47 | int me = arr[i]; 48 | if (me > recAns) { 49 | return me; 50 | } 51 | return recAns; 52 | } 53 | 54 | public static void main(String[] args) { 55 | Scanner sc = new Scanner(System.in); 56 | int n = sc.nextInt(); 57 | int[] arr = new int[n]; 58 | for (int i = 0; i < n; i++) { 59 | arr[i] = sc.nextInt(); 60 | } 61 | System.out.println(maxOfArray(arr, 0)); 62 | sc.close(); 63 | } 64 | } 65 | ``` 66 | -------------------------------------------------------------------------------- /level-1/lecture-052/SelectionSort.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class SelectionSort { 5 | 6 | public static void selectionSort(int[] arr) { 7 | int n = arr.length; 8 | for (int x = 0; x < n - 1; x++) { 9 | int minIdx = x; 10 | for (int i = x + 1; i < n; i++) { 11 | if (isSmaller(arr, i, minIdx) == true) { 12 | minIdx = i; 13 | } 14 | } 15 | swap(arr, x, minIdx); 16 | 17 | } 18 | 19 | } 20 | 21 | // used for swapping ith and jth elements of array 22 | public static void swap(int[] arr, int i, int j) { 23 | System.out.println("Swapping " + arr[i] + " and " + arr[j]); 24 | int temp = arr[i]; 25 | arr[i] = arr[j]; 26 | arr[j] = temp; 27 | } 28 | 29 | // return true if ith element is smaller than jth element 30 | public static boolean isSmaller(int[] arr, int i, int j) { 31 | System.out.println("Comparing " + arr[i] + " and " + arr[j]); 32 | if (arr[i] < arr[j]) { 33 | return true; 34 | } else { 35 | return false; 36 | } 37 | } 38 | 39 | public static void print(int[] arr) { 40 | for (int i = 0; i < arr.length; i++) { 41 | System.out.println(arr[i]); 42 | } 43 | } 44 | 45 | public static void main(String[] args) throws Exception { 46 | Scanner scn = new Scanner(System.in); 47 | int n = scn.nextInt(); 48 | int[] arr = new int[n]; 49 | for (int i = 0; i < n; i++) { 50 | arr[i] = scn.nextInt(); 51 | } 52 | selectionSort(arr); 53 | print(arr); 54 | } 55 | 56 | } -------------------------------------------------------------------------------- /level-1/lecture-027/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) throws Exception { 6 | Scanner sc = new Scanner(System.in); 7 | int n = sc.nextInt(); 8 | ArrayList ans = getStairPaths(n); 9 | System.out.println(ans); 10 | sc.close(); 11 | } 12 | 13 | public static ArrayList getStairPaths(int n) { 14 | if (n == 0) { // base case 15 | ArrayList base = new ArrayList(); 16 | base.add(""); 17 | return base; 18 | } 19 | ArrayList ans = new ArrayList(); 20 | for (int i = 1; i <= 3; i++) { 21 | if (n - i >= 0) { 22 | ArrayList rec = getStairPaths(n - i); 23 | for (String s : rec) { 24 | ans.add(i + s); 25 | } 26 | } 27 | } 28 | // if (n - 1 >= 0) { 29 | // ArrayList nm1 = getStairPaths(n - 1); // get all possible paths for 30 | // n-1 or 1 step 31 | // for (String s : nm1) { 32 | // ans.add("1" + s); 33 | // } 34 | // } 35 | // if (n - 2 >= 0) { 36 | // ArrayList nm2 = getStairPaths(n - 2); // get all possible paths for 37 | // n-2 or 2 step 38 | // for (String s : nm2) { 39 | // ans.add("2" + s); 40 | // } 41 | // } 42 | // if (n - 3 >= 0) { 43 | // ArrayList nm3 = getStairPaths(n - 3); // get all possible paths for 44 | // n-3 or 3 step 45 | // for (String s : nm3) { 46 | // ans.add("3" + s); 47 | // } 48 | // } 49 | return ans; 50 | } 51 | } -------------------------------------------------------------------------------- /level-1/lecture-052/InsertionSort.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class InsertionSort { 5 | 6 | public static void insertionSort(int[] arr) { 7 | int n = arr.length; 8 | for (int x = 1; x < n; x++) { 9 | for (int i = x - 1; i >= 0; i--) { 10 | if (isGreater(arr, i, i + 1))// arr[i] > arr[i+1]){ 11 | { 12 | swap(arr, i, i + 1); 13 | } else { 14 | break; 15 | } 16 | } 17 | } 18 | 19 | } 20 | 21 | // used for swapping ith and jth elements of array 22 | public static void swap(int[] arr, int i, int j) { 23 | System.out.println("Swapping " + arr[i] + " and " + arr[j]); 24 | int temp = arr[i]; 25 | arr[i] = arr[j]; 26 | arr[j] = temp; 27 | } 28 | 29 | // return true if jth element is greater than ith element 30 | public static boolean isGreater(int[] arr, int j, int i) { 31 | System.out.println("Comparing " + arr[i] + " and " + arr[j]); 32 | if (arr[i] < arr[j]) { 33 | return true; 34 | } else { 35 | return false; 36 | } 37 | } 38 | 39 | public static void print(int[] arr) { 40 | for (int i = 0; i < arr.length; i++) { 41 | System.out.println(arr[i]); 42 | } 43 | } 44 | 45 | public static void main(String[] args) throws Exception { 46 | Scanner scn = new Scanner(System.in); 47 | int n = scn.nextInt(); 48 | int[] arr = new int[n]; 49 | for (int i = 0; i < n; i++) { 50 | arr[i] = scn.nextInt(); 51 | } 52 | insertionSort(arr); 53 | print(arr); 54 | } 55 | 56 | } -------------------------------------------------------------------------------- /level-1/lecture-016/hw/spiral-display-2.md: -------------------------------------------------------------------------------- 1 | # Spiral Display 2 (Homework Problem) 2 | 3 | ```java 4 | import java.util.Scanner; 5 | 6 | public class Main { 7 | // Spiral Display Homework: 8 | public static void spiralMatrix2(int[][] arr, int row, int col) { 9 | int minrow = 0, maxrow = arr.length - 1, mincol = 0, maxcol = arr[0].length - 1; 10 | int count = 0, total = row * col; 11 | while (count < total) { 12 | for (int i = mincol; i <= maxcol && count < total; i++) { 13 | System.out.println(arr[minrow][i]); 14 | count++; 15 | } 16 | minrow++; 17 | for (int i = minrow; i <= maxrow && count < total; i++) { 18 | System.out.println(arr[i][maxcol]); 19 | count++; 20 | } 21 | maxcol--; 22 | for (int i = maxcol; i >= mincol && count < total; i--) { 23 | System.out.println(arr[maxrow][i]); 24 | count++; 25 | } 26 | maxrow--; 27 | for (int i = maxrow; i >= minrow && count < total; i--) { 28 | System.out.println(arr[i][mincol]); 29 | count++; 30 | } 31 | mincol++; 32 | } 33 | } 34 | 35 | public static void main(String[] args) { 36 | Scanner sc = new Scanner(System.in); 37 | int row = sc.nextInt(); 38 | int col = sc.nextInt(); 39 | int[][] arr = new int[row][col]; 40 | for (int i = 0; i < row; i++) { 41 | for (int j = 0; j < col; j++) { 42 | arr[i][j] = sc.nextInt(); 43 | } 44 | } 45 | spiralMatrix2(arr, row, col); 46 | sc.close(); 47 | } 48 | } 49 | 50 | ``` 51 | -------------------------------------------------------------------------------- /level-1/lecture-007/anybase-to-anybase.md: -------------------------------------------------------------------------------- 1 | # Any Base To Any Base 2 | 3 | > 1. You are given a number n. 4 | > 2. You are given a base b1. n is a number on base b. 5 | > 3. You are given another base b2. 6 | > 4. You are required to convert the number n of base b1 to a number in base b2. 7 | 8 | ``` 9 | Input Format 10 | A number n 11 | A base b1 12 | A base b2 13 | Output Format 14 | A number of base b2 equal in value to n of base b1. 15 | 16 | Constraints 17 | 0 <= n <= 512 18 | 2 <= b1 <= 10 19 | 2 <= b2 <= 10 20 | Sample Input 21 | 111001 22 | 2 23 | 3 24 | Sample Output 25 | 2010 26 | ``` 27 | 28 | ## Solution 29 | 30 | ```java 31 | import java.util.*; 32 | 33 | public class Main { 34 | 35 | public static int any2dec(int n, int b) { 36 | int ans = 0; 37 | int pow = 1; 38 | while(n > 0) { 39 | int rem = n % 10; 40 | n = n / 10; 41 | ans += (rem * pow); 42 | pow = pow * b; 43 | } 44 | return ans; 45 | } 46 | 47 | public static int dec2any(int n, int b) { 48 | int ans = 0; 49 | int pow = 1; 50 | while(n > 0) { 51 | int rem = n % b; 52 | n = n / b; 53 | ans += (rem * pow); 54 | pow = pow * 10; 55 | } 56 | return ans; 57 | } 58 | 59 | public static int any2any(int n, int b1, int b2) { 60 | int a2d = any2dec(n, b1); 61 | int d2a = dec2any(a2d, b2); 62 | return d2a; 63 | } 64 | 65 | public static void main(String[] args) { 66 | Scanner sc = new Scanner(System.in); 67 | int n = sc.nextInt(); 68 | int b1 = sc.nextInt(); 69 | int b2 = sc.nextInt(); 70 | System.out.print(any2any(n, b1, b2)); 71 | } 72 | } 73 | ``` 74 | -------------------------------------------------------------------------------- /level-1/lecture-023/power-log.md: -------------------------------------------------------------------------------- 1 | # Power-logarithmic 2 | 3 | > 1. You are given a number x. 4 | > 2. You are given another number n. 5 | > 3. You are required to calculate x raised to the power n. Don't change the signature of power function. 6 | > `Note1 -> The previous version expects the call stack to be of n height. This function expects call function to be only log(n) high.` 7 | > `Note2 -> The online judge can't force you to write the function recursively but that is what the spirit of question is. Write recursive and not iterative logic. The purpose of the question is to aid learning recursion and not test you.` 8 | 9 | ```text 10 | Input Format 11 | A number x 12 | A number n 13 | Output Format 14 | x raised to the power n 15 | 16 | Constraints 17 | 1 <= x <= 10 18 | 0 <= n <= 9 19 | Sample Input 20 | 2 21 | 5 22 | Sample Output 23 | 32 24 | ``` 25 | ## Note 26 | 27 | ![linear](https://user-images.githubusercontent.com/28717686/146039769-009a43e8-62db-4c2c-8bbc-bb171aed22ff.png) 28 | ![breakdown](https://user-images.githubusercontent.com/28717686/146039783-1b4e3399-5615-48ec-a19a-50761211f41a.png) 29 | ![relation](https://user-images.githubusercontent.com/28717686/146039799-89a53c7b-5cdf-4008-b7dd-5e517d03fa9e.png) 30 | 31 | ## Solution 32 | 33 | ```java 34 | import java.io.*; 35 | import java.util.*; 36 | 37 | public class Main { 38 | 39 | public static void main(String[] args) throws Exception { 40 | Scanner sc = new Scanner(System.in); 41 | int n = sc.nextInt(); 42 | int p = sc.nextInt(); 43 | System.out.println(powerLogarithmic(n, p)); 44 | } 45 | 46 | public static int powerLogarithmic(int n, int p){ 47 | return (p == 0) ? 1 : (p % 2 == 0) ? powerLogarithmic(n * n, p / 2) : n * powerLogarithmic(n * n, p / 2); 48 | } 49 | 50 | } 51 | ``` 52 | -------------------------------------------------------------------------------- /level-1/lecture-024/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Main { 4 | // Agenda 5 | // tower of hanoi 6 | // Recursion in arrays 7 | 8 | public static void toh(int n, int t1, int t2, int t3) { 9 | if (n == 0) { 10 | return; 11 | } 12 | toh(n - 1, t1, t3, t2); 13 | System.out.println(n + "[" + t1 + " -> " + t2 + "]"); 14 | toh(n - 1, t3, t2, t1); 15 | } 16 | 17 | public static void displayArray(int[] a, int i) { 18 | if (i == a.length) { 19 | return; 20 | } 21 | System.out.println(a[i]); 22 | displayArray(a, i + 1); 23 | } 24 | 25 | public static void dar(int[] a, int i) { 26 | if (i == a.length) { 27 | return; 28 | } 29 | dar(a, i + 1); 30 | System.out.println(a[i]); 31 | } 32 | 33 | // max of array recursively 34 | public static int maxOfArray(int[] a, int i) { 35 | if (i == a.length - 1) { 36 | return a[i]; 37 | } 38 | int misa = maxOfArray(a, i + 1); 39 | if (misa > a[i]) { 40 | return misa; 41 | } else { 42 | return a[i]; 43 | } 44 | } 45 | 46 | public static void main(String[] args) { 47 | Scanner sc = new Scanner(System.in); 48 | int n = sc.nextInt(); 49 | // int t1 = sc.nextInt(); 50 | // int t2 = sc.nextInt(); 51 | // int t3 = sc.nextInt(); 52 | // toh(n, t1, t2, t3); // t1 -> t2 using t3 53 | int[] a = new int[n]; 54 | for (int i = 0; i < n; i++) { 55 | a[i] = sc.nextInt(); 56 | } 57 | // displayArray(a, 0); 58 | // dar(a, 0); // display array reverse 59 | System.out.println(maxOfArray(a, 0)); 60 | sc.close(); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /level-1/lecture-049/KLargestElements.md: -------------------------------------------------------------------------------- 1 | # K Largest Elements 2 | 3 | `Easy` 4 | 5 | > 1. You will be given an Array with its length 6 | > 2. You will also be given an integral value k 7 | > 3. You need to find k largest elements from the given array 8 | > 4. Input is handled for you 9 | > 5. It is a functional problem ,please do not modify main() 10 | 11 | ## Constraints 12 | 13 | - 1 <= N <= 100 14 | - K <= N 15 | - -1000 <= C[i] <= 1000 16 | 17 | ## Format 18 | 19 | ## Input 20 | 21 | - Input is handled for you 22 | 23 | ## Output 24 | 25 | - Output MUST be in descending order 26 | 27 | ## Example 28 | 29 | ## Sample Input 30 | 31 | ``` 32 | 8 33 | 44 -5 -2 41 12 19 21 -6 34 | 2 35 | ``` 36 | 37 | ## Sample Output 38 | 39 | `44 41` 40 | 41 | ## Solution 42 | 43 | ```java 44 | 45 | import java.util.*; 46 | 47 | public class Main { 48 | 49 | public static void kLargestElements(int[] arr, int k) { 50 | PriorityQueue pq = new PriorityQueue<>(); // min heap 51 | for (int i = 0; i < arr.length; i++) { 52 | if (i < k) 53 | pq.add(arr[i]); // add all elements to the heap at max k times 54 | else { 55 | if (arr[i] > pq.peek()) { 56 | pq.remove(); 57 | pq.add(arr[i]); 58 | } 59 | } 60 | } 61 | while (!pq.isEmpty()) { 62 | System.out.println(pq.remove()); 63 | } 64 | } 65 | 66 | public static void main(String[] args) { 67 | Scanner sc = new Scanner(System.in); 68 | int n = sc.nextInt(); 69 | int[] arr = new int[n]; 70 | for (int i = 0; i < n; i++) { 71 | arr[i] = sc.nextInt(); 72 | } 73 | int k = sc.nextInt(); 74 | kLargestElements(arr, k); 75 | sc.close(); 76 | } 77 | } 78 | ``` 79 | -------------------------------------------------------------------------------- /level-1/lecture-011/bar-chart.md: -------------------------------------------------------------------------------- 1 | # Bar Chart 2 | 3 | > 1. You are given a number n, representing the size of array a. 4 | > 2. You are given n numbers, representing elements of array a. 5 | > 3. You are required to print a bar chart representing value of arr a. 6 | 7 | ``` 8 | Input Format 9 | A number n 10 | n1 11 | n2 12 | .. n number of elements 13 | Output Format 14 | A bar chart of asteriks representing value of array a 15 | 16 | Constraints 17 | 1 <= n <= 30 18 | 0 <= n1, n2, .. n elements <= 10 19 | Sample Input 20 | 5 21 | 3 22 | 1 23 | 0 24 | 7 25 | 5 26 | Sample Output 27 | * 28 | * 29 | * * 30 | * * 31 | * * * 32 | * * * 33 | * * * * 34 | ``` 35 | 36 | ## Solution 37 | 38 | ```java 39 | import java.io.*; 40 | import java.util.*; 41 | 42 | public class Main{ 43 | 44 | public static int maximum(int[] arr) { 45 | int max = Integer.MIN_VALUE; 46 | // max = -(int)1e8; 47 | for (int i = 0; i < arr.length; i++) { 48 | if (arr[i] > max) { 49 | max = arr[i]; 50 | } 51 | } 52 | return max; 53 | } 54 | 55 | public static void printBars(int[] arr, int n) { 56 | int height = maximum(arr); 57 | for (int h = height; h >= 1; h--) { 58 | for (int i = 0; i < n; i++) { 59 | if (arr[i] >= h) { 60 | System.out.print("*\t"); 61 | } else { 62 | System.out.print("\t"); 63 | } 64 | } 65 | System.out.println(); 66 | } 67 | } 68 | 69 | 70 | public static void main(String[] args){ 71 | Scanner sc = new Scanner(System.in); 72 | int n = sc.nextInt(); 73 | int[] arr = new int[n]; 74 | for(int i = 0; i < n; i++) { 75 | arr[i] = sc.nextInt(); 76 | } 77 | printBars(arr, n); 78 | } 79 | 80 | } 81 | ``` 82 | -------------------------------------------------------------------------------- /level-1/lecture-026/get-subsequence.md: -------------------------------------------------------------------------------- 1 | # Get Subsequence 2 | 3 | > 1. You are given a string str. 4 | > 2. Complete the body of getSS function - without changing signature - to calculate all subsequences of str. 5 | > Use sample input and output to take idea about subsequences. 6 | 7 | > `Note -> The online judge can't force you to write the function recursively but that is what the spirit of question is.` 8 | > `Write recursive and not iterative logic. The purpose of the question is to aid learning recursion and not test you.` 9 | 10 | ``` 11 | Input Format 12 | A string str 13 | Output Format 14 | Contents of the arraylist containing subsequences as shown in sample output 15 | 16 | Constraints 17 | 0 <= str.length <= 20 18 | Sample Input 19 | abc 20 | Sample Output 21 | [, c, b, bc, a, ac, ab, abc] 22 | ``` 23 | 24 | ## Solution 25 | 26 | ```java 27 | import java.io.*; 28 | import java.util.*; 29 | 30 | public class Main { 31 | 32 | // Get Subsequence : to calculate all subsequences of string 33 | public static ArrayList gss(String s) { 34 | if (s.length() == 0) { 35 | ArrayList empty = new ArrayList(); 36 | empty.add(""); 37 | return empty; 38 | } 39 | char ch = s.charAt(0); 40 | String ros = s.substring(1); 41 | ArrayList rec = gss(ros); 42 | ArrayList result = new ArrayList(); 43 | for (String str : rec) { 44 | result.add(str); 45 | } 46 | for (String str : rec) { 47 | result.add(ch + str); 48 | } 49 | return result; 50 | } 51 | 52 | public static void main(String[] args) { 53 | Scanner sc = new Scanner(System.in); 54 | String s = sc.next(); 55 | ArrayList list = gss(s); 56 | System.out.println(list); 57 | sc.close(); 58 | } 59 | } 60 | ``` 61 | -------------------------------------------------------------------------------- /level-1/lecture-025/first-index.md: -------------------------------------------------------------------------------- 1 | # First Index 2 | 3 | > 1. You are given a number n, representing the count of elements. 4 | > 2. You are given n numbers. 5 | > 3. You are given a number x. 6 | > 4. You are required to find the first index at which x occurs in array a. 7 | > 5. If x exists in array, print the first index where it is found otherwise print -1. 8 | 9 | > `Note -> The online judge can't force you to write the function recursively but that is what the spirit of question is. Write recursive and not iterative logic. The purpose of the question is to aid learning recursion and not test you.` 10 | 11 | ``` 12 | Input Format 13 | A number n 14 | n1 15 | n2 16 | .. n number of elements 17 | A number x 18 | Output Format 19 | A number representing first index of occurence of x in array a or -1 if not found at all. 20 | 21 | Constraints 22 | 1 <= n <= 10^4 23 | 0 <= n1, n2, .. n elements <= 10 ^ 3 24 | 0 <= x <= 10 ^ 3 25 | Sample Input 26 | 6 27 | 15 28 | 11 29 | 40 30 | 4 31 | 4 32 | 9 33 | 4 34 | Sample Output 35 | 3 36 | ``` 37 | 38 | ## Solution 39 | 40 | ```java 41 | import java.io.*; 42 | import java.util.*; 43 | 44 | public class Main { 45 | 46 | // First Index of Array using Recursion 47 | public static int firstIndexOfArray(int[] arr, int i, int target) { 48 | if (i == arr.length) { 49 | return -1; 50 | } 51 | if (arr[i] == target) { 52 | return i; 53 | } 54 | return firstIndexOfArray(arr, i + 1, target); 55 | } 56 | 57 | public static void main(String[] args) { 58 | Scanner sc = new Scanner(System.in); 59 | int n = sc.nextInt(); 60 | int[] arr = new int[n]; 61 | for (int i = 0; i < n; i++) { 62 | arr[i] = sc.nextInt(); 63 | } 64 | int target = sc.nextInt(); 65 | System.out.println(firstIndexOfArray(arr, 0, target)); 66 | sc.close(); 67 | } 68 | 69 | } 70 | ``` 71 | -------------------------------------------------------------------------------- /level-1/lecture-004/rotate-by-k.md: -------------------------------------------------------------------------------- 1 | # Rotate A Number 2 | 3 | > 1. You are given two numbers n and k. You are required to rotate n, k times to the right. If k is positive, rotate to the right i.e. remove rightmost digit and make it leftmost. Do the reverse for negative value of k. Also k can have an absolute value larger than number of digits in n. 4 | > 2. Take as input n and k. 5 | > 3. Print the rotated number. 6 | > 4. Note - Assume that the number of rotations will not cause leading 0's in the result. e.g. such an input will not be given 7 | > n = 12340056 8 | > k = 3 9 | > r = 05612340 10 | 11 | ``` 12 | Input Format 13 | "n" where n is any integer. 14 | "K" where k is any integer. 15 | Output Format 16 | "r", the rotated number 17 | 18 | Constraints 19 | 1 <= n < 10^9 20 | -10^9 < k < 10^9 21 | Sample Input 22 | 562984 23 | 2 24 | Sample Output 25 | 845629 26 | ``` 27 | 28 | ## Solution 29 | 30 | ```java 31 | import java.util.Scanner; 32 | 33 | public class Main { 34 | 35 | public static int countDigits(int n) { 36 | int count = 0; 37 | while (n != 0) { 38 | n = n / 10; 39 | count++; 40 | } 41 | return count; 42 | } 43 | 44 | public static int rotateNumber(int n, int k) { 45 | int len = countDigits(n); 46 | k = (k % len + len) % len; 47 | int div = 1, mul = 1; 48 | for (int i = 1; i <= k; i++) { 49 | div *= 10; 50 | } 51 | for (int i = 1; i <= len - k; i++) { 52 | mul *= 10; 53 | } 54 | int a = n / div; 55 | int b = n % div; 56 | return (b * mul + a); 57 | } 58 | 59 | public static void main(String[] args) { 60 | Scanner sc = new Scanner(System.in); 61 | int n = sc.nextInt(); 62 | int k = sc.nextInt(); 63 | int rotatedNumber = rotateNumber(n, k); 64 | System.out.println(rotatedNumber); 65 | sc.close(); 66 | } 67 | } 68 | ``` 69 | -------------------------------------------------------------------------------- /level-1/lecture-015/subsets-of-array.md: -------------------------------------------------------------------------------- 1 | # Subsets of Array 2 | 3 | > 1. You are given a number n, representing the count of elements. 4 | > 2. You are given n numbers. 5 | > 3. You are required to print all subsets of arr. Each subset should be 6 | > on separate line. For more clarity check out sample input and output. 7 | 8 | ```text 9 | Input Format 10 | A number n 11 | n1 12 | n2 13 | .. n number of elements 14 | Output Format 15 | [Tab separated elements of subset] 16 | .. 17 | All subsets 18 | 19 | Constraints 20 | 1 <= n <= 10 21 | 0 <= n1, n2, .. n elements <= 10^3 22 | Sample Input 23 | 3 24 | 10 25 | 20 26 | 30 27 | Sample Output 28 | - - - 29 | - - 30 30 | - 20 - 31 | - 20 30 32 | 10 - - 33 | 10 - 30 34 | 10 20 - 35 | 10 20 30 36 | 37 | ``` 38 | 39 | ```java 40 | import java.io.*; 41 | import java.util.*; 42 | 43 | public class Main{ 44 | 45 | public static void printSubsets(int[] arr) { 46 | int n = arr.length; 47 | int total = (int) Math.pow(2, n); 48 | for (int i = 0; i < total; i++) { // loop runs from 0 to (2^n - 1) 49 | String str = ""; 50 | int temp = i; // temp is used to store the value of i 51 | for (int j = n - 1; j >= 0; j--) { 52 | int r = temp % 2; // r is used to store the remainder 53 | temp /= 2; // temp is divided by 2 54 | if (r == 0) { 55 | str = "-\t" + str; 56 | } else { 57 | str = arr[j] + "\t" + str; 58 | } 59 | } 60 | System.out.println(str); 61 | } 62 | } 63 | 64 | public static void main(String[] args) { 65 | Scanner sc = new Scanner(System.in); 66 | int n = sc.nextInt(); 67 | int[] arr = new int[n]; 68 | for (int i = 0; i < arr.length; i++) { 69 | arr[i] = sc.nextInt(); 70 | } 71 | printSubsets(arr); 72 | sc.close(); 73 | } 74 | } 75 | ``` 76 | -------------------------------------------------------------------------------- /level-1/archive/test-problems/intuit-test/003-aman-and-garden.md: -------------------------------------------------------------------------------- 1 | # Aman and garden 2 | 3 | `Medium` 4 | 5 | ``` 6 | There is a garden and Aman is the gardener and he wants to arrange trees in a row 7 | of length n such that every Kth plant is non-decreasing in height. For example the plant 8 | at position 1 should be smaller than or equal to the tree planted at position 1+K and plant 9 | at position 1+K should be smaller than or equal to 1+2*K and so on, this 10 | should be true for every position(for eg. 2 <= 2+K <= 2+2*K ...). 11 | 12 | Now Aman can change a plant at any position with a plant of any height in order to create 13 | the required arrangment. He wants to know minimum number of trees he will have to change to get the required arrangement. 14 | 15 | We"ll be given a plants array which represents the height of plants at every position 16 | and a number K and we need to output the minimum number of plants we will have to change to get the required arrangement. 17 | 18 | Example: 19 | plants = [5,3,4,1,6,5]; 20 | K=2; 21 | 22 | here plants at index (0,2,4) and (1,3,5) should be non decreasing. 23 | plants[0]=5; 24 | plants[2]=4; 25 | plants[4]=6; 26 | We can change the plant at index 2 with a plant of height 5(one change). 27 | 28 | Similarly 29 | plants[1]=3; 30 | plants[3]=1; 31 | plants[5]=5; 32 | We can change the plant at index 3 with a plant of height 4(one change). 33 | 34 | So minimum 2 changes are required. 35 | 36 | Constraints 37 | 38 | 1<=plants.length<=100000 39 | 1<=plants[i],K<=plants.length 40 | 41 | Format 42 | 43 | Input 44 | 45 | First line contains an integer N representing length of the plant array. 46 | Next line contains N space separated integers representing height of trees. 47 | Last line contains an integer K 48 | 49 | Output 50 | 51 | Output the minimum number of changes required. 52 | 53 | Example 54 | 55 | Sample Input 56 | 57 | 6 58 | 5 59 | 3 60 | 4 61 | 1 62 | 6 63 | 5 64 | 2 65 | 66 | Sample Output 67 | 68 | 2 69 | ``` 70 | -------------------------------------------------------------------------------- /level-1/lecture-026/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Main { 4 | 5 | // All Indices of target in Array using top down approach 6 | public static int[] allIndices(int[] arr, int i, int target, int count) { 7 | if (i == arr.length) { // Base Case 8 | int[] indices = new int[count]; 9 | return indices; 10 | } 11 | if (arr[i] == target) { 12 | count++; 13 | } 14 | int[] indices = allIndices(arr, i + 1, target, count); 15 | if (arr[i] == target) { 16 | indices[count - 1] = i; 17 | } 18 | return indices; 19 | } 20 | 21 | // Get Subsequence : to get all subsequences of the given string 22 | public static ArrayList gss(String s) { 23 | if (s.length() == 0) { 24 | ArrayList empty = new ArrayList(); 25 | empty.add(""); 26 | return empty; 27 | } 28 | char ch = s.charAt(0); 29 | String ros = s.substring(1); 30 | ArrayList rec = gss(ros); 31 | ArrayList result = new ArrayList(); 32 | for (String str : rec) { 33 | result.add(str); 34 | } 35 | for (String str : rec) { 36 | result.add(ch + str); 37 | } 38 | return result; 39 | } 40 | 41 | public static void main(String[] args) { 42 | Scanner sc = new Scanner(System.in); 43 | // int[] arr = new int[sc.nextInt()]; 44 | // for (int i = 0; i < arr.length; i++) { 45 | // arr[i] = sc.nextInt(); 46 | // } 47 | // int target = sc.nextInt(); 48 | // int[] indices = allIndices(arr, 0, target, 0); 49 | // for (int i = 0; i < indices.length; i++) { 50 | // System.out.print(indices[i] + " "); 51 | // } 52 | String s = sc.next(); 53 | ArrayList list = gss(s); 54 | System.out.println(list); 55 | sc.close(); 56 | } 57 | } -------------------------------------------------------------------------------- /level-1/lecture-052/sort-01.md: -------------------------------------------------------------------------------- 1 | # Sort 01 2 | 3 | `Easy` 4 | 5 | - 1. You are given an array(arr) containing only 0's and 1's. 6 | - 2. You have to sort the given array in increasing order and in linear time. 7 | 8 | ## Constraints 9 | 10 | - 1 <= N <= 10000 11 | - arr[i] = 0,1 12 | 13 | ## Format 14 | 15 | ## Input 16 | 17 | - An Integer N 18 | - arr1 19 | - arr2.. 20 | - n integers 21 | 22 | ## Output 23 | 24 | - sample output 25 | 26 | ## Example 27 | 28 | ## Sample Input 29 | 30 | ``` 31 | 5 32 | 0 33 | 1 34 | 0 35 | 1 36 | 0 37 | ``` 38 | 39 | ## Sample Output 40 | 41 | ``` 42 | Swapping index 0 and index 0 43 | Swapping index 2 and index 1 44 | Swapping index 4 and index 2 45 | 0 46 | 0 47 | 0 48 | 1 49 | 1 50 | ``` 51 | 52 | ## Solution 53 | 54 | ```java 55 | import java.io.*; 56 | import java.util.*; 57 | 58 | public class Main { 59 | 60 | public static void sort01(int[] arr){ 61 | int i = 0; 62 | int j = 0; 63 | int n = arr.length; 64 | for(; j Sorting - The process of arranging the elements of a collection in order for a given data set. 6 | 7 | - Expectation from Bubble Sort : Spit out the sorted array in a non decreasing order. 8 | - In every iteration, the largest element is always at the end of the array. 9 | 10 | ## Iterations in a Bubble sort 11 | 12 | - Given Data Set (Array) : `arr = [4,7,9,1,5,3]` 13 | > Iteration 1 14 | - Find current largest element in the array => `9` 15 | - Swap the largest element with the last element in the array => `[4,7,9,1,5,3]` 16 | - `arr = [4,7,(9,1),5,3]` => `arr = [4,7,1,9,5,3]` 17 | - `arr = [4,7,1,(9,5),3]` => `arr = [4,7,1,5,9,3]` 18 | - `arr = [4,7,1,5,(9,3)]` => `arr = [4,7,1,5,3,9]` 19 | > Iteration 2 20 | - Find current largest element in the array => `7` 21 | - Swap the largest element with the second last element in the array => `[4,7,1,5,3,9]` 22 | - `arr = [4,(7,1),5,3,9]` => `arr = [4,1,7,5,3,9]` 23 | - `arr = [4,1,(7,5),3,9]` => `arr = [4,1,5,7,3,9]` 24 | - `arr = [4,1,5,(7,3),9]` => `arr = [4,1,5,3,7,9]` 25 | > Iteration 3 26 | - Find current largest element in the array => `5` 27 | - Swap the largest element with the third last element in the array => `[4,1,5,3,7,9]` 28 | - `arr = [4,1,(5,3),7,9]` => `arr = [4,1,3,5,7,9]` 29 | > Iteration 4 30 | - Find current largest element in the array => `4` 31 | - Swap the largest element with the fourth last element in the array => `[4,1,3,5,7,9]` 32 | - `arr = [(4,1),3,5,7,9]` => `arr = [1,4,3,5,7,9]` 33 | - `arr = [1,(4,3),5,7,9]` => `arr = [1,3,4,5,7,9]` 34 | > Iteration 5 35 | - Find current largest element in the array => `3` 36 | - Swap the largest element with the fifth last element in the array => `[1,3,4,5,7,9]` 37 | - sorted array => `[1,3,4,5,7,9]` (already sorted) 38 | 39 | ## Number of Iterations in Bubble Sort - `(N-1)` 40 | 41 | - In (N-1) iterations, [1, (n-1)element] will be sorted hence 1 will automatically be sorted. 42 | -------------------------------------------------------------------------------- /level-1/lecture-048/highest-frequency-character.md: -------------------------------------------------------------------------------- 1 | # Highest Frequency Character 2 | 3 | `Easy` 4 | 5 | > 1. You are given a string str. 6 | > 2. You are required to find the character with maximum frequency. 7 | 8 | ## Constraints 9 | 10 | > 0 < str.length() <= 100 11 | > There will be a single character with highest frequency 12 | 13 | ## Format 14 | 15 | ## Input 16 | 17 | > A string str 18 | 19 | ## Output 20 | 21 | > The character with highest frequency 22 | 23 | ## Example 24 | 25 | ## Sample Input 26 | 27 | > zmszeqxllzvheqwrofgcuntypejcxovtaqbnqyqlmrwitc 28 | 29 | ## Sample Output 30 | 31 | > q 32 | 33 | ## Solution 34 | 35 | ```java 36 | import java.util.*; 37 | 38 | public class HighFreqChar { 39 | public static void main(String[] args) { 40 | // to get maximum frequency character using hashmap 41 | Scanner sc = new Scanner(System.in); 42 | String s = sc.nextLine(); 43 | HashMap hm = new HashMap(); 44 | for (int i = 0; i < s.length(); i++) { 45 | char ch = s.charAt(i); 46 | if (hm.containsKey(ch)) { 47 | int val = hm.get(ch); 48 | hm.put(ch, val + 1); // repeat character 49 | } else { 50 | hm.put(ch, 1); // first time seeing this character 51 | } 52 | // hm.put(ch, hm.getOrDefault(ch, 0) + 1); 53 | } 54 | 55 | char maxFreqChar = s.charAt(0); 56 | for (Character key : hm.keySet()) { 57 | if (hm.get(key) > hm.get(maxFreqChar)) { 58 | maxFreqChar = key; // update maxFreqChar if current key is greater than maxFreqChar 59 | } 60 | } 61 | System.out.println(maxFreqChar); 62 | sc.close(); 63 | } 64 | } 65 | ``` 66 | 67 | ```bash 68 | > java HighFreqChar.java 69 | aaaabbb 70 | a 71 | ``` 72 | 73 | ## Dry Run 74 | 75 | | Character | Integer | 76 | | :-------: | :-----: | 77 | | a | 4 | 78 | | b | 3 | 79 | 80 | > So, we have 4 a's and 3 b's. Output is a because it has highest frequency. 81 | -------------------------------------------------------------------------------- /level-1/lecture-053/mergeSort/Main.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Main { 5 | 6 | public static int[] mergeSort(int[] arr, int lo, int hi) { 7 | if(lo == hi){ 8 | // int [] base = new int[1]; 9 | // base[0] = arr[lo]; 10 | // return base; 11 | 12 | return new int[]{arr[lo]}; 13 | } 14 | 15 | 16 | int mid = (lo + hi) / 2; 17 | 18 | 19 | int [] left = mergeSort(arr, lo, mid); 20 | int [] right = mergeSort(arr, mid + 1, hi); 21 | 22 | int [] ans = mergeTwoSortedArrays(left, right); 23 | 24 | return ans; 25 | } 26 | 27 | //used for merging two sorted arrays 28 | public static int[] mergeTwoSortedArrays(int[] a, int[] b){ 29 | System.out.println("Merging these two arrays "); 30 | System.out.print("left array -> "); 31 | print(a); 32 | System.out.print("right array -> "); 33 | print(b); 34 | int i = 0, j =0, k = 0; 35 | int[] ans = new int[a.length + b.length]; 36 | while(i < a.length && j < b.length){ 37 | if(a[i] <= b[j]){ 38 | ans[k] = a[i]; 39 | i++; 40 | k++; 41 | }else{ 42 | ans[k] = b[j]; 43 | j++; 44 | k++; 45 | } 46 | } 47 | 48 | while(i < a.length){ 49 | ans[k] = a[i]; 50 | k++; 51 | i++; 52 | } 53 | 54 | while(j < b.length){ 55 | ans[k] = b[j]; 56 | k++; 57 | j++; 58 | } 59 | 60 | return ans; 61 | } 62 | 63 | public static void print(int[] arr) { 64 | for (int i = 0; i < arr.length; i++) { 65 | System.out.print(arr[i] + " "); 66 | } 67 | System.out.println(); 68 | } 69 | 70 | // -2,147,483,648 to 2,147,483,647 71 | 72 | public static void main(String[] args) throws Exception { 73 | Scanner scn = new Scanner(System.in); 74 | int n = scn.nextInt(); 75 | int[] arr = new int[n]; 76 | for (int i = 0; i < n; i++) { 77 | arr[i] = scn.nextInt(); 78 | } 79 | int[] sa = mergeSort(arr,0,arr.length - 1); 80 | System.out.print("Sorted Array -> "); 81 | print(sa); 82 | } 83 | 84 | } -------------------------------------------------------------------------------- /level-1/lecture-003/inverse-of-a-number.md: -------------------------------------------------------------------------------- 1 | # Inverse Of A Number 2 | 3 | > 1. You are given a number following certain constraints. 4 | > 2. The key constraint is if the number is 5 digits long, it'll contain all the digits from 1 to 5 without missing any and without repeating any. e.g. 23415 is a 5 digit long number containing all digits from 1 to 5 without missing and repeating any digit from 1 to 5.Take a look at few other valid numbers - 624135, 81456273 etc.Here are a few invalid numbers - 139, 7421357 etc. 5 | > 3. The inverse of a number is defined as the number created by interchanging the face value and index of digits of number.e.g. for 426135 (reading from right to left, 5 is in place 1, 3 is in place 2, 1 is in place 3, 6 is in place 4, 2 is in place 5 and 4 is in place 6), the inverse will be 416253 (reading from right to left, 3 is in place 1, 5 is in place 2,2 is in place 3, 6 is in place 4, 1 is in place 5 and 4 is in place 6) More examples - inverse of 2134 is 1243 and inverse of 24153 is 24153 6 | > 4. Take as input number "n", assume that the number will follow constraints. 7 | > 5. Print it's inverse. 8 | 9 | ``` 10 | Input Format 11 | "n" where n is any integer, following constraints defined above. 12 | Output Format 13 | "i", the inverted number 14 | 15 | Constraints 16 | 1 <= n < 10^8, and follwing other constraints defined above. 17 | Sample Input 18 | 28346751 19 | Sample Output 20 | 73425681 21 | ``` 22 | 23 | ## Solution 24 | 25 | ```java 26 | import java.util.Scanner; 27 | 28 | public class Main { 29 | public static void main(String[] args) { 30 | Scanner sc = new Scanner(System.in); 31 | int n = sc.nextInt(); 32 | int ans = printInverse(n); 33 | System.out.println(ans); 34 | sc.close(); 35 | } 36 | 37 | public static int printInverse(int num) { 38 | int lastIndex = 1, res = 0; 39 | while (num != 0) { 40 | int lastDigit = num % 10; 41 | num /= 10; 42 | res += lastIndex * (int) Math.pow(10, lastDigit - 1); 43 | lastIndex++; 44 | } 45 | return res; 46 | } 47 | } 48 | ``` 49 | -------------------------------------------------------------------------------- /level-1/lecture-011/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Main { 4 | 5 | public static int maximum(int[] arr) { 6 | int max = Integer.MIN_VALUE; 7 | // max = -(int)1e8; 8 | for (int i = 0; i < arr.length; i++) { 9 | if (arr[i] > max) { 10 | max = arr[i]; 11 | } 12 | } 13 | return max; 14 | } 15 | 16 | public static int minimum(int[] arr) { 17 | int min = Integer.MAX_VALUE; 18 | // min = (int)1e8; 19 | for (int i = 0; i < arr.length; i++) { 20 | if (arr[i] < min) { 21 | min = arr[i]; 22 | } 23 | } 24 | return min; 25 | } 26 | 27 | // span of array : max - min 28 | // diffrence of max and min 29 | public static int spanOfArray(int[] arr) { 30 | int max = maximum(arr); 31 | int min = minimum(arr); 32 | return (max - min); 33 | } 34 | 35 | public static void printBars(int[] arr, int n) { 36 | int height = maximum(arr); 37 | for (int h = height; h >= 1; h--) { 38 | for (int i = 0; i < n; i++) { 39 | if (arr[i] >= h) { 40 | System.out.print("*\t"); 41 | } else { 42 | System.out.print("\t"); 43 | } 44 | } 45 | System.out.println(); 46 | } 47 | } 48 | 49 | public static void reverseArray(int[] arr, int n) { 50 | int i = 0, j = n - 1; 51 | while (i < j) { 52 | int temp = arr[i]; 53 | arr[i] = arr[j]; 54 | arr[j] = temp; 55 | i++; 56 | j--; 57 | } 58 | } 59 | 60 | public static void main(String[] args) { 61 | Scanner sc = new Scanner(System.in); 62 | int n = sc.nextInt(); 63 | int[] arr = new int[n]; 64 | for (int i = 0; i < n; i++) { 65 | arr[i] = sc.nextInt(); 66 | } 67 | reverseArray(arr, n); 68 | for (int i = 0; i < n; i++) { 69 | System.out.print(arr[i] + " "); 70 | } 71 | sc.close(); 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /level-1/lecture-029/print-stair-path.md: -------------------------------------------------------------------------------- 1 | # Print Stair Paths 2 | 3 | > 1. You are given a number n representing number of stairs in a staircase. 4 | > 2. You are standing at the bottom of staircase. You are allowed to climb 1 step, 2 steps or 3 steps in one move. 5 | > 3. Complete the body of printStairPaths function - without changing signature - to print the list of all paths that can be used to climb the staircase up. 6 | 7 | > `Note -> The online judge can't force you to write the function recursively but that is what the spirit of question is. Write recursive and not iterative logic. The purpose of the question is to aid learning recursion and not test you.` 8 | 9 | ``` 10 | Input Format 11 | A number n 12 | Output Format 13 | Print paths (one path in each line) in order hinted by Sample output 14 | 15 | Constraints 16 | 0 <= n <= 10 17 | Sample Input 18 | 3 19 | Sample Output 20 | 111 21 | 12 22 | 21 23 | 3 24 | ``` 25 | 26 | ## Solution 27 | 28 | ```java 29 | import java.io.*; 30 | import java.util.*; 31 | 32 | public class Main { 33 | 34 | public static void main(String[] args) throws Exception { 35 | Scanner sc = new Scanner(System.in); 36 | int n = sc.nextInt(); 37 | printStairPaths(n, ""); 38 | } 39 | 40 | public static void printStairPaths(int n, String path) { 41 | 42 | // ! in a stair path, you can either take 1, 2 or 3 steps 43 | 44 | if (n == 0) { 45 | System.out.println(path); // print the answer 46 | return; 47 | } 48 | 49 | if (n - 1 >= 0) { // 1 step 50 | printStairPaths(n - 1, path + "1"); // add 1 step 51 | } 52 | 53 | if (n - 2 >= 0) { // 2 steps 54 | printStairPaths(n - 2, path + "2"); // add 2 steps 55 | } 56 | 57 | if (n - 3 >= 0) { // 3 steps 58 | printStairPaths(n - 3, path + "3"); // add 3 steps 59 | } 60 | 61 | // solving in one loop 62 | // for (int step = 1; step <= 3; step++) { 63 | // if (n - step >= 0) { // 1 step 64 | // printStairPaths(n - step, path + step); // add path number of steps 65 | // } 66 | // } 67 | } 68 | 69 | } 70 | ``` 71 | -------------------------------------------------------------------------------- /level-1/lecture-014/first-last-index.md: -------------------------------------------------------------------------------- 1 | # First and Last Index 2 | 3 | > 1. You are given a number n, representing the size of array a. 4 | > 2. You are given n numbers, representing elements of array a. 5 | > Asssumption - Array is sorted. Array may have duplicate values. 6 | 7 | ```text 8 | Input Format 9 | A number n 10 | n1 11 | n2 12 | .. n number of elements 13 | A number d 14 | Output Format 15 | A number representing first index 16 | A number representing last index 17 | 18 | Constraints 19 | 1 <= n <= 1000 20 | 1 <= n1, n2, .. n elements <= 100 21 | 1 <= d <= 100 22 | Sample Input 23 | 15 24 | 1 25 | 5 26 | 10 27 | 15 28 | 22 29 | 33 30 | 33 31 | 33 32 | 33 33 | 33 34 | 40 35 | 42 36 | 55 37 | 66 38 | 77 39 | 33 40 | Sample Output 41 | 5 42 | 9 43 | ``` 44 | 45 | ```java 46 | import java.io.*; 47 | import java.util.*; 48 | 49 | public class Main{ 50 | 51 | public static void firstLastIndex(int[] arr, int d) { 52 | int start = 0, end = arr.length, fi = -1, li = -1; 53 | while (start <= end) { 54 | int mid = start + (end - start) / 2; 55 | if (d < arr[mid]) { 56 | end = mid - 1; 57 | } else if (d > arr[mid]) { 58 | start = mid + 1; 59 | } else { 60 | li = mid; 61 | start = mid + 1; 62 | } 63 | } 64 | start = 0; 65 | end = arr.length; 66 | while (start <= end) { 67 | int mid = start + (end - start) / 2; 68 | if (d < arr[mid]) { 69 | end = mid - 1; 70 | } else if (d > arr[mid]) { 71 | start = mid + 1; 72 | } else { 73 | fi = mid; 74 | end = mid - 1; 75 | } 76 | } 77 | System.out.println(fi); 78 | System.out.println(li); 79 | } 80 | public static void main(String[] args) throws Exception { 81 | Scanner sc = new Scanner(System.in); 82 | int n = sc.nextInt(); 83 | int[] arr = new int[n]; 84 | for (int i = 0; i < n; i++) { 85 | arr[i] = sc.nextInt(); 86 | } 87 | int d = sc.nextInt(); 88 | firstLastIndex(arr, d); 89 | sc.close(); 90 | } 91 | 92 | } 93 | ``` 94 | -------------------------------------------------------------------------------- /level-1/lecture-016/hw/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Main { 4 | 5 | // Wave Array Homework : 6 | public static void waveArray2(int[][] arr, int row, int col) { 7 | for (int j = 0; j < col; j++) { 8 | if (j % 2 != 0) { 9 | for (int i = 0; i < row; i++) { 10 | System.out.println(arr[i][j]); 11 | } 12 | } else { 13 | for (int i = row - 1; i >= 0; i--) { 14 | System.out.println(arr[i][j]); 15 | } 16 | } 17 | } 18 | } 19 | 20 | // Spiral Display Homework: 21 | public static void spiralMatrix2(int[][] arr, int row, int col) { 22 | int minrow = 0, maxrow = arr.length - 1, mincol = 0, maxcol = arr[0].length - 1; 23 | int count = 0, total = row * col; 24 | while (count < total) { 25 | for (int i = mincol; i <= maxcol && count < total; i++) { 26 | System.out.println(arr[minrow][i]); 27 | count++; 28 | } 29 | minrow++; 30 | for (int i = minrow; i <= maxrow && count < total; i++) { 31 | System.out.println(arr[i][maxcol]); 32 | count++; 33 | } 34 | maxcol--; 35 | for (int i = maxcol; i >= mincol && count < total; i--) { 36 | System.out.println(arr[maxrow][i]); 37 | count++; 38 | } 39 | maxrow--; 40 | for (int i = maxrow; i >= minrow && count < total; i--) { 41 | System.out.println(arr[i][mincol]); 42 | count++; 43 | } 44 | mincol++; 45 | } 46 | } 47 | 48 | public static void main(String[] args) { 49 | Scanner sc = new Scanner(System.in); 50 | int row = sc.nextInt(); 51 | int col = sc.nextInt(); 52 | int[][] arr = new int[row][col]; 53 | for (int i = 0; i < row; i++) { 54 | for (int j = 0; j < col; j++) { 55 | arr[i][j] = sc.nextInt(); 56 | } 57 | } 58 | // waveArray2(arr, row, col); 59 | spiralMatrix2(arr, row, col); 60 | sc.close(); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /level-1/archive/test-problems/intuit-test/002-tokyo-drift.md: -------------------------------------------------------------------------------- 1 | # Tokyo Drift 2 | 3 | `Easy` 4 | 5 | ``` 6 | There is a racing competition which will be held in your city next weekend. There are N racers who are going to take part in the competition. Each racer is at a given distance from the finish line, which is given in an integer array. 7 | 8 | Due to some safety reasons, total sum of speeds with which racers can drive is restricted with a given limit. Since, cost of organizing such an event depends on how long the event will last, you need to find out the minimum time in which all racers will cross the finishing line, but sum of speeds of all racers should be less than or equal to the given limit. 9 | 10 | If it is impossible to complete the race for all racers within given limit, we have to return -1. 11 | 12 | > Note: Speed is defined as Ceil (distance/time), where ceil represents smaller than or equal to. For example if distance is 20 km and time taken to travel is 3 hrs then speed equals ceil(20/3) i.e. 7 km/hr. 13 | 14 | Example: Let us take 4 Racers with Distances from finishing line as [15 km, 25 km, 5 km, 20 km], and the maximum sum of speeds allowed is 12 km/hr. The minimum time in which all racers will reach the finishing line will be 7 hours. 15 | 16 | Racer 1 will have 15 km / 7 hr = 3 km/hr speed 17 | Racer 2 will have 25 km / 7 hr = 4 km/hr speed 18 | Racer 3 will have 05 km / 7 hr = 1 km/hr speed 19 | Racer 4 will have 20 km / 7 hr = 3 km/hr speed 20 | 21 | Hence, the total sum of speeds will be (3 + 4 + 1 + 3) = 11 km/hr which is less than or equal to 12 km/hr. 22 | 23 | Constraints 24 | 25 | 1 <= N <= 100000 26 | 1 <= racers[i] <= 10000000 27 | 1 <= Limit <= 10000000 28 | 29 | Format 30 | 31 | Input 32 | 33 | First line contains an integer N representing length of array. 34 | Next line contains N space seprated integers representing distance from finishing line. 35 | Last line contains an integer representing limit of sum of speeds with which racers can drive 36 | 37 | Output 38 | 39 | A single line integer representing minimum time in which all racers will cross the finishing line, If not possible print -1 40 | 41 | Example 42 | 43 | Sample Input 44 | 45 | 4 46 | 15 25 5 20 47 | 12 48 | 49 | Sample Output 50 | 51 | 7 52 | ``` 53 | -------------------------------------------------------------------------------- /level-1/lecture-010/Arrays.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Arrays { 4 | 5 | public static void takeInput(int[] arr) { 6 | Scanner sc = new Scanner(System.in); 7 | for (int i = 0; i < arr.length; i++) { 8 | arr[i] = scn.nextInt(); // setter 9 | scn.close(); 10 | } 11 | } 12 | 13 | public static void printArray(int[] arr) { 14 | for (int i = 0; i < arr.length; i++) { 15 | System.out.print(arr[i] + " "); // getter 16 | } 17 | System.out.println(); 18 | } 19 | 20 | public static boolean findElement(int[] arr, int data) { 21 | for (int i = 0; i < arr.length; i++) { 22 | if (arr[i] == data) { 23 | return true; 24 | } 25 | } 26 | return false; 27 | } 28 | 29 | public static int maximum(int[] arr) { 30 | int max = Integer.MIN_VALUE; 31 | for (int i = 0; i < arr.length; i++) { 32 | if (arr[i] > max) { 33 | max = arr[i]; 34 | } 35 | } 36 | return max; 37 | } 38 | 39 | public static int minimum(int[] arr) { 40 | int min = Integer.MAX_VALUE; 41 | for (int i = 0; i < arr.length; i++) { 42 | if (arr[i] < min) { 43 | min = arr[i]; 44 | } 45 | } 46 | return min; 47 | } 48 | 49 | public static void firstIndex(int[] arr, int data) { 50 | for (int i = 0; i < arr.length; i++) { 51 | if (arr[i] == data) { 52 | System.out.println(i); 53 | break; 54 | } 55 | } 56 | } 57 | 58 | public static void lastIndex(int[] arr, int data) { 59 | for (int i = arr.length - 1; i >= 0; i--) { 60 | if (arr[i] == data) { 61 | System.out.println(i); 62 | break; 63 | } 64 | } 65 | } 66 | 67 | // understanding arrays in java 68 | public static void main(String[] args) { 69 | // declaring an array 70 | // int[] numbers = new int[5]; 71 | // declaring and initializing an array in one line 72 | // int[] numbers2 = { 1, 2, 3, 4, 5 }; 73 | } 74 | 75 | } 76 | // Home Work : 77 | // make : firstIndex(), lastIndex() of array -------------------------------------------------------------------------------- /level-1/lecture-018/search-in-a-sorted-2d-array.md: -------------------------------------------------------------------------------- 1 | # Search in 2D Array 2 | 3 | > 1. You are given a number n, representing the number of rows and columns of a square matrix. 4 | > 2. You are given n \* n numbers, representing elements of 2d array a. 5 | > `Note - Each row and column is sorted in increasing order.` 6 | > 3. You are given a number x. 7 | > 4. You are required to find x in the matrix and print it's location int (row, col) format as discussed in output format below. 8 | > 5. In case element is not found, print "Not Found". 9 | 10 | ``` 11 | Input Format 12 | A number n 13 | e11 14 | e12.. 15 | e21 16 | e22.. 17 | .. n * n number of elements of array a 18 | A number x 19 | Output Format 20 | row 21 | col of the location where element is found or "Not Found" if element is not in the matrix 22 | 23 | Constraints 24 | 1 <= n <= 10^2 25 | -10^9 <= e11, e12, .. n * m elements <= 10^9 26 | All rows and columns are sorted in increasing order 27 | Sample Input 28 | 4 29 | 11 30 | 12 31 | 13 32 | 14 33 | 21 34 | 22 35 | 23 36 | 24 37 | 31 38 | 32 39 | 33 40 | 34 41 | 41 42 | 42 43 | 43 44 | 44 45 | 43 46 | Sample Output 47 | 3 48 | 2 49 | ``` 50 | 51 | ## Solution 52 | 53 | ```java 54 | import java.io.*; 55 | import java.util.*; 56 | 57 | public class Main { 58 | 59 | public static void search2D(int[][] arr, int x) { 60 | // starting : top - right (optimal approach) 61 | int n = arr.length; // rows 62 | int i = 0; 63 | int j = n - 1; 64 | while (j >= 0 && i < n) { 65 | if (arr[i][j] == x) { 66 | System.out.println(i); 67 | System.out.println(j); 68 | return; 69 | } 70 | if (arr[i][j] > x) { 71 | j--; 72 | } else { 73 | i++; 74 | } 75 | } 76 | System.out.println("Not Found"); 77 | } 78 | 79 | public static void main(String[] args) throws Exception { 80 | Scanner scn = new Scanner(System.in); 81 | int n = scn.nextInt(); 82 | int[][] arr = new int[n][n]; 83 | for (int i = 0; i < n; i++) { 84 | for (int j = 0; j < n; j++) { 85 | arr[i][j] = scn.nextInt(); 86 | } 87 | } 88 | int x = scn.nextInt(); 89 | search2D(arr, x); 90 | scn.close(); 91 | } 92 | } 93 | ``` 94 | -------------------------------------------------------------------------------- /level-1/lecture-030/print-encodings.md: -------------------------------------------------------------------------------- 1 | # Print Encodings 2 | 3 | > 1. You are given a string str of digits. (will never start with a 0) 4 | > 2. You are required to encode the str as per following rules 5 | 6 | ``` 7 | 1 -> a 8 | 2 -> b 9 | 3 -> c 10 | .. 11 | 25 -> y 12 | 26 -> z 13 | ``` 14 | 15 | > 3. Complete the body of printEncodings function - without changing signature - to calculate and print all encodings of str. 16 | 17 | ``` 18 | Use the input-output below to get more understanding on what is required 19 | 123 -> abc, aw, lc 20 | 993 -> iic 21 | 013 -> Invalid input. A string starting with 0 will not be passed. 22 | 103 -> jc 23 | 303 -> No output possible. But such a string maybe passed. In this case print nothing. 24 | ``` 25 | 26 | > `Note -> The online judge can't force you to write the function recursively but that is what the spirit of question is. Write recursive and not iterative logic. The purpose of the question is to aid learning recursion and not test you.` 27 | 28 | ```text 29 | Input Format 30 | A string str 31 | Output Format 32 | Permutations of str in order hinted by Sample output 33 | 34 | Constraints 35 | 0 <= str.length <= 10 36 | Sample Input 37 | 655196 38 | 39 | Sample Output 40 | feeaif 41 | feesf 42 | ``` 43 | 44 | ## Solution 45 | 46 | ```java 47 | import java.io.*; 48 | import java.util.*; 49 | 50 | public class Main { 51 | 52 | // Print Encodings 53 | public static void printEncodings(String str, String ans) { 54 | if (str.length() == 0) { 55 | System.out.println(ans); 56 | return; // base case 57 | } 58 | if (str.charAt(0) == '0') { 59 | return; // since 01 != a, according to problem statement 60 | } 61 | char ch1 = str.charAt(0); 62 | int v1 = ch1 - '0'; 63 | printEncodings(str.substring(1), ans + (char) (v1 + 'a' - 1)); 64 | if (str.length() >= 2) { 65 | char ch2 = str.charAt(1); 66 | int v2 = ch2 - '0'; 67 | if (v1 * 10 + v2 <= 26) { 68 | printEncodings(str.substring(2), ans + (char) (v1 * 10 + v2 + 'a' - 1)); 69 | } 70 | } 71 | } 72 | 73 | public static void main(String[] args) { 74 | Scanner sc = new Scanner(System.in); 75 | String s = sc.next(); 76 | printEncodings(s, ""); 77 | sc.close(); 78 | } 79 | } 80 | ``` 81 | -------------------------------------------------------------------------------- /level-1/lecture-027/get-stair-path.md: -------------------------------------------------------------------------------- 1 | # Get Stair Paths 2 | 3 | > 1. You are given a number n representing number of stairs in a staircase. 4 | > 2. You are standing at the bottom of staircase. You are allowed to climb 1 step, 2 steps or 3 steps in one move. 5 | > 3. Complete the body of getStairPaths function - without changing signature - to get the list of all paths that can be used to climb the staircase up. 6 | > Use sample input and output to take idea about output. 7 | 8 | > `Note -> The online judge can't force you to write the function recursively but that is what the spirit of question is. Write recursive and not iterative logic. The purpose of the question is to aid learning recursion and not test you.` 9 | 10 | ``` 11 | Input Format 12 | A number n 13 | Output Format 14 | Contents of the arraylist containing paths as shown in sample output 15 | 16 | Constraints 17 | 0 <= n <= 10 18 | Sample Input 19 | 3 20 | Sample Output 21 | [111, 12, 21, 3] 22 | ``` 23 | 24 | ## Solution 25 | 26 | ```java 27 | import java.util.*; 28 | 29 | public class Main { 30 | 31 | public static void main(String[] args) throws Exception { 32 | Scanner sc = new Scanner(System.in); 33 | int n = sc.nextInt(); 34 | ArrayList ans = getStairPaths(n); 35 | System.out.println(ans); 36 | sc.close(); 37 | } 38 | 39 | public static ArrayList getStairPaths(int n) { 40 | if (n == 0) { // base case 41 | ArrayList base = new ArrayList(); 42 | base.add(""); 43 | return base; 44 | } 45 | ArrayList ans = new ArrayList(); 46 | if (n - 1 >= 0) { 47 | ArrayList nm1 = getStairPaths(n - 1); // get all possible paths for n-1 or 1 step 48 | for (String s : nm1) { 49 | ans.add("1" + s); 50 | } 51 | } 52 | if (n - 2 >= 0) { 53 | ArrayList nm2 = getStairPaths(n - 2); // get all possible paths for n-2 or 2 step 54 | for (String s : nm2) { 55 | ans.add("2" + s); 56 | } 57 | } 58 | if (n - 3 >= 0) { 59 | ArrayList nm3 = getStairPaths(n - 3); // get all possible paths for n-3 or 3 step 60 | for (String s : nm3) { 61 | ans.add("3" + s); 62 | } 63 | } 64 | return ans; 65 | } 66 | } 67 | ``` 68 | -------------------------------------------------------------------------------- /level-1/lecture-026/all-indices.md: -------------------------------------------------------------------------------- 1 | # All Indices Of Array 2 | 3 | > 1. You are given a number n, representing the count of elements. 4 | > 2. You are given n numbers. 5 | > 3. You are given a number x. 6 | > 4. You are required to find the all indices at which x occurs in array a. 7 | > 5. Return an array of appropriate size which contains all indices at which x occurs in array a. 8 | 9 | > `Note -> The online judge can't force you to write the function recursively but that is what the spirit of question is.` 10 | > `Write recursive and not iterative logic. The purpose of the question is to aid learning recursion and not test you.` 11 | 12 | ``` 13 | Input Format 14 | A number n 15 | n1 16 | n2 17 | .. n number of elements 18 | A number x 19 | Output Format 20 | Return the array of indices from the allIndices function. Display is managed for you. 21 | 22 | Constraints 23 | 1 <= n <= 10^4 24 | 0 <= n1, n2, .. n elements <= 10 ^ 3 25 | 0 <= x <= 10 ^ 3 26 | Sample Input 27 | 6 28 | 15 29 | 11 30 | 40 31 | 4 32 | 4 33 | 9 34 | 4 35 | Sample Output 36 | 3 37 | 4 38 | ``` 39 | 40 | ## Solution 41 | 42 | ```java 43 | import java.io.*; 44 | import java.util.*; 45 | 46 | public class Main { 47 | 48 | public static void main(String[] args) throws Exception { 49 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 50 | int n = Integer.parseInt(br.readLine()); 51 | int[] arr = new int[n]; 52 | 53 | for (int i = 0; i < n; i++) { 54 | arr[i] = Integer.parseInt(br.readLine()); 55 | } 56 | int x = Integer.parseInt(br.readLine()); 57 | int[] iarr = allIndices(arr, x, 0, 0); 58 | 59 | if(iarr.length == 0){ 60 | System.out.println(); 61 | return; 62 | } 63 | 64 | for(int i = 0; i < iarr.length; i++){ 65 | System.out.println(iarr[i]); 66 | } 67 | } 68 | 69 | // All Indices of target in Array using top down approach 70 | public static int[] allIndices(int[] arr, int target, int i, int count) { 71 | if (i == arr.length) { // Base Case 72 | int[] indices = new int[count]; 73 | return indices; 74 | } 75 | if (arr[i] == target) { 76 | count++; 77 | } 78 | int[] indices = allIndices(arr, target, i + 1, count); 79 | if (arr[i] == target) { 80 | indices[count - 1] = i; 81 | } 82 | return indices; 83 | } 84 | 85 | } 86 | ``` 87 | -------------------------------------------------------------------------------- /level-1/lecture-032/target-sum-subsets.md: -------------------------------------------------------------------------------- 1 | # Target Sum Subsets 2 | 3 | > 1. You are given a number n, representing the count of elements. 4 | > 2. You are given n numbers. 5 | > 3. You are given a number "tar". 6 | > 4. Complete the body of printTargetSumSubsets function - without changing signature - to calculate and print all subsets of given elements, the contents of which sum to "tar". Use sample input and output to get more idea. 7 | 8 | > `Note -> The online judge can't force you to write the function recursively but that is what the spirit of question is. Write recursive and not iterative logic. The purpose of the question is to aid learning recursion and not test you.` 9 | 10 | ``` 11 | Input Format 12 | A number n 13 | n1 14 | n2 15 | .. n number of elements 16 | A number tar 17 | Output Format 18 | Comma separated elements of the subset, the contents of which add to "tar" 19 | .. all such subsets, each in a single line (the elements of each subset should be comma separated) 20 | 21 | Constraints 22 | 1 <= n <= 30 23 | 0 <= n1, n2, .. n elements <= 20 24 | 0 <= tar <= 50 25 | Sample Input 26 | 5 27 | 10 28 | 20 29 | 30 30 | 40 31 | 50 32 | 60 33 | Sample Output 34 | 10, 20, 30, . 35 | 10, 50, . 36 | 20, 40, . 37 | ``` 38 | 39 | ## Solution 40 | 41 | ```java 42 | import java.util.*; 43 | 44 | public class Main { 45 | // ! Agenda 46 | // Problem : Target Sum Subsets 47 | // asf is answer so far 48 | // sum is sum of subset 49 | // tar is target 50 | public static void printTargetSumSubsets(int[] arr, int idx, String asf, int sum, int tar) { 51 | if (idx == arr.length) { // base case when we reach the end of the array 52 | if (tar == sum) { // if the sum of the subset is equal to the target 53 | System.out.println(asf + "."); // print the subset 54 | } 55 | return; // return out of the function 56 | } 57 | // picked 58 | printTargetSumSubsets(arr, idx + 1, asf + arr[idx] + ", ", sum + arr[idx], tar); 59 | // not picked 60 | printTargetSumSubsets(arr, idx + 1, asf, sum, tar); 61 | } 62 | 63 | public static void main(String[] args) { 64 | Scanner sc = new Scanner(System.in); 65 | int n = sc.nextInt(); 66 | int[] arr = new int[n]; 67 | for (int i = 0; i < n; i++) { 68 | arr[i] = sc.nextInt(); 69 | } 70 | int tar = sc.nextInt(); 71 | printTargetSumSubsets(arr, 0, "", 0, tar); 72 | sc.close(); 73 | } 74 | } 75 | ``` 76 | -------------------------------------------------------------------------------- /level-1/lecture-029/print-maze-path.md: -------------------------------------------------------------------------------- 1 | # Print Maze Paths 2 | 3 | > 1. You are given a number n and a number m representing number of rows and columns in a maze. 4 | > 2. You are standing in the top-left corner and have to reach the bottom-right corner. Only two moves are allowed 'h' (1-step horizontal) and 'v' (1-step vertical). 5 | > 3. Complete the body of pri tMazePath function - without changing signature - to print the list of all paths that can be used to move from top-left to bottom-right. 6 | 7 | > `Note -> The online judge can't force you to write the function recursively but that is what the spirit of question is. Write recursive and not iterative logic. The purpose of the question is to aid learning recursion and not test you.` 8 | 9 | ``` 10 | Input Format 11 | A number n 12 | A number m 13 | Output Format 14 | Print paths (one path in each line) in order hinted by Sample output 15 | 16 | Constraints 17 | 0 <= n <= 10 18 | 0 <= m <= 10 19 | Sample Input 20 | 2 21 | 2 22 | Sample Output 23 | hv 24 | vh 25 | ``` 26 | 27 | ## Solution 28 | 29 | ```java 30 | import java.io.*; 31 | import java.util.*; 32 | 33 | public class Main { 34 | 35 | public static void main(String[] args) throws Exception { 36 | Scanner sc = new Scanner(System.in); 37 | int n = sc.nextInt(); 38 | int m = sc.nextInt(); 39 | printMazePaths(0, 0, n - 1, m - 1, ""); 40 | } 41 | 42 | // sr - source row 43 | // sc - source column 44 | // dr - destination row 45 | // dc - destination column 46 | public static void printMazePaths(int sr, int sc, int dr, int dc, String psf) { 47 | 48 | if (sr == dr && sc == dc) { // base case 49 | System.out.println(psf); // print the answer 50 | return; 51 | } 52 | 53 | // ! Proactive call : when we call with cases 54 | 55 | if (sc + 1 <= dc) { // can move right 56 | printMazePaths(sr, sc + 1, dr, dc, psf + "h"); // add h to the path 57 | } 58 | if (sr + 1 <= dr) { // can move down 59 | printMazePaths(sr + 1, sc, dr, dc, psf + "v"); // add v to the path 60 | } 61 | 62 | // Either way we can call and handle negative cases 63 | // ! Reactive call : when we call without cases 64 | // if (sr > dr && sc > dc) { 65 | // return; 66 | // } 67 | // printMazePaths(sr, sc + 1, dr, dc, psf + "h"); // add h to the path 68 | // printMazePaths(sr + 1, sc, dr, dc, psf + "v"); // add v to the path 69 | } 70 | 71 | } 72 | ``` 73 | -------------------------------------------------------------------------------- /level-1/lecture-013/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Main { 4 | 5 | public static void reverse(int[] arr, int start, int end) { 6 | while (start < end) { 7 | int temp = arr[start]; 8 | arr[start] = arr[end]; 9 | arr[end] = temp; 10 | start++; 11 | end--; 12 | } 13 | } 14 | 15 | public static void rotate(int[] arr, int k) { 16 | k = (k > 0) ? k % arr.length : (k % arr.length) + arr.length; 17 | reverse(arr, 0, arr.length - k - 1); 18 | reverse(arr, arr.length - k, arr.length - 1); 19 | reverse(arr, 0, arr.length - 1); 20 | } 21 | 22 | public static int[] inverse(int[] arr) { 23 | int[] ans = new int[arr.length]; 24 | for (int i = 0; i < arr.length; i++) { 25 | int val = arr[i]; 26 | ans[val] = i; 27 | } 28 | return ans; 29 | } 30 | 31 | /* 32 | * Common misconceptions : subarray, subsequence 33 | * Subarray : continious segment of an array 34 | * Its unidirectional 35 | * Total numbers of subarray : n * (n - 1) / 2 36 | * 37 | * Subsequence : It may or may not be continious segment of an array 38 | * Its unidirectional 39 | * Can be analysed by writing binary numbers of length n 40 | * Total numbers of subsequence : 2 ^ n 41 | * 42 | * 43 | * NOTE : "All subarrays are subsequences!" 44 | */ 45 | 46 | public static void subarray(int[] arr) { 47 | for (int start = 0; start < arr.length; start++) { 48 | for (int end = start; end < arr.length; end++) { 49 | for (int i = start; i <= end; i++) { 50 | System.out.print(arr[i] + "\t"); 51 | } 52 | System.out.println(); 53 | } 54 | } 55 | } 56 | 57 | public static void main(String[] args) { 58 | Scanner sc = new Scanner(System.in); 59 | int n = sc.nextInt(); 60 | int[] arr = new int[n]; 61 | for (int i = 0; i < n; i++) { 62 | arr[i] = sc.nextInt(); 63 | } 64 | // int k = sc.nextInt(); 65 | // rotate(arr, k); 66 | // for (int i = 0; i < n; i++) { 67 | // System.out.print(arr[i] + " "); 68 | // } 69 | 70 | // int[] ans = inverse(arr); 71 | // for (int i = 0; i < n; i++) { 72 | // System.out.print(ans[i] + " "); 73 | // } 74 | subarray(arr); 75 | sc.close(); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /level-1/lecture-049/README.md: -------------------------------------------------------------------------------- 1 | # Lecture 49 2 | 3 | ## `Priority Queue` in Java 4 | 5 | - [PriorityQueue](https://docs.oracle.com/javase/7/docs/api/java/util/PriorityQueue.html) 6 | - PriorityQueue is a class that implements the Queue interface and provides a method for retrieving and removing the highest priority element. 7 | - There are two types of Priority Queues : min/max priority queues 8 | - By default java uses min priority queue 9 | 10 | ## Member functions of PriorityQueue 11 | 12 | - `pq.add(e)` : adds an element to the priority queue 13 | - `pq.peek()` : returns the highest priority element 14 | - `pq.poll()` : returns and removes the highest priority element 15 | - `pq.size()` : returns the number of elements in the priority queue 16 | - `pq.remove(e)` : removes an element from the priority queue 17 | - `pq.contains(e)` : returns true if the priority queue contains the specified element 18 | - `pq.clear()` : removes all of the elements from the priority queue 19 | 20 | ## Demo for Priority Queue 21 | 22 | ```java 23 | public static void pq() { 24 | int[] a = { 1, 21, 57, 92, 43, 22, 21, 6 }; 25 | // Initialising a PriorityQueue of type int : by default min PQ 26 | PriorityQueue pq = new PriorityQueue(); 27 | for (int i : a) { 28 | pq.add(a[i]); // adding elements to PQ 29 | // cost of add() is O(log n) where n is the size of PQ 30 | // for n elements is O(n log n) 31 | } 32 | while (!pq.isEmpty()) { 33 | System.out.println(pq.remove()); // removing elements from PQ 34 | // cost of remove() is O(log n) where n is the size of PQ 35 | } 36 | // printing elements in sorted order 37 | while (!pq.isEmpty()) { 38 | System.out.println(pq.peek()); 39 | pq.remove(); 40 | } 41 | } 42 | ``` 43 | 44 | ## Three options to make Max Priority Queue : Lambda Expression, Collections.reverseOrder() and Comparator Interface 45 | 46 | - We choose to do it with `Collections.reverseOrder()` 47 | 48 | ```java 49 | import java.util.Collection; 50 | 51 | // the code makes a max Priotity queue 52 | PriorityQueue pq = new PriorityQueue(Collections.reverseOrder()); 53 | ``` 54 | 55 | ## Time Complexity for operations in Priority Queue 56 | 57 | | Operation | Time Complexity | 58 | | :--------- | :-------------- | 59 | | add() | O(log n) | 60 | | peek() | O(1) | 61 | | poll() | O(log n) | 62 | | remove() | O(log n) | 63 | | contains() | O(1) | 64 | | clear() | O(n) | 65 | -------------------------------------------------------------------------------- /level-1/lecture-048/get-common-elements-2.md: -------------------------------------------------------------------------------- 1 | # Get Common Elements - 2 2 | 3 | `Easy` 4 | 5 | > 1. You are given a number n1, representing the size of array a1. 6 | > 2. You are given n1 numbers, representing elements of array a1. 7 | > 3. You are given a number n2, representing the size of array a2. 8 | > 4. You are given n2 numbers, representing elements of array a2. 9 | > 5. You are required to find the intersection of a1 and a2. To get an idea check the example below: 10 | 11 | - if a1 -> 1 1 2 2 2 3 5 12 | - and a2 -> 1 1 1 2 2 4 5 13 | - intersection is -> 1 1 2 2 5 14 | 15 | `Note -> Don't assume the arrays to be sorted. Check out the question video.` 16 | 17 | ## Constraints 18 | 19 | - 1 <= n1, n2 <= 100 20 | - 0 <= a1[i], a2[i] < 10 21 | `Time complexity should be O(n)` 22 | 23 | ## Format 24 | 25 | ## Input 26 | 27 | - A number n1 28 | - n1 number of elements line separated 29 | - A number n2 30 | - n2 number of elements line separated 31 | 32 | ## Output 33 | 34 | - All relevant elements of intersection in separate lines 35 | - The elements of intersection should be printed in order of their occurence in a2. 36 | 37 | ## Example 38 | 39 | ## Sample Input 40 | 41 | ``` 42 | 7 43 | 1 44 | 1 45 | 2 46 | 2 47 | 2 48 | 3 49 | 5 50 | 7 51 | 1 52 | 1 53 | 1 54 | 2 55 | 2 56 | 4 57 | 5 58 | ``` 59 | 60 | ## Sample Output 61 | 62 | ``` 63 | 1 64 | 1 65 | 2 66 | 2 67 | 5 68 | ``` 69 | 70 | ## Solution 71 | 72 | ```java 73 | import java.io.*; 74 | import java.util.*; 75 | 76 | public class GetCommonElement2 { 77 | 78 | public static void main(String[] args) throws Exception { 79 | Scanner scn = new Scanner(System.in); 80 | int n1 = scn.nextInt(); 81 | int[] a = new int[n1]; 82 | for (int i = 0; i < n1; i++) 83 | a[i] = scn.nextInt(); 84 | 85 | int n2 = scn.nextInt(); 86 | int[] b = new int[n2]; 87 | for (int i = 0; i < n2; i++) 88 | b[i] = scn.nextInt(); 89 | 90 | // FreqMap of A array 91 | HashMap hm = new HashMap<>(); 92 | for (int ele : a) 93 | hm.put(ele, hm.getOrDefault(ele, 0) + 1); 94 | 95 | for (int ele : b) { 96 | if (hm.containsKey(ele) && hm.get(ele) > 0) { 97 | System.out.println(ele); 98 | 99 | int oldFreq = hm.get(ele); 100 | int newFreq = oldFreq - 1; 101 | hm.put(ele, newFreq); 102 | } 103 | } 104 | 105 | } 106 | 107 | } 108 | ``` 109 | -------------------------------------------------------------------------------- /level-1/lecture-012/sum-of-two-arrays.md: -------------------------------------------------------------------------------- 1 | # Sum Of Two Arrays 2 | 3 | > 1. You are given a number n1, representing the size of array a1. 4 | > 2. You are given n1 numbers, representing elements of array a1. 5 | > 3. You are given a number n2, representing the size of array a2. 6 | > 4. You are given n2 numbers, representing elements of array a2. 7 | > 5. The two arrays represent digits of two numbers. 8 | > 6. You are required to add the numbers represented by two arrays and print the arrays. 9 | 10 | ``` 11 | Input Format 12 | A number n1 13 | n1 number of elements line separated 14 | A number n2 15 | n2 number of elements line separated 16 | Output Format 17 | A number representing sum of two numbers, represented by two arrays. 18 | 19 | Constraints 20 | 1 <= n1, n2 <= 100 21 | 0 <= a1[i], a2[i] < 10 22 | Sample Input 23 | 5 24 | 3 25 | 1 26 | 0 27 | 7 28 | 5 29 | 6 30 | 1 31 | 1 32 | 1 33 | 1 34 | 1 35 | 1 36 | Sample Output 37 | 1 38 | 4 39 | 2 40 | 1 41 | 8 42 | 6 43 | ``` 44 | 45 | ## Solution 46 | 47 | ```java 48 | import java.util.Scanner; 49 | 50 | public class Main { 51 | public static void main(String[] args) { 52 | Scanner sc = new Scanner(System.in); 53 | int n1 = sc.nextInt(); 54 | int a1[] = new int[n1]; 55 | for (int i = 0; i < a1.length; i++) { 56 | a1[i] = sc.nextInt(); 57 | } 58 | int n2 = sc.nextInt(); 59 | int a2[] = new int[n2]; 60 | for (int i = 0; i < a2.length; i++) { 61 | a2[i] = sc.nextInt(); 62 | } 63 | sumOfTwoArrays(a1, a2); 64 | sc.close(); 65 | } 66 | 67 | public static void sumOfTwoArrays(int a1[], int a2[]) { 68 | int n1 = a1.length; 69 | int n2 = a2.length; 70 | int[] sum = new int[(n1 > n2) ? n1 : n2]; 71 | int c = 0; 72 | int i = a1.length - 1; // index of a1 73 | int j = a2.length - 1; // index of a2 74 | int k = sum.length - 1; // index of sum 75 | while (k >= 0) { 76 | int d = c; 77 | if (i >= 0) { 78 | d += a1[i]; 79 | } 80 | if (j >= 0) { 81 | d += a2[j]; 82 | } 83 | c = d / 10; 84 | d = d % 10; 85 | sum[k] = d; 86 | i--; 87 | j--; 88 | k--; 89 | } 90 | 91 | if (c != 0) { 92 | System.out.println(c); 93 | } 94 | for (int val : sum) { 95 | System.out.println(val); 96 | } 97 | } 98 | 99 | } 100 | ``` 101 | -------------------------------------------------------------------------------- /level-1/lecture-048/get-common-elements-1.md: -------------------------------------------------------------------------------- 1 | # Get Common Elements - 1 2 | 3 | `Easy` 4 | 5 | > 1. You are given a number n1, representing the size of array a1. 6 | > 2. You are given n1 numbers, representing elements of array a1. 7 | > 3. You are given a number n2, representing the size of array a2. 8 | > 4. You are given n2 numbers, representing elements of array a2. 9 | > 5. You are required to print all elements of a2 which are also present in a1 (in order of their occurence in a2). Make sure to not print duplicates (a2 may have same value present many times). 10 | 11 | ## Constraints 12 | 13 | > 1 <= n1, n2 <= 100 14 | > 0 <= a1[i], a2[i] < 10 15 | 16 | `Time complexity should be O(n)` 17 | 18 | ## Format 19 | 20 | ## Input 21 | 22 | > A number n1 23 | > n1 number of elements line separated 24 | > A number n2 25 | > n2 number of elements line separated 26 | 27 | ## Output 28 | 29 | > All relevant elements of a2 in separate lines (no duplicacy) 30 | 31 | ## Example 32 | 33 | > Sample Input 34 | 35 | ``` 36 | 9 37 | 5 38 | 5 39 | 9 40 | 8 41 | 5 42 | 5 43 | 8 44 | 0 45 | 3 46 | 18 47 | 9 48 | 7 49 | 1 50 | 0 51 | 3 52 | 6 53 | 5 54 | 9 55 | 1 56 | 1 57 | 8 58 | 0 59 | 2 60 | 4 61 | 2 62 | 9 63 | 1 64 | 5 65 | ``` 66 | 67 | ## Sample Output 68 | 69 | ``` 70 | 9 71 | 0 72 | 3 73 | 5 74 | 8 75 | ``` 76 | 77 | ## Solution 78 | 79 | ```java 80 | import java.io.*; 81 | import java.util.*; 82 | 83 | public class GetCommonElements { 84 | 85 | public static void main(String[] args) throws Exception { 86 | Scanner sc = new Scanner(System.in); 87 | int n1 = sc.nextInt(); 88 | int a1[] = new int[n1]; 89 | for (int i = 0; i < n1; i++) { 90 | a1[i] = sc.nextInt(); 91 | } 92 | int n2 = sc.nextInt(); 93 | int a2[] = new int[n2]; 94 | for (int i = 0; i < n2; i++) { 95 | a2[i] = sc.nextInt(); 96 | } 97 | // frequency map of a1[] 98 | HashMap hm = new HashMap(); 99 | for (int ele : a1) { 100 | hm.put(ele, hm.getOrDefault(ele, 0) + 1); // it will add the element if it is not present in the map 101 | } 102 | // frequency map of a2[] 103 | for (int ele : a2) { 104 | if (hm.containsKey(ele)) { 105 | System.out.println(ele); 106 | // remove 107 | hm.remove(ele); // it will remove the element if it is present in the map 108 | } 109 | } 110 | sc.close(); 111 | } 112 | } 113 | ``` 114 | -------------------------------------------------------------------------------- /level-1/lecture-024/tower-of-hanoi.md: -------------------------------------------------------------------------------- 1 | # Tower Of Hanoi 2 | 3 | > 1. There are 3 towers. Tower 1 has n disks, where n is a positive number. Tower 2 and 3 are empty. 4 | > 2. The disks are increasingly placed in terms of size such that the smallest disk is on top and largest disk is at bottom. 5 | > 3. You are required to 6 | > 3.1. Print the instructions to move the disks. 7 | > 3.2. from tower 1 to tower 2 using tower 3 8 | > 3.3. following the rules 9 | > 3.3.1 move 1 disk at a time. 10 | > 3.3.2 never place a smaller disk under a larger disk. 11 | > 3.3.3 you can only move a disk at the top. 12 | 13 | >`Note -> The online judge can't force you to write the function recursively but that is what the spirit of question is.Write recursive and not iterative logic. The purpose of the question is to aid learning recursion and not test you.` 14 | 15 | ```text 16 | Input Format 17 | A number n, representing number of disks 18 | A number n1, representing id of tower 1 19 | A number n2, representing id of tower 2 20 | A number n3, representing id of tower 3 21 | Output Format 22 | n[n1 -> n2] 23 | .. 24 | A set of instructions in above format to represent, move nth disc from n1 tower to n2 tower 25 | 26 | Constraints 27 | 0 <= n <= 9 28 | 10 <= n1, n2, n3 <= 10^9 29 | n1 != n2 != n3 30 | Sample Input 31 | 3 32 | 10 33 | 11 34 | 12 35 | Sample Output 36 | 1[10 -> 11] 37 | 2[10 -> 12] 38 | 1[11 -> 12] 39 | 3[10 -> 11] 40 | 1[12 -> 10] 41 | 2[12 -> 11] 42 | 1[10 -> 11] 43 | ``` 44 | 45 | ## Notes 46 | 47 | ![Screenshot 2021-12-15 at 10 06 22 PM](https://user-images.githubusercontent.com/28717686/146226915-5e2b5fed-cfa6-4ece-bdc5-d78be81f6660.png) 48 | 49 | ## Simulation 50 | 51 | ![toh](https://user-images.githubusercontent.com/28717686/146224597-95c616f3-0ad2-440a-aa32-f2a8cd237d86.gif) 52 | 53 | [`💡 Try Simulation Here`](http://towersofhanoi.info/Animate.aspx) 54 | 55 | ## Solution 56 | 57 | ```java 58 | import java.util.Scanner; 59 | 60 | public class Main { 61 | // tower of hanoi 62 | 63 | public static void toh(int n, int t1, int t2, int t3) { 64 | if (n == 0) { 65 | return; 66 | } 67 | toh(n - 1, t1, t3, t2); 68 | System.out.println(n + "[" + t1 + " -> " + t2 + "]"); 69 | toh(n - 1, t3, t2, t1); 70 | } 71 | 72 | public static void main(String[] args) { 73 | Scanner sc = new Scanner(System.in); 74 | int n = sc.nextInt(); 75 | int t1 = sc.nextInt(); 76 | int t2 = sc.nextInt(); 77 | int t3 = sc.nextInt(); 78 | toh(n, t1, t2, t3); // t1 -> t2 using t3 79 | sc.close(); 80 | } 81 | } 82 | 83 | ``` 84 | -------------------------------------------------------------------------------- /level-1/lecture-025/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Main { 4 | 5 | // Max of Array using Recursion 6 | public static int maxOfArray(int[] arr, int i) { 7 | if (i == arr.length - 1) { 8 | return arr[i]; 9 | } 10 | int recAns = maxOfArray(arr, i + 1); 11 | int me = arr[i]; 12 | if (me > recAns) { 13 | return me; 14 | } 15 | return recAns; 16 | } 17 | 18 | // First Index of Array using Recursion 19 | public static int firstIndexOfArray(int[] arr, int i, int target) { 20 | if (i == arr.length) { 21 | return -1; 22 | } 23 | if (arr[i] == target) { 24 | return i; 25 | } 26 | return firstIndexOfArray(arr, i + 1, target); 27 | } 28 | 29 | // Last Index of Array using Recursion 30 | // Bottom-up approach (simple) 31 | public static int lastIndexOfArray(int[] arr, int i, int target) { 32 | if (i == -1) { 33 | return -1; 34 | } 35 | if (arr[i] == target) { 36 | return i; 37 | } 38 | return lastIndexOfArray(arr, i - 1, target); 39 | } 40 | 41 | // Top-down approach (complex - just to show the idea of top down approach) 42 | public static int lastIndexOfArrayTopDown(int[] arr, int i, int target) { 43 | if (i == arr.length) { 44 | return -1; 45 | } 46 | // faith 47 | int li = lastIndexOfArrayTopDown(arr, i + 1, target); 48 | if (li == -1) { 49 | if (arr[i] == target) { 50 | return i; 51 | } else { 52 | return li; 53 | } 54 | } else { // li != -1 (li is the last index of target) 55 | // we got our last index of target 56 | return li; 57 | } 58 | } 59 | 60 | // All Indices of Array using Recursion (Home-work) 61 | // public static int[] allIndices(int[] arr, int i, int target) { 62 | // } 63 | 64 | public static void main(String[] args) { 65 | Scanner sc = new Scanner(System.in); 66 | int n = sc.nextInt(); 67 | int[] arr = new int[n]; 68 | for (int i = 0; i < n; i++) { 69 | arr[i] = sc.nextInt(); 70 | } 71 | // System.out.println(maxOfArray(arr, 0)); 72 | int target = sc.nextInt(); 73 | // System.out.println(firstIndexOfArray(arr, 0, target)); 74 | // System.out.println(lastIndexOfArray(arr, n - 1, target)); // Bottom-up 75 | System.out.println(lastIndexOfArrayTopDown(arr, 0, target)); // Top-down 76 | sc.close(); 77 | } 78 | } --------------------------------------------------------------------------------