├── Lecture001 Introduction, Flow Charts, PseudoCode, Programming and Why ├── Readme.md └── flowchart.png ├── Lecture002 Write First C++ Program ├── firstProgram.cpp └── firstProgram.py ├── Lecture003 If-Else, While loop & Lots of Patterns ├── Pattern1.java ├── Pattern10.java ├── Pattern11.java ├── Pattern12.java ├── Pattern13.java ├── Pattern14.java ├── Pattern15.java ├── Pattern16.java ├── Pattern17.java ├── Pattern18.java ├── Pattern2.java ├── Pattern20.java ├── Pattern21.java ├── Pattern22.java ├── Pattern25.java ├── Pattern26.java ├── Pattern27.java ├── Pattern28.java ├── Pattern29.java ├── Pattern3.java ├── Pattern30.java ├── Pattern31.java ├── Pattern31A.java ├── Pattern32.java ├── Pattern32A.java ├── Pattern32B.java ├── Pattern32C.java ├── Pattern33.java ├── Pattern34.java ├── Pattern35.java ├── Pattern4.java ├── Pattern5.java ├── Pattern5A.java ├── Pattern6.java ├── Pattern7.java ├── Pattern8.java ├── Pattern9.java ├── Python Code │ ├── conditionals.py │ ├── pattern1.py │ ├── pattern2.py │ ├── primeNumber.py │ ├── sumOfN.py │ └── whileLoop.py ├── conditionals.cpp ├── pattern1.cpp ├── pattern2.cpp ├── primeNumber.cpp ├── sumOfN.cpp ├── sumOfN.py ├── whileLoop.cpp └── whileLoop.py ├── Lecture004 Solving Pattern Questions (Part-2) ├── Python Code │ ├── patern10.py │ ├── pattern1.py │ ├── pattern2.py │ ├── pattern3.py │ ├── pattern4.py │ ├── pattern5.py │ ├── pattern6.py │ ├── pattern7.py │ ├── pattern8.py │ └── pattern9.py ├── all_patterns.cpp ├── all_triangle_patterns.cpp ├── pattern1.cpp ├── pattern1.py ├── pattern10.cpp ├── pattern2.cpp ├── pattern2.py ├── pattern3.cpp ├── pattern3.py ├── pattern4.cpp ├── pattern5.cpp ├── pattern6.cpp ├── pattern7.cpp ├── pattern8.cpp └── pattern9.cpp ├── Lecture005 Bitwise Operators, For Loops, Operator Precedence & Variable Scoping ├── EvenOrOdd.java ├── FindUnique.java ├── FindithBitOfNumber.java ├── MagicNumber.java ├── NegativeNumberRepresentation.java ├── NumberOfDigitsInBaseb.java ├── NumberOfDigitsInBaseb2.java ├── NumberOfSetBits.java ├── PascalsTriangleSumOfNthRow.java ├── Power.java ├── PowerOf2OrNot.java ├── RangeXOR.java ├── bitwiseOperators.cpp ├── continue.cpp ├── forFib.cpp ├── forLoop.cpp ├── forPrime.cpp ├── forSum.cpp ├── numberOfOneBits.cpp ├── scope.cpp └── subtractProdAndSum.cpp ├── Lecture006 Binary & Decimal Number System ├── binToDec.cpp └── decToBin.cpp ├── Lecture007 LeetCode Problem Solving Session ├── complimentBase10Integer.cpp ├── powerOfTwo.cpp └── reverseInteger.cpp ├── Lecture008 Switch Statement & Functions ├── calculator.cpp ├── counting.cpp ├── evenOdd.cpp ├── functionsIntro.cpp ├── nCr.cpp ├── passByValue.cpp ├── power.cpp ├── primeNumber.cpp └── switch.cpp ├── Lecture009 Introduction to Arrays in C++ ├── arrayIntro.cpp ├── arrayScope.cpp ├── linearSeaerch.cpp ├── maxMin.cpp └── reverse.cpp ├── Lecture010 Solving LeetCode Arrays ├── arrayIntersection.cpp ├── duplicatesInArray.cpp ├── findUnique.cpp ├── pairSum.cpp ├── sort01.cpp └── swapAlternates.cpp ├── Lecture012 Binary Search Explained in 1 Video [Theory + Code] ├── BinarySearch.java ├── binarySearch.cpp └── binarySearch.py ├── Lecture013 Binary Search Interview Questions ├── firstLastPosition.cpp └── peakInMountain.cpp ├── Lecture014 Binary Search Interview Questions ├── pivot.cpp ├── searchRotatedArray.cpp └── sqrt.cpp ├── Lecture015 Binary Search Advanced Problems ├── aggressiveCows.cpp └── bookAllocationProblem.cpp ├── Lecture016 Selection Sort ├── SelectionSort.java ├── SelectionSort.py └── selectionSort.cpp ├── Lecture017 Bubble Sort ├── BubbleSort.java ├── bubbleSort.cpp └── bubbleSort.py ├── Lecture018 Insertion Sort ├── InsertionSort.java ├── insertionSort.cpp └── insertion_sort.py ├── Lecture020 ArrayQuestions ├── MergeSortedArr1Java.java ├── ReverseArrJava.java ├── mergeSortedArray1.cpp ├── reverseArray.cpp └── reverseArray.py ├── Lecture021 Solving Array Questions 2 ├── addArray.cpp ├── checkSortedRotatedArray.cpp └── rotateArray.cpp ├── Lecture022 Strings ├── intro.cpp ├── removeOccurrences.cpp ├── replaceSpaces.cpp ├── reverseString.cpp └── validPalindrome.cpp ├── Lecture023 2D arrays ├── 2dArrayIntro.cpp ├── Search2DMatrix1.java ├── Search2DMatrix2.java ├── SpiralPrint.java ├── WavePrint.java ├── search2DMatrix1.cpp ├── search2DMatrix2.cpp ├── spiralPrint.cpp └── wavePrint.cpp ├── Lecture024 Maths for DSA ├── FactorsOfANumber.java ├── GCD.java ├── LCM.java ├── NewtonsSquareRoot.java ├── PrimeNumber.java ├── SieveOfEratosthenes.java ├── SquareRootOfANumber.java ├── fastExpo.cpp ├── gcd.cpp ├── primeNumberBrute.cpp └── sieve.cpp ├── Lecture025 Intro to Pointers in C++ ├── intro.cpp └── play.cpp ├── Lecture026 Pointers with Arrays and Functions ├── pointer_array.cpp ├── pointer_characterArray.cpp └── pointer_functions.cpp ├── Lecture027 Double Pointers and MCQs ├── doublePointer.cpp └── pointerMCQ.cpp ├── Lecture028 Dynamic Allocation (Part-1) ├── ReferenceVariable.cpp └── dynamicArray.cpp ├── Lecture029 Dynamic Allocation (Part-2) └── 2dArray.cpp ├── Lecture030 Macros, Gloab Variable etc ├── counting.cpp ├── defaultArgs.cpp ├── globalVariable.cpp ├── inlineFunctions.cpp └── macros.cpp ├── Lecture031 Recursion Day1 ├── Factorial.java ├── Power.java ├── factroial.cpp ├── power.cpp ├── power.py └── tempCodeRunnerFile.cpp ├── Lecture032 Recursion Day2 ├── ClimbStairs.java ├── Fibbonacci.cpp ├── Fibonnaci.java ├── RecursionExample.java ├── SayDigits.cpp ├── SayDigits.java ├── climbStairs.cpp └── walkingExample.cpp ├── Lecture033 Recursion Day3 ├── BinarySearch.java ├── LinearSearch.java ├── SumCalc.java ├── binarySearch.cpp ├── isSorted.java ├── linearSearch.cpp ├── sortedArray.cpp └── sum.cpp ├── Lecture034 Recursion Day4 ├── BubbleSort.java ├── InsertionSort.java ├── SelectionSort.java ├── bubbleSort.cpp ├── exponent.cpp ├── palindromeCheck.cpp └── reverseString.cpp ├── Lecture035 Recursion Day5 ├── InversionCount.java ├── MergeSort.java └── mergeSort.cpp ├── Lecture036 Recursion Day6 ├── QuickSort.java └── quickSort.cpp ├── Lecture037 Recursion Day7 ├── subsequences.cpp └── subsets.cpp ├── Lecture038 Recursion Day8 └── keypad.cpp ├── Lecture042 OOPs Day1 ├── Hero.cpp └── intro.cpp ├── Lecture043 OOPs Day2 ├── ambiguity.cpp ├── concept.cpp ├── hierarchicalInheritance.cpp ├── inheritance.cpp ├── multiLevelInheritance.cpp ├── multipleInheritance.cpp ├── polymorphism.cpp └── singleInheritance.cpp ├── Lecture044 Linked List Day1 ├── circularLinkedList.cpp ├── doublyLinkedList.cpp └── singlyLinkedList.cpp ├── Lecture045 Linked List Day2 └── linkedList.cpp ├── Lecture046 Linked List Day3 └── reverseInKGroup.cpp ├── Lecture047 Linked List Day4 └── loop.cpp ├── Lecture048 Linked List Day5 └── duplicates.cpp ├── Lecture049 Linked List Day6 ├── mergeList.cpp └── sort.cpp ├── Lecture050 Linked List Day7 └── palindrome.cpp ├── Lecture051 Linked List Day8 └── add.cpp ├── Lecture052 Linked List Day9 └── clone.cpp ├── Lecture053 Linked List Day10 └── mergeSort.cpp ├── Lecture054 Stack & its Implementation ├── stackIntro.cpp └── twoStacks.cpp ├── Lecture055 Stack Questions ├── bracketReversals.cpp ├── deleteMiddle.cpp ├── insertAtBottom.cpp ├── redundantBrackets.cpp ├── reverseStack.cpp ├── reverseString.cpp ├── sortStack.cpp └── validBrackets.cpp ├── Lecture056 Stacks Questions ├── areaHistogram.cpp └── nextSmaller.cpp ├── Lecture057 Stack Questions ├── celebrity.cpp └── maxRectangle.cpp ├── Lecture058 Stack Questions └── NStack.cpp ├── Lecture059 Stack Questions └── specialStack.cpp ├── Lecture060 Queue & its Implementation ├── circularQueue.cpp ├── deque.cpp ├── implementQueue.cpp └── queueIntro.cpp ├── Lecture061 Queue Interview Questions ├── KQueue.cpp ├── circularTour.cpp ├── negativeInteger.cpp ├── nonRepeating.cpp ├── reverseFirstK.cpp ├── reverseQueue.cpp └── sumMaxMin.cpp ├── Lecture062 Tree Introduction ├── binaryTree.cpp └── countLeaf.cpp ├── Lecture063 Tree Interview Questions Day1 ├── balancedTree.cpp ├── diameter.cpp ├── height.cpp ├── identicalTree.cpp └── sumTree.cpp ├── Lecture064 Tree Interview Questions Day2 ├── LeftView.cpp ├── bottomView.cpp ├── boundaryTraversal.cpp ├── rightView.cpp ├── topView.cpp ├── verticalTraversal.cpp └── zigzag.cpp ├── Lecture065 Tree Interview Questions Day3 ├── bloodline.cpp ├── kAncestor.cpp ├── kSumPaths.cpp ├── lca.cpp └── maxSumNonAdjacentNodes.cpp └── README.md /Lecture001 Introduction, Flow Charts, PseudoCode, Programming and Why/Readme.md: -------------------------------------------------------------------------------- 1 | <h6> 2 | In this video, We would learn about what is flow chart & what is pseudocode. What is use of programming amd why to do programming? 3 | </h6> 4 | <ul> 5 | <li> 6 | <b>FlowChart:</b> It is visual representation of program using diagrams amd shapes having a fixed meaning.<br/> 7 | Example : In a flow chart the <b>Eclipse</b> denotes the starting and end of the program.<br/> 8 | <b>Parallelogram</b> denotes the input or output statement. <br/> 9 | <b>Rectangle</b> denotes the calculation or some arthimatic operations.<br /> 10 | <b>Rhombus</b> denotes the decision or comparison, etc.<br /> 11 | <b>Note : There is no symbol or shape for loops but decision shape can be used to make loop.</b> <br /> 12 | <img src="flowchart.png" /> 13 | </li> 14 | <li> 15 | <b>PseudoCode: </b> It is general instructions without any syntax but once it is written it can be easily converted into a code. 16 | </li> 17 | <li> 18 | <b>Programming Languages </b> are special lamguages which can be conberted into computer understandable format which is written in a fix format known as syntax. 19 | </li> 20 | <li> 21 | We know that a computer can perform calculations and other action better than human beings but we can't give instuctions directly to computer as it only understand 0 and 1. so we write a program in a human understandable format and then a program convert it into the computer understandable program this program is known as compiler. 22 | </li> 23 | </ul> 24 | -------------------------------------------------------------------------------- /Lecture001 Introduction, Flow Charts, PseudoCode, Programming and Why/flowchart.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/loveBabbar/CodeHelp-DSA-Busted-Series/b6b8347d191491c1a3a7d9c4b2c612f9987b91e3/Lecture001 Introduction, Flow Charts, PseudoCode, Programming and Why/flowchart.png -------------------------------------------------------------------------------- /Lecture002 Write First C++ Program/firstProgram.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | cout << " Namaste Dunia " << endl; 7 | 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /Lecture002 Write First C++ Program/firstProgram.py: -------------------------------------------------------------------------------- 1 | # first program 2 | 3 | print("Namaste Dunia") 4 | -------------------------------------------------------------------------------- /Lecture003 If-Else, While loop & Lots of Patterns/Pattern1.java: -------------------------------------------------------------------------------- 1 | package com.company; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Pattern1 { 6 | // Pattern 1 7 | public static void main(String [] args){ 8 | /* 9 | Pattern 1 10 | ******* 11 | ******* 12 | ******* 13 | */ 14 | // Creating Scanner Object For taking inputs from the USER 15 | Scanner sc = new Scanner(System.in); 16 | // Take Number of ROWS and COLUMNS from the USER 17 | System.out.print(" Enter Number of ROWS and COLUMNS for the Pattern: "); 18 | int noOfRows = sc.nextInt(); 19 | int noOfColumns = sc.nextInt(); 20 | 21 | // Procedure to Print the Required Pattern 22 | for(int row = 1; row <= noOfRows; row++){ 23 | for(int column = 1; column <= noOfColumns; column++){ 24 | System.out.print("*"); 25 | } 26 | System.out.println(); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Lecture003 If-Else, While loop & Lots of Patterns/Pattern10.java: -------------------------------------------------------------------------------- 1 | package com.company; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Pattern10 { 6 | // Pattern 10 7 | public static void main(String [] args){ 8 | /* 9 | Pattern 10 10 | * 11 | * * 12 | * * * 13 | * * * * 14 | * * * * * 15 | */ 16 | // Creating Scanner Object For taking inputs from the USER 17 | Scanner sc = new Scanner(System.in); 18 | // Take Number of ROWS and COLUMNS from the USER 19 | System.out.print(" Enter Number of ROWS for the Pattern: "); 20 | int n = sc.nextInt(); 21 | pattern(n); 22 | } 23 | static void pattern(int n){ 24 | // Procedure to print the Required Pattern 25 | for(int row =1; row <= n ; row++){ 26 | for(int spaces = 1; spaces<= n - row; spaces++){ 27 | System.out.print(" "); 28 | } 29 | for(int column = 1; column <= row ; column++){ 30 | System.out.print("* "); 31 | } 32 | System.out.println(); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Lecture003 If-Else, While loop & Lots of Patterns/Pattern11.java: -------------------------------------------------------------------------------- 1 | package com.company; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Pattern11 { 6 | // Pattern 11 7 | public static void main(String [] args){ 8 | /* 9 | Pattern 11 10 | * * * * * 11 | * * * * 12 | * * * 13 | * * 14 | * 15 | */ 16 | // Creating Scanner Object For taking inputs from the USER 17 | Scanner sc = new Scanner(System.in); 18 | // Take Number of ROWS and COLUMNS from the USER 19 | System.out.print(" Enter Number of ROWS for the Pattern: "); 20 | int n = sc.nextInt(); 21 | pattern(n); 22 | } 23 | static void pattern(int n){ 24 | // Procedure to print the Required Pattern 25 | for(int row =1; row <= n ; row++){ 26 | for(int spaces = 1; spaces<= row - 1; spaces++){ 27 | System.out.print(" "); 28 | } 29 | for(int column = 1; column <= n - row + 1; column++){ 30 | System.out.print("* "); 31 | } 32 | System.out.println(); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Lecture003 If-Else, While loop & Lots of Patterns/Pattern12.java: -------------------------------------------------------------------------------- 1 | package com.company; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Pattern12 { 6 | // Pattern 12 7 | public static void main(String [] args){ 8 | /* 9 | Pattern 12 10 | * * * * * 11 | * * * * 12 | * * * 13 | * * 14 | * 15 | * 16 | * * 17 | * * * 18 | * * * * 19 | * * * * * 20 | */ 21 | // Creating Scanner Object For taking inputs from the USER 22 | Scanner sc = new Scanner(System.in); 23 | // Take Number of ROWS and COLUMNS from the USER 24 | System.out.print(" Enter Number of ROWS for the Pattern: "); 25 | int n = sc.nextInt(); 26 | pattern(n); 27 | } 28 | static void pattern(int n){ 29 | // Procedure to print the Required Pattern 30 | for(int row =1; row <= 2 * n ; row++){ 31 | int totalSpaces = row > n ? n - (row - n) : row - 1; 32 | for(int spaces = 1; spaces<=totalSpaces; spaces++){ 33 | System.out.print(" "); 34 | } 35 | int totalColumns = row > n ? row - n : n - row + 1 ; 36 | for(int column = 1; column <= totalColumns; column++){ 37 | System.out.print("* "); 38 | } 39 | System.out.println(); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Lecture003 If-Else, While loop & Lots of Patterns/Pattern13.java: -------------------------------------------------------------------------------- 1 | package com.company; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Pattern13 { 6 | // Pattern 13 7 | public static void main(String [] args){ 8 | /* 9 | Pattern 13 10 | * 11 | * * 12 | * * 13 | * * 14 | ********* 15 | */ 16 | // Creating Scanner Object For taking inputs from the USER 17 | Scanner sc = new Scanner(System.in); 18 | // Take Number of ROWS and COLUMNS from the USER 19 | System.out.print(" Enter Number of ROWS for the Pattern: "); 20 | int n = sc.nextInt(); 21 | pattern(n); 22 | } 23 | static void pattern(int n){ 24 | // Procedure to print the Required Pattern 25 | int inSpace = 1; 26 | for(int row =1; row <= n ; row++){ 27 | for(int spaces = 1; spaces<= n - row; spaces++){ 28 | System.out.print(" "); 29 | } 30 | int totalColumns = row != n ? 2 : 2 * n -1 ; 31 | if(row == 1){ 32 | totalColumns = row; 33 | } 34 | for(int column = 1; column <= totalColumns ; column++){ 35 | System.out.print("*"); 36 | if(row >= 2 && row != n){ 37 | for(int space = 1; space <= inSpace ; space++){ 38 | System.out.print(" "); 39 | } 40 | inSpace += 1; 41 | } 42 | } 43 | System.out.println(); 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Lecture003 If-Else, While loop & Lots of Patterns/Pattern14.java: -------------------------------------------------------------------------------- 1 | package com.company; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Pattern14 { 6 | // Pattern 14 7 | public static void main(String [] args){ 8 | /* 9 | Pattern 14 10 | ********* 11 | * * 12 | * * 13 | * * 14 | * 15 | */ 16 | // Creating Scanner Object For taking inputs from the USER 17 | Scanner sc = new Scanner(System.in); 18 | // Take Number of ROWS and COLUMNS from the USER 19 | System.out.print(" Enter Number of ROWS for the Pattern: "); 20 | int n = sc.nextInt(); 21 | pattern(n); 22 | } 23 | static void pattern(int n){ 24 | // Procedure to print the Required Pattern 25 | int inSpace = 2 * n - 1 - 4; 26 | for(int row =1; row <= n ; row++){ 27 | for(int spaces = 1; spaces<= row - 1; spaces++){ 28 | System.out.print(" "); 29 | } 30 | int totalColumns = row != n ? 2 : 1 ; 31 | if(row == 1){ 32 | totalColumns = 2 * n - 1; 33 | } 34 | for(int column = 1; column <= totalColumns ; column++){ 35 | System.out.print("*"); 36 | if(row >= 2 && row != n){ 37 | for(int space = 1; space <= inSpace ; space++){ 38 | System.out.print(" "); 39 | } 40 | inSpace -= 1; 41 | } 42 | } 43 | System.out.println(); 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Lecture003 If-Else, While loop & Lots of Patterns/Pattern16.java: -------------------------------------------------------------------------------- 1 | package com.company; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Pattern16 { 6 | // Pattern 16 --> PASCAL'S TRIANGLE V.V. IMPORTANT 7 | public static void main(String [] args){ 8 | /* 9 | Pattern 16 --> PASCAL'S TRIANGLE V.V. IMPORTANT 10 | 1 11 | 1 1 12 | 1 2 1 13 | 1 3 3 1 14 | 1 4 6 4 1 15 | 16 | */ 17 | // Creating Scanner Object For taking inputs from the USER 18 | Scanner sc = new Scanner(System.in); 19 | // Take Number of ROWS and COLUMNS from the USER 20 | System.out.print(" Enter Number of ROWS for the Pattern: "); 21 | int n = sc.nextInt(); 22 | pattern(n); 23 | } 24 | static void pattern(int n){ 25 | // Procedure to print the Required Pattern 26 | 27 | for(int row =1; row <= n ; row++){ 28 | for(int spaces = 1; spaces<= n - row; spaces++){ 29 | System.out.print(" "); 30 | } 31 | int c = 1; 32 | for(int column = 1; column <= row ; column++){ 33 | System.out.print( c + " "); 34 | c = c * (row - column)/ column; 35 | } 36 | System.out.println(); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Lecture003 If-Else, While loop & Lots of Patterns/Pattern17.java: -------------------------------------------------------------------------------- 1 | package com.company; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Pattern17 { 6 | // Pattern 17 7 | public static void main(String [] args){ 8 | /* 9 | Pattern 17 10 | 1 11 | 2 1 2 12 | 3 2 1 2 3 13 | 4 3 2 1 2 3 4 14 | 5 4 3 2 1 2 3 4 5 15 | 4 3 2 1 2 3 4 16 | 3 2 1 2 3 17 | 2 1 2 18 | 1 19 | */ 20 | // Creating Scanner Object For taking inputs from the USER 21 | Scanner sc = new Scanner(System.in); 22 | // Take Number of ROWS and COLUMNS from the USER 23 | System.out.print(" Enter Number of ROWS for the Pattern: "); 24 | int n = sc.nextInt(); 25 | pattern(n); 26 | } 27 | static void pattern(int n){ 28 | // Procedure to print the Required Pattern 29 | for(int row =1; row < 2*n ; row++){ 30 | int c = row > n ? 2 * n - row : row ; 31 | int spaces = n - c; 32 | for(int i=0; i < spaces; i++){ 33 | System.out.print(" "); 34 | } 35 | 36 | for(int column = c; column >= 1; column--){ 37 | System.out.print(column + " "); 38 | } 39 | for(int column = 2; column <= c; column++){ 40 | System.out.print(column + " "); 41 | } 42 | System.out.println(); 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Lecture003 If-Else, While loop & Lots of Patterns/Pattern2.java: -------------------------------------------------------------------------------- 1 | package com.company; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Pattern2 { 6 | // Pattern 2 7 | public static void main(String [] args){ 8 | /* 9 | Pattern 2 10 | * 11 | ** 12 | *** 13 | **** 14 | */ 15 | // Creating Scanner Object For taking inputs from the USER 16 | Scanner sc = new Scanner(System.in); 17 | // Take Number of ROWS and COLUMNS from the USER 18 | System.out.print(" Enter Number of ROWS for the Pattern: "); 19 | int noOfRows = sc.nextInt(); 20 | 21 | // Procedure to Print the Required Pattern 22 | for(int row = 1; row < noOfRows; row++){ 23 | for(int column = 1; column <= row; column++){ 24 | System.out.print("*"); 25 | } 26 | System.out.println(); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Lecture003 If-Else, While loop & Lots of Patterns/Pattern20.java: -------------------------------------------------------------------------------- 1 | package com.company; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Pattern20 { 6 | // Pattern 20 7 | public static void main(String [] args){ 8 | /* 9 | Pattern 20 10 | **** 11 | * * 12 | * * 13 | * * 14 | **** 15 | */ 16 | // Creating Scanner Object For taking inputs from the USER 17 | Scanner sc = new Scanner(System.in); 18 | // Take Number of ROWS and COLUMNS from the USER 19 | System.out.print(" Enter Number of ROWS for the Pattern: "); 20 | int n = sc.nextInt(); 21 | pattern(n); 22 | } 23 | static void pattern(int n){ 24 | // Procedure to print the Required Pattern 25 | for(int row =1; row <= n ; row++){ 26 | int totalColumns = 2; 27 | if(row == 1 || row == n) 28 | totalColumns = n - 1; 29 | for(int column = 1; column <= totalColumns ; column++){ 30 | System.out.print("* "); 31 | if(row > 1 && row < n){ 32 | for(int spaces = 1; spaces <= n - 3 ; spaces++) 33 | System.out.print(" "); 34 | } 35 | } 36 | System.out.println(); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Lecture003 If-Else, While loop & Lots of Patterns/Pattern21.java: -------------------------------------------------------------------------------- 1 | package com.company; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Pattern21 { 6 | // Pattern 21 7 | public static void main(String[] args) { 8 | /* 9 | Pattern 21 10 | 1 11 | 2 3 12 | 4 5 6 13 | 7 8 9 10 14 | 11 12 13 14 15 15 | */ 16 | // Creating Scanner Object For taking inputs from the USER 17 | Scanner sc = new Scanner(System.in); 18 | // Take Number of ROWS and COLUMNS from the USER 19 | System.out.print(" Enter Number of ROWS for the Pattern: "); 20 | int n = sc.nextInt(); 21 | pattern(n); 22 | } 23 | 24 | static void pattern(int n) { 25 | // Procedure to print the Required Pattern 26 | int number = 1; 27 | for (int row = 1; row <= n; row++) { 28 | for (int column = 1; column <= row; column++) { 29 | System.out.print(number + " "); 30 | number++; 31 | } 32 | System.out.println(); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Lecture003 If-Else, While loop & Lots of Patterns/Pattern22.java: -------------------------------------------------------------------------------- 1 | package com.company; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Pattern22 { 6 | // Pattern 22 7 | public static void main(String[] args) { 8 | /* 9 | Pattern 22 10 | 1 11 | 0 1 12 | 1 0 1 13 | 0 1 0 1 14 | 1 0 1 0 1 15 | */ 16 | // Creating Scanner Object For taking inputs from the USER 17 | Scanner sc = new Scanner(System.in); 18 | // Take Number of ROWS and COLUMNS from the USER 19 | System.out.print(" Enter Number of ROWS for the Pattern: "); 20 | int n = sc.nextInt(); 21 | pattern(n); 22 | } 23 | 24 | static void pattern(int n) { 25 | // Procedure to print the Required Pattern 26 | for (int row = 1; row <= n; row++) { 27 | for (int column = 1; column <= row; column++) { 28 | if((row + column) % 2 == 0) 29 | System.out.print(0 + " "); 30 | else 31 | System.out.print(1 + " "); 32 | } 33 | System.out.println(); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Lecture003 If-Else, While loop & Lots of Patterns/Pattern25.java: -------------------------------------------------------------------------------- 1 | package com.company; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Pattern25 { 6 | // Pattern 25 7 | public static void main(String [] args){ 8 | /* 9 | Pattern 25 10 | ***** 11 | * * 12 | * * 13 | * * 14 | ***** 15 | */ 16 | // Creating Scanner Object For taking inputs from the USER 17 | Scanner sc = new Scanner(System.in); 18 | // Take Number of ROWS and COLUMNS from the USER 19 | System.out.print(" Enter Number of ROWS for the Pattern: "); 20 | int n = sc.nextInt(); 21 | pattern(n); 22 | } 23 | static void pattern(int n){ 24 | // Procedure to print the Required Pattern 25 | for(int row =1; row <= n ; row++){ 26 | // Trailing Spaces 27 | for(int spaces = 1; spaces <= n - row; spaces ++) 28 | System.out.print(" "); 29 | 30 | int totalColumns = 2; 31 | if(row == 1 || row == n) 32 | totalColumns = n - 1; 33 | for(int column = 1; column <= totalColumns ; column++){ 34 | System.out.print("* "); 35 | if(row > 1 && row < n){ 36 | for(int spaces = 1; spaces <= n - 3 ; spaces++) 37 | System.out.print(" "); 38 | } 39 | } 40 | System.out.println(); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Lecture003 If-Else, While loop & Lots of Patterns/Pattern26.java: -------------------------------------------------------------------------------- 1 | package com.company; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Pattern26 { 6 | // Pattern 26 7 | public static void main(String [] args){ 8 | /* 9 | Pattern 26 10 | 1 1 1 1 1 1 11 | 2 2 2 2 2 12 | 3 3 3 3 13 | 4 4 4 14 | 5 5 15 | 6 16 | */ 17 | // Creating Scanner Object For taking inputs from the USER 18 | Scanner sc = new Scanner(System.in); 19 | // Take Number of ROWS and COLUMNS from the USER 20 | System.out.print(" Enter Number of ROWS for the Pattern: "); 21 | int n = sc.nextInt(); 22 | pattern(n); 23 | } 24 | static void pattern(int n){ 25 | // Procedure to print the Required Pattern 26 | int number = 1; 27 | for(int row =1; row <= n ; row++){ 28 | 29 | for(int column = 1; column <= n - row +1 ; column++){ 30 | System.out.print(number + " "); 31 | } 32 | 33 | number ++; 34 | System.out.println(); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Lecture003 If-Else, While loop & Lots of Patterns/Pattern27.java: -------------------------------------------------------------------------------- 1 | package com.company; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Pattern27 { 6 | // Pattern 27 7 | public static void main(String [] args){ 8 | /* 9 | Pattern 27 10 | 1 2 3 4 17 18 19 20 11 | 5 6 7 14 15 16 12 | 8 9 12 13 13 | 10 11 14 | */ 15 | // Creating Scanner Object For taking inputs from the USER 16 | Scanner sc = new Scanner(System.in); 17 | // Take Number of ROWS and COLUMNS from the USER 18 | System.out.print(" Enter Number of ROWS for the Pattern: "); 19 | int n = sc.nextInt(); 20 | pattern(n); 21 | } 22 | static void pattern(int n){ 23 | // Procedure to print the Required Pattern 24 | int number = 1; 25 | int secondHalf = (number * number) + 1; 26 | for(int row =1; row <= n ; row++){ 27 | for(int spaces = 1; spaces <= row - 1; spaces++) { 28 | System.out.print(" "); 29 | } 30 | 31 | for(int column = 1; column <= n - row + 1; column++){ 32 | System.out.print(number + " "); 33 | number ++; 34 | } 35 | 36 | for(int column = 1; column <=n - row + 1; column++ ){ 37 | System.out.print(secondHalf + " "); 38 | secondHalf ++; 39 | } 40 | System.out.println(); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Lecture003 If-Else, While loop & Lots of Patterns/Pattern28.java: -------------------------------------------------------------------------------- 1 | package com.company; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Pattern28 { 6 | // Pattern 28 7 | public static void main(String [] args){ 8 | /* 9 | Pattern 28 10 | * 11 | * * 12 | * * * 13 | * * 14 | * 15 | */ 16 | // Creating Scanner Object For taking inputs from the USER 17 | Scanner sc = new Scanner(System.in); 18 | // Take Number of ROWS and COLUMNS from the USER 19 | System.out.print(" Enter Number of ROWS for the Pattern: "); 20 | int n = sc.nextInt(); 21 | pattern(n); 22 | } 23 | static void pattern(int n){ 24 | // Procedure to print the Required Pattern 25 | for(int row =0; row < 2*n ; row++){ 26 | int totalColsInRow = row > n ? 2 * n - row : row ; 27 | int spaces = n - totalColsInRow; 28 | for(int i=0; i< spaces; i++){ 29 | System.out.print(" "); 30 | } 31 | for(int column = 0; column < totalColsInRow; column ++){ 32 | System.out.print("* "); 33 | } 34 | System.out.println(); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Lecture003 If-Else, While loop & Lots of Patterns/Pattern3.java: -------------------------------------------------------------------------------- 1 | package com.company; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Pattern3 { 6 | // Pattern 2 7 | public static void main(String [] args){ 8 | /* 9 | Pattern 2 10 | **** 11 | *** 12 | ** 13 | * 14 | */ 15 | // Creating Scanner Object For taking inputs from the USER 16 | Scanner sc = new Scanner(System.in); 17 | // Take Number of ROWS and COLUMNS from the USER 18 | System.out.print(" Enter Number of ROWS for the Pattern: "); 19 | int noOfRows = sc.nextInt(); 20 | 21 | // Procedure to Print the Required Pattern 22 | for(int row = 1; row <= noOfRows; row++){ 23 | for(int column = noOfRows; column >= row; column--){ 24 | System.out.print("*"); 25 | } 26 | System.out.println(); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Lecture003 If-Else, While loop & Lots of Patterns/Pattern30.java: -------------------------------------------------------------------------------- 1 | package com.company; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Pattern30 { 6 | // Pattern 30 7 | public static void main(String [] args){ 8 | /* 9 | Pattern 30 10 | 1 11 | 2 1 2 12 | 3 2 1 2 3 13 | 4 3 2 1 2 3 4 14 | 5 4 3 2 1 2 3 4 5 15 | */ 16 | // Creating Scanner Object For taking inputs from the USER 17 | Scanner sc = new Scanner(System.in); 18 | // Take Number of ROWS and COLUMNS from the USER 19 | System.out.print(" Enter Number of ROWS for the Pattern: "); 20 | int n = sc.nextInt(); 21 | pattern(n); 22 | } 23 | static void pattern(int n){ 24 | // Procedure to print the Required Pattern 25 | for(int row =1; row <= n ; row++){ 26 | int spaces = n - row; 27 | for(int i=0; i < spaces; i++){ 28 | System.out.print(" "); 29 | } 30 | for(int column = row; column >= 1; column--){ 31 | System.out.print(column + " "); 32 | } 33 | for(int column = 2; column <= row; column++){ 34 | System.out.print(column + " "); 35 | } 36 | System.out.println(); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Lecture003 If-Else, While loop & Lots of Patterns/Pattern31.java: -------------------------------------------------------------------------------- 1 | package com.company; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Pattern31 { 6 | // Pattern 31 7 | public static void main(String [] args){ 8 | /* 9 | Pattern 31 10 | 4 4 4 4 4 4 4 11 | 4 3 3 3 3 3 4 12 | 4 3 2 2 2 3 4 13 | 4 3 2 1 2 3 4 14 | 4 3 2 2 2 3 4 15 | 4 3 3 3 3 3 4 16 | 4 4 4 4 4 4 4 17 | 18 | */ 19 | // Creating Scanner Object For taking inputs from the USER 20 | Scanner sc = new Scanner(System.in); 21 | // Take Number of ROWS and COLUMNS from the USER 22 | System.out.print(" Enter Number of ROWS for the Pattern: "); 23 | int n = sc.nextInt(); 24 | pattern(n); 25 | } 26 | static void pattern(int n){ 27 | // Procedure to print the Required Pattern 28 | int rcSize = 2 * n - 1; // Row Column SIZE 29 | for(int row =1; row <=rcSize; row++){ 30 | for(int column = 1; column <= rcSize; column++){ 31 | int atEveryIndex = n - Math.min(Math.min(row - 1, column - 1), Math.min(rcSize - row , rcSize - column)); 32 | System.out.print(atEveryIndex + " "); 33 | } 34 | System.out.println(); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Lecture003 If-Else, While loop & Lots of Patterns/Pattern31A.java: -------------------------------------------------------------------------------- 1 | package com.company; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Pattern31A { 6 | // Pattern 31 A 7 | public static void main(String [] args){ 8 | /* 9 | Pattern 31 A 10 | 0 0 0 0 0 0 0 0 11 | 0 1 1 1 1 1 1 0 12 | 0 1 2 2 2 2 1 0 13 | 0 1 2 3 3 2 1 0 14 | 0 1 2 3 3 2 1 0 15 | 0 1 2 2 2 2 1 0 16 | 0 1 1 1 1 1 1 0 17 | 0 0 0 0 0 0 0 0 18 | 19 | */ 20 | // Creating Scanner Object For taking inputs from the USER 21 | Scanner sc = new Scanner(System.in); 22 | // Take Number of ROWS and COLUMNS from the USER 23 | System.out.print(" Enter Number of ROWS for the Pattern: "); 24 | int n = sc.nextInt(); 25 | pattern(n); 26 | } 27 | static void pattern(int n){ 28 | // Procedure to print the Required Pattern 29 | int rcSize = 2 * n - 1; // Row Column SIZE 30 | for(int row =0; row <= rcSize; row++){ 31 | for(int column = 0; column <= rcSize; column++){ 32 | int atEveryIndex = Math.min(Math.min(row, column), Math.min(rcSize - row , rcSize - column)); 33 | System.out.print(atEveryIndex + " "); 34 | } 35 | System.out.println(); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /Lecture003 If-Else, While loop & Lots of Patterns/Pattern32.java: -------------------------------------------------------------------------------- 1 | package com.company; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Pattern32 { 6 | // Pattern 32 7 | public static void main(String [] args){ 8 | /* 9 | Pattern 32 10 | E 11 | D E 12 | C D E 13 | B C D E 14 | A B C D E 15 | */ 16 | // Creating Scanner Object For taking inputs from the USER 17 | Scanner sc = new Scanner(System.in); 18 | // Take Number of ROWS and COLUMNS from the USER 19 | System.out.print(" Enter Number of ROWS for the Pattern: "); 20 | int n = sc.nextInt(); 21 | pattern(n); 22 | } 23 | static void pattern(int n){ 24 | // Procedure to print the Required Pattern 25 | for(int row =1; row <= n ; row++){ 26 | 27 | int ch = 60 + n - row + 1 + 4; // ( e.g E = 69 (in ASCII) so 60 + 5 - 1 + 1 + 4 ) 28 | for(int column = 1; column <= row ; column++){ 29 | System.out.print( (char)(ch) + " "); 30 | ch ++; 31 | } 32 | System.out.println(); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Lecture003 If-Else, While loop & Lots of Patterns/Pattern32A.java: -------------------------------------------------------------------------------- 1 | package com.company; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Pattern32A { 6 | // Pattern 32 A 7 | public static void main(String [] args){ 8 | /* 9 | Pattern 32 A 10 | 5 11 | 4 5 12 | 3 4 5 13 | 2 3 4 5 14 | 1 2 3 4 5 15 | */ 16 | // Creating Scanner Object For taking inputs from the USER 17 | Scanner sc = new Scanner(System.in); 18 | // Take Number of ROWS and COLUMNS from the USER 19 | System.out.print(" Enter Number of ROWS for the Pattern: "); 20 | int n = sc.nextInt(); 21 | pattern(n); 22 | } 23 | static void pattern(int n){ 24 | // Procedure to print the Required Pattern 25 | for(int row =1; row <= n ; row++){ 26 | 27 | int number = n - row + 1; 28 | for(int column = 1; column <= row ; column++){ 29 | System.out.print( number + " "); 30 | number ++; 31 | } 32 | System.out.println(); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Lecture003 If-Else, While loop & Lots of Patterns/Pattern32B.java: -------------------------------------------------------------------------------- 1 | package com.company; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Pattern32B { 6 | // Pattern 32B 7 | public static void main(String [] args){ 8 | /* 9 | Pattern 32B 10 | E 11 | E F 12 | E F G 13 | E F G H 14 | E F G H I 15 | */ 16 | // Creating Scanner Object For taking inputs from the USER 17 | Scanner sc = new Scanner(System.in); 18 | // Take Number of ROWS and COLUMNS from the USER 19 | System.out.print(" Enter Number of ROWS for the Pattern: "); 20 | int n = sc.nextInt(); 21 | pattern(n); 22 | } 23 | static void pattern(int n){ 24 | // Procedure to print the Required Pattern 25 | for(int row =1; row <= n ; row++){ 26 | 27 | int ch = 60 + n + 4; // ( e.g E = 69 (in ASCII) so 60 + 5 + 4 ) 28 | for(int column = 1; column <= row ; column++){ 29 | System.out.print( (char)(ch) + " "); 30 | ch ++; 31 | } 32 | System.out.println(); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Lecture003 If-Else, While loop & Lots of Patterns/Pattern32C.java: -------------------------------------------------------------------------------- 1 | package com.company; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Pattern32C { 6 | // Pattern 32 C 7 | public static void main(String [] args){ 8 | /* 9 | Pattern 32 C 10 | E 11 | E D 12 | E D C 13 | E D C B 14 | E D C B A 15 | */ 16 | // Creating Scanner Object For taking inputs from the USER 17 | Scanner sc = new Scanner(System.in); 18 | // Take Number of ROWS and COLUMNS from the USER 19 | System.out.print(" Enter Number of ROWS for the Pattern: "); 20 | int n = sc.nextInt(); 21 | pattern(n); 22 | } 23 | static void pattern(int n){ 24 | // Procedure to print the Required Pattern 25 | for(int row =1; row <= n ; row++){ 26 | 27 | int ch = 60 + n + 4; // ( e.g E = 69 (in ASCII) so 60 + 5 + 4 ) 28 | for(int column = 1; column <= row ; column++){ 29 | System.out.print( (char)(ch) + " "); 30 | ch --; 31 | } 32 | System.out.println(); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Lecture003 If-Else, While loop & Lots of Patterns/Pattern33.java: -------------------------------------------------------------------------------- 1 | package com.company; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Pattern33 { 6 | // Pattern 33 7 | public static void main(String [] args){ 8 | /* 9 | Pattern 33 10 | a 11 | B c 12 | D e F 13 | g H i J 14 | k L m N o 15 | */ 16 | // Creating Scanner Object For taking inputs from the USER 17 | Scanner sc = new Scanner(System.in); 18 | // Take Number of ROWS and COLUMNS from the USER 19 | System.out.print(" Enter Number of ROWS for the Pattern: "); 20 | int n = sc.nextInt(); 21 | pattern(n); 22 | } 23 | static void pattern(int n){ 24 | // Procedure to print the Required Pattern 25 | int ch = 65; 26 | for(int row =1; row <= n ; row++){ 27 | for(int column = 1; column <= row ; column++){ 28 | if(ch % 2 == 0) 29 | System.out.print((char)(ch) + " "); 30 | else 31 | System.out.print((char)(ch + 32) + " "); 32 | ch ++; 33 | } 34 | System.out.println(); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Lecture003 If-Else, While loop & Lots of Patterns/Pattern34.java: -------------------------------------------------------------------------------- 1 | package com.company; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Pattern34 { 6 | // Pattern 34 7 | public static void main(String [] args){ 8 | /* 9 | Pattern 34 10 | E D C B A 11 | D C B A 12 | C B A 13 | B A 14 | A 15 | */ 16 | // Creating Scanner Object For taking inputs from the USER 17 | Scanner sc = new Scanner(System.in); 18 | // Take Number of ROWS and COLUMNS from the USER 19 | System.out.print(" Enter Number of ROWS for the Pattern: "); 20 | int n = sc.nextInt(); 21 | pattern(n); 22 | } 23 | static void pattern(int n){ 24 | // Procedure to print the Required Pattern 25 | for(int row =1; row <= n ; row++){ 26 | 27 | int ch = 60 + n - row + 1 + 4; // ( e.g E = 69 (in ASCII) so 60 + 5 - 1 + 1 + 4 ) 28 | for(int column = 1; column <= n - row + 1 ; column++){ 29 | System.out.print( (char)(ch) + " "); 30 | ch -- ; 31 | } 32 | System.out.println(); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Lecture003 If-Else, While loop & Lots of Patterns/Pattern35.java: -------------------------------------------------------------------------------- 1 | package com.company; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Pattern35 { 6 | // Pattern 35 7 | public static void main(String [] args){ 8 | /* 9 | Pattern 35 10 | 1 1 11 | 12 21 12 | 123 321 13 | 12344321 14 | */ 15 | // Creating Scanner Object For taking inputs from the USER 16 | Scanner sc = new Scanner(System.in); 17 | // Take Number of ROWS and COLUMNS from the USER 18 | System.out.print(" Enter Number of ROWS for the Pattern: "); 19 | int n = sc.nextInt(); 20 | pattern(n); 21 | } 22 | static void pattern(int n){ 23 | // Procedure to print the Required Pattern 24 | int inSpaces = 2 * n - 2; 25 | for(int row =1; row <= n ; row++){ 26 | 27 | for(int column = 1; column <= row ; column++){ 28 | System.out.print(column); 29 | } 30 | 31 | // Printing Spaces In SIDE 32 | for(int spaces = 1; spaces <= inSpaces; spaces++){ 33 | System.out.print(" "); 34 | } 35 | inSpaces = inSpaces - 2; 36 | 37 | for(int column = row; column >= 1; column--) 38 | System.out.print(column); 39 | System.out.println(); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Lecture003 If-Else, While loop & Lots of Patterns/Pattern4.java: -------------------------------------------------------------------------------- 1 | package com.company; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Pattern4 { 6 | // Pattern 4 7 | public static void main(String [] args){ 8 | /* 9 | Pattern 4 10 | 1 11 | 1 2 12 | 1 2 3 13 | 1 2 3 4 14 | */ 15 | // Creating Scanner Object For taking inputs from the USER 16 | Scanner sc = new Scanner(System.in); 17 | // Take Number of ROWS and COLUMNS from the USER 18 | System.out.print(" Enter Number of ROWS for the Pattern: "); 19 | int noOfRows = sc.nextInt(); 20 | 21 | // Procedure to Print the Required Pattern 22 | for(int row = 1; row <= noOfRows; row++){ 23 | for(int column = 1; column <= row; column++){ 24 | System.out.print(column + " "); 25 | } 26 | System.out.println(); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Lecture003 If-Else, While loop & Lots of Patterns/Pattern5.java: -------------------------------------------------------------------------------- 1 | package com.company; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Pattern5 { 6 | // Pattern 5 7 | public static void main(String [] args){ 8 | /* 9 | Pattern 5 10 | * 11 | * * 12 | * * * 13 | * * 14 | * 15 | */ 16 | // Creating Scanner Object For taking inputs from the USER 17 | Scanner sc = new Scanner(System.in); 18 | // Take Number of ROWS and COLUMNS from the USER 19 | System.out.print(" Enter Number of ROWS for the Pattern: "); 20 | int n = sc.nextInt(); 21 | pattern5(n); 22 | } 23 | static void pattern5(int n){ 24 | // Procedure to print the Required Pattern 25 | for(int row =0; row < 2*n ; row++){ 26 | int totalColsInRow = row > n ? 2 * n - row : row ; 27 | for(int column = 0; column < totalColsInRow; column ++){ 28 | System.out.print("* "); 29 | } 30 | System.out.println(); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Lecture003 If-Else, While loop & Lots of Patterns/Pattern5A.java: -------------------------------------------------------------------------------- 1 | package com.company; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Pattern5A { 6 | // Pattern 5 A 7 | public static void main(String [] args){ 8 | /* 9 | Pattern 5 A 10 | 1 11 | 1 2 12 | 1 2 3 13 | 1 2 14 | 1 15 | */ 16 | // Creating Scanner Object For taking inputs from the USER 17 | Scanner sc = new Scanner(System.in); 18 | // Take Number of ROWS and COLUMNS from the USER 19 | System.out.print(" Enter Number of ROWS for the Pattern: "); 20 | int n = sc.nextInt(); 21 | pattern5(n); 22 | } 23 | static void pattern5(int n){ 24 | // Procedure to print the Required Pattern 25 | for(int row =0; row < 2*n ; row++){ 26 | int totalColsInRow = row > n ? 2 * n - row : row ; 27 | for(int column = 0; column < totalColsInRow; column ++){ 28 | System.out.print( (column+1)+ " "); 29 | } 30 | System.out.println(); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Lecture003 If-Else, While loop & Lots of Patterns/Pattern6.java: -------------------------------------------------------------------------------- 1 | package com.company; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Pattern6 { 6 | // Pattern 6 7 | public static void main(String [] args){ 8 | /* 9 | Pattern 6 10 | * 11 | ** 12 | *** 13 | **** 14 | ***** 15 | */ 16 | // Creating Scanner Object For taking inputs from the USER 17 | Scanner sc = new Scanner(System.in); 18 | // Take Number of ROWS and COLUMNS from the USER 19 | System.out.print(" Enter Number of ROWS for the Pattern: "); 20 | int n = sc.nextInt(); 21 | pattern(n); 22 | } 23 | static void pattern(int n){ 24 | // Procedure to print the Required Pattern 25 | for(int row =1; row <= n ; row++){ 26 | for(int spaces = 1; spaces<= n - row; spaces++){ 27 | System.out.print(" "); 28 | } 29 | for(int column = 1; column <= row; column ++){ 30 | System.out.print("* "); 31 | } 32 | System.out.println(); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Lecture003 If-Else, While loop & Lots of Patterns/Pattern7.java: -------------------------------------------------------------------------------- 1 | package com.company; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Pattern7 { 6 | // Pattern 7 7 | public static void main(String [] args){ 8 | /* 9 | Pattern 7 10 | ***** 11 | **** 12 | *** 13 | ** 14 | * 15 | */ 16 | // Creating Scanner Object For taking inputs from the USER 17 | Scanner sc = new Scanner(System.in); 18 | // Take Number of ROWS and COLUMNS from the USER 19 | System.out.print(" Enter Number of ROWS for the Pattern: "); 20 | int n = sc.nextInt(); 21 | pattern(n); 22 | } 23 | static void pattern(int n){ 24 | // Procedure to print the Required Pattern 25 | for(int row =1; row <= n ; row++){ 26 | for(int spaces = 1; spaces<= row -1; spaces++){ 27 | System.out.print(" "); 28 | } 29 | for(int column = 1; column <= n-row +1; column ++){ 30 | System.out.print("* "); 31 | } 32 | System.out.println(); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Lecture003 If-Else, While loop & Lots of Patterns/Pattern8.java: -------------------------------------------------------------------------------- 1 | package com.company; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Pattern8 { 6 | // Pattern 8 7 | public static void main(String [] args){ 8 | /* 9 | Pattern 8 10 | * 11 | *** 12 | ***** 13 | ******* 14 | ********* 15 | */ 16 | // Creating Scanner Object For taking inputs from the USER 17 | Scanner sc = new Scanner(System.in); 18 | // Take Number of ROWS and COLUMNS from the USER 19 | System.out.print(" Enter Number of ROWS for the Pattern: "); 20 | int n = sc.nextInt(); 21 | pattern(n); 22 | } 23 | static void pattern(int n){ 24 | // Procedure to print the Required Pattern 25 | for(int row =1; row <= n ; row++){ 26 | for(int spaces = 1; spaces<= n - row; spaces++){ 27 | System.out.print(" "); 28 | } 29 | for(int column = 1; column <= 2 * row - 1 ; column++){ 30 | System.out.print("* "); 31 | } 32 | System.out.println(); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Lecture003 If-Else, While loop & Lots of Patterns/Pattern9.java: -------------------------------------------------------------------------------- 1 | package com.company; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Pattern9 { 6 | // Pattern 9 7 | public static void main(String [] args){ 8 | /* 9 | Pattern 9 10 | ********* 11 | ******* 12 | ***** 13 | *** 14 | * 15 | */ 16 | // Creating Scanner Object For taking inputs from the USER 17 | Scanner sc = new Scanner(System.in); 18 | // Take Number of ROWS and COLUMNS from the USER 19 | System.out.print(" Enter Number of ROWS for the Pattern: "); 20 | int n = sc.nextInt(); 21 | pattern(n); 22 | } 23 | static void pattern(int n){ 24 | // Procedure to print the Required Pattern 25 | int odd = 1; 26 | for(int row =1; row <= n ; row++){ 27 | for(int spaces = 1; spaces<= row - 1; spaces++){ 28 | System.out.print(" "); 29 | } 30 | 31 | for(int column = 1; column <= 2 * n - odd; column++){ 32 | System.out.print("* "); 33 | } 34 | odd += 2; 35 | System.out.println(); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /Lecture003 If-Else, While loop & Lots of Patterns/Python Code/conditionals.py: -------------------------------------------------------------------------------- 1 | a = int(input("Enter the value of a : ")) 2 | if a >0: 3 | print("A is positive") 4 | elif a<0: 5 | print("A is negative") 6 | else: 7 | print("A is 0") 8 | -------------------------------------------------------------------------------- /Lecture003 If-Else, While loop & Lots of Patterns/Python Code/pattern1.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | i = 0 3 | 4 | while(i < n): 5 | j = 0 6 | while(j < n): 7 | j = j + 1 8 | print('*', end="") 9 | i = i + 1 10 | print('') 11 | -------------------------------------------------------------------------------- /Lecture003 If-Else, While loop & Lots of Patterns/Python Code/pattern2.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | i = 1 3 | 4 | while(i < n+1): 5 | j = 0 6 | while(j < n): 7 | j = j + 1 8 | print(i, end="") 9 | i = i + 1 10 | print('') 11 | -------------------------------------------------------------------------------- /Lecture003 If-Else, While loop & Lots of Patterns/Python Code/primeNumber.py: -------------------------------------------------------------------------------- 1 | num = int(input()) 2 | 3 | if num > 1: 4 | for i in range(2,num): 5 | if (num % i) == 0: 6 | print(num,"is not a prime number") 7 | break 8 | else: 9 | print(num,"is a prime number") 10 | else: 11 | print(num,"is not a prime number") 12 | -------------------------------------------------------------------------------- /Lecture003 If-Else, While loop & Lots of Patterns/Python Code/sumOfN.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | sum = 0 3 | 4 | while n > 0: 5 | sum += n 6 | n -= 1 7 | 8 | print("Value of sum is",sum) 9 | -------------------------------------------------------------------------------- /Lecture003 If-Else, While loop & Lots of Patterns/Python Code/whileLoop.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | i = 1 3 | 4 | while i<=n: 5 | print(i) 6 | i += 1 7 | -------------------------------------------------------------------------------- /Lecture003 If-Else, While loop & Lots of Patterns/conditionals.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 2 | using namespace std; 3 | 4 | int main() { 5 | //int a; 6 | //cin>>a; 7 | 8 | //cout<<" value of n is :" << n <<endl; 9 | 10 | //if a is positive 11 | /* 12 | if(a>0) { 13 | cout<<" A is Positive" << endl; 14 | } 15 | else{ 16 | cout<<" A is negative" << endl; 17 | } 18 | 19 | 20 | int a,b; 21 | 22 | cout<<"Enter the value of a "<<endl; 23 | cin>>a; 24 | cout<<"Enter the value of b "<<endl; 25 | cin>>b; 26 | 27 | if(a>b) { 28 | cout<<" A is greater " << endl; 29 | } 30 | if(b>a) { 31 | cout<<" B is greater " << endl; 32 | } 33 | 34 | */ 35 | 36 | int a ; 37 | cout<<" enter the value of a "<<endl; 38 | cin>>a; 39 | 40 | 41 | 42 | if(a>0) { 43 | cout<<" A is positive"<< endl; 44 | } 45 | else if(a<0) { 46 | cout<<" A is negative"<<endl; 47 | } 48 | else { 49 | cout<<" A is 0"<<endl; 50 | } 51 | 52 | 53 | 54 | } -------------------------------------------------------------------------------- /Lecture003 If-Else, While loop & Lots of Patterns/pattern1.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 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 | 13 | int j = 1; 14 | while(j<=n) { 15 | cout<< "*"; 16 | j = j+1; 17 | } 18 | cout<<endl; 19 | 20 | i = i+1; 21 | } 22 | } -------------------------------------------------------------------------------- /Lecture003 If-Else, While loop & Lots of Patterns/pattern2.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 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 | 13 | int j = 1; 14 | while(j<=n) { 15 | cout << i; 16 | j = j + 1; 17 | } 18 | cout<<endl; 19 | i = i+1; 20 | } 21 | } -------------------------------------------------------------------------------- /Lecture003 If-Else, While loop & Lots of Patterns/primeNumber.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | int n; 7 | cin >> n; 8 | 9 | int i = 2; 10 | 11 | while(i<n) { 12 | //divide hogya, not prime 13 | if(n%i==0) { 14 | cout<< " Not prime for "<< i << endl; 15 | } 16 | else{ 17 | cout<<" Prime for "<< i <<endl; 18 | } 19 | i = i + 1; 20 | } 21 | 22 | } -------------------------------------------------------------------------------- /Lecture003 If-Else, While loop & Lots of Patterns/sumOfN.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 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 = i + 1; 15 | } 16 | 17 | cout<< "value of sum is "<<sum<<endl; 18 | 19 | } -------------------------------------------------------------------------------- /Lecture003 If-Else, While loop & Lots of Patterns/sumOfN.py: -------------------------------------------------------------------------------- 1 | n=int(input()) 2 | i=1 3 | sum=0 4 | while i<=n: 5 | sum+=i 6 | i+=1 7 | print(f"value of sum is {sum}") 8 | -------------------------------------------------------------------------------- /Lecture003 If-Else, While loop & Lots of Patterns/whileLoop.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 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 | 13 | cout<<i<<" "; 14 | i = i+1; 15 | 16 | } 17 | 18 | } -------------------------------------------------------------------------------- /Lecture003 If-Else, While loop & Lots of Patterns/whileLoop.py: -------------------------------------------------------------------------------- 1 | n=int(input()) 2 | i=1 3 | while i<=n: 4 | print(i,end=" ") 5 | i=i+1 6 | 7 | -------------------------------------------------------------------------------- /Lecture004 Solving Pattern Questions (Part-2)/Python Code/patern10.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | row = 1 3 | 4 | while row <= n: 5 | #Print karo space (1st triangle) 6 | space = n - row 7 | while space: 8 | print(" ", end="") 9 | space = space - 1 10 | 11 | #print 2nd triangle 12 | j = 1 13 | while j <= row: 14 | print(j, end="") 15 | j = j + 1 16 | 17 | #print 3rd triangle 18 | start = row - 1 19 | while start: 20 | print(start, end="") 21 | start = start - 1 22 | 23 | print() 24 | row = row + 1 25 | -------------------------------------------------------------------------------- /Lecture004 Solving Pattern Questions (Part-2)/Python Code/pattern1.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | for i in range(n,0,-1): 3 | for j in range(n,0,-1): 4 | print(j,end="") 5 | print("") 6 | -------------------------------------------------------------------------------- /Lecture004 Solving Pattern Questions (Part-2)/Python Code/pattern2.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | i = 1 3 | count = 1 4 | while i<=n: 5 | j=1 6 | while j<=n: 7 | print(count, end=" ") 8 | count += 1 9 | j += 1 10 | print() 11 | i = i +1 12 | -------------------------------------------------------------------------------- /Lecture004 Solving Pattern Questions (Part-2)/Python Code/pattern3.py: -------------------------------------------------------------------------------- 1 | n=int(input()) 2 | for i in range(n+1): 3 | for j in range(i): 4 | print(i,end="") 5 | print() 6 | -------------------------------------------------------------------------------- /Lecture004 Solving Pattern Questions (Part-2)/Python Code/pattern4.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | 3 | row = 1 4 | 5 | while row<=n: 6 | col = 1 7 | value = row 8 | while col <= row: 9 | print(value, end="") 10 | value = value + 1 11 | col = col + 1 12 | print() 13 | row = row + 1; 14 | 15 | -------------------------------------------------------------------------------- /Lecture004 Solving Pattern Questions (Part-2)/Python Code/pattern5.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | i = 1 3 | while i <= n: 4 | j = 1 5 | while j <= i: 6 | print(i-j+1, end=" ") 7 | j = j + 1 8 | print() 9 | i = i + 1 10 | -------------------------------------------------------------------------------- /Lecture004 Solving Pattern Questions (Part-2)/Python Code/pattern6.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | 3 | for i in range(n): 4 | for j in range(n): 5 | print(chr(65 + i), end="") 6 | print() 7 | -------------------------------------------------------------------------------- /Lecture004 Solving Pattern Questions (Part-2)/Python Code/pattern7.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | 3 | row = 1 4 | 5 | while row <= n: 6 | col = 1 7 | while col <= row: 8 | ch = (ord('A') + row + col - 2) 9 | print(chr(ch), end="") 10 | col = col + 1 11 | 12 | print() 13 | row = row + 1 14 | -------------------------------------------------------------------------------- /Lecture004 Solving Pattern Questions (Part-2)/Python Code/pattern8.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | row = 1 3 | 4 | while row <= n: 5 | col = 1 6 | start = ord('A') + n - row 7 | while col <= row: 8 | print(chr(start), end="") 9 | start = start + 1 10 | col = col + 1 11 | 12 | print() 13 | row = row + 1 14 | -------------------------------------------------------------------------------- /Lecture004 Solving Pattern Questions (Part-2)/Python Code/pattern9.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | row = 1 3 | 4 | while row <= n: 5 | # space print karlo 6 | space = n - row 7 | while space: 8 | print(" ", end="") 9 | space = space - 1 10 | 11 | #stars print karlo 12 | col = 1; 13 | while col <= row: 14 | print("*", end="") 15 | col = col + 1 16 | 17 | print() 18 | row = row + 1 19 | 20 | -------------------------------------------------------------------------------- /Lecture004 Solving Pattern Questions (Part-2)/pattern1.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin>>n; 7 | 8 | int i = 1; 9 | 10 | while(i<=n) { 11 | 12 | int j =1; 13 | while(j<=n) { 14 | cout<<n-j+1; 15 | j = j + 1; 16 | } 17 | cout<<endl; 18 | i = i + 1; 19 | } 20 | return 0; 21 | } -------------------------------------------------------------------------------- /Lecture004 Solving Pattern Questions (Part-2)/pattern1.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | i = 1 3 | while i <= n: 4 | j = 1 5 | while j <= n: 6 | print(n - j + 1, end = '') 7 | j = j + 1 8 | print() 9 | i = i + 1 10 | -------------------------------------------------------------------------------- /Lecture004 Solving Pattern Questions (Part-2)/pattern10.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | int n; 7 | cin>>n; 8 | 9 | int row = 1; 10 | 11 | while(row <= n) { 12 | 13 | //Print karo space (1st triangle) 14 | int space = n - row ; 15 | while(space) { 16 | cout<<" "; 17 | space = space -1; 18 | } 19 | 20 | //print 2nd triangle 21 | int j = 1; 22 | while(j <= row ) { 23 | cout<<j; 24 | j = j + 1; 25 | } 26 | 27 | //print 3rd triangle 28 | 29 | int start = row - 1; 30 | while(start) { 31 | cout<<start; 32 | start = start - 1; 33 | } 34 | 35 | cout<<endl; 36 | row = row + 1; 37 | } 38 | return 0; 39 | } -------------------------------------------------------------------------------- /Lecture004 Solving Pattern Questions (Part-2)/pattern2.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin>>n; 7 | 8 | int i = 1; 9 | int count = 1; 10 | while(i<=n) { 11 | 12 | int j=1; 13 | while(j<=n) { 14 | cout<<count<<" "; 15 | count = count + 1; 16 | j = j + 1; 17 | } 18 | cout<<endl; 19 | i = i +1; 20 | 21 | } 22 | return 0; 23 | } -------------------------------------------------------------------------------- /Lecture004 Solving Pattern Questions (Part-2)/pattern2.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | i = 1 3 | count = 1 4 | while i <= n: 5 | j = 1 6 | while j <= n: 7 | print(count, end = ' ') 8 | count = count + 1 9 | j = j + 1 10 | print() 11 | i = i + 1 12 | -------------------------------------------------------------------------------- /Lecture004 Solving Pattern Questions (Part-2)/pattern3.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin>>n; 7 | 8 | int row = 1; 9 | while(row <= n) { 10 | 11 | int col = 1; 12 | while(col <= row) { 13 | cout<< row; 14 | col = col + 1; 15 | } 16 | cout<<endl; 17 | row = row + 1; 18 | } 19 | return 0; 20 | } -------------------------------------------------------------------------------- /Lecture004 Solving Pattern Questions (Part-2)/pattern3.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | row = 1 3 | while row <= n: 4 | col = 1 5 | while col <= row: 6 | print(row, end = '') 7 | col = col + 1 8 | print() 9 | row = row + 1 10 | -------------------------------------------------------------------------------- /Lecture004 Solving Pattern Questions (Part-2)/pattern4.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin>>n; 7 | 8 | int row = 1; 9 | 10 | while(row<=n) { 11 | 12 | int col = 1; 13 | int value = row; 14 | while(col <= row) { 15 | cout<<value; 16 | value = value + 1; 17 | col = col + 1; 18 | } 19 | cout<<endl; 20 | row = row + 1; 21 | } 22 | return 0; 23 | } -------------------------------------------------------------------------------- /Lecture004 Solving Pattern Questions (Part-2)/pattern5.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin>>n; 7 | 8 | int i = 1; 9 | 10 | while(i <= n) { 11 | 12 | int j = 1; 13 | while(j <= i) { 14 | cout<<(i-j+1)<<" "; 15 | j = j + 1; 16 | } 17 | cout<<endl; 18 | i = i + 1; 19 | } 20 | return 0; 21 | } -------------------------------------------------------------------------------- /Lecture004 Solving Pattern Questions (Part-2)/pattern6.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | int n; 7 | cin>>n; 8 | 9 | int row = 1; 10 | while(row <= n) { 11 | 12 | int col = 1; 13 | 14 | while(col <= n) { 15 | char ch = 'A' + row - 1; 16 | cout<< ch; 17 | col = col + 1; 18 | } 19 | cout<<endl; 20 | row = row + 1; 21 | } 22 | return 0; 23 | } -------------------------------------------------------------------------------- /Lecture004 Solving Pattern Questions (Part-2)/pattern7.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | int n; 7 | cin>>n; 8 | 9 | int row = 1; 10 | 11 | while(row <= n) { 12 | 13 | int col = 1; 14 | 15 | while(col <= row) { 16 | char ch = ('A' + row + col - 2); 17 | cout<<ch; 18 | col = col + 1; 19 | } 20 | cout<<endl; 21 | row = row + 1; 22 | 23 | } 24 | return 0; 25 | } -------------------------------------------------------------------------------- /Lecture004 Solving Pattern Questions (Part-2)/pattern8.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | int n; 7 | cin>>n; 8 | 9 | int row = 1; 10 | 11 | while(row <= n) { 12 | 13 | int col = 1; 14 | char start = 'A' + n - row; 15 | while(col <= row) { 16 | cout<< start; 17 | start = start + 1; 18 | col = col + 1; 19 | } 20 | cout<< endl; 21 | row = row + 1; 22 | } 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Lecture004 Solving Pattern Questions (Part-2)/pattern9.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | int n; 7 | cin>>n; 8 | 9 | int row = 1; 10 | 11 | while(row <= n) { 12 | 13 | //space print karlo 14 | int space = n - row; 15 | while(space) { 16 | cout<<" "; 17 | space = space - 1; 18 | } 19 | 20 | //stars print karlo 21 | int col = 1; 22 | while( col <= row ) { 23 | cout<<"*"; 24 | col = col + 1; 25 | } 26 | cout<<endl; 27 | row = row + 1; 28 | } 29 | return 0; 30 | } -------------------------------------------------------------------------------- /Lecture005 Bitwise Operators, For Loops, Operator Precedence & Variable Scoping/EvenOrOdd.java: -------------------------------------------------------------------------------- 1 | package BitManipulation; 2 | import java.util.Scanner; 3 | 4 | public class EvenOrOdd { 5 | // Find if a Number is Even or Odd 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 | // taking Any Number as a Inout from the USER 10 | System.out.print(" Enter Any Number: "); 11 | int n = sc.nextInt(); 12 | /* 13 | CODE EXPLANATION : 14 | 10 --> 1010 9 --> 1001 15 | & 0001 & 0001 16 | -------- -------- 17 | 0000 -> So EVEN 0001 -> So EVEN 18 | */ 19 | if((n & 1) == 1) 20 | System.out.println(n + " is ODD "); 21 | else 22 | System.out.println(n + " is EVEN "); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Lecture005 Bitwise Operators, For Loops, Operator Precedence & Variable Scoping/FindUnique.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | public class FindUnique { 4 | // Find Unique Element in an Array that Appears only once 5 | public static void main(String [] args){ 6 | int [] arr = {1,2,1,3,4,3,2}; 7 | // Display the unique Element in the Array 8 | System.out.println(" Unique Element in the Array " + Arrays.toString(arr) + " is: " + unique(arr)); 9 | } 10 | 11 | static int unique(int [] arr){ 12 | int uniqueElement = 0; 13 | for (int element : arr) uniqueElement ^= element; 14 | return uniqueElement; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Lecture005 Bitwise Operators, For Loops, Operator Precedence & Variable Scoping/FindithBitOfNumber.java: -------------------------------------------------------------------------------- 1 | package BitManipulation; 2 | 3 | public class FindithBitOfNumber { 4 | // Find ith Bit of the given Binary Number 5 | public static void main(String [] args){ 6 | int a = 1100101; 7 | // Find 2nd Bit of the Number 8 | int n = 2; 9 | System.out.println(n + " Bit of " + a + " is: " + Integer.toBinaryString(n & (1 << n -1))); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Lecture005 Bitwise Operators, For Loops, Operator Precedence & Variable Scoping/MagicNumber.java: -------------------------------------------------------------------------------- 1 | public class MagicNumber { 2 | // Find the nth Magic Number 3 | public static void main(String [] args){ 4 | int n = 9; 5 | int ans = 0; 6 | int base = 5; 7 | while(n > 0){ 8 | // Find Last Digit of Binary number by and with 1 9 | int lastDigit = n & 1; 10 | ans = lastDigit * base; 11 | base = base * 5; 12 | n = n >> 1; 13 | } 14 | 15 | System.out.println(" Ans is: " + ans); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Lecture005 Bitwise Operators, For Loops, Operator Precedence & Variable Scoping/NegativeNumberRepresentation.java: -------------------------------------------------------------------------------- 1 | public class NegativeNumberRepresentation { 2 | // Negative Number Representation in Binary 3 | public static void main(String [] args){ 4 | int a = 10; 5 | System.out.println( - a + " in Binary: " + Integer.toBinaryString(-a)); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /Lecture005 Bitwise Operators, For Loops, Operator Precedence & Variable Scoping/NumberOfDigitsInBaseb.java: -------------------------------------------------------------------------------- 1 | public class NumberOfDigitsInBaseb { 2 | // find Number of Digits in Base b 3 | public static void main(String [] args){ 4 | int n = 8; 5 | int num = n; 6 | int count = 0; 7 | while(n > 0){ 8 | n = n >> 1; 9 | count ++; 10 | } 11 | System.out.println(" Number of Digits of " + num + " in Binary Format are: " + count); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Lecture005 Bitwise Operators, For Loops, Operator Precedence & Variable Scoping/NumberOfDigitsInBaseb2.java: -------------------------------------------------------------------------------- 1 | public class NumberOfDigitsInBaseb2 { 2 | // Number of Digits in Base b using log 3 | public static void main(String [] args){ 4 | int n = 12; 5 | int base = 2; 6 | int numberOfDigits = (int)(Math.log(n) / Math.log(base)) + 1; 7 | System.out.println(" Number of Digits in " + n + " with base " + base + " is: " + numberOfDigits); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /Lecture005 Bitwise Operators, For Loops, Operator Precedence & Variable Scoping/NumberOfSetBits.java: -------------------------------------------------------------------------------- 1 | public class NumberOfSetBits { 2 | // Find Number of Set Bits in a Number 3 | public static void main(String [] args){ 4 | int num = 9; 5 | int count = 0; 6 | while(num > 0){ 7 | if((num & 1) == 1) 8 | count ++; 9 | num = num >> 1; 10 | } 11 | System.out.println(count); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Lecture005 Bitwise Operators, For Loops, Operator Precedence & Variable Scoping/PascalsTriangleSumOfNthRow.java: -------------------------------------------------------------------------------- 1 | public class PascalsTriangleSumOfNthRow { 2 | // Find Sum of Nth Row in Pascal's Triangle 3 | public static void main(String [] args){ 4 | /* 5 | Pascal's Triangle : 6 | 1 --> Sum = 1 -> 2^0 7 | 1 1 --> Sum = 2 -> 2^1 8 | 1 2 1 --> Sum = 4 -> 2^2 9 | 1 3 3 1 --> Sum = 8 -> 2^3 10 | 11 | Formulas -> 2 ^ n - 1 12 | 1 << (n - 1) Both are Equivalent 13 | */ 14 | 15 | int row = 4; 16 | // First Way: 17 | System.out.println(" Sum of " + row + " Row is: " + Math.pow(2, row -1)); 18 | // Second Way: 19 | System.out.println(" Sum of " + row + " Row is: " + (1 << ( row - 1 ))); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Lecture005 Bitwise Operators, For Loops, Operator Precedence & Variable Scoping/Power.java: -------------------------------------------------------------------------------- 1 | public class Power { 2 | // calculate Power of a Number 3 | public static void main(String [] args){ 4 | int base = 3; 5 | int power = 2; 6 | int ans = 1; 7 | 8 | while(power > 0){ 9 | if((power & 1) == 1){ 10 | ans *= base; 11 | } 12 | power = power >> 1; 13 | base *= base; 14 | } 15 | System.out.println(ans); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Lecture005 Bitwise Operators, For Loops, Operator Precedence & Variable Scoping/PowerOf2OrNot.java: -------------------------------------------------------------------------------- 1 | public class PowerOf2OrNot { 2 | // Find Whether a Number is Power of 2 or Not 3 | public static void main(String [] args){ 4 | int num = 12; 5 | int n = num; 6 | int count = 0; 7 | while(num > 1){ 8 | num = num >> 1; 9 | if(num == 1) 10 | count++; 11 | } 12 | if(count == 1) 13 | System.out.println(" YES " + n + " is Power of Two"); 14 | else 15 | System.out.println(" NO " + n + " is not Power of Two"); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Lecture005 Bitwise Operators, For Loops, Operator Precedence & Variable Scoping/RangeXOR.java: -------------------------------------------------------------------------------- 1 | public class RangeXOR { 2 | // Range XOR 3 | public static void main(String [] args){ 4 | // Range XOR from a, b --> xor(b) ^ xor(a - 1) 5 | int a = 3; 6 | int b = 9; 7 | int ans = xor(b) ^ xor(a - 1); 8 | System.out.println(ans); 9 | 10 | // This is Only for check this will give TLE for Large Numbers 11 | int ans2 = 0; 12 | for(int i= a; i<= b; i++) 13 | ans2 ^= i; 14 | System.out.println(ans2); 15 | 16 | } 17 | 18 | // This Function will XOR from 0 to a 19 | static int xor(int a){ 20 | if(a % 4 == 0) 21 | return a; 22 | else if(a % 4 == 1) 23 | return 1; 24 | else if(a % 4 == 2) 25 | return a+1; 26 | return 0; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Lecture005 Bitwise Operators, For Loops, Operator Precedence & Variable Scoping/bitwiseOperators.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 2 | using namespace std; 3 | 4 | 5 | int main() { 6 | 7 | int a = 4; 8 | int b = 6; 9 | 10 | cout<<" a&b " << (a&b) << endl; 11 | cout<<" a|b " << (a|b) << endl; 12 | cout<<" ~a " << ~a << endl; 13 | cout<<" a^b " << (a^b) << endl; 14 | 15 | cout<< (17>>1)<<endl; 16 | cout<< (17>>2) <<endl; 17 | cout<< (19<<1) <<endl; 18 | cout<< (21<<2) <<endl; 19 | 20 | int i = 7; 21 | 22 | cout<< (++i) <<endl; 23 | // 8 24 | cout<< (i++) <<endl; 25 | // 8 , i = 9 26 | cout<< (i--) <<endl; 27 | //9 , i = 8 28 | cout<< (--i) <<endl; 29 | // 7, i =7 30 | return 0; 31 | } -------------------------------------------------------------------------------- /Lecture005 Bitwise Operators, For Loops, Operator Precedence & Variable Scoping/continue.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 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 | } 15 | return 0; 16 | } -------------------------------------------------------------------------------- /Lecture005 Bitwise Operators, For Loops, Operator Precedence & Variable Scoping/forFib.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | int n = 10; 7 | 8 | int a = 0; 9 | int b = 1; 10 | cout<<a <<" " <<b<<" "; 11 | for(int i = 1; i<=n; i++ ) { 12 | 13 | int nextNumber = a+b; 14 | cout<<nextNumber<<" "; 15 | 16 | a = b; 17 | b = nextNumber; 18 | } 19 | return 0; 20 | } -------------------------------------------------------------------------------- /Lecture005 Bitwise Operators, For Loops, Operator Precedence & Variable Scoping/forLoop.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | int n ; 7 | cout<<" enter the value of n" <<endl; 8 | // cin >> n; 9 | 10 | cout<<"printing count from 1 to n" << endl; 11 | int i = 1; 12 | for(; ; ) { 13 | if(i<=n) { 14 | cout<< i << endl; 15 | } 16 | else{ 17 | break; 18 | } 19 | i++; 20 | } 21 | 22 | 23 | for(int a = 0 , b =1, c = 2; a>=0 && b>=1 && c>=2; a--,b--, c-- ){ 24 | cout<<a <<" "<< b << " "<<c << endl; 25 | } 26 | return 0; 27 | } -------------------------------------------------------------------------------- /Lecture005 Bitwise Operators, For Loops, Operator Precedence & Variable Scoping/forPrime.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | int n ; 7 | cout<<" enter the value of n" <<endl; 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 | } 25 | else 26 | { 27 | cout<<"is a Prime Number "<<endl; 28 | } 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /Lecture005 Bitwise Operators, For Loops, Operator Precedence & Variable Scoping/forSum.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | int n ; 7 | cout<<" enter the value of n" <<endl; 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 | 17 | cout<< sum <<endl; 18 | return 0; 19 | } -------------------------------------------------------------------------------- /Lecture005 Bitwise Operators, For Loops, Operator Precedence & Variable Scoping/numberOfOneBits.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int hammingWeight(int n) { 4 | 5 | int count = 0; 6 | while(n!=0) { 7 | 8 | //checking last bit 9 | if(n&1) { 10 | count++; 11 | } 12 | n = n>>1; 13 | } 14 | return count; 15 | } 16 | }; -------------------------------------------------------------------------------- /Lecture005 Bitwise Operators, For Loops, Operator Precedence & Variable Scoping/scope.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/loveBabbar/CodeHelp-DSA-Busted-Series/b6b8347d191491c1a3a7d9c4b2c612f9987b91e3/Lecture005 Bitwise Operators, For Loops, Operator Precedence & Variable Scoping/scope.cpp -------------------------------------------------------------------------------- /Lecture005 Bitwise Operators, For Loops, Operator Precedence & Variable Scoping/subtractProdAndSum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int subtractProductAndSum(int n) { 4 | 5 | int prod = 1; 6 | int sum = 0; 7 | 8 | while(n!=0) { 9 | 10 | int digit = n%10; 11 | prod = prod * digit; 12 | sum = sum + digit; 13 | 14 | n = n/10; 15 | } 16 | 17 | int answer = prod - sum; 18 | return answer; 19 | } 20 | }; -------------------------------------------------------------------------------- /Lecture006 Binary & Decimal Number System/binToDec.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 2 | #include<math.h> 3 | using namespace std; 4 | 5 | 6 | int main() { 7 | 8 | int n; 9 | cin >> n; 10 | 11 | int i = 0, ans = 0 ; 12 | 13 | while( n != 0) { 14 | 15 | int digit = n % 10; 16 | 17 | if( digit == 1) { 18 | ans = ans + pow(2, i); 19 | } 20 | 21 | n = n/10; 22 | i++; 23 | 24 | } 25 | cout<< ans << endl; 26 | return 0; 27 | } -------------------------------------------------------------------------------- /Lecture006 Binary & Decimal Number System/decToBin.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 2 | #include<math.h> 3 | using namespace std; 4 | 5 | 6 | int main() { 7 | 8 | int n; 9 | cin >> n; 10 | 11 | 12 | int ans = 0; 13 | int i = 0; 14 | while(n != 0 ) { 15 | 16 | int bit = n & 1; 17 | 18 | ans = (bit * pow(10, i) ) + ans; 19 | 20 | n = n >> 1; 21 | i++; 22 | 23 | } 24 | 25 | cout<<" Answer is " << ans << endl; 26 | return 0; 27 | } -------------------------------------------------------------------------------- /Lecture007 LeetCode Problem Solving Session/complimentBase10Integer.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int bitwiseComplement(int n) { 4 | 5 | int m = n; 6 | int mask = 0; 7 | 8 | if(n == 0) 9 | return 1; 10 | 11 | while( m!=0) { 12 | mask = (mask << 1) | 1; 13 | m = m >> 1; 14 | } 15 | 16 | int ans = (~n) & mask; 17 | 18 | return ans; 19 | 20 | } 21 | }; -------------------------------------------------------------------------------- /Lecture007 LeetCode Problem Solving Session/powerOfTwo.cpp: -------------------------------------------------------------------------------- 1 | #include<limits.h> 2 | 3 | class Solution { 4 | public: 5 | bool isPowerOfTwo(int n) { 6 | 7 | int ans = 1; 8 | 9 | for(int i = 0; i <= 30; i++) { 10 | 11 | //cout<<" ans "<<ans <<endl; 12 | if(ans == n) 13 | { 14 | return true; 15 | } 16 | if(ans < INT_MAX/2) 17 | ans = ans * 2; 18 | 19 | } 20 | return false; 21 | } 22 | }; -------------------------------------------------------------------------------- /Lecture007 LeetCode Problem Solving Session/reverseInteger.cpp: -------------------------------------------------------------------------------- 1 | #include<limits.h> 2 | 3 | class Solution { 4 | public: 5 | int reverse(int x) { 6 | 7 | int ans = 0; 8 | while( x != 0) { 9 | 10 | int digit = x % 10; 11 | 12 | if( (ans > INT_MAX/10) || (ans < INT_MIN/10)) { 13 | return 0; 14 | } 15 | 16 | ans = (ans * 10) + digit; 17 | x = x / 10; 18 | 19 | } 20 | return ans; 21 | } 22 | }; -------------------------------------------------------------------------------- /Lecture008 Switch Statement & Functions/calculator.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | int a, b; 7 | 8 | cout <<" Enter the value of a "<<endl; 9 | cin >> a; 10 | 11 | cout<<" Enter the value of b " <<endl; 12 | cin >> b; 13 | 14 | char op; 15 | cout<<" Enter the Operation you want to perform" <<endl; 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 | } 38 | 39 | 40 | return 0; 41 | } -------------------------------------------------------------------------------- /Lecture008 Switch Statement & Functions/counting.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 2 | using namespace std; 3 | 4 | //Function Signature 5 | void printCounting(int num) { 6 | 7 | // cout << n << endl; 8 | //Function Body 9 | for(int i=1; i<=num; i++) { 10 | cout<< i << " "; 11 | } 12 | cout<<endl; 13 | } 14 | 15 | int main() { 16 | 17 | int n; 18 | cin >> n; 19 | 20 | //function Call 21 | printCounting(n); 22 | 23 | 24 | return 0; 25 | } -------------------------------------------------------------------------------- /Lecture008 Switch Statement & Functions/evenOdd.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 2 | using namespace std; 3 | 4 | 5 | //1 -> Even 6 | // 0 -> odd 7 | bool isEven(int a) { 8 | //odd 9 | if(a&1) { 10 | return 0; 11 | } 12 | else { //Even 13 | return 1; 14 | } 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 | 30 | 31 | return 0; 32 | } -------------------------------------------------------------------------------- /Lecture008 Switch Statement & Functions/functionsIntro.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | // pow(a,b) 7 | 8 | int a , b; 9 | 10 | cin >> a >> b; 11 | 12 | int ans = 1; 13 | 14 | for(int i = 1; i<=b; i++) { 15 | ans = ans * a; 16 | } 17 | 18 | cout<<" answer is " << ans <<endl; 19 | 20 | 21 | int c , d; 22 | 23 | cin >> c >> d; 24 | 25 | ans = 1; 26 | 27 | for(int i = 1; i<=d; i++) { 28 | ans = ans * c; 29 | } 30 | 31 | cout<<" answer is " << ans <<endl; 32 | 33 | int e , f; 34 | 35 | cin >> e >> f; 36 | 37 | ans = 1; 38 | 39 | for(int i = 1; i<=f; i++) { 40 | ans = ans * e; 41 | } 42 | 43 | cout<<" answer is " << ans <<endl; 44 | 45 | 46 | return 0; 47 | } -------------------------------------------------------------------------------- /Lecture008 Switch Statement & Functions/nCr.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 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 | 12 | return fact; 13 | } 14 | 15 | int nCr(int n, int r) { 16 | 17 | int num = factorial(n); 18 | 19 | int denom = factorial(r) * factorial(n-r); 20 | 21 | return num/denom; 22 | } 23 | 24 | 25 | int main( ) { 26 | 27 | int n, r; 28 | 29 | cin>> n >> r ; 30 | 31 | cout <<" Answer is " << nCr(n,r) << endl; 32 | 33 | return 0; 34 | } -------------------------------------------------------------------------------- /Lecture008 Switch Statement & Functions/passByValue.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 2 | using namespace std; 3 | 4 | void dummy(int n ) { 5 | n++; 6 | cout << " n is " << n <<endl; 7 | 8 | } 9 | 10 | int main() { 11 | 12 | int n; 13 | cin >> n; 14 | 15 | dummy(n); 16 | 17 | cout<<"number n is "<< n << endl; 18 | 19 | 20 | return 0; 21 | } -------------------------------------------------------------------------------- /Lecture008 Switch Statement & Functions/power.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 2 | using namespace std; 3 | 4 | int power(int num1, int num2) { 5 | 6 | //cout << a <<endl; 7 | 8 | int ans = 1; 9 | 10 | for(int i = 1; i <= num2; i++) { 11 | ans = ans * num1; 12 | } 13 | 14 | return ans; 15 | } 16 | int main() 17 | { 18 | int c , d; 19 | cin>> c >> d; 20 | 21 | int answer = power(c,d); 22 | cout << " answer is " << answer << endl; 23 | 24 | return 0; 25 | } -------------------------------------------------------------------------------- /Lecture008 Switch Statement & Functions/primeNumber.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 2 | using namespace std; 3 | // 0 -> Not a Prime no. 4 | // 1 -> Prime no. 5 | 6 | bool isPrime(int n ) { 7 | for(int i = 2; i < n; i++){ 8 | if(n % i == 0) { 9 | //divide hogya h , not a prime no. 10 | return 0; 11 | } 12 | } 13 | return 1; 14 | } 15 | 16 | int main() { 17 | 18 | int n ; 19 | cin >> n; 20 | if(isPrime(n)) { 21 | cout <<" is a Prime number " <<endl; 22 | } 23 | else{ 24 | cout<< "Not a prime number "<<endl; 25 | 26 | } 27 | } -------------------------------------------------------------------------------- /Lecture008 Switch Statement & Functions/switch.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | char ch = '1'; 7 | int num = 1; 8 | 9 | cout << endl; 10 | switch( 2*num ) { 11 | 12 | case 2: cout << "First" <<endl; 13 | cout << " First again " <<endl; 14 | break; 15 | 16 | case '1': switch(num) { 17 | case 1: cout<<" Value of num is "<< num <<endl; 18 | break; 19 | } 20 | break; 21 | 22 | default: cout << " It is default case" <<endl; 23 | 24 | } 25 | 26 | cout << endl; 27 | 28 | return 0; 29 | } -------------------------------------------------------------------------------- /Lecture009 Introduction to Arrays in C++/arrayScope.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 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 | } cout<<endl; 15 | 16 | 17 | cout << "Going back to main function" << endl; 18 | } 19 | 20 | 21 | int main() { 22 | 23 | int arr[3] = {1,2,3}; 24 | 25 | update(arr, 3); 26 | 27 | //printing the array 28 | cout<< endl << "Printing in main function" << endl; 29 | for(int i=0; i<3; i++) { 30 | cout << arr[i] <<" "; 31 | } 32 | cout << endl; 33 | 34 | return 0; 35 | } -------------------------------------------------------------------------------- /Lecture009 Introduction to Arrays in C++/linearSeaerch.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 2 | using namespace std; 3 | 4 | bool search(int arr[], int size, int key) { 5 | 6 | for( int i = 0; i<size; i++ ) { 7 | 8 | if( arr[i] == key) { 9 | return 1; 10 | } 11 | 12 | } 13 | return 0; 14 | } 15 | 16 | 17 | int main() { 18 | 19 | int arr[10] = { 5, 7, -2, 10, 22, -2, 0, 5, 22, 1}; 20 | 21 | cout <<" Enter the element to search for " << endl; 22 | int key; 23 | cin >> key; 24 | 25 | bool found = search(arr, 10, key); 26 | 27 | if( found ) { 28 | cout <<" Key is present "<< endl; 29 | } 30 | else{ 31 | cout <<" Key is absent " << endl; 32 | } 33 | 34 | 35 | return 0; 36 | } -------------------------------------------------------------------------------- /Lecture009 Introduction to Arrays in C++/maxMin.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 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 | 17 | //returning min value 18 | return mini; 19 | } 20 | 21 | int getMax(int num[], int n) { 22 | 23 | int maxi = INT_MIN; 24 | 25 | for(int i = 0; i<n; i++) { 26 | 27 | maxi = max(maxi, num[i]); 28 | 29 | // if(num[i] > max){ 30 | // max = num[i]; 31 | // } 32 | } 33 | 34 | //returning max value 35 | return maxi; 36 | } 37 | 38 | int main() { 39 | 40 | int size; 41 | cin >> size; 42 | 43 | int num[100]; 44 | 45 | //taking input in array 46 | for(int i = 0; i<size; i++) { 47 | cin >> num[i]; 48 | } 49 | 50 | cout << " Maximum value is " << getMax(num, size) << endl; 51 | cout << " Minimum value is " << getMin(num, size) << endl; 52 | 53 | 54 | return 0; 55 | } -------------------------------------------------------------------------------- /Lecture009 Introduction to Arrays in C++/reverse.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 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 | swap(arr[start], arr[end]); 11 | start++; 12 | end--; 13 | } 14 | } 15 | 16 | void printArray(int arr[], int n) { 17 | 18 | for(int i=0; i<n; i++) { 19 | cout << arr[i] << " "; 20 | } 21 | cout << endl; 22 | } 23 | 24 | int main() { 25 | 26 | int arr[6] = {1,4,0,5,-2,15}; 27 | int brr[5] = {2,6,3,9,4}; 28 | 29 | reverse(arr, 6); 30 | reverse(brr, 5); 31 | 32 | printArray(arr, 6); 33 | printArray(brr, 5); 34 | 35 | 36 | return 0; 37 | } -------------------------------------------------------------------------------- /Lecture010 Solving LeetCode Arrays/arrayIntersection.cpp: -------------------------------------------------------------------------------- 1 | #include<vector> 2 | using namespace std; 3 | vector<int> findArrayIntersection(vector<int> &arr1, int n, vector<int> &arr2, int m) 4 | { 5 | int i = 0, j = 0; 6 | vector<int> ans; 7 | while(i<n && j<m) { 8 | 9 | if(arr1[i]==arr2[j]) 10 | { 11 | ans.push_back(arr1[i]); 12 | i++; 13 | j++; 14 | } 15 | else if (arr1[i] < arr2[j]) { 16 | i++; 17 | } 18 | else 19 | { 20 | j++; 21 | } 22 | 23 | } 24 | return ans; 25 | } -------------------------------------------------------------------------------- /Lecture010 Solving LeetCode Arrays/duplicatesInArray.cpp: -------------------------------------------------------------------------------- 1 | #include<vector> 2 | using namespace std; 3 | int findDuplicate(vector<int> &arr) 4 | { 5 | int ans = 0; 6 | 7 | //XOR ing all array elements 8 | for(int i = 0; i<arr.size(); i++ ) { 9 | ans = ans^arr[i]; 10 | } 11 | 12 | //XOR [1, n-1] 13 | for(int i = 1; i<arr.size();i++ ) { 14 | ans = ans^i; 15 | } 16 | return ans; 17 | } 18 | -------------------------------------------------------------------------------- /Lecture010 Solving LeetCode Arrays/findUnique.cpp: -------------------------------------------------------------------------------- 1 | int findUnique(int *arr, int size) 2 | { 3 | int ans = 0; 4 | 5 | for(int i = 0; i<size; i++) { 6 | ans = ans^arr[i]; 7 | } 8 | return ans; 9 | } -------------------------------------------------------------------------------- /Lecture010 Solving LeetCode Arrays/pairSum.cpp: -------------------------------------------------------------------------------- 1 | #include<vector> 2 | using namespace std; 3 | vector<vector<int> > pairSum(vector<int> &arr, int s){ 4 | vector< vector<int> > ans; 5 | 6 | for(int i=0;i<arr.size(); i++ ) 7 | { 8 | for(int j = i+1; j<arr.size(); j++) { 9 | if(arr[i] +arr[j] == s) 10 | { 11 | vector<int> temp; 12 | temp.push_back(min(arr[i], arr[j])); 13 | temp.push_back(max(arr[i], arr[j])); 14 | ans.push_back(temp); 15 | } 16 | } 17 | } 18 | sort(ans.begin(), ans.end()); 19 | return ans; 20 | } -------------------------------------------------------------------------------- /Lecture010 Solving LeetCode Arrays/sort01.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 2 | using namespace std; 3 | 4 | void printArray(int arr[], int n) { 5 | for(int i=0; i<n; i++) { 6 | cout << arr[i] << " "; 7 | } 8 | cout << endl; 9 | } 10 | 11 | void sortOne(int arr[], int n) { 12 | 13 | int left = 0, right = n-1; 14 | 15 | while(left < right) { 16 | 17 | while(arr[left] == 0 && left < right ) { 18 | left++; 19 | } 20 | 21 | while(arr[right]==1 && left < right){ 22 | right--; 23 | } 24 | 25 | //agar yha pohoch gye ho, iska matlab 26 | //arr[left]==1 and arr[right]==0 27 | if(left<right) 28 | { 29 | swap(arr[left], arr[right]); 30 | left++; 31 | right--; 32 | } 33 | } 34 | 35 | } 36 | 37 | int main() { 38 | 39 | int arr[8] = {1,1,0,0,0,0,1,0}; 40 | 41 | sortOne(arr, 8); 42 | printArray(arr, 8); 43 | 44 | 45 | return 0; 46 | } -------------------------------------------------------------------------------- /Lecture010 Solving LeetCode Arrays/swapAlternates.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 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 | }cout<<endl; 9 | 10 | } 11 | 12 | void swapAlternate(int arr[], int size) { 13 | 14 | for(int i = 0; i<size; i+=2 ) { 15 | if(i+1 < size) { 16 | swap(arr[i], arr[i+1]); 17 | } 18 | } 19 | 20 | } 21 | 22 | int main() { 23 | 24 | int even[8] = {5,2,9,4,7,6,1,0}; 25 | int odd[5] = {11, 33, 9, 76, 43}; 26 | 27 | swapAlternate(even, 8); 28 | printArray(even, 8); 29 | 30 | cout << endl; 31 | 32 | swapAlternate(odd, 5); 33 | printArray(odd, 5); 34 | 35 | 36 | 37 | return 0; 38 | } -------------------------------------------------------------------------------- /Lecture012 Binary Search Explained in 1 Video [Theory + Code]/binarySearch.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 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-start)/2; 10 | 11 | while(start <= end) { 12 | 13 | if(arr[mid] == key) { 14 | return mid; 15 | } 16 | 17 | //go to right wala part 18 | if(key > arr[mid]) { 19 | start = mid + 1; 20 | } 21 | else{ //key < arr[mid] 22 | end = mid - 1; 23 | } 24 | 25 | mid = start + (end-start)/2; 26 | } 27 | 28 | return -1; 29 | } 30 | 31 | 32 | int main() { 33 | 34 | int even[6] = {2,4,6,8,12,18}; 35 | int odd[5] = {3, 8, 11, 14, 16}; 36 | 37 | int evenIndex = binarySearch(even, 6, 6); 38 | 39 | cout << " Index of 6 is " << evenIndex << endl; 40 | 41 | int oddIndex = binarySearch(odd, 5, 14); 42 | 43 | cout << " Index of 14 is " << oddIndex << endl; 44 | 45 | 46 | return 0; 47 | } 48 | 49 | int findPeak(int arr[], int n) { 50 | 51 | int s =0, e = n-1; 52 | int mid = s + (e-s)/2; 53 | 54 | while(s<e) { 55 | //cout<<" s " << s <<" e " << e << endl; 56 | if(arr[mid] < arr[mid+1]){ 57 | s = mid+1; 58 | } 59 | else{ 60 | e = mid; 61 | } 62 | mid = s + (e-s)/2; 63 | } 64 | return s; 65 | } -------------------------------------------------------------------------------- /Lecture012 Binary Search Explained in 1 Video [Theory + Code]/binarySearch.py: -------------------------------------------------------------------------------- 1 | def binarySearch(arr, size, key): 2 | start = 0 3 | end = size-1 4 | mid = start + (end-start)//2 5 | 6 | while start <= end: 7 | if arr[mid] == key: 8 | return mid 9 | 10 | if arr[mid] < key: 11 | start = mid + 1 12 | else: 13 | end = mid - 1 14 | 15 | mid = start + (end-start)//2 16 | return -1 17 | 18 | if __name__ == '__main__': 19 | even = [2,4,6,8,12,18] 20 | odd = [3, 8, 11, 14, 16] 21 | 22 | evenIndex = binarySearch(even, 6, 6) 23 | print("Index of 6 is: ", evenIndex) 24 | 25 | oddIndex = binarySearch(odd, 5, 14) 26 | print("Index of 14 is: ", oddIndex) 27 | -------------------------------------------------------------------------------- /Lecture013 Binary Search Interview Questions/firstLastPosition.cpp: -------------------------------------------------------------------------------- 1 | #include<vector> 2 | using namespace std; 3 | int firstOcc(vector<int>& arr, int n, int key) { 4 | 5 | int s = 0, e = n-1; 6 | int mid = s + (e-s)/2; 7 | int ans = -1; 8 | while(s<=e) { 9 | 10 | if(arr[mid] == key){ 11 | ans = mid; 12 | e = mid - 1; 13 | } 14 | else if(key > arr[mid]) {//Right me jao 15 | s = mid + 1; 16 | } 17 | else if(key < arr[mid]) {//left me jao 18 | e = mid - 1; 19 | } 20 | 21 | mid = s + (e-s)/2; 22 | } 23 | return ans; 24 | } 25 | 26 | int lastOcc(vector<int>& arr, int n, int key) { 27 | 28 | int s = 0, e = n-1; 29 | int mid = s + (e-s)/2; 30 | int ans = -1; 31 | while(s<=e) { 32 | 33 | if(arr[mid] == key){ 34 | ans = mid; 35 | s = mid + 1; 36 | } 37 | else if(key > arr[mid]) {//Right me jao 38 | s = mid + 1; 39 | } 40 | else if(key < arr[mid]) {//left me jao 41 | e = mid - 1; 42 | } 43 | 44 | mid = s + (e-s)/2; 45 | } 46 | return ans; 47 | } 48 | 49 | pair<int, int> firstAndLastPosition(vector<int>& arr, int n, int k) 50 | { 51 | pair<int,int> p; 52 | p.first = firstOcc(arr, n, k); 53 | p.second = lastOcc(arr, n, k); 54 | 55 | return p; 56 | } -------------------------------------------------------------------------------- /Lecture013 Binary Search Interview Questions/peakInMountain.cpp: -------------------------------------------------------------------------------- 1 | #include<vector> 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | 7 | int find_pivot(vector<int> v) { 8 | int s = 0, e = v.size() - 1; 9 | int mid=(s + e) / 2; 10 | while (s < e) 11 | { 12 | if(v[mid] < v[mid+1]) 13 | s=mid+1; 14 | else 15 | e = mid; 16 | 17 | mid = (s + e) / 2; 18 | } 19 | return s; 20 | } 21 | int peakIndexInMountainArray(vector<int>& arr) { 22 | return find_pivot(arr); 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /Lecture014 Binary Search Interview Questions/pivot.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 2 | using namespace std; 3 | 4 | int getPivot(int arr[], int n) { 5 | 6 | int s = 0; 7 | int e = n-1; 8 | int mid = s + (e-s)/2; 9 | 10 | while(s<e) { 11 | 12 | if(arr[mid] >= arr[0]) 13 | { 14 | s = mid+1; 15 | } 16 | else{ 17 | e = mid; 18 | } 19 | mid = s + (e-s)/2; 20 | } 21 | return s; 22 | } 23 | 24 | int main() { 25 | int arr[5] = {1, 3, 8, 10, 17}; 26 | cout << "Pivot is " << getPivot(arr, 5) << endl; 27 | } -------------------------------------------------------------------------------- /Lecture014 Binary Search Interview Questions/searchRotatedArray.cpp: -------------------------------------------------------------------------------- 1 | #include<vector> 2 | using namespace std; 3 | 4 | int getPivot(vector<int>& arr, int n) { 5 | 6 | int s = 0; 7 | int e = n-1; 8 | int mid = s + (e-s)/2; 9 | 10 | while(s<e) { 11 | 12 | if(arr[mid] >= arr[0]) 13 | { 14 | s = mid+1; 15 | } 16 | else{ 17 | e = mid; 18 | } 19 | mid = s + (e-s)/2; 20 | } 21 | return s; 22 | } 23 | 24 | int binarySearch(vector<int>& arr, int s, int e, int key) { 25 | 26 | int start = s; 27 | int end = e; 28 | 29 | int mid = start + (end-start)/2; 30 | 31 | while(start <= end) { 32 | 33 | if(arr[mid] == key) { 34 | return mid; 35 | } 36 | 37 | //go to right wala part 38 | if(key > arr[mid]) { 39 | start = mid + 1; 40 | } 41 | else{ //key < arr[mid] 42 | end = mid - 1; 43 | } 44 | 45 | mid = start + (end-start)/2; 46 | } 47 | 48 | return -1; 49 | } 50 | 51 | 52 | 53 | int findPosition(vector<int>& arr, int n, int k) 54 | { 55 | int pivot = getPivot(arr, n); 56 | if( k >= arr[pivot] && k <= arr[n-1]) 57 | {//BS on second line 58 | return binarySearch(arr, pivot, n-1, k); 59 | } 60 | else 61 | {//BS on first line 62 | return binarySearch(arr, 0, pivot - 1, k); 63 | } 64 | 65 | } 66 | 67 | -------------------------------------------------------------------------------- /Lecture014 Binary Search Interview Questions/sqrt.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 2 | using namespace std; 3 | 4 | long long int sqrtInteger(int n) { 5 | 6 | int s = 0; 7 | int e = n; 8 | long long int mid = s + (e-s)/2; 9 | 10 | long long int ans = -1; 11 | while(s<=e) { 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 | s = mid+1; 21 | } 22 | else 23 | { 24 | e = mid - 1; 25 | } 26 | mid = s + (e-s)/2; 27 | } 28 | return ans; 29 | } 30 | 31 | double morePrecision(int n, int precision, int tempSol) { 32 | 33 | double factor = 1; 34 | double ans = tempSol; 35 | 36 | for(int i=0; i<precision; i++) { 37 | factor = factor/10; 38 | 39 | for(double j=ans; j*j<n; j= j+factor ){ 40 | ans = j; 41 | } 42 | } 43 | return ans; 44 | } 45 | 46 | int main() { 47 | int n; 48 | cout <<" Enter the number " << endl; 49 | cin >> n; 50 | 51 | int tempSol = sqrtInteger(n); 52 | cout <<" Answer is " << morePrecision(n, 3, tempSol) << endl; 53 | 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /Lecture015 Binary Search Advanced Problems/aggressiveCows.cpp: -------------------------------------------------------------------------------- 1 | bool isPossible(vector<int> &stalls, int k, int mid, int n) { 2 | 3 | int cowCount = 1; 4 | int lastPos = stalls[0]; 5 | 6 | for(int i=0; i<n; i++ ){ 7 | 8 | if(stalls[i]-lastPos >= mid){ 9 | cowCount++; 10 | if(cowCount==k) 11 | { 12 | return true; 13 | } 14 | lastPos = stalls[i]; 15 | } 16 | } 17 | return false; 18 | } 19 | 20 | int aggressiveCows(vector<int> &stalls, int k) 21 | { 22 | sort(stalls.begin(), stalls.end()); 23 | int s = 0; 24 | int n = stalls.size(); 25 | int e=stalls[n-1]; 26 | int ans = -1; 27 | int mid = s + (e-s)/2; 28 | 29 | while(s<=e) { 30 | if(isPossible(stalls, k, mid, n)) { 31 | ans = mid; 32 | s = mid + 1; 33 | } 34 | else 35 | { 36 | e = mid - 1; 37 | } 38 | mid = s + (e-s)/2; 39 | } 40 | return ans; 41 | } 42 | -------------------------------------------------------------------------------- /Lecture015 Binary Search Advanced Problems/bookAllocationProblem.cpp: -------------------------------------------------------------------------------- 1 | #include<vector> 2 | using namespace std; 3 | 4 | bool isPossible(vector<int> arr, int n, int m, int mid) { 5 | int studentCount = 1; 6 | int pageSum = 0; 7 | //cout << "checking for mid "<< mid <<endl; 8 | 9 | for(int i = 0; i<n; i++) { 10 | if(pageSum + arr[i] <= mid) { 11 | pageSum += arr[i]; 12 | } 13 | else 14 | { 15 | studentCount++; 16 | if(studentCount > m || arr[i] > mid ) { 17 | return false; 18 | } 19 | pageSum = arr[i]; 20 | } 21 | if(studentCount > m) { 22 | return false; 23 | } 24 | //cout << " for i " << i << " Student "<< studentCount << " pageSum " << pageSum << endl; 25 | } 26 | return true; 27 | } 28 | 29 | int allocateBooks(vector<int> arr, int n, int m) { 30 | int s = 0; 31 | int sum = 0; 32 | 33 | for(int i = 0; i<n; i++) { 34 | sum += arr[i]; 35 | } 36 | int e = sum; 37 | int ans = -1; 38 | int mid = s + (e-s)/2; 39 | 40 | while(s<=e) 41 | { 42 | if(isPossible(arr,n,m,mid)) { 43 | //cout<<" Mid returned TRUE" << endl; 44 | ans = mid; 45 | e = mid - 1; 46 | } 47 | else 48 | { 49 | s = mid + 1; 50 | } 51 | mid = s + (e-s)/2; 52 | } 53 | return ans; 54 | } -------------------------------------------------------------------------------- /Lecture016 Selection Sort/SelectionSort.py: -------------------------------------------------------------------------------- 1 | def selectionSort(arr, n): 2 | for i in range(n-1): 3 | minIndex = i 4 | 5 | for j in range (i+1,n): 6 | if arr[j] < arr[minIndex]: 7 | minIndex = j 8 | 9 | arr[minIndex] , arr[i]=arr[i] , arr[minIndex] 10 | 11 | return arr 12 | 13 | if __name__ == "__main__": 14 | n=5 # size of arr 15 | arr=[5,9,2,4,3] #take input in list 16 | 17 | ans = selectionSort (arr,n) 18 | print(ans) 19 | -------------------------------------------------------------------------------- /Lecture016 Selection Sort/selectionSort.cpp: -------------------------------------------------------------------------------- 1 | #include<vector> 2 | using namespace std; 3 | void selectionSort(vector<int>& arr, int n) 4 | { 5 | for(int i = 0; i < n-1; i++ ) { 6 | int minIndex = i; 7 | 8 | for(int j = i+1; j<n; j++) { 9 | 10 | if(arr[j] < arr[minIndex]) 11 | minIndex = j; 12 | 13 | } 14 | swap(arr[minIndex], arr[i]); 15 | } 16 | } -------------------------------------------------------------------------------- /Lecture017 Bubble Sort/BubbleSort.java: -------------------------------------------------------------------------------- 1 | 2 | 3 | import java.util.Scanner; 4 | import java.util.Arrays; 5 | public class BubbleSort { 6 | // Bubble Sort -> Also Known as Sinking Sort and Exchange Sort 7 | public static void main(String [] args){ 8 | /* 9 | */ 10 | 11 | // Creating Scanner Object for taking inputs from the USER 12 | Scanner sc = new Scanner(System.in); 13 | // Take the SIZE of 1D Array from the USER 14 | System.out.print(" Enter SIZE of 1D Array: "); 15 | int n = sc.nextInt(); 16 | // Create Array of Required SIZE 17 | int [] arr = new int[n]; 18 | //Fill the Array by taking n Inputs from the USER 19 | System.out.print(" Enter " + n + " Elements: "); 20 | for(int i=0; i<arr.length; i++) 21 | arr[i] = sc.nextInt(); 22 | // Display Array Before Sorting 23 | System.out.println(" Array Before Sorting is: " + Arrays.toString(arr)); 24 | // Apply Bubble Sort on the Given Array 25 | bubbleSort(arr); 26 | // Display Array After Sorting 27 | System.out.println(" Array After Sorting is: " + Arrays.toString(arr)); 28 | } 29 | // Method of Bubble Sort 30 | static void bubbleSort(int [] arr){ 31 | for(int i=0; i<arr.length-1 ; i++){ 32 | boolean isSorted = true; 33 | for(int j = 0; j<arr.length-i-1; j++){ 34 | if(arr[j] > arr[j+1]){ 35 | int temp = arr[j]; 36 | arr[j] = arr[j+1]; 37 | arr[j+1] = temp; 38 | isSorted = false; 39 | } 40 | } 41 | if(isSorted) 42 | break; 43 | 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Lecture017 Bubble Sort/bubbleSort.cpp: -------------------------------------------------------------------------------- 1 | #include<vector> 2 | using namespace std; 3 | void bubbleSort(vector<int>& arr, int n) 4 | { 5 | for(int i = 1; i<n; i++) { 6 | //for round 1 to n-1 7 | bool swapped = false; 8 | 9 | for(int j =0; j<n-i; j++) { 10 | 11 | //process element till n-i th index 12 | if(arr[j] > arr[j+1]) { 13 | swap(arr[j], arr[j+1]); 14 | swapped = true; 15 | } 16 | 17 | } 18 | 19 | if(swapped == false) { 20 | //already sorted 21 | break; 22 | } 23 | 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Lecture017 Bubble Sort/bubbleSort.py: -------------------------------------------------------------------------------- 1 | def bubbleSort(arr, n): 2 | for i in range(1, n): 3 | 4 | for j in range(n-1): 5 | if arr[j] > arr[j+1]: 6 | arr[j], arr[j+1] = arr[j+1], arr[j] 7 | 8 | 9 | if __name__ == '__main__': 10 | arr = [2, 5, 3, 6, 1, 7, 4] 11 | n = len(arr) 12 | 13 | print("Before sorting") 14 | print(arr) 15 | 16 | bubbleSort(arr, n) 17 | 18 | print("After sorting") 19 | print(arr) 20 | -------------------------------------------------------------------------------- /Lecture018 Insertion Sort/insertionSort.cpp: -------------------------------------------------------------------------------- 1 | #include<vector> 2 | using namespace std; 3 | 4 | void insertionSort(int n, vector<int> &arr){ 5 | 6 | for(int i = 1; i<n; i++) { 7 | int temp = arr[i]; 8 | int j = i-1; 9 | for(; j>=0; j--) { 10 | 11 | if(arr[j] > temp) { 12 | //shift 13 | arr[j+1] = arr[j]; 14 | } 15 | else { // ruk jao 16 | break; 17 | } 18 | 19 | } 20 | //copy temp value 21 | arr[j+1] = temp; 22 | } 23 | } -------------------------------------------------------------------------------- /Lecture018 Insertion Sort/insertion_sort.py: -------------------------------------------------------------------------------- 1 | def insertionSort(a): 2 | # storing the length of the given array or list in a variable. 3 | n = len(a) 4 | 5 | for i in range(1 , n): 6 | # current is storing the i-th element of the list and 7 | # j is pointing to the index before the current index 8 | current = a[i] 9 | j = i-1 10 | 11 | # checking j's boundary condition and 12 | # while the current elemet is smaller than a[j], keep shifting j 13 | # at last place the current element to its correct positon 14 | while j > -1 and current < a[j]: 15 | a[j + 1] = a[j] 16 | j = j - 1 17 | 18 | a[j + 1] = current 19 | 20 | 21 | a = [64, 34, 25, 12, 22, 11, 90] 22 | insertionSort(a) 23 | print("Sorted array : ") 24 | for i in a: 25 | print(i) 26 | -------------------------------------------------------------------------------- /Lecture020 ArrayQuestions/MergeSortedArr1Java.java: -------------------------------------------------------------------------------- 1 | class MergeSortedArr1Java { 2 | public static void main(String[] args) { 3 | int arr1[] = new int[] {1,3,5,7,9,0,0,0}; 4 | int arr2[] = new int[] {2,4,6}; 5 | int m = 5; 6 | int n = 3; 7 | mergeSortedArrLeetcode(arr1, m, arr2, n); 8 | 9 | int arr3[] = new int[] {1,3,5,7,9}; 10 | int arr4[] = new int[8]; 11 | mergeSortedArr(arr3, 5, arr2, 3, arr4); 12 | } 13 | 14 | public static void mergeSortedArrLeetcode(int arr1[], int m, int arr2[], int n) { 15 | int i = m-1; 16 | int j = n-1; 17 | int k = arr1.length-1; 18 | 19 | while(i >=0 && j >= 0) { 20 | if(arr1[i] > arr2[j]) { 21 | arr1[k--] = arr1[i--]; 22 | } else { 23 | arr1[k--] = arr2[j--]; 24 | } 25 | } 26 | 27 | while(j >= 0) { 28 | arr1[k--] = arr2[j--]; 29 | } 30 | 31 | //printing array 32 | for(int ele : arr1) { 33 | System.out.print(ele+" "); 34 | } 35 | } 36 | 37 | public static void mergeSortedArr(int arr1[], int n, int arr2[], int m, int arr3[]) { 38 | int i = 0, j = 0; 39 | int k = 0; 40 | while( i<n && j<m) { 41 | if(arr1[i] < arr2[j]){ 42 | arr3[k++] = arr1[i++]; 43 | } 44 | else{ 45 | arr3[k++] = arr2[j++]; 46 | } 47 | } 48 | 49 | //copy first array k element ko 50 | while(i<n) { 51 | arr3[k++] = arr1[i++]; 52 | } 53 | 54 | //copy kardo second array k remaining element ko 55 | while(j<m) { 56 | arr3[k++] = arr2[j++]; 57 | } 58 | 59 | System.out.println(); 60 | System.out.println("from mergeSortedArr method"); 61 | //printing array 62 | for(int ele : arr3) { 63 | System.out.print(ele+" "); 64 | } 65 | } 66 | } -------------------------------------------------------------------------------- /Lecture020 ArrayQuestions/ReverseArrJava.java: -------------------------------------------------------------------------------- 1 | class ReverseArrJava { 2 | public static void main(String[] args) { 3 | int arr[] = new int[] { 11, 7, 3, 12, 4 }; 4 | reverseArr(arr); 5 | } 6 | 7 | public static void reverseArr(int arr[]) { 8 | int start = 0; 9 | int end = arr.length-1; 10 | 11 | while(start <= end) { 12 | int temp = arr[start]; 13 | arr[start] = arr[end]; 14 | arr[end] = temp; 15 | start++; 16 | end--; 17 | } 18 | 19 | //printing array 20 | for(int i : arr) { 21 | System.out.print(i+" "); 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /Lecture020 ArrayQuestions/mergeSortedArray1.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 2 | #include<vector> 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 | if(arr1[i] < arr2[j]){ 11 | arr3[k++] = arr1[i++]; 12 | } 13 | else{ 14 | arr3[k++] = arr2[j++]; 15 | } 16 | } 17 | 18 | //copy first array k element ko 19 | while(i<n) { 20 | arr3[k++] = arr1[i++]; 21 | } 22 | 23 | //copy kardo second array k remaining element ko 24 | while(j<m) { 25 | arr2[k++] = arr2[j++]; 26 | } 27 | } 28 | 29 | void print(int ans[], int n) { 30 | for(int i=0; i<n; i++) { 31 | cout<< ans[i] <<" "; 32 | } 33 | cout << endl; 34 | } 35 | 36 | int main() { 37 | 38 | int arr1[5] = {1,3,5,7,9}; 39 | int arr2[3] = {2,4,6}; 40 | 41 | int arr3[8] = {0}; 42 | 43 | merge(arr1, 5, arr2, 3, arr3); 44 | 45 | print(arr3, 8); 46 | 47 | 48 | return 0; 49 | } -------------------------------------------------------------------------------- /Lecture020 ArrayQuestions/reverseArray.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 2 | #include<vector> 3 | using namespace std; 4 | 5 | vector<int> reverse(vector<int> 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 | 15 | return v; 16 | 17 | } 18 | 19 | void print(vector<int> v) { 20 | 21 | for(int i=0; i<v.size(); i++) { 22 | cout << v[i] << " "; 23 | } 24 | cout << endl; 25 | } 26 | 27 | int main() { 28 | 29 | vector<int> v ; 30 | 31 | v.push_back(11); 32 | v.push_back(7); 33 | v.push_back(3); 34 | v.push_back(12); 35 | v.push_back(4); 36 | 37 | vector<int> ans = reverse(v); 38 | 39 | cout << "Printing reverse Array" <<endl; 40 | print(ans); 41 | 42 | 43 | return 0; 44 | } -------------------------------------------------------------------------------- /Lecture020 ArrayQuestions/reverseArray.py: -------------------------------------------------------------------------------- 1 | 2 | def reverse(v): 3 | s = 0 4 | e = len(v)-1 5 | 6 | while s <= e: 7 | v[s], v[e] = v[e], v[s] 8 | s += 1 9 | e -= 1 10 | 11 | return v 12 | 13 | 14 | if __name__ == '__main__': 15 | v = [] 16 | 17 | v.append(11) 18 | v.append(7) 19 | v.append(3) 20 | v.append(12) 21 | v.append(4) 22 | 23 | print(v) 24 | 25 | ans = reverse(v) 26 | print("Reverse Array") 27 | print(ans) 28 | -------------------------------------------------------------------------------- /Lecture021 Solving Array Questions 2/addArray.cpp: -------------------------------------------------------------------------------- 1 | vector<int> reverse(vector<int> v) { 2 | 3 | int s = 0; 4 | int e = v.size()-1; 5 | 6 | while(s<e) 7 | { 8 | swap(v[s++], v[e--]); 9 | } 10 | return v; 11 | } 12 | 13 | vector<int> findArraySum(vector<int>&a, int n, vector<int>&b, int m) { 14 | 15 | int i = n-1; 16 | int j = m-1; 17 | vector<int> ans; 18 | int carry = 0; 19 | 20 | while(i>=0 && j>=0) { 21 | int val1 = a[i]; 22 | int val2 = b[j]; 23 | 24 | int sum = val1 + val2 + carry; 25 | 26 | carry = sum/10; 27 | sum = sum%10; 28 | ans.push_back(sum); 29 | i--; 30 | j--; 31 | } 32 | 33 | // first case 34 | while(i>=0) { 35 | int sum = a[i] + carry; 36 | carry = sum/10; 37 | sum = sum%10; 38 | ans.push_back(sum); 39 | i--; 40 | } 41 | 42 | // second case 43 | while(j>=0) { 44 | int sum = b[j] + carry; 45 | carry = sum/10; 46 | int value = sum%10; 47 | ans.push_back(value); 48 | j--; 49 | } 50 | 51 | // second case 52 | while(carry != 0) { 53 | int sum = carry; 54 | carry = sum/10; 55 | sum = sum%10; 56 | ans.push_back(sum); 57 | } 58 | 59 | //print(ans); 60 | return reverse(ans); 61 | } -------------------------------------------------------------------------------- /Lecture021 Solving Array Questions 2/checkSortedRotatedArray.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool check(vector<int>& nums) { 4 | int cnt = 0; 5 | for(int i=1;i<nums.size();i++) 6 | { 7 | if(nums[i] < nums[i-1]) 8 | cnt++; 9 | } 10 | if(nums[nums.size()-1] > nums[0]) 11 | cnt++; 12 | 13 | return cnt<=1; 14 | } 15 | }; -------------------------------------------------------------------------------- /Lecture021 Solving Array Questions 2/rotateArray.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void rotate(vector<int>& nums, int k) { 4 | 5 | // just to practice the modulus approach 6 | int n = nums.size(); 7 | vector<int> temp(n); 8 | 9 | 10 | for(int i =0;i<n;i++) { 11 | 12 | temp[(i+k)%n] = nums[i]; 13 | 14 | } 15 | 16 | 17 | for(int i=0;i<nums.size();i++) 18 | { 19 | nums[i] = temp[i]; 20 | } 21 | 22 | } 23 | }; -------------------------------------------------------------------------------- /Lecture022 Strings/removeOccurrences.cpp: -------------------------------------------------------------------------------- 1 | #include<string> 2 | #include<iostream> 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 | }; -------------------------------------------------------------------------------- /Lecture022 Strings/replaceSpaces.cpp: -------------------------------------------------------------------------------- 1 | #include<string> 2 | #include<iostream> 3 | using namespace std; 4 | string replaceSpaces(string &str){ 5 | string temp = ""; 6 | 7 | for(int i=0; i<str.length(); i++) { 8 | if(str[i] == ' '){ 9 | temp.push_back('@'); 10 | temp.push_back('4'); 11 | temp.push_back('0'); 12 | } 13 | else 14 | { 15 | temp.push_back(str[i]); 16 | } 17 | } 18 | return temp; 19 | } -------------------------------------------------------------------------------- /Lecture022 Strings/reverseString.cpp: -------------------------------------------------------------------------------- 1 | #include<string> 2 | #include<iostream> 3 | #include<vector> 4 | using namespace std; 5 | class Solution { 6 | public: 7 | 8 | void reverseString(vector<char>& s) { 9 | int st=0; 10 | int e = s.size()-1; 11 | 12 | while(st<e) { 13 | swap(s[st++], s[e--]); 14 | } 15 | } 16 | }; -------------------------------------------------------------------------------- /Lecture022 Strings/validPalindrome.cpp: -------------------------------------------------------------------------------- 1 | #include<string> 2 | #include<iostream> 3 | #include<vector> 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<s.length(); j++) { 46 | if(valid(s[j])) { 47 | temp.push_back(s[j]); 48 | } 49 | } 50 | 51 | //lowercase me kardo 52 | for(int j=0; j<temp.length(); j++) { 53 | temp[j] = toLowerCase(temp[j]); 54 | } 55 | 56 | //check palindrome 57 | return checkPalindrome(temp); 58 | 59 | } 60 | }; -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /Lecture023 2D arrays/SpiralPrint.java: -------------------------------------------------------------------------------- 1 | class SpiralPrint { 2 | public List<Integer> spiralOrder(int[][] matrix) { 3 | List<Integer> 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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /Lecture023 2D arrays/search2DMatrix1.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 2 | #include<vector> 3 | using namespace std; 4 | class Solution { 5 | public: 6 | bool searchMatrix(vector<vector<int> >& 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 | }; -------------------------------------------------------------------------------- /Lecture023 2D arrays/search2DMatrix2.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 2 | #include<vector> 3 | using namespace std; 4 | class Solution { 5 | public: 6 | bool searchMatrix(vector<vector<int> >& 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<iostream> 2 | #include<vector> 3 | using namespace std; 4 | vector<int> wavePrint(vector<vector<int> > arr, int nRows, int mCols) 5 | { 6 | vector<int> ans; 7 | 8 | for(int col=0; col<mCols; col++) { 9 | 10 | if( col&1 ) { 11 | //odd Index -> 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<nRows; row++) { 22 | //cout << arr[row][col] << " "; 23 | ans.push_back(arr[row][col]); 24 | } 25 | } 26 | } 27 | return ans; 28 | } -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /Lecture024 Maths for DSA/fastExpo.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 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 | } -------------------------------------------------------------------------------- /Lecture024 Maths for DSA/gcd.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 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 | } -------------------------------------------------------------------------------- /Lecture024 Maths for DSA/primeNumberBrute.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 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; i++) 10 | { 11 | if(n%i==0) 12 | { 13 | return false; 14 | } 15 | } 16 | return true; 17 | } 18 | 19 | int main() { 20 | 21 | int n; 22 | cin >> 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 | } -------------------------------------------------------------------------------- /Lecture024 Maths for DSA/sieve.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 2 | #include<vector> 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 | } -------------------------------------------------------------------------------- /Lecture025 Intro to Pointers in C++/intro.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 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 | 30 | 31 | return 0; 32 | } -------------------------------------------------------------------------------- /Lecture025 Intro to Pointers in C++/play.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 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 | 53 | return 0; 54 | } -------------------------------------------------------------------------------- /Lecture026 Pointers with Arrays and Functions/pointer_array.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 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 | 24 | int temp[10] = {1,2}; 25 | cout << sizeof(temp) << endl; 26 | cout << " 1st " << sizeof(*temp) << endl; 27 | cout << " 2nd " << sizeof(&temp) << endl; 28 | 29 | int *ptr = &temp[0]; 30 | cout << sizeof(ptr) << endl ; 31 | cout << sizeof(*ptr) << endl ; 32 | cout << sizeof(&ptr) << endl ; 33 | 34 | 35 | 36 | int a[20] = {1,2,3,5}; 37 | //cout << " ->" << &a[0] << endl; 38 | // cout << &a << endl; 39 | //cout << a << endl; 40 | 41 | int *p = &a[0]; 42 | //cout << p << endl; 43 | //cout << *p << endl; 44 | cout << "-> " << &p << endl; 45 | 46 | */ 47 | 48 | int arr[10]; 49 | 50 | //ERROR 51 | //arr = arr+1; 52 | 53 | int *ptr = &arr[0]; 54 | cout << ptr << endl; 55 | ptr = ptr + 1; 56 | cout << ptr << endl; 57 | 58 | 59 | 60 | 61 | 62 | 63 | return 0; 64 | } -------------------------------------------------------------------------------- /Lecture026 Pointers with Arrays and Functions/pointer_characterArray.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 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 | 10 | cout << arr << endl; 11 | //attention here 12 | cout << ch << endl; 13 | 14 | char *c = &ch[0]; 15 | //prints entire string 16 | cout << c << endl; 17 | 18 | char temp = 'z'; 19 | char *p = &temp; 20 | 21 | cout << p << endl; 22 | 23 | 24 | return 0; 25 | } -------------------------------------------------------------------------------- /Lecture026 Pointers with Arrays and Functions/pointer_functions.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 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 <<endl; 14 | *p = *p + 1; 15 | 16 | } 17 | 18 | int getSum(int *arr, int n) { 19 | 20 | cout << endl << "Size : " << sizeof(arr) << endl; 21 | 22 | int sum = 0; 23 | for(int i=0;i<n;i++) { 24 | sum += arr[i]; 25 | } 26 | return sum; 27 | } 28 | 29 | int main() { 30 | /* 31 | int value = 5; 32 | int *p = &value; 33 | 34 | //print(p); 35 | cout <<" Before " << *p << endl; 36 | update(p); 37 | cout <<" After " << *p << endl; 38 | */ 39 | 40 | int arr[6] = {1,2,3,4,5,8}; 41 | 42 | cout << "Sum is " << getSum(arr+3 ,3) << endl ; 43 | 44 | return 0; 45 | } -------------------------------------------------------------------------------- /Lecture027 Double Pointers and MCQs/doublePointer.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 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 | } -------------------------------------------------------------------------------- /Lecture028 Dynamic Allocation (Part-1)/ReferenceVariable.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 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 | } -------------------------------------------------------------------------------- /Lecture028 Dynamic Allocation (Part-1)/dynamicArray.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 2 | using namespace std; 3 | 4 | int getSum(int *arr, int n) { 5 | 6 | int sum = 0; 7 | for(int i=0; i<n; i++) { 8 | sum += arr[i]; 9 | } 10 | return sum; 11 | } 12 | 13 | int main() { 14 | 15 | int n; 16 | cin >> n; 17 | 18 | //variable size array 19 | int* arr = new int[n]; 20 | 21 | //takign inputn in aray 22 | for(int i=0; i<n; i++) { 23 | cin >> 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 | } -------------------------------------------------------------------------------- /Lecture029 Dynamic Allocation (Part-2)/2dArray.cpp: -------------------------------------------------------------------------------- 1 | #include <iostream> 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<row; i++) { 15 | arr[i] = new int[col]; 16 | } 17 | 18 | //taking input 19 | for(int i=0; i<row; i++) { 20 | for(int j=0; j<col; j++) { 21 | cin >> arr[i][j]; 22 | } 23 | } 24 | 25 | //taking output 26 | cout << endl; 27 | for(int i=0; i<row; i++) { 28 | for(int j=0; j<col; j++) { 29 | cout << arr[i][j] << " "; 30 | } cout << endl; 31 | } 32 | 33 | 34 | //releasing memory 35 | for(int i=0; i<row; i++) { 36 | delete [] arr[i]; 37 | } 38 | 39 | delete []arr; 40 | 41 | //how to create a 2D array dynamically 42 | //input/Output 43 | //memory free kaise karani hai 44 | 45 | return 0; 46 | } -------------------------------------------------------------------------------- /Lecture030 Macros, Gloab Variable etc/counting.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 2 | using namespace std; 3 | 4 | void print(int n) { 5 | //base case 6 | if(n == 0) { 7 | return ; 8 | } 9 | 10 | //Recursive relation 11 | print(n-1); 12 | 13 | cout << n << endl; 14 | } 15 | 16 | int main() { 17 | 18 | int n; 19 | cin >> n; 20 | cout << endl; 21 | print(n); 22 | 23 | 24 | return 0; 25 | } -------------------------------------------------------------------------------- /Lecture030 Macros, Gloab Variable etc/defaultArgs.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 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 | } 11 | 12 | int main() { 13 | 14 | int arr[5] = {1,4,7,8,9}; 15 | int size = 5; 16 | 17 | print(arr, size); 18 | cout << endl; 19 | print(arr, size, 2); 20 | 21 | 22 | return 0; 23 | } -------------------------------------------------------------------------------- /Lecture030 Macros, Gloab Variable etc/globalVariable.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 2 | using namespace std; 3 | 4 | int score = 15; 5 | 6 | 7 | void a(int& i) { 8 | 9 | cout << score << " in a" << endl; 10 | score++; 11 | char ch = 'a'; 12 | cout << i << endl; 13 | } 14 | 15 | void b(int& i) { 16 | cout << score << " in b" << endl; 17 | cout << i << endl; 18 | } 19 | 20 | int main() { 21 | cout << score << " in main" << endl; 22 | int i = 5; 23 | a(i); 24 | b(i); 25 | 26 | return 0; 27 | } -------------------------------------------------------------------------------- /Lecture030 Macros, Gloab Variable etc/inlineFunctions.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 2 | using namespace std; 3 | 4 | 5 | 6 | inline int getMax(int& a, int& b) { 7 | return (a>b) ? a : b; 8 | } 9 | 10 | int main() { 11 | 12 | int a = 1, b = 2; 13 | int ans = 0; 14 | 15 | ans = getMax(a,b); 16 | cout << ans << endl; 17 | 18 | a = a + 3; 19 | b = b + 1; 20 | 21 | ans = getMax(a,b); 22 | cout << ans << endl; 23 | 24 | 25 | 26 | return 0; 27 | } -------------------------------------------------------------------------------- /Lecture030 Macros, Gloab Variable etc/macros.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 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 | } -------------------------------------------------------------------------------- /Lecture031 Recursion Day1/Factorial.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Factorial { 4 | 5 | public static int factorial(int n) { 6 | if(n==0) { 7 | return 1; 8 | } 9 | return (n * factorial(n-1)); 10 | } 11 | 12 | public static void main(String[] args) { 13 | Scanner sc = new Scanner(System.in); 14 | int n = sc.nextInt(); 15 | int fact = Factorial.factorial(n); 16 | System.out.println("Factorial of "+n+" is : "+fact); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Lecture031 Recursion Day1/Power.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Power { 4 | 5 | public static int power(int n) { 6 | if(n==0) { 7 | return 1; 8 | } 9 | return (2*power(n-1)); 10 | } 11 | 12 | public static void main(String[] args) { 13 | Scanner sc = new Scanner(System.in); 14 | int n = sc.nextInt(); 15 | int power = Power.power(n); 16 | System.out.println("2 raise to the power "+n+" is : "+power); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Lecture031 Recursion Day1/factroial.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 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 | 25 | return 0; 26 | } -------------------------------------------------------------------------------- /Lecture031 Recursion Day1/power.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 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 | 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Lecture031 Recursion Day1/power.py: -------------------------------------------------------------------------------- 1 | def power(n): 2 | 3 | #base case 4 | if(n == 0) : 5 | return 1 6 | 7 | #recursive relation 8 | return 2 * power(n-1) 9 | 10 | 11 | if __name__ == "__main__": 12 | n=int(input()) 13 | ans = power(n) 14 | print(ans) 15 | -------------------------------------------------------------------------------- /Lecture031 Recursion Day1/tempCodeRunnerFile.cpp: -------------------------------------------------------------------------------- 1 | if(n==0) 2 | return 1; 3 | -------------------------------------------------------------------------------- /Lecture032 Recursion Day2/ClimbStairs.java: -------------------------------------------------------------------------------- 1 | public class ClimbStairs { 2 | public static int countDistinctWayToClimbStair(long nStairs) { 3 | if (nStairs == 0) // base case if we reach here we have successfully reached bottom 4 | return 1; 5 | 6 | if (nStairs < 0) // if we reached here negative value has arrived 7 | return 0; 8 | 9 | //Recurrence Relation 10 | int ans = countDistinctWayToClimbStair(nStairs - 1) + countDistinctWayToClimbStair(nStairs - 2); 11 | 12 | return ans; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Lecture032 Recursion Day2/Fibbonacci.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 2 | using namespace std; 3 | 4 | 5 | int main() { 6 | 7 | 8 | 9 | 10 | return 0; 11 | } -------------------------------------------------------------------------------- /Lecture032 Recursion Day2/Fibonnaci.java: -------------------------------------------------------------------------------- 1 | class Fibonnaci { 2 | public int fib(int n) { 3 | 4 | if(n <= 1) //base case 5 | return n; 6 | 7 | return fib(n - 1) + fib(n - 2); // recurrence relation 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /Lecture032 Recursion Day2/RecursionExample.java: -------------------------------------------------------------------------------- 1 | public class RecursionExample { 2 | public static void main(String[] args) { 3 | int dest = 10; 4 | int src = 1; 5 | 6 | reachHome(src, dest); 7 | } 8 | public static void reachHome(int src , int dest){ 9 | if(src == dest) { 10 | System.out.println("Reached Home Successfully!!!"); 11 | return; 12 | } 13 | reachHome(src+1 , dest); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Lecture032 Recursion Day2/SayDigits.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 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 | 15 | //recursive call 16 | sayDigit(n, arr); 17 | 18 | cout << arr[digit] << " "; 19 | 20 | } 21 | 22 | int main() { 23 | 24 | string arr[10] = {"zero", "one", "two", "three", 25 | "four", "five", "six" 26 | , "seven", "eight", "nine"}; 27 | int n; 28 | cin >> n; 29 | 30 | cout << endl << endl << endl ; 31 | sayDigit(n, arr); 32 | cout << endl << endl << endl ; 33 | 34 | 35 | return 0; 36 | } -------------------------------------------------------------------------------- /Lecture032 Recursion Day2/SayDigits.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class SayDigits { 3 | 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int n = sc.nextInt(); 7 | 8 | String[] arr = {"Zero", "One", "Two", "Three", "Four", 9 | "Five", "Six", "Seven", "Eight", 10 | "Nine", "Ten"}; 11 | 12 | sayNumber(n, arr); 13 | } 14 | 15 | public static void sayNumber(int n , String[] arr) { 16 | if (n == 0) // base case 17 | return; 18 | 19 | int rem = n % 10; 20 | int nextDigit = n / 10; 21 | 22 | sayNumber(nextDigit, arr); // recursive relation 23 | 24 | System.out.print(arr[rem] + " "); // print the value while coming back from stack 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Lecture032 Recursion Day2/climbStairs.cpp: -------------------------------------------------------------------------------- 1 | int countDistinctWayToClimbStair(long long nStairs) 2 | { 3 | //base case 4 | if(nStairs < 0) 5 | return 0; 6 | 7 | if(nStairs == 0) 8 | return 1; 9 | 10 | //R.C 11 | int ans = countDistinctWayToClimbStair(nStairs-1) 12 | + countDistinctWayToClimbStair(nStairs-2); 13 | 14 | return ans; 15 | } -------------------------------------------------------------------------------- /Lecture032 Recursion Day2/walkingExample.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 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 | 21 | int main() { 22 | 23 | int dest = 10; 24 | int src = 1; 25 | 26 | cout << endl; 27 | 28 | reachHome(src, dest); 29 | 30 | 31 | return 0; 32 | } -------------------------------------------------------------------------------- /Lecture033 Recursion Day3/BinarySearch.java: -------------------------------------------------------------------------------- 1 | public class BinarySearch { 2 | public static void main(String[] args) { 3 | int[] arr = {2 ,3 , 4 ,10 , 12, 122}; 4 | int key = 3; 5 | 6 | int ans = findIdx(arr , 0 , 5 , key); 7 | 8 | if(ans == -1) 9 | System.out.println("Not Found"); 10 | 11 | else 12 | System.out.println("Element present at index " + ans); 13 | } 14 | public static int findIdx(int[] arr , int s , int e , int key){ 15 | //base case 16 | if(s > e) 17 | return -1; 18 | //find mid 19 | int mid = s + (e - s)/2; 20 | 21 | if(arr[mid] == key) // key is found 22 | return mid; 23 | 24 | if(arr[mid] < key) // search in right part 25 | return findIdx(arr , mid + 1 , e , key); // recurrence relation 26 | else // search in left part 27 | return findIdx(arr , s , mid - 1 , key);// recurrence relation 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Lecture033 Recursion Day3/LinearSearch.java: -------------------------------------------------------------------------------- 1 | package com.company; 2 | 3 | public class LinearSearch { 4 | public static void main(String[] args) { 5 | 6 | int[] arr = {3, 5 ,1 ,2 , 6}; 7 | int ans = search(arr , 7 , 5); 8 | if(ans == -1) 9 | System.out.println("Not Found"); 10 | else 11 | System.out.println("Element present at index " + ans); 12 | } 13 | 14 | public static int search(int[] arr , int key , int size){ 15 | return helper(arr , key , 0 , size); 16 | } 17 | 18 | public static int helper(int[] arr , int key , int idx , int size){ 19 | //base cases 20 | if(idx == size) // if reached here key is not present in array return -1; 21 | return -1; 22 | 23 | if(arr[idx] == key) // key found return index; 24 | return idx; 25 | 26 | // recurrence relation 27 | return helper(arr , key , idx + 1 , size); // element not present at current index do idx++; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Lecture033 Recursion Day3/SumCalc.java: -------------------------------------------------------------------------------- 1 | public class SumCalc { 2 | public static void main(String[] args) { 3 | 4 | int[] arr = {2 ,4 ,15 ,15 ,15}; 5 | int ans = calcSum(arr , 5); 6 | System.out.println(ans); // Ans is 51 7 | } 8 | 9 | public static int calcSum(int[] arr , int size){ 10 | if(size == 1) 11 | return arr[0]; 12 | return sumHelper(arr , 0 , size ,0); 13 | } 14 | 15 | public static int sumHelper(int[] arr , int idx , int size , int sum){ 16 | //base case 17 | if(idx == size) 18 | return sum; 19 | int currSum = sum + arr[idx]; 20 | return sumHelper(arr , idx + 1 , size , currSum); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Lecture033 Recursion Day3/binarySearch.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 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 | } cout << endl; 9 | } 10 | 11 | bool binarySearch(int *arr, int s, int e , int k ) { 12 | 13 | //base case 14 | 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 | 33 | 34 | int main() { 35 | 36 | int arr[11] = {2,4,6,10,14,18,22,38,49,55,222}; 37 | int size = 11; 38 | int key = 222; 39 | 40 | cout << "Present or not " << binarySearch(arr, 0, size-1, key) << endl; 41 | 42 | return 0; 43 | } -------------------------------------------------------------------------------- /Lecture033 Recursion Day3/isSorted.java: -------------------------------------------------------------------------------- 1 | public class isSorted { 2 | public static void main(String[] args) { 3 | 4 | int[] arr = {2 ,4 ,15 ,15 ,15}; 5 | boolean ans = checkSorted(arr , 5); 6 | if(ans) 7 | System.out.println("Array is Sorted"); 8 | else 9 | System.out.println("Array is unsorted"); 10 | } 11 | 12 | public static boolean checkSorted(int[] arr , int size){ 13 | if(size <=1) return true; 14 | return sortedHelper(arr , 1 , size); 15 | } 16 | 17 | //created this helper method as in Java we cannot do(arr + 1) (Java does not allow us to play with pointers) 18 | // Java allows us to create references but at backend it uses concept of pointers. 19 | 20 | //we have passed the index value and if condition is satisfied we increase idx + 1; 21 | 22 | public static boolean sortedHelper(int[] arr , int idx , int size){ 23 | //base case 24 | if(idx == size) 25 | return true; 26 | 27 | //case to check previous element is greater than current element 28 | if(arr[idx - 1] > arr[idx]) 29 | return false; 30 | return sortedHelper(arr , idx + 1 , size); // if reached till here we have to check for remaining element 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Lecture033 Recursion Day3/linearSearch.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 2 | using namespace std; 3 | 4 | void print(int arr[], int n) { 5 | cout << "Size of array is " << n << endl; 6 | 7 | for(int i=0; i<n; i++) { 8 | cout << arr[i] << " "; 9 | } cout << endl; 10 | 11 | } 12 | 13 | bool linearSearch(int arr[], int size, int k ) { 14 | print(arr,size); 15 | //base case 16 | if(size == 0) 17 | return false; 18 | 19 | if(arr[0] == k) { 20 | return true; 21 | } 22 | else { 23 | bool remainingPart = linearSearch(arr+1, size-1, k ); 24 | return remainingPart; 25 | } 26 | } 27 | 28 | int main() { 29 | 30 | int arr[5] = {3,5,1,2,6}; 31 | int size = 5; 32 | int key = 6; 33 | bool ans = linearSearch(arr, size, key); 34 | 35 | if(ans ){ 36 | cout << "Present " << endl; 37 | } 38 | else{ 39 | cout << "absent " << endl; 40 | } 41 | 42 | return 0; 43 | } -------------------------------------------------------------------------------- /Lecture033 Recursion Day3/sortedArray.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 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 | 11 | if(arr[0] > arr[1]) 12 | return false; 13 | else { 14 | bool remainingPart = isSorted(arr + 1, size - 1 ); 15 | return remainingPart; 16 | } 17 | 18 | } 19 | 20 | int main() { 21 | 22 | int arr[5] = {2,4,9,9,9}; 23 | int size = 5; 24 | 25 | bool ans = isSorted(arr, size); 26 | 27 | if(ans){ 28 | cout << "Array is sorted " << endl; 29 | } 30 | else { 31 | cout << "Array is not sorted " << endl; 32 | } 33 | 34 | return 0; 35 | } -------------------------------------------------------------------------------- /Lecture033 Recursion Day3/sum.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 2 | using namespace std; 3 | 4 | int getSum(int *arr, int size) { 5 | 6 | //base case 7 | if(size == 0) { 8 | return 0; 9 | } 10 | if(size == 1 ) 11 | { 12 | return arr[0]; 13 | } 14 | 15 | int remainingPart = getSum(arr+1, size-1); 16 | int sum = arr[0] + remainingPart; 17 | return sum; 18 | } 19 | 20 | int main() { 21 | 22 | int arr[5] = {2,4,9,9,9}; 23 | int size = 5; 24 | 25 | int sum = getSum(arr, size); 26 | 27 | cout << "Sum is " << sum << endl; 28 | 29 | return 0; 30 | } -------------------------------------------------------------------------------- /Lecture034 Recursion Day4/BubbleSort.java: -------------------------------------------------------------------------------- 1 | public class BubbleSort { 2 | public static void main(String[] args) { 3 | //Array to be sorted 4 | int[] arr = {2 , 8 , 4 , 9 , 5}; 5 | //size of array 6 | int n = 5; 7 | 8 | //printing array before sorting 9 | System.out.println("Before"); 10 | 11 | for(int i : arr){ 12 | System.out.print(i + " "); 13 | } 14 | 15 | System.out.println(); 16 | 17 | //recursive call 18 | sortRecursive(arr , n); 19 | 20 | //print array after sorting 21 | System.out.println("After"); 22 | 23 | for(int i : arr){ 24 | System.out.print(i + " "); 25 | } 26 | } 27 | public static void sortRecursive(int[] arr , int n){ 28 | //base case 29 | if(n == 0 || n == 1) 30 | return; 31 | 32 | for(int i = 0; i < n - 1; i++){ 33 | if(arr[i] > arr[i + 1]){ 34 | int temp = arr[i]; 35 | arr[i] = arr[i + 1]; 36 | arr[i + 1] = temp; 37 | } 38 | } 39 | sortRecursive(arr , n - 1); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Lecture034 Recursion Day4/InsertionSort.java: -------------------------------------------------------------------------------- 1 | public class InsertionSort { 2 | public static void main(String[] args) { 3 | 4 | //Array to be sorted 5 | int[] arr = {10 , 1 , 7 , 6 , 14 , 9}; 6 | //size of array 7 | int size = 6; 8 | 9 | //recursive function call 10 | sort(arr , 1 , size); 11 | 12 | //printing array at end 13 | for(int i : arr){ 14 | System.out.print(i + " "); 15 | } 16 | } 17 | 18 | //recursive function to sort array 19 | public static void sort(int[] arr , int idx , int size){ 20 | //base case 21 | if(idx == size) 22 | return; 23 | 24 | int curr = arr[idx]; 25 | int j = idx - 1; 26 | 27 | while(j >= 0){ 28 | if(curr < arr[j]){ // found greater value than curr shift to right 29 | arr[j + 1] = arr[j--]; 30 | } 31 | else // Smaller element found 32 | break; 33 | } 34 | arr[j + 1] = curr; //place curr element at correct place 35 | 36 | // one part Done recursion will do other work; 37 | sort(arr , idx + 1 , size); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Lecture034 Recursion Day4/SelectionSort.java: -------------------------------------------------------------------------------- 1 | public class SelectionSort { 2 | public static void main(String[] args) { 3 | int[] arr = {6, 2, 8, 4, 10}; 4 | int size = 5; 5 | 6 | sort(arr , 0 , size); 7 | 8 | for(int i = 0; i < size; i++){ 9 | System.out.print(arr[i] + " "); 10 | } 11 | } 12 | 13 | public static void sort(int[] arr , int idx , int size) 14 | { 15 | //base case 16 | if(idx == size) 17 | return; 18 | 19 | int smallestIdx = idx; // take curr idx element as smallest 20 | 21 | for(int i = idx + 1; i < size; i++){ 22 | if(arr[smallestIdx] > arr[i]) // found another smallest element than arr[smallestIdx] 23 | smallestIdx = i; // update the smallestIdx val 24 | } 25 | 26 | //place smallest element found at idx 27 | int temp = arr[idx]; 28 | arr[idx] = arr[smallestIdx]; 29 | arr[smallestIdx] = temp; 30 | 31 | // one part Done recursion will do other work; 32 | sort(arr , idx + 1 , size); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Lecture034 Recursion Day4/bubbleSort.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 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 | 11 | //1 case sovle karlia - largest element ko end me rakh dega 12 | for(int i=0; i<n-1; i++) { 13 | if(arr[i] > arr[i+1]){ 14 | swap(arr[i], arr[i+1]); 15 | } 16 | } 17 | 18 | //Recursive Call 19 | sortArray(arr, n-1); 20 | 21 | } 22 | 23 | int main() { 24 | 25 | int arr[5] = {2,5,1,6,9}; 26 | 27 | sortArray(arr,5); 28 | 29 | for(int i=0; i<5; i++) { 30 | cout << arr[i] << " "; 31 | }cout << endl; 32 | 33 | 34 | return 0; 35 | } -------------------------------------------------------------------------------- /Lecture034 Recursion Day4/exponent.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 2 | using namespace std; 3 | 4 | int power(int a, int b) { 5 | //base case 6 | if( b == 0 ) 7 | return 1; 8 | 9 | if(b == 1) 10 | return a; 11 | 12 | //RECURSIVE CALL 13 | int ans = power(a, b/2); 14 | 15 | //if b is even 16 | if(b%2 == 0) { 17 | return ans * ans; 18 | } 19 | else { 20 | //if b is odd 21 | return a * ans * ans; 22 | } 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 | 34 | return 0; 35 | } -------------------------------------------------------------------------------- /Lecture034 Recursion Day4/palindromeCheck.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 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 | else{ 13 | //Recursive call 14 | return checkPalindrome(str, i+1,j-1); 15 | } 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 << "Its a Palindrome " << endl; 27 | } 28 | else { 29 | cout << "Its not a Palindrome " << endl; 30 | } 31 | 32 | 33 | return 0; 34 | } -------------------------------------------------------------------------------- /Lecture034 Recursion Day4/reverseString.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 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 | 21 | int main() { 22 | 23 | string name = "abcde"; 24 | cout << endl; 25 | reverse(name, 0 , name.length()-1 ); 26 | cout << endl; 27 | cout << name << endl; 28 | 29 | return 0; 30 | } -------------------------------------------------------------------------------- /Lecture036 Recursion Day6/quickSort.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 2 | using namespace std; 3 | 4 | 5 | int partition( int arr[], int s, int e) { 6 | 7 | int pivot = arr[s]; 8 | 9 | int cnt = 0; 10 | for(int i = s+1; i<=e; i++) { 11 | if(arr[i] <=pivot) { 12 | cnt++; 13 | } 14 | } 15 | 16 | //place pivot at right position 17 | int pivotIndex = s + cnt; 18 | swap(arr[pivotIndex], arr[s]); 19 | 20 | //left and right wala part smbhal lete h 21 | int i = s, j = e; 22 | 23 | while(i < pivotIndex && j > pivotIndex) { 24 | 25 | while(arr[i] <= pivot) 26 | { 27 | i++; 28 | } 29 | 30 | while(arr[j] > pivot) { 31 | j--; 32 | } 33 | 34 | if(i < pivotIndex && j > pivotIndex) { 35 | swap(arr[i++], arr[j--]); 36 | } 37 | 38 | } 39 | 40 | return pivotIndex; 41 | 42 | } 43 | 44 | void quickSort(int arr[], int s, int e) { 45 | 46 | //base case 47 | if(s >= e) 48 | return ; 49 | 50 | //partitioon karenfe 51 | int p = partition(arr, s, e); 52 | 53 | //left part sort karo 54 | quickSort(arr, s, p-1); 55 | 56 | //right wala part sort karo 57 | quickSort(arr, p+1, e); 58 | 59 | } 60 | 61 | int main() { 62 | 63 | int arr[10] = {2,4,1,6,9 ,9,9,9,9,9}; 64 | int n = 10; 65 | 66 | quickSort(arr, 0, n-1); 67 | 68 | for(int i=0; i<n; i++) 69 | { 70 | cout << arr[i] << " "; 71 | } cout << endl; 72 | 73 | 74 | return 0; 75 | } -------------------------------------------------------------------------------- /Lecture037 Recursion Day7/subsequences.cpp: -------------------------------------------------------------------------------- 1 | void solve(vector<string>& 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<string> subsequences(string str){ 17 | 18 | vector<string> ans; 19 | string output = ""; 20 | solve(ans,str,output,0); 21 | return ans; 22 | 23 | } 24 | -------------------------------------------------------------------------------- /Lecture037 Recursion Day7/subsets.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | private: 3 | void solve(vector<int> nums, vector<int> output, int index, vector<vector<int> >& ans) { 4 | //base case 5 | if(index >= nums.size()) { 6 | ans.push_back(output); 7 | return ; 8 | } 9 | 10 | //exclude 11 | solve(nums, output, index+1, ans); 12 | 13 | //include 14 | int element = nums[index]; 15 | output.push_back(element); 16 | solve(nums, output, index+1, ans); 17 | 18 | } 19 | 20 | public: 21 | vector<vector<int>> subsets(vector<int>& nums) { 22 | 23 | vector<vector<int> > ans; 24 | vector<int> output; 25 | int index = 0; 26 | solve(nums, output, index, ans); 27 | return ans; 28 | 29 | } 30 | }; -------------------------------------------------------------------------------- /Lecture038 Recursion Day8/keypad.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | private: 3 | void solve(string digit, string output, int index, vector<string>& 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<value.length(); i++) { 15 | output.push_back(value[i]); 16 | solve(digit, output, index+1, ans, mapping); 17 | output.pop_back(); 18 | } 19 | 20 | } 21 | public: 22 | vector<string> letterCombinations(string digits) { 23 | vector<string> 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 | }; -------------------------------------------------------------------------------- /Lecture042 OOPs Day1/Hero.cpp: -------------------------------------------------------------------------------- 1 | class Hero { 2 | public: 3 | int health; 4 | char level; 5 | }; -------------------------------------------------------------------------------- /Lecture043 OOPs Day2/ambiguity.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 2 | using namespace std; 3 | 4 | class A { 5 | public: 6 | 7 | void func() { 8 | cout << " I am A" << endl; 9 | } 10 | }; 11 | 12 | class B { 13 | public: 14 | 15 | void func() { 16 | cout << " I am B" << endl; 17 | } 18 | }; 19 | 20 | class C: public A, public B { 21 | 22 | 23 | }; 24 | 25 | int main() { 26 | 27 | C obj; 28 | //obj.func(); 29 | 30 | obj.A::func() ; 31 | obj.B::func(); 32 | 33 | return 0; 34 | } -------------------------------------------------------------------------------- /Lecture043 OOPs Day2/concept.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 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 | 21 | cout << "Sab sahi chalra hai" << endl; 22 | 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Lecture043 OOPs Day2/hierarchicalInheritance.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 2 | using namespace std; 3 | //Hierarchical Inheritance 4 | class A { 5 | 6 | public: 7 | void func1() { 8 | cout << "Inside Funcion 1" << endl; 9 | } 10 | 11 | }; 12 | 13 | class B: public A { 14 | public: 15 | void func2() { 16 | cout << "Inside Funcion 2" << endl; 17 | } 18 | }; 19 | 20 | class C: public A { 21 | public: 22 | void func3() { 23 | cout << "Inside Funcion 3" << endl; 24 | } 25 | }; 26 | 27 | int main() { 28 | 29 | A object1; 30 | object1.func1(); 31 | 32 | B object2; 33 | object2.func1(); 34 | object2.func2(); 35 | 36 | C object3; 37 | object3.func1(); 38 | object3.func3(); 39 | 40 | return 0; 41 | } -------------------------------------------------------------------------------- /Lecture043 OOPs Day2/inheritance.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 2 | using namespace std; 3 | 4 | class Human { 5 | 6 | private: 7 | int height; 8 | 9 | public: 10 | int weight; 11 | 12 | private: 13 | int age; 14 | 15 | public: 16 | int getAge() { 17 | return this->age; 18 | } 19 | 20 | void setWeight(int w) { 21 | this->weight = w; 22 | 23 | } 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 | 51 | /* 52 | Male object1; 53 | cout << object1.age << endl; 54 | cout << object1.weight << endl; 55 | cout << object1.height << endl; 56 | 57 | cout << object1.color << endl; 58 | 59 | object1.setWeight(84); 60 | cout << object1.weight << endl; 61 | object1.sleep(); 62 | */ 63 | 64 | return 0; 65 | } -------------------------------------------------------------------------------- /Lecture043 OOPs Day2/multiLevelInheritance.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 2 | using namespace std; 3 | 4 | 5 | class Animal { 6 | 7 | public: 8 | int age; 9 | int weight; 10 | 11 | 12 | public: 13 | void speak() { 14 | cout << "Speaking " << endl; 15 | } 16 | }; 17 | 18 | class Dog: public Animal { 19 | 20 | }; 21 | 22 | class GermanShepherd: public Dog { 23 | 24 | 25 | }; 26 | 27 | int main() { 28 | 29 | GermanShepherd g; 30 | g.speak(); 31 | 32 | 33 | return 0; 34 | } -------------------------------------------------------------------------------- /Lecture043 OOPs Day2/multipleInheritance.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 2 | using namespace std; 3 | 4 | 5 | class Animal { 6 | 7 | public: 8 | int age; 9 | int weight; 10 | 11 | 12 | public: 13 | void bark() { 14 | cout << "Barking " << endl; 15 | } 16 | }; 17 | 18 | class Human { 19 | public: 20 | string color; 21 | 22 | public: 23 | void speak() { 24 | cout << "Speaking " << endl; 25 | } 26 | }; 27 | 28 | //Multiple Inheritance 29 | class Hybrid: public Animal, public Human { 30 | 31 | }; 32 | 33 | 34 | int main() { 35 | 36 | Hybrid obj1; 37 | obj1.speak(); 38 | obj1.bark(); 39 | 40 | 41 | return 0; 42 | } -------------------------------------------------------------------------------- /Lecture043 OOPs Day2/polymorphism.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 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 | 22 | class B { 23 | public: 24 | int a; 25 | int b; 26 | 27 | public: 28 | int add() { 29 | return a+b; 30 | } 31 | 32 | void operator+ (B &obj) { 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 | 46 | class Animal { 47 | public: 48 | void speak() { 49 | cout << "Speaking "<< endl; 50 | } 51 | }; 52 | 53 | class Dog: public Animal { 54 | 55 | public: 56 | void speak() { 57 | cout << "Barking " << endl; 58 | } 59 | 60 | 61 | }; 62 | 63 | 64 | 65 | int main() { 66 | 67 | Dog obj; 68 | obj.speak(); 69 | 70 | 71 | 72 | 73 | /* 74 | B obj1, obj2; 75 | 76 | obj1.a = 4; 77 | obj2.a = 7; 78 | 79 | obj1 + obj2; 80 | obj1();*/ 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | /* 90 | A obj; 91 | obj.sayHello(); 92 | */ 93 | 94 | 95 | return 0; 96 | } -------------------------------------------------------------------------------- /Lecture043 OOPs Day2/singleInheritance.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 2 | using namespace std; 3 | 4 | 5 | class Animal { 6 | 7 | public: 8 | int age; 9 | int weight; 10 | 11 | 12 | public: 13 | void speak() { 14 | cout << "Speaking " << endl; 15 | } 16 | }; 17 | 18 | class Dog: public Animal { 19 | 20 | }; 21 | 22 | 23 | int main() { 24 | 25 | Dog d; 26 | d.speak(); 27 | cout << d.age << endl; 28 | 29 | 30 | return 0; 31 | } -------------------------------------------------------------------------------- /Lecture046 Linked List Day3/reverseInKGroup.cpp: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | Following is the Linked List node structure 3 | 4 | class Node 5 | { 6 | public: 7 | int data; 8 | Node *next; 9 | Node(int data) 10 | { 11 | this->data = data; 12 | this->next = NULL; 13 | } 14 | }; 15 | 16 | *****************************************************************/ 17 | 18 | 19 | Node* kReverse(Node* head, int k) { 20 | //base call 21 | if(head == NULL) { 22 | return NULL; 23 | } 24 | 25 | //step1: reverse first k nodes 26 | Node* next = NULL; 27 | Node* curr = head; 28 | Node* prev = NULL; 29 | int count= 0; 30 | 31 | while( curr != NULL && count < k ) { 32 | next = curr -> next; 33 | curr -> next = prev; 34 | prev = curr; 35 | curr = next; 36 | count++; 37 | } 38 | 39 | //step2: Recursion dekhlega aage ka 40 | if(next != NULL) { 41 | head -> next = kReverse(next,k); 42 | } 43 | 44 | //step3: return head of reversed list 45 | return prev; 46 | } -------------------------------------------------------------------------------- /Lecture047 Linked List Day4/loop.cpp: -------------------------------------------------------------------------------- 1 | /************************************************* 2 | 3 | class Node { 4 | public : 5 | 6 | int data; 7 | Node *next; 8 | 9 | Node(int data) { 10 | this -> data = data; 11 | this -> next = NULL; 12 | } 13 | }; 14 | 15 | *************************************************/ 16 | 17 | Node* floydDetectLoop(Node* head) { 18 | 19 | if(head == NULL) 20 | return NULL; 21 | 22 | Node* slow = head; 23 | Node* fast = head; 24 | 25 | while(slow != NULL && fast !=NULL) { 26 | 27 | fast = fast -> next; 28 | if(fast != NULL) { 29 | fast = fast -> next; 30 | } 31 | 32 | slow = slow -> next; 33 | 34 | if(slow == fast) { 35 | return slow; 36 | } 37 | } 38 | 39 | return NULL; 40 | 41 | } 42 | 43 | Node* getStartingNode(Node* head) { 44 | 45 | if(head == NULL) 46 | return NULL; 47 | 48 | Node* intersection = floydDetectLoop(head); 49 | 50 | if(intersection == NULL) 51 | return NULL; 52 | 53 | Node* slow = head; 54 | 55 | while(slow != intersection) { 56 | slow = slow -> next; 57 | intersection = intersection -> next; 58 | } 59 | 60 | return slow; 61 | 62 | } 63 | 64 | Node *removeLoop(Node *head) 65 | { 66 | if( head == NULL) 67 | return NULL; 68 | 69 | Node* startOfLoop = getStartingNode(head); 70 | 71 | if(startOfLoop == NULL) 72 | return head; 73 | 74 | Node* temp = startOfLoop; 75 | 76 | while(temp -> next != startOfLoop) { 77 | temp = temp -> next; 78 | } 79 | 80 | temp -> next = NULL; 81 | return head; 82 | } -------------------------------------------------------------------------------- /Lecture048 Linked List Day5/duplicates.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************ 2 | 3 | Following is the linked list node structure. 4 | 5 | class Node 6 | { 7 | public: 8 | int data; 9 | Node* next; 10 | 11 | Node(int data) 12 | { 13 | this->data = data; 14 | this->next = NULL; 15 | } 16 | }; 17 | 18 | ************************************************************/ 19 | 20 | Node * uniqueSortedList(Node * head) { 21 | //empty List 22 | if(head == NULL) 23 | return NULL; 24 | 25 | //non empty list 26 | Node* curr = head; 27 | 28 | while(curr != NULL) { 29 | 30 | if( (curr -> next != NULL) && curr -> data == curr -> next -> data) { 31 | Node* next_next = curr ->next -> next; 32 | Node* nodeToDelete = curr -> next; 33 | delete(nodeToDelete); 34 | curr -> next = next_next; 35 | } 36 | else //not equal 37 | { 38 | curr = curr -> next; 39 | } 40 | } 41 | 42 | return head; 43 | } -------------------------------------------------------------------------------- /Lecture049 Linked List Day6/mergeList.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************ 2 | 3 | Following is the linked list node structure. 4 | 5 | template <typename T> 6 | class Node { 7 | public: 8 | T data; 9 | Node* next; 10 | 11 | Node(T data) { 12 | next = NULL; 13 | this->data = data; 14 | } 15 | 16 | ~Node() { 17 | if (next != NULL) { 18 | delete next; 19 | } 20 | } 21 | }; 22 | 23 | ************************************************************/ 24 | 25 | void solve(Node<int>* first, Node<int>* second) { 26 | 27 | 28 | Node* curr1 = first; 29 | Node* next1 = curr1 -> next; 30 | 31 | Node* curr2 = second; 32 | Node* next2 = curr2 -> next; 33 | 34 | while(next1 != NULL && curr2 != NULL) { 35 | 36 | if( (curr2 -> data >= curr1 -> data ) 37 | && ( curr2 -> data <= next1 -> data)) { 38 | 39 | curr1 -> next = curr2; 40 | curr2 -> next = next1; 41 | curr1 = curr2; 42 | curr2 = next2; 43 | } 44 | else { 45 | 46 | } 47 | 48 | 49 | } 50 | 51 | 52 | } 53 | 54 | Node<int>* sortTwoLists(Node<int>* first, Node<int>* second) 55 | { 56 | if(first == NULL) 57 | return second; 58 | 59 | if(second == NULL) 60 | return first; 61 | 62 | if(first -> data <= second -> data ){ 63 | solve(first, second); 64 | } 65 | else 66 | { 67 | solve(second, first); 68 | } 69 | 70 | 71 | } 72 | -------------------------------------------------------------------------------- /Lecture049 Linked List Day6/sort.cpp: -------------------------------------------------------------------------------- 1 | /******************************** 2 | class Node 3 | { 4 | public: 5 | int data; 6 | Node *next; 7 | Node(int data) 8 | { 9 | this->data = data; 10 | this->next = NULL; 11 | } 12 | }; 13 | 14 | ********************************/ 15 | void insertAtTail(Node* &tail, Node* curr ) { 16 | tail -> next = curr; 17 | tail = curr; 18 | } 19 | 20 | 21 | Node* sortList(Node *head) 22 | { 23 | Node* zeroHead = new Node(-1); 24 | Node* zeroTail = zeroHead; 25 | Node* oneHead = new Node(-1); 26 | Node* oneTail = oneHead; 27 | Node* twoHead = new Node(-1); 28 | Node* twoTail = twoHead; 29 | 30 | Node* curr = head; 31 | 32 | // create separate list 0s, 1s and 2s 33 | while(curr != NULL) { 34 | 35 | int value = curr -> data; 36 | 37 | if(value == 0) { 38 | insertAtTail(zeroTail, curr); 39 | } 40 | else if(value == 1) { 41 | insertAtTail(oneTail, curr); 42 | } 43 | else if(value == 2) { 44 | insertAtTail(twoTail, curr); 45 | } 46 | curr = curr -> next; 47 | } 48 | 49 | //merge 3 sublist 50 | 51 | // 1s list not empty 52 | if(oneHead -> next != NULL) { 53 | zeroTail -> next = oneHead -> next; 54 | } 55 | else { 56 | //1s list -> empty 57 | zeroTail -> next = twoHead -> next; 58 | } 59 | 60 | oneTail -> next = twoHead -> next; 61 | twoTail -> next = NULL; 62 | 63 | //setup head 64 | head = zeroHead -> next; 65 | 66 | //delete dummy nodes 67 | delete zeroHead; 68 | delete oneHead; 69 | delete twoHead; 70 | 71 | return head; 72 | } 73 | -------------------------------------------------------------------------------- /Lecture054 Stack & its Implementation/twoStacks.cpp: -------------------------------------------------------------------------------- 1 | class TwoStack { 2 | int *arr; 3 | int top1; 4 | int top2; 5 | int size; 6 | public: 7 | 8 | // Initialize TwoStack. 9 | TwoStack(int s) { 10 | this -> size = s; 11 | top1 = -1; 12 | top2 = s; 13 | arr = new int[s]; 14 | } 15 | 16 | // Push in stack 1. 17 | void push1(int num) { 18 | //atleast a empty space present 19 | if(top2 - top1 > 1 ) { 20 | top1++; 21 | arr[top1] = num; 22 | } 23 | 24 | } 25 | 26 | // Push in stack 2. 27 | void push2(int num) { 28 | if(top2 - top1 > 1 ) { 29 | top2--; 30 | arr[top2] = num; 31 | } 32 | 33 | } 34 | 35 | // Pop from stack 1 and return popped element. 36 | int pop1() { 37 | if( top1 >= 0) { 38 | int ans = arr[top1]; 39 | top1--; 40 | return ans; 41 | } 42 | else 43 | { 44 | return -1; 45 | } 46 | } 47 | 48 | // Pop from stack 2 and return popped element. 49 | int pop2() { 50 | if( top2 < size) { 51 | int ans = arr[top2]; 52 | top2++; 53 | return ans; 54 | } 55 | else 56 | { 57 | return -1; 58 | } 59 | } 60 | 61 | }; 62 | -------------------------------------------------------------------------------- /Lecture055 Stack Questions/bracketReversals.cpp: -------------------------------------------------------------------------------- 1 | #include<stack> 2 | int findMinimumCost(string str) { 3 | 4 | //odd condition 5 | if(str.length()%2 == 1) { 6 | return -1; 7 | } 8 | 9 | stack<char> s; 10 | for(int i=0; i<str.length(); i++) { 11 | char ch = str[i]; 12 | 13 | if(ch == '{') 14 | s.push(ch); 15 | else 16 | { 17 | //ch is closed brace 18 | if(!s.empty() && s.top() == '{') { 19 | s.pop(); 20 | } 21 | else 22 | { 23 | s.push(ch); 24 | } 25 | } 26 | } 27 | 28 | //stack contains invalid expression 29 | int a = 0, b = 0; 30 | while(!s.empty()) { 31 | if(s.top() == '{') { 32 | b++; 33 | } 34 | else 35 | { 36 | a++; 37 | } 38 | s.pop(); 39 | } 40 | 41 | int ans = (a+1)/2 + (b+1)/2; 42 | return ans; 43 | 44 | 45 | } -------------------------------------------------------------------------------- /Lecture055 Stack Questions/deleteMiddle.cpp: -------------------------------------------------------------------------------- 1 | 2 | void solve(stack<int>&inputStack, int count, int size) { 3 | //base case 4 | if(count == size/2) { 5 | inputStack.pop(); 6 | return ; 7 | } 8 | 9 | int num = inputStack.top(); 10 | inputStack.pop(); 11 | 12 | //RECURSIVE CALL 13 | solve(inputStack, count+1, size); 14 | 15 | inputStack.push(num); 16 | 17 | } 18 | 19 | void deleteMiddle(stack<int>&inputStack, int N){ 20 | 21 | int count = 0; 22 | solve(inputStack, count, N); 23 | 24 | } -------------------------------------------------------------------------------- /Lecture055 Stack Questions/insertAtBottom.cpp: -------------------------------------------------------------------------------- 1 | void solve(stack<int>& s, int x) { 2 | //base case 3 | if(s.empty()) { 4 | s.push(x); 5 | return ; 6 | } 7 | 8 | int num = s.top(); 9 | s.pop(); 10 | 11 | //recursive call 12 | solve(s, x); 13 | 14 | s.push(num); 15 | } 16 | 17 | 18 | stack<int> pushAtBottom(stack<int>& myStack, int x) 19 | { 20 | solve(myStack, x); 21 | return myStack; 22 | } 23 | -------------------------------------------------------------------------------- /Lecture055 Stack Questions/redundantBrackets.cpp: -------------------------------------------------------------------------------- 1 | #include<stack> 2 | 3 | bool findRedundantBrackets(string &s) 4 | { 5 | stack<char> st; 6 | for(int i=0; i<s.length(); i++) { 7 | char ch =s[i]; 8 | 9 | if(ch == '(' || ch == '+' ||ch == '-' || ch == '*' || ch == '/') { 10 | st.push(ch); 11 | } 12 | else 13 | { 14 | //ch ya toh ')' hai or lowercase letter 15 | 16 | if(ch == ')') { 17 | bool isRedundant = true; 18 | 19 | while(st.top() != '(') { 20 | char top = st.top(); 21 | if(top == '+' ||top == '-' || top == '*' || top == '/') { 22 | isRedundant = false; 23 | } 24 | st.pop(); 25 | } 26 | 27 | if(isRedundant == true) 28 | return true; 29 | st.pop(); 30 | } 31 | 32 | } 33 | } 34 | return false; 35 | } 36 | -------------------------------------------------------------------------------- /Lecture055 Stack Questions/reverseStack.cpp: -------------------------------------------------------------------------------- 1 | 2 | void insertAtBottom(stack<int> &s, int element) { 3 | //basecase 4 | if(s.empty()) { 5 | s.push(element); 6 | return ; 7 | } 8 | 9 | int num = s.top(); 10 | s.pop(); 11 | 12 | //recursive call 13 | insertAtBottom(s, element); 14 | 15 | s.push(num); 16 | } 17 | 18 | void reverseStack(stack<int> &stack) { 19 | //base case 20 | if(stack.empty()) { 21 | return ; 22 | } 23 | 24 | int num = stack.top(); 25 | stack.pop(); 26 | 27 | //recursive call 28 | reverseStack(stack); 29 | 30 | insertAtBottom(stack,num); 31 | } -------------------------------------------------------------------------------- /Lecture055 Stack Questions/reverseString.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 2 | #include<stack> 3 | using namespace std; 4 | 5 | 6 | 7 | int main () { 8 | string str = "babbar"; 9 | 10 | stack<char> s; 11 | 12 | for (int i = 0; i<str.length(); i++) { 13 | char ch = str[i]; 14 | s.push(ch); 15 | } 16 | 17 | string ans = ""; 18 | 19 | while(!s.empty()) { 20 | char ch = s.top(); 21 | ans.push_back(ch); 22 | 23 | s.pop(); 24 | } 25 | 26 | cout << "answer is: "<< ans << endl; 27 | 28 | return 0; 29 | } -------------------------------------------------------------------------------- /Lecture055 Stack Questions/sortStack.cpp: -------------------------------------------------------------------------------- 1 | void sortedInsert(stack<int> &stack, int num) { 2 | //base case 3 | if(stack.empty() || (!stack.empty() && stack.top() < num) ) { 4 | stack.push(num); 5 | return; 6 | } 7 | 8 | int n = stack.top(); 9 | stack.pop(); 10 | 11 | //recusrive call 12 | sortedInsert(stack, num); 13 | 14 | stack.push(n); 15 | } 16 | 17 | void sortStack(stack<int> &stack) 18 | { 19 | //base case 20 | if(stack.empty()) { 21 | return ; 22 | } 23 | 24 | int num = stack.top(); 25 | stack.pop(); 26 | 27 | //recursive call 28 | sortStack(stack); 29 | 30 | sortedInsert(stack, num); 31 | } -------------------------------------------------------------------------------- /Lecture055 Stack Questions/validBrackets.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | bool isValidParenthesis(string expression) 4 | { 5 | stack<char> s; 6 | for(int i=0; i<expression.length(); i++) { 7 | 8 | char ch = expression[i]; 9 | 10 | //if opening bracket, stack push 11 | //if close bracket, stacktop check and pop 12 | 13 | if(ch == '(' || ch == '{' || ch == '['){ 14 | s.push(ch); 15 | } 16 | else 17 | { 18 | //for closing bracket 19 | if(!s.empty()) { 20 | char top = s.top(); 21 | if( (ch == ')' && top == '(') || 22 | ( ch == '}' && top == '{') || 23 | (ch == ']' && top == '[') ) { 24 | s.pop(); 25 | } 26 | else 27 | { 28 | return false; 29 | } 30 | } 31 | else 32 | { 33 | return false; 34 | } 35 | } 36 | } 37 | 38 | if(s.empty()) 39 | return true; 40 | else 41 | return false; 42 | } -------------------------------------------------------------------------------- /Lecture056 Stacks Questions/areaHistogram.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | private: 3 | vector<int> nextSmallerElement(vector<int> arr, int n) { 4 | stack<int> s; 5 | s.push(-1); 6 | vector<int> ans(n); 7 | 8 | for(int i=n-1; i>=0 ; i--) { 9 | int curr = arr[i]; 10 | while(s.top() != -1 && arr[s.top()] >= curr) 11 | { 12 | s.pop(); 13 | } 14 | //ans is stack ka top 15 | ans[i] = s.top(); 16 | s.push(i); 17 | } 18 | return ans; 19 | } 20 | 21 | vector<int> prevSmallerElement(vector<int> arr, int n) { 22 | stack<int> s; 23 | s.push(-1); 24 | vector<int> ans(n); 25 | 26 | for(int i=0; i<n; i++) { 27 | int curr = arr[i]; 28 | while(s.top() != -1 && arr[s.top()] >= curr) 29 | { 30 | s.pop(); 31 | } 32 | //ans is stack ka top 33 | ans[i] = s.top(); 34 | s.push(i); 35 | } 36 | return ans; 37 | } 38 | 39 | public: 40 | int largestRectangleArea(vector<int>& heights) { 41 | int n= heights.size(); 42 | 43 | vector<int> next(n); 44 | next = nextSmallerElement(heights, n); 45 | 46 | vector<int> prev(n); 47 | prev = prevSmallerElement(heights, n); 48 | 49 | int area = INT_MIN; 50 | for(int i=0; i<n; i++) { 51 | int l = heights[i]; 52 | 53 | if(next[i] == -1) { 54 | next[i] = n; 55 | } 56 | int b = next[i] - prev[i] - 1; 57 | int newArea = l*b; 58 | area = max(area, newArea); 59 | } 60 | return area; 61 | } 62 | }; -------------------------------------------------------------------------------- /Lecture056 Stacks Questions/nextSmaller.cpp: -------------------------------------------------------------------------------- 1 | #include<stack> 2 | vector<int> nextSmallerElement(vector<int> &arr, int n) 3 | { 4 | stack<int> s; 5 | s.push(-1); 6 | vector<int> ans(n); 7 | 8 | for(int i=n-1; i>=0 ; i--) { 9 | int curr = arr[i]; 10 | while(s.top() >= curr) 11 | { 12 | s.pop(); 13 | } 14 | //ans is stack ka top 15 | ans[i] = s.top(); 16 | s.push(curr); 17 | } 18 | return ans; 19 | } -------------------------------------------------------------------------------- /Lecture059 Stack Questions/specialStack.cpp: -------------------------------------------------------------------------------- 1 | #include<stack> 2 | #include<limits.h> 3 | class SpecialStack { 4 | // Define the data members. 5 | stack<int> s; 6 | int mini = INT_MAX; 7 | /*----------------- Public Functions of SpecialStack -----------------*/ 8 | public: 9 | 10 | void push(int data) { 11 | //for first element 12 | if(s.empty()) { 13 | s.push(data); 14 | mini = data; 15 | } 16 | else 17 | { 18 | if(data < mini) { 19 | s.push(2*data - mini); 20 | mini = data; 21 | } 22 | else 23 | { 24 | s.push(data); 25 | } 26 | } 27 | } 28 | 29 | int pop() { 30 | if(s.empty()){ 31 | return -1; 32 | } 33 | 34 | int curr = s.top(); 35 | s.pop(); 36 | if(curr > mini) { 37 | return curr; 38 | } 39 | else 40 | { 41 | int prevMin = mini; 42 | int val = 2*mini - curr; 43 | mini = val; 44 | return prevMin; 45 | } 46 | } 47 | 48 | int top() { 49 | if(s.empty()) 50 | return -1; 51 | 52 | int curr = s.top(); 53 | if(curr < mini) { 54 | return mini; 55 | } 56 | else 57 | { 58 | return curr; 59 | } 60 | } 61 | 62 | bool isEmpty() { 63 | return s.empty(); 64 | } 65 | 66 | int getMin() { 67 | if(s.empty()) 68 | return -1; 69 | 70 | return mini; 71 | } 72 | }; -------------------------------------------------------------------------------- /Lecture060 Queue & its Implementation/implementQueue.cpp: -------------------------------------------------------------------------------- 1 | class Queue { 2 | 3 | int* arr; 4 | int qfront; 5 | int rear; 6 | int size; 7 | 8 | public: 9 | Queue() { 10 | size = 100001; 11 | arr = new int[size]; 12 | qfront = 0; 13 | rear = 0; 14 | } 15 | 16 | /*----------------- Public Functions of Queue -----------------*/ 17 | 18 | bool isEmpty() { 19 | if(qfront == rear) { 20 | return true; 21 | } 22 | else 23 | { 24 | return false; 25 | } 26 | } 27 | 28 | void enqueue(int data) { 29 | if(rear == size) 30 | cout << "Queue is Full" << endl; 31 | else 32 | { 33 | arr[rear] = data; 34 | rear++; 35 | } 36 | } 37 | 38 | int dequeue() { 39 | if(qfront == rear) { 40 | return -1; 41 | } 42 | else 43 | { int ans = arr[qfront]; 44 | arr[qfront] = -1; 45 | qfront++; 46 | if(qfront==rear){ 47 | qfront = 0; 48 | rear = 0; 49 | } 50 | return ans; 51 | } 52 | } 53 | 54 | int front() { 55 | if(qfront == rear) { 56 | return -1; 57 | } 58 | else 59 | { 60 | return arr[qfront]; 61 | } 62 | } 63 | }; -------------------------------------------------------------------------------- /Lecture060 Queue & its Implementation/queueIntro.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 2 | #include<queue> 3 | using namespace std; 4 | 5 | int main() { 6 | cout << (-1)%1<< endl; 7 | deque<int> d; 8 | 9 | d.push_front(12); 10 | d.push_back(14); 11 | 12 | cout << d.front() << endl; 13 | cout << d.back() << endl; 14 | 15 | d.pop_front(); 16 | 17 | cout << d.front() << endl; 18 | cout << d.back() << endl; 19 | d.pop_back(); 20 | 21 | if(d.empty()) { 22 | cout << "queue is empty" << endl; 23 | } 24 | else{ 25 | cout << "queue is not empty " << endl; 26 | } 27 | 28 | /* 29 | //create a queue 30 | queue<int> q; 31 | 32 | q.push(11); 33 | cout << "front of q is: " << q.front() << endl; 34 | q.push(15); 35 | cout << "front of q is: " << q.front() << endl; 36 | 37 | q.push(13); 38 | cout << "front of q is: " << q.front() << endl; 39 | 40 | cout << "size of queue is : " << q.size() << endl; 41 | 42 | q.pop(); 43 | q.pop(); 44 | q.pop(); 45 | 46 | cout << "size of queue is : " << q.size() << endl; 47 | 48 | if(q.empty()) { 49 | cout << "Queue is empty " << endl; 50 | } 51 | else{ 52 | cout << "queue is not empty " << endl; 53 | } 54 | */ 55 | 56 | 57 | return 0; 58 | } -------------------------------------------------------------------------------- /Lecture061 Queue Interview Questions/circularTour.cpp: -------------------------------------------------------------------------------- 1 | // { Driver Code Starts 2 | #include<bits/stdc++.h> 3 | using namespace std; 4 | 5 | struct petrolPump 6 | { 7 | int petrol; 8 | int distance; 9 | }; 10 | 11 | 12 | // } Driver Code Ends 13 | /* 14 | The structure of petrolPump is 15 | struct petrolPump 16 | { 17 | int petrol; 18 | int distance; 19 | };*/ 20 | 21 | /*You are required to complete this method*/ 22 | class Solution{ 23 | public: 24 | 25 | //Function to find starting point where the truck can start to get through 26 | //the complete circle without exhausting its petrol in between. 27 | int tour(petrolPump p[],int n) 28 | { 29 | int deficit = 0; 30 | int balance = 0; 31 | int start = 0; 32 | 33 | for(int i=0; i<n; i++) { 34 | balance += p[i].petrol - p[i].distance; 35 | if(balance < 0) { 36 | start = i+1; 37 | deficit += balance; 38 | balance = 0; 39 | } 40 | } 41 | 42 | if(balance + deficit >=0) 43 | return start; 44 | return -1; 45 | 46 | } 47 | }; 48 | 49 | 50 | 51 | // { Driver Code Starts. 52 | 53 | int main() 54 | { 55 | int t; 56 | cin>>t; 57 | while(t--) 58 | { 59 | int n; 60 | cin>>n; 61 | petrolPump p[n]; 62 | for(int i=0;i<n;i++) 63 | cin>>p[i].petrol>>p[i].distance; 64 | Solution obj; 65 | cout<<obj.tour(p,n)<<endl; 66 | } 67 | } 68 | // } Driver Code Ends -------------------------------------------------------------------------------- /Lecture061 Queue Interview Questions/nonRepeating.cpp: -------------------------------------------------------------------------------- 1 | // { Driver Code Starts 2 | #include<bits/stdc++.h> 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | class Solution { 7 | public: 8 | string FirstNonRepeating(string A){ 9 | map<char, int> m; 10 | string ans = ""; 11 | queue<char> q; 12 | 13 | for(int i=0; i<A.length(); i++) { 14 | char ch = A[i]; 15 | 16 | q.push(ch); 17 | m[ch]++; 18 | 19 | while(!q.empty()) { 20 | if(m[q.front()] > 1){ 21 | q.pop(); 22 | } 23 | else 24 | { 25 | ans.push_back(q.front()); 26 | break; 27 | } 28 | } 29 | if(q.empty()){ 30 | ans.push_back('#'); 31 | } 32 | } 33 | return ans; 34 | } 35 | 36 | }; 37 | 38 | // { Driver Code Starts. 39 | int main(){ 40 | int tc; 41 | cin >> tc; 42 | while(tc--){ 43 | string A; 44 | cin >> A; 45 | Solution obj; 46 | string ans = obj.FirstNonRepeating(A); 47 | cout << ans << "\n"; 48 | } 49 | return 0; 50 | } // } Driver Code Ends -------------------------------------------------------------------------------- /Lecture061 Queue Interview Questions/reverseFirstK.cpp: -------------------------------------------------------------------------------- 1 | // { Driver Code Starts 2 | // Initial Template for C++ 3 | 4 | #include <bits/stdc++.h> 5 | using namespace std; 6 | queue<int> modifyQueue(queue<int> q, int k); 7 | int main() { 8 | int t; 9 | cin >> t; 10 | while (t-- > 0) { 11 | int n, k; 12 | cin >> n >> k; 13 | queue<int> q; 14 | while (n-- > 0) { 15 | int a; 16 | cin >> a; 17 | q.push(a); 18 | } 19 | queue<int> ans = modifyQueue(q, k); 20 | while (!ans.empty()) { 21 | int a = ans.front(); 22 | ans.pop(); 23 | cout << a << " "; 24 | } 25 | cout << endl; 26 | } 27 | }// } Driver Code Ends 28 | 29 | 30 | // User function Template for C++ 31 | 32 | // Function to reverse first k elements of a queue. 33 | queue<int> modifyQueue(queue<int> q, int k) { 34 | //algo: 35 | //first k element stack me daaldo and queue se hatado 36 | // k element stack se nikalke wapas queue me daalde 37 | // fer queue k (n-k) starting k elements, wapas queue me daalde 38 | 39 | stack<int> s; 40 | 41 | for(int i = 0; i<k; i++) { 42 | int val = q.front(); 43 | q.pop(); 44 | s.push(val); 45 | } 46 | 47 | while(!s.empty()) { 48 | int val = s.top(); 49 | s.pop(); 50 | q.push(val); 51 | } 52 | 53 | int t = q.size()-k; 54 | 55 | while(t--) { 56 | int val = q.front(); 57 | q.pop(); 58 | q.push(val); 59 | } 60 | 61 | return q; 62 | } -------------------------------------------------------------------------------- /Lecture061 Queue Interview Questions/reverseQueue.cpp: -------------------------------------------------------------------------------- 1 | queue<int> rev(queue<int> q) 2 | { 3 | stack<int> s; 4 | 5 | while(!q.empty()) { 6 | int element = q.front(); 7 | q.pop(); 8 | s.push(element); 9 | } 10 | 11 | while(!s.empty()) { 12 | int element = s.top(); 13 | s.pop(); 14 | q.push(element); 15 | } 16 | return q; 17 | } -------------------------------------------------------------------------------- /Lecture061 Queue Interview Questions/sumMaxMin.cpp: -------------------------------------------------------------------------------- 1 | #include<iostream> 2 | #include<queue> 3 | using namespace std; 4 | 5 | int solve(int *arr, int n, int k) { 6 | 7 | deque<int> maxi(k); 8 | deque<int> mini(k); 9 | 10 | //Addition of first k size window 11 | 12 | for(int i=0; i<k; i++) { 13 | 14 | while(!maxi.empty() && arr[maxi.back()] <= arr[i]) 15 | maxi.pop_back(); 16 | 17 | while(!mini.empty() && arr[mini.back()] >= arr[i]) 18 | mini.pop_back(); 19 | 20 | maxi.push_back(i); 21 | mini.push_back(i); 22 | } 23 | int ans = 0; 24 | 25 | ans += arr[maxi.front()] + arr[mini.front()]; 26 | 27 | //remaining windows ko process karlo 28 | for(int i=k; i<n; i++) { 29 | 30 | 31 | 32 | //next window 33 | 34 | //removal 35 | while(!maxi.empty() && i - maxi.front() >=k) { 36 | maxi.pop_front(); 37 | } 38 | 39 | while(!mini.empty() && i - mini.front() >=k) { 40 | mini.pop_front(); 41 | } 42 | 43 | //addition 44 | 45 | while(!maxi.empty() && arr[maxi.back()] <= arr[i]) 46 | maxi.pop_back(); 47 | 48 | while(!mini.empty() && arr[mini.back()] >= arr[i]) 49 | mini.pop_back(); 50 | 51 | maxi.push_back(i); 52 | mini.push_back(i); 53 | 54 | ans += arr[maxi.front()] + arr[mini.front()]; 55 | } 56 | return ans; 57 | } 58 | 59 | int main() { 60 | 61 | 62 | int arr[7] = {2, 5, -1, 7, -3, -1, -2}; 63 | int k = 4; 64 | cout << solve(arr, 7, k) << endl; 65 | 66 | 67 | return 0; 68 | } -------------------------------------------------------------------------------- /Lecture062 Tree Introduction/countLeaf.cpp: -------------------------------------------------------------------------------- 1 | /********************************************************** 2 | 3 | Following is the Binary Tree Node class structure: 4 | 5 | template <typename T> 6 | class BinaryTreeNode { 7 | public : 8 | T data; 9 | BinaryTreeNode<T> *left; 10 | BinaryTreeNode<T> *right; 11 | 12 | BinaryTreeNode(T data) { 13 | this -> data = data; 14 | left = NULL; 15 | right = NULL; 16 | } 17 | }; 18 | 19 | ***********************************************************/ 20 | 21 | void inorder(BinaryTreeNode<int> * root, int &count) { 22 | //base case 23 | if(root == NULL) { 24 | return ; 25 | } 26 | 27 | inorder(root->left, count); 28 | 29 | //leaf node 30 | if(root->left == NULL && root->right == NULL) { 31 | count++; 32 | } 33 | 34 | inorder(root->right, count); 35 | 36 | } 37 | 38 | 39 | int noOfLeafNodes(BinaryTreeNode<int> *root){ 40 | int cnt = 0; 41 | inorder(root, cnt); 42 | return cnt; 43 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CodeHelp-DSA-Busted-Series 2 | Hello Jee, 3 | 4 | This repo is for the students for easy access to all the programs taught under Codehelp's DSA Busted Series. 5 | 6 | 7 | [DSA Busted Series](https://www.youtube.com/watch?v=WQoB2z67hvY&list=PLDzeHZWIZsTryvtXdMr6rPh4IDexB5NIA) 8 | 9 | 10 | Youtube Channel: CodeHelp - by Babbar 11 | 12 | 13 | Your Instructor: Love Babbar 14 | 15 | 16 | 17 | Learn a Lot, Enjoy a Lot. 18 | --------------------------------------------------------------------------------