├── Lecture011 Time & Space Complexity ├── images.png └── Complexity.jpeg ├── Lecture059 Stack Interview Question-5 └── Coding Questions.md ├── Lecture002 Write First C++ Program └── firstProgram.cpp ├── Lecture068 Minimum Time to BURN the Entire Binary Tree └── Coding Question.md ├── Lecture001 Introduction, Flow Charts, PseudoCode, Programming and Why ├── flowchart.png └── Readme.md ├── Lecture072 Merge 2 Binary Search Trees └── Coding Question.md ├── Lecture056 Stack Interview Question-2 └── Coding Questions.md ├── Lecture084 Sudoku Solver Problem Backtracking Day 3 └── Coding Question.md ├── Lecture003 If-Else, While loop & Lots of Patterns ├── whileLoop.cpp ├── sumOfN.cpp ├── pattern2.cpp ├── pattern1.cpp ├── primeNumber.cpp └── conditionals.cpp ├── Lecture095 Dijkstra's Algorithm └── Coding Question.md ├── Lecture030 Macros, Gloab Variable etc ├── macros.cpp ├── Homework.cpp ├── defaultArgs.cpp ├── inlineFunctions.cpp └── globalVariable.cpp ├── Lecture008 Switch Statement & Functions ├── Homework1.cpp ├── passByValue.cpp ├── counting.cpp ├── power.cpp ├── Homework2.cpp ├── Homework3.cpp ├── evenOdd.cpp ├── nCr.cpp ├── primeNumber.cpp ├── switch.cpp └── calculator.cpp ├── Lecture083 N-Queen Problem Backtracking Day 2 └── Coding Question.md ├── Lecture031 Recursion Day1 ├── factorial_simple.cpp ├── counting.cpp ├── power.cpp └── factroial.cpp ├── Lecture009 Introduction to Arrays in C++ ├── Homework_1.cpp ├── Homework_2.cpp ├── linearSeaerch.cpp ├── reverse.cpp ├── arrayScope.cpp ├── maxMin.cpp └── arrayIntro.cpp ├── Lecture005 Bitwise Operators, For Loops, Operator Precedence & Variable Scoping ├── continue.cpp ├── EvenOrOdd.cpp ├── forSum.cpp ├── forFib.cpp ├── forLoop.cpp ├── forPrime.cpp ├── bitwiseOperators.cpp └── Coding Question.md ├── Lecture022 Strings ├── reverseString.cpp ├── removeOccurrences.cpp ├── replaceSpaces.cpp ├── validPalindrome.cpp └── intro.cpp ├── Lecture006 Binary & Decimal Number System ├── Optimize_binTodec.cpp ├── decToBin.cpp ├── binToDec.cpp └── Negative_decTobin.cpp ├── Lecture032 Recursion Day2 ├── Fibbonacci.cpp ├── climbStairs.cpp ├── walkingExample.cpp └── SayDigits.cpp ├── Lecture043 OOPs Day2 ├── singleInheritance.cpp ├── Encapsulation.cpp ├── multiLevelInheritance.cpp ├── ambiguity.cpp ├── multipleInheritance.cpp ├── hierarchicalInheritance.cpp ├── HybridInheritance.cpp ├── inheritance.cpp └── polymorphism.cpp ├── Lecture055 Stack Interview Question └── reverseString.cpp ├── Lecture024 Maths for DSA ├── GCD.java ├── FactorsOfANumber.java ├── LCM.java ├── fastExpo.cpp ├── primeNumberBrute.cpp ├── gcd.cpp ├── NewtonsSquareRoot.java ├── sieve.cpp ├── PrimeNumber.java ├── SieveOfEratosthenes.java └── SquareRootOfANumber.java ├── README.md ├── Lecture004 Solving Pattern Questions (Part-2) ├── pattern1.cpp ├── pattern3.cpp ├── pattern6.cpp ├── pattern9.cpp ├── pattern2.cpp ├── pattern5.cpp ├── pattern8.cpp ├── pattern17.cpp ├── pattern4.cpp ├── pattern13.cpp ├── pattern10.cpp ├── pattern11.cpp ├── pattern7.cpp ├── pattern12.cpp ├── pattern14.cpp ├── pattern15.cpp ├── pattern19.cpp ├── pattern18.cpp ├── pattern22.cpp ├── pattern21.cpp ├── pattern16.cpp ├── pattern20.cpp └── pattern23.cpp ├── Lecture026 Pointers with Arrays and Functions ├── pointer_characterArray.cpp ├── pointer_functions.cpp └── pointer_array.cpp ├── Lecture021 Solving Array Questions 2 ├── rotateArray.cpp ├── checkSortedRotatedArray.cpp └── addArray.cpp ├── Lecture034 Recursion Day4 ├── HomeWork1.cpp ├── reverseString.cpp ├── exponent.cpp ├── HomeWork2.cpp ├── bubbleSort.cpp └── palindromeCheck.cpp ├── Lecture017 Bubble Sort ├── Coding Question.md ├── Bubble_Sort.cpp └── bubbleSort.cpp ├── Lecture069 Binary Search Tree and its Implementation ├── Coding Question.md ├── max-min.cpp ├── BST.cpp └── IPPtraversal.cpp ├── Lecture033 Recursion Day3 ├── arraysum.cpp ├── sortedArray.cpp ├── binarySearch.cpp └── linearSearch.cpp ├── Lecture037 Recursion Day7 ├── subsequences.cpp └── subsets.cpp ├── Lecture054 Introduction to Stacks ├── stack.cpp ├── Coding Question.md └── stackarray.cpp ├── Lecture014 Binary Search Interview Questions ├── pivot.cpp ├── sqrt.cpp └── Coding Question.md ├── Lecture023 2D arrays ├── Search2DMatrix2.java ├── Search2DMatrix1.java ├── search2DMatrix2.cpp ├── wavePrint.cpp ├── WavePrint.java ├── search2DMatrix1.cpp ├── SpiralPrint.java ├── spiralPrint.cpp └── 2dArrayIntro.cpp ├── Lecture016 Selection Sort ├── selectionSort.cpp └── Coding Question.md ├── Lecture018 Insertion Sort ├── insertionSort.cpp └── Coding Question.md ├── Lecture060 Introduction to the Queues ├── dequeue.cpp ├── queue.cpp └── queuearray.cpp ├── Lecture010 Solving LeetCode CodeStudio Questions Arrays ├── swapAlternates.cpp └── sort_01.cpp ├── Lecture025 Intro to Pointers in C++ ├── intro.cpp ├── CodeStudio.cpp └── play.cpp ├── Lecture020 Solving LeetCode CodeStudio Questions [Arrays] ├── MoveZeroes.cpp ├── reverseArray.cpp ├── mergeSortedArray1.cpp └── Coding Question.md ├── Lecture078 Hashmaps in C++ ├── Coding Question.md └── unorder_map.cpp ├── Lecture028 Dynamic Allocation (Part-1) ├── dynamicArray.cpp └── ReferenceVariable.cpp ├── Lecture019 C++ STL └── STL.md ├── Lecture012 Binary Search ├── binarySearch.cpp └── Optimize_binarySearch.cpp ├── Lecture045 Linked List Day2 ├── ReverseLL.cpp ├── ReverseLLRecursion.cpp └── Homework.cpp ├── Lecture074 Heaps in C++ ├── Coding Question.md ├── priority_queue.cpp ├── HeapSort.cpp └── heaps.cpp ├── Lecture085 Introduction to Graphs ├── Coding Question.md ├── GraphList.cpp └── graphTemplete.cpp ├── Lecture029 Dynamic Allocation (Part-2) └── 2dArray.cpp ├── Lecture086 BFS Traversal in Graph └── Coding Question.md ├── Lecture038 Recursion Day8 └── keypad.cpp ├── Lecture092 Cycle Detection in Directed Graph (Using BFS) └── Coding Question.md ├── Lecture090 Topological Sort ( Using DFS ) └── Coding Question.md ├── Lecture087 DFS Traversal in Graph └── Coding Question.md ├── Lecture007 LeetCode Problem Solving Session └── Coding Question.md ├── Lecture027 Double Pointers and MCQs └── doublePointer.cpp ├── Lecture036 Recursion Day6 └── quickSort.cpp ├── Lecture091 Topological Sort (Using Kahn's Algorithm) └── Coding Question.md ├── Lecture070 Binary SearchTree FAANG Interview Questions-1 └── Coding Question.md ├── Lecture013 Binary Search Interview Questions ├── TotalNumberOfOccurence.cpp └── Coding Question.md ├── Lecture093 Shortest Path in Undirected Graphs └── Coding Question.md ├── Lecture089 Cycle Detection in Directed Graphs └── Coding Question.md ├── Lecture058 Stack Interview Question-4 └── Coding Questions.md ├── Lecture035 Recursion Day5 └── mergeSort.cpp ├── Lecture082 Rat in Maze Problem Backtracking Day 1 └── Coding Question.md ├── Lecture062 Binary Trees and its Representation ├── binarytree.cpp └── IPPtraversal.cpp ├── Lecture057 Stack Interview Question-3 └── Coding Questions.md ├── Lecture076 Heap Interview Question-2 └── Coding Question.md ├── Lecture094 Shortest Path in Directed Acyclic Graphs └── SPinDAG.cpp ├── Lecture079 Trie & its Implementation ├── trieintro.cpp └── Coding Question.md ├── Lecture061 Queue Faang Interview Question └── Kqueues.cpp ├── Lecture066 Construct a Binary Tree Using Inorder, Preorder, Postorder Traversal └── Coding Question.md ├── Lecture044 Linked List Day1 ├── circularLinkedList.cpp └── singlyLinkedList.cpp └── Lecture088 Cycle Detection in Undirected Graphs └── Coding Question.md /Lecture011 Time & Space Complexity/images.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vishalparmarr/CodeHelp-DSA-Busted-Series/HEAD/Lecture011 Time & Space Complexity/images.png -------------------------------------------------------------------------------- /Lecture059 Stack Interview Question-5/Coding Questions.md: -------------------------------------------------------------------------------- 1 | # Coding Question 2 | 3 | 1. Special Stack: [https://bit.ly/3LpVPJM] 4 | 5 | ``` cpp 6 | 1. 7 | ``` 8 | -------------------------------------------------------------------------------- /Lecture002 Write First C++ Program/firstProgram.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | cout << "Namaste Dunia" << endl; 6 | } 7 | -------------------------------------------------------------------------------- /Lecture011 Time & Space Complexity/Complexity.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vishalparmarr/CodeHelp-DSA-Busted-Series/HEAD/Lecture011 Time & Space Complexity/Complexity.jpeg -------------------------------------------------------------------------------- /Lecture068 Minimum Time to BURN the Entire Binary Tree/Coding Question.md: -------------------------------------------------------------------------------- 1 | # Coding Quesion 2 | 3 | 1. Burning Tree: [https://practice.geeksforgeeks.org/problems/burning-tree/1] 4 | 5 | ```cpp 6 | 1. 7 | ``` 8 | -------------------------------------------------------------------------------- /Lecture001 Introduction, Flow Charts, PseudoCode, Programming and Why/flowchart.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vishalparmarr/CodeHelp-DSA-Busted-Series/HEAD/Lecture001 Introduction, Flow Charts, PseudoCode, Programming and Why/flowchart.png -------------------------------------------------------------------------------- /Lecture072 Merge 2 Binary Search Trees/Coding Question.md: -------------------------------------------------------------------------------- 1 | # Coding Quesion 2 | 3 | 1. Merge Two BSTs: [https://www.codingninjas.com/studio/problems/h_920474?leftPanelTab=0&utm_source=youtube&utm_medium=affiliate&utm_campaign=Lovebabbar] 4 | 5 | ``` cpp 6 | 1. 7 | ``` 8 | -------------------------------------------------------------------------------- /Lecture056 Stack Interview Question-2/Coding Questions.md: -------------------------------------------------------------------------------- 1 | # Coding Question 2 | 3 | 1. Next Smaller Element: [https://bit.ly/34GjYL4] 4 | 2. Largest Rectangular Area in Histogram: [https://leetcode.com/problems/largest-rectangle-in-histogram] 5 | 6 | ``` cpp 7 | 1. 8 | ``` 9 | -------------------------------------------------------------------------------- /Lecture084 Sudoku Solver Problem Backtracking Day 3/Coding Question.md: -------------------------------------------------------------------------------- 1 | # Coding Quesion 2 | 3 | 1. Sudoku Solver: [https://www.codingninjas.com/studio/problems/sudoku-solver_699919?leftPanelTab=0&utm_source=youtube&utm_medium=affiliate&utm_campaign=Lovebabbar] 4 | 5 | ``` cpp 6 | 1. 7 | ``` 8 | -------------------------------------------------------------------------------- /Lecture003 If-Else, While loop & Lots of Patterns/whileLoop.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | int n; 7 | cin >> n; 8 | 9 | int i = 1; 10 | 11 | while (i <= n) { 12 | cout << i << " "; 13 | i++; 14 | } 15 | } -------------------------------------------------------------------------------- /Lecture095 Dijkstra's Algorithm/Coding Question.md: -------------------------------------------------------------------------------- 1 | # Coding Quesion 2 | 3 | 1. Dijkstra's shortest path: [https://www.codingninjas.com/studio/problems/dijkstra-s-shortest-path_920469?leftPanelTab=0&utm_source=youtube&utm_medium=affiliate&utm_campaign=Lovebabbar] 4 | 5 | ``` cpp 6 | 1. 7 | ``` 8 | -------------------------------------------------------------------------------- /Lecture030 Macros, Gloab Variable etc/macros.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define PI 3.14 5 | 6 | int main() { 7 | 8 | int r = 5; 9 | //double pi = 3.14; 10 | 11 | double area = PI * r * r; 12 | 13 | cout << "Area is " << area << endl; 14 | 15 | return 0; 16 | } -------------------------------------------------------------------------------- /Lecture008 Switch Statement & Functions/Homework1.cpp: -------------------------------------------------------------------------------- 1 | // AP series 2 | 3 | #include 4 | using namespace std; 5 | 6 | int AP(int n) { 7 | 8 | int ap = 3 * n + 7; 9 | return ap; 10 | } 11 | 12 | int main() { 13 | 14 | int n; 15 | cin >> n; 16 | 17 | cout << "The AP is = " << AP(n); 18 | } -------------------------------------------------------------------------------- /Lecture008 Switch Statement & Functions/passByValue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void dummy(int n) { 5 | 6 | n++; 7 | cout << "n is " << n << endl; 8 | } 9 | 10 | int main() { 11 | 12 | int n; 13 | cin >> n; 14 | 15 | dummy(n); 16 | cout << "number n is " << n << endl; 17 | } -------------------------------------------------------------------------------- /Lecture083 N-Queen Problem Backtracking Day 2/Coding Question.md: -------------------------------------------------------------------------------- 1 | # Coding Quesion 2 | 3 | 1. The N - Queens Puzzle: [https://www.codingninjas.com/studio/problems/the-n-queens-puzzle_981286?topList=love-babbar-dsa-sheet-problems&leftPanelTab=0&utm_source=youtube&utm_medium=affiliate&utm_campaign=Lovebabbar] 4 | 5 | ``` cpp 6 | 1. 7 | ``` 8 | -------------------------------------------------------------------------------- /Lecture031 Recursion Day1/factorial_simple.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int factorial (int n) { 5 | 6 | if (n == 1) 7 | return 1; 8 | 9 | return n * factorial(n-1); 10 | } 11 | 12 | int main() { 13 | 14 | int n; 15 | cin >> n; 16 | 17 | int ans = factorial(n); 18 | cout << ans; 19 | } -------------------------------------------------------------------------------- /Lecture003 If-Else, While loop & Lots of Patterns/sumOfN.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | int n; 7 | cin >> n; 8 | 9 | int i = 1; 10 | int sum = 0; 11 | 12 | while(i <= n) { 13 | sum = sum + i; 14 | i++; 15 | } 16 | cout << "Value of sum is "<< sum << endl; 17 | } -------------------------------------------------------------------------------- /Lecture009 Introduction to Arrays in C++/Homework_1.cpp: -------------------------------------------------------------------------------- 1 | //All element of array set to 1 2 | 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | int n[5]; 8 | 9 | fill_n(n,5,1); //fill_n property is used to store same element in all position 10 | 11 | for(int i = 0; i < 5; i++) 12 | cout << n[i] << " "; 13 | } -------------------------------------------------------------------------------- /Lecture030 Macros, Gloab Variable etc/Homework.cpp: -------------------------------------------------------------------------------- 1 | // Constant Variable 2 | 3 | #include 4 | using namespace std; 5 | 6 | int main(){ 7 | 8 | int a = 5; 9 | cout << a << endl; 10 | a++; 11 | cout << a << endl; 12 | 13 | const int b = 7; // The value doesn't change furthera 14 | cout << b << endl; 15 | 16 | 17 | 18 | } -------------------------------------------------------------------------------- /Lecture005 Bitwise Operators, For Loops, Operator Precedence & Variable Scoping/continue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | for(int i = 0; i < 5; i++) { 7 | 8 | cout << "HI" << endl; 9 | cout << "Hey" << endl; 10 | continue; 11 | 12 | cout << "Reply toh karde" << endl; 13 | } 14 | } -------------------------------------------------------------------------------- /Lecture022 Strings/reverseString.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | class Solution { 6 | public: 7 | 8 | void reverseString(vector& s) { 9 | int st=0; 10 | int e = s.size()-1; 11 | 12 | while(st 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | int n; 7 | cout << "Enter a number = "; 8 | cin >> n; 9 | 10 | if ((n & 1) == 1) 11 | cout << "The number is odd"; 12 | else 13 | cout << "The number is even"; 14 | 15 | } -------------------------------------------------------------------------------- /Lecture006 Binary & Decimal Number System/Optimize_binTodec.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main (){ 6 | 7 | int ans = 0, i = 0, n = 5; 8 | while(n != 0){ 9 | 10 | int bit = n & 1; 11 | ans = (bit * pow(10,i)) + ans; 12 | n = n >> 1; 13 | i++; 14 | } 15 | cout << ans; 16 | } -------------------------------------------------------------------------------- /Lecture022 Strings/removeOccurrences.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | string removeOccurrences(string s, string part) { 8 | 9 | while(s.length()!=0 && s.find(part) < s.length()) { 10 | s.erase(s.find(part), part.length()); 11 | } 12 | return s; 13 | } 14 | }; -------------------------------------------------------------------------------- /Lecture031 Recursion Day1/counting.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void print (int n) { 5 | 6 | //base case 7 | if(n == 0) 8 | return; 9 | 10 | //Recursive relation 11 | print(n-1); 12 | cout << n << endl; 13 | } 14 | 15 | int main() { 16 | 17 | int n; 18 | cin >> n; 19 | cout << endl; 20 | print(n); 21 | } -------------------------------------------------------------------------------- /Lecture031 Recursion Day1/power.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int power(int n) { 5 | 6 | //base case 7 | if(n == 0) 8 | return 1; 9 | 10 | //recursive relation 11 | return 2 * power(n-1); 12 | } 13 | 14 | int main() { 15 | 16 | int n; 17 | cin >> n; 18 | 19 | int ans = power(n); 20 | 21 | cout << ans << endl; 22 | } -------------------------------------------------------------------------------- /Lecture005 Bitwise Operators, For Loops, Operator Precedence & Variable Scoping/forSum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | 6 | int n; 7 | cout << "Enter the value of n = "; 8 | cin >> n; 9 | 10 | int sum = 0; 11 | 12 | for(int i = 1; i <= n; i++) { 13 | //sum = sum + i; 14 | sum += i; 15 | } 16 | cout << sum << endl; 17 | } -------------------------------------------------------------------------------- /Lecture032 Recursion Day2/Fibbonacci.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int fib(int n) { 5 | 6 | if (n == 0) 7 | return 0; 8 | 9 | if (n == 1) 10 | return 1; 11 | 12 | return fib(n-2) + fib(n-1); 13 | } 14 | 15 | int main() { 16 | 17 | int number; 18 | cin >> number; 19 | 20 | int ans = fib(number); 21 | cout << ans; 22 | } -------------------------------------------------------------------------------- /Lecture030 Macros, Gloab Variable etc/defaultArgs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void print(int arr[], int n, int start = 0) { 5 | 6 | for(int i = start; i < n; i++) 7 | cout << arr[i] << endl; 8 | } 9 | 10 | int main() { 11 | 12 | int arr[5] = {1,4,7,8,9}; 13 | int size = 5; 14 | 15 | print(arr, size); 16 | cout << endl; 17 | print(arr, size, 2); 18 | } -------------------------------------------------------------------------------- /Lecture043 OOPs Day2/singleInheritance.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Animal { 5 | 6 | public: 7 | int age; 8 | int weight; 9 | 10 | void speak() { 11 | cout << "Speaking " << endl; 12 | } 13 | }; 14 | 15 | class Dog: public Animal { 16 | 17 | }; 18 | 19 | int main() { 20 | 21 | Dog d; 22 | d.speak(); 23 | cout << d.age << endl; 24 | } -------------------------------------------------------------------------------- /Lecture008 Switch Statement & Functions/counting.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Function Signature 5 | void printCounting(int num) { 6 | 7 | //Function Body 8 | for(int i = 1; i <= num; i++) 9 | cout<< i << " "; 10 | 11 | cout<> n; 18 | 19 | //function Call 20 | printCounting(n); 21 | } -------------------------------------------------------------------------------- /Lecture055 Stack Interview Question/reverseString.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main () { 6 | 7 | string name = "Vishal"; 8 | stack s; 9 | 10 | for (int i = 0; i < name.length(); i++){ 11 | char ch = name[i]; 12 | s.push(ch); 13 | } 14 | 15 | while(!s.empty()){ 16 | cout << s.top(); 17 | s.pop(); 18 | } 19 | } -------------------------------------------------------------------------------- /Lecture043 OOPs Day2/Encapsulation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Student { 5 | 6 | private: 7 | string name; 8 | int age; 9 | int height; 10 | 11 | public: 12 | int getAge() { 13 | return this->age; 14 | } 15 | }; 16 | 17 | int main() { 18 | 19 | Student first; 20 | cout << "Sab sahi chalra hai" << endl; 21 | 22 | return 0; 23 | } -------------------------------------------------------------------------------- /Lecture030 Macros, Gloab Variable etc/inlineFunctions.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | inline int getMax(int& a, int& b) { 5 | return (a>b) ? a : b; 6 | } 7 | 8 | int main() { 9 | 10 | int a = 1, b = 2; 11 | int ans = 0; 12 | 13 | ans = getMax(a,b); 14 | cout << ans << endl; 15 | 16 | a = a + 3; 17 | b = b + 1; 18 | 19 | ans = getMax(a,b); 20 | cout << ans << endl; 21 | } -------------------------------------------------------------------------------- /Lecture008 Switch Statement & Functions/power.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int power(int num1, int num2) { 5 | 6 | int ans = 1; 7 | 8 | for(int i = 1; i <= num2; i++) 9 | ans = ans * num1; 10 | 11 | return ans; 12 | } 13 | 14 | int main() { 15 | 16 | int c, d; 17 | cin >> c >> d; 18 | 19 | int answer = power(c, d); 20 | cout << "Answer is " << answer << endl; 21 | } -------------------------------------------------------------------------------- /Lecture024 Maths for DSA/GCD.java: -------------------------------------------------------------------------------- 1 | package MathsForDSA; 2 | 3 | public class GCD { 4 | // Find GCD of Two Numbers 5 | public static void main(String [] args){ 6 | int a = 4; 7 | int b = 9; 8 | System.out.format(" Gcd of %d and %d is: %d ", a, b, gcd(a,b)); 9 | } 10 | static int gcd(int a, int b){ 11 | if(a == 0) 12 | return b; 13 | return gcd(b % a , a); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Lecture043 OOPs Day2/multiLevelInheritance.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Animal { 5 | 6 | public: 7 | int age; 8 | int weight; 9 | 10 | void speak() { 11 | cout << "Speaking " << endl; 12 | } 13 | }; 14 | 15 | class Dog: public Animal { 16 | 17 | }; 18 | 19 | class GermanShepherd: public Dog { 20 | 21 | }; 22 | 23 | int main() { 24 | 25 | GermanShepherd g; 26 | g.speak(); 27 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CodeHelp-DSA-Busted-Series 2 | 3 | Hello Folks, 4 | 5 | This repository is for the persons who are stuck and easy access to all the source codes taught under Codehelp's DSA Busted Series by Love Babbar sir. I import this repo from codehelp DSA playlist and complete the series.. 6 | 7 | DSA Busted Series 8 | 9 | Youtube Channel: CodeHelp - by Babbar 10 | 11 | Instructor: Love Babbar 12 | 13 | By - Vishal Parmar 14 | 15 | Thank You... 16 | -------------------------------------------------------------------------------- /Lecture005 Bitwise Operators, For Loops, Operator Precedence & Variable Scoping/forFib.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | int n = 10; 7 | int a = 0; 8 | int b = 1; 9 | 10 | cout << a << " " << b << " "; 11 | 12 | for(int i = 1; i <= n; i++) { 13 | 14 | int nextNumber = a + b; 15 | cout << nextNumber << " "; 16 | 17 | a = b; 18 | b = nextNumber; 19 | } 20 | } -------------------------------------------------------------------------------- /Lecture004 Solving Pattern Questions (Part-2)/pattern1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 1 2 3 3 | 1 2 3 4 | 1 2 3 5 | */ 6 | 7 | #include 8 | using namespace std; 9 | 10 | int main() { 11 | int n; 12 | cin >> n; 13 | 14 | int i = 1; 15 | 16 | while(i <= n) { 17 | 18 | int j = 1; 19 | while(j <= n) { 20 | cout << j << " "; 21 | j = j + 1; 22 | } 23 | cout << endl; 24 | i = i + 1; 25 | } 26 | } -------------------------------------------------------------------------------- /Lecture031 Recursion Day1/factroial.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int factorial(int n) { 5 | 6 | //base case 7 | if(n==0) 8 | return 1; 9 | 10 | int smallerProblem = factorial(n-1); 11 | int biggerProblem = n * smallerProblem; 12 | 13 | return biggerProblem; 14 | } 15 | 16 | int main() { 17 | 18 | int n; 19 | cin >> n; 20 | 21 | int ans = factorial(n); 22 | 23 | cout << ans << endl; 24 | } -------------------------------------------------------------------------------- /Lecture004 Solving Pattern Questions (Part-2)/pattern3.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 1 1 1 3 | 2 2 2 4 | 3 3 3 5 | */ 6 | 7 | #include 8 | using namespace std; 9 | 10 | int main() { 11 | int n; 12 | cin >> n; 13 | 14 | int row = 1; 15 | while(row <= n) { 16 | 17 | int col = 1; 18 | while(col <= row) { 19 | cout << row; 20 | col = col + 1; 21 | } 22 | cout << endl; 23 | row = row + 1; 24 | } 25 | } -------------------------------------------------------------------------------- /Lecture006 Binary & Decimal Number System/decToBin.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | int n; 8 | cin >> n; 9 | 10 | int ans = 0; 11 | int i = 0; 12 | 13 | while(n != 0) { 14 | 15 | int bit = n & 1; 16 | 17 | ans = (bit * pow(10, i)) + ans; 18 | 19 | n = n >> 1; 20 | i++; 21 | } 22 | 23 | cout << "Answer is " << ans << endl; 24 | return 0; 25 | } -------------------------------------------------------------------------------- /Lecture003 If-Else, While loop & Lots of Patterns/pattern2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 1 1 1 3 | 2 2 2 4 | 3 3 3 5 | */ 6 | 7 | #include 8 | using namespace std; 9 | 10 | int main () { 11 | 12 | int n; 13 | cin >> n; 14 | 15 | int i = 1; 16 | 17 | while (i <= n) { 18 | int j = 1; 19 | 20 | while(j <= n) { 21 | cout << i << " "; 22 | j = j + 1; 23 | } 24 | cout << endl; 25 | i = i + 1; 26 | } 27 | } -------------------------------------------------------------------------------- /Lecture004 Solving Pattern Questions (Part-2)/pattern6.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 1 3 | 2 2 4 | 3 3 3 5 | 4 4 4 4 6 | */ 7 | 8 | #include 9 | using namespace std; 10 | 11 | int main() { 12 | int n; 13 | cin >> n; 14 | 15 | int i = 1; 16 | 17 | while(i <= n) { 18 | 19 | int j = 1; 20 | while(j <= i) { 21 | cout << i << " "; 22 | j = j + 1; 23 | } 24 | cout << endl; 25 | i = i + 1; 26 | } 27 | } -------------------------------------------------------------------------------- /Lecture004 Solving Pattern Questions (Part-2)/pattern9.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 1 3 | 2 1 4 | 3 2 1 5 | */ 6 | 7 | #include 8 | using namespace std; 9 | 10 | int main() { 11 | int n; 12 | cin >> n; 13 | 14 | int i = 1; 15 | 16 | while(i <= n) { 17 | int j = 1; 18 | 19 | while(j <= i) { 20 | cout << (i - j + 1) << " "; 21 | j = j + 1; 22 | } 23 | cout << endl; 24 | i = i + 1; 25 | } 26 | } -------------------------------------------------------------------------------- /Lecture004 Solving Pattern Questions (Part-2)/pattern2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 2 1 3 | 3 2 1 4 | 3 2 1 5 | */ 6 | 7 | #include 8 | using namespace std; 9 | 10 | int main() { 11 | int n; 12 | cin >> n; 13 | 14 | int i = 1; 15 | 16 | while(i <= n) { 17 | int j = 1; 18 | 19 | while(j <= n) { 20 | cout << n - j + 1 << " "; 21 | j = j + 1; 22 | } 23 | cout << endl; 24 | i = i + 1; 25 | } 26 | } -------------------------------------------------------------------------------- /Lecture004 Solving Pattern Questions (Part-2)/pattern5.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * 4 | * * * 5 | * * * * 6 | */ 7 | 8 | #include 9 | using namespace std; 10 | 11 | int main() { 12 | int n; 13 | cin >> n; 14 | 15 | int i = 1; 16 | 17 | while(i <= n) { 18 | 19 | int j = 1; 20 | while(j <= i) { 21 | cout << "*" << " "; 22 | j = j + 1; 23 | } 24 | cout << endl; 25 | i = i + 1; 26 | } 27 | } -------------------------------------------------------------------------------- /Lecture004 Solving Pattern Questions (Part-2)/pattern8.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 1 3 | 2 3 4 | 3 4 5 5 | 4 5 6 7 6 | */ 7 | 8 | #include 9 | using namespace std; 10 | 11 | int main() { 12 | int n; 13 | cin >> n; 14 | 15 | int i = 1; 16 | 17 | while(i <= n) { 18 | 19 | int j = 1; 20 | while(j <= i) { 21 | cout << (i + j - 1) << " "; 22 | j = j + 1; 23 | } 24 | cout << endl; 25 | i = i + 1; 26 | } 27 | } -------------------------------------------------------------------------------- /Lecture024 Maths for DSA/FactorsOfANumber.java: -------------------------------------------------------------------------------- 1 | package MathsForDSA; 2 | 3 | public class FactorsOfANumber { 4 | // Find All the factors of a Number 5 | public static void main(String [] args){ 6 | int num = 40; 7 | factors(num); 8 | } 9 | // Time Complexity O(N) 10 | static void factors(int num){ 11 | for(int i=1; i<= num; i++){ 12 | if(num % i == 0) 13 | System.out.print(i + " "); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Lecture006 Binary & Decimal Number System/binToDec.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | int n; 8 | cin >> n; 9 | 10 | int i = 0, ans = 0 ; 11 | 12 | while( n != 0) { 13 | 14 | int digit = n % 10; 15 | 16 | if( digit == 1) { 17 | ans = ans + pow(2, i); 18 | } 19 | 20 | n = n / 10; 21 | i++; 22 | 23 | } 24 | cout << ans << endl; 25 | return 0; 26 | } -------------------------------------------------------------------------------- /Lecture022 Strings/replaceSpaces.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | string replaceSpaces(string &str){ 5 | string temp = ""; 6 | 7 | for(int i=0; i 9 | using namespace std; 10 | 11 | int main() { 12 | 13 | int n; 14 | cin >> n; 15 | 16 | int i = 0; 17 | 18 | while (i < n) { 19 | int j = 0; 20 | 21 | while (j < n) { 22 | cout << "* "; 23 | j++; 24 | } 25 | 26 | cout << endl; 27 | i++; 28 | } 29 | } -------------------------------------------------------------------------------- /Lecture026 Pointers with Arrays and Functions/pointer_characterArray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | int arr[5] = {1,2,3,4,5}; 7 | char ch[6] = "abcde"; 8 | 9 | cout << arr << endl; 10 | 11 | //attention here 12 | cout << ch << endl; 13 | 14 | char *c = &ch[0]; 15 | 16 | //prints entire string 17 | cout << c << endl; 18 | 19 | char temp = 'z'; 20 | char *p = &temp; 21 | 22 | cout << p << endl; 23 | 24 | } -------------------------------------------------------------------------------- /Lecture043 OOPs Day2/ambiguity.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class A { 5 | 6 | public: 7 | void func() { 8 | cout << "I am A" << endl; 9 | } 10 | }; 11 | 12 | class B { 13 | 14 | public: 15 | void func() { 16 | cout << "I am B" << endl; 17 | } 18 | }; 19 | 20 | class C: public A, public B { 21 | 22 | }; 23 | 24 | int main() { 25 | 26 | C obj; 27 | //obj.func(); 28 | 29 | obj.A::func() ; 30 | obj.B::func(); 31 | } -------------------------------------------------------------------------------- /Lecture004 Solving Pattern Questions (Part-2)/pattern17.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * * * * * 3 | * * * * 4 | * * * 5 | * * 6 | * 7 | */ 8 | 9 | #include 10 | using namespace std; 11 | 12 | int main() { 13 | 14 | int n; 15 | cin >> n; 16 | 17 | int row = n; 18 | 19 | while(row >= 1) { 20 | 21 | int col = row; 22 | while( col >= 1) { 23 | cout<<"* "; 24 | col = col - 1; 25 | } 26 | cout < 2 | using namespace std; 3 | 4 | void rotate(vector& nums, int k) { 5 | 6 | int n = nums.size(); 7 | vector temp(n); 8 | 9 | for(int i = 0; i < n; i++) 10 | temp[(i+k)%n] = nums[i]; 11 | 12 | nums = temp; 13 | } 14 | 15 | int main() { 16 | 17 | vector v1 = {1,2,3,4,5,6,7}; 18 | int k = 3; 19 | rotate(v1,k); 20 | 21 | for(int i = 0; i < v1.size(); i++) 22 | cout << v1[i] << " "; 23 | } -------------------------------------------------------------------------------- /Lecture034 Recursion Day4/HomeWork1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void reverse (string &str, int i) { 5 | 6 | int size = str.size() - 1 - i; 7 | 8 | if(i > size) 9 | return; 10 | 11 | swap(str[i], str[size]); 12 | size--; 13 | i++; 14 | 15 | reverse(str,i); 16 | } 17 | 18 | int main() { 19 | 20 | string str; 21 | cout << "ENTER A STRING = "; 22 | getline(cin,str); 23 | 24 | reverse(str, 0); 25 | cout << str; 26 | } -------------------------------------------------------------------------------- /Lecture004 Solving Pattern Questions (Part-2)/pattern4.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 1 2 3 3 | 4 5 6 4 | 7 8 9 5 | */ 6 | 7 | #include 8 | using namespace std; 9 | 10 | int main() { 11 | int n; 12 | cin >> n; 13 | 14 | int i = 1; 15 | int count = 1; 16 | while(i <= n) { 17 | 18 | int j = 1; 19 | while(j <= n) { 20 | cout << count << " "; 21 | count = count + 1; 22 | j = j + 1; 23 | } 24 | cout << endl; 25 | i = i + 1; 26 | } 27 | } -------------------------------------------------------------------------------- /Lecture024 Maths for DSA/LCM.java: -------------------------------------------------------------------------------- 1 | package MathsForDSA; 2 | 3 | public class LCM { 4 | // Find LCM of Two numbers 5 | public static void main(String [] args){ 6 | int a = 4; 7 | int b = 9; 8 | System.out.format(" Gcd of %d and %d is: %d ", a, b, lcm(a,b)); 9 | } 10 | static int gcd(int a, int b){ 11 | if(a == 0) 12 | return b; 13 | return gcd(b % a , a); 14 | } 15 | static int lcm(int a, int b){ 16 | return a*b/gcd(a,b); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Lecture030 Macros, Gloab Variable etc/globalVariable.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int score = 15; 5 | 6 | void a(int& i) { 7 | 8 | cout << score << " in a" << endl; 9 | score++; 10 | char ch = 'a'; 11 | cout << i << endl; 12 | } 13 | 14 | void b(int& i) { 15 | cout << score << " in b" << endl; 16 | cout << i << endl; 17 | } 18 | 19 | int main() { 20 | cout << score << " in main" << endl; 21 | int i = 5; 22 | a(i); 23 | b(i); 24 | 25 | return 0; 26 | } -------------------------------------------------------------------------------- /Lecture004 Solving Pattern Questions (Part-2)/pattern13.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | A 3 | B B 4 | C C C 5 | */ 6 | 7 | #include 8 | using namespace std; 9 | 10 | int main() { 11 | 12 | int n; 13 | cin >> n; 14 | 15 | int row = 1; 16 | while(row <= n) { 17 | 18 | int col = 1; 19 | 20 | while(col <= row) { 21 | char ch = 'A' + row - 1; 22 | cout << ch << " "; 23 | col = col + 1; 24 | } 25 | cout << endl; 26 | row = row + 1; 27 | } 28 | } -------------------------------------------------------------------------------- /Lecture008 Switch Statement & Functions/Homework2.cpp: -------------------------------------------------------------------------------- 1 | // Number of Set bits 2 | 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int count(int n) { 8 | 9 | int count = 0; 10 | 11 | for(int i = 31; i >= 0; i--) { 12 | 13 | if((1LL << i) & n) 14 | count++; 15 | } 16 | return count; 17 | } 18 | 19 | int main() { 20 | 21 | int a, b; 22 | cin >> a >> b; 23 | 24 | int num1 = count(a); 25 | int num2 = count(b); 26 | 27 | cout << num1 + num2; 28 | } -------------------------------------------------------------------------------- /Lecture004 Solving Pattern Questions (Part-2)/pattern10.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | A A A 3 | B B B 4 | C C C 5 | */ 6 | 7 | #include 8 | using namespace std; 9 | 10 | int main() { 11 | 12 | int n; 13 | cin >> n; 14 | 15 | int row = 1; 16 | while(row <= n) { 17 | 18 | int col = 1; 19 | 20 | while(col <= n) { 21 | char ch = 'A' + row - 1; 22 | cout << ch << " "; 23 | col = col + 1; 24 | } 25 | cout << endl; 26 | row = row + 1; 27 | } 28 | } -------------------------------------------------------------------------------- /Lecture004 Solving Pattern Questions (Part-2)/pattern11.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | A B C 3 | A B C 4 | A B C 5 | */ 6 | 7 | #include 8 | using namespace std; 9 | 10 | int main() { 11 | 12 | int n; 13 | cin >> n; 14 | 15 | int row = 1; 16 | while(row <= n) { 17 | 18 | int col = 1; 19 | 20 | while(col <= n) { 21 | char ch = 'A' + col - 1; 22 | cout << ch << " "; 23 | col = col + 1; 24 | } 25 | cout << endl; 26 | row = row + 1; 27 | } 28 | } -------------------------------------------------------------------------------- /Lecture004 Solving Pattern Questions (Part-2)/pattern7.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 1 3 | 2 3 4 | 4 5 6 5 | 7 8 9 10 6 | */ 7 | 8 | #include 9 | using namespace std; 10 | 11 | int main() { 12 | int n; 13 | cin >> n; 14 | 15 | int i = 1; 16 | int count = 1; 17 | while(i <= n) { 18 | 19 | int j = 1; 20 | while(j <= i) { 21 | cout << count << " "; 22 | count = count + 1; 23 | j = j + 1; 24 | } 25 | cout << endl; 26 | i = i + 1; 27 | } 28 | } -------------------------------------------------------------------------------- /Lecture008 Switch Statement & Functions/Homework3.cpp: -------------------------------------------------------------------------------- 1 | // Fibonacci Series 2 | 3 | #include 4 | using namespace std; 5 | 6 | void fibonacci(int n){ 7 | 8 | int a = 0; 9 | int b = 1; 10 | 11 | cout << a << " " << b << " "; 12 | 13 | for(int i = 1; i <= n - 2; i++){ 14 | 15 | int nextNumber = a + b; 16 | cout << nextNumber << " "; 17 | 18 | a = b; 19 | b = nextNumber; 20 | } 21 | } 22 | 23 | int main() { 24 | 25 | int n; 26 | cin >> n; 27 | fibonacci(n); 28 | } -------------------------------------------------------------------------------- /Lecture004 Solving Pattern Questions (Part-2)/pattern12.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | A B C 3 | D E F 4 | G H I 5 | */ 6 | 7 | #include 8 | using namespace std; 9 | 10 | int main() { 11 | 12 | int n; 13 | cin >> n; 14 | 15 | int row = 1; 16 | char ch = 'A'; 17 | while(row <= n) { 18 | 19 | int col = 1; 20 | 21 | while(col <= n) { 22 | cout << ch << " "; 23 | ch = ch + 1; 24 | col = col + 1; 25 | } 26 | cout << endl; 27 | row = row + 1; 28 | } 29 | } -------------------------------------------------------------------------------- /Lecture004 Solving Pattern Questions (Part-2)/pattern14.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | A 3 | B C 4 | D E F 5 | */ 6 | 7 | #include 8 | using namespace std; 9 | 10 | int main() { 11 | 12 | int n; 13 | cin >> n; 14 | 15 | int row = 1; 16 | char ch = 'A'; 17 | while(row <= n) { 18 | 19 | int col = 1; 20 | 21 | while(col <= row) { 22 | cout << ch << " "; 23 | ch = ch + 1; 24 | col = col + 1; 25 | } 26 | cout << endl; 27 | row = row + 1; 28 | } 29 | } -------------------------------------------------------------------------------- /Lecture003 If-Else, While loop & Lots of Patterns/primeNumber.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | int n; 7 | bool f = 1; 8 | cin >> n; 9 | 10 | int i = 2; 11 | 12 | while(i < n) { 13 | 14 | if(n % i == 0) { 15 | f = 0; 16 | break; 17 | } 18 | else 19 | f == 1; 20 | 21 | i++; 22 | } 23 | 24 | if (f == 0) 25 | cout << "Not prime Number" << endl; 26 | else 27 | cout << "Prime Number" << endl; 28 | } -------------------------------------------------------------------------------- /Lecture008 Switch Statement & Functions/evenOdd.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //1 -> Even 5 | // 0 -> odd 6 | bool isEven(int a) { 7 | 8 | //odd 9 | if(a & 1) { 10 | return 0; 11 | } 12 | //Even 13 | else { 14 | return 1; 15 | } 16 | } 17 | 18 | int main() { 19 | 20 | int num; 21 | cin >> num; 22 | 23 | if(isEven(num)) 24 | cout << "Number is Even" << endl; 25 | 26 | else 27 | cout<< "Number is Odd" << endl; 28 | 29 | return 0; 30 | } -------------------------------------------------------------------------------- /Lecture004 Solving Pattern Questions (Part-2)/pattern15.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | C 3 | B C 4 | A B C 5 | */ 6 | 7 | #include 8 | using namespace std; 9 | 10 | int main() { 11 | 12 | int n; 13 | cin >> n; 14 | 15 | int row = 1; 16 | 17 | while(row <= n) { 18 | 19 | int col = 1; 20 | char start = 'A' + n - row; 21 | while(col <= row) { 22 | cout << start; 23 | start = start + 1; 24 | col = col + 1; 25 | } 26 | cout << endl; 27 | row = row + 1; 28 | } 29 | } -------------------------------------------------------------------------------- /Lecture008 Switch Statement & Functions/nCr.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int factorial(int n) { 5 | 6 | int fact = 1; 7 | 8 | for(int i = 1; i <= n; i++) { 9 | fact = fact * i; 10 | } 11 | return fact; 12 | } 13 | 14 | int nCr(int n, int r) { 15 | 16 | int num = factorial(n); 17 | int denom = factorial(r) * factorial(n-r); 18 | 19 | return num/denom; 20 | } 21 | 22 | int main() { 23 | 24 | int n, r; 25 | cin >> n >> r ; 26 | 27 | cout << "Answer is = " << nCr(n, r) << endl; 28 | } -------------------------------------------------------------------------------- /Lecture017 Bubble Sort/Coding Question.md: -------------------------------------------------------------------------------- 1 | # Coding Questions :- 2 | 3 | 1. [Bubble Sort](https://www.naukri.com/code360/problems/bubble-sort_980524?source=youtube&campaign=love_babbar_codestudio2&utm_source=youtube&utm_medium=affiliate&utm_campaign=love_babbar_codestudio2) 4 | 5 | ``` cpp 6 | 1. void bubbleSort(int arr[], int size) { 7 | 8 | for( int i = 1; i < size; i++) { 9 | 10 | for(int j = 0; j < size - i; j++) { 11 | 12 | if(arr[j] > arr[j+1]) 13 | swap(arr[j], arr[j+1]); 14 | } 15 | } 16 | } 17 | ``` -------------------------------------------------------------------------------- /Lecture069 Binary Search Tree and its Implementation/Coding Question.md: -------------------------------------------------------------------------------- 1 | # Coding Quesion 2 | 3 | 1. Search In BST: [https://www.codingninjas.com/studio/problems/search-in-bst_1402878?leftPanelTab=0] 4 | 5 | ```cpp 6 | 1. bool searchInBST(BinaryTreeNode *root, int x) { 7 | //base case 8 | if(root == NULL) 9 | return false; 10 | 11 | if(root -> data == x) 12 | return true; 13 | 14 | if(root -> data < x) 15 | searchInBST(root -> right, x); 16 | else 17 | searchInBST(root -> left, x); 18 | } 19 | ``` 20 | -------------------------------------------------------------------------------- /Lecture024 Maths for DSA/fastExpo.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int fastExpo(int a, int b ) { 5 | 6 | int res = 1; 7 | 8 | while(b > 0) { 9 | 10 | if(b&1) { 11 | //odd 12 | res = res*a; 13 | } 14 | b = b >> 1; 15 | a = a * a; 16 | } 17 | return res; 18 | } 19 | 20 | int main() { 21 | 22 | int a,b; 23 | cout << "Enter the Values of a and b" << endl; 24 | cin >> a >> b; 25 | 26 | cout << "Answer is: " << fastExpo(a,b) << endl; 27 | 28 | return 0; 29 | } -------------------------------------------------------------------------------- /Lecture033 Recursion Day3/arraysum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int sum (int *arr, int size) { 5 | 6 | //base case 7 | if (size == 0) 8 | return 0; 9 | 10 | if (size == 1) 11 | return arr[0]; 12 | 13 | /* int remainingPart = sum(arr+1, size-1); 14 | int sum = arr[0] + remainingPart; 15 | return sum; */ 16 | 17 | return arr[0] + sum(arr+1, size-1); 18 | } 19 | 20 | int main() { 21 | 22 | int arr[5] = {3, 2, 5, 1, 6}; 23 | 24 | int ans = sum(arr, 5); 25 | cout << ans; 26 | } -------------------------------------------------------------------------------- /Lecture008 Switch Statement & Functions/primeNumber.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // 0 -> Not a Prime no. 5 | // 1 -> Prime no. 6 | bool isPrime(int n) { 7 | 8 | for(int i = 2; i < n; i++){ 9 | 10 | if(n % i == 0) 11 | //divide hogya h , not a prime no. 12 | return 0; 13 | } 14 | return 1; 15 | } 16 | 17 | int main() { 18 | 19 | int n; 20 | cin >> n; 21 | 22 | if(isPrime(n)) 23 | cout << "Is a Prime number" << endl; 24 | 25 | else 26 | cout << "Not a prime number" << endl; 27 | } -------------------------------------------------------------------------------- /Lecture009 Introduction to Arrays in C++/Homework_2.cpp: -------------------------------------------------------------------------------- 1 | // Sum of an array 2 | 3 | #include 4 | using namespace std; 5 | 6 | int sum (int arr[], int size) { 7 | 8 | int sum = 0; 9 | 10 | for (int i = 0; i < size; i++) 11 | sum = sum + arr[i]; 12 | 13 | return sum; 14 | } 15 | 16 | int main() { 17 | 18 | int size; 19 | cout << "Enter the size of an array = "; 20 | cin >> size; 21 | 22 | int a[100]; 23 | 24 | for (int i = 0; i < size; i++) 25 | cin >> a[i]; 26 | 27 | cout << "Sum = "<< sum(a, size); 28 | } -------------------------------------------------------------------------------- /Lecture034 Recursion Day4/reverseString.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void reverse(string &str, int i, int j ) { 5 | 6 | cout << "call recieved for " << str << endl; 7 | 8 | //base case 9 | if(i > j) 10 | return; 11 | 12 | swap(str[i], str[j]); 13 | i++; 14 | j--; 15 | 16 | //Recursive call 17 | reverse(str, i, j); 18 | } 19 | 20 | int main() { 21 | 22 | string name = "abcde"; 23 | cout << endl; 24 | reverse(name, 0, name.length() - 1); 25 | cout << endl; 26 | cout << name << endl; 27 | } -------------------------------------------------------------------------------- /Lecture032 Recursion Day2/climbStairs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int countDistinctWayToClimbStair(long long nStairs) { 5 | 6 | //base case 7 | if(nStairs < 0) 8 | return 0; 9 | 10 | if(nStairs == 0) 11 | return 1; 12 | 13 | //R.C 14 | int ans = countDistinctWayToClimbStair(nStairs-1) + countDistinctWayToClimbStair(nStairs-2); 15 | 16 | return ans; 17 | } 18 | 19 | int main (){ 20 | 21 | int stairs; 22 | cin >> stairs; 23 | 24 | int ans = countDistinctWayToClimbStair(stairs); 25 | cout << ans; 26 | } -------------------------------------------------------------------------------- /Lecture024 Maths for DSA/primeNumberBrute.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool isPrime(int n) { 5 | 6 | if(n<=1) 7 | return false; 8 | 9 | for(int i=2; i> n; 23 | 24 | if(isPrime(n)) { 25 | cout << "It is a Prime Number" << endl; 26 | } 27 | else{ 28 | cout << " It is not a Prime Number" << endl; 29 | } 30 | 31 | 32 | return 0; 33 | } -------------------------------------------------------------------------------- /Lecture037 Recursion Day7/subsequences.cpp: -------------------------------------------------------------------------------- 1 | void solve(vector& ans, string str, string output, int i) { 2 | //base case 3 | if(i>=str.length()) { 4 | if(output.length()>0) 5 | ans.push_back(output); 6 | return ; 7 | } 8 | 9 | //exclude 10 | solve(ans, str, output, i+1); 11 | //include 12 | output.push_back(str[i]); 13 | solve(ans, str, output, i+1); 14 | } 15 | 16 | vector subsequences(string str){ 17 | 18 | vector ans; 19 | string output = ""; 20 | solve(ans,str,output,0); 21 | return ans; 22 | 23 | } 24 | -------------------------------------------------------------------------------- /Lecture054 Introduction to Stacks/stack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | void stacks(stack s){ 6 | 7 | while(!s.empty()){ 8 | cout << s.top() << endl; 9 | s.pop(); 10 | } 11 | } 12 | 13 | int main () { 14 | 15 | stack s; 16 | 17 | s.push(10); 18 | s.push(20); 19 | s.push(30); 20 | 21 | stacks(s); 22 | 23 | cout << "Top element = " << s.top() << endl; 24 | 25 | s.pop(); 26 | 27 | cout << "Top element = " << s.top() << endl; 28 | 29 | cout << "Is empty = " << s.empty() << endl; 30 | } -------------------------------------------------------------------------------- /Lecture004 Solving Pattern Questions (Part-2)/pattern19.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 1111 3 | 222 4 | 33 5 | 4 6 | */ 7 | 8 | #include 9 | using namespace std; 10 | 11 | int main() { 12 | 13 | int n; 14 | cin >> n; 15 | 16 | int row = 1; 17 | while(row <= n) { 18 | 19 | int space = 1; 20 | while (space <= row) { 21 | cout << " "; 22 | space++; 23 | } 24 | 25 | int col = n; 26 | while (col >= row) { 27 | cout << row ; 28 | col--; 29 | } 30 | cout << endl; 31 | row++; 32 | } 33 | } -------------------------------------------------------------------------------- /Lecture032 Recursion Day2/walkingExample.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void reachHome(int src, int dest) { 5 | 6 | cout << "source " << src << " destination " << dest << endl; 7 | //base case 8 | if(src == dest) { 9 | cout << "pahuch gya" << endl; 10 | return ; 11 | } 12 | 13 | //processing - ek step aage badhjao 14 | src++; 15 | 16 | //recursive call 17 | reachHome(src, dest); 18 | } 19 | 20 | int main() { 21 | 22 | int dest = 10; 23 | int src = 1; 24 | 25 | cout << endl; 26 | 27 | reachHome(src, dest); 28 | } -------------------------------------------------------------------------------- /Lecture008 Switch Statement & Functions/switch.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | 6 | char ch = '1'; 7 | int num = 1; 8 | 9 | switch( 2 * num ) { 10 | 11 | case 2: cout << "First" << endl; 12 | cout << "First again" << endl; 13 | break; 14 | 15 | case '1': switch(num) { 16 | case 1: cout <<"Value of num is = " << num << endl; 17 | break; 18 | } 19 | break; 20 | 21 | default : cout << "It is default case" << endl; 22 | } 23 | } -------------------------------------------------------------------------------- /Lecture004 Solving Pattern Questions (Part-2)/pattern18.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | ***** 3 | **** 4 | *** 5 | ** 6 | * 7 | */ 8 | 9 | #include 10 | using namespace std; 11 | 12 | int main() { 13 | 14 | int n; 15 | cin >> n; 16 | 17 | int row = 1; 18 | while(row <= n) { 19 | 20 | int space = 1; 21 | while (space <= row) { 22 | cout << " "; 23 | space++; 24 | } 25 | 26 | int col = n; 27 | while (col >= row) { 28 | cout << "*"; 29 | col--; 30 | } 31 | cout << endl; 32 | row++; 33 | } 34 | } -------------------------------------------------------------------------------- /Lecture004 Solving Pattern Questions (Part-2)/pattern22.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 1 3 | 1 2 1 4 | 1 2 3 2 1 5 | 1 2 3 4 3 2 1 6 | */ 7 | 8 | #include 9 | using namespace std; 10 | 11 | int main() { 12 | 13 | int height; 14 | cin >> height; 15 | 16 | for (int i = 1; i <= height; i++) { 17 | for (int k = 1; k <= (height - i); k++) 18 | cout << " "; 19 | 20 | for (int j = 1; j <= i; j++) 21 | cout << j << " "; 22 | 23 | for (int l = i - 1; l >= 1; l--) 24 | cout << l << " "; 25 | 26 | cout << endl; 27 | } 28 | } -------------------------------------------------------------------------------- /Lecture014 Binary Search Interview Questions/pivot.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int getPivot(int arr[], int n) { 5 | 6 | int start = 0; 7 | int end = n - 1; 8 | int mid = start + (end - start) / 2; 9 | 10 | while(start < end ) { 11 | 12 | if(arr[mid] >= arr[0]) 13 | start = mid + 1; 14 | 15 | else 16 | end = mid; 17 | 18 | mid = start + (end - start) / 2; 19 | } 20 | return start; 21 | } 22 | 23 | int main() { 24 | int arr[4] = {8, 9, 4, 5}; 25 | cout << "Pivot is " << getPivot(arr, 4) << endl; 26 | } -------------------------------------------------------------------------------- /Lecture021 Solving Array Questions 2/checkSortedRotatedArray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool check(vector& nums) { 5 | 6 | int count = 0; 7 | 8 | for (int i = 1; i < nums.size(); i++) { 9 | 10 | if(nums[i] < nums[i - 1]) 11 | count++; 12 | } 13 | if (nums[nums.size() - 1] > nums[0]) 14 | count++; 15 | 16 | return count<=1; 17 | } 18 | 19 | int main() { 20 | 21 | vector nums = {3, 4, 5, 1, 2}; 22 | bool ans = check(nums); 23 | 24 | cout << ans; 25 | } -------------------------------------------------------------------------------- /Lecture043 OOPs Day2/multipleInheritance.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Animal { 5 | 6 | public: 7 | int age; 8 | int weight; 9 | 10 | void bark() { 11 | cout << "Barking " << endl; 12 | } 13 | }; 14 | 15 | class Human { 16 | public: 17 | string color; 18 | 19 | public: 20 | void speak() { 21 | cout << "Speaking " << endl; 22 | } 23 | }; 24 | 25 | //Multiple Inheritance 26 | class Hybrid: public Animal, public Human { 27 | 28 | }; 29 | 30 | 31 | int main() { 32 | 33 | Hybrid obj1; 34 | obj1.speak(); 35 | obj1.bark(); 36 | } -------------------------------------------------------------------------------- /Lecture023 2D arrays/Search2DMatrix2.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean searchMatrix(int[][] matrix, int target) { 3 | int rows = matrix.length; 4 | int col = matrix[0].length; 5 | 6 | int rowIdx = 0; 7 | int colIdx = col - 1; 8 | 9 | while(rowIdx < rows && colIdx >= 0){ 10 | int val = matrix[rowIdx][colIdx]; 11 | 12 | if(val == target){ 13 | return true; 14 | } 15 | if(val < target){ 16 | rowIdx++; 17 | } 18 | else colIdx--; 19 | } 20 | 21 | return false; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Lecture005 Bitwise Operators, For Loops, Operator Precedence & Variable Scoping/forLoop.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | int n; 7 | //cout << "Enter the value of n" <> n; 9 | 10 | cout<<"printing count from 1 to n" << endl; 11 | int i = 1; 12 | for(; ; ) { //Omit Statement 13 | if(i<=n) 14 | cout<< i << endl; 15 | else 16 | break; 17 | 18 | i++; 19 | } 20 | 21 | for(int a = 0, b =1, c = 2; a >= 0 && b >= 1 && c >= 2; a--, b--, c--) 22 | cout << a << " " << b << " " << c << endl; 23 | } -------------------------------------------------------------------------------- /Lecture034 Recursion Day4/exponent.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int power (int a, int b) { 5 | 6 | //base case 7 | if(b == 0) 8 | return 1; 9 | 10 | if(b == 1) 11 | return a; 12 | 13 | //RECURSIVE CALL 14 | int ans = power(a, b/2); 15 | 16 | //if b is even 17 | if(b % 2 == 0) 18 | return ans * ans; 19 | 20 | //if b is odd 21 | else 22 | return a * ans * ans; 23 | } 24 | 25 | int main() { 26 | 27 | int a, b; 28 | cin >> a >> b; 29 | cout << endl; 30 | int ans = power(a, b); 31 | 32 | cout << "Answer is " << ans << endl; 33 | } -------------------------------------------------------------------------------- /Lecture004 Solving Pattern Questions (Part-2)/pattern21.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 1234 3 | 234 4 | 34 5 | 4 6 | */ 7 | 8 | #include 9 | using namespace std; 10 | 11 | int main() { 12 | 13 | int n; 14 | cin >> n; 15 | 16 | int row = 1; 17 | while(row <= n) { 18 | 19 | int space = 1; 20 | while (space <= row) { 21 | cout << " "; 22 | space++; 23 | } 24 | int number = row; 25 | int col = n; 26 | while (col >= row) { 27 | cout << number; 28 | number++; 29 | col--; 30 | } 31 | cout << endl; 32 | row++; 33 | } 34 | } -------------------------------------------------------------------------------- /Lecture005 Bitwise Operators, For Loops, Operator Precedence & Variable Scoping/forPrime.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | 6 | int n; 7 | cout << "Enter the value of n = "; 8 | cin >> n; 9 | 10 | bool isPrime = 1; 11 | 12 | for(int i = 2; i < n; i++) { 13 | 14 | //rem = 0, Not a Prime 15 | if(n % i == 0) { 16 | //cout<<" Not a Prime Number" << endl; 17 | isPrime = 0; 18 | break; 19 | } 20 | } 21 | 22 | if(isPrime == 0) 23 | cout << "Not a Prime Number" << endl; 24 | else 25 | cout << "Is a Prime Number" << endl; 26 | } 27 | -------------------------------------------------------------------------------- /Lecture023 2D arrays/Search2DMatrix1.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean searchMatrix(int[][] matrix, int target) { 3 | int row = matrix.length; 4 | int col = matrix[0].length; 5 | 6 | int start = 0; 7 | int end = row * col - 1; 8 | 9 | while(start <= end){ 10 | int mid = start + (end - start)/2; 11 | 12 | if(matrix[mid/col][mid%col] == target) 13 | return true; 14 | 15 | if(matrix[mid/col][mid%col] < target){ 16 | start = mid + 1; 17 | } 18 | else end = mid - 1; 19 | 20 | } 21 | return false; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Lecture024 Maths for DSA/gcd.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int gcd(int a, int b) { 5 | 6 | if(a==0) 7 | return b; 8 | 9 | if(b==0) 10 | return a; 11 | 12 | while(a != b) { 13 | 14 | if(a>b) 15 | { 16 | a = a-b; 17 | } 18 | else{ 19 | b = b-a; 20 | } 21 | } 22 | return a; 23 | } 24 | 25 | int main() { 26 | 27 | int a,b; 28 | cout << "Enter the Values of a and b" << endl; 29 | cin >> a >> b; 30 | 31 | int ans = gcd(a,b); 32 | 33 | cout << " The GCD of " << a << " & " << b << " is: " << ans << endl; 34 | 35 | 36 | 37 | return 0; 38 | } -------------------------------------------------------------------------------- /Lecture004 Solving Pattern Questions (Part-2)/pattern16.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | ** 4 | *** 5 | */ 6 | 7 | #include 8 | using namespace std; 9 | 10 | int main() { 11 | 12 | int n; 13 | cin >> n; 14 | 15 | int row = 1; 16 | 17 | while(row <= n) { 18 | 19 | //space print karlo 20 | int space = n - row; 21 | while(space) { 22 | cout<<" "; 23 | space = space - 1; 24 | } 25 | 26 | //stars print karlo 27 | int col = 1; 28 | while( col <= row ) { 29 | cout << "*"; 30 | col = col + 1; 31 | } 32 | cout << endl; 33 | row = row + 1; 34 | } 35 | } -------------------------------------------------------------------------------- /Lecture033 Recursion Day3/sortedArray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool isSorted(int arr[], int size) { 5 | 6 | //base case 7 | if(size == 0 || size == 1 ) 8 | return true; 9 | 10 | if(arr[0] > arr[1]) 11 | return false; 12 | 13 | else { 14 | bool remainingPart = isSorted(arr + 1, size - 1); 15 | return remainingPart; 16 | } 17 | } 18 | 19 | int main() { 20 | 21 | int arr[5] = {2,4,9,9,9}; 22 | int size = 5; 23 | 24 | bool ans = isSorted(arr, size); 25 | 26 | if(ans) 27 | cout << "Array is sorted " << endl; 28 | 29 | else 30 | cout << "Array is not sorted " << endl; 31 | } -------------------------------------------------------------------------------- /Lecture034 Recursion Day4/HomeWork2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool palindrome(string str, int i) { 5 | 6 | if(str.size() == 1) 7 | return true; 8 | 9 | if(i >str.size()-1-i) 10 | return true; 11 | 12 | if(str[i] !=str[str.size()-1-i]) 13 | return false; 14 | 15 | else 16 | return palindrome(str, ++i); 17 | } 18 | 19 | int main() { 20 | 21 | string str; 22 | cout << "ENTER A STRING = "; 23 | getline(cin,str); 24 | 25 | bool ans = palindrome(str, 0); 26 | 27 | if(ans) 28 | cout << "PALINDROME"; 29 | else 30 | cout<< "NOT PALINDROME"; 31 | } -------------------------------------------------------------------------------- /Lecture016 Selection Sort/selectionSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | void selectionSort(vector &arr) { 6 | 7 | for (int i = 0; i < arr.size() - 1 ; i++) { 8 | 9 | int minIndex = i; 10 | 11 | for(int j = i + 1; j < arr.size(); j++) { 12 | 13 | if (arr[j] < arr[minIndex]) 14 | minIndex = j; 15 | 16 | } 17 | swap(arr[minIndex], arr[i]); 18 | } 19 | } 20 | 21 | int main (){ 22 | 23 | vector a = {5,2,7,9,10}; 24 | selectionSort (a); 25 | 26 | for (int i = 0; i < a.size(); i++) 27 | cout << a[i] << " "; 28 | } -------------------------------------------------------------------------------- /Lecture004 Solving Pattern Questions (Part-2)/pattern20.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 1 3 | 22 4 | 333 5 | 4444 6 | */ 7 | 8 | #include 9 | using namespace std; 10 | 11 | int main() { 12 | 13 | int n; 14 | cin >> n; 15 | 16 | int row = 1; 17 | 18 | while(row <= n) { 19 | 20 | //space print karlo 21 | int space = n - row; 22 | while(space) { 23 | cout<<" "; 24 | space = space - 1; 25 | } 26 | 27 | //stars print karlo 28 | int col = 1; 29 | while( col <= row ) { 30 | cout << row; 31 | col = col + 1; 32 | } 33 | cout << endl; 34 | row = row + 1; 35 | } 36 | } -------------------------------------------------------------------------------- /Lecture009 Introduction to Arrays in C++/linearSeaerch.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool search(int arr[], int size, int key) { 5 | 6 | for( int i = 0; i 2 | using namespace std; 3 | 4 | void bubbleSort(int arr[], int size) { 5 | 6 | for( int i = 1; i < size; i++) { 7 | 8 | for(int j = 0; j < size - i; j++) { 9 | 10 | if(arr[j] > arr[j+1]) 11 | swap(arr[j], arr[j+1]); 12 | } 13 | } 14 | } 15 | 16 | void printArray(int array[], int size) { 17 | 18 | for (int i = 0; i < size; i++) 19 | cout << array[i] << " "; 20 | 21 | cout << endl; 22 | } 23 | 24 | int main() { 25 | 26 | int arr[] = {20, 12, 10, 15, 2}; 27 | int size = sizeof(arr) / sizeof(arr[0]); 28 | bubbleSort (arr, size); 29 | printArray(arr,size); 30 | } -------------------------------------------------------------------------------- /Lecture016 Selection Sort/Coding Question.md: -------------------------------------------------------------------------------- 1 | # Coding Questions :- 2 | 3 | 1. [Selection Sort](https://www.naukri.com/code360/problems/selection-sort_981162?source=youtube&campaign=love_babbar_codestudio2&utm_source=youtube&utm_medium=affiliate&utm_campaign=love_babbar_codestudio2) 4 | 5 | ``` cpp 6 | 1. void selectionSort(vector &arr) { 7 | 8 | for (int i = 0; i < arr.size() - 1 ; i++) { 9 | 10 | int minIndex = i; 11 | 12 | for(int j = i + 1; j < arr.size(); j++) { 13 | 14 | if (arr[j] < arr[minIndex]) 15 | minIndex = j; 16 | 17 | } 18 | swap(arr[minIndex], arr[i]); 19 | } 20 | } 21 | ``` -------------------------------------------------------------------------------- /Lecture034 Recursion Day4/bubbleSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void sortArray(int *arr, int n) { 5 | 6 | //base case - already sorted 7 | if(n == 0 || n == 1) 8 | return; 9 | 10 | //1 case sovle karlia - largest element ko end me rakh dega 11 | for (int i = 0; i < n - 1; i++) { 12 | 13 | if(arr[i] > arr[i + 1]) 14 | swap(arr[i], arr[i+1]); 15 | } 16 | 17 | //Recursive Call 18 | sortArray(arr, n - 1); 19 | } 20 | 21 | int main() { 22 | 23 | int arr[5] = {2, 5, 1, 6, 9}; 24 | 25 | sortArray(arr, 5); 26 | 27 | for(int i = 0; i < 5; i++) 28 | cout << arr[i] << " "; 29 | 30 | cout << endl; 31 | } -------------------------------------------------------------------------------- /Lecture034 Recursion Day4/palindromeCheck.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool checkPalindrome(string str, int i, int j) { 5 | 6 | //base case 7 | if(i > j) 8 | return true; 9 | 10 | if(str[i] != str[j]) 11 | return false; 12 | 13 | else 14 | //Recursive call 15 | return checkPalindrome(str, i + 1, j - 1); 16 | } 17 | 18 | int main() { 19 | 20 | string name = "BookkooB"; 21 | cout << endl; 22 | 23 | bool isPalindrome = checkPalindrome(name, 0, name.length()-1); 24 | 25 | if(isPalindrome) 26 | cout << "It's a Palindrome" << endl; 27 | 28 | else 29 | cout << "It's not a Palindrome" << endl; 30 | } -------------------------------------------------------------------------------- /Lecture006 Binary & Decimal Number System/Negative_decTobin.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | 6 | int num; 7 | cout << "Enter a positive or negative decimal number: "; 8 | cin >> num; 9 | 10 | // convert to binary 11 | int bits = sizeof(num) * 8; // number of bits in int type 12 | unsigned int mask = 1 << (bits - 1); // mask for the most significant bit 13 | 14 | cout << "Binary representation: "; 15 | 16 | for (int i = 0; i < bits; i++) { 17 | 18 | cout << ((num & mask) ? 1 : 0); // output 1 or 0 based on the bit value 19 | mask >>= 1; // shift mask to the right 20 | } 21 | 22 | cout << endl; 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Lecture032 Recursion Day2/SayDigits.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void sayDigit(int n, string arr[]) { 5 | 6 | //base case 7 | if(n == 0) 8 | return ; 9 | 10 | //processing 11 | int digit = n % 10; 12 | n = n / 10; 13 | 14 | //recursive call 15 | sayDigit(n, arr); 16 | 17 | cout << arr[digit] << " "; 18 | } 19 | 20 | int main() { 21 | 22 | string arr[10] = {"zero", "one", "two", "three", 23 | "four", "five", "six" 24 | , "seven", "eight", "nine"}; 25 | int n; 26 | cin >> n; 27 | 28 | cout << endl << endl << endl ; 29 | sayDigit(n, arr); 30 | cout << endl << endl << endl ; 31 | } -------------------------------------------------------------------------------- /Lecture018 Insertion Sort/insertionSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | void insertionSort(vector &arr) { 6 | 7 | for(int i = 1; i < arr.size(); i++){ 8 | 9 | int temp = arr[i]; 10 | int j = i - 1; 11 | 12 | for(; j >= 0; j--) { 13 | 14 | if(arr[j] > temp) 15 | arr[j+1] = arr[j]; 16 | else 17 | break; 18 | } 19 | arr[j+1] = temp; 20 | } 21 | } 22 | 23 | int main(){ 24 | 25 | vector arr = {4,52,5,2,15,67,2,35,67,78}; 26 | insertionSort(arr); 27 | 28 | for(int i = 0; i < arr.size(); i++) 29 | cout << arr[i] << " "; 30 | } -------------------------------------------------------------------------------- /Lecture060 Introduction to the Queues/dequeue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | cout << endl; 8 | 9 | deque d1; 10 | 11 | d1.push_front(10); 12 | d1.push_back(14); 13 | 14 | cout << "Front element = " << d1.front() << endl; 15 | cout << "Last element = " << d1.back() << endl; 16 | 17 | d1.pop_front(); 18 | 19 | cout << "Front element = " << d1.front() << endl; 20 | cout << "Last element = " << d1.back() << endl; 21 | 22 | d1.pop_front(); 23 | 24 | if(d1.empty()) 25 | cout << "Queue is empty" << endl; 26 | 27 | else 28 | cout << "Queue is not empty" << endl; 29 | 30 | cout << endl; 31 | 32 | } -------------------------------------------------------------------------------- /Lecture009 Introduction to Arrays in C++/reverse.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void reverse(int arr[], int n) { 5 | 6 | int start = 0; 7 | int end = n - 1; 8 | 9 | while(start <= end){ 10 | 11 | swap(arr[start], arr[end]); 12 | start++; 13 | end--; 14 | } 15 | } 16 | 17 | void printArray(int arr[], int n) { 18 | 19 | for(int i = 0; i < n; i++) 20 | cout << arr[i] << " "; 21 | 22 | cout << endl; 23 | } 24 | 25 | int main() { 26 | 27 | int arr[6] = {1,4,0,5,-2,15}; 28 | int brr[5] = {2,6,3,9,4}; 29 | 30 | reverse(arr, 6); 31 | reverse(brr, 5); 32 | 33 | printArray(arr, 6); 34 | printArray(brr, 5); 35 | 36 | return 0; 37 | } -------------------------------------------------------------------------------- /Lecture024 Maths for DSA/NewtonsSquareRoot.java: -------------------------------------------------------------------------------- 1 | package MathsForDSA; 2 | 3 | public class NewtonsSquareRoot { 4 | // Calculating Square Root of a Number Using Newton's Square Root Method 5 | public static void main(String [] args){ 6 | double num = 40; 7 | // Display the Square Root of n 8 | System.out.println(" Square Root of " + num + " is: " + sqrt(num)); 9 | } 10 | 11 | static double sqrt(double n){ 12 | double x = n; 13 | double root; 14 | while(true){ 15 | root = 0.5 * (x + (n/x)); // Similar to Root = ( X + N/X ) / 2 16 | if(Math.abs(root - x) < 0.5) 17 | break; 18 | x = root; 19 | } 20 | return root; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Lecture009 Introduction to Arrays in C++/arrayScope.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void update(int arr[], int n) { 5 | 6 | cout << endl << "Inside the function" << endl; 7 | 8 | //updating array's first element 9 | arr[0] = 120; 10 | 11 | //printing the array 12 | for(int i = 0; i < 3; i++) 13 | cout << arr[i] << " "; 14 | 15 | cout << endl; 16 | cout << "Going back to main function" << endl; 17 | } 18 | 19 | int main() { 20 | 21 | int arr[3] = {1,2,3}; 22 | 23 | update(arr, 3); 24 | 25 | //printing the array 26 | cout<< endl << "Printing in main function" << endl; 27 | for(int i = 0; i < 3; i++) 28 | cout << arr[i] << " "; 29 | 30 | cout << endl; 31 | } -------------------------------------------------------------------------------- /Lecture010 Solving LeetCode CodeStudio Questions Arrays/swapAlternates.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void printArray(int arr[], int size) { 5 | 6 | for (int i = 0; i < size; i++) 7 | cout << arr[i] << " "; 8 | 9 | cout << endl; 10 | } 11 | 12 | void swapAlternate(int arr[], int size) { 13 | 14 | for( int i = 0; i < size; i += 2 ) { 15 | 16 | if( i+1 < size) 17 | swap(arr[i], arr[i+1]); 18 | } 19 | } 20 | 21 | int main() { 22 | 23 | int even[8] = {5,2,9,4,7,6,1,0}; 24 | int odd[5] = {11, 33, 9, 76, 43}; 25 | 26 | swapAlternate(even, 8); 27 | printArray(even, 8); 28 | 29 | cout << endl; 30 | 31 | swapAlternate(odd, 5); 32 | printArray(odd, 5); 33 | } -------------------------------------------------------------------------------- /Lecture025 Intro to Pointers in C++/intro.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | int num = 5 ; 7 | 8 | cout << num << endl; 9 | 10 | // address of Operator - & 11 | 12 | cout << "Address of num is " << &num << endl; 13 | 14 | int *ptr = # 15 | 16 | cout << "Address is : " << ptr << endl; 17 | cout << "Value is : " << *ptr << endl; 18 | 19 | double d = 4.3; 20 | double *p2 = &d; 21 | 22 | cout << "Address is : " << p2 << endl; 23 | cout << "Value is : " << *p2 << endl; 24 | 25 | cout << "size of integer is " << sizeof(num) << endl; 26 | cout << "size of pointer is " << sizeof(ptr) << endl; 27 | cout << "size of pointer is " << sizeof(p2) << endl; 28 | 29 | } -------------------------------------------------------------------------------- /Lecture005 Bitwise Operators, For Loops, Operator Precedence & Variable Scoping/bitwiseOperators.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | int a = 4; 7 | int b = 6; 8 | 9 | cout << "a & b " << (a & b) << endl; 10 | cout << "a | b " << (a | b) << endl; 11 | cout << "~a " << ~a << endl; 12 | cout << "a^b " << (a ^ b) << endl; 13 | 14 | cout << (17 >> 1) << endl; 15 | cout << (17 >> 2) << endl; 16 | cout << (19 << 1) << endl; 17 | cout << (21 << 2) << endl; 18 | 19 | int i = 7; 20 | 21 | cout << (++i) << endl; 22 | // 8 23 | cout << (i++) << endl; 24 | // 8 , i = 9 25 | cout << (i--) << endl; 26 | //9 , i = 8 27 | cout << (--i) << endl; 28 | // 7, i =7 29 | } -------------------------------------------------------------------------------- /Lecture043 OOPs Day2/hierarchicalInheritance.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Hierarchical Inheritance 5 | class A { 6 | 7 | public: 8 | void func1() { 9 | cout << "Inside Funcion 1" << endl; 10 | } 11 | }; 12 | 13 | class B: public A { 14 | 15 | public: 16 | void func2() { 17 | cout << "Inside Funcion 2" << endl; 18 | } 19 | }; 20 | 21 | class C: public A { 22 | public: 23 | void func3() { 24 | cout << "Inside Funcion 3" << endl; 25 | } 26 | }; 27 | 28 | int main() { 29 | 30 | A object1; 31 | object1.func1(); 32 | 33 | B object2; 34 | object2.func1(); 35 | object2.func2(); 36 | 37 | C object3; 38 | object3.func1(); 39 | object3.func3(); 40 | } -------------------------------------------------------------------------------- /Lecture018 Insertion Sort/Coding Question.md: -------------------------------------------------------------------------------- 1 | # Coding Questions :- 2 | 3 | 1. [Insertion Sort](https://www.naukri.com/code360/problems/insertion-sort_3155179?source=youtube&campaign=love_babbar_codestudio2&utm_source=youtube&utm_medium=affiliate&utm_campaign=love_babbar_codestudio2) 4 | 5 | ``` cpp 6 | 1. void insertionSort(int n, vector &arr){ 7 | 8 | for(int i = 1; i < arr.size(); i++) { 9 | 10 | int temp = arr[i]; 11 | int j = i - 1; 12 | 13 | for(; j >= 0; j--) { 14 | 15 | if(arr[j] > temp) 16 | 17 | arr[j+1] = arr[j]; 18 | 19 | else 20 | break; 21 | 22 | } 23 | arr[j+1] = temp; 24 | } 25 | } 26 | ``` -------------------------------------------------------------------------------- /Lecture020 Solving LeetCode CodeStudio Questions [Arrays]/MoveZeroes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void moveZeroes(vector& nums) { 5 | 6 | int i = 0; 7 | int j = i+1; 8 | int m = nums.size(); 9 | 10 | while( i < m && j < m){ 11 | if(nums[i] == 0 && nums[j] != 0 ){ 12 | 13 | swap(nums[i], nums[j]); 14 | i++; 15 | j++; 16 | } 17 | else 18 | j++; 19 | 20 | if(nums[i] != 0) 21 | i++; 22 | } 23 | } 24 | 25 | int main() { 26 | 27 | vector v1 = {0,1,0,3,12}; 28 | moveZeroes(v1); 29 | 30 | for(int i = 0; i < v1.size(); i++) 31 | cout << v1[i] << " "; 32 | } -------------------------------------------------------------------------------- /Lecture078 Hashmaps in C++/Coding Question.md: -------------------------------------------------------------------------------- 1 | # Coding Quesion 2 | 3 | 1. Maximum Frequency Number: [https://www.codingninjas.com/studio/problems/maximum-frequency-number_920319?leftPanelTab=0&utm_source=youtube&utm_medium=affiliate&utm_campaign=Lovebabbar] 4 | 5 | ``` cpp 6 | 1. int maximumFrequency(vector &arr, int n) { 7 | 8 | unordered_map count; 9 | int maxFreq = 0; 10 | int maxAns = 0; 11 | 12 | for (int i = 0; i < n; i++) { 13 | count[arr[i]]++; 14 | maxFreq = max(maxFreq, count[arr[i]]); 15 | } 16 | 17 | for (int i = 0; i < n; i++) { 18 | 19 | if (maxFreq == count[arr[i]]) { 20 | maxAns = arr[i]; 21 | break; 22 | } 23 | } 24 | return maxAns; 25 | } 26 | ``` 27 | -------------------------------------------------------------------------------- /Lecture028 Dynamic Allocation (Part-1)/dynamicArray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int getSum(int *arr, int n) { 5 | 6 | int sum = 0; 7 | for(int i=0; i> n; 17 | 18 | //variable size array 19 | int* arr = new int[n]; 20 | 21 | //takign inputn in aray 22 | for(int i=0; i> arr[i]; 24 | } 25 | 26 | int ans = getSum(arr, n); 27 | 28 | cout << "answer is "<< ans << endl; 29 | 30 | //case 1 31 | while(true) { 32 | int i = 5; 33 | } 34 | 35 | //case 2 36 | while(true) { 37 | int* ptr = new int; 38 | } 39 | 40 | 41 | 42 | 43 | return 0; 44 | } -------------------------------------------------------------------------------- /Lecture004 Solving Pattern Questions (Part-2)/pattern23.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 1 2 3 4 5 5 4 3 2 1 3 | 1 2 3 4 * * 4 3 2 1 4 | 1 2 3 * * * * 3 2 1 5 | 1 2 * * * * * * 2 1 6 | 1 * * * * * * * * 1 7 | */ 8 | 9 | #include 10 | using namespace std; 11 | 12 | int main() { 13 | 14 | int height; 15 | cin >> height; 16 | 17 | for (int i = 1; i <= height; i++) { 18 | 19 | for (int j = 1; j <= (height - i + 1); j++) 20 | cout << j << " "; 21 | 22 | for (int k = 1; k <= (i - 1); k++) 23 | cout << "* "; 24 | 25 | for (int k = (i - 1); k >= 1; k--) 26 | cout << "* "; 27 | 28 | for (int l = (height - i + 1); l >= 1; l--) 29 | cout << l << " "; 30 | 31 | cout << endl; 32 | } 33 | } -------------------------------------------------------------------------------- /Lecture043 OOPs Day2/HybridInheritance.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class A { 5 | 6 | public: 7 | void func1() { 8 | cout << "Calling function 1" << endl; 9 | } 10 | }; 11 | 12 | class B : public A{ 13 | 14 | public: 15 | void func2() { 16 | cout << "Calling function 2" << endl; 17 | } 18 | }; 19 | 20 | class D { 21 | 22 | public: 23 | void func3() { 24 | cout << "Calling function 3" << endl; 25 | } 26 | }; 27 | 28 | class C : public B, public D { 29 | 30 | }; 31 | 32 | int main() { 33 | 34 | A obj1; 35 | obj1.func1(); 36 | 37 | B obj2; 38 | obj2.func1(); 39 | obj2.func2(); 40 | 41 | D obj3; 42 | obj3.func3(); 43 | 44 | C obj4; 45 | obj4.func1(); 46 | obj4.func2(); 47 | obj4.func3(); 48 | } -------------------------------------------------------------------------------- /Lecture020 Solving LeetCode CodeStudio Questions [Arrays]/reverseArray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | vector reverse(vector v) { 6 | 7 | int s = 0, e = v.size()-1; 8 | 9 | while(s <= e) { 10 | swap(v[s], v[e]); 11 | s++; 12 | e--; 13 | } 14 | return v; 15 | } 16 | 17 | void print(vector v) { 18 | 19 | for(int i = 0; i < v.size(); i++) 20 | cout << v[i] << " "; 21 | 22 | cout << endl; 23 | } 24 | 25 | int main(){ 26 | 27 | vector v ; 28 | 29 | v.push_back(11); 30 | v.push_back(7); 31 | v.push_back(3); 32 | v.push_back(12); 33 | v.push_back(4); 34 | 35 | vector ans = reverse(v); 36 | 37 | cout << "Printing reverse Array" < 2 | #include 3 | using namespace std; 4 | class Solution { 5 | public: 6 | bool searchMatrix(vector >& matrix, int target) { 7 | 8 | int row = matrix.size(); 9 | int col = matrix[0].size(); 10 | 11 | int rowIndex = 0; 12 | int colIndex = col-1; 13 | 14 | while(rowIndex < row && colIndex>=0 ) { 15 | int element = matrix[rowIndex][colIndex]; 16 | 17 | if(element == target) { 18 | return 1; 19 | } 20 | 21 | if(element < target){ 22 | rowIndex++; 23 | } 24 | else 25 | { 26 | colIndex--; 27 | } 28 | } 29 | return 0; 30 | } 31 | }; -------------------------------------------------------------------------------- /Lecture023 2D arrays/wavePrint.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | vector wavePrint(vector > arr, int nRows, int mCols) 5 | { 6 | vector ans; 7 | 8 | for(int col=0; col Bottom to top 12 | 13 | for(int row = nRows-1; row>=0; row--) { 14 | //cout << arr[row][col] <<" "; 15 | ans.push_back(arr[row][col]); 16 | } 17 | } 18 | else 19 | { 20 | // 0 or even iondex -> top to bottom 21 | for(int row = 0; row 2 | using namespace std; 3 | 4 | int& func(int a) { 5 | int num = a; 6 | int& ans = num; 7 | return ans; 8 | } 9 | 10 | int* fun(int n) { 11 | int* ptr = &n; 12 | return ptr; 13 | } 14 | 15 | void update2(int& n) { 16 | n++; 17 | } 18 | 19 | void update1(int n) { 20 | n++; 21 | } 22 | 23 | int main() { 24 | 25 | /* 26 | int i = 5; 27 | 28 | //create a ref variable 29 | 30 | int& j = i; 31 | 32 | cout << i << endl; 33 | i++; 34 | cout << i << endl; 35 | j++; 36 | cout << i << endl; 37 | cout << j << endl; 38 | */ 39 | 40 | int n = 5; 41 | 42 | cout << "Before " << n << endl; 43 | update2(n); 44 | cout << "After " << n << endl; 45 | 46 | 47 | fun(n); 48 | 49 | return 0; 50 | } -------------------------------------------------------------------------------- /Lecture019 C++ STL/STL.md: -------------------------------------------------------------------------------- 1 | ### The STL taught in this lecture are :- 2 | 3 | ## Containers:- 4 | 5 | - Arrays - Static Array 6 | - Vector - Dynamic Array 7 | - Deque - Doubly Ended Queue 8 | - List - Doubly Linked List 9 | - Stack - LIFO Principle 10 | - Queue - FIFO Principle 11 | - Priority Queue - Heap Data Structure 12 | - Max heap - `priority_queue ` 13 | - Min heap - `priority_queue < int, vector, greater >` 14 | 15 | - Set - Store Unique Elements 16 | - Sorted - `set ` (BST implementaion ) 17 | - Unsorted - `unordered_set ` 18 | 19 | - Map - (Key Value pair) 20 | - Sorted - `map ` (Balanced Tree) 21 | - Unsorted - `unordered_map ` 22 | 23 | 24 | ## Algorithms :- 25 | 26 | - Binary Search 27 | - Maximum / Minimum 28 | - Swap 29 | - Reverse 30 | - Rotate 31 | - Sort - intro sort (quick, heap, insertion) -------------------------------------------------------------------------------- /Lecture023 2D arrays/WavePrint.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public static int[] wavePrint(int arr[][], int nRows, int mCols) { 3 | // Write your code here. 4 | int[] ans = new int[nRows * mCols]; // array to store the values of wave and return 5 | 6 | int idx = 0; // index for traversing the ans indexes 7 | 8 | // Here i represents columns & j represents Rows 9 | for(int i = 0; i < mCols; i++){ 10 | 11 | if(i % 2 != 0){ // bottom to top 12 | 13 | for(int j = nRows - 1; j >= 0; j--){ 14 | ans[idx++] = arr[j][i]; 15 | } 16 | } 17 | else { // top to bottom 18 | for(int j = 0; j < nRows; j++){ 19 | ans[idx++] = arr[j][i]; 20 | } 21 | } 22 | } 23 | 24 | return ans; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Lecture025 Intro to Pointers in C++/CodeStudio.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() { 4 | int firstvalue = 5, secondvalue = 15; 5 | char thirdvalue = 'a'; 6 | int * p1, * p2; 7 | char * p3; 8 | p1 = & firstvalue; // p1 = address of firstvalue 9 | p2 = & secondvalue; // p2 = address of secondvalue 10 | p3 = & thirdvalue; // p3 = address of thirdvalue 11 | * p1 = 10; // value pointed to by p1 = 10 12 | * p2 = * p1; // value pointed to by p2 = value pointed to by p1 13 | p1 = p2; // p1 = p2 (value of pointer is copied) 14 | * p1 = 20; // value pointed to by p1 = 20 15 | * p3 = 'b'; // value pointed to by p3 = ‘b ’ 16 | cout << "firstvalue is " << firstvalue << endl; 17 | cout << "secondvalue is " << secondvalue << endl; 18 | cout << "thirdvalue is " << thirdvalue << endl; 19 | return 0; 20 | } -------------------------------------------------------------------------------- /Lecture026 Pointers with Arrays and Functions/pointer_functions.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void print(int *p) { 5 | 6 | cout << *p << endl; 7 | 8 | } 9 | 10 | void update(int *p) { 11 | 12 | // p = p + 1; 13 | //cout << "inside "<< p < 2 | using namespace std; 3 | 4 | int binarySearch (int arr[], int size, int key) { 5 | 6 | int start = 0; 7 | int end = size - 1; 8 | 9 | int mid = ( start + end ) / 2; 10 | 11 | while(start <= end) { 12 | 13 | if(arr[mid] == key) 14 | return mid; 15 | 16 | if(key > arr[mid]) 17 | start = mid + 1; 18 | 19 | else 20 | end = mid - 1; 21 | 22 | mid = (start + end) / 2; 23 | } 24 | return -1; 25 | } 26 | 27 | int main() { 28 | 29 | int even[6] = {2,4,6,8,12,18}; 30 | int odd[5] = {3, 8, 11, 14, 16}; 31 | 32 | int evenIndex = binarySearch(even, 6, 6); 33 | 34 | cout << "Index of 6 is " << evenIndex << endl; 35 | 36 | int oddIndex = binarySearch(odd, 5, 14); 37 | 38 | cout << "Index of 14 is " << oddIndex << endl; 39 | } -------------------------------------------------------------------------------- /Lecture045 Linked List Day2/ReverseLL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Node { 5 | 6 | public: 7 | Node *next; 8 | int data; 9 | 10 | Node(int d){ 11 | this -> data = d; 12 | this -> next = NULL; 13 | } 14 | }; 15 | 16 | void inserthead (Node* &head, int data) { 17 | 18 | Node* temp = new Node(data); 19 | temp -> next = head; 20 | head = temp; 21 | } 22 | 23 | void print(Node* &head){ 24 | 25 | Node* temp = head; 26 | while(temp != NULL){ 27 | cout << temp->data << endl; 28 | temp = temp ->next; 29 | } 30 | } 31 | 32 | int main() { 33 | 34 | Node* node1 = new Node(10); //created a node 35 | Node* head = node1; //head point to node 1 36 | print(head); 37 | inserthead(head, 15); 38 | print(head); 39 | 40 | } -------------------------------------------------------------------------------- /Lecture008 Switch Statement & Functions/calculator.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | 6 | int a, b; 7 | 8 | cout << "Enter the value of a = "; 9 | cin >> a; 10 | 11 | cout << "Enter the value of b = "; 12 | cin >> b; 13 | 14 | char op; 15 | cout << "Enter the Operation you want to perform = "; 16 | cin >> op; 17 | 18 | switch( op ) { 19 | 20 | case '+': cout << (a + b) << endl; 21 | break; 22 | 23 | case '-': cout << (a - b) << endl; 24 | break; 25 | 26 | case '*': cout << (a * b) << endl; 27 | break; 28 | 29 | case '/': cout << (a / b) << endl; 30 | break; 31 | 32 | case '%': cout << (a % b) << endl; 33 | break; 34 | 35 | default: cout << "Please enter a valid Operation " << endl; 36 | } 37 | } -------------------------------------------------------------------------------- /Lecture074 Heaps in C++/Coding Question.md: -------------------------------------------------------------------------------- 1 | # Coding Quesion 2 | 3 | 1. Build Min Heap: [https://www.codingninjas.com/studio/problems/build-min-heap_1171167?leftPanelTab=0&utm_source=youtube&utm_medium=affiliate&utm_campaign=Lovebabbar] 4 | 5 | ``` cpp 6 | 1. void heapify (vector &arr, int n, int i) { 7 | 8 | int smallest = i; 9 | int left = 2*i + 1; 10 | int right = (2*i) + 2; 11 | 12 | if(left < n && arr[smallest] > arr[left]) 13 | smallest = left; 14 | 15 | if(right < n && arr[smallest] > arr[right]) 16 | smallest = right; 17 | 18 | if(smallest != i) { 19 | swap(arr[smallest], arr[i]); 20 | heapify(arr, n, smallest); 21 | } 22 | } 23 | 24 | vector buildMinHeap(vector &arr) { 25 | 26 | int n = arr.size(); 27 | for(int i = (n/2)-1; i >= 0; i--) 28 | heapify(arr, n, i); 29 | 30 | return arr; 31 | } 32 | ``` 33 | -------------------------------------------------------------------------------- /Lecture033 Recursion Day3/binarySearch.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void print(int arr[], int s, int e) { 5 | 6 | for (int i = s; i <= e; i++) 7 | cout << arr[i] << " "; 8 | 9 | cout << endl; 10 | } 11 | 12 | bool binarySearch(int *arr, int s, int e , int k) { 13 | 14 | //base case 15 | //element not found 16 | if(s > e) 17 | return false; 18 | 19 | int mid = s + (e - s) / 2; 20 | 21 | //element found 22 | if(arr[mid] == k) 23 | return true; 24 | 25 | if(arr[mid] < k) 26 | return binarySearch(arr, mid + 1, e, k); 27 | 28 | else 29 | return binarySearch(arr, s, mid - 1, k); 30 | } 31 | 32 | int main() { 33 | 34 | int arr[] = {2, 4, 6, 10, 14, 18, 22, 38, 49, 55, 222}; 35 | int size = 11; 36 | int key = 222; 37 | 38 | cout << "Present or not " << binarySearch(arr, 0, size - 1, key) << endl; 39 | } -------------------------------------------------------------------------------- /Lecture085 Introduction to Graphs/Coding Question.md: -------------------------------------------------------------------------------- 1 | # Coding Quesion 2 | 3 | 1. Creating and Printing: [https://www.codingninjas.com/studio/problems/create-a-graph-and-print-it_1214551?topList=love-babbar-dsa-sheet-problems&leftPanelTab=0&utm_source=youtube&utm_medium=affiliate&utm_campaign=Lovebabbar] 4 | 5 | ``` cpp 6 | 1. vector < vector < int >> printAdjacency(int n, int m, vector < vector < int >> & edges) { 7 | 8 | vector ans[n]; 9 | 10 | for (int i = 0; i < m; i++) { 11 | int u = edges[i][0]; 12 | int v = edges[i][1]; 13 | 14 | ans[u].push_back(v); 15 | ans[v].push_back(u); 16 | } 17 | 18 | vector < vector < int >> adj(n); 19 | 20 | for(int i = 0; i < n; i++) { 21 | adj[i].push_back(i); 22 | 23 | for (int j = 0; j < ans[i].size(); j++) { 24 | adj[i].push_back(ans[i][j]); 25 | } 26 | } 27 | return adj; 28 | } 29 | ``` 30 | -------------------------------------------------------------------------------- /Lecture010 Solving LeetCode CodeStudio Questions Arrays/sort_01.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void printArray(int arr[], int n) { 5 | 6 | for(int i = 0; i < n; i++) 7 | cout << arr[i] << " "; 8 | 9 | cout << endl; 10 | } 11 | 12 | void sortOne(int arr[], int n) { 13 | 14 | int left = 0, right = n-1; 15 | 16 | while(left < right) { 17 | 18 | while(arr[left] == 0 && left < right ) 19 | left++; 20 | 21 | while(arr[right]==1 && left < right) 22 | right--; 23 | 24 | //agar yha pohoch gye ho, iska matlab 25 | //arr[left]==1 and arr[right]==0 26 | if(left 2 | using namespace std; 3 | 4 | void print(int arr[], int n) { 5 | 6 | cout << "Size of array is " << n << endl; 7 | 8 | for (int i = 0; i < n; i++) 9 | cout << arr[i] << " "; 10 | 11 | cout << endl; 12 | } 13 | 14 | bool linearSearch(int arr[], int size, int k) { 15 | 16 | print(arr,size); 17 | 18 | //base case 19 | if(size == 0) 20 | return false; 21 | 22 | if(arr[0] == k) 23 | return true; 24 | 25 | else { 26 | bool remainingPart = linearSearch(arr+1, size-1, k ); 27 | return remainingPart; 28 | } 29 | } 30 | 31 | int main() { 32 | 33 | int arr[5] = {3,5,1,2,6}; 34 | int size = 5; 35 | int key = 6; 36 | bool ans = linearSearch(arr, size, key); 37 | 38 | if(ans) 39 | cout << "Present " << endl; 40 | 41 | else 42 | cout << "Absent " << endl; 43 | } -------------------------------------------------------------------------------- /Lecture023 2D arrays/search2DMatrix1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | class Solution { 5 | public: 6 | bool searchMatrix(vector >& matrix, int target) { 7 | 8 | int row = matrix.size(); 9 | int col = matrix[0].size(); 10 | 11 | int start = 0; 12 | int end = row*col-1; 13 | 14 | int mid = start + (end-start)/2; 15 | 16 | while(start<=end) { 17 | 18 | int element = matrix[mid/col][mid%col]; 19 | 20 | if(element == target) { 21 | return 1; 22 | } 23 | 24 | if(element < target) { 25 | start = mid+1; 26 | } 27 | else 28 | { 29 | end = mid-1; 30 | } 31 | mid = start + (end-start)/2; 32 | } 33 | return 0; 34 | } 35 | }; -------------------------------------------------------------------------------- /Lecture024 Maths for DSA/sieve.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | //using Sieve 6 | void sieve(bool prime[]) { 7 | 8 | //setting TRUE for every number 9 | for(int i=2;i<=1000000;i++){ 10 | prime[i]=1; 11 | } 12 | 13 | for(int i = 2; i<=1000000; i++) { 14 | 15 | if(prime[i]){ 16 | //iske saare multiple mark krde - non prime 17 | for(int j = 2*i; j<=1000000; j+=i){ 18 | prime[j] = 0; 19 | } 20 | } 21 | } 22 | prime[0] = prime[1] = 0; 23 | } 24 | 25 | int main() { 26 | 27 | bool prime[1000001]; 28 | sieve(prime); 29 | 30 | int n; 31 | cin >> n; 32 | while(n!=-1) { 33 | if(prime[n]) { 34 | cout << "It is a Prime Number" << endl; 35 | } 36 | else{ 37 | cout << " It is not a Prime Number" << endl; 38 | } 39 | cin >> n; 40 | } 41 | 42 | return 0; 43 | } -------------------------------------------------------------------------------- /Lecture060 Introduction to the Queues/queue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | cout << endl; 8 | 9 | queue q; 10 | 11 | q.push(1); 12 | q.push(5); 13 | q.push(7); 14 | q.push(11); 15 | 16 | cout << "Rear of Queue = " << q.back() << endl; 17 | 18 | cout << "Size of queue = " << q.size() << endl; // 4 19 | 20 | q.pop(); 21 | 22 | cout << "Front of Queue = " << q.front() << endl; // 5 23 | 24 | cout << "Size of queue = " << q.size() << endl; // 3 25 | 26 | if(q.empty()) 27 | cout << "Queue is empty" << endl; 28 | else 29 | cout << "Queue is not empty" << endl; 30 | 31 | q.pop(); 32 | q.pop(); 33 | q.pop(); 34 | 35 | cout << "Size of queue = " << q.size() << endl; 36 | 37 | if(q.empty()) 38 | cout << "Queue is empty" << endl; 39 | else 40 | cout << "Queue is not empty" << endl; 41 | 42 | cout << endl; 43 | } -------------------------------------------------------------------------------- /Lecture024 Maths for DSA/PrimeNumber.java: -------------------------------------------------------------------------------- 1 | package MathsForDSA; 2 | import java.util.Scanner; 3 | 4 | public class PrimeNumber { 5 | // Check Whether a Given Number is Prime or Not 6 | public static void main(String [] args){ 7 | // Creating Scanner Object for taking inputs from the USER 8 | Scanner sc = new Scanner(System.in); 9 | // Take any Number as a Input from the USER 10 | System.out.print(" Enter Any Number: "); 11 | int num = sc.nextInt(); 12 | // Display is the Number Entered by USER is Prime or NOT 13 | System.out.println(num + " is: " + isPrime(num)); 14 | } 15 | 16 | static String isPrime(int num){ 17 | if(num == 0) 18 | return " Neither Prime Nor Composite "; 19 | if( num < 0 ) 20 | return " Not Prime "; 21 | for(int i=2; i*i < num; i++){ 22 | if(num % i == 0) 23 | return " Not Prime "; 24 | } 25 | return " Prime "; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Lecture012 Binary Search/Optimize_binarySearch.cpp: -------------------------------------------------------------------------------- 1 | // The mid is in the integer range so it will not be OutofBound 2 | 3 | #include 4 | using namespace std; 5 | 6 | int binarySearch (int arr[], int size, int key) { 7 | 8 | int start = 0; 9 | int end = size - 1; 10 | 11 | int mid = start + ( end - start ) / 2; 12 | 13 | while(start <= end) { 14 | 15 | if(arr[mid] == key) 16 | return mid; 17 | 18 | if(key > arr[mid]) 19 | start = mid + 1; 20 | 21 | else 22 | end = mid - 1; 23 | 24 | mid = start + ( end - start ) / 2; 25 | } 26 | return -1; 27 | } 28 | 29 | int main() { 30 | 31 | int even[6] = {2,4,6,8,12,18}; 32 | int odd[5] = {3, 8, 11, 14, 16}; 33 | 34 | int evenIndex = binarySearch(even, 6, 6); 35 | 36 | cout << "Index of 6 is " << evenIndex << endl; 37 | 38 | int oddIndex = binarySearch(odd, 5, 14); 39 | 40 | cout << "Index of 14 is " << oddIndex << endl; 41 | } 42 | -------------------------------------------------------------------------------- /Lecture029 Dynamic Allocation (Part-2)/2dArray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | int row; 7 | cin >> row; 8 | 9 | int col; 10 | cin >> col; 11 | 12 | //creating a 2D array 13 | int** arr = new int*[row]; 14 | for(int i=0; i> arr[i][j]; 22 | } 23 | } 24 | 25 | //taking output 26 | cout << endl; 27 | for(int i=0; i> 1; 15 | } 16 | return count; 17 | } 18 | ``` 19 | 20 | ``` cpp 21 | 2. int subtractProductAndSum(int n) { 22 | int product = 1; 23 | int sum = 0; 24 | 25 | while(n != 0){ 26 | int digit = n % 10; 27 | product = product * digit; 28 | sum = sum + digit; 29 | 30 | n = n/10; 31 | } 32 | 33 | int ans = product - sum; 34 | return ans; 35 | } 36 | ``` -------------------------------------------------------------------------------- /Lecture003 If-Else, While loop & Lots of Patterns/conditionals.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | //int a; 6 | //cin>>a; 7 | 8 | //cout<<" value of n is :" << n <0) { 13 | cout<<" A is Positive" << endl; 14 | } 15 | else{ 16 | cout<<" A is negative" << endl; 17 | } 18 | 19 | int a,b; 20 | 21 | cout<<"Enter the value of a "<>a; 23 | cout<<"Enter the value of b "<>b; 25 | 26 | if(a>b) { 27 | cout<<" A is greater " << endl; 28 | } 29 | if(b>a) { 30 | cout<<" B is greater " << endl; 31 | } 32 | 33 | */ 34 | 35 | int a ; 36 | cout << " enter the value of a " << endl; 37 | cin >> a; 38 | 39 | if (a > 0) { 40 | cout << "A is positive" << endl; 41 | } 42 | else if (a < 0) { 43 | cout << "A is negative" < 2 | #include 3 | using namespace std; 4 | 5 | void merge(int arr1[], int n, int arr2[], int m, int arr3[]){ 6 | 7 | int i = 0, j = 0; 8 | int k = 0; 9 | while( i < n && j < m){ 10 | 11 | if(arr1[i] < arr2[j]) 12 | arr3[k++] = arr1[i++]; 13 | 14 | else 15 | arr3[k++] = arr2[j++]; 16 | } 17 | 18 | //copy first array k element ko 19 | while(i < n) 20 | arr3[k++] = arr1[i++]; 21 | 22 | //copy kardo second array k remaining element ko 23 | while(j < m) 24 | arr3[k++] = arr2[j++]; 25 | } 26 | 27 | void print(int ans[], int n) { 28 | for(int i = 0; i < n; i++) { 29 | cout<< ans[i] <<" "; 30 | } 31 | cout << endl; 32 | } 33 | 34 | int main() { 35 | 36 | int arr1[5] = {1,3,5,7,9}; 37 | int arr2[3] = {2,4,6}; 38 | 39 | int arr3[8] = {0}; 40 | 41 | merge(arr1, 5, arr2, 3, arr3); 42 | 43 | print(arr3, 8); 44 | } -------------------------------------------------------------------------------- /Lecture017 Bubble Sort/bubbleSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | void bubbleSort(vector &arr, int n) { 6 | 7 | for(int i = 1; i < n; i++) { 8 | //for round 1 to n-1 9 | bool swapped = false; 10 | 11 | for(int j =0; j < n - i; j++) { 12 | 13 | //process element till n-i th index 14 | if(arr[j] > arr[j+1]) { 15 | swap(arr[j], arr[j+1]); 16 | swapped = true; 17 | } 18 | } 19 | 20 | if(swapped == false) { 21 | //already sorted 22 | break; 23 | } 24 | } 25 | } 26 | 27 | void printArray(vector &array, int size) { 28 | 29 | for (int i = 1; i < size; i++) 30 | cout << array[i] << " "; 31 | } 32 | 33 | int main(){ 34 | 35 | vector arr = {20, 12, 10, 15, 2}; 36 | int size = sizeof(arr) / sizeof(arr[0]); 37 | bubbleSort (arr, size); 38 | printArray(arr,size); 39 | cout << endl << arr.capacity(); 40 | } 41 | -------------------------------------------------------------------------------- /Lecture086 BFS Traversal in Graph/Coding Question.md: -------------------------------------------------------------------------------- 1 | # Coding Quesion 2 | 3 | 1. BFS in Graph: [https://www.codingninjas.com/studio/problems/bfs-in-graph_973002?topList=love-babbar-dsa-sheet-problems&leftPanelTab=0&utm_source=youtube&utm_medium=affiliate&utm_campaign=Lovebabbar] 4 | 5 | ``` cpp 6 | 1. void bfs (vector> &adj, unordered_map < int, bool > &visited, vector &ans, int node) { 7 | 8 | queue q; 9 | q.push(node); 10 | visited[node] = 1; 11 | 12 | while(!q.empty()) { 13 | 14 | int frontnode = q.front(); 15 | q.pop(); 16 | 17 | ans.push_back(frontnode); 18 | 19 | for (auto i:adj[frontnode]) { 20 | if(!visited[i]){ 21 | q.push(i); 22 | visited[i] = 1; 23 | } 24 | } 25 | } 26 | } 27 | 28 | vector bfsTraversal(int n, vector> &adj){ 29 | 30 | unordered_map < int, bool > visited; 31 | vector ans; 32 | bfs(adj, visited, ans, 0); 33 | 34 | return ans; 35 | } 36 | ``` 37 | -------------------------------------------------------------------------------- /Lecture038 Recursion Day8/keypad.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | private: 3 | void solve(string digit, string output, int index, vector& ans, string mapping[] ) { 4 | 5 | //base case 6 | if(index >= digit.length()) { 7 | ans.push_back(output); 8 | return; 9 | } 10 | 11 | int number = digit[index] - '0'; 12 | string value = mapping[number]; 13 | 14 | for(int i=0; i letterCombinations(string digits) { 23 | vector ans; 24 | if(digits.length()==0) 25 | return ans; 26 | string output; 27 | int index = 0; 28 | string mapping[10] = {"", "", "abc", "def", "ghi", "jkl","mno","pqrs","tuv","wxyz"}; 29 | solve(digits, output, index, ans, mapping); 30 | return ans; 31 | } 32 | }; -------------------------------------------------------------------------------- /Lecture009 Introduction to Arrays in C++/maxMin.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int getMin(int num[], int n) { 5 | 6 | int mini = INT_MAX; 7 | 8 | for(int i = 0; i < n; i++) { 9 | 10 | mini = min( mini, num[i]); 11 | 12 | //if(num[i] < min){ 13 | // min = num[i]; 14 | //} 15 | } 16 | //returning min value 17 | return mini; 18 | } 19 | 20 | int getMax(int num[], int n) { 21 | 22 | int maxi = INT_MIN; 23 | 24 | for(int i = 0; i < n; i++) { 25 | 26 | maxi = max(maxi, num[i]); 27 | 28 | // if(num[i] > max){ 29 | // max = num[i]; 30 | // } 31 | } 32 | //returning max value 33 | return maxi; 34 | } 35 | 36 | int main() { 37 | 38 | int size; 39 | cin >> size; 40 | 41 | int num[100]; 42 | 43 | //taking input in array 44 | for(int i = 0; i < size; i++) 45 | cin >> num[i]; 46 | 47 | cout << "Maximum value is = " << getMax(num, size) << endl; 48 | cout << "Minimum value is = " << getMin(num, size) << endl; 49 | 50 | } -------------------------------------------------------------------------------- /Lecture074 Heaps in C++/priority_queue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main () { 6 | 7 | //max-heap 8 | priority_queue pq; 9 | pq.push(4); 10 | pq.push(3); 11 | pq.push(2); 12 | pq.push(5); 13 | cout << "Root node = " << pq.top() << endl; 14 | 15 | pq.pop(); 16 | cout << "Root node = " << pq.top() << endl; 17 | 18 | cout << "Size of heap = " << pq.size() << endl; 19 | 20 | if(pq.empty()) 21 | cout << "Heap is empty" << endl; 22 | else 23 | cout << "Heap is not empty" << endl; 24 | 25 | //min-heap 26 | priority_queue , greater > min; 27 | min.push(4); 28 | min.push(3); 29 | min.push(2); 30 | min.push(5); 31 | cout << "Root node = " << min.top() << endl; 32 | 33 | min.pop(); 34 | cout << "Root node = " << min.top() << endl; 35 | 36 | cout << "Size of heap = " << min.size() << endl; 37 | 38 | if(min.empty()) 39 | cout << "Heap is empty" << endl; 40 | else 41 | cout << "Heap is not empty" << endl; 42 | } -------------------------------------------------------------------------------- /Lecture043 OOPs Day2/inheritance.cpp: -------------------------------------------------------------------------------- 1 | // Modes of Inheritance (Access specifiers) 2 | 3 | #include 4 | using namespace std; 5 | 6 | class Human { 7 | 8 | public: 9 | int height; 10 | 11 | public: 12 | int weight; 13 | 14 | private: 15 | int age; 16 | 17 | public: 18 | int getAge() { 19 | return this->age; 20 | } 21 | 22 | void setWeight(int w) { 23 | this->weight = w; 24 | } 25 | }; 26 | 27 | class Male: private Human { 28 | 29 | public: 30 | string color; 31 | 32 | void sleep() { 33 | cout << "Male Sleeping" << endl; 34 | } 35 | 36 | int getHeight() { 37 | return this->height; 38 | } 39 | 40 | }; 41 | 42 | 43 | 44 | int main() { 45 | 46 | Male m1; 47 | //cout << m1.height << endl; 48 | 49 | /* 50 | Male object1; 51 | cout << object1.age << endl; 52 | cout << object1.weight << endl; 53 | cout << object1.height << endl; 54 | 55 | cout << object1.color << endl; 56 | 57 | object1.setWeight(84); 58 | cout << object1.weight << endl; 59 | object1.sleep(); 60 | */ 61 | } -------------------------------------------------------------------------------- /Lecture024 Maths for DSA/SieveOfEratosthenes.java: -------------------------------------------------------------------------------- 1 | package MathsForDSA; 2 | 3 | public class SieveOfEratosthenes { 4 | // Sieve of Eratosthenes 5 | // Time Complexity O(N * log(logN)) 6 | // Space Complexity O(N) 7 | public static void main(String [] args){ 8 | int n = 40; 9 | boolean [] primes = new boolean[n + 1]; 10 | // By Default all the values in the Boolean Array are false 11 | // System.out.println(Arrays.toString(primes)); 12 | sieve(n , primes); 13 | 14 | } 15 | // False in Array Means Number is Prime 16 | static void sieve(int n, boolean [] primes){ 17 | for(int i=2; i*i <= n; i++){ 18 | // if False then Make all Multiple of i to true 19 | if(!primes[i]){ 20 | // Make all Multiples of i to True 21 | for(int j = i*2; j <= n ; j+= i){ 22 | primes[j] = true; 23 | } 24 | } 25 | } 26 | // if False then Print the Index 27 | for(int i=2; i<= n; i++){ 28 | if(!primes[i]) 29 | System.out.print(i + " "); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Lecture025 Intro to Pointers in C++/play.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | //pointer to int is created, and pointing to some garbage address 7 | //int *p = 0; 8 | 9 | //cout << *p << endl; 10 | 11 | /* 12 | int i = 5; 13 | 14 | int *q = &i; 15 | cout << q << endl; 16 | cout << *q << endl; 17 | 18 | int *p = 0; 19 | p = &i; 20 | 21 | cout << p << endl; 22 | cout << *p << endl; 23 | */ 24 | 25 | int num = 5; 26 | int a = num; 27 | cout << "a before " << num << endl; 28 | a++; 29 | cout << "a after " << num << endl; 30 | 31 | 32 | int *p = # 33 | cout << "before " << num << endl; 34 | (*p)++; 35 | cout << "after " << num << endl; 36 | 37 | //copying a pointer 38 | int *q = p; 39 | cout << p <<" - " << q << endl; 40 | cout << *p <<" - " << *q << endl; 41 | 42 | //important concept 43 | int i = 3; 44 | int *t = &i; 45 | //cout << (*t)++ << endl; 46 | *t = *t +1; 47 | cout << *t << endl; 48 | cout << "before t " << t << endl; 49 | t = t + 1; 50 | cout << "after t " << t << endl; 51 | 52 | } -------------------------------------------------------------------------------- /Lecture074 Heaps in C++/HeapSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void heapify (int arr[], int n, int i) { 5 | 6 | int largest = i; 7 | int left = 2*i; 8 | int right = (2*i)+1; 9 | 10 | if(left <= n && arr[largest] < arr[left]) 11 | largest = left; 12 | 13 | if(right <= n && arr[largest] < arr[right]) 14 | largest = right; 15 | 16 | if(largest != i) { 17 | swap(arr[largest], arr[i]); 18 | heapify(arr, n, largest); 19 | } 20 | } 21 | 22 | void heapSort (int arr[], int n) { 23 | 24 | int size = n; 25 | 26 | while (size > 1) { 27 | 28 | //step 1 swap root node to last node 29 | swap(arr[size], arr[1]); 30 | //step 2 Decrease size 31 | size--; 32 | //step 3 Root node to its correct position 33 | heapify(arr, size, 1); 34 | } 35 | } 36 | 37 | int main () { 38 | 39 | int arr[6] = {-1, 54, 53, 55, 52, 50}; 40 | int n = 5; 41 | 42 | for(int i = n/2 ; i > 0; i--) 43 | heapify(arr, n, i); 44 | 45 | cout << "Heap Sort: " << endl; 46 | for(int i = 1 ; i <= n; i++) 47 | cout << arr[i] << " "; 48 | 49 | cout << endl; 50 | } -------------------------------------------------------------------------------- /Lecture037 Recursion Day7/subsets.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | private: 6 | void solve(vector nums, vector output, int index, vector>& ans) { 7 | //base case 8 | if(index >= nums.size()) { 9 | ans.push_back(output); 10 | return ; 11 | } 12 | 13 | //exclude 14 | solve(nums, output, index+1, ans); 15 | 16 | //include 17 | int element = nums[index]; 18 | output.push_back(element); 19 | solve(nums, output, index+1, ans); 20 | 21 | } 22 | 23 | public: 24 | vector> subsets(vector& nums) { 25 | 26 | vector> ans; 27 | vector output; 28 | int index = 0; 29 | solve(nums, output, index, ans); 30 | return ans; 31 | } 32 | }; 33 | 34 | int main() { 35 | vector arr = {1,2,3}; 36 | Solution s1; 37 | 38 | vector> ans = s1.subsets(arr); 39 | 40 | for(int i = 0; i <= ans.size(); i++){ 41 | for(int j = 0; j <= ans.size(); j++){ 42 | cout << ans[i][j]; 43 | } 44 | } 45 | } -------------------------------------------------------------------------------- /Lecture045 Linked List Day2/ReverseLLRecursion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | template 5 | 6 | class LinkedListNode { 7 | public: 8 | 9 | LinkedListNode *next; 10 | int data; 11 | LinkedListNode(int data) { 12 | this->data = data; 13 | this->next = NULL; 14 | } 15 | }; 16 | 17 | void recursion(LinkedListNode* &head, LinkedListNode* curr, LinkedListNode* prev) { 18 | 19 | //base condition 20 | if (curr == NULL) { 21 | head = prev; 22 | return; 23 | } 24 | 25 | //recurence relation 26 | LinkedListNode* forward = curr->next; 27 | curr->next = prev; 28 | 29 | //recursion 30 | recursion(head, forward, curr); 31 | } 32 | 33 | 34 | LinkedListNode *reverseLinkedList(LinkedListNode *head) 35 | { 36 | 37 | if(head == NULL || head -> next == NULL){ 38 | return head; 39 | } 40 | 41 | LinkedListNode* prev = NULL; 42 | LinkedListNode* curr = head; 43 | 44 | recursion(head,curr,prev); 45 | return head; 46 | } 47 | 48 | int main() { 49 | LinkedListNode* node1 = new LinkedListNode(10); 50 | } -------------------------------------------------------------------------------- /Lecture078 Hashmaps in C++/unorder_map.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | 8 | //creation 9 | unordered_map m; 10 | 11 | //insertion 12 | //1 13 | pair p = make_pair("babbar", 3); 14 | m.insert(p); 15 | 16 | //2 17 | pair pair2("love", 2); 18 | m.insert(pair2); 19 | 20 | //3 21 | m["mera"] = 1; 22 | 23 | //Searching 24 | cout << m["mera"] << endl; 25 | cout << m.at("love") << endl; 26 | 27 | //cout << m.at("unknown") << endl; Throw an error 28 | cout << m["unknown"] << endl; // Give entry to the map 29 | 30 | //Size 31 | cout << "Size of map = " << m.size() << endl; 32 | 33 | //to check presence 34 | cout << m.count("love") << endl; 35 | 36 | //Delete an element 37 | m.erase("love"); 38 | 39 | //Traverse 40 | //1 41 | for (auto i:m) 42 | cout << i.first << " " << i.second << endl; 43 | 44 | //2 45 | unordered_map :: iterator it = m.begin(); 46 | 47 | while(it != m.end()){ 48 | cout << it->first << " " << it->second << endl; 49 | it++; 50 | } 51 | 52 | } -------------------------------------------------------------------------------- /Lecture085 Introduction to Graphs/GraphList.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | class graph { 7 | public: 8 | 9 | unordered_map > adj; 10 | 11 | void addEdge (int u, int v, bool direction) { 12 | //direction = 0 -> undirected graph 13 | //direction = 1 -> directed graph 14 | 15 | //create an edge from u to v 16 | adj[u].push_back(v); 17 | 18 | //for undirected graph 19 | if(direction == 0) 20 | adj[v].push_back(u); 21 | } 22 | 23 | void printAdjList() { 24 | 25 | for (auto i:adj) { 26 | cout << i.first << "-> "; 27 | 28 | for(auto j: i.second) 29 | cout << j << ", "; 30 | 31 | cout << endl; 32 | } 33 | } 34 | }; 35 | 36 | int main() { 37 | 38 | graph g; 39 | int n; 40 | cout << "Enter the no. of nodes = "; 41 | cin >> n; 42 | int m; 43 | cout << "Enter the no. of edges = "; 44 | cin >> m; 45 | 46 | for (int i = 0; i < m; i++) { 47 | int u, v; 48 | cin >> u >> v; 49 | g.addEdge(u, v, 0); 50 | } 51 | 52 | g.printAdjList(); 53 | } -------------------------------------------------------------------------------- /Lecture024 Maths for DSA/SquareRootOfANumber.java: -------------------------------------------------------------------------------- 1 | package MathsForDSA; 2 | import java.util.Scanner; 3 | 4 | public class SquareRootOfANumber { 5 | // Find Square Root of a Number Using Binary Search 6 | public static void main(String [] args){ 7 | // Creating Scanner Object for taking Input from the USER 8 | Scanner sc = new Scanner(System.in); 9 | // Take Any Number as a Input from the USER 10 | System.out.print(" Enter Any Number: "); 11 | int num = sc.nextInt(); 12 | // Display the Square Root of Number 13 | System.out.println(" Square Root of " + num + " is: " + sqrt(num)); 14 | } 15 | 16 | static int sqrt(int num){ 17 | // Apply Simple Binary Search to Find the Square Root 18 | int start = 0; 19 | int end = num; 20 | while(start <= end){ 21 | // Find Mid 22 | int mid = start + (end - start)/2; 23 | if(mid * mid == num) 24 | return mid; 25 | else if(mid * mid > num) 26 | end = mid - 1; 27 | else 28 | start = mid + 1; 29 | } 30 | // Otherwise return Floor Value of the Square Root 31 | return end; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Lecture085 Introduction to Graphs/graphTemplete.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | template < typename T > 7 | 8 | class graph { 9 | public: 10 | 11 | unordered_map > adj; 12 | 13 | void addEdge (T u, T v, bool direction) { 14 | //direction = 0 -> undirected graph 15 | //direction = 1 -> directed graph 16 | 17 | //create an edge from u to v 18 | adj[u].push_back(v); 19 | 20 | //for undirected graph 21 | if(direction == 0) 22 | adj[v].push_back(u); 23 | } 24 | 25 | void printAdjList() { 26 | 27 | for (auto i:adj) { 28 | cout << i.first << "-> "; 29 | 30 | for(auto j: i.second) 31 | cout << j << ", "; 32 | 33 | cout << endl; 34 | } 35 | } 36 | }; 37 | 38 | int main() { 39 | 40 | graph g; 41 | int n; 42 | cout << "Enter the no. of nodes = "; 43 | cin >> n; 44 | int m; 45 | cout << "Enter the no. of edges = "; 46 | cin >> m; 47 | 48 | for (int i = 0; i < m; i++) { 49 | int u, v; 50 | cin >> u >> v; 51 | g.addEdge(u, v, 0); 52 | } 53 | 54 | g.printAdjList(); 55 | } -------------------------------------------------------------------------------- /Lecture092 Cycle Detection in Directed Graph (Using BFS)/Coding Question.md: -------------------------------------------------------------------------------- 1 | # Coding Quesion 2 | 3 | 1. Detect Cycle In A Directed Graph: [https://www.codingninjas.com/studio/problems/detect-cycle-in-a-directed-graph_1062626?leftPanelTab=0&utm_source=youtube&utm_medium=affiliate&utm_campaign=Lovebabbar] 4 | 5 | ``` cpp 6 | 1. int detectCycleInDirectedGraph(int n, vector < pair < int, int >> & edges) { 7 | 8 | // create an adjacency list 9 | vector> adj(n+1); 10 | vector indegree(n+1,0); 11 | 12 | for(auto i:edges) { 13 | adj[i.first].push_back(i.second); 14 | indegree[i.second]++; 15 | } 16 | 17 | // 0 indegree push in queue 18 | queue q; 19 | for(int i = 1; i <= n; i++) { 20 | if(indegree[i] == 0) 21 | q.push(i); 22 | } 23 | 24 | //BFS algorithm 25 | int cnt = 0; 26 | while (!q.empty()){ 27 | int front = q.front(); 28 | q.pop(); 29 | cnt++; 30 | 31 | for(auto neighbour:adj[front]) { 32 | indegree[neighbour]--; 33 | if(indegree[neighbour] == 0) 34 | q.push(neighbour); 35 | } 36 | } 37 | 38 | if (cnt == n) 39 | return false; 40 | else 41 | return true; 42 | } 43 | ``` 44 | -------------------------------------------------------------------------------- /Lecture090 Topological Sort ( Using DFS )/Coding Question.md: -------------------------------------------------------------------------------- 1 | # Coding Quesion 2 | 3 | 1. Topological Sort: [https://www.codingninjas.com/studio/problems/topological-sort_982938?topList=love-babbar-dsa-sheet-problems&leftPanelTab=0&utm_source=youtube&utm_medium=affiliate&utm_campaign=Lovebabbar&leftPanelTabValue=PROBLEM] 4 | 5 | ``` cpp 6 | 1. void topoSort (int node, vector &visited, stack &s, unordered_map > &adj) { 7 | 8 | visited[node] = 1; 9 | 10 | for(auto neighbour: adj[node]) { 11 | if(!visited[neighbour]) 12 | topoSort(neighbour, visited, s, adj); 13 | } 14 | s.push(node); 15 | } 16 | vector topologicalSort(vector> &edges, int v, int e) { 17 | 18 | unordered_map > adj; 19 | for (int i = 0; i < e; i++){ 20 | int u = edges[i][0]; 21 | int v = edges[i][1]; 22 | 23 | adj[u].push_back(v); 24 | } 25 | 26 | vector visited(v); 27 | stack s; 28 | 29 | for(int i = 0; i < v; i++) { 30 | if(!visited[i]) 31 | topoSort(i, visited, s, adj); 32 | } 33 | 34 | vector ans; 35 | 36 | while(!s.empty()) { 37 | ans.push_back(s.top()); 38 | s.pop(); 39 | } 40 | 41 | return ans; 42 | } 43 | ``` 44 | -------------------------------------------------------------------------------- /Lecture087 DFS Traversal in Graph/Coding Question.md: -------------------------------------------------------------------------------- 1 | # Coding Quesion 2 | 3 | 1. DFS Traversal: [https://www.codingninjas.com/studio/problems/dfs-traversal_630462?leftPanelTab=0&utm_source=youtube&utm_medium=affiliate&utm_campaign=Lovebabbar&leftPanelTabValue=PROBLEM] 4 | 5 | ``` cpp 6 | 1. void dfs (int node, unordered_map > &adj, unordered_map &visited, vector &component) { 7 | 8 | component.push_back(node); 9 | visited[node] = 1; 10 | 11 | for(auto i:adj[node]){ 12 | if(!visited[i]){ 13 | dfs(i, adj, visited, component); 14 | } 15 | } 16 | } 17 | 18 | vector> depthFirstSearch(int V, int E, vector> &edges) { 19 | 20 | unordered_map > adj; 21 | 22 | for(int i = 0; i < edges.size(); i++) { 23 | int u = edges[i][0]; 24 | int v = edges[i][1]; 25 | 26 | adj[u].push_back(v); 27 | adj[v].push_back(u); 28 | } 29 | 30 | vector > ans; 31 | unordered_map visited; 32 | 33 | for(int i = 0; i < V; i++) { 34 | if(!visited[i]){ 35 | vector component; 36 | dfs(i, adj, visited, component); 37 | ans.push_back(component); 38 | } 39 | } 40 | return ans; 41 | } 42 | ``` 43 | -------------------------------------------------------------------------------- /Lecture014 Binary Search Interview Questions/sqrt.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | long long int sqrtInteger(int n) { 5 | 6 | int start = 0; 7 | int end = n; 8 | long long int mid = start + (end - start) / 2; 9 | long long int ans = -1; 10 | 11 | while(start <= end) { 12 | 13 | long long int square = mid * mid; 14 | 15 | if(square == n) 16 | return mid; 17 | 18 | if(square < n) { 19 | ans = mid; 20 | start = mid + 1; 21 | } 22 | else 23 | end = mid - 1; 24 | 25 | mid = start + (end - start) / 2; 26 | } 27 | return ans; 28 | } 29 | 30 | double morePrecision(int n, int precision, int tempSol) { 31 | 32 | double factor = 1; 33 | double ans = tempSol; 34 | 35 | for(int i = 0; i < precision; i++) { 36 | 37 | factor = factor / 10; 38 | 39 | for(double j = ans; j * j < n; j = j + factor) 40 | ans = j; 41 | } 42 | return ans; 43 | } 44 | 45 | int main() { 46 | int n; 47 | cout <<" Enter the number " << endl; 48 | cin >> n; 49 | 50 | int tempSol = sqrtInteger(n); 51 | cout << "Answer is " << morePrecision(n, 3, tempSol) << endl; 52 | } 53 | -------------------------------------------------------------------------------- /Lecture007 LeetCode Problem Solving Session/Coding Question.md: -------------------------------------------------------------------------------- 1 | # Coding Questions :- 2 | 3 | 1. [Reverse Integer](https://leetcode.com/problems/reverse-integer) 4 | 2. [Complement of Base 10 Integer](https://leetcode.com/problems/complement-of-base-10-integer) 5 | 3. [Power of Two](https://leetcode.com/problems/power-of-two/) 6 | 7 | ``` cpp 8 | 1. int reverse(int x) { 9 | 10 | int ans = 0; 11 | 12 | while( x != 0) { 13 | 14 | int digit = x % 10; 15 | 16 | if((ans > INT_MAX/10) || (ans < INT_MIN/10)) 17 | return 0; 18 | 19 | ans = (ans * 10) + digit; 20 | x = x / 10; 21 | } 22 | 23 | return ans; 24 | } 25 | ``` 26 | 27 | ``` cpp 28 | 2. int bitwiseComplement(int n) { 29 | 30 | int m = n; 31 | int mask = 0; 32 | 33 | if(n == 0) 34 | return 1; 35 | 36 | while( m != 0) { 37 | mask = (mask << 1) | 1; 38 | m = m >> 1; 39 | } 40 | 41 | int ans = (~n) & mask; 42 | return ans; 43 | } 44 | ``` 45 | 46 | ``` cpp 47 | 3. bool isPowerOfTwo(int n) { 48 | 49 | if(n <= 0) 50 | return false; 51 | 52 | else 53 | return((n & (n-1)) == 0); 54 | } 55 | ``` -------------------------------------------------------------------------------- /Lecture027 Double Pointers and MCQs/doublePointer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void update(int **p2) { 5 | //p2 = p2 + 1; 6 | //kuch change hoga - NO 7 | 8 | //*p2 = *p2 + 1; 9 | //kuch change hoga - YES 10 | 11 | **p2 = **p2 + 1; 12 | //kuch change hoga - YES 13 | } 14 | void update(int *p){ 15 | *p = (*p) * 2; 16 | } 17 | void increment(int **p){ 18 | ++(**p); 19 | } 20 | int main() { 21 | /* 22 | int i = 5; 23 | int* p = &i; 24 | int** p2 = &p; 25 | 26 | cout<< endl << endl <<" Sab sahi chal rha h " << endl << endl ; 27 | 28 | cout << i << endl; 29 | cout << *p << endl; 30 | cout << **p2 << endl; 31 | 32 | cout << &i << endl; 33 | cout << p << endl; 34 | cout << *p2 << endl; 35 | 36 | cout << &p << endl; 37 | cout << p2 << endl; 38 | 39 | cout << endl << endl; 40 | cout<< "before " << i << endl; 41 | cout<< "before " << p << endl; 42 | cout<< "before " << p2 << endl; 43 | update(p2); 44 | cout<< "after " << i << endl; 45 | cout<< "after " << p << endl; 46 | cout<< "after " << p2 << endl; 47 | cout << endl << endl; 48 | */ 49 | 50 | 51 | 52 | 53 | int num = 110; 54 | int *ptr = # 55 | increment(&ptr); 56 | cout << num << endl; 57 | 58 | 59 | 60 | return 0; 61 | } -------------------------------------------------------------------------------- /Lecture036 Recursion Day6/quickSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int partition (int arr[], int s, int e) { 5 | 6 | int pivot = arr[s]; 7 | int cnt = 0; 8 | 9 | for (int i = s + 1; i <= e; i++) { 10 | 11 | if(arr[i] <=pivot) 12 | cnt++; 13 | } 14 | 15 | //place pivot at right position 16 | int pivotIndex = s + cnt; 17 | swap(arr[pivotIndex], arr[s]); 18 | 19 | //left and right wala part smbhal lete h 20 | int i = s, j = e; 21 | 22 | while(i < pivotIndex && j > pivotIndex) { 23 | 24 | while(arr[i] <= pivot) 25 | i++; 26 | 27 | while(arr[j] > pivot) 28 | j--; 29 | 30 | if(i < pivotIndex && j > pivotIndex) 31 | swap(arr[i++], arr[j--]); 32 | } 33 | return pivotIndex; 34 | } 35 | 36 | void quickSort(int arr[], int s, int e) { 37 | 38 | //base case 39 | if(s >= e) 40 | return ; 41 | 42 | //partitioon karenfe 43 | int p = partition(arr, s, e); 44 | 45 | //left part sort karo 46 | quickSort(arr, s, p-1); 47 | 48 | //right wala part sort karo 49 | quickSort(arr, p+1, e); 50 | } 51 | 52 | int main() { 53 | 54 | int arr[10] = {2,4,1,6,9 ,9,9,9,9,9}; 55 | int n = 10; 56 | 57 | quickSort(arr, 0, n-1); 58 | 59 | for(int i=0; i topologicalSort(vector> &edges, int v, int e) { 7 | 8 | //create adjacency list 9 | unordered_map > adj; 10 | for (int i = 0; i < e; i++){ 11 | int u = edges[i][0]; 12 | int v = edges[i][1]; 13 | 14 | adj[u].push_back(v); 15 | } 16 | 17 | //Find all indegree 18 | vector indegree(v); 19 | for(auto i: adj) { 20 | for(auto j: i.second) { 21 | indegree[j]++; 22 | } 23 | } 24 | 25 | // 0 indegree push in queue 26 | queue q; 27 | for(int i = 0; i < v; i++) { 28 | if(indegree[i] == 0) 29 | q.push(i); 30 | } 31 | 32 | //BFS algorithm 33 | vector ans; 34 | while (!q.empty()){ 35 | int front = q.front(); 36 | q.pop(); 37 | ans.push_back(front); 38 | 39 | for(auto neighbour:adj[front]) { 40 | indegree[neighbour]--; 41 | if(indegree[neighbour] == 0) 42 | q.push(neighbour); 43 | } 44 | } 45 | return ans; 46 | } 47 | ``` 48 | -------------------------------------------------------------------------------- /Lecture043 OOPs Day2/polymorphism.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class A { 5 | 6 | public: 7 | void sayHello() { 8 | cout << "Hello Love Babbar" << endl; 9 | } 10 | 11 | int sayHello(char name) { 12 | cout << "Hello Love Babbar" << endl; 13 | return 1; 14 | } 15 | 16 | void sayHello(string name) { 17 | cout << "Hello" << name << endl; 18 | } 19 | }; 20 | 21 | class B { 22 | public: 23 | int a; 24 | int b; 25 | 26 | public: 27 | int add() { 28 | return a+b; 29 | } 30 | 31 | void operator+ (B &obj) { 32 | 33 | /* int value1 = this -> a; 34 | int value2 = obj.a; 35 | cout << "output " << value2 - value1 << endl; 36 | */ 37 | cout << "Hello Babbar" << endl; 38 | } 39 | 40 | void operator() () { 41 | cout << "main Bracket hu " << this->a << endl; 42 | } 43 | }; 44 | 45 | class Animal { 46 | public: 47 | void speak() { 48 | cout << "Speaking "<< endl; 49 | } 50 | }; 51 | 52 | class Dog: public Animal { 53 | 54 | public: 55 | void speak() { 56 | cout << "Barking " << endl; 57 | } 58 | }; 59 | 60 | int main() { 61 | 62 | Dog obj; 63 | obj.speak(); 64 | 65 | /* 66 | B obj1, obj2; 67 | 68 | obj1.a = 4; 69 | obj2.a = 7; 70 | 71 | obj1 + obj2; 72 | obj1();*/ 73 | 74 | /* 75 | A obj; 76 | obj.sayHello(); 77 | */ 78 | } -------------------------------------------------------------------------------- /Lecture026 Pointers with Arrays and Functions/pointer_array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | //int arr[10] = {23, 122, 41, 67}; 7 | 8 | /* 9 | cout <<" address of first memory block is " << arr << endl; 10 | cout << arr[0] << endl; 11 | cout <<" address of first memory block is " << &arr[0] << endl; 12 | 13 | cout << "4th " << *arr << endl; 14 | cout << "5th " << *arr + 1 << endl; 15 | cout << "6th " << *(arr + 1) << endl; 16 | cout << "7th " << *(arr) + 1 << endl; 17 | cout << "8th " << arr[2] << endl; 18 | cout << "9th " << *(arr+2) << endl; 19 | 20 | int i = 3; 21 | cout << i[arr] << endl; 22 | 23 | int temp[10] = {1,2}; 24 | cout << sizeof(temp) << endl; 25 | cout << " 1st " << sizeof(*temp) << endl; 26 | cout << " 2nd " << sizeof(&temp) << endl; 27 | 28 | int *ptr = &temp[0]; 29 | cout << sizeof(ptr) << endl ; 30 | cout << sizeof(*ptr) << endl ; 31 | cout << sizeof(&ptr) << endl ; 32 | 33 | int a[20] = {1,2,3,5}; 34 | //cout << " ->" << &a[0] << endl; 35 | // cout << &a << endl; 36 | //cout << a << endl; 37 | 38 | int *p = &a[0]; 39 | //cout << p << endl; 40 | //cout << *p << endl; 41 | cout << "-> " << &p << endl; 42 | 43 | */ 44 | 45 | int arr[10]; 46 | 47 | //ERROR 48 | //arr = arr+1; 49 | 50 | int *ptr = &arr[0]; 51 | cout << ptr << endl; 52 | ptr = ptr + 1; 53 | cout << ptr << endl; 54 | 55 | } -------------------------------------------------------------------------------- /Lecture070 Binary SearchTree FAANG Interview Questions-1/Coding Question.md: -------------------------------------------------------------------------------- 1 | # Coding Quesion 2 | 3 | 1. Partial BST: [https://www.codingninjas.com/studio/problems/validate-bst_799483?leftPanelTab=0] 4 | 2. Find K-th smallest Element in BST: [https://www.codingninjas.com/studio/problems/find-k-th-smallest-element-in-bst_1069333?leftPanelTab=0] 5 | 3. 6 | 7 | ```cpp 8 | 1. bool isBST(BinaryTreeNode *root, int min, int max) { 9 | //base case 10 | if(root == NULL) 11 | return true; 12 | 13 | if(root -> data <= max && root -> data >= min) { 14 | bool left = isBST(root -> left, min, root -> data); 15 | bool right = isBST(root -> right, root -> data, max); 16 | return left && right; 17 | } 18 | else 19 | return false; 20 | } 21 | 22 | bool validateBST(BinaryTreeNode *root) { 23 | return isBST(root, INT_MIN, INT_MAX); 24 | } 25 | ``` 26 | 27 | ``` cpp 28 | 2. int solve(BinaryTreeNode* root, int k, int &i) { 29 | //base case 30 | if(root == NULL) 31 | return -1; 32 | 33 | //Inorder Traversal L 34 | int left = solve(root -> left, k, i); 35 | 36 | if(left != -1) 37 | return left; 38 | // N 39 | i++; 40 | if(i == k) 41 | return root -> data; 42 | // R 43 | return solve(root -> right, k, i); 44 | } 45 | 46 | int kthSmallest(BinaryTreeNode* root, int k) { 47 | 48 | int i = 0; 49 | int ans = solve(root, k, i); 50 | return ans; 51 | } 52 | ``` 53 | -------------------------------------------------------------------------------- /Lecture022 Strings/validPalindrome.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | class Solution { 6 | private: 7 | bool valid(char ch) { 8 | if( (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9')) { 9 | return 1; 10 | } 11 | 12 | return 0; 13 | } 14 | 15 | char toLowerCase(char ch) { 16 | if( (ch >='a' && ch <='z') || (ch >='0' && ch <='9') ) 17 | return ch; 18 | else{ 19 | char temp = ch - 'A' + 'a'; 20 | return temp; 21 | } 22 | } 23 | bool checkPalindrome(string a) { 24 | int s = 0; 25 | int e = a.length()-1; 26 | 27 | while(s<=e) { 28 | if(a[s] != a[e]) 29 | { 30 | return 0; 31 | } 32 | else{ 33 | s++; 34 | e--; 35 | } 36 | } 37 | return 1; 38 | } 39 | public: 40 | bool isPalindrome(string s) { 41 | 42 | //faltu character hatado 43 | string temp = ""; 44 | 45 | for(int j=0; j 7 | class TwoStack { 8 | 9 | private: 10 | int top1,top2; 11 | int *arr; 12 | int size; 13 | public: 14 | 15 | // Initialize TwoStack. 16 | TwoStack(int s) { 17 | 18 | this -> size = s; 19 | arr = new int[size]; 20 | top1 = -1; 21 | top2 = size; 22 | } 23 | 24 | // Push in stack 1. 25 | void push1(int num) { 26 | 27 | if(top2 - top1 > 1){ 28 | top1++; 29 | arr[top1] = num; 30 | } 31 | } 32 | 33 | // Push in stack 2. 34 | void push2(int num) { 35 | 36 | if(top2 - top1 > 1){ 37 | top2--; 38 | arr[top2] = num; 39 | } 40 | } 41 | 42 | // Pop from stack 1 and return popped element. 43 | int pop1() { 44 | 45 | if (top1 == -1){ 46 | return -1; 47 | } 48 | else{ 49 | int ans = arr[top1]; 50 | top1--; 51 | return ans; 52 | } 53 | } 54 | 55 | // Pop from stack 2 and return popped element. 56 | int pop2() { 57 | 58 | if(top2 == size){ 59 | return -1; 60 | } 61 | else{ 62 | int ans = arr[top2]; 63 | top2++; 64 | return ans; 65 | } 66 | } 67 | }; 68 | ``` 69 | -------------------------------------------------------------------------------- /Lecture013 Binary Search Interview Questions/TotalNumberOfOccurence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int firstoccurence (int arr[], int size, int key) { 5 | 6 | int start = 0; 7 | int end = size - 1; 8 | int mid = start + (end - start) / 2; 9 | int ans = 0; 10 | 11 | while (start <= end) { 12 | 13 | if (key == arr[mid]) { 14 | ans = mid; 15 | end = mid - 1; 16 | } 17 | 18 | else if (key < arr[mid]) 19 | end = mid - 1; 20 | 21 | else 22 | start = mid + 1; 23 | 24 | mid = start + (end - start) / 2; 25 | } 26 | return ans; 27 | } 28 | 29 | int lastoccurence (int arr[], int size, int key) { 30 | 31 | int start = 0; 32 | int end = size - 1; 33 | int mid = start + (end - start) / 2; 34 | int ans = 0; 35 | 36 | while (start <= end) { 37 | 38 | if (key == arr[mid]) { 39 | ans = mid; 40 | start = start + 1; 41 | } 42 | 43 | else if (key < arr[mid]) 44 | end = mid - 1; 45 | 46 | else 47 | start = mid + 1; 48 | 49 | mid = start + (end - start) / 2; 50 | } 51 | return ans; 52 | } 53 | 54 | inline int Totaloccurence (int arr[], int size, int key) { 55 | return ((lastoccurence(arr,size,key) - firstoccurence(arr,size,key)) + 1); 56 | } 57 | 58 | int main () { 59 | 60 | int arr[7] = {1,2,3,3,3,3,5}; 61 | int ans = Totaloccurence(arr,7,3); 62 | cout << ans; 63 | } -------------------------------------------------------------------------------- /Lecture023 2D arrays/SpiralPrint.java: -------------------------------------------------------------------------------- 1 | class SpiralPrint { 2 | public List spiralOrder(int[][] matrix) { 3 | List al = new ArrayList<>(); 4 | 5 | int row = matrix.length; 6 | int col = matrix[0].length; 7 | 8 | int total = row * col; 9 | int count = 0; 10 | 11 | // initialise the indexes; 12 | int startRow = 0; 13 | int endCol = col - 1; 14 | int endRow = row - 1; 15 | int startCol = 0; 16 | 17 | while(count < total){ 18 | 19 | //print startRow 20 | for(int idx = startCol; count < total && idx <= endCol; idx++){ 21 | al.add(matrix[startRow][idx]); 22 | count++; 23 | } 24 | startRow++; 25 | 26 | // print lastCol 27 | for(int idx = startRow; count < total && idx <= endRow; idx++){ 28 | al.add(matrix[idx][endCol]); 29 | count++; 30 | } 31 | endCol--; 32 | 33 | // print lastRow 34 | 35 | for(int idx = endCol; count < total && idx >= startCol; idx--){ 36 | al.add(matrix[endRow][idx]); 37 | count++; 38 | } 39 | endRow--; 40 | 41 | // print firstCol 42 | 43 | for(int idx = endRow; count < total && idx >= startRow; idx--){ 44 | al.add(matrix[idx][startCol]); 45 | count++; 46 | } 47 | startCol++; 48 | } 49 | 50 | return al; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /Lecture060 Introduction to the Queues/queuearray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Queue{ 5 | 6 | // Declaring data member 7 | private: 8 | int fronts, rear; 9 | int *arr; 10 | int size; 11 | 12 | // functions 13 | public: 14 | 15 | Queue(int s){ 16 | 17 | this -> size = s; 18 | fronts = -1; 19 | arr = new int[size]; 20 | rear = -1; 21 | } 22 | 23 | bool isEmpty() { 24 | 25 | if(fronts == -1 || fronts > rear) 26 | return true; 27 | else 28 | return false; 29 | } 30 | 31 | void enqueue(int data) { 32 | 33 | if(fronts == -1) 34 | fronts++; 35 | 36 | rear++; 37 | arr[rear] = data; 38 | 39 | } 40 | 41 | int dequeue() { 42 | 43 | if(fronts > rear || rear == -1) 44 | return -1; 45 | else 46 | return arr[fronts++]; 47 | } 48 | 49 | int front() { 50 | 51 | if(fronts > rear || rear == -1) 52 | return -1; 53 | else 54 | return arr[fronts]; 55 | } 56 | 57 | void display (Queue q1){ 58 | 59 | while(!q1.isEmpty()){ 60 | cout << q1.front() << " "; 61 | q1.dequeue(); 62 | } 63 | } 64 | }; 65 | 66 | int main() { 67 | 68 | Queue q1(4); 69 | 70 | cout << "Queue is empty = " << q1.isEmpty() << endl; 71 | 72 | q1.enqueue(10); 73 | q1.enqueue(20); 74 | q1.enqueue(30); 75 | q1.enqueue(40); 76 | 77 | q1.display(q1); 78 | 79 | q1.dequeue(); 80 | } -------------------------------------------------------------------------------- /Lecture020 Solving LeetCode CodeStudio Questions [Arrays]/Coding Question.md: -------------------------------------------------------------------------------- 1 | # Coding Questions :- 2 | 3 | 1. [Reverse The Array](https://www.naukri.com/code360/problems/reverse-the-array_1262298?utm_source=youtube&utm_medium=affiliate&utm_campaign=love_babbar_codestudio3) 4 | 5 | 2. [Move Zeroes](https://leetcode.com/problems/move-zeroes/) 6 | 7 | ``` cpp 8 | 1. void reverseArray(vector &arr , int m) { 9 | 10 | int start = m + 1; 11 | int end = arr.size() - 1; 12 | 13 | while(start < end ) { 14 | 15 | swap(arr[start], arr[end]); 16 | start++; 17 | end--; 18 | } 19 | } 20 | ``` 21 | 22 | ``` cpp 23 | 2. void moveZeroes(vector& nums) { 24 | 25 | int i = 0; 26 | 27 | for (int j = 0; j < nums.size(); j++) { 28 | if (nums[j] != 0) { 29 | swap(nums[j],nums[i]); 30 | i++; 31 | } 32 | } 33 | } 34 | ``` 35 | 36 | ## Homework :- 37 | 38 | 1.[Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array/) 39 | 40 | ``` cpp 41 | 1. void merge(vector& nums1, int m, vector& nums2, int n) { 42 | 43 | int a1 = m-1, a2 = n-1; 44 | int i = nums1.size() - 1; 45 | 46 | while(i >= 0) { 47 | 48 | if(a1 < 0|| a2 < 0) 49 | break; 50 | 51 | if(nums1[a1] > nums2[a2]) 52 | nums1[i--] = nums1[a1--]; 53 | else 54 | nums1[i--] = nums2[a2--]; 55 | } 56 | 57 | while(a1> = 0) 58 | nums1[i--] = nums1[a1--]; 59 | while(a2 >= 0) 60 | nums1[i--] = nums2[a2--]; 61 | } 62 | ``` 63 | -------------------------------------------------------------------------------- /Lecture069 Binary Search Tree and its Implementation/max-min.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Node { 5 | 6 | public: 7 | int data; 8 | Node* left; 9 | Node* right; 10 | 11 | Node(int data) { 12 | this -> data = data; 13 | this -> left = NULL; 14 | this -> right = NULL; 15 | } 16 | }; 17 | 18 | Node* InsertInBST (Node* &root, int data) { 19 | //base case 20 | if (root == NULL) { 21 | root = new Node(data); 22 | return root; 23 | } 24 | 25 | if (data > root -> data) 26 | InsertInBST(root -> right, data); //Enter data in left node 27 | 28 | else 29 | InsertInBST(root -> left, data); //Enter data in right node 30 | 31 | return root; 32 | } 33 | 34 | void takeInput (Node* &root) { 35 | 36 | int data; 37 | cin >> data; 38 | 39 | while (data != -1) { 40 | root = InsertInBST (root, data); 41 | cin >> data; 42 | } 43 | } 44 | 45 | Node* maxi (Node* root) { 46 | 47 | Node* temp = root; 48 | 49 | while (temp -> right != NULL){ 50 | temp = temp -> right; 51 | } 52 | return temp; 53 | } 54 | 55 | Node* mini (Node* root) { 56 | 57 | Node* temp = root; 58 | 59 | while (temp -> left != NULL){ 60 | temp = temp -> left; 61 | } 62 | return temp; 63 | } 64 | 65 | int main () { 66 | 67 | Node* root = NULL; 68 | 69 | cout << "Enter data to create BST" << endl; 70 | takeInput(root); 71 | 72 | cout << "Maximum data of BST = " << maxi(root) -> data << endl; 73 | cout << "Minimum data of BST = " << mini(root) -> data << endl; 74 | } 75 | 76 | // 10 8 21 7 27 5 4 3 -1 -------------------------------------------------------------------------------- /Lecture093 Shortest Path in Undirected Graphs/Coding Question.md: -------------------------------------------------------------------------------- 1 | # Coding Quesion 2 | 3 | 1. Shortest path in an unweighted graph: [https://www.codingninjas.com/studio/problems/shortest-path-in-an-unweighted-graph_981297?leftPanelTab=0&utm_source=youtube&utm_medium=affiliate&utm_campaign=Lovebabbar] 4 | 5 | ``` cpp 6 | 1. vector shortestPath( vector> edges , int n , int m, int s , int t){ 7 | 8 | //create an adjacency list 9 | vector> adj(n+1); 10 | 11 | for (int i = 0; i < edges.size(); i++) { 12 | int u = edges[i].first; 13 | int v = edges[i].second; 14 | 15 | adj[u].push_back(v); 16 | adj[v].push_back(u); 17 | } 18 | 19 | //do bfs 20 | vector visited(n+1, 0); 21 | vector parent(n+1, 0); 22 | 23 | queue q; 24 | q.push(s); 25 | visited[s] = true; 26 | parent[s] = -1; 27 | 28 | while(!q.empty()) { 29 | int front = q.front(); 30 | q.pop(); 31 | 32 | for(auto i:adj[front]) { 33 | if(!visited[i]) { 34 | visited[i] = true; 35 | parent[i] = front; 36 | q.push(i); 37 | } 38 | } 39 | } 40 | 41 | //finding the shortest path 42 | vector ans; 43 | int currentNode = t; 44 | ans.push_back(currentNode); 45 | 46 | while(currentNode != s) { 47 | currentNode = parent[currentNode]; 48 | ans.push_back(currentNode); 49 | } 50 | reverse(ans.begin(), ans.end()); 51 | return ans; 52 | } 53 | ``` 54 | -------------------------------------------------------------------------------- /Lecture021 Solving Array Questions 2/addArray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | vector reverse(vector v) { 5 | 6 | int start = 0; 7 | int end = v.size() - 1; 8 | 9 | while(start < end) 10 | swap(v[start++], v[end--]); 11 | 12 | return v; 13 | } 14 | 15 | vector findArraySum(vector&a, int n, vector&b, int m) { 16 | 17 | int i = n - 1; 18 | int j = m - 1; 19 | vector ans; 20 | int carry = 0; 21 | 22 | while(i >= 0 && j >= 0) { 23 | int val1 = a[i]; 24 | int val2 = b[j]; 25 | int sum = val1 + val2 + carry; 26 | 27 | carry = sum / 10; 28 | sum = sum % 10; 29 | ans.push_back(sum); 30 | i--; 31 | j--; 32 | } 33 | 34 | // first case 35 | while(i >= 0) { 36 | int sum = a[i] + carry; 37 | carry = sum / 10; 38 | sum = sum % 10; 39 | ans.push_back(sum); 40 | i--; 41 | } 42 | 43 | // second case 44 | while(j >= 0) { 45 | int sum = b[j] + carry; 46 | carry = sum / 10; 47 | int value = sum % 10; 48 | ans.push_back(value); 49 | j--; 50 | } 51 | 52 | // third case 53 | while(carry != 0) { 54 | int sum = carry; 55 | carry = sum / 10; 56 | sum = sum % 10; 57 | ans.push_back(sum); 58 | } 59 | 60 | return reverse(ans); 61 | } 62 | 63 | int main() { 64 | 65 | vector a = {4, 5, 1}; 66 | 67 | vector b = {3, 4, 5}; 68 | 69 | vector ans = findArraySum(a, a.size(), b, b.size()); 70 | 71 | for(int i = 0; i < ans.size(); i++) 72 | cout << ans[i] << " "; 73 | } -------------------------------------------------------------------------------- /Lecture089 Cycle Detection in Directed Graphs/Coding Question.md: -------------------------------------------------------------------------------- 1 | # Coding Quesion 2 | 3 | 1. Detect Cycle In A Directed Graph: [https://www.codingninjas.com/studio/problems/detect-cycle-in-a-directed-graph_1062626?leftPanelTab=0&utm_source=youtube&utm_medium=affiliate&utm_campaign=Lovebabbar] 4 | 5 | ``` cpp 6 | 1. //Depth First Search 7 | bool isCycleDFS (int node, unordered_map > &adj, 8 | unordered_map &visited, unordered_map &dfsvisited) { 9 | 10 | visited[node] = true; 11 | dfsvisited[node] = true; 12 | 13 | for (int neighbour:adj[node]) { 14 | if (!visited[neighbour]) { 15 | bool ans = isCycleDFS(neighbour, adj, visited, dfsvisited); 16 | 17 | if(ans) 18 | return true; 19 | } 20 | else if (dfsvisited[neighbour]) 21 | return true; 22 | } 23 | dfsvisited[node] = false; 24 | return false; 25 | } 26 | 27 | int detectCycleInDirectedGraph(int n, vector < pair < int, int >> & edges) { 28 | 29 | // create an adjacency list 30 | unordered_map > adj; 31 | for (int i = 0; i < edges.size(); i++) { 32 | int u = edges[i].first; 33 | int v = edges[i].second; 34 | 35 | adj[u].push_back(v); 36 | } 37 | 38 | unordered_map visited; 39 | unordered_map dfsvisited; 40 | 41 | for (int i = 1; i <= n; i++) { 42 | 43 | if (!visited[i]) { 44 | bool ans = isCycleDFS(i, adj, visited, dfsvisited); 45 | 46 | if (ans) 47 | return true; 48 | } 49 | } 50 | return false; 51 | } 52 | ``` 53 | -------------------------------------------------------------------------------- /Lecture058 Stack Interview Question-4/Coding Questions.md: -------------------------------------------------------------------------------- 1 | # Coding Question 2 | 3 | 1. N Stacks in an Array: [https://bit.ly/3BexNwv] 4 | 5 | ``` cpp 6 | 1. class NStack { 7 | 8 | int *arr, *top, *next; 9 | int n, s, freespot; 10 | 11 | public: 12 | 13 | NStack(int N, int S) { 14 | 15 | n = N; 16 | s = S; 17 | arr = new int[s]; 18 | top = new int[n]; 19 | next = new int[s]; 20 | freespot = 0; 21 | 22 | //top initialization 23 | for (int i = 0; i < n; i++) 24 | top[i] = -1; 25 | 26 | //next initialization 27 | for (int i = 0; i < s; i++) 28 | next[i] = i+1; 29 | 30 | //update the last index of next 31 | next[s-1] = -1; 32 | } 33 | 34 | bool push(int x, int m) { 35 | 36 | //check stack is overflow 37 | if(freespot == -1) 38 | return false; 39 | 40 | //find index 41 | int index = freespot; 42 | 43 | //update freespot 44 | freespot = next[index]; 45 | 46 | //push element 47 | arr[index] = x; 48 | 49 | //update next 50 | next[index] = top[m-1]; 51 | 52 | //update top 53 | top[m-1] = index; 54 | 55 | return true; 56 | } 57 | 58 | int pop(int m) { 59 | 60 | //check stack is underflow 61 | if(top[m-1] == -1) 62 | return -1; 63 | 64 | //update index 65 | int index = top[m-1]; 66 | 67 | //update top 68 | top[m-1] = next[index]; 69 | 70 | //update next 71 | next[index] = freespot; 72 | 73 | //update freespot 74 | freespot = index; 75 | 76 | return arr[index]; 77 | } 78 | }; 79 | ``` 80 | -------------------------------------------------------------------------------- /Lecture054 Introduction to Stacks/stackarray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Stack{ 5 | 6 | // Declaring data member 7 | private: 8 | int top; 9 | int *arr; 10 | int size; 11 | 12 | // functions 13 | public: 14 | 15 | Stack(int s){ 16 | 17 | this -> size = s; 18 | top = -1; 19 | arr = new int[size]; 20 | } 21 | 22 | void push (int data){ 23 | 24 | if (top == size - 1) 25 | cout << "Stack is Overflow" << endl; 26 | 27 | else{ 28 | top++; 29 | arr[top] = data; 30 | } 31 | } 32 | 33 | void pop () { 34 | 35 | if (top == -1) 36 | cout << "Stack is Underflow" << endl; 37 | 38 | else 39 | top--; 40 | } 41 | 42 | int peak() { 43 | 44 | if (top == -1){ 45 | cout << "Stack is Underflow" << endl; 46 | return -1; 47 | } 48 | else 49 | return arr[top]; 50 | } 51 | 52 | bool isEmpty (){ 53 | 54 | if (top == -1) 55 | return true; 56 | 57 | else 58 | return false; 59 | } 60 | 61 | void display (Stack s){ 62 | 63 | while(!s.isEmpty()){ 64 | cout << s.peak() << " " << endl; 65 | s.pop(); 66 | } 67 | } 68 | }; 69 | 70 | int main() { 71 | 72 | Stack s1(4); 73 | 74 | cout << "Stack is empty = " << s1.isEmpty() << endl; 75 | 76 | s1.push(10); 77 | s1.push(20); 78 | s1.push(30); 79 | s1.push(40); 80 | 81 | cout << "Top element = " << s1.peak() << endl; 82 | s1.display(s1); 83 | 84 | s1.pop(); 85 | cout << "Top element = " << s1.peak() << endl; 86 | 87 | } -------------------------------------------------------------------------------- /Lecture009 Introduction to Arrays in C++/arrayIntro.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void printArray(int arr[], int size) { 5 | 6 | cout<< " printing the array " << endl; 7 | 8 | //print the array 9 | for(int i = 0; i < size; i++) 10 | cout << arr[i] << " "; 11 | 12 | cout<< "Printing DONE" << endl; 13 | } 14 | 15 | int main() { 16 | 17 | //declare 18 | int number[15]; 19 | 20 | //accessing an array 21 | cout << "Value at 14 index " << number[14] << endl; 22 | 23 | //cout << "Value at 20 index " << number[20] << endl; 24 | 25 | //initialising an array 26 | int second[3] = {5, 7, 11}; 27 | 28 | //accesing an element 29 | cout << "Value at 2 index " << second[2] << endl; 30 | 31 | int third[15] = {2,7}; 32 | 33 | int n = 15; 34 | 35 | //printArray(third, 15); 36 | int thirdSize = sizeof(third)/sizeof(int); 37 | cout<<" Size of Third is " << thirdSize < 2 | #include 3 | using namespace std; 4 | class Solution { 5 | public: 6 | vector spiralOrder(vector >& matrix) { 7 | 8 | 9 | vector ans; 10 | int row = matrix.size(); 11 | int col = matrix[0].size(); 12 | 13 | int count = 0; 14 | int total = row*col; 15 | 16 | //index initialisation 17 | int startingRow = 0; 18 | int startingCol = 0; 19 | int endingRow = row-1; 20 | int endingCol = col-1; 21 | 22 | 23 | while(count < total) { 24 | 25 | //print starting row 26 | for(int index = startingCol; count < total && index<=endingCol; index++) { 27 | ans.push_back(matrix[startingRow][index]); 28 | count++; 29 | } 30 | startingRow++; 31 | 32 | //print ending column 33 | for(int index = startingRow; count < total && index<=endingRow; index++) { 34 | ans.push_back(matrix[index][endingCol]); 35 | count++; 36 | } 37 | endingCol--; 38 | 39 | //print ending row 40 | for(int index = endingCol; count < total && index>=startingCol; index--) { 41 | ans.push_back(matrix[endingRow][index]); 42 | count++; 43 | } 44 | endingRow--; 45 | 46 | //print starting column 47 | for(int index = endingRow; count < total && index>=startingRow; index--) { 48 | ans.push_back(matrix[index][startingCol]); 49 | count++; 50 | } 51 | startingCol++; 52 | } 53 | return ans; 54 | } 55 | }; -------------------------------------------------------------------------------- /Lecture035 Recursion Day5/mergeSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void merge(int *arr, int s, int e) { 5 | 6 | int mid = (s + e) /2; 7 | 8 | int len1 = mid - s + 1; 9 | int len2 = e - mid; 10 | 11 | int *first = new int[len1]; 12 | int *second = new int[len2]; 13 | 14 | //copy values 15 | int mainArrayIndex = s; 16 | for(int i = 0; i < len1; i++) 17 | first[i] = arr[mainArrayIndex++]; 18 | 19 | mainArrayIndex = mid+1; 20 | 21 | for(int i=0; i= e) 52 | return; 53 | 54 | int mid = s + (e-s) / 2; 55 | 56 | //left part sort karna h 57 | mergeSort(arr, s, mid); 58 | 59 | //right part sort karna h 60 | mergeSort(arr, mid+1, e); 61 | 62 | //merge 63 | merge(arr, s, e); 64 | } 65 | 66 | int main() { 67 | 68 | int arr[15] = {3, 7, 0, 1, 5, 8, 3, 2, 34, 66, 87, 23, 12, 12, 12}; 69 | int n = 15; 70 | 71 | mergeSort(arr, 0, n-1); 72 | 73 | for (int i = 0; i < n; i++) 74 | cout << arr[i] << " "; 75 | 76 | cout << endl; 77 | } -------------------------------------------------------------------------------- /Lecture045 Linked List Day2/Homework.cpp: -------------------------------------------------------------------------------- 1 | // Reverse Doubly Linkded List 2 | 3 | #include 4 | using namespace std; 5 | 6 | class Node { 7 | 8 | public: 9 | int data; 10 | Node *next,*prev; 11 | 12 | Node() { 13 | this->data = 0; 14 | next = NULL; 15 | prev= NULL; 16 | } 17 | Node(int data) 18 | { 19 | this->data = data; 20 | this->next = NULL; 21 | this->prev= NULL; 22 | } 23 | Node(int data, Node* next, Node *prev) 24 | { 25 | this->data = data; 26 | this->next = next; 27 | this->prev= prev; 28 | } 29 | }; 30 | 31 | void insertAtTail(Node* &tail, int d) { 32 | if(tail == NULL) { 33 | Node* temp = new Node(d); 34 | tail = temp; 35 | } 36 | else{ 37 | Node* temp = new Node(d); 38 | tail -> next = temp; 39 | temp -> prev = tail; 40 | tail = temp; 41 | } 42 | } 43 | 44 | Node* reverseDLL(Node* &head) { 45 | 46 | Node* forward = NULL; 47 | Node* curr = head; 48 | 49 | while (curr != NULL) { 50 | forward = curr -> next; 51 | curr -> next = curr -> prev; 52 | curr -> prev = forward; 53 | head = curr; 54 | curr = forward; 55 | } 56 | return head; 57 | } 58 | 59 | void print(Node* &head) { 60 | Node* temp = head; 61 | 62 | while(temp != NULL ) { 63 | cout << temp -> data << " "; 64 | temp = temp -> next; 65 | } 66 | cout << endl; 67 | } 68 | 69 | int main() { 70 | 71 | Node* node1 = new Node(1); 72 | Node* tail = node1; 73 | Node(2, node1->next, node1->prev); 74 | Node(3, node1->next, node1->prev); 75 | Node(4, node1->next, node1->prev); 76 | 77 | insertAtTail(tail, 11); 78 | print(tail); 79 | 80 | reverseDLL(node1); 81 | print(node1); 82 | } 83 | 84 | -------------------------------------------------------------------------------- /Lecture082 Rat in Maze Problem Backtracking Day 1/Coding Question.md: -------------------------------------------------------------------------------- 1 | # Coding Quesion 2 | 3 | 1. Rat In A Maze: [https://www.codingninjas.com/studio/problems/rat-in-a-maze_1215030?topList=love-babbar-dsa-sheet-problems&leftPanelTab=0&utm_source=youtube&utm_medium=affiliate&utm_campaign=Lovebabbar&leftPanelTabValue=PROBLEM] 4 | 5 | ``` cpp 6 | 1. bool isSafe(int newx, int newy, vector < vector > &visited, 7 | vector < vector < int > > arr, int n) { 8 | 9 | if((newx >= 0 && newx < n) && (newy >= 0 && newy < n) && visited[newx][newy] != 1 10 | && arr[newx][newy] == 1) 11 | return true; 12 | 13 | else 14 | return false; 15 | } 16 | 17 | void solve(int x, int y, vector < vector < int > > arr, int n, vector &ans, 18 | vector < vector > &visited, string path) { 19 | 20 | //base case 21 | if (x == n - 1 && y == n - 1) { 22 | ans.push_back(path); 23 | return; 24 | } 25 | 26 | visited[x][y] = 1; 27 | 28 | //Down 29 | if(isSafe (x+1, y, visited, arr, n)) 30 | solve(x+1, y, arr, n, ans, visited, path + 'D'); 31 | 32 | //Left 33 | if(isSafe (x, y-1, visited, arr, n)) 34 | solve(x, y-1, arr, n, ans, visited, path + 'L'); 35 | 36 | //Right 37 | if(isSafe (x, y+1, visited, arr, n)) 38 | solve(x, y+1, arr, n, ans, visited, path + 'R'); 39 | 40 | //Up 41 | if(isSafe (x-1, y, visited, arr, n)) 42 | solve(x-1, y, arr, n, ans, visited, path + 'U'); 43 | 44 | visited[x][y] = 0; 45 | } 46 | 47 | vector < string > searchMaze(vector < vector < int >> & arr, int n) { 48 | 49 | vector ans; 50 | vector < vector > visited(n, vector (n,0)); 51 | string path = ""; 52 | 53 | if(arr[0][0] == 0) 54 | return ans; 55 | 56 | solve (0, 0, arr, n, ans, visited, path); 57 | return ans; 58 | } 59 | ``` 60 | -------------------------------------------------------------------------------- /Lecture062 Binary Trees and its Representation/binarytree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class node { 6 | 7 | public: 8 | 9 | int data; 10 | node *left; 11 | node *right; 12 | 13 | node(int data) { 14 | 15 | this -> data = data; 16 | this -> left = NULL; 17 | this -> right = NULL; 18 | } 19 | }; 20 | 21 | node *buildTree(node *root) { 22 | 23 | int data; 24 | cout << "Enter the data = " << endl; 25 | cin >> data; 26 | root = new node(data); 27 | 28 | if(data == -1) 29 | return NULL; 30 | 31 | cout << "Enter the data inserting in the left of " << data << endl; 32 | root -> left = buildTree(root -> left); 33 | 34 | cout << "Enter the data inserting in the right of " << data << endl; 35 | root -> right = buildTree(root -> right); 36 | 37 | return root; 38 | } 39 | 40 | void levelOrderTraversal (node *root) { 41 | 42 | queue q; 43 | q.push(root); 44 | q.push(NULL); 45 | 46 | while(!q.empty()) { 47 | 48 | node* temp = q.front(); 49 | q.pop(); 50 | 51 | if(temp == NULL){ 52 | //level complete new traversal 53 | cout << endl; 54 | if(!q.empty()) // queue still have some child nodes 55 | q.push(NULL); 56 | } 57 | else{ 58 | cout << temp -> data << " "; 59 | 60 | if (temp -> left) 61 | q.push(temp -> left); 62 | 63 | if(temp -> right) 64 | q.push(temp -> right); 65 | } 66 | } 67 | } 68 | 69 | int main() { 70 | 71 | node *root = NULL; 72 | 73 | //creating a tree 74 | root = buildTree(root); 75 | 76 | // Printing in order 77 | cout << endl; 78 | cout << "Level Order Traversal :-" << endl; 79 | levelOrderTraversal(root); 80 | 81 | } 82 | 83 | //1 3 7 -1 -1 11 -1 -1 5 17 -1 -1 -1 -------------------------------------------------------------------------------- /Lecture069 Binary Search Tree and its Implementation/BST.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class Node { 6 | 7 | public: 8 | int data; 9 | Node* left; 10 | Node* right; 11 | 12 | Node(int data) { 13 | this -> data = data; 14 | this -> left = NULL; 15 | this -> right = NULL; 16 | } 17 | }; 18 | 19 | Node* InsertInBST (Node* &root, int data) { 20 | //base case 21 | if (root == NULL) { 22 | root = new Node(data); 23 | return root; 24 | } 25 | 26 | if (data > root -> data) 27 | InsertInBST(root -> right, data); //Enter data in left node 28 | 29 | else 30 | InsertInBST(root -> left, data); //Enter data in right node\ 31 | 32 | return root; 33 | } 34 | 35 | void takeInput (Node* &root) { 36 | 37 | int data; 38 | cin >> data; 39 | 40 | while (data != -1) { 41 | root = InsertInBST (root, data); 42 | cin >> data; 43 | } 44 | } 45 | 46 | void levelOrderTraversal (Node *root) { 47 | 48 | queue q; 49 | q.push(root); 50 | q.push(NULL); 51 | 52 | while(!q.empty()) { 53 | 54 | Node* temp = q.front(); 55 | q.pop(); 56 | 57 | if(temp == NULL){ 58 | //level complete new traversal 59 | cout << endl; 60 | if(!q.empty()) // queue still have some child nodes 61 | q.push(NULL); 62 | } 63 | else{ 64 | cout << temp -> data << " "; 65 | 66 | if (temp -> left) 67 | q.push(temp -> left); 68 | 69 | if(temp -> right) 70 | q.push(temp -> right); 71 | } 72 | } 73 | } 74 | 75 | int main () { 76 | 77 | Node* root = NULL; 78 | 79 | cout << "Enter data to create BST" << endl; 80 | takeInput(root); 81 | 82 | cout << "Level order Traversal" << endl; 83 | levelOrderTraversal(root); 84 | } 85 | 86 | // 10 8 21 7 27 5 4 3 -1 -------------------------------------------------------------------------------- /Lecture057 Stack Interview Question-3/Coding Questions.md: -------------------------------------------------------------------------------- 1 | # Coding Question 2 | 3 | 1. Celebrity Problem: [https://practice.geeksforgeeks.org/problems/the-celebrity-problem/1] 4 | 2. Max Rectangle in Binary Matrix with all 1's: [https://practice.geeksforgeeks.org/problems/max-rectangle/1] 5 | 6 | ``` cpp 7 | 1. class Solution { 8 | 9 | private: 10 | 11 | bool knows(vector >& M, int a, int b){ 12 | 13 | if(M[a][b] == 1) 14 | return true; 15 | 16 | else 17 | return false; 18 | } 19 | public: 20 | 21 | int celebrity(vector >& M, int n) { 22 | 23 | stack s; 24 | 25 | for (int i = 0; i < n; i++) 26 | s.push(i); 27 | 28 | while(s.size() > 1) { 29 | 30 | int a = s.top(); 31 | s.pop(); 32 | 33 | int b = s.top(); 34 | s.pop(); 35 | 36 | if(knows(M, a, b)) 37 | s.push(b); 38 | 39 | else 40 | s.push(a); 41 | } 42 | int ans = s.top(); 43 | 44 | int zeroCount = 0; 45 | 46 | for(int i = 0; i>t; 72 | while(t--) { 73 | 74 | int n; 75 | cin>>n; 76 | vector > M( n , vector (n, 0)); 77 | 78 | for(int i=0;i>M[i][j]; 81 | } 82 | Solution ob; 83 | cout<&arr,int n, int key) { 9 | 10 | int start = 0; 11 | int end = n - 1; 12 | int mid = start + (end - start) / 2; 13 | int ans = -1; 14 | 15 | while(start <= end){ 16 | if(arr[mid] == key) { 17 | ans = mid; 18 | end = mid - 1; 19 | } 20 | 21 | else if(key > arr[mid]) 22 | start = mid + 1; 23 | 24 | else 25 | end = mid - 1; 26 | 27 | mid = start + (end-start)/2; 28 | } 29 | return ans; 30 | } 31 | 32 | int last(vector&arr,int n, int key){ 33 | int start = 0; 34 | int end = n-1; 35 | int mid = start + (end - start) / 2; 36 | int ans = -1; 37 | 38 | while(start <= end) { 39 | if(arr[mid] == key) { 40 | ans = mid; 41 | start = start + 1; 42 | } 43 | 44 | else if(key > arr[mid]) 45 | start = mid + 1; 46 | 47 | else 48 | end = mid - 1; 49 | 50 | mid = start + (end-start)/2; 51 | } 52 | return ans; 53 | } 54 | 55 | pair firstAndLastPosition(vector& arr, int n, int k) { 56 | 57 | pair p; 58 | p.first = first(arr,n,k); 59 | p.second = last(arr,n,k); 60 | return p; 61 | } 62 | ``` 63 | 64 | ``` cpp 65 | 2. int peakIndexInMountainArray(vector& arr) { 66 | 67 | int start = 0; 68 | int end = arr.size() - 1; 69 | 70 | int mid = start + (end - start) / 2; 71 | 72 | while (start < end) { 73 | 74 | if(arr[mid] < arr[mid + 1]) 75 | start = mid + 1; 76 | else 77 | end = mid; 78 | 79 | mid = start + (end - start) / 2; 80 | } 81 | return start; 82 | } 83 | ``` -------------------------------------------------------------------------------- /Lecture076 Heap Interview Question-2/Coding Question.md: -------------------------------------------------------------------------------- 1 | # Coding Quesion 2 | 3 | 1. K-th Largest Sum Subarray: [https://www.codingninjas.com/studio/problems/k-th-largest-sum-contiguous-subarray_920398?leftPanelTab=0&utm_source=youtube&utm_medium=affiliate&utm_campaign=Lovebabbar] 4 | 2. Merge K Sorted Arrays: [https://www.codingninjas.com/studio/problems/merge-k-sorted-arrays_975379?leftPanelTab=0&utm_source=youtube&utm_medium=affiliate&utm_campaign=Lovebabbar] 5 | 6 | ``` cpp 7 | 1. int getKthLargest(vector &arr, int k) { 8 | 9 | priority_queue , greater > pq; 10 | 11 | int n = arr.size(); 12 | for (int i = 0; i pq.top()) { 21 | pq.pop(); 22 | pq.push(sum); 23 | } 24 | } 25 | } 26 | return pq.top(); 27 | } 28 | ``` 29 | 30 | ``` cpp 31 | 2. class node { 32 | 33 | public: 34 | int data; 35 | int i; 36 | int j; 37 | 38 | node(int data, int row, int col) { 39 | this -> data = data; 40 | this -> i = row; 41 | this -> j = col; 42 | } 43 | }; 44 | 45 | class compare { 46 | public: 47 | bool operator() (node* a, node* b) { 48 | return a-> data > b -> data; 49 | } 50 | }; 51 | 52 | vector mergeKSortedArrays(vector>&kArrays, int k) { 53 | 54 | priority_queue < node*, vector , compare > minHeap; 55 | 56 | //step1 insert all first elements of karrays in heap 57 | for (int i = 0; i < k; i++) { 58 | node *tmp = new node(kArrays[i][0], i, 0); 59 | minHeap.push(tmp); 60 | } 61 | //step2 62 | vector ans; 63 | while (!minHeap.empty()) { 64 | node* tmp = minHeap.top(); 65 | ans.push_back(tmp -> data); 66 | minHeap.pop(); 67 | 68 | int i = tmp -> i; 69 | int j = tmp -> j; 70 | 71 | if(j+1 < kArrays[i].size()) { 72 | node* next = new node(kArrays[i][j+1], i, j+1); 73 | minHeap.push(next); 74 | } 75 | } 76 | return ans; 77 | } 78 | ``` 79 | 80 | ``` cpp 81 | 3. 82 | ``` 83 | -------------------------------------------------------------------------------- /Lecture062 Binary Trees and its Representation/IPPtraversal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class node { 6 | 7 | public: 8 | 9 | int data; 10 | node *left; 11 | node *right; 12 | 13 | node(int data) { 14 | 15 | this -> data = data; 16 | this -> left = NULL; 17 | this -> right = NULL; 18 | } 19 | }; 20 | 21 | node *buildTree(node *root) { 22 | 23 | int data; 24 | cout << "Enter the data = "; 25 | cin >> data; 26 | root = new node(data); 27 | 28 | if(data == -1) 29 | return NULL; 30 | 31 | cout << "Enter the data inserting in the left of " << data << endl; 32 | root -> left = buildTree(root -> left); 33 | 34 | cout << "Enter the data inserting in the right of " << data << endl; 35 | root -> right = buildTree(root -> right); 36 | 37 | return root; 38 | } 39 | 40 | void inorder (node* root) { 41 | 42 | //base case 43 | if(root == NULL) 44 | return; 45 | 46 | inorder(root -> left); //traverse left node 47 | cout << root -> data << " "; //print node 48 | inorder(root -> right); //traverse right node 49 | } 50 | 51 | void preorder (node* root) { 52 | 53 | //base case 54 | if(root == NULL) 55 | return; 56 | 57 | cout << root ->data << " "; //print node 58 | preorder(root -> left); //traverse left node 59 | preorder(root -> right); //traverse right node 60 | } 61 | 62 | void postorder (node* root) { 63 | 64 | //base case 65 | if(root == NULL) 66 | return; 67 | 68 | postorder(root -> left); //traverse left node 69 | postorder(root -> right); //traverse right node 70 | cout << root ->data << " "; //print node 71 | } 72 | 73 | int main() { 74 | 75 | node *root = NULL; 76 | 77 | //creating a tree 78 | root = buildTree(root); 79 | 80 | //inorder traversal 81 | cout << endl; 82 | cout << "Inorder Traversal :-" << endl; 83 | inorder(root); 84 | 85 | //preorder traversal 86 | cout << endl; 87 | cout << "Preorder Traversal :-" << endl; 88 | preorder(root); 89 | 90 | //postorder traversal 91 | cout << endl; 92 | cout << "Postorder Traversal :-" << endl; 93 | postorder(root); 94 | } -------------------------------------------------------------------------------- /Lecture094 Shortest Path in Directed Acyclic Graphs/SPinDAG.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Graph{ 5 | public: 6 | unordered_map >> adj; 7 | 8 | void addEdge(int u, int v, int weight) { 9 | pair p = make_pair(v, weight); 10 | adj[u].push_back(p); 11 | } 12 | 13 | void printAdj() { 14 | 15 | for(auto i: adj) { 16 | cout << i.first << " -> "; 17 | for(auto j: i.second) { 18 | cout << "(" << j.first << "," << j.second << "), "; 19 | } 20 | cout << endl; 21 | } 22 | } 23 | 24 | void dfs(int node, unordered_map &visited, stack &s) { 25 | visited[node] = true; 26 | 27 | for(auto neighbour: adj[node]) { 28 | if(!visited[neighbour.first]) 29 | dfs(neighbour.first, visited, s); 30 | } 31 | 32 | s.push(node); 33 | } 34 | 35 | void getShortestPath (int src, vector &distance, stack &s) { 36 | 37 | distance[src] = 0; 38 | 39 | while (!s.empty()) { 40 | int top = s.top(); 41 | s.pop(); 42 | 43 | if(distance[top] != INT_MAX) { 44 | for (auto i: adj[top]) { 45 | if(distance[top] + i.second < distance[i.first]) 46 | distance[i.first] = distance[top] + i.second; 47 | } 48 | } 49 | } 50 | 51 | } 52 | }; 53 | 54 | int main() { 55 | 56 | Graph g; 57 | g.addEdge(0,1,5); 58 | g.addEdge(0,2,3); 59 | g.addEdge(1,2,2); 60 | g.addEdge(1,3,6); 61 | g.addEdge(2,3,7); 62 | g.addEdge(2,4,4); 63 | g.addEdge(2,5,2); 64 | g.addEdge(3,4,-1); 65 | g.addEdge(4,5,-2); 66 | 67 | g.printAdj(); 68 | 69 | //topological Sort 70 | unordered_map visited; 71 | stack s; 72 | int n = 6; 73 | for (int i = 0; i < n; i++) { 74 | if(!visited[i]) 75 | g.dfs(i, visited, s); 76 | } 77 | 78 | //Shortest Path 79 | int src = 1; 80 | vector distance(n); 81 | 82 | for(int i = 0; i < n; i++) 83 | distance[i] = INT_MAX; 84 | 85 | g.getShortestPath(src, distance, s); 86 | 87 | for (int i = 0; i < distance.size(); i++) { 88 | cout << distance[i] << " "; 89 | } 90 | } -------------------------------------------------------------------------------- /Lecture022 Strings/intro.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | char toLowerCase(char ch) { 5 | if(ch >='a' && ch <='z') 6 | return ch; 7 | else{ 8 | char temp = ch - 'A' + 'a'; 9 | return temp; 10 | } 11 | } 12 | 13 | bool checkPalindrome(char a[], int n) { 14 | int s = 0; 15 | int e = n-1; 16 | 17 | while(s<=e) { 18 | if(toLowerCase( a[s] ) != toLowerCase( a[e] ) ) 19 | { 20 | return 0; 21 | } 22 | else{ 23 | s++; 24 | e--; 25 | } 26 | } 27 | return 1; 28 | } 29 | 30 | void reverse(char name[], int n) { 31 | int s=0; 32 | int e = n-1; 33 | 34 | while(s> name; 82 | //name[2] = '\0'; 83 | 84 | cout << "Your name is "; 85 | cout << name << endl; 86 | int len = getLength(name); 87 | cout << " Length: " << len << endl; 88 | 89 | reverse(name, len); 90 | cout << "Your name is "; 91 | cout << name << endl; 92 | 93 | cout <<" Palindrome or Not: " << checkPalindrome(name, len) << endl; 94 | 95 | cout << " CHARACTER is " << toLowerCase('b') << endl; 96 | cout << " CHARACTER is " << toLowerCase('C') << endl; 97 | */ 98 | 99 | string s; 100 | cin >> s; 101 | 102 | cout << getMaxOccCharacter(s) << endl; 103 | 104 | return 0; 105 | } -------------------------------------------------------------------------------- /Lecture079 Trie & its Implementation/trieintro.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class TrieNode { 5 | public: 6 | 7 | char data; 8 | TrieNode* childern[26]; 9 | bool isTerminal; 10 | 11 | TrieNode (char ch) { 12 | 13 | this -> data = ch; 14 | 15 | for (int i = 0; i < 26; i++) 16 | childern[i] = NULL; 17 | 18 | isTerminal = false; 19 | } 20 | }; 21 | 22 | class Trie { 23 | public: 24 | 25 | TrieNode* root = NULL; 26 | 27 | Trie () { 28 | root = new TrieNode('\0'); 29 | } 30 | 31 | void insertUtil (TrieNode* root, string word) { 32 | //base case 33 | if (word.length() == 0){ 34 | root -> isTerminal = true; 35 | return ; 36 | } 37 | 38 | //assumptions that all are char are CAPS 39 | int index = word[0] - 'A'; 40 | 41 | TrieNode* child; 42 | 43 | //Present case 44 | if(root -> childern[index] != NULL) 45 | child = root -> childern[index]; 46 | 47 | //Absent case 48 | else 49 | child = new TrieNode(word[0]); 50 | root -> childern[index] = child; 51 | 52 | //Recursion Calling 53 | insertUtil(child, word.substr(1)); 54 | } 55 | 56 | void insertWord (string word) { 57 | insertUtil(root, word); 58 | } 59 | 60 | bool searchUtil (TrieNode* root, string word) { 61 | //base case 62 | if (word.length() == 0) 63 | return root -> isTerminal; 64 | 65 | //assumptions that all are char are CAPS 66 | int index = word[0] - 'A'; 67 | 68 | TrieNode* child = NULL; 69 | 70 | //Present case 71 | if(root -> childern[index] != NULL) 72 | child = root -> childern[index]; 73 | 74 | //Absent case 75 | else 76 | return false; 77 | 78 | //Recursion Calling 79 | return searchUtil(child, word.substr(1)); 80 | } 81 | 82 | bool searchWord (string word) { 83 | return searchUtil (root, word); 84 | } 85 | 86 | }; 87 | 88 | int main () { 89 | 90 | Trie* t = new Trie(); 91 | t->insertWord("ABC"); 92 | t->insertWord("TIME"); 93 | 94 | cout << "Present or not " << t->searchWord("abc") << endl; 95 | cout << "Present or not " << t->searchWord("TIME") << endl; 96 | } -------------------------------------------------------------------------------- /Lecture014 Binary Search Interview Questions/Coding Question.md: -------------------------------------------------------------------------------- 1 | # Coding Questions :- 2 | 3 | 1. [Search In Rotated Sorted Array](https://www.naukri.com/code360/problems/search-in-rotated-sorted-array_1082554?source=youtube&campaign=love_babbar_codestudio2&utm_source=youtube&utm_medium=affiliate&utm_campaign=love_babbar_codestudio2) 4 | 5 | 2. [Square Root of a number](https://www.naukri.com/code360/problems/square-root_893351?source=youtube&campaign=love_babbar_codestudio2&utm_source=youtube&utm_medium=affiliate&utm_campaign=love_babbar_codestudio2) 6 | 7 | ``` cpp 8 | 1. int getPivot(vector& arr, int n) { 9 | 10 | int start = 0; 11 | int end = n - 1; 12 | int mid = start + (end - start) / 2; 13 | 14 | while(start < end ) { 15 | 16 | if(arr[mid] >= arr[0]) 17 | start = mid + 1; 18 | 19 | else 20 | end = mid; 21 | 22 | mid = start + (end - start) / 2; 23 | } 24 | return start; 25 | } 26 | 27 | int binarySearch(vector& arr, int s, int e, int key) { 28 | 29 | int start = s; 30 | int end = e; 31 | 32 | int mid = start + (end - start) / 2; 33 | 34 | while(start <= end) { 35 | 36 | if(arr[mid] == key) 37 | return mid; 38 | 39 | if(key > arr[mid]) 40 | start = mid + 1; 41 | 42 | else 43 | end = mid - 1; 44 | 45 | mid = start + (end-start)/2; 46 | } 47 | return -1; 48 | } 49 | 50 | int search(vector& arr, int n, int k) { 51 | 52 | int pivot = getPivot(arr, n); 53 | 54 | if( k >= arr[pivot] && k <= arr[n-1]) 55 | return binarySearch(arr, pivot, n-1, k); //BS on second line 56 | 57 | else 58 | return binarySearch(arr, 0, pivot - 1, k); //BS on first line 59 | } 60 | 61 | ``` 62 | 63 | ``` cpp 64 | 2. int floorSqrt(int n) { 65 | 66 | int start = 0; 67 | int end = n; 68 | long long int mid = start + (end - start) / 2; 69 | long long int ans = -1; 70 | 71 | while(start <= end) { 72 | 73 | long long int square = mid * mid; 74 | 75 | if(square == n) 76 | return mid; 77 | 78 | if(square < n) { 79 | ans = mid; 80 | start = mid + 1; 81 | } 82 | else 83 | end = mid - 1; 84 | 85 | mid = start + (end - start) / 2; 86 | } 87 | return ans; 88 | } 89 | ``` -------------------------------------------------------------------------------- /Lecture069 Binary Search Tree and its Implementation/IPPtraversal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Node { 5 | 6 | public: 7 | 8 | int data; 9 | Node *left; 10 | Node *right; 11 | 12 | Node(int data) { 13 | 14 | this -> data = data; 15 | this -> left = NULL; 16 | this -> right = NULL; 17 | } 18 | }; 19 | 20 | Node* InsertInBST (Node* &root, int data) { 21 | //base case 22 | if (root == NULL) { 23 | root = new Node(data); 24 | return root; 25 | } 26 | 27 | if (data < root -> data) 28 | InsertInBST(root -> left, data); //Enter data in left node 29 | 30 | else 31 | InsertInBST(root -> right, data); //Enter data in right node\ 32 | 33 | return root; 34 | } 35 | 36 | void takeInput (Node* &root) { 37 | 38 | int data; 39 | cin >> data; 40 | 41 | while (data != -1) { 42 | root = InsertInBST (root, data); 43 | cin >> data; 44 | } 45 | } 46 | 47 | void inorder (Node* root) { 48 | 49 | //base case 50 | if(root == NULL) 51 | return; 52 | 53 | inorder(root -> left); //traverse left node 54 | cout << root -> data << " "; //print node 55 | inorder(root -> right); //traverse right node 56 | } 57 | 58 | void preorder (Node* root) { 59 | 60 | //base case 61 | if(root == NULL) 62 | return; 63 | 64 | cout << root ->data << " "; //print node 65 | preorder(root -> left); //traverse left node 66 | preorder(root -> right); //traverse right node 67 | } 68 | 69 | void postorder (Node* root) { 70 | 71 | //base case 72 | if(root == NULL) 73 | return; 74 | 75 | postorder(root -> left); //traverse left node 76 | postorder(root -> right); //traverse right node 77 | cout << root ->data << " "; //print node 78 | } 79 | 80 | int main() { 81 | 82 | Node *root = NULL; 83 | 84 | //creating a tree 85 | takeInput(root); 86 | 87 | //inorder traversal 88 | cout << endl; 89 | cout << "Inorder Traversal :-" << endl; 90 | inorder(root); 91 | 92 | //preorder traversal 93 | cout << endl; 94 | cout << "Preorder Traversal :-" << endl; 95 | preorder(root); 96 | 97 | //postorder traversal 98 | cout << endl; 99 | cout << "Postorder Traversal :-" << endl; 100 | postorder(root); 101 | } -------------------------------------------------------------------------------- /Lecture079 Trie & its Implementation/Coding Question.md: -------------------------------------------------------------------------------- 1 | # Coding Quesion 2 | 3 | 1. Implement Trie: [https://www.codingninjas.com/studio/problems/implement-trie_631356?topList=love-babbar-dsa-sheet-problems&leftPanelTab=0&utm_source=youtube&utm_medium=affiliate&utm_campaign=Lovebabbar] 4 | 5 | ``` cpp 6 | 1. class TrieNode { 7 | public: 8 | 9 | char data; 10 | TrieNode* childern[26]; 11 | bool isTerminal; 12 | 13 | TrieNode (char ch) { 14 | 15 | data = ch; 16 | 17 | for (int i = 0; i < 26; i++) 18 | childern[i] = NULL; 19 | 20 | isTerminal = false; 21 | } 22 | }; 23 | 24 | class Trie { 25 | TrieNode* root = NULL; 26 | public: 27 | Trie() { 28 | root = new TrieNode('\0'); 29 | } 30 | 31 | void insertUtil (TrieNode* root, string word) { 32 | 33 | if (word.length() == 0){ 34 | root -> isTerminal = true; 35 | return ; 36 | } 37 | 38 | int index = word[0] - 'a'; 39 | TrieNode* child; 40 | 41 | if(root -> childern[index] != NULL) 42 | child = root -> childern[index]; 43 | 44 | else 45 | child = new TrieNode(word[0]); 46 | root -> childern[index] = child; 47 | 48 | insertUtil(child, word.substr(1)); 49 | } 50 | 51 | void insert(string word) { 52 | insertUtil(root, word); 53 | } 54 | 55 | bool searchUtil (TrieNode* root, string word) { 56 | 57 | if (word.length() == 0) 58 | return root -> isTerminal; 59 | 60 | int index = word[0] - 'a'; 61 | TrieNode* child = NULL; 62 | 63 | if(root -> childern[index] != NULL) 64 | child = root -> childern[index]; 65 | 66 | else 67 | return false; 68 | 69 | return searchUtil(child, word.substr(1)); 70 | } 71 | 72 | bool search(string word) { 73 | return searchUtil (root, word); 74 | } 75 | 76 | bool prefixUtil (TrieNode* root, string word) { 77 | //base case 78 | if (word.length() == 0) 79 | return true; 80 | 81 | int index = word[0] - 'a'; 82 | TrieNode* child = NULL; 83 | 84 | if(root -> childern[index] != NULL) 85 | child = root -> childern[index]; 86 | 87 | else 88 | return false; 89 | 90 | return prefixUtil(child, word.substr(1)); 91 | } 92 | 93 | bool startsWith(string prefix) { 94 | return prefixUtil(root, prefix); 95 | } 96 | }; 97 | ``` 98 | -------------------------------------------------------------------------------- /Lecture061 Queue Faang Interview Question/Kqueues.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Kqueue { 5 | 6 | private: 7 | 8 | int *arr; 9 | int *front; 10 | int *rear; 11 | int *next; 12 | int freeSpot; 13 | int n, k; 14 | 15 | public: 16 | 17 | Kqueue(int n, int k){ 18 | 19 | this -> n = n; 20 | this -> k = k; 21 | arr = new int[n]; 22 | 23 | front = new int[k]; 24 | rear = new int[k]; 25 | 26 | for (int i = 0; i < k; i++){ 27 | front[i] = -1; 28 | rear[i] = -1; 29 | } 30 | 31 | next = new int[n]; 32 | 33 | for (int i = 0; i < n; i++) 34 | next[i] = i+1; 35 | 36 | next[n-1] = -1; 37 | freeSpot = 0; 38 | } 39 | 40 | void enqueue (int data, int qn){ 41 | 42 | //checking condition for overflow 43 | if (freeSpot == -1){ 44 | cout << "Queue is Overflow" << endl; 45 | return; 46 | } 47 | 48 | //find first free index 49 | int index = freeSpot; 50 | 51 | //update freespot 52 | freeSpot = next[index]; 53 | 54 | //check whether first element 55 | if(front[qn - 1] == -1) 56 | front[qn - 1] = index; 57 | 58 | else 59 | //link new element to prev element 60 | next[rear[qn - 1]] = index; 61 | next[index] = -1; 62 | rear[qn - 1] = index; 63 | arr[index] = data; 64 | } 65 | 66 | int dequeue (int qn) { 67 | 68 | //checking condition of underflow 69 | if (front[qn - 1] == -1){ 70 | cout << "Queue is underflow" << endl; 71 | return -1; 72 | } 73 | 74 | //find the index to pop 75 | int index = front[qn - 1]; 76 | 77 | //front to aage badhao 78 | front[qn - 1] = next[index]; 79 | 80 | //manage freeSpot 81 | next[index] = freeSpot; 82 | freeSpot = index; 83 | return arr[index]; 84 | } 85 | }; 86 | 87 | int main () { 88 | 89 | Kqueue k(10,3); 90 | 91 | k.enqueue(10,1); 92 | k.enqueue(12,1); 93 | k.enqueue(15,2); 94 | k.enqueue(14,1); 95 | 96 | cout << k.dequeue(1) << endl; 97 | cout << k.dequeue(2) << endl; 98 | cout << k.dequeue(1) << endl; 99 | cout << k.dequeue(1) << endl; 100 | cout << k.dequeue(1) << endl; 101 | } -------------------------------------------------------------------------------- /Lecture066 Construct a Binary Tree Using Inorder, Preorder, Postorder Traversal/Coding Question.md: -------------------------------------------------------------------------------- 1 | # Coding Quesion 2 | 3 | 1. Construct Tree from Inorder & Preorder: [https://practice.geeksforgeeks.org/problems/construct-tree-1/1] 4 | 2. Tree from Postorder and Inorder: [https://practice.geeksforgeeks.org/problems/tree-from-postorder-and-inorder/1] 5 | 6 | ```cpp 7 | 1. class Solution { 8 | private: 9 | int Findposition(int in[], int inorderStart, int inorderEnd, int element){ 10 | 11 | for(int i = inorderStart; i<=inorderEnd; i++) { 12 | 13 | if(in[i] == element) 14 | return i; 15 | } 16 | return -1 ; 17 | } 18 | 19 | Node* solve(int in[], int pre[], int &index, int inorderStart, int inorderEnd, int n) { 20 | //base case 21 | if(index >= n || inorderStart > inorderEnd) 22 | return NULL ; 23 | 24 | int element = pre[index++] ; // At every interation index is increasing 25 | Node* root = new Node(element); 26 | int position = Findposition(in, inorderStart, inorderEnd, element); 27 | 28 | root->left = solve(in, pre, index, inorderStart, position-1, n); 29 | root->right = solve(in, pre, index, position+1, inorderEnd, n); 30 | 31 | return root; 32 | } 33 | 34 | public: 35 | 36 | Node* buildTree(int in[], int pre[], int n) { 37 | int preorderindex = 0; //Pre order is NLR so First element is root 38 | 39 | Node* ans = solve(in, pre, preorderindex, 0, n-1, n); 40 | return ans; 41 | } 42 | }; 43 | ``` 44 | 45 | ```cpp 46 | 2. int Findposition(int in[], int inorderStart, int inorderEnd, int element, int n){ 47 | 48 | for(int i = inorderStart; i<=inorderEnd; i++) { 49 | 50 | if(in[i] == element) 51 | return i; 52 | } 53 | return -1; 54 | } 55 | 56 | Node* solve(int in[], int post[], int &index, int inorderStart, int inorderEnd, int n) { 57 | //base case 58 | if(index < 0 || inorderStart > inorderEnd) 59 | return NULL; 60 | 61 | int element = post[index--]; // At every interation index is decreasing 62 | Node* root = new Node(element); 63 | int position = Findposition(in, inorderStart, inorderEnd, element, n); 64 | 65 | //recursive calls 66 | root-> right = solve(in, post, index, position+1, inorderEnd, n); 67 | root-> left = solve(in, post, index, inorderStart, position-1, n); 68 | 69 | return root; 70 | } 71 | 72 | Node *buildTree(int in[], int post[], int n) { 73 | 74 | int postorderindex = n-1; //Post order is LRN so last element is root 75 | 76 | Node* ans = solve(in, post, postorderindex, 0, n-1, n); 77 | return ans; 78 | } 79 | ``` 80 | -------------------------------------------------------------------------------- /Lecture074 Heaps in C++/heaps.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class heap { 5 | 6 | public: 7 | int arr[100]; 8 | int size; 9 | 10 | heap () { 11 | arr[0] = -1; 12 | size = 0; 13 | } 14 | 15 | void insertion (int val) { 16 | 17 | size = size + 1; 18 | int index = size; 19 | arr[index] = val; 20 | 21 | while (index > 1) { 22 | int parent = index / 2; 23 | 24 | if(arr[parent] < arr[index]) { 25 | swap(arr[parent], arr[index]); 26 | index = parent; 27 | } 28 | else 29 | return; 30 | } 31 | } 32 | 33 | void deletefromHeap() { 34 | 35 | if(size == 0) { 36 | cout << "Nothing to delete" << endl; 37 | return; 38 | } 39 | //step1 put the last element into first index 40 | arr[1] = arr[size]; 41 | //step2 remove last element 42 | size--; 43 | 44 | //step3 take root to its correct position 45 | int i = 1; 46 | while (i < size) { 47 | int leftIndex = 2*i; 48 | int rightIndex = (2*i) + 1; 49 | 50 | if(leftIndex < size && arr[i] < arr[leftIndex]) { 51 | swap(arr[i], arr[leftIndex]); 52 | i = leftIndex; 53 | } 54 | else if (rightIndex < size && arr[i] < arr[rightIndex]) { 55 | swap(arr[i], arr[rightIndex]); 56 | i = rightIndex; 57 | } 58 | else 59 | return; 60 | 61 | } 62 | } 63 | 64 | void print() { 65 | 66 | for (int i = 1; i <= size; i++) 67 | cout << arr[i] << " "; 68 | 69 | cout << endl; 70 | } 71 | }; 72 | 73 | void heapify (int arr[], int n, int i) { 74 | 75 | int largest = i; 76 | int left = 2*i; 77 | int right = (2*i)+1; 78 | 79 | if(left <= n && arr[largest] < arr[left]) 80 | largest = left; 81 | 82 | if(right <= n && arr[largest] < arr[right]) 83 | largest = right; 84 | 85 | if(largest != i) { 86 | swap(arr[largest], arr[i]); 87 | heapify(arr, n, largest); 88 | } 89 | } 90 | 91 | int main () { 92 | 93 | heap h; 94 | h.insertion(50); 95 | h.insertion(55); 96 | h.insertion(53); 97 | h.insertion(52); 98 | h.insertion(54); 99 | h.print(); 100 | h.deletefromHeap(); 101 | h.print(); 102 | 103 | int arr[6] = {-1, 54, 53, 55, 52, 50}; 104 | int n = 5; 105 | for(int i = n/2 ; i > 0; i--) 106 | heapify(arr, n, i); 107 | 108 | cout << "Heapify algorithm: " << endl; 109 | for(int i = 1 ; i <= n; i++) 110 | cout << arr[i] << " "; 111 | 112 | cout << endl; 113 | } -------------------------------------------------------------------------------- /Lecture023 2D arrays/2dArrayIntro.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool isPresent(int arr[][3], int target, int row, int col) { 5 | 6 | for(int row=0; row<3; row++) { 7 | for(int col=0; col<3; col++) { 8 | 9 | if( arr[row][col] == target) { 10 | return 1; 11 | } 12 | 13 | } 14 | } 15 | return 0; 16 | } 17 | 18 | //to print row wise sum 19 | void printColSum(int arr[][3], int row, int col) { 20 | cout << "Printing Sum -> " << endl; 21 | for(int col=0; col<3; col++) { 22 | int sum = 0; 23 | for(int row=0; row<3; row++) { 24 | sum += arr[row][col]; 25 | } 26 | cout << sum << " "; 27 | } 28 | 29 | cout << endl; 30 | } 31 | 32 | 33 | //to print row wise sum 34 | void printSum(int arr[][3], int row, int col) { 35 | cout << "Printing Sum -> " << endl; 36 | for(int row=0; row<3; row++) { 37 | int sum = 0; 38 | for(int col=0; col<3; col++) { 39 | sum += arr[row][col]; 40 | } 41 | cout << sum << " "; 42 | } 43 | 44 | cout << endl; 45 | } 46 | 47 | int largestRowSum(int arr[][3], int row, int col) { 48 | 49 | int maxi = INT_MIN; 50 | int rowIndex = -1; 51 | 52 | for(int row=0; row<3; row++) { 53 | int sum = 0; 54 | for(int col=0; col<3; col++) { 55 | sum += arr[row][col]; 56 | } 57 | 58 | if(sum > maxi ) { 59 | maxi = sum; 60 | rowIndex = row; 61 | } 62 | 63 | } 64 | 65 | cout << "the maximum sum is " << maxi << endl; 66 | return rowIndex; 67 | } 68 | 69 | int main() { 70 | 71 | //create 2 d array 72 | int arr[3][3]; 73 | //int arr[3][4] = {1,2,3,4,5,6,7,8,9,10,14,16}; 74 | //int arr[3][4] = {{1,11,111,1111}, {2,22,222,2222}, {3,33,333,3333}}; 75 | 76 | cout << "Enter the elements " << endl; 77 | //taking input -> row wise input 78 | for(int row=0; row<3; row++) { 79 | for(int col=0; col<3; col++) { 80 | cin >> arr[row][col]; 81 | } 82 | } 83 | 84 | 85 | /* 86 | //taking input -> col wise input 87 | for(int col=0; col<4; col++) { 88 | for(int row=0; row<3; row++) { 89 | cin >> arr[row][col]; 90 | } 91 | } 92 | 93 | */ 94 | 95 | cout << "Printing the array " << endl; 96 | //print 97 | for(int row=0; row<3; row++) { 98 | for(int col=0; col<3; col++) { 99 | cout << arr[row][col] << " "; 100 | } 101 | cout << endl; 102 | } 103 | /* 104 | cout <<" Enter the element to search " << endl; 105 | int target; 106 | cin >> target; 107 | 108 | if(isPresent(arr, target, 3, 3)) { 109 | cout <<" Element found " << endl; 110 | } 111 | else{ 112 | cout <<" Not Found" << endl; 113 | } 114 | 115 | printColSum(arr, 3, 3 ); 116 | */ 117 | int ansIndex = largestRowSum(arr,3,3); 118 | cout << " Max row is at index " << ansIndex << endl; 119 | 120 | return 0; 121 | } -------------------------------------------------------------------------------- /Lecture044 Linked List Day1/circularLinkedList.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Node { 5 | public: 6 | int data; 7 | Node* next; 8 | 9 | //constrcutor 10 | Node(int d) { 11 | this->data = d; 12 | this->next = NULL; 13 | } 14 | 15 | ~Node() { 16 | int value = this->data; 17 | if(this->next != NULL) { 18 | delete next; 19 | next = NULL; 20 | } 21 | cout << " memory is free for node with data " << value << endl; 22 | } 23 | 24 | }; 25 | 26 | void insertNode(Node* &tail, int element, int d) { 27 | 28 | 29 | //empty list 30 | if(tail == NULL) { 31 | Node* newNode = new Node(d); 32 | tail = newNode; 33 | newNode -> next = newNode; 34 | } 35 | else{ 36 | //non-empty list 37 | //assuming that the element is present in the list 38 | 39 | Node* curr = tail; 40 | 41 | while(curr->data != element) { 42 | curr = curr -> next; 43 | } 44 | 45 | //element found -> curr is representing element wala node 46 | Node* temp = new Node(d); 47 | temp -> next = curr -> next; 48 | curr -> next = temp; 49 | 50 | } 51 | 52 | } 53 | 54 | void print(Node* tail) { 55 | 56 | Node* temp = tail; 57 | 58 | //empty list 59 | if(tail == NULL) { 60 | cout << "List is Empty "<< endl; 61 | return ; 62 | } 63 | 64 | do { 65 | cout << tail -> data << " "; 66 | tail = tail -> next; 67 | } while(tail != temp); 68 | 69 | cout << endl; 70 | } 71 | 72 | void deleteNode(Node* &tail, int value) { 73 | 74 | //empty list 75 | if(tail == NULL) { 76 | cout << " List is empty, please check again" << endl; 77 | return; 78 | } 79 | else{ 80 | //non-empty 81 | 82 | //assuming that "value" is present in the Linked List 83 | Node* prev = tail; 84 | Node* curr = prev -> next; 85 | 86 | while(curr -> data != value) { 87 | prev = curr; 88 | curr = curr -> next; 89 | } 90 | 91 | prev -> next = curr -> next; 92 | 93 | //1 Node Linked List 94 | if(curr == prev) { 95 | tail = NULL; 96 | } 97 | 98 | //>=2 Node linked list 99 | else if(tail == curr ) { 100 | tail = prev; 101 | } 102 | 103 | curr -> next = NULL; 104 | delete curr; 105 | 106 | } 107 | 108 | } 109 | 110 | 111 | int main() { 112 | 113 | Node* tail = NULL; 114 | 115 | insertNode(tail, 5, 3); 116 | print(tail); 117 | 118 | insertNode(tail, 3, 5); 119 | print(tail); 120 | /* 121 | insertNode(tail, 5, 7); 122 | print(tail); 123 | 124 | insertNode(tail, 7, 9); 125 | print(tail); 126 | 127 | insertNode(tail, 5, 6); 128 | print(tail); 129 | 130 | insertNode(tail, 9, 10); 131 | print(tail); 132 | 133 | insertNode(tail, 3, 4); 134 | print(tail); 135 | */ 136 | 137 | deleteNode(tail, 5); 138 | print(tail); 139 | 140 | return 0; 141 | } -------------------------------------------------------------------------------- /Lecture088 Cycle Detection in Undirected Graphs/Coding Question.md: -------------------------------------------------------------------------------- 1 | # Coding Quesion 2 | 3 | 1. Cycle Detection In Undirected Graph: [https://www.codingninjas.com/studio/problems/cycle-detection-in-undirected-graph_1062670?topList=love-babbar-dsa-sheet-problems&leftPanelTab=0&utm_source=youtube&utm_medium=affiliate&utm_campaign=Lovebabbar] 4 | 5 | ``` cpp 6 | 1. //Depth First Search 7 | bool isCycleDFS (int node, int parent, unordered_map > &adj, unordered_map &visited) { 8 | 9 | visited[node] = 1; 10 | 11 | for(auto neighbour: adj[node]) { 12 | 13 | if (!visited[neighbour]) { 14 | bool ans = isCycleDFS(neighbour, node, adj, visited); 15 | 16 | if(ans) 17 | return true; 18 | } 19 | 20 | else if(neighbour != parent) 21 | return true; 22 | } 23 | return false; 24 | } 25 | 26 | string cycleDetection (vector>& edges, int n, int m) { 27 | // create an adjacency list 28 | unordered_map > adj; 29 | for (int i = 0; i < m; i++) { 30 | int u = edges[i][0]; 31 | int v = edges[i][1]; 32 | 33 | adj[u].push_back(v); 34 | adj[v].push_back(u); 35 | } 36 | 37 | // for disconnected graph 38 | unordered_map visited; 39 | for(int i = 0; i < n; i++){ 40 | 41 | if (!visited[i]) { 42 | bool ans = isCycleDFS(i, -1, adj, visited); 43 | 44 | if (ans == 1) 45 | return "Yes"; 46 | } 47 | } 48 | return "No"; 49 | } 50 | ``` 51 | 52 | ``` cpp 53 | // Breadth First Search 54 | bool isCycle (int src, unordered_map > &adj, unordered_map &visited) { 55 | 56 | unordered_map parent; 57 | parent[src] = -1; 58 | visited[src] = 1; 59 | queue q; 60 | q.push(src); 61 | 62 | while(!q.empty()) { 63 | 64 | int front = q.front(); 65 | q.pop(); 66 | for (auto neighbour : adj[front]) { 67 | if (visited[neighbour] == 1 && neighbour != parent[front]) 68 | return true; 69 | 70 | else if (!visited[neighbour]) { 71 | q.push(neighbour); 72 | visited[neighbour] = 1; 73 | parent[neighbour] = front; 74 | } 75 | } 76 | } 77 | return false; 78 | } 79 | 80 | string cycleDetection (vector>& edges, int n, int m) { 81 | // create an adjacency list 82 | unordered_map > adj; 83 | for (int i = 0; i < m; i++) { 84 | int u = edges[i][0]; 85 | int v = edges[i][1]; 86 | 87 | adj[u].push_back(v); 88 | adj[v].push_back(u); 89 | } 90 | 91 | // for disconnected graph 92 | unordered_map visited; 93 | for(int i = 0; i < n; i++){ 94 | 95 | if (!visited[i]) { 96 | bool ans = isCycle(i, adj, visited); 97 | 98 | if (ans == 1) 99 | return "Yes"; 100 | } 101 | } 102 | return "No"; 103 | } 104 | ``` 105 | -------------------------------------------------------------------------------- /Lecture044 Linked List Day1/singlyLinkedList.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Node { 5 | 6 | public: 7 | int data; 8 | Node* next; 9 | 10 | //constructor 11 | Node(int d) { 12 | this -> data = d; 13 | this -> next = NULL; 14 | } 15 | 16 | //destructor 17 | ~Node() { 18 | int value = this -> data; 19 | //memory free 20 | if(this ->next != NULL) { 21 | delete next; 22 | this -> next = NULL; 23 | } 24 | cout << " memory is free for node with data " << value << endl; 25 | } 26 | }; 27 | 28 | void insertAtHead(Node* &head, int d) { 29 | 30 | // new node create 31 | Node* temp = new Node(d); 32 | temp -> next = head; 33 | head = temp; 34 | } 35 | 36 | void insertAtTail(Node* &tail, int d) { 37 | // new node create 38 | Node* temp = new Node(d); 39 | tail -> next = temp; 40 | tail = temp; 41 | } 42 | 43 | void print(Node* &head) { 44 | Node* temp = head; 45 | 46 | while(temp != NULL ) { 47 | cout << temp -> data << " "; 48 | temp = temp -> next; 49 | } 50 | cout << endl; 51 | } 52 | 53 | void insertAtPosition(Node* &tail, Node* & head, int position, int d) { 54 | 55 | 56 | //insert at Start 57 | if(position == 1) { 58 | insertAtHead(head, d); 59 | return; 60 | } 61 | 62 | Node* temp = head; 63 | int cnt = 1; 64 | 65 | while(cnt < position-1) { 66 | temp = temp->next; 67 | cnt++; 68 | } 69 | 70 | //inserting at Last Position 71 | if(temp -> next == NULL) { 72 | insertAtTail(tail,d); 73 | return ; 74 | } 75 | 76 | //creating a node for d 77 | Node* nodeToInsert = new Node(d); 78 | 79 | nodeToInsert -> next = temp -> next; 80 | 81 | temp -> next = nodeToInsert; 82 | } 83 | 84 | void deleteNode(int position, Node* & head) { 85 | 86 | //deleting first or start node 87 | if(position == 1) { 88 | Node* temp = head; 89 | head = head -> next; 90 | //memory free start ndoe 91 | temp -> next = NULL; 92 | delete temp; 93 | } 94 | else 95 | { 96 | //deleting any middle node or last node 97 | Node* curr = head; 98 | Node* prev = NULL; 99 | 100 | int cnt = 1; 101 | while(cnt < position) { 102 | prev = curr; 103 | curr = curr -> next; 104 | cnt++; 105 | } 106 | 107 | prev -> next = curr -> next; 108 | curr -> next = NULL; 109 | delete curr; 110 | 111 | } 112 | } 113 | 114 | int main() { 115 | 116 | //created a new node 117 | Node* node1 = new Node(10); 118 | //cout << node1 -> data << endl; 119 | // cout << node1 -> next << endl; 120 | 121 | //head pointed to node1 122 | Node* head = node1; 123 | Node* tail = node1; 124 | print(head); 125 | 126 | insertAtTail(tail, 12); 127 | 128 | print(head); 129 | 130 | insertAtTail(tail, 15); 131 | print(head); 132 | 133 | insertAtPosition(tail,head, 4, 22); 134 | print(head); 135 | 136 | cout << "head " << head -> data << endl; 137 | cout << "tail " << tail -> data << endl; 138 | 139 | deleteNode(4, head); 140 | print(head); 141 | 142 | cout << "head " << head -> data << endl; 143 | cout << "tail " << tail -> data << endl; 144 | 145 | 146 | return 0; 147 | } --------------------------------------------------------------------------------